14 static Ndbtuple* filter(Ndb *db, Ndbtuple *t, Ndbtuple *f);
15 static Ndbtuple* mkfilter(int argc, char **argv);
16 static int filtercomplete(Ndbtuple *f);
17 static int prefixlen(uchar *ip);
18 static Ndbtuple* subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix);
20 /* make a filter to be used in filter */
22 mkfilter(int argc, char **argv)
24 Ndbtuple *t, *first, *last;
36 if(*p == '@'){ /* @attr=val ? */
37 t->ptr |= Faddr; /* return resolved address(es) */
40 strncpy(t->attr, p, sizeof(t->attr)-1);
42 ndbsetmalloctag(first, getcallerpc(&argc));
46 /* return true if every pair of filter has been used */
48 filtercomplete(Ndbtuple *f)
50 for(; f; f = f->entry)
51 if((f->ptr & Fignore) == 0)
56 /* set the attribute of all entries in a tuple */
58 setattr(Ndbtuple *t, char *attr)
62 for(nt = t; nt; nt = nt->entry)
63 strcpy(nt->attr, attr);
68 * return only the attr/value pairs in t maching the filter, f.
69 * others are freed. line structure is preserved.
72 filter(Ndb *db, Ndbtuple *t, Ndbtuple *f)
74 Ndbtuple *nt, *nf, *next;
76 /* filter out what we don't want */
77 for(nt = t; nt; nt = next){
80 /* look through filter */
81 for(nf = f; nf != nil; nf = nf->entry){
82 if(!(nf->ptr&Fignore) && strcmp(nt->attr, nf->attr) == 0)
86 /* remove nt from t */
87 t = ndbdiscard(t, nt);
90 t = ndbsubstitute(t, nt, setattr(ndbgetipaddr(db, nt->val), nt->attr));
95 /* remember filter etnries that matched */
96 for(nf = f; nf != nil; nf = nf->entry)
98 nf->ptr = (nf->ptr & ~Ffound) | Fignore;
100 ndbsetmalloctag(t, getcallerpc(&db));
109 for(y = IPaddrlen-1; y >= 0; y--)
110 for(i = 8; i > 0; i--)
111 if(ip[y] & (1<<(8-i)))
117 * look through a containing subset
120 subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix)
123 Ndbtuple *t, *nt, *xt;
125 uchar mask[IPaddrlen];
129 sprint(netstr, "%I", net);
130 nt = ndbsearch(db, &s, "ip", netstr);
132 xt = ndbfindattr(nt, nt, "ipnet");
134 xt = ndbfindattr(nt, nt, "ipmask");
136 parseipmask(mask, xt->val);
138 ipmove(mask, defmask(net));
139 masklen = prefixlen(mask);
140 if(masklen <= prefix){
141 t = ndbconcatenate(t, filter(db, nt, f));
146 nt = ndbsnext(&s, "ip", netstr);
148 ndbsetmalloctag(t, getcallerpc(&db));
153 * fill in all the requested attributes for a system.
154 * if the system's entry doesn't have all required,
155 * walk through successively more inclusive networks
156 * for inherited attributes.
159 ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n)
161 Ndbtuple *t, *nt, *f;
164 uchar net[IPaddrlen], ip[IPaddrlen];
165 int prefix, smallestprefix, force;
169 fmtinstall('I', eipfmt);
170 fmtinstall('M', eipfmt);
172 /* get needed attributes */
173 f = mkfilter(n, alist);
176 * first look for a matching entry with an ip address
179 ipstr = ndbgetvalue(db, &s, attr, val, "ip", &nt);
181 /* none found, make one up */
182 if(strcmp(attr, "ip") != 0) {
186 t = ndbnew("ip", val);
189 r = parseip(net, val);
195 nt = ndbreorder(nt, s.t);
196 t = ndbconcatenate(t, nt);
197 nt = ndbsnext(&s, attr, val);
199 r = parseip(net, ipstr);
207 t = filter(db, t, f);
210 * now go through subnets to fill in any missing attributes
214 smallestprefix = 100;
217 /* in v6, the last 8 bytes have no structure (we hope) */
225 * to find a containing network, keep turning off
226 * the lower bit and look for a network with
227 * that address and a shorter mask. tedius but
228 * complete, we may need to find a trick to speed this up.
230 for(; prefix >= smallestprefix; prefix--){
231 if(filtercomplete(f))
233 if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0)
236 net[prefix/8] &= ~(1<<(7-(prefix%8)));
237 t = ndbconcatenate(t, subnet(db, net, f, prefix));
241 * if there's an unfulfilled ipmask, make one up
243 nt = ndbfindattr(f, f, "ipmask");
244 if(nt && !(nt->ptr & Fignore)){
247 snprint(x, sizeof(x), "%M", defmask(ip));
248 t = ndbconcatenate(t, ndbnew("ipmask", x));
252 ndbsetmalloctag(t, getcallerpc(&db));