18 Maxhost= 64, /* maximum host name size */
19 Maxservice= 64, /* maximum service name size */
20 Maxactive= 200, /* maximum number of active slave procs */
26 typedef struct Mfile Mfile;
27 typedef struct Mlist Mlist;
28 typedef struct Network Network;
29 typedef struct Flushreq Flushreq;
30 typedef struct Job Job;
32 int vers; /* incremented each clone/attach */
36 int busy; /* fid in use */
37 int ref; /* cleanup when drops to zero */
52 * result of the last lookup
85 jmp_buf masterjmp; /* return through here after a slave process has been created */
86 int *isslave; /* *isslave non-zero means this is a slave process */
87 long active; /* number of active slaves */
93 void rattach(Job*, Mfile*);
94 char* rwalk(Job*, Mfile*);
95 void ropen(Job*, Mfile*);
96 void rcreate(Job*, Mfile*);
97 void rread(Job*, Mfile*);
98 void rwrite(Job*, Mfile*);
99 void rclunk(Job*, Mfile*);
100 void rremove(Job*, Mfile*);
101 void rstat(Job*, Mfile*);
102 void rwstat(Job*, Mfile*);
104 void sendmsg(Job*, char*);
106 void mountinit(char*, char*);
111 char *genquery(Mfile*, char*);
112 char* ipinfoquery(Mfile*, char**, int);
113 int needproto(Network*, Ndbtuple*);
115 Ndbtuple* reorder(Ndbtuple*, Ndbtuple*);
117 void readipinterfaces(void);
119 char* estrdup(char*);
122 void setext(char*, int, char*);
123 void cleanmf(Mfile*);
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 */
158 * net doesn't apply to (r)udp, icmp(v6), or telco (for speed).
160 Network network[] = {
161 { "il", iplookup, iptrans, 0, 1, },
162 { "tcp", iplookup, iptrans, 0, 0, },
163 { "il", iplookup, iptrans, 0, 0, },
164 { "udp", iplookup, iptrans, 1, 0, },
165 { "icmp", iplookup, iptrans, 1, 0, },
166 { "icmpv6", iplookup, iptrans, 1, 0, },
167 { "rudp", iplookup, iptrans, 1, 0, },
168 { "ssh", iplookup, iptrans, 1, 0, },
169 { "telco", telcolookup, telcotrans, 1, 0, },
176 char eaddr[16]; /* ascii ethernet address */
177 char ipaddr[64]; /* ascii internet address */
178 uchar ipa[IPaddrlen]; /* binary internet address */
181 Network *netlist; /* networks ordered by preference */
185 nstrcpy(char *to, char *from, int len)
187 strncpy(to, from, len);
194 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
199 * based on libthread's threadsetname, but drags in less library code.
200 * actually just sets the arguments displayed.
203 procsetname(char *fmt, ...)
211 cmdname = vsmprint(fmt, arg);
215 snprint(buf, sizeof buf, "#p/%d/args", getpid());
216 if((fd = open(buf, OWRITE)) >= 0){
217 write(fd, cmdname, strlen(cmdname)+1);
224 main(int argc, char *argv[])
227 char ext[Maxpath], servefile[Maxpath];
230 setnetmtpt(mntpt, sizeof(mntpt), nil);
240 dbfile = EARGF(usage());
246 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
247 setext(ext, sizeof(ext), mntpt);
253 rfork(RFREND|RFNOTEG);
255 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
256 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
257 unmount(servefile, mntpt);
260 fmtinstall('E', eipfmt);
261 fmtinstall('I', eipfmt);
262 fmtinstall('M', eipfmt);
263 fmtinstall('F', fcallfmt);
269 mountinit(servefile, mntpt);
276 * if a mount point is specified, set the cs extention to be the mount point
277 * with '_'s replacing '/'s
280 setext(char *ext, int n, char *p)
285 for(i = 0; i < n; i++){
297 mountinit(char *service, char *mntpt)
304 error("pipe failed");
309 f = create(service, OWRITE|ORCLOSE, 0666);
312 snprint(buf, sizeof(buf), "%d", p[1]);
313 if(write(f, buf, strlen(buf)) != strlen(buf))
314 error("write /srv/cs");
316 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
319 procsetname("%s", mntpt);
322 error("fork failed\n");
325 * put ourselves into the file system
328 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
329 error("mount failed\n");
332 mfd[0] = mfd[1] = p[0];
338 db = ndbopen(dbfile);
340 error("can't open network database");
342 for(netdb = db; netdb; netdb = netdb->next)
343 if(strcmp(netdb->file, netndb) == 0)
346 netdb = ndbopen(netndb);
349 db = ndbcat(netdb, db);
360 for(f = mlist; f; f = f->next)
361 if(f->mf.busy && f->mf.fid == fid)
363 else if(!ff && !f->mf.busy && !f->mf.ref)
366 ff = emalloc(sizeof *f);
371 memset(mf, 0, sizeof *mf);
381 job = emalloc(sizeof *job);
385 job->request.tag = -1;
396 for(l = &joblist; *l; l = &(*l)->next){
412 for(job = joblist; job; job = job->next){
413 if(job->request.tag == tag && job->request.type != Tflush){
427 uchar mdata[IOHDRSZ + Maxfdata];
431 * if we ask dns to fulfill requests,
432 * a slave process is created to wait for replies. The
433 * master process returns immediately via a longjmp
434 * through 'masterjmp'.
436 * *isslave is a pointer into the call stack to a variable
437 * that tells whether or not the current process is a slave.
439 slaveflag = 0; /* init slave variable */
440 isslave = &slaveflag;
444 n = read9pmsg(mfd[0], mdata, sizeof mdata);
448 if(convM2S(mdata, n, &job->request) != n){
449 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
450 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
455 mf = newfid(job->request.fid);
457 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());
522 if(job->request.msize > IOHDRSZ + Maxfdata)
523 job->reply.msize = IOHDRSZ + Maxfdata;
525 job->reply.msize = job->request.msize;
526 if(strncmp(job->request.version, "9P2000", 6) != 0)
527 sendmsg(job, "unknown 9P version");
529 job->reply.version = "9P2000";
537 sendmsg(job, "cs: authentication not required");
541 * don't flush till all the slaves are done
546 flushjob(job->request.oldtag);
551 rattach(Job *job, Mfile *mf)
555 mf->user = estrdup(job->request.uname);
557 mf->qid.vers = vers++;
558 mf->qid.type = QTDIR;
560 job->reply.qid = mf->qid;
566 rwalk(Job *job, Mfile *mf)
577 elems = job->request.wname;
578 nelems = job->request.nwname;
579 job->reply.nwqid = 0;
581 if(job->request.newfid != job->request.fid){
583 nmf = newfid(job->request.newfid);
586 err = "clone to used channel";
590 nmf->user = estrdup(mf->user);
591 nmf->fid = job->request.newfid;
592 nmf->qid.vers = vers++;
595 /* else nmf will be nil */
600 for(i=0; i<nelems && i<MAXWELEM; i++){
601 if((qid.type & QTDIR) == 0){
602 err = "not a directory";
605 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
609 job->reply.wqid[i] = qid;
613 if(strcmp(elems[i], "cs") == 0){
618 err = "file does not exist";
624 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
638 ropen(Job *job, Mfile *mf)
644 mode = job->request.mode;
645 if(mf->qid.type & QTDIR){
647 err = "permission denied";
649 job->reply.qid = mf->qid;
650 job->reply.iounit = 0;
655 rcreate(Job *job, Mfile *mf)
658 sendmsg(job, "creation permission denied");
662 rread(Job *job, Mfile *mf)
665 long off, toff, clock;
672 off = job->request.offset;
673 cnt = job->request.count;
676 if(mf->qid.type & QTDIR){
679 memset(&dir, 0, sizeof dir);
681 dir.qid.type = QTFILE;
689 dir.atime = clock; /* wrong */
690 dir.mtime = clock; /* wrong */
691 n = convD2M(&dir, buf, sizeof buf);
693 job->reply.data = (char*)buf;
698 /* look for an answer at the right offset */
700 for(i = 0; mf->reply[i] && i < mf->nreply; i++){
707 break; /* got something to return */
709 /* try looking up more answers */
710 if(lookup(mf) == 0 || job->flushed){
717 /* give back a single reply (or part of one) */
718 job->reply.data = mf->reply[i] + (off - toff);
719 if(cnt > toff - off + n)
725 job->reply.count = n;
728 if(--mf->ref == 0 && mf->busy == 0)
753 for(i = 0; i < mf->nreply; i++){
759 mf->nextnet = netlist;
763 rwrite(Job *job, Mfile *mf)
771 cnt = job->request.count;
772 if(mf->qid.type & QTDIR){
773 err = "can't write directory";
776 if(cnt >= Maxrequest){
777 err = "request too long";
780 job->request.data[cnt] = 0;
785 if(strncmp(job->request.data, "debug", 5)==0){
787 syslog(1, logfile, "debug %d", debug);
792 * toggle ipv6 lookups
794 if(strncmp(job->request.data, "ipv6", 4)==0){
796 syslog(1, logfile, "ipv6lookups %d", ipv6lookups);
803 if(strncmp(job->request.data, "paranoia", 8)==0){
805 syslog(1, logfile, "paranoia %d", paranoia);
810 * add networks to the default list
812 if(strncmp(job->request.data, "add ", 4)==0){
813 if(job->request.data[cnt-1] == '\n')
814 job->request.data[cnt-1] = 0;
815 netadd(job->request.data+4);
823 if(strncmp(job->request.data, "refresh", 7)==0){
829 err = "query already in progress";
834 /* start transaction with a clean slate */
838 * look for a general query
840 if(*job->request.data == '!'){
841 err = genquery(mf, job->request.data+1);
846 syslog(0, logfile, "write %s", job->request.data);
848 syslog(0, paranoiafile, "write %s by %s", job->request.data, mf->user);
852 n = getfields(job->request.data, field, 4, 1, "!");
855 mf->net = estrdup("net");
856 mf->host = estrdup(field[0]);
859 mf->rem = estrdup(field[3]);
862 mf->serv = estrdup(field[2]);
865 mf->host = estrdup(field[1]);
866 mf->net = estrdup(field[0]);
871 * do the first net worth of lookup
874 rerrstr(curerr, sizeof curerr);
879 if(--mf->ref == 0 && mf->busy == 0)
883 job->reply.count = cnt;
888 rclunk(Job *job, Mfile *mf)
900 rremove(Job *job, Mfile *mf)
903 sendmsg(job, "remove permission denied");
907 rstat(Job *job, Mfile *mf)
910 uchar buf[IOHDRSZ+Maxfdata];
912 memset(&dir, 0, sizeof dir);
913 if(mf->qid.type & QTDIR){
915 dir.mode = DMDIR|0555;
925 dir.atime = dir.mtime = time(0);
926 job->reply.nstat = convD2M(&dir, buf, sizeof buf);
927 job->reply.stat = buf;
932 rwstat(Job *job, Mfile *mf)
935 sendmsg(job, "wstat permission denied");
939 sendmsg(Job *job, char *err)
942 uchar mdata[IOHDRSZ + Maxfdata];
946 job->reply.type = Rerror;
947 snprint(ename, sizeof(ename), "cs: %s", err);
948 job->reply.ename = ename;
950 job->reply.type = job->request.type+1;
952 job->reply.tag = job->request.tag;
953 n = convS2M(&job->reply, mdata, sizeof mdata);
955 syslog(1, logfile, "sendmsg convS2M of %F returns 0", &job->reply);
959 if(job->flushed == 0)
960 if(write(mfd[1], mdata, n)!=n)
961 error("mount write");
964 syslog(0, logfile, "%F %d", &job->reply, n);
970 syslog(1, "cs", "%s: %r", s);
977 return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0;
980 static uchar loopbacknet[IPaddrlen] = {
986 static uchar loopbackmask[IPaddrlen] = {
987 0xff, 0xff, 0xff, 0xff,
988 0xff, 0xff, 0xff, 0xff,
989 0xff, 0xff, 0xff, 0xff,
994 readipinterfaces(void)
996 if(myipaddr(ipa, mntpt) != 0)
997 ipmove(ipa, IPnoaddr);
998 sprint(ipaddr, "%I", ipa);
1000 syslog(0, "dns", "ipaddr is %s\n", ipaddr);
1004 * get the system name
1016 /* use environment, ether addr, or ipaddr to get system name */
1019 * environment has priority.
1021 * on the sgi power the default system name
1022 * is the ip address. ignore that.
1025 p = getenv("sysname");
1028 if(strcmp(attr, "ip") != 0)
1029 mysysname = estrdup(p);
1033 * the /net/ndb contains what the network
1034 * figured out from DHCP. use that name if
1037 if(mysysname == 0 && netdb != nil){
1039 for(tt = t = ndbparse(netdb); t != nil; t = t->entry){
1040 if(strcmp(t->attr, "sys") == 0){
1041 mysysname = estrdup(t->val);
1048 /* next network database, ip address, and ether address to find a name */
1052 free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t));
1054 for(f = 0; f < 3; f++){
1055 snprint(buf, sizeof buf, "%s/ether%d", mntpt, f);
1056 if(myetheraddr(addr, buf) >= 0){
1057 snprint(eaddr, sizeof(eaddr), "%E", addr);
1058 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1064 for(tt = t; tt != nil; tt = tt->entry){
1065 if(strcmp(tt->attr, "sys") == 0){
1066 mysysname = estrdup(tt->val);
1073 /* nothing else worked, use the ip address */
1074 if(mysysname == 0 && isvalidip(ipa))
1075 mysysname = estrdup(ipaddr);
1078 /* set /dev/sysname if we now know it */
1080 f = open("/dev/sysname", OWRITE);
1082 write(f, mysysname, strlen(mysysname));
1090 * Set up a list of default networks by looking for
1094 netinit(int background)
1096 char clone[Maxpath];
1101 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1110 /* add the mounted networks to the default list */
1111 for(np = network; np->net; np++){
1114 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1115 if(access(clone, AEXIST) < 0)
1126 /* find out what our ip address is */
1129 /* set the system name if we need to, these days ip is all we have */
1133 syslog(0, logfile, "mysysname %s eaddr %s ipaddr %s ipa %I\n",
1134 mysysname?mysysname:"???", eaddr, ipaddr, ipa);
1143 * add networks to the standard list
1152 n = getfields(p, field, 12, 1, " ");
1153 for(i = 0; i < n; i++){
1154 for(np = network; np->net; np++){
1155 if(strcmp(field[i], np->net) != 0)
1171 lookforproto(Ndbtuple *t, char *proto)
1173 for(; t != nil; t = t->entry)
1174 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1180 * lookup a request. the network "net" means we should pick the
1181 * best network to get there.
1189 char reply[Maxreply];
1193 /* open up the standard db files */
1197 error("can't open mf->network database\n");
1202 return 0; /* must have been a genquery */
1204 if(strcmp(mf->net, "net") == 0){
1206 * go through set of default nets
1208 for(np = mf->nextnet; np; np = np->next){
1209 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1212 hack = np->fasttimeouthack && !lookforproto(nt, np->net);
1213 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1214 cp = (*np->trans)(t, np, mf->serv, mf->rem, hack);
1216 /* avoid duplicates */
1217 for(i = 0; i < mf->nreply; i++)
1218 if(strcmp(mf->reply[i], cp) == 0)
1220 if(i == mf->nreply){
1221 /* save the reply */
1222 mf->replylen[mf->nreply] = strlen(cp);
1223 mf->reply[mf->nreply++] = cp;
1237 * if not /net, we only get one lookup
1242 * look for a specific network
1244 for(np = netlist; np && np->net != nil; np++){
1245 if(np->fasttimeouthack)
1247 if(strcmp(np->net, mf->net) == 0)
1251 if(np && np->net != nil){
1255 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1256 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1257 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1259 mf->replylen[mf->nreply] = strlen(cp);
1260 mf->reply[mf->nreply++] = cp;
1268 * not a known network, don't translate host or service
1271 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1272 mntpt, mf->net, mf->host, mf->serv);
1274 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1275 mntpt, mf->net, mf->host);
1276 mf->reply[0] = estrdup(reply);
1277 mf->replylen[0] = strlen(reply);
1284 * translate an ip service name into a port number. If it's a numeric port
1285 * number, look for restricted access.
1287 * the service '*' needs no translation.
1290 ipserv(Network *np, char *name, char *buf, int blen)
1299 /* '*' means any service */
1300 if(strcmp(name, "*")==0){
1301 nstrcpy(buf, name, blen);
1305 /* see if it's numeric or symbolic */
1307 for(p = name; *p; p++){
1310 else if(isalpha(*p) || *p == '-' || *p == '$')
1318 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1322 /* look up only for tcp ports < 1024 to get the restricted
1325 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1326 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1332 for(nt = t; nt; nt = nt->entry)
1333 if(strcmp(nt->attr, "restricted") == 0)
1337 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1344 * lookup an ip attribute
1347 ipattrlookup(Ndb *db, char *ipa, char *attr, char *val, int vlen)
1354 t = ndbipinfo(db, "ip", ipa, alist, 1);
1357 for(nt = t; nt != nil; nt = nt->entry){
1358 if(strcmp(nt->attr, attr) == 0){
1359 nstrcpy(val, nt->val, vlen);
1365 /* we shouldn't get here */
1371 * lookup (and translate) an ip destination
1374 iplookup(Network *np, char *host, char *serv, int nolookup)
1376 char *attr, *dnsname;
1379 char ts[Maxservice];
1380 char dollar[Maxhost];
1381 uchar ip[IPaddrlen];
1382 uchar net[IPaddrlen];
1383 uchar tnet[IPaddrlen];
1390 * start with the service since it's the most likely to fail
1391 * and costs the least
1393 werrstr("can't translate address");
1394 if(serv==0 || ipserv(np, serv, ts, sizeof ts) == 0){
1395 werrstr("can't translate service");
1399 /* for dial strings with no host */
1400 if(strcmp(host, "*") == 0)
1401 return ndbnew("ip", "*");
1404 * hack till we go v6 :: = 0.0.0.0
1406 if(strcmp("::", host) == 0)
1407 return ndbnew("ip", "*");
1411 * '$' means the rest of the name is an attribute that we
1412 * need to search for
1415 if(ipattrlookup(db, ipaddr, host+1, dollar, sizeof dollar))
1420 * turn '[ip address]' into just 'ip address'
1426 nstrcpy(dollar, host, sizeof dollar);
1429 if(x = strchr(++host, ']'))
1434 * just accept addresses
1436 attr = ipattr(host);
1437 if(strcmp(attr, "ip") == 0)
1438 return ndbnew("ip", host);
1441 * give the domain name server the first opportunity to
1442 * resolve domain names. if that fails try the database.
1445 werrstr("can't translate address");
1446 if(strcmp(attr, "dom") == 0)
1447 t = dnsiplookup(host, &s);
1449 free(ndbgetvalue(db, &s, attr, host, "ip", &t));
1451 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1453 t = dnsiplookup(dnsname, &s);
1458 t = dnsiplookup(host, &s);
1463 * reorder the tuple to have the matched line first and
1464 * save that in the request structure.
1466 t = reorder(t, s.t);
1469 * reorder according to our interfaces
1472 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1473 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1474 maskip(lifc->ip, lifc->mask, net);
1475 for(nt = t; nt; nt = nt->entry){
1476 if(strcmp(nt->attr, "ip") != 0)
1478 parseip(ip, nt->val);
1479 maskip(ip, lifc->mask, tnet);
1480 if(memcmp(net, tnet, IPaddrlen) == 0){
1482 qunlock(&ipifclock);
1488 qunlock(&ipifclock);
1494 * translate an ip address
1497 iptrans(Ndbtuple *t, Network *np, char *serv, char *rem, int hack)
1499 char ts[Maxservice];
1500 char reply[Maxreply];
1503 if(strcmp(t->attr, "ip") != 0)
1506 if(serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0){
1507 werrstr("can't translate service");
1511 snprint(x, sizeof(x), "!%s", rem);
1516 snprint(reply, sizeof(reply), "%s/%s/clone %s%s",
1517 mntpt, np->net, ts, x);
1519 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s",
1520 mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": "");
1522 return estrdup(reply);
1526 * lookup a telephone number
1529 telcolookup(Network *np, char *host, char *serv, int nolookup)
1534 USED(np, nolookup, serv);
1536 werrstr("can't translate address");
1537 free(ndbgetvalue(db, &s, "sys", host, "telco", &t));
1539 return ndbnew("telco", host);
1541 return reorder(t, s.t);
1545 * translate a telephone address
1548 telcotrans(Ndbtuple *t, Network *np, char *serv, char *rem, int)
1550 char reply[Maxreply];
1553 if(strcmp(t->attr, "telco") != 0)
1557 snprint(x, sizeof(x), "!%s", rem);
1561 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net,
1564 snprint(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net,
1566 return estrdup(reply);
1570 * reorder the tuple to put x's line first in the entry
1573 reorder(Ndbtuple *t, Ndbtuple *x)
1578 /* find start of this entry's line */
1579 for(line = x; line->entry == line->line; line = line->line)
1583 return t; /* already the first line */
1585 /* remove this line and everything after it from the entry */
1586 for(nt = t; nt->entry != line; nt = nt->entry)
1590 /* make that the start of the entry */
1591 for(nt = line; nt->entry; nt = nt->entry)
1598 * create a slave process to handle a request to avoid one request blocking
1599 * another. parent returns to job loop.
1605 return; /* we're already a slave process */
1606 if(ainc(&active) >= Maxactive){
1610 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1617 syslog(0, logfile, "slave %d", getpid());
1618 procsetname("%s", host);
1621 longjmp(masterjmp, 1);
1627 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1631 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1635 /* convert ipv6 attr to ip */
1636 for (tt = t6; tt != nil; tt = tt->entry)
1637 if (strcmp(tt->attr, "ipv6") == 0)
1638 strcpy(tt->attr, "ip");
1643 /* append t6 list to t list */
1644 for (tt = t; tt->entry != nil; tt = tt->entry)
1651 * call the dns process and have it try to translate a name
1654 dnsiplookup(char *host, Ndbs *s)
1663 werrstr("too mutch activity");
1667 if(strcmp(ipattr(host), "ip") == 0)
1668 t = dnsquery(mntpt, host, "ptr");
1670 t = dnsquery(mntpt, host, "ip");
1671 /* special case: query ipv6 (AAAA dns RR) too */
1673 t = dnsip6lookup(mntpt, host, t);
1678 rerrstr(buf, sizeof buf);
1679 if(strstr(buf, "exist"))
1680 werrstr("can't translate address: %s", buf);
1681 else if(strstr(buf, "dns failure"))
1682 werrstr("temporary problem: %s", buf);
1690 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1695 for(i = 1; i < n; i++){
1697 for(nt = t; nt; nt = nt->entry)
1698 if(strcmp(attr[i], nt->attr) == 0)
1699 if(strcmp(val[i], "*") == 0
1700 || strcmp(val[i], nt->val) == 0){
1711 qreply(Mfile *mf, Ndbtuple *t)
1717 for(nt = t; mf->nreply < Nreply && nt; nt = nt->entry){
1718 s_append(s, nt->attr);
1720 s_append(s, nt->val);
1722 if(nt->line != nt->entry){
1723 mf->replylen[mf->nreply] = s_len(s);
1724 mf->reply[mf->nreply++] = estrdup(s_to_c(s));
1738 * generic query lookup. The query is of one of the following
1741 * attr1=val1 attr2=val2 attr3=val3 ...
1743 * returns the matching tuple
1745 * ipinfo attr=val attr1 attr2 attr3 ...
1747 * is like ipinfo and returns the attr{1-n}
1748 * associated with the ip address.
1751 genquery(Mfile *mf, char *query)
1755 char *attr[Maxattr];
1760 n = getfields(query, attr, nelem(attr), 1, " ");
1764 if(strcmp(attr[0], "ipinfo") == 0)
1765 return ipinfoquery(mf, attr, n);
1768 for(i = 0; i < n; i++){
1769 p = strchr(attr[i], '=');
1776 /* give dns a chance */
1777 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1778 t = dnsiplookup(val[0], &s);
1780 if(qmatch(t, attr, val, n)){
1789 /* first pair is always the key. It can't be a '*' */
1790 t = ndbsearch(db, &s, attr[0], val[0]);
1792 /* search is the and of all the pairs */
1794 if(qmatch(t, attr, val, n)){
1801 t = ndbsnext(&s, attr[0], val[0]);
1808 * resolve an ip address
1811 ipresolve(char *attr, char *host)
1813 Ndbtuple *t, *nt, **l;
1815 t = iplookup(&network[Ntcp], host, "*", 0);
1816 for(l = &t; *l != nil; ){
1818 if(strcmp(nt->attr, "ip") != 0){
1824 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1831 ipinfoquery(Mfile *mf, char **list, int n)
1834 int resolve[Maxattr];
1835 Ndbtuple *t, *nt, **l;
1844 /* get search attribute=value, or assume ip=myipaddr */
1846 if((val = strchr(attr, '=')) != nil){
1859 * don't let ndbipinfo resolve the addresses, we're
1863 for(i = 0; i < n; i++)
1864 if(*list[i] == '@'){ /* @attr=val ? */
1866 resolve[i] = 1; /* we'll resolve it */
1871 t = ndbipinfo(db, attr, val, list, n);
1876 for(l = &t; *l != nil;){
1879 /* already an address? */
1880 if(strcmp(ipattr(nt->val), "ip") == 0){
1885 /* user wants it resolved? */
1886 for(i = 0; i < n; i++)
1887 if(strcmp(list[i], nt->attr) == 0)
1889 if(i >= n || resolve[i] == 0){
1894 /* resolve address and replace entry */
1895 *l = ipresolve(nt->attr, nt->val);
1905 /* make it all one line */
1906 for(nt = t; nt != nil; nt = nt->entry){
1907 if(nt->entry == nil)
1910 nt->line = nt->entry;
1925 error("out of memory");
1939 error("out of memory");
1940 memmove(p, s, size);