From 9ea93a5fd347a04ed409c46410cd5ead033c0695 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Fri, 17 Jul 2020 16:53:20 +0200 Subject: libndb: order subnets by prefix length for ndbipinfo() lookups to reproduce: ipnet=foo0 ip=192.168.0.0 ipmask=/16 ipnet=foo1 ip=192.168.0.0 ipmask=/24 ip=192.168.0.1 sys=foo2 % ndb/ipquery sys foo2 ipnet ipmask ipnet=foo0 ipmask=/16 we would expect to get ipnet=foo1 here as it is more specific subnet. the solution is to order the subnets by prefix length in subnet() before calling filter(), so that we process the longest prefixes first. --- sys/src/libndb/ndbipinfo.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'sys/src') diff --git a/sys/src/libndb/ndbipinfo.c b/sys/src/libndb/ndbipinfo.c index 5ee706e55..71092d4bc 100644 --- a/sys/src/libndb/ndbipinfo.c +++ b/sys/src/libndb/ndbipinfo.c @@ -114,17 +114,18 @@ prefixlen(uchar *ip) } /* - * look through a containing subset + * look through containing subsets */ static Ndbtuple* subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix) { Ndbs s; + int nprefix; char netstr[64]; uchar mask[IPaddrlen]; - Ndbtuple *t, *nt, *xt; + Ndbtuple *at[128+1], *nt, *xt, *t; - t = nil; + memset(at, 0, sizeof(at)); snprint(netstr, sizeof(netstr), "%I", net); nt = ndbsearch(db, &s, "ip", netstr); while(nt != nil){ @@ -133,14 +134,21 @@ subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix) xt = ndbfindattr(nt, nt, "ipmask"); if(xt == nil || parseipmask(mask, xt->val, isv4(net)) == -1) ipmove(mask, defmask(net)); - if(prefixlen(mask) <= prefix){ - t = ndbconcatenate(t, filter(db, nt, f)); + nprefix = prefixlen(mask); + if(nprefix <= prefix && at[nprefix] == nil){ + /* remember containing subnet, order by prefix length */ + at[nprefix] = nt; nt = nil; } } ndbfree(nt); nt = ndbsnext(&s, "ip", netstr); } + /* filter subnets, longest prefix first */ + for(t = nil; prefix >= 0; prefix--){ + if(at[prefix] != nil) + t = ndbconcatenate(t, filter(db, at[prefix], f)); + } ndbsetmalloctag(t, getcallerpc(&db)); return t; } -- cgit v1.2.3