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 Ndbtuple* toipaddr(Ndb *db, Ndbtuple *t);
18 static int prefixlen(uchar *ip);
19 static Ndbtuple* subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix);
21 /* make a filter to be used in filter */
23 mkfilter(int argc, char **argv)
25 Ndbtuple *t, *first, *last;
37 if(*p == '@'){ /* @attr=val ? */
38 t->ptr |= Faddr; /* return resolved address(es) */
41 strncpy(t->attr, p, sizeof(t->attr)-1);
43 ndbsetmalloctag(first, getcallerpc(&argc));
47 /* return true if every pair of filter has been used */
49 filtercomplete(Ndbtuple *f)
51 for(; f; f = f->entry)
52 if((f->ptr & Fignore) == 0)
57 /* set the attribute of all entries in a tuple */
59 setattr(Ndbtuple *t, char *attr)
63 for(nt = t; nt; nt = nt->entry)
64 strcpy(nt->attr, attr);
69 * return only the attr/value pairs in t maching the filter, f.
70 * others are freed. line structure is preserved.
73 filter(Ndb *db, Ndbtuple *t, Ndbtuple *f)
75 Ndbtuple *nt, *nf, *next;
77 /* filter out what we don't want */
78 for(nt = t; nt; nt = next){
81 /* look through filter */
82 for(nf = f; nf != nil; nf = nf->entry){
83 if(!(nf->ptr&Fignore) && strcmp(nt->attr, nf->attr) == 0)
87 /* remove nt from t */
88 t = ndbdiscard(t, nt);
91 t = ndbsubstitute(t, nt, setattr(ndbgetipaddr(db, nt->val), nt->attr));
96 /* remember filter etnries that matched */
97 for(nf = f; nf != nil; nf = nf->entry)
99 nf->ptr = (nf->ptr & ~Ffound) | Fignore;
101 ndbsetmalloctag(t, getcallerpc(&db));
110 for(y = IPaddrlen-1; y >= 0; y--)
111 for(i = 8; i > 0; i--)
112 if(ip[y] & (1<<(8-i)))
118 * look through a containing subset
121 subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix)
124 Ndbtuple *t, *nt, *xt;
126 uchar mask[IPaddrlen];
130 sprint(netstr, "%I", net);
131 nt = ndbsearch(db, &s, "ip", netstr);
133 xt = ndbfindattr(nt, nt, "ipnet");
135 xt = ndbfindattr(nt, nt, "ipmask");
137 parseipmask(mask, xt->val);
139 ipmove(mask, defmask(net));
140 masklen = prefixlen(mask);
141 if(masklen <= prefix){
142 t = ndbconcatenate(t, filter(db, nt, f));
147 nt = ndbsnext(&s, "ip", netstr);
149 ndbsetmalloctag(t, getcallerpc(&db));
154 * fill in all the requested attributes for a system.
155 * if the system's entry doesn't have all required,
156 * walk through successively more inclusive networks
157 * for inherited attributes.
160 ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n)
162 Ndbtuple *t, *nt, *f;
165 uchar net[IPaddrlen], ip[IPaddrlen];
166 int prefix, smallestprefix, force;
170 fmtinstall('I', eipfmt);
171 fmtinstall('M', eipfmt);
173 /* get needed attributes */
174 f = mkfilter(n, alist);
177 * first look for a matching entry with an ip address
180 ipstr = ndbgetvalue(db, &s, attr, val, "ip", &nt);
182 /* none found, make one up */
183 if(strcmp(attr, "ip") != 0) {
187 t = ndbnew("ip", val);
190 r = parseip(net, val);
196 nt = ndbreorder(nt, s.t);
197 t = ndbconcatenate(t, nt);
198 nt = ndbsnext(&s, attr, val);
200 r = parseip(net, ipstr);
208 t = filter(db, t, f);
211 * now go through subnets to fill in any missing attributes
215 smallestprefix = 100;
218 /* in v6, the last 8 bytes have no structure (we hope) */
226 * to find a containing network, keep turning off
227 * the lower bit and look for a network with
228 * that address and a shorter mask. tedius but
229 * complete, we may need to find a trick to speed this up.
231 for(; prefix >= smallestprefix; prefix--){
232 if(filtercomplete(f))
234 if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0)
237 net[prefix/8] &= ~(1<<(7-(prefix%8)));
238 t = ndbconcatenate(t, subnet(db, net, f, prefix));
242 * if there's an unfulfilled ipmask, make one up
244 nt = ndbfindattr(f, f, "ipmask");
245 if(nt && !(nt->ptr & Fignore)){
248 snprint(x, sizeof(x), "%M", defmask(ip));
249 t = ndbconcatenate(t, ndbnew("ipmask", x));
253 ndbsetmalloctag(t, getcallerpc(&db));