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;
40 strncpy(t->attr, p, sizeof(t->attr)-1);
45 /* return true if every pair of filter has been used */
47 filtercomplete(Ndbtuple *f)
49 for(; f; f = f->entry)
50 if((f->ptr & Fignore) == 0)
55 /* set the attribute of all entries in a tuple */
57 setattr(Ndbtuple *t, char *attr)
61 for(nt = t; nt; nt = nt->entry)
62 strcpy(nt->attr, attr);
67 * return only the attr/value pairs in t maching the filter, f.
68 * others are freed. line structure is preserved.
71 filter(Ndb *db, Ndbtuple *t, Ndbtuple *f)
73 Ndbtuple *nt, *nf, *next;
75 /* filter out what we don't want */
76 for(nt = t; nt; nt = next){
79 /* look through filter */
80 for(nf = f; nf != nil; nf = nf->entry){
81 if(!(nf->ptr&Fignore) && strcmp(nt->attr, nf->attr) == 0)
85 /* remove nt from t */
86 t = ndbdiscard(t, nt);
89 t = ndbsubstitute(t, nt, setattr(ndbgetipaddr(db, nt->val), nt->attr));
94 /* remember filter etnries that matched */
95 for(nf = f; nf != nil; nf = nf->entry)
97 nf->ptr = (nf->ptr & ~Ffound) | Fignore;
107 for(y = IPaddrlen-1; y >= 0; y--)
108 for(i = 8; i > 0; i--)
109 if(ip[y] & (1<<(8-i)))
115 * look through a containing subset
118 subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix)
121 Ndbtuple *t, *nt, *xt;
123 uchar mask[IPaddrlen];
127 sprint(netstr, "%I", net);
128 nt = ndbsearch(db, &s, "ip", netstr);
130 xt = ndbfindattr(nt, nt, "ipnet");
132 xt = ndbfindattr(nt, nt, "ipmask");
134 parseipmask(mask, xt->val);
136 ipmove(mask, defmask(net));
137 masklen = prefixlen(mask);
138 if(masklen <= prefix)
139 t = ndbconcatenate(t, filter(db, nt, f));
142 nt = ndbsnext(&s, "ip", netstr);
148 * fill in all the requested attributes for a system.
149 * if the system's entry doesn't have all required,
150 * walk through successively more inclusive networks
151 * for inherited attributes.
154 ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n)
156 Ndbtuple *t, *nt, *f;
159 uchar net[IPaddrlen];
161 int prefix, smallestprefix;
165 fmtinstall('I', eipfmt);
166 fmtinstall('M', eipfmt);
168 /* get needed attributes */
169 f = mkfilter(n, alist);
172 * first look for a matching entry with an ip address
175 ipstr = ndbgetvalue(db, &s, attr, val, "ip", &nt);
177 /* none found, make one up */
178 if(strcmp(attr, "ip") != 0)
180 t = ndbnew("ip", val);
187 nt = ndbreorder(nt, s.t);
188 t = ndbconcatenate(t, nt);
189 nt = ndbsnext(&s, attr, val);
195 t = filter(db, t, f);
198 * now go through subnets to fill in any missing attributes
202 smallestprefix = 100;
205 /* in v6, the last 8 bytes have no structure (we hope) */
213 * to find a containing network, keep turning off
214 * the lower bit and look for a network with
215 * that address and a shorter mask. tedius but
216 * complete, we may need to find a trick to speed this up.
218 for(; prefix >= smallestprefix; prefix--){
219 if(filtercomplete(f))
221 if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0)
224 net[prefix/8] &= ~(1<<(7-(prefix%8)));
225 t = ndbconcatenate(t, subnet(db, net, f, prefix));
229 * if there's an unfulfilled ipmask, make one up
231 nt = ndbfindattr(f, f, "ipmask");
232 if(nt && !(nt->ptr & Fignore)){
235 snprint(x, sizeof(x), "%M", defmask(ip));
236 t = ndbconcatenate(t, ndbnew("ipmask", x));