9front - general discussion about 9front
 help / color / mirror / Atom feed
From: Aidan K. Wiggins <akw@oneiri.one>
To: 9front@9front.org
Subject: [9front] [PATCH] libndb/ndbipinfo.c: filter out irrelevant entries if attr is "ip"
Date: Fri, 05 Aug 2022 13:42:00 -0700	[thread overview]
Message-ID: <3A4632FC098F0B4FFA173525231E1FBC@oneiri.one> (raw)

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));

                 reply	other threads:[~2022-08-05 20:43 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3A4632FC098F0B4FFA173525231E1FBC@oneiri.one \
    --to=akw@oneiri.one \
    --cc=9front@9front.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).