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
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 */
86 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";
131 char netndb[Maxpath];
134 * Network specific translators
136 Ndbtuple* iplookup(Network*, char*, char*);
137 char* iptrans(Ndbtuple*, Network*, char*, char*, int);
138 Ndbtuple* telcolookup(Network*, char*, char*);
139 char* telcotrans(Ndbtuple*, Network*, char*, char*, int);
140 Ndbtuple* dnsiplookup(char*, Ndbs*, int);
145 Ndbtuple *(*lookup)(Network*, char*, char*);
146 char *(*trans)(Ndbtuple*, Network*, char*, char*, int);
148 char considered; /* flag: ignored for "net!"? */
149 char fasttimeouthack; /* flag. was for IL */
161 * net doesn't apply to (r)udp, icmp(v6), or telco (for speed).
163 Network network[] = {
164 { "il", iplookup, iptrans, 0, 1, 1, 0, },
165 { "tcp", iplookup, iptrans, 0, 0, 0, 0, },
166 { "il", iplookup, iptrans, 0, 0, 1, 0, },
167 { "udp", iplookup, iptrans, 1, 0, 0, 0, },
168 { "icmp", iplookup, iptrans, 1, 0, 1, 0, },
169 { "icmpv6", iplookup, iptrans, 1, 0, 0, 1, },
170 { "rudp", iplookup, iptrans, 1, 0, 1, 0, },
171 { "ssh", iplookup, iptrans, 1, 0, 0, 0, },
172 { "telco", telcolookup, telcotrans, 1, 0, 0, 0, },
179 char eaddr[16]; /* ascii ethernet address */
180 char ipaddr[64]; /* ascii internet address */
181 uchar ipa[IPaddrlen]; /* binary internet address */
184 Network *netlist; /* networks ordered by preference */
188 nstrcpy(char *to, char *from, int len)
190 strncpy(to, from, len);
197 fprint(2, "usage: %s [-dn] [-f ndb-file] [-x netmtpt]\n", argv0);
202 * based on libthread's threadsetname, but drags in less library code.
203 * actually just sets the arguments displayed.
206 procsetname(char *fmt, ...)
214 cmdname = vsmprint(fmt, arg);
218 snprint(buf, sizeof buf, "#p/%d/args", getpid());
219 if((fd = open(buf, OWRITE)) >= 0){
220 write(fd, cmdname, strlen(cmdname)+1);
227 main(int argc, char *argv[])
230 char ext[Maxpath], servefile[Maxpath];
233 setnetmtpt(mntpt, sizeof(mntpt), nil);
243 dbfile = EARGF(usage());
249 setnetmtpt(mntpt, sizeof(mntpt), EARGF(usage()));
250 setext(ext, sizeof(ext), mntpt);
256 snprint(netndb, sizeof(netndb), "%s/ndb", mntpt);
258 fmtinstall('E', eipfmt);
259 fmtinstall('I', eipfmt);
260 fmtinstall('M', eipfmt);
261 fmtinstall('F', fcallfmt);
267 snprint(servefile, sizeof(servefile), "#s/cs%s", ext);
268 unmount(servefile, mntpt);
271 rfork(RFREND|RFNOTEG);
272 csuser = estrdup(getuser());
273 mountinit(servefile, mntpt);
280 * if a mount point is specified, set the cs extention to be the mount point
281 * with '_'s replacing '/'s
284 setext(char *ext, int n, char *p)
289 for(i = 0; i < n; i++){
301 mountinit(char *service, char *mntpt)
308 error("pipe failed");
313 f = create(service, OWRITE|ORCLOSE, 0666);
316 snprint(buf, sizeof(buf), "%d", p[1]);
317 if(write(f, buf, strlen(buf)) != strlen(buf))
318 error("write /srv/cs");
320 switch(rfork(RFFDG|RFPROC|RFNAMEG)){
323 procsetname("%s", mntpt);
326 error("fork failed");
329 * put ourselves into the file system
332 if(mount(p[1], -1, mntpt, MAFTER, "") < 0)
333 error("mount failed");
336 mfd[0] = mfd[1] = p[0];
342 db = ndbopen(dbfile);
344 error("can't open network database");
346 for(netdb = db; netdb != nil; netdb = netdb->next)
347 if(strcmp(netdb->file, netndb) == 0)
350 netdb = ndbopen(netndb);
353 db = ndbcat(netdb, db);
364 for(f = mlist; f != nil; f = f->next)
365 if(f->mf.busy && f->mf.fid == fid)
367 else if(ff == nil && !f->mf.busy && !f->mf.ref)
370 ff = emalloc(sizeof *f);
375 memset(mf, 0, sizeof *mf);
385 job = emalloc(sizeof *job);
389 job->request.tag = -1;
400 for(l = &joblist; *l != nil; l = &(*l)->next){
416 for(job = joblist; job != nil; job = job->next){
417 if(job->request.tag == tag && job->request.type != Tflush){
431 uchar mdata[IOHDRSZ + Maxfdata];
435 * if we ask dns to fulfill requests,
436 * a slave process is created to wait for replies. The
437 * master process returns immediately via a longjmp
438 * through 'masterjmp'.
440 * *isslave is a pointer into the call stack to a variable
441 * that tells whether or not the current process is a slave.
443 slaveflag = 0; /* init slave variable */
444 isslave = &slaveflag;
448 n = read9pmsg(mfd[0], mdata, sizeof mdata);
454 if(convM2S(mdata, n, &job->request) != n){
455 syslog(1, logfile, "format error %ux %ux %ux %ux %ux",
456 mdata[0], mdata[1], mdata[2], mdata[3], mdata[4]);
461 mf = newfid(job->request.fid);
463 syslog(0, logfile, "%F", &job->request);
465 switch(job->request.type){
467 syslog(1, logfile, "unknown request type %d", job->request.type);
514 * slave processes die after replying
518 syslog(0, logfile, "slave death %d", getpid());
528 if(job->request.msize > IOHDRSZ + Maxfdata)
529 job->reply.msize = IOHDRSZ + Maxfdata;
531 job->reply.msize = job->request.msize;
532 if(strncmp(job->request.version, "9P2000", 6) != 0)
533 sendmsg(job, "unknown 9P version");
535 job->reply.version = "9P2000";
543 sendmsg(job, "cs: authentication not required");
547 * don't flush till all the slaves are done
552 flushjob(job->request.oldtag);
557 rattach(Job *job, Mfile *mf)
561 mf->user = estrdup(job->request.uname);
563 mf->qid.vers = vers++;
564 mf->qid.type = QTDIR;
566 job->reply.qid = mf->qid;
572 rwalk(Job *job, Mfile *mf)
583 elems = job->request.wname;
584 nelems = job->request.nwname;
585 job->reply.nwqid = 0;
587 if(job->request.newfid != job->request.fid){
589 nmf = newfid(job->request.newfid);
592 err = "clone to used channel";
596 nmf->user = estrdup(mf->user);
597 nmf->fid = job->request.newfid;
598 nmf->qid.vers = vers++;
601 /* else nmf will be nil */
606 for(i=0; i<nelems && i<MAXWELEM; i++){
607 if((qid.type & QTDIR) == 0){
608 err = "not a directory";
611 if(strcmp(elems[i], "..") == 0 || strcmp(elems[i], ".") == 0){
615 job->reply.wqid[i] = qid;
619 if(strcmp(elems[i], "cs") == 0){
624 err = "file does not exist";
630 if(nmf != nil && (err!=nil || job->reply.nwqid<nelems)){
644 ropen(Job *job, Mfile *mf)
650 mode = job->request.mode;
651 if(mf->qid.type & QTDIR){
653 err = "permission denied";
655 job->reply.qid = mf->qid;
656 job->reply.iounit = 0;
661 rcreate(Job *job, Mfile *mf)
664 sendmsg(job, "creation permission denied");
668 rread(Job *job, Mfile *mf)
671 long off, toff, clock;
678 off = job->request.offset;
679 cnt = job->request.count;
682 if(mf->qid.type & QTDIR){
685 memset(&dir, 0, sizeof dir);
687 dir.qid.type = QTFILE;
695 dir.atime = clock; /* wrong */
696 dir.mtime = clock; /* wrong */
697 n = convD2M(&dir, buf, sizeof buf);
699 job->reply.data = (char*)buf;
704 /* look for an answer at the right offset */
706 for(i = 0; mf->reply[i] != nil && i < mf->nreply; i++){
713 break; /* got something to return */
715 /* try looking up more answers */
716 if(lookup(mf) == 0 || job->flushed){
723 /* give back a single reply (or part of one) */
724 job->reply.data = mf->reply[i] + (off - toff);
725 if(cnt > toff - off + n)
731 job->reply.count = n;
734 if(--mf->ref == 0 && mf->busy == 0)
759 for(i = 0; i < mf->nreply; i++){
765 mf->nextnet = netlist;
769 rwrite(Job *job, Mfile *mf)
777 cnt = job->request.count;
778 if(mf->qid.type & QTDIR){
779 err = "can't write directory";
782 if(cnt >= Maxrequest){
783 err = "request too long";
786 job->request.data[cnt] = 0;
788 if(strcmp(mf->user, "none") == 0 || strcmp(mf->user, csuser) != 0)
789 goto query; /* skip special commands if not owner */
794 if(strncmp(job->request.data, "debug", 5)==0){
796 syslog(1, logfile, "debug %d", debug);
801 * toggle ipv6 lookups
803 if(strncmp(job->request.data, "ipv6", 4)==0){
805 syslog(1, logfile, "ipv6lookups %d", ipv6lookups);
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){
830 err = "query already in progress";
835 /* start transaction with a clean slate */
839 * look for a general query
841 if(*job->request.data == '!'){
842 err = genquery(mf, job->request.data+1);
847 syslog(0, logfile, "write %s", job->request.data);
851 n = getfields(job->request.data, field, 4, 1, "!");
854 mf->net = estrdup("net");
855 mf->host = estrdup(field[0]);
858 mf->rem = estrdup(field[3]);
861 mf->serv = estrdup(field[2]);
864 mf->host = estrdup(field[1]);
865 mf->net = estrdup(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, logfile, "%s: %r", s);
976 return ipcmp(ip, IPnoaddr) != 0 && ipcmp(ip, v4prefix) != 0;
980 readipinterfaces(void)
982 if(myipaddr(ipa, mntpt) != 0)
983 ipmove(ipa, IPnoaddr);
984 sprint(ipaddr, "%I", ipa);
986 syslog(0, logfile, "ipaddr is %s", ipaddr);
990 * get the system name
1003 /* use environment, ether addr, or ipaddr to get system name */
1004 if(mysysname == nil){
1006 * environment has priority.
1008 * on the sgi power the default system name
1009 * is the ip address. ignore that.
1012 p = getenv("sysname");
1015 if(strcmp(attr, "ip") != 0)
1016 mysysname = estrdup(p);
1020 * the /net/ndb contains what the network
1021 * figured out from DHCP. use that name if
1024 if(mysysname == nil && netdb != nil){
1026 for(tt = t = ndbparse(netdb); t != nil; t = t->entry){
1027 if(strcmp(t->attr, "sys") == 0){
1028 mysysname = estrdup(t->val);
1035 /* next network database, ip address, and ether address to find a name */
1036 if(mysysname == nil){
1039 free(ndbgetvalue(db, &s, "ip", ipaddr, "sys", &t));
1043 f = open(mntpt, OREAD);
1045 n = dirreadall(f, &d);
1048 for(f = 0; f < n; f++){
1049 if((d[f].mode & DMDIR) == 0 || strncmp(d[f].name, "ether", 5) != 0)
1051 snprint(buf, sizeof buf, "%s/%s", mntpt, d[f].name);
1052 if(myetheraddr(addr, buf) >= 0){
1053 snprint(eaddr, sizeof(eaddr), "%E", addr);
1054 free(ndbgetvalue(db, &s, "ether", eaddr, "sys", &t));
1061 for(tt = t; tt != nil; tt = tt->entry){
1062 if(strcmp(tt->attr, "sys") == 0){
1063 mysysname = estrdup(tt->val);
1070 /* nothing else worked, use the ip address */
1071 if(mysysname == nil && isvalidip(ipa))
1072 mysysname = estrdup(ipaddr);
1075 /* set /dev/sysname if we now know it */
1076 if(mysysname != nil){
1077 f = open("/dev/sysname", OWRITE);
1079 write(f, mysysname, strlen(mysysname));
1087 * Set up a list of default networks by looking for
1091 netinit(int background)
1093 char clone[Maxpath];
1097 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1106 /* add the mounted networks to the default list */
1107 for(np = network; np->net != nil; np++){
1110 snprint(clone, sizeof(clone), "%s/%s/clone", mntpt, np->net);
1111 if(access(clone, AEXIST) < 0)
1122 /* find out what our ip address is */
1125 /* set the system name if we need to, these days ip is all we have */
1129 syslog(0, logfile, "mysysname %s eaddr %s ipaddr %s ipa %I",
1130 mysysname?mysysname:"???", eaddr, ipaddr, ipa);
1139 * add networks to the standard list
1148 n = getfields(p, field, 12, 1, " ");
1149 for(i = 0; i < n; i++){
1150 for(np = network; np->net != nil; np++){
1151 if(strcmp(field[i], np->net) != 0)
1167 lookforproto(Ndbtuple *t, char *proto)
1169 for(; t != nil; t = t->entry)
1170 if(strcmp(t->attr, "proto") == 0 && strcmp(t->val, proto) == 0)
1176 * lookup a request. the network "net" means we should pick the
1177 * best network to get there.
1185 char reply[Maxreply];
1189 /* open up the standard db files */
1193 error("can't open mf->network database\n");
1196 return 0; /* must have been a genquery */
1199 if(strcmp(mf->net, "net") == 0){
1201 * go through set of default nets
1203 for(np = mf->nextnet; np != nil && rv == 0; np = np->next){
1204 nt = (*np->lookup)(np, mf->host, mf->serv);
1207 hack = np->fasttimeouthack && !lookforproto(nt, np->net);
1208 for(t = nt; mf->nreply < Nreply && t != nil; t = t->entry){
1209 cp = (*np->trans)(t, np, mf->serv, mf->rem, hack);
1211 /* avoid duplicates */
1212 for(i = 0; i < mf->nreply; i++)
1213 if(strcmp(mf->reply[i], cp) == 0)
1215 if(i == mf->nreply){
1216 /* save the reply */
1217 mf->replylen[mf->nreply] = strlen(cp);
1218 mf->reply[mf->nreply++] = cp;
1231 * if not /net, we only get one lookup
1237 * look for a specific network
1239 for(np = network; np->net != nil; np++){
1240 if(np->fasttimeouthack)
1242 if(strcmp(np->net, mf->net) == 0)
1250 nt = (*np->lookup)(np, mf->host, mf->serv);
1251 for(t = nt; mf->nreply < Nreply && t != nil; t = t->entry){
1252 cp = (*np->trans)(t, np, mf->serv, mf->rem, 0);
1254 mf->replylen[mf->nreply] = strlen(cp);
1255 mf->reply[mf->nreply++] = cp;
1263 * not a known network, don't translate host or service
1266 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s",
1267 mntpt, mf->net, mf->host, mf->serv);
1269 snprint(reply, sizeof(reply), "%s/%s/clone %s",
1270 mntpt, mf->net, mf->host);
1271 mf->reply[0] = estrdup(reply);
1272 mf->replylen[0] = strlen(reply);
1279 * translate an ip service name into a port number. If it's a numeric port
1280 * number, look for restricted access.
1282 * the service '*' needs no translation.
1285 ipserv(Network *np, char *name, char *buf, int blen)
1293 /* '*' means any service */
1294 if(strcmp(name, "*") == 0){
1295 nstrcpy(buf, name, blen);
1299 /* see if it's numeric or symbolic */
1300 for(p = name; *p; p++){
1303 else if(isalpha(*p) || *p == '-' || *p == '$')
1311 p = ndbgetvalue(db, &s, np->net, name, "port", &t);
1315 /* look up only for tcp ports < 1024 to get the restricted
1318 if(atoi(name) < 1024 && strcmp(np->net, "tcp") == 0)
1319 p = ndbgetvalue(db, &s, "port", name, "port", &t);
1325 for(nt = t; nt != nil; nt = nt->entry)
1326 if(strcmp(nt->attr, "restricted") == 0)
1330 snprint(buf, blen, "%s%s", p, restr ? "!r" : "");
1337 * lookup an ip attribute
1340 ipattrlookup(Ndb *db, char *ipa, char *attr, char *val, int vlen)
1347 t = ndbipinfo(db, "ip", ipa, alist, 1);
1350 for(nt = t; nt != nil; nt = nt->entry){
1351 if(strcmp(nt->attr, attr) == 0){
1352 nstrcpy(val, nt->val, vlen);
1358 /* we shouldn't get here */
1366 uchar ip[IPaddrlen];
1367 return parseip(ip, s) != -1 && isv4(ip);
1371 * lookup (and translate) an ip destination
1374 iplookup(Network *np, char *host, char *serv)
1376 char *attr, *dnsname;
1379 char ts[Maxservice];
1380 char dollar[Maxhost];
1381 uchar ip[IPaddrlen];
1382 uchar net[IPaddrlen];
1383 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 == nil || ipserv(np, serv, ts, sizeof ts) == nil){
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, !np->v4only);
1447 free(ndbgetvalue(db, &s, attr, host, "ip", &t));
1449 dnsname = ndbgetvalue(db, &s, attr, host, "dom", nil);
1451 t = dnsiplookup(dnsname, &s, !np->v4only);
1456 t = dnsiplookup(host, &s, !np->v4only);
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 == nil || ipserv(np, serv, ts, sizeof ts) == nil){
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 if(np->v4only && !isv4str(t->val))
1520 if(np->v6only && isv4str(t->val))
1523 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s%s",
1524 mntpt, np->net, t->val, ts, x, hack? "!fasttimeout": "");
1527 return estrdup(reply);
1531 * lookup a telephone number
1534 telcolookup(Network *np, char *host, char *serv)
1541 werrstr("can't translate address");
1542 free(ndbgetvalue(db, &s, "sys", host, "telco", &t));
1544 return ndbnew("telco", host);
1546 return reorder(t, s.t);
1550 * translate a telephone address
1553 telcotrans(Ndbtuple *t, Network *np, char *serv, char *rem, int)
1555 char reply[Maxreply];
1558 if(strcmp(t->attr, "telco") != 0)
1562 snprint(x, sizeof(x), "!%s", rem);
1566 snprint(reply, sizeof(reply), "%s/%s/clone %s!%s%s", mntpt, np->net,
1569 snprint(reply, sizeof(reply), "%s/%s/clone %s%s", mntpt, np->net,
1571 return estrdup(reply);
1575 * reorder the tuple to put x's line first in the entry
1578 reorder(Ndbtuple *t, Ndbtuple *x)
1583 /* find start of this entry's line */
1584 for(line = x; line->entry == line->line; line = line->line)
1588 return t; /* already the first line */
1590 /* remove this line and everything after it from the entry */
1591 for(nt = t; nt->entry != line; nt = nt->entry)
1595 /* make that the start of the entry */
1596 for(nt = line; nt->entry != nil; nt = nt->entry)
1603 * create a slave process to handle a request to avoid one request blocking
1604 * another. parent returns to job loop.
1610 return; /* we're already a slave process */
1611 if(ainc(&active) >= Maxactive){
1615 switch(rfork(RFPROC|RFNOTEG|RFMEM|RFNOWAIT)){
1622 syslog(0, logfile, "slave %d", getpid());
1623 procsetname("%s", host);
1626 longjmp(masterjmp, 1);
1632 dnsip6lookup(char *mntpt, char *buf, Ndbtuple *t)
1636 t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */
1640 /* convert ipv6 attr to ip */
1641 for (tt = t6; tt != nil; tt = tt->entry)
1642 if (strcmp(tt->attr, "ipv6") == 0)
1643 strcpy(tt->attr, "ip");
1648 /* append t6 list to t list */
1649 for (tt = t; tt->entry != nil; tt = tt->entry)
1656 * call the dns process and have it try to translate a name
1659 dnsiplookup(char *host, Ndbs *s, int v6)
1668 werrstr("too much activity");
1672 if(strcmp(ipattr(host), "ip") == 0)
1673 t = dnsquery(mntpt, host, "ptr");
1675 t = dnsquery(mntpt, host, "ip");
1676 /* special case: query ipv6 (AAAA dns RR) too */
1677 if (v6 && ipv6lookups)
1678 t = dnsip6lookup(mntpt, host, t);
1683 rerrstr(buf, sizeof buf);
1684 if(strstr(buf, "exist") != nil)
1685 werrstr("can't translate address: %s", buf);
1686 else if(strstr(buf, "dns failure") != nil)
1687 werrstr("temporary problem: %s", buf);
1695 qmatch(Ndbtuple *t, char **attr, char **val, int n)
1700 for(i = 1; i < n; i++){
1702 for(nt = t; nt != nil; nt = nt->entry)
1703 if(strcmp(attr[i], nt->attr) == 0)
1704 if(strcmp(val[i], "*") == 0
1705 || strcmp(val[i], nt->val) == 0){
1716 qreply(Mfile *mf, Ndbtuple *t)
1722 for(nt = t; mf->nreply < Nreply && nt != nil; nt = nt->entry){
1723 s_append(s, nt->attr);
1725 s_append(s, nt->val);
1727 if(nt->line != nt->entry){
1728 mf->replylen[mf->nreply] = s_len(s);
1729 mf->reply[mf->nreply++] = estrdup(s_to_c(s));
1743 * generic query lookup. The query is of one of the following
1746 * attr1=val1 attr2=val2 attr3=val3 ...
1748 * returns the matching tuple
1750 * ipinfo attr=val attr1 attr2 attr3 ...
1752 * is like ipinfo and returns the attr{1-n}
1753 * associated with the ip address.
1756 genquery(Mfile *mf, char *query)
1760 char *attr[Maxattr];
1765 n = getfields(query, attr, nelem(attr), 1, " ");
1769 if(strcmp(attr[0], "ipinfo") == 0)
1770 return ipinfoquery(mf, attr, n);
1773 for(i = 0; i < n; i++){
1774 p = strchr(attr[i], '=');
1781 /* give dns a chance */
1782 if((strcmp(attr[0], "dom") == 0 || strcmp(attr[0], "ip") == 0) && val[0]){
1783 t = dnsiplookup(val[0], &s, 1);
1785 if(qmatch(t, attr, val, n)){
1794 /* first pair is always the key. It can't be a '*' */
1795 t = ndbsearch(db, &s, attr[0], val[0]);
1797 /* search is the and of all the pairs */
1799 if(qmatch(t, attr, val, n)){
1806 t = ndbsnext(&s, attr[0], val[0]);
1813 * resolve an ip address
1816 ipresolve(char *attr, char *host)
1818 Ndbtuple *t, *nt, **l;
1820 t = iplookup(&network[Ntcp], host, "*");
1821 for(l = &t; *l != nil; ){
1823 if(strcmp(nt->attr, "ip") != 0){
1829 nstrcpy(nt->attr, attr, sizeof(nt->attr));
1836 ipinfoquery(Mfile *mf, char **list, int n)
1839 int resolve[Maxattr];
1840 Ndbtuple *t, *nt, **l;
1849 /* get search attribute=value, or assume ip=myipaddr */
1851 if((val = strchr(attr, '=')) != nil){
1864 * don't let ndbipinfo resolve the addresses, we're
1868 for(i = 0; i < n; i++)
1869 if(*list[i] == '@'){ /* @attr=val ? */
1871 resolve[i] = 1; /* we'll resolve it */
1876 t = ndbipinfo(db, attr, val, list, n);
1881 for(l = &t; *l != nil;){
1884 /* already an address? */
1885 if(strcmp(ipattr(nt->val), "ip") == 0){
1890 /* user wants it resolved? */
1891 for(i = 0; i < n; i++)
1892 if(strcmp(list[i], nt->attr) == 0)
1894 if(i >= n || resolve[i] == 0){
1899 /* resolve address and replace entry */
1900 *l = ipresolve(nt->attr, nt->val);
1910 /* make it all one line */
1911 for(nt = t; nt != nil; nt = nt->entry){
1912 if(nt->entry == nil)
1915 nt->line = nt->entry;
1930 error("out of memory");
1944 error("out of memory");
1945 memmove(p, s, size);