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 { "telco", telcolookup, telcotrans, 1 },
174 char eaddr[16]; /* ascii ethernet address */
175 char ipaddr[64]; /* ascii internet address */
176 uchar ipa[IPaddrlen]; /* binary internet address */
179 Network *netlist; /* networks ordered by preference */
183 nstrcpy(char *to, char *from, int len)
185 strncpy(to, from, len);
192 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
197 * based on libthread's threadsetname, but drags in less library code.
198 * actually just sets the arguments displayed.
201 procsetname(char *fmt, ...)
209 cmdname = vsmprint(fmt, arg);
213 snprint(buf, sizeof buf, "#p/%d/args", getpid());
214 if((fd = open(buf, OWRITE)) >= 0){
215 write(fd, cmdname, strlen(cmdname)+1);
222 main(int argc, char *argv[])
225 char ext[Maxpath], servefile[Maxpath];
228 setnetmtpt(mntpt, sizeof(mntpt), nil);
238 dbfile = EARGF(usage());
244 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
245 setext(ext, sizeof(ext), mntpt);
251 rfork(RFREND|RFNOTEG);
253 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
254 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
255 unmount(servefile, mntpt);
258 fmtinstall('E', eipfmt);
259 fmtinstall('I', eipfmt);
260 fmtinstall('M', eipfmt);
261 fmtinstall('F', fcallfmt);
267 mountinit(servefile, mntpt);
274 * if a mount point is specified, set the cs extention to be the mount point
275 * with '_'s replacing '/'s
278 setext(char *ext, int n, char *p)
283 for(i = 0; i < n; i++){
295 mountinit(char *service, char *mntpt)
302 error("pipe failed");
307 f = create(service, OWRITE|ORCLOSE, 0666);
310 snprint(buf, sizeof(buf), "%d", p[1]);
311 if(write(f, buf, strlen(buf)) != strlen(buf))
312 error("write /srv/cs");
314 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
317 procsetname("%s", mntpt);
320 error("fork failed\n");
323 * put ourselves into the file system
326 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
327 error("mount failed\n");
330 mfd[0] = mfd[1] = p[0];
336 db = ndbopen(dbfile);
338 error("can't open network database");
340 for(netdb = db; netdb; netdb = netdb->next)
341 if(strcmp(netdb->file, netndb) == 0)
344 netdb = ndbopen(netndb);
347 db = ndbcat(netdb, db);
358 for(f = mlist; f; f = f->next)
359 if(f->mf.busy && f->mf.fid == fid)
361 else if(!ff && !f->mf.busy && !f->mf.ref)
364 ff = emalloc(sizeof *f);
369 memset(mf, 0, sizeof *mf);
379 job = mallocz(sizeof(Job), 1);
383 job->request.tag = -1;
394 for(l = &joblist; *l; l = &(*l)->next){
410 for(job = joblist; job; job = job->next){
411 if(job->request.tag == tag && job->request.type != Tflush){
425 uchar mdata[IOHDRSZ + Maxfdata];
429 * if we ask dns to fulfill requests,
430 * a slave process is created to wait for replies. The
431 * master process returns immediately via a longjmp
432 * through 'masterjmp'.
434 * *isslave is a pointer into the call stack to a variable
435 * that tells whether or not the current process is a slave.
437 slaveflag = 0; /* init slave variable */
438 isslave = &slaveflag;
442 n = read9pmsg(mfd[0], mdata, sizeof mdata);
446 if(convM2S(mdata, n, &job->request) != n){
447 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
448 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
453 mf = newfid(job->request.fid);
455 syslog(0, logfile, "%F", &job->request);
458 switch(job->request.type){
460 syslog(1, logfile, "unknown request type %d", job->request.type);
507 * slave processes die after replying
511 syslog(0, logfile, "slave death %d", getpid());
520 if(job->request.msize > IOHDRSZ + Maxfdata)
521 job->reply.msize = IOHDRSZ + Maxfdata;
523 job->reply.msize = job->request.msize;
524 if(strncmp(job->request.version, "9P2000", 6) != 0)
525 sendmsg(job, "unknown 9P version");
527 job->reply.version = "9P2000";
535 sendmsg(job, "cs: authentication not required");
539 * don't flush till all the slaves are done
544 flushjob(job->request.oldtag);
549 rattach(Job *job, Mfile *mf)
553 mf->user = estrdup(job->request.uname);
555 mf->qid.vers = vers++;
556 mf->qid.type = QTDIR;
558 job->reply.qid = mf->qid;
564 rwalk(Job *job, Mfile *mf)
575 elems = job->request.wname;
576 nelems = job->request.nwname;
577 job->reply.nwqid = 0;
579 if(job->request.newfid != job->request.fid){
581 nmf = newfid(job->request.newfid);
584 err = "clone to used channel";
588 nmf->user = estrdup(mf->user);
589 nmf->fid = job->request.newfid;
590 nmf->qid.vers = vers++;
593 /* else nmf will be nil */
598 for(i=0; i<nelems && i<MAXWELEM; i++){
599 if((qid.type & QTDIR) == 0){
600 err = "not a directory";
603 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
607 job->reply.wqid[i] = qid;
611 if(strcmp(elems[i], "cs") == 0){
616 err = "file does not exist";
622 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
636 ropen(Job *job, Mfile *mf)
642 mode = job->request.mode;
643 if(mf->qid.type & QTDIR){
645 err = "permission denied";
647 job->reply.qid = mf->qid;
648 job->reply.iounit = 0;
653 rcreate(Job *job, Mfile *mf)
656 sendmsg(job, "creation permission denied");
660 rread(Job *job, Mfile *mf)
663 long off, toff, clock;
670 off = job->request.offset;
671 cnt = job->request.count;
674 if(mf->qid.type & QTDIR){
677 memset(&dir, 0, sizeof dir);
679 dir.qid.type = QTFILE;
687 dir.atime = clock; /* wrong */
688 dir.mtime = clock; /* wrong */
689 n = convD2M(&dir, buf, sizeof buf);
691 job->reply.data = (char*)buf;
696 /* look for an answer at the right offset */
698 for(i = 0; mf->reply[i] && i < mf->nreply; i++){
705 break; /* got something to return */
707 /* try looking up more answers */
708 if(lookup(mf) == 0 || job->flushed){
715 /* give back a single reply (or part of one) */
716 job->reply.data = mf->reply[i] + (off - toff);
717 if(cnt > toff - off + n)
723 job->reply.count = n;
726 if(--mf->ref == 0 && mf->busy == 0)
751 for(i = 0; i < mf->nreply; i++){
757 mf->nextnet = netlist;
761 rwrite(Job *job, Mfile *mf)
769 cnt = job->request.count;
770 if(mf->qid.type & QTDIR){
771 err = "can't write directory";
774 if(cnt >= Maxrequest){
775 err = "request too long";
778 job->request.data[cnt] = 0;
783 if(strncmp(job->request.data, "debug", 5)==0){
785 syslog(1, logfile, "debug %d", debug);
790 * toggle ipv6 lookups
792 if(strncmp(job->request.data, "ipv6", 4)==0){
794 syslog(1, logfile, "ipv6lookups %d", ipv6lookups);
801 if(strncmp(job->request.data, "paranoia", 8)==0){
803 syslog(1, logfile, "paranoia %d", paranoia);
808 * add networks to the default list
810 if(strncmp(job->request.data, "add ", 4)==0){
811 if(job->request.data[cnt-1] == '\n')
812 job->request.data[cnt-1] = 0;
813 netadd(job->request.data+4);
821 if(strncmp(job->request.data, "refresh", 7)==0){
827 err = "query already in progress";
832 /* start transaction with a clean slate */
836 * look for a general query
838 if(*job->request.data == '!'){
839 err = genquery(mf, job->request.data+1);
844 syslog(0, logfile, "write %s", job->request.data);
846 syslog(0, paranoiafile, "write %s by %s", job->request.data, mf->user);
850 n = getfields(job->request.data, field, 4, 1, "!");
853 mf->net = strdup("net");
854 mf->host = strdup(field[0]);
857 mf->rem = strdup(field[3]);
860 mf->serv = strdup(field[2]);
863 mf->host = strdup(field[1]);
864 mf->net = strdup(field[0]);
869 * do the first net worth of lookup
872 rerrstr(curerr, sizeof curerr);
877 if(--mf->ref == 0 && mf->busy == 0)
881 job->reply.count = cnt;
886 rclunk(Job *job, Mfile *mf)
898 rremove(Job *job, Mfile *mf)
901 sendmsg(job, "remove permission denied");
905 rstat(Job *job, Mfile *mf)
908 uchar buf[IOHDRSZ+Maxfdata];
910 memset(&dir, 0, sizeof dir);
911 if(mf->qid.type & QTDIR){
913 dir.mode = DMDIR|0555;
923 dir.atime = dir.mtime = time(0);
924 job->reply.nstat = convD2M(&dir, buf, sizeof buf);
925 job->reply.stat = buf;
930 rwstat(Job *job, Mfile *mf)
933 sendmsg(job, "wstat permission denied");
937 sendmsg(Job *job, char *err)
940 uchar mdata[IOHDRSZ + Maxfdata];
944 job->reply.type = Rerror;
945 snprint(ename, sizeof(ename), "cs: %s", err);
946 job->reply.ename = ename;
948 job->reply.type = job->request.type+1;
950 job->reply.tag = job->request.tag;
951 n = convS2M(&job->reply, mdata, sizeof mdata);
953 syslog(1, logfile, "sendmsg convS2M of %F returns 0", &job->reply);
957 if(job->flushed == 0)
958 if(write(mfd[1], mdata, n)!=n)
959 error("mount write");
962 syslog(0, logfile, "%F %d", &job->reply, n);
968 syslog(1, "cs", "%s: %r", s);
975 return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0;
978 static uchar loopbacknet[IPaddrlen] = {
984 static uchar loopbackmask[IPaddrlen] = {
985 0xff, 0xff, 0xff, 0xff,
986 0xff, 0xff, 0xff, 0xff,
987 0xff, 0xff, 0xff, 0xff,
992 readipinterfaces(void)
994 if(myipaddr(ipa, mntpt) != 0)
995 ipmove(ipa, IPnoaddr);
996 sprint(ipaddr, "%I", ipa);
998 syslog(0, "dns", "ipaddr is %s\n", ipaddr);
1002 * get the system name
1014 /* use environment, ether addr, or ipaddr to get system name */
1017 * environment has priority.
1019 * on the sgi power the default system name
1020 * is the ip address. ignore that.
1023 p = getenv("sysname");
1026 if(strcmp(attr, "ip") != 0)
1027 mysysname = strdup(p);
1031 * the /net/ndb contains what the network
1032 * figured out from DHCP. use that name if
1035 if(mysysname == 0 && netdb != nil){
1037 for(tt = t = ndbparse(netdb); t != nil; t = t->entry){
1038 if(strcmp(t->attr, "sys") == 0){
1039 mysysname = strdup(t->val);
1046 /* next network database, ip address, and ether address to find a name */
1050 free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t));
1052 for(f = 0; f < 3; f++){
1053 snprint(buf, sizeof buf, "%s/ether%d", mntpt, f);
1054 if(myetheraddr(addr, buf) >= 0){
1055 snprint(eaddr, sizeof(eaddr), "%E", addr);
1056 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1062 for(tt = t; tt != nil; tt = tt->entry){
1063 if(strcmp(tt->attr, "sys") == 0){
1064 mysysname = strdup(tt->val);
1071 /* nothing else worked, use the ip address */
1072 if(mysysname == 0 && isvalidip(ipa))
1073 mysysname = strdup(ipaddr);
1076 /* set /dev/sysname if we now know it */
1078 f = open("/dev/sysname", OWRITE);
1080 write(f, mysysname, strlen(mysysname));
1088 * Set up a list of default networks by looking for
1092 netinit(int background)
1094 char clone[Maxpath];
1099 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1108 /* add the mounted networks to the default list */
1109 for(np = network; np->net; np++){
1112 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1113 if(access(clone, AEXIST) < 0)
1124 /* find out what our ip address is */
1127 /* set the system name if we need to, these days ip is all we have */
1131 syslog(0, logfile, "mysysname %s eaddr %s ipaddr %s ipa %I\n",
1132 mysysname?mysysname:"???", eaddr, ipaddr, ipa);
1141 * add networks to the standard list
1150 n = getfields(p, field, 12, 1, " ");
1151 for(i = 0; i < n; i++){
1152 for(np = network; np->net; np++){
1153 if(strcmp(field[i], np->net) != 0)
1169 lookforproto(Ndbtuple *t, char *proto)
1171 for(; t != nil; t = t->entry)
1172 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1178 * lookup a request. the network "net" means we should pick the
1179 * best network to get there.
1187 char reply[Maxreply];
1191 /* open up the standard db files */
1195 error("can't open mf->network database\n");
1200 return 0; /* must have been a genquery */
1202 if(strcmp(mf->net, "net") == 0){
1204 * go through set of default nets
1206 for(np = mf->nextnet; np; np = np->next){
1207 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1210 hack = np->fasttimeouthack && !lookforproto(nt, np->net);
1211 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1212 cp = (*np->trans)(t, np, mf->serv, mf->rem, hack);
1214 /* avoid duplicates */
1215 for(i = 0; i < mf->nreply; i++)
1216 if(strcmp(mf->reply[i], cp) == 0)
1218 if(i == mf->nreply){
1219 /* save the reply */
1220 mf->replylen[mf->nreply] = strlen(cp);
1221 mf->reply[mf->nreply++] = cp;
1235 * if not /net, we only get one lookup
1240 * look for a specific network
1242 for(np = netlist; np && np->net != nil; np++){
1243 if(np->fasttimeouthack)
1245 if(strcmp(np->net, mf->net) == 0)
1249 if(np && np->net != nil){
1253 nt = (*np->lookup)(np, mf->host, mf->serv, 1);
1254 for(t = nt; mf->nreply < Nreply && t; t = t->entry){
1255 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1257 mf->replylen[mf->nreply] = strlen(cp);
1258 mf->reply[mf->nreply++] = cp;
1266 * not a known network, don't translate host or service
1269 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1270 mntpt, mf->net, mf->host, mf->serv);
1272 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1273 mntpt, mf->net, mf->host);
1274 mf->reply[0] = strdup(reply);
1275 mf->replylen[0] = strlen(reply);
1282 * translate an ip service name into a port number. If it's a numeric port
1283 * number, look for restricted access.
1285 * the service '*' needs no translation.
1288 ipserv(Network *np, char *name, char *buf, int blen)
1297 /* '*' means any service */
1298 if(strcmp(name, "*")==0){
1299 nstrcpy(buf, name, blen);
1303 /* see if it's numeric or symbolic */
1305 for(p = name; *p; p++){
1308 else if(isalpha(*p) || *p == '-' || *p == '$')
1316 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1320 /* look up only for tcp ports < 1024 to get the restricted
1323 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1324 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1330 for(nt = t; nt; nt = nt->entry)
1331 if(strcmp(nt->attr, "restricted") == 0)
1335 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1342 * lookup an ip attribute
1345 ipattrlookup(Ndb *db, char *ipa, char *attr, char *val, int vlen)
1352 t = ndbipinfo(db, "ip", ipa, alist, 1);
1355 for(nt = t; nt != nil; nt = nt->entry){
1356 if(strcmp(nt->attr, attr) == 0){
1357 nstrcpy(val, nt->val, vlen);
1363 /* we shouldn't get here */
1369 * lookup (and translate) an ip destination
1372 iplookup(Network *np, char *host, char *serv, int nolookup)
1374 char *attr, *dnsname;
1377 char ts[Maxservice];
1378 char dollar[Maxhost];
1379 uchar ip[IPaddrlen];
1380 uchar net[IPaddrlen];
1381 uchar tnet[IPaddrlen];
1388 * start with the service since it's the most likely to fail
1389 * and costs the least
1391 werrstr("can't translate address");
1392 if(serv==0 || ipserv(np, serv, ts, sizeof ts) == 0){
1393 werrstr("can't translate service");
1397 /* for dial strings with no host */
1398 if(strcmp(host, "*") == 0)
1399 return ndbnew("ip", "*");
1402 * hack till we go v6 :: = 0.0.0.0
1404 if(strcmp("::", host) == 0)
1405 return ndbnew("ip", "*");
1409 * '$' means the rest of the name is an attribute that we
1410 * need to search for
1413 if(ipattrlookup(db, ipaddr, host+1, dollar, sizeof dollar))
1418 * turn '[ip address]' into just 'ip address'
1424 nstrcpy(dollar, host, sizeof dollar);
1427 if(x = strchr(++host, ']'))
1432 * just accept addresses
1434 attr = ipattr(host);
1435 if(strcmp(attr, "ip") == 0)
1436 return ndbnew("ip", host);
1439 * give the domain name server the first opportunity to
1440 * resolve domain names. if that fails try the database.
1443 werrstr("can't translate address");
1444 if(strcmp(attr, "dom") == 0)
1445 t = dnsiplookup(host, &s);
1447 free(ndbgetvalue(db, &s, attr, host, "ip", &t));
1449 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1451 t = dnsiplookup(dnsname, &s);
1456 t = dnsiplookup(host, &s);
1461 * reorder the tuple to have the matched line first and
1462 * save that in the request structure.
1464 t = reorder(t, s.t);
1467 * reorder according to our interfaces
1470 for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
1471 for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
1472 maskip(lifc->ip, lifc->mask, net);
1473 for(nt = t; nt; nt = nt->entry){
1474 if(strcmp(nt->attr, "ip") != 0)
1476 parseip(ip, nt->val);
1477 maskip(ip, lifc->mask, tnet);
1478 if(memcmp(net, tnet, IPaddrlen) == 0){
1480 qunlock(&ipifclock);
1486 qunlock(&ipifclock);
1492 * translate an ip address
1495 iptrans(Ndbtuple *t, Network *np, char *serv, char *rem, int hack)
1497 char ts[Maxservice];
1498 char reply[Maxreply];
1501 if(strcmp(t->attr, "ip") != 0)
1504 if(serv == 0 || ipserv(np, serv, ts, sizeof ts) == 0){
1505 werrstr("can't translate service");
1509 snprint(x, sizeof(x), "!%s", rem);
1514 snprint(reply, sizeof(reply), "%s/%s/clone %s%s",
1515 mntpt, np->net, ts, x);
1517 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s",
1518 mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": "");
1520 return strdup(reply);
1524 * lookup a telephone number
1527 telcolookup(Network *np, char *host, char *serv, int nolookup)
1532 USED(np, nolookup, serv);
1534 werrstr("can't translate address");
1535 free(ndbgetvalue(db, &s, "sys", host, "telco", &t));
1537 return ndbnew("telco", host);
1539 return reorder(t, s.t);
1543 * translate a telephone address
1546 telcotrans(Ndbtuple *t, Network *np, char *serv, char *rem, int)
1548 char reply[Maxreply];
1551 if(strcmp(t->attr, "telco") != 0)
1555 snprint(x, sizeof(x), "!%s", rem);
1559 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net,
1562 snprint(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net,
1564 return strdup(reply);
1568 * reorder the tuple to put x's line first in the entry
1571 reorder(Ndbtuple *t, Ndbtuple *x)
1576 /* find start of this entry's line */
1577 for(line = x; line->entry == line->line; line = line->line)
1581 return t; /* already the first line */
1583 /* remove this line and everything after it from the entry */
1584 for(nt = t; nt->entry != line; nt = nt->entry)
1588 /* make that the start of the entry */
1589 for(nt = line; nt->entry; nt = nt->entry)
1596 * create a slave process to handle a request to avoid one request blocking
1597 * another. parent returns to job loop.
1603 return; /* we're already a slave process */
1605 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1610 syslog(0, logfile, "slave %d", getpid());
1611 procsetname("%s", host);
1615 longjmp(masterjmp, 1);
1621 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1625 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1629 /* convert ipv6 attr to ip */
1630 for (tt = t6; tt != nil; tt = tt->entry)
1631 if (strcmp(tt->attr, "ipv6") == 0)
1632 strcpy(tt->attr, "ip");
1637 /* append t6 list to t list */
1638 for (tt = t; tt->entry != nil; tt = tt->entry)
1645 * call the dns process and have it try to translate a name
1648 dnsiplookup(char *host, Ndbs *s)
1656 if(strcmp(ipattr(host), "ip") == 0)
1657 t = dnsquery(mntpt, host, "ptr");
1659 t = dnsquery(mntpt, host, "ip");
1660 /* special case: query ipv6 (AAAA dns RR) too */
1662 t = dnsip6lookup(mntpt, host, t);
1667 rerrstr(buf, sizeof buf);
1668 if(strstr(buf, "exist"))
1669 werrstr("can't translate address: %s", buf);
1670 else if(strstr(buf, "dns failure"))
1671 werrstr("temporary problem: %s", buf);
1679 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1684 for(i = 1; i < n; i++){
1686 for(nt = t; nt; nt = nt->entry)
1687 if(strcmp(attr[i], nt->attr) == 0)
1688 if(strcmp(val[i], "*") == 0
1689 || strcmp(val[i], nt->val) == 0){
1700 qreply(Mfile *mf, Ndbtuple *t)
1706 for(nt = t; mf->nreply < Nreply && nt; nt = nt->entry){
1707 s_append(s, nt->attr);
1709 s_append(s, nt->val);
1711 if(nt->line != nt->entry){
1712 mf->replylen[mf->nreply] = s_len(s);
1713 mf->reply[mf->nreply++] = strdup(s_to_c(s));
1727 * generic query lookup. The query is of one of the following
1730 * attr1=val1 attr2=val2 attr3=val3 ...
1732 * returns the matching tuple
1734 * ipinfo attr=val attr1 attr2 attr3 ...
1736 * is like ipinfo and returns the attr{1-n}
1737 * associated with the ip address.
1740 genquery(Mfile *mf, char *query)
1744 char *attr[Maxattr];
1749 n = getfields(query, attr, nelem(attr), 1, " ");
1753 if(strcmp(attr[0], "ipinfo") == 0)
1754 return ipinfoquery(mf, attr, n);
1757 for(i = 0; i < n; i++){
1758 p = strchr(attr[i], '=');
1765 /* give dns a chance */
1766 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1767 t = dnsiplookup(val[0], &s);
1769 if(qmatch(t, attr, val, n)){
1778 /* first pair is always the key. It can't be a '*' */
1779 t = ndbsearch(db, &s, attr[0], val[0]);
1781 /* search is the and of all the pairs */
1783 if(qmatch(t, attr, val, n)){
1790 t = ndbsnext(&s, attr[0], val[0]);
1797 * resolve an ip address
1800 ipresolve(char *attr, char *host)
1802 Ndbtuple *t, *nt, **l;
1804 t = iplookup(&network[Ntcp], host, "*", 0);
1805 for(l = &t; *l != nil; ){
1807 if(strcmp(nt->attr, "ip") != 0){
1813 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1820 ipinfoquery(Mfile *mf, char **list, int n)
1823 int resolve[Maxattr];
1824 Ndbtuple *t, *nt, **l;
1833 /* get search attribute=value, or assume ip=myipaddr */
1835 if((val = strchr(attr, '=')) != nil){
1848 * don't let ndbipinfo resolve the addresses, we're
1852 for(i = 0; i < n; i++)
1853 if(*list[i] == '@'){ /* @attr=val ? */
1855 resolve[i] = 1; /* we'll resolve it */
1860 t = ndbipinfo(db, attr, val, list, n);
1865 for(l = &t; *l != nil;){
1868 /* already an address? */
1869 if(strcmp(ipattr(nt->val), "ip") == 0){
1874 /* user wants it resolved? */
1875 for(i = 0; i < n; i++)
1876 if(strcmp(list[i], nt->attr) == 0)
1878 if(i >= n || resolve[i] == 0){
1883 /* resolve address and replace entry */
1884 *l = ipresolve(nt->attr, nt->val);
1894 /* make it all one line */
1895 for(nt = t; nt != nil; nt = nt->entry){
1896 if(nt->entry == nil)
1899 nt->line = nt->entry;
1929 memmove(p, s, size);