18 Maxhost= 64, /* maximum host name size */
19 Maxservice= 64, /* maximum service name size */
25 typedef struct Mfile Mfile;
26 typedef struct Mlist Mlist;
27 typedef struct Network Network;
28 typedef struct Flushreq Flushreq;
29 typedef struct Job Job;
31 int vers; /* incremented each clone/attach */
35 int busy; /* fid in use */
36 int ref; /* cleanup when drops to zero */
51 * result of the last lookup
84 jmp_buf masterjmp; /* return through here after a slave process has been created */
85 int *isslave; /* *isslave non-zero means this is a slave process */
91 void rattach(Job*, Mfile*);
92 char* rwalk(Job*, Mfile*);
93 void ropen(Job*, Mfile*);
94 void rcreate(Job*, Mfile*);
95 void rread(Job*, Mfile*);
96 void rwrite(Job*, Mfile*);
97 void rclunk(Job*, Mfile*);
98 void rremove(Job*, Mfile*);
99 void rstat(Job*, Mfile*);
100 void rwstat(Job*, Mfile*);
102 void sendmsg(Job*, char*);
104 void mountinit(char*, char*);
109 char *genquery(Mfile*, char*);
110 char* ipinfoquery(Mfile*, char**, int);
111 int needproto(Network*, Ndbtuple*);
113 Ndbtuple* reorder(Ndbtuple*, Ndbtuple*);
115 void readipinterfaces(void);
117 char* estrdup(char*);
120 void setext(char*, int, char*);
121 void cleanmf(Mfile*);
123 extern void paralloc(void);
125 QLock dblock; /* mutex on database operations */
126 QLock netlock; /* mutex for netinit() */
128 char *logfile = "cs";
129 char *paranoiafile = "cs.paranoia";
132 char netndb[Maxpath];
135 * Network specific translators
137 Ndbtuple* iplookup(Network*, char*, char*, int);
138 char* iptrans(Ndbtuple*, Network*, char*, char*, int);
139 Ndbtuple* telcolookup(Network*, char*, char*, int);
140 char* telcotrans(Ndbtuple*, Network*, char*, char*, int);
141 Ndbtuple* dnsiplookup(char*, Ndbs*);
146 Ndbtuple *(*lookup)(Network*, char*, char*, int);
147 char *(*trans)(Ndbtuple*, Network*, char*, char*, int);
148 int considered; /* flag: ignored for "net!"? */
149 int fasttimeouthack; /* flag. was for IL */
159 * net doesn't apply to (r)udp, icmp(v6), or telco (for speed).
161 Network network[] = {
162 [Ntcp] { "tcp", iplookup, iptrans, 0 },
163 { "udp", iplookup, iptrans, 1 },
164 { "icmp", iplookup, iptrans, 1 },
165 { "icmpv6", iplookup, iptrans, 1 },
166 { "rudp", iplookup, iptrans, 1 },
167 { "ssh", iplookup, iptrans, 1 },
168 { "telco", telcolookup, telcotrans, 1 },
175 char eaddr[16]; /* ascii ethernet address */
176 char ipaddr[64]; /* ascii internet address */
177 uchar ipa[IPaddrlen]; /* binary internet address */
180 Network *netlist; /* networks ordered by preference */
184 nstrcpy(char *to, char *from, int len)
186 strncpy(to, from, len);
193 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
198 * based on libthread's threadsetname, but drags in less library code.
199 * actually just sets the arguments displayed.
202 procsetname(char *fmt, ...)
210 cmdname = vsmprint(fmt, arg);
214 snprint(buf, sizeof buf, "#p/%d/args", getpid());
215 if((fd = open(buf, OWRITE)) >= 0){
216 write(fd, cmdname, strlen(cmdname)+1);
223 main(int argc, char *argv[])
226 char ext[Maxpath], servefile[Maxpath];
229 setnetmtpt(mntpt, sizeof(mntpt), nil);
239 dbfile = EARGF(usage());
245 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
246 setext(ext, sizeof(ext), mntpt);
252 rfork(RFREND|RFNOTEG);
254 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
255 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
256 unmount(servefile, mntpt);
259 fmtinstall('E', eipfmt);
260 fmtinstall('I', eipfmt);
261 fmtinstall('M', eipfmt);
262 fmtinstall('F', fcallfmt);
268 mountinit(servefile, mntpt);
275 * if a mount point is specified, set the cs extention to be the mount point
276 * with '_'s replacing '/'s
279 setext(char *ext, int n, char *p)
284 for(i = 0; i < n; i++){
296 mountinit(char *service, char *mntpt)
303 error("pipe failed");
308 f = create(service, OWRITE|ORCLOSE, 0666);
311 snprint(buf, sizeof(buf), "%d", p[1]);
312 if(write(f, buf, strlen(buf)) != strlen(buf))
313 error("write /srv/cs");
315 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
318 procsetname("%s", mntpt);
321 error("fork failed\n");
324 * put ourselves into the file system
327 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
328 error("mount failed\n");
331 mfd[0] = mfd[1] = p[0];
337 db = ndbopen(dbfile);
339 error("can't open network database");
341 for(netdb = db; netdb; netdb = netdb->next)
342 if(strcmp(netdb->file, netndb) == 0)
345 netdb = ndbopen(netndb);
348 db = ndbcat(netdb, db);
359 for(f = mlist; f; f = f->next)
360 if(f->mf.busy && f->mf.fid == fid)
362 else if(!ff && !f->mf.busy && !f->mf.ref)
365 ff = emalloc(sizeof *f);
370 memset(mf, 0, sizeof *mf);
380 job = mallocz(sizeof(Job), 1);
384 job->request.tag = -1;
395 for(l = &joblist; *l; l = &(*l)->next){
411 for(job = joblist; job; job = job->next){
412 if(job->request.tag == tag && job->request.type != Tflush){
426 uchar mdata[IOHDRSZ + Maxfdata];
430 * if we ask dns to fulfill requests,
431 * a slave process is created to wait for replies. The
432 * master process returns immediately via a longjmp
433 * through 'masterjmp'.
435 * *isslave is a pointer into the call stack to a variable
436 * that tells whether or not the current process is a slave.
438 slaveflag = 0; /* init slave variable */
439 isslave = &slaveflag;
443 n = read9pmsg(mfd[0], mdata, sizeof mdata);
447 if(convM2S(mdata, n, &job->request) != n){
448 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
449 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
454 mf = newfid(job->request.fid);
456 syslog(0, logfile, "%F", &job->request);
459 switch(job->request.type){
461 syslog(1, logfile, "unknown request type %d", job->request.type);
508 * slave processes die after replying
512 syslog(0, logfile, "slave death %d", getpid());
521 if(job->request.msize > IOHDRSZ + Maxfdata)
522 job->reply.msize = IOHDRSZ + Maxfdata;
524 job->reply.msize = job->request.msize;
525 if(strncmp(job->request.version, "9P2000", 6) != 0)
526 sendmsg(job, "unknown 9P version");
528 job->reply.version = "9P2000";
536 sendmsg(job, "cs: authentication not required");
540 * don't flush till all the slaves are done
545 flushjob(job->request.oldtag);
550 rattach(Job *job, Mfile *mf)
554 mf->user = estrdup(job->request.uname);
556 mf->qid.vers = vers++;
557 mf->qid.type = QTDIR;
559 job->reply.qid = mf->qid;
565 rwalk(Job *job, Mfile *mf)
576 elems = job->request.wname;
577 nelems = job->request.nwname;
578 job->reply.nwqid = 0;
580 if(job->request.newfid != job->request.fid){
582 nmf = newfid(job->request.newfid);
585 err = "clone to used channel";
589 nmf->user = estrdup(mf->user);
590 nmf->fid = job->request.newfid;
591 nmf->qid.vers = vers++;
594 /* else nmf will be nil */
599 for(i=0; i<nelems && i<MAXWELEM; i++){
600 if((qid.type & QTDIR) == 0){
601 err = "not a directory";
604 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
608 job->reply.wqid[i] = qid;
612 if(strcmp(elems[i], "cs") == 0){
617 err = "file does not exist";
623 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
637 ropen(Job *job, Mfile *mf)
643 mode = job->request.mode;
644 if(mf->qid.type & QTDIR){
646 err = "permission denied";
648 job->reply.qid = mf->qid;
649 job->reply.iounit = 0;
654 rcreate(Job *job, Mfile *mf)
657 sendmsg(job, "creation permission denied");
661 rread(Job *job, Mfile *mf)
664 long off, toff, clock;
671 off = job->request.offset;
672 cnt = job->request.count;
675 if(mf->qid.type & QTDIR){
678 memset(&dir, 0, sizeof dir);
680 dir.qid.type = QTFILE;
688 dir.atime = clock; /* wrong */
689 dir.mtime = clock; /* wrong */
690 n = convD2M(&dir, buf, sizeof buf);
692 job->reply.data = (char*)buf;
697 /* look for an answer at the right offset */
699 for(i = 0; mf->reply[i] && i < mf->nreply; i++){
706 break; /* got something to return */
708 /* try looking up more answers */
709 if(lookup(mf) == 0 || job->flushed){
716 /* give back a single reply (or part of one) */
717 job->reply.data = mf->reply[i] + (off - toff);
718 if(cnt > toff - off + n)
724 job->reply.count = n;
727 if(--mf->ref == 0 && mf->busy == 0)
752 for(i = 0; i < mf->nreply; i++){
758 mf->nextnet = netlist;
762 rwrite(Job *job, Mfile *mf)
770 cnt = job->request.count;
771 if(mf->qid.type & QTDIR){
772 err = "can't write directory";
775 if(cnt >= Maxrequest){
776 err = "request too long";
779 job->request.data[cnt] = 0;
784 if(strncmp(job->request.data, "debug", 5)==0){
786 syslog(1, logfile, "debug %d", debug);
791 * toggle ipv6 lookups
793 if(strncmp(job->request.data, "ipv6", 4)==0){
795 syslog(1, logfile, "ipv6lookups %d", ipv6lookups);
802 if(strncmp(job->request.data, "paranoia", 8)==0){
804 syslog(1, logfile, "paranoia %d", paranoia);
809 * add networks to the default list
811 if(strncmp(job->request.data, "add ", 4)==0){
812 if(job->request.data[cnt-1] == '\n')
813 job->request.data[cnt-1] = 0;
814 netadd(job->request.data+4);
822 if(strncmp(job->request.data, "refresh", 7)==0){
828 err = "query already in progress";
833 /* start transaction with a clean slate */
837 * look for a general query
839 if(*job->request.data == '!'){
840 err = genquery(mf, job->request.data+1);
845 syslog(0, logfile, "write %s", job->request.data);
847 syslog(0, paranoiafile, "write %s by %s", job->request.data, mf->user);
851 n = getfields(job->request.data, field, 4, 1, "!");
854 mf->net = strdup("net");
855 mf->host = strdup(field[0]);
858 mf->rem = strdup(field[3]);
861 mf->serv = strdup(field[2]);
864 mf->host = strdup(field[1]);
865 mf->net = strdup(field[0]);
870 * do the first net worth of lookup
873 rerrstr(curerr, sizeof curerr);
878 if(--mf->ref == 0 && mf->busy == 0)
882 job->reply.count = cnt;
887 rclunk(Job *job, Mfile *mf)
899 rremove(Job *job, Mfile *mf)
902 sendmsg(job, "remove permission denied");
906 rstat(Job *job, Mfile *mf)
909 uchar buf[IOHDRSZ+Maxfdata];
911 memset(&dir, 0, sizeof dir);
912 if(mf->qid.type & QTDIR){
914 dir.mode = DMDIR|0555;
924 dir.atime = dir.mtime = time(0);
925 job->reply.nstat = convD2M(&dir, buf, sizeof buf);
926 job->reply.stat = buf;
931 rwstat(Job *job, Mfile *mf)
934 sendmsg(job, "wstat permission denied");
938 sendmsg(Job *job, char *err)
941 uchar mdata[IOHDRSZ + Maxfdata];
945 job->reply.type = Rerror;
946 snprint(ename, sizeof(ename), "cs: %s", err);
947 job->reply.ename = ename;
949 job->reply.type = job->request.type+1;
951 job->reply.tag = job->request.tag;
952 n = convS2M(&job->reply, mdata, sizeof mdata);
954 syslog(1, logfile, "sendmsg convS2M of %F returns 0", &job->reply);
958 if(job->flushed == 0)
959 if(write(mfd[1], mdata, n)!=n)
960 error("mount write");
963 syslog(0, logfile, "%F %d", &job->reply, n);
969 syslog(1, "cs", "%s: %r", s);
976 return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0;
979 static uchar loopbacknet[IPaddrlen] = {
985 static uchar loopbackmask[IPaddrlen] = {
986 0xff, 0xff, 0xff, 0xff,
987 0xff, 0xff, 0xff, 0xff,
988 0xff, 0xff, 0xff, 0xff,
993 readipinterfaces(void)
995 if(myipaddr(ipa, mntpt) != 0)
996 ipmove(ipa, IPnoaddr);
997 sprint(ipaddr, "%I", ipa);
999 syslog(0, "dns", "ipaddr is %s\n", ipaddr);
1003 * get the system name
1015 /* use environment, ether addr, or ipaddr to get system name */
1018 * environment has priority.
1020 * on the sgi power the default system name
1021 * is the ip address. ignore that.
1024 p = getenv("sysname");
1027 if(strcmp(attr, "ip") != 0)
1028 mysysname = strdup(p);
1032 * the /net/ndb contains what the network
1033 * figured out from DHCP. use that name if
1036 if(mysysname == 0 && netdb != nil){
1038 for(tt = t = ndbparse(netdb); t != nil; t = t->entry){
1039 if(strcmp(t->attr, "sys") == 0){
1040 mysysname = strdup(t->val);
1047 /* next network database, ip address, and ether address to find a name */
1051 free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t));
1053 for(f = 0; f < 3; f++){
1054 snprint(buf, sizeof buf, "%s/ether%d", mntpt, f);
1055 if(myetheraddr(addr, buf) >= 0){
1056 snprint(eaddr, sizeof(eaddr), "%E", addr);
1057 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1063 for(tt = t; tt != nil; tt = tt->entry){
1064 if(strcmp(tt->attr, "sys") == 0){
1065 mysysname = strdup(tt->val);
1072 /* nothing else worked, use the ip address */
1073 if(mysysname == 0 && isvalidip(ipa))
1074 mysysname = strdup(ipaddr);
1077 /* set /dev/sysname if we now know it */
1079 f = open("/dev/sysname", OWRITE);
1081 write(f, mysysname, strlen(mysysname));
1089 * Set up a list of default networks by looking for
1093 netinit(int background)
1095 char clone[Maxpath];
1100 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1109 /* add the mounted networks to the default list */
1110 for(np = network; np->net; np++){
1113 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1114 if(access(clone, AEXIST) < 0)
1125 /* find out what our ip address is */
1128 /* set the system name if we need to, these days ip is all we have */
1132 syslog(0, logfile, "mysysname %s eaddr %s ipaddr %s ipa %I\n",
1133 mysysname?mysysname:"???", eaddr, ipaddr, ipa);
1142 * add networks to the standard list
1151 n = getfields(p, field, 12, 1, " ");
1152 for(i = 0; i < n; i++){
1153 for(np = network; np->net; np++){
1154 if(strcmp(field[i], np->net) != 0)
1170 lookforproto(Ndbtuple *t, char *proto)
1172 for(; t != nil; t = t->entry)
1173 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1179 * lookup a request. the network "net" means we should pick the
1180 * best network to get there.
1188 char reply[Maxreply];
1192 /* open up the standard db files */
1196 error("can't open mf->network database\n");
1201 return 0; /* must have been a genquery */
1203 if(strcmp(mf->net, "net") == 0){
1205 * go through set of default nets
1207 for(np = mf->nextnet; np; np = np->next){
1208 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1211 hack = np->fasttimeouthack && !lookforproto(nt, np->net);
1212 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1213 cp = (*np->trans)(t, np, mf->serv, mf->rem, hack);
1215 /* avoid duplicates */
1216 for(i = 0; i < mf->nreply; i++)
1217 if(strcmp(mf->reply[i], cp) == 0)
1219 if(i == mf->nreply){
1220 /* save the reply */
1221 mf->replylen[mf->nreply] = strlen(cp);
1222 mf->reply[mf->nreply++] = cp;
1236 * if not /net, we only get one lookup
1241 * look for a specific network
1243 for(np = netlist; np && np->net != nil; np++){
1244 if(np->fasttimeouthack)
1246 if(strcmp(np->net, mf->net) == 0)
1250 if(np && np->net != nil){
1254 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1255 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1256 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1258 mf->replylen[mf->nreply] = strlen(cp);
1259 mf->reply[mf->nreply++] = cp;
1267 * not a known network, don't translate host or service
1270 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1271 mntpt, mf->net, mf->host, mf->serv);
1273 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1274 mntpt, mf->net, mf->host);
1275 mf->reply[0] = strdup(reply);
1276 mf->replylen[0] = strlen(reply);
1283 * translate an ip service name into a port number. If it's a numeric port
1284 * number, look for restricted access.
1286 * the service '*' needs no translation.
1289 ipserv(Network *np, char *name, char *buf, int blen)
1298 /* '*' means any service */
1299 if(strcmp(name, "*")==0){
1300 nstrcpy(buf, name, blen);
1304 /* see if it's numeric or symbolic */
1306 for(p = name; *p; p++){
1309 else if(isalpha(*p) || *p == '-' || *p == '$')
1317 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1321 /* look up only for tcp ports < 1024 to get the restricted
1324 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1325 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1331 for(nt = t; nt; nt = nt->entry)
1332 if(strcmp(nt->attr, "restricted") == 0)
1336 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1343 * lookup an ip attribute
1346 ipattrlookup(Ndb *db, char *ipa, char *attr, char *val, int vlen)
1353 t = ndbipinfo(db, "ip", ipa, alist, 1);
1356 for(nt = t; nt != nil; nt = nt->entry){
1357 if(strcmp(nt->attr, attr) == 0){
1358 nstrcpy(val, nt->val, vlen);
1364 /* we shouldn't get here */
1370 * lookup (and translate) an ip destination
1373 iplookup(Network *np, char *host, char *serv, int nolookup)
1375 char *attr, *dnsname;
1378 char ts[Maxservice];
1379 char dollar[Maxhost];
1380 uchar ip[IPaddrlen];
1381 uchar net[IPaddrlen];
1382 uchar tnet[IPaddrlen];
1389 * start with the service since it's the most likely to fail
1390 * and costs the least
1392 werrstr("can't translate address");
1393 if(serv==0 || ipserv(np, serv, ts, sizeof ts) == 0){
1394 werrstr("can't translate service");
1398 /* for dial strings with no host */
1399 if(strcmp(host, "*") == 0)
1400 return ndbnew("ip", "*");
1403 * hack till we go v6 :: = 0.0.0.0
1405 if(strcmp("::", host) == 0)
1406 return ndbnew("ip", "*");
1410 * '$' means the rest of the name is an attribute that we
1411 * need to search for
1414 if(ipattrlookup(db, ipaddr, host+1, dollar, sizeof dollar))
1419 * turn '[ip address]' into just 'ip address'
1425 nstrcpy(dollar, host, sizeof dollar);
1428 if(x = strchr(++host, ']'))
1433 * just accept addresses
1435 attr = ipattr(host);
1436 if(strcmp(attr, "ip") == 0)
1437 return ndbnew("ip", host);
1440 * give the domain name server the first opportunity to
1441 * resolve domain names. if that fails try the database.
1444 werrstr("can't translate address");
1445 if(strcmp(attr, "dom") == 0)
1446 t = dnsiplookup(host, &s);
1448 free(ndbgetvalue(db, &s, attr, host, "ip", &t));
1450 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1452 t = dnsiplookup(dnsname, &s);
1457 t = dnsiplookup(host, &s);
1462 * reorder the tuple to have the matched line first and
1463 * save that in the request structure.
1465 t = reorder(t, s.t);
1468 * reorder according to our interfaces
1471 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1472 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1473 maskip(lifc->ip, lifc->mask, net);
1474 for(nt = t; nt; nt = nt->entry){
1475 if(strcmp(nt->attr, "ip") != 0)
1477 parseip(ip, nt->val);
1478 maskip(ip, lifc->mask, tnet);
1479 if(memcmp(net, tnet, IPaddrlen) == 0){
1481 qunlock(&ipifclock);
1487 qunlock(&ipifclock);
1493 * translate an ip address
1496 iptrans(Ndbtuple *t, Network *np, char *serv, char *rem, int hack)
1498 char ts[Maxservice];
1499 char reply[Maxreply];
1502 if(strcmp(t->attr, "ip") != 0)
1505 if(serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0){
1506 werrstr("can't translate service");
1510 snprint(x, sizeof(x), "!%s", rem);
1515 snprint(reply, sizeof(reply), "%s/%s/clone %s%s",
1516 mntpt, np->net, ts, x);
1518 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s",
1519 mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": "");
1521 return strdup(reply);
1525 * lookup a telephone number
1528 telcolookup(Network *np, char *host, char *serv, int nolookup)
1533 USED(np, nolookup, serv);
1535 werrstr("can't translate address");
1536 free(ndbgetvalue(db, &s, "sys", host, "telco", &t));
1538 return ndbnew("telco", host);
1540 return reorder(t, s.t);
1544 * translate a telephone address
1547 telcotrans(Ndbtuple *t, Network *np, char *serv, char *rem, int)
1549 char reply[Maxreply];
1552 if(strcmp(t->attr, "telco") != 0)
1556 snprint(x, sizeof(x), "!%s", rem);
1560 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net,
1563 snprint(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net,
1565 return strdup(reply);
1569 * reorder the tuple to put x's line first in the entry
1572 reorder(Ndbtuple *t, Ndbtuple *x)
1577 /* find start of this entry's line */
1578 for(line = x; line->entry == line->line; line = line->line)
1582 return t; /* already the first line */
1584 /* remove this line and everything after it from the entry */
1585 for(nt = t; nt->entry != line; nt = nt->entry)
1589 /* make that the start of the entry */
1590 for(nt = line; nt->entry; nt = nt->entry)
1597 * create a slave process to handle a request to avoid one request blocking
1598 * another. parent returns to job loop.
1604 return; /* we're already a slave process */
1606 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1611 syslog(0, logfile, "slave %d", getpid());
1612 procsetname("%s", host);
1616 longjmp(masterjmp, 1);
1622 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1626 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1630 /* convert ipv6 attr to ip */
1631 for (tt = t6; tt != nil; tt = tt->entry)
1632 if (strcmp(tt->attr, "ipv6") == 0)
1633 strcpy(tt->attr, "ip");
1638 /* append t6 list to t list */
1639 for (tt = t; tt->entry != nil; tt = tt->entry)
1646 * call the dns process and have it try to translate a name
1649 dnsiplookup(char *host, Ndbs *s)
1657 if(strcmp(ipattr(host), "ip") == 0)
1658 t = dnsquery(mntpt, host, "ptr");
1660 t = dnsquery(mntpt, host, "ip");
1661 /* special case: query ipv6 (AAAA dns RR) too */
1663 t = dnsip6lookup(mntpt, host, t);
1668 rerrstr(buf, sizeof buf);
1669 if(strstr(buf, "exist"))
1670 werrstr("can't translate address: %s", buf);
1671 else if(strstr(buf, "dns failure"))
1672 werrstr("temporary problem: %s", buf);
1680 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1685 for(i = 1; i < n; i++){
1687 for(nt = t; nt; nt = nt->entry)
1688 if(strcmp(attr[i], nt->attr) == 0)
1689 if(strcmp(val[i], "*") == 0
1690 || strcmp(val[i], nt->val) == 0){
1701 qreply(Mfile *mf, Ndbtuple *t)
1707 for(nt = t; mf->nreply < Nreply && nt; nt = nt->entry){
1708 s_append(s, nt->attr);
1710 s_append(s, nt->val);
1712 if(nt->line != nt->entry){
1713 mf->replylen[mf->nreply] = s_len(s);
1714 mf->reply[mf->nreply++] = strdup(s_to_c(s));
1728 * generic query lookup. The query is of one of the following
1731 * attr1=val1 attr2=val2 attr3=val3 ...
1733 * returns the matching tuple
1735 * ipinfo attr=val attr1 attr2 attr3 ...
1737 * is like ipinfo and returns the attr{1-n}
1738 * associated with the ip address.
1741 genquery(Mfile *mf, char *query)
1745 char *attr[Maxattr];
1750 n = getfields(query, attr, nelem(attr), 1, " ");
1754 if(strcmp(attr[0], "ipinfo") == 0)
1755 return ipinfoquery(mf, attr, n);
1758 for(i = 0; i < n; i++){
1759 p = strchr(attr[i], '=');
1766 /* give dns a chance */
1767 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1768 t = dnsiplookup(val[0], &s);
1770 if(qmatch(t, attr, val, n)){
1779 /* first pair is always the key. It can't be a '*' */
1780 t = ndbsearch(db, &s, attr[0], val[0]);
1782 /* search is the and of all the pairs */
1784 if(qmatch(t, attr, val, n)){
1791 t = ndbsnext(&s, attr[0], val[0]);
1798 * resolve an ip address
1801 ipresolve(char *attr, char *host)
1803 Ndbtuple *t, *nt, **l;
1805 t = iplookup(&network[Ntcp], host, "*", 0);
1806 for(l = &t; *l != nil; ){
1808 if(strcmp(nt->attr, "ip") != 0){
1814 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1821 ipinfoquery(Mfile *mf, char **list, int n)
1824 int resolve[Maxattr];
1825 Ndbtuple *t, *nt, **l;
1834 /* get search attribute=value, or assume ip=myipaddr */
1836 if((val = strchr(attr, '=')) != nil){
1849 * don't let ndbipinfo resolve the addresses, we're
1853 for(i = 0; i < n; i++)
1854 if(*list[i] == '@'){ /* @attr=val ? */
1856 resolve[i] = 1; /* we'll resolve it */
1861 t = ndbipinfo(db, attr, val, list, n);
1866 for(l = &t; *l != nil;){
1869 /* already an address? */
1870 if(strcmp(ipattr(nt->val), "ip") == 0){
1875 /* user wants it resolved? */
1876 for(i = 0; i < n; i++)
1877 if(strcmp(list[i], nt->attr) == 0)
1879 if(i >= n || resolve[i] == 0){
1884 /* resolve address and replace entry */
1885 *l = ipresolve(nt->attr, nt->val);
1895 /* make it all one line */
1896 for(nt = t; nt != nil; nt = nt->entry){
1897 if(nt->entry == nil)
1900 nt->line = nt->entry;
1930 memmove(p, s, size);