7 static uchar noether[6];
10 * Look for a pair with the given attribute. look first on the same line,
11 * then in the whole entry.
14 lookval(Ndbtuple *entry, Ndbtuple *line, char *attr, char *to)
18 /* first look on same line (closer binding) */
20 if(strcmp(attr, nt->attr) == 0){
21 strncpy(to, nt->val, Ndbvlen);
28 /* search whole tuple */
29 for(nt = entry; nt; nt = nt->entry)
30 if(strcmp(attr, nt->attr) == 0){
31 strncpy(to, nt->val, Ndbvlen);
38 * lookup an ip address
41 lookupip(Ndb *db, char *name, uchar *to, Ipinfo *iip)
45 uchar subnet[IPaddrlen];
50 if(strcmp(attr, "ip") == 0){
55 t = ndbgetval(db, &s, attr, name, "ip", buf);
57 /* first look for match on same subnet */
58 for(nt = t; nt; nt = nt->entry){
59 if(strcmp(nt->attr, "ip") != 0)
62 maskip(to, iip->ipmask, subnet);
63 if(memcmp(subnet, iip->ipnet, sizeof(subnet)) == 0)
67 /* otherwise, just take what we have */
76 * lookup a subnet and fill in anything we can
79 recursesubnet(Ndb *db, uchar *mask, Ipinfo *iip, char *fs, char *gw, char *au)
83 uchar submask[IPaddrlen];
86 memmove(iip->ipmask, mask, 4);
87 maskip(iip->ipaddr, iip->ipmask, iip->ipnet);
88 sprint(ip, "%I", iip->ipnet);
89 t = ndbsearch(db, &s, "ip", ip);
92 /* look for a further subnet */
93 if(lookval(t, s.t, "ipmask", ip)){
96 /* recurse only if it has changed */
97 if(!equivip(submask, mask))
98 recursesubnet(db, submask, iip, fs, gw, au);
102 /* fill in what we don't have */
104 lookval(t, s.t, "ipgw", gw);
106 lookval(t, s.t, "fs", fs);
108 lookval(t, s.t, "auth", au);
115 * find out everything we can about a system from what has been
119 ipinfo(Ndb *db, char *etherin, char *ipin, char *name, Ipinfo *iip)
125 char fsname[Ndbvlen];
126 char gwname[Ndbvlen];
127 char auname[Ndbvlen];
129 memset(iip, 0, sizeof(Ipinfo));
135 * look for a matching entry
139 t = ndbgetval(db, &s, "ether", etherin, "ip", ip);
141 t = ndbsearch(db, &s, "ip", ipin);
143 t = ndbgetval(db, &s, ipattr(name), name, "ip", ip);
146 * copy in addresses and name
148 if(lookval(t, s.t, "ip", ip))
149 parseip(iip->ipaddr, ip);
150 if(lookval(t, s.t, "ether", ether))
151 parseether(iip->etheraddr, ether);
152 lookval(t, s.t, "dom", iip->domain);
155 * Look for bootfile, fs, and gateway.
156 * If necessary, search through all entries for
160 if(iip->bootf[0] == 0)
161 lookval(t, s.t, "bootf", iip->bootf);
163 lookval(t, s.t, "fs", fsname);
165 lookval(t, s.t, "ipgw", gwname);
167 lookval(t, s.t, "auth", auname);
169 if(iip->bootf[0] && fsname[0] && gwname[0] && auname[0])
171 t = ndbsnext(&s, "ether", ether);
175 * copy in addresses (all we know)
177 parseip(iip->ipaddr, ipin);
179 parseether(iip->etheraddr, etherin);
184 * Look up the client's network and find a subnet mask for it.
185 * Fill in from the subnet (or net) entry anything we can't figure
186 * out from the client record.
188 recursesubnet(db, classmask[CLASS(iip->ipaddr)], iip, fsname, gwname, auname);
190 /* lookup fs's and gw's ip addresses */
193 lookupip(db, fsname, iip->fsip, iip);
195 lookupip(db, gwname, iip->gwip, iip);
197 lookupip(db, auname, iip->auip, iip);
202 main(int argc, char **argv)
209 fmtinstall('E', eipconv);
210 fmtinstall('I', eipconv);
213 if(strchr(argv[1], '.')){
214 if(ipinfo(db, 0, argv[1], 0, &ii) < 0)
217 if(ipinfo(db, argv[1], 0, 0, &ii) < 0)
220 fprint(2, "a %I m %I n %I f %s e %E\n", ii.ipaddr,
221 ii.ipmask, ii.ipnet, ii.bootf, ii.etheraddr);