* [9front] [PATCH] libndb/ndbipinfo.c: filter out irrelevant entries if attr is "ip"
@ 2022-08-05 20:42 Aidan K. Wiggins
0 siblings, 0 replies; only message in thread
From: Aidan K. Wiggins @ 2022-08-05 20:42 UTC (permalink / raw)
To: 9front
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));
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2022-08-05 20:43 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-05 20:42 [9front] [PATCH] libndb/ndbipinfo.c: filter out irrelevant entries if attr is "ip" Aidan K. Wiggins
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).