/* * interface to ndbipinfo */ Ndbtuple* lookupinfo(uchar *ipaddr, char **attr, int n) { char ip[32]; sprint(ip, "%I", ipaddr); return ndbipinfo(db, "ip", ip, attr, n); }
void search(Ndb *db, char *attr, char *val, char **rattr, int nrattr) { Ndbtuple *t, *tt; tt = ndbipinfo(db, attr, val, rattr, nrattr); for(t = tt; t; t = t->entry) print("%s=%s ", t->attr, t->val); print("\n"); ndbfree(tt); }
/* * lookup an ip attribute */ static int ipattrlookup(struct ndb *db, char *ipa, char *attr, char *val, int vlen) { struct ndbtuple *t, *nt; char *alist[2]; alist[0] = attr; t = ndbipinfo(db, "ip", ipa, alist, 1); if (t == NULL) return 0; for (nt = t; nt != NULL; nt = nt->entry) { if (strcmp(nt->attr, attr) == 0) { strlcpy(val, nt->val, vlen); ndbfree(t); return 1; } } /* we shouldn't get here */ ndbfree(t); return 0; }
static char *ipinfoquery(struct mfile *mf, char **list, int n) { int i, nresolve; int resolve[Maxattr]; struct ndbtuple *t, *nt, **l; char *attr, *val; /* skip 'ipinfo' */ list++; n--; if (n < 1) return "bad query"; /* get search attribute=value, or assume ip=myipaddr */ attr = *list; val = strchr(attr, '='); if (val != NULL) { *val++ = 0; list++; n--; } else { attr = "ip"; val = ipaddr; } if (n < 1) return "bad query"; /* * don't let ndbipinfo resolve the addresses, we're * better at it. */ nresolve = 0; for (i = 0; i < n; i++) if (*list[i] == '@') { /* @attr=val ? */ list[i]++; resolve[i] = 1; /* we'll resolve it */ nresolve++; } else resolve[i] = 0; t = ndbipinfo(db, attr, val, list, n); if (t == NULL) return "no match"; if (nresolve != 0) { for (l = &t; *l != NULL;) { nt = *l; /* already an address? */ if (strcmp(ipattr(nt->val), "ip") == 0) { l = &(*l)->entry; continue; } /* user wants it resolved? */ for (i = 0; i < n; i++) if (strcmp(list[i], nt->attr) == 0) break; if (i >= n || resolve[i] == 0) { l = &(*l)->entry; continue; } /* resolve address and replace entry */ *l = ipresolve(nt->attr, nt->val); while (*l != NULL) l = &(*l)->entry; *l = nt->entry; nt->entry = NULL; ndbfree(nt); } } /* make it all one line */ for (nt = t; nt != NULL; nt = nt->entry) { if (nt->entry == NULL) nt->line = t; else nt->line = nt->entry; } qreply(mf, t); return NULL; }
/* * do an ipinfo with defaults */ int lookupip(uchar *ipaddr, Info *iip, int gate) { char ip[32]; Ndbtuple *t, *nt; char *attrs[32], **p; if(opendb() == nil){ warning(1, "can't open db"); return -1; } p = attrs; *p++ = "ip"; *p++ = "ipmask"; *p++ = "@ipgw"; if(!gate){ *p++ = "bootf"; *p++ = "bootf2"; *p++ = "@tftp"; *p++ = "@tftp2"; *p++ = "rootpath"; *p++ = "dhcp"; *p++ = "vendorclass"; *p++ = "ether"; *p++ = "dom"; *p++ = "@fs"; *p++ = "@auth"; } *p = 0; memset(iip, 0, sizeof(*iip)); snprint(ip, sizeof(ip), "%I", ipaddr); t = ndbipinfo(db, "ip", ip, attrs, p - attrs); if(t == nil) return -1; for(nt = t; nt != nil; nt = nt->entry){ if(strcmp(nt->attr, "ip") == 0) setipaddr(iip->ipaddr, nt->val); else if(strcmp(nt->attr, "ipmask") == 0) setipmask(iip->ipmask, nt->val); else if(strcmp(nt->attr, "fs") == 0) setipaddr(iip->fsip, nt->val); else if(strcmp(nt->attr, "auth") == 0) setipaddr(iip->auip, nt->val); else if(strcmp(nt->attr, "tftp") == 0) setipaddr(iip->tftp, nt->val); else if(strcmp(nt->attr, "tftp2") == 0) setipaddr(iip->tftp2, nt->val); else if(strcmp(nt->attr, "ipgw") == 0) setipaddr(iip->gwip, nt->val); else if(strcmp(nt->attr, "ether") == 0){ /* * this is probably wrong for machines with multiple * ethers. bootp or dhcp requests could come from any * of the ethers listed in the ndb entry. */ if(memcmp(iip->etheraddr, noetheraddr, 6) == 0) parseether(iip->etheraddr, nt->val); iip->indb = 1; } else if(strcmp(nt->attr, "dhcp") == 0){ if(iip->dhcpgroup[0] == 0) strcpy(iip->dhcpgroup, nt->val); } else if(strcmp(nt->attr, "bootf") == 0){ if(iip->bootf[0] == 0) strcpy(iip->bootf, nt->val); } else if(strcmp(nt->attr, "bootf2") == 0){ if(iip->bootf2[0] == 0) strcpy(iip->bootf2, nt->val); } else if(strcmp(nt->attr, "vendor") == 0){ if(iip->vendor[0] == 0) strcpy(iip->vendor, nt->val); } else if(strcmp(nt->attr, "dom") == 0){ if(iip->domain[0] == 0) strcpy(iip->domain, nt->val); } else if(strcmp(nt->attr, "rootpath") == 0){ if(iip->rootpath[0] == 0) strcpy(iip->rootpath, nt->val); } } ndbfree(t); maskip(iip->ipaddr, iip->ipmask, iip->ipnet); return 0; }