From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=1.0 required=5.0 tests=FROM_FMBLA_NEWDOM14 autolearn=no autolearn_force=no version=3.4.4 Received: (qmail 25206 invoked from network); 5 Aug 2022 20:43:46 -0000 Received: from 9front.inri.net (168.235.81.73) by inbox.vuxu.org with ESMTPUTF8; 5 Aug 2022 20:43:46 -0000 Received: from oneiri.one ([170.39.227.229]) by 9front; Fri Aug 5 16:42:10 -0400 2022 Message-ID: <3A4632FC098F0B4FFA173525231E1FBC@oneiri.one> From: Aidan K. Wiggins Date: Fri, 05 Aug 2022 13:42:00 -0700 To: 9front@9front.org MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit List-ID: <9front.9front.org> List-Help: X-Glyph: ➈ X-Bullshit: enhancement-scale HTML over JSON just-in-time self-signing control Subject: [9front] [PATCH] libndb/ndbipinfo.c: filter out irrelevant entries if attr is "ip" Reply-To: 9front@9front.org Precedence: bulk Currently, if we have a /lib/ndb/local configuration alike to: ipnet=net ip=1:2:3:4:: ipmask=/64 ipgw=fe80::1 ip=9.9.9.0 ipmask=255.255.255.0 ipgw=9.9.9.1 sys=host ether=aabbccddeeff ip=1:2:3:4::1 ip=9.9.9.9 Calling 'ndbipinfo(db, "ip", "9.9.9.9", &attrs, 2)' where attrs is {"ipmask", "ipgw"}, will return the values for the ipv6 address instead of the desired values on the ipv4 line. This behavior can be seen in 'ip/ipconfig -N', where it will fail for whichever address comes second under the ipnet. diff 70cc0a7b44ad70929a00565b03327296d2e64c52 uncommitted --- a//sys/src/libndb/ndbipinfo.c +++ b//sys/src/libndb/ndbipinfo.c @@ -12,11 +12,14 @@ }; static Ndbtuple* filter(Ndb *db, Ndbtuple *t, Ndbtuple *f); +static Ndbtuple* filternet(Ndb *db, Ndbtuple *t, char *net); static Ndbtuple* mkfilter(int argc, char **argv); static int filtercomplete(Ndbtuple *f); static int prefixlen(uchar *ip); static Ndbtuple* subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix); +static int attrisip = 0; /* to filternet() or not */ + /* make a filter to be used in filter */ static Ndbtuple* mkfilter(int argc, char **argv) @@ -92,7 +95,7 @@ } } - /* remember filter etnries that matched */ + /* remember filter entries that matched */ for(nf = f; nf != nil; nf = nf->entry) if(nf->ptr & Ffound) nf->ptr = (nf->ptr & ~Ffound) | Fignore; @@ -101,6 +104,36 @@ return t; } +/* + * return only entries pertaining to net. + */ +static Ndbtuple* +filternet(Ndb *db, Ndbtuple *t, char *net) +{ + Ndbtuple *nt, *xt, *next; + int i; + + for(nt = t; nt; nt = next){ + next = nt->entry; + xt = ndbfindattr(nil, nt, "ip"); + if(xt && xt->val != nil && strcmp(xt->val, net) != 0){ + /* walk through line and filter out ip specific attrs */ + for(i = 0, xt = nt->line; xt != nt; xt = xt->line, i++) + ; + while(i-- >= 0){ + if(strcmp(nt->attr, "ip") == 0 || + strcmp(nt->attr, "ipmask") == 0 || + strcmp(nt->attr, "ipgw") == 0) + t = ndbdiscard(t, nt); + nt = next; + next = nt->entry; + } + } + } + ndbsetmalloctag(t, getcallerpc(&db)); + return t; +} + static int prefixlen(uchar *ip) { @@ -131,6 +164,8 @@ while(nt != nil){ xt = ndbfindattr(nt, nt, "ipnet"); if(xt != nil){ + if(attrisip) + nt = filternet(db, nt, netstr); xt = ndbfindattr(nt, nt, "ipmask"); if(xt == nil || parseipmask(mask, xt->val, isv4(net)) == -1) ipmove(mask, defmask(net)); @@ -246,7 +281,9 @@ if(ipstr == nil){ /* none found, make one up */ if(strcmp(attr, "ip") != 0) - return nil; + return nil; + else + attrisip = 1; nt = ndbnew("ip", val); nt->line = nt; nt->entry = nil; @@ -253,8 +290,12 @@ t = netinfo(db, nt, alist, n); } else { /* found one */ - free(ipstr); t = nil; + if(strcmp(attr, "ip") == 0){ + nt = filternet(db, nt, ipstr); + attrisip = 1; + } + free(ipstr); do { nt = ndbreorder(nt, s.t); t = ndbconcatenate(t, netinfo(db, nt, alist, n));