8 * this comment used to say `our target is 4000 names cached, this should
9 * be larger on large servers'. dns at Bell Labs starts off with
12 * aging seems to corrupt the cache, so raise the trigger from 4000 until we
16 /* these settings will trigger frequent aging */
19 Defagefreq = 15*Min, /* age names this often (seconds) */
23 * Hash table for domain names. The hash is based only on the
24 * first element of the domain name.
30 ulong names; /* names allocated */
31 ulong oldest; /* longest we'll leave a name around */
34 ushort id; /* same size as in packet */
37 /* names of RR types */
62 [Tnsapptr] "nsap-ptr",
84 [Tipseckey] "ipseckey",
103 /* names of response codes */
104 char *rname[Rmask+1] =
107 [Rformat] "format error",
108 [Rserver] "server failure",
110 [Runimplimented] "unimplemented",
111 [Rrefused] "we don't like you",
112 [Ryxdomain] "name should not exist",
113 [Ryxrrset] "rr set should not exist",
114 [Rnxrrset] "rr set should exist",
115 [Rnotauth] "not authorative",
116 [Rnotzone] "not in zone",
117 [Rbadvers] "bad opt version",
118 /* [Rbadsig] "bad signature", */
120 [Rbadtime] "bad signature time",
121 [Rbadmode] "bad mode",
122 [Rbadname] "duplicate key name",
123 [Rbadalg] "bad algorithm",
125 unsigned nrname = nelem(rname);
127 /* names of op codes */
131 [Oinverse] "inverse query (retired)",
136 ulong target = Deftarget;
139 static ulong agefreq = Defagefreq;
141 static int rrequiv(RR *r1, RR *r2);
142 static int sencodefmt(Fmt*);
145 ding(void*, char *msg)
147 if(strstr(msg, "alarm") != nil) {
149 noted(NCONT); /* resume with system call error */
151 noted(NDFLT); /* die */
157 fmtinstall('E', eipfmt);
158 fmtinstall('I', eipfmt);
159 fmtinstall('V', eipfmt);
160 fmtinstall('R', rrfmt);
161 fmtinstall('Q', rravfmt);
162 fmtinstall('H', sencodefmt);
164 dnvars.oldest = maxage;
166 dnvars.id = truerand(); /* don't start with same id every time */
172 * hash for a domain name
178 uchar *val = (uchar*)name;
180 for(hash = 0; *val; val++)
181 hash = hash*13 + tolower(*val)-'a';
186 * lookup a symbol. if enter is not zero and the name is
187 * not found, create it.
190 dnlookup(char *name, int class, int enter)
195 l = &ht[dnhash(name)];
197 for(dp = *l; dp; dp = dp->next) {
198 assert(dp->magic == DNmagic);
199 if(dp->class == class && cistrcmp(dp->name, name) == 0){
200 dp->referenced = now;
212 dp = emalloc(sizeof(*dp));
214 dp->name = estrdup(name);
217 dp->referenced = now;
218 /* add new DN to tail of the hash list. *l points to last next ptr. */
227 idnlookup(char *name, int class, int enter)
231 if(utf2idn(name, dom, sizeof dom) != nil)
233 return dnlookup(name, class, enter);
237 rrsame(RR *rr1, RR *rr2)
239 return rr1 == rr2 || rr2 && rrequiv(rr1, rr2) &&
240 rr1->db == rr2->db && rr1->auth == rr2->auth;
244 rronlist(RR *rp, RR *lp)
246 for(; lp; lp = lp->next)
260 fd = create(file, OWRITE, 0666);
265 fprint(fd, "# system %s\n", sysname());
266 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
267 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
268 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
269 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
270 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
271 for (i = 0; i < nelem(stats.under10ths); i++)
272 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
273 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
274 (double)(i+1)/10, stats.under10ths[i]);
275 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
276 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
277 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
278 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
279 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
280 fprint(fd, "# negative answers w bad delegation\t%lud\n",
282 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
284 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
285 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
289 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
304 fd = create(file, OWRITE, 0666);
309 for(i = 0; i < HTLEN; i++)
310 for(dp = ht[i]; dp; dp = dp->next){
311 fprint(fd, "%s\n", dp->name);
312 for(rp = dp->rr; rp; rp = rp->next) {
313 fprint(fd, "\t%R %c%c %ld/%lud\n",
314 rp, rp->auth? 'A': 'U',
315 rp->db? 'D': 'N', (long)(rp->expire - now), rp->ttl);
316 if (rronlist(rp, rp->next))
317 fprint(fd, "*** duplicate:\n");
336 for(i = 0; i < HTLEN; i++)
337 for(dp = ht[i]; dp; dp = dp->next){
340 for(; rp != nil; rp = rp->next)
349 * delete head of *l and free the old head.
350 * call with dnlock held.
357 if (canlock(&dnlock))
358 abort(); /* rrdelhead called with dnlock not held */
362 *l = rp->next; /* unlink head */
363 rp->cached = 0; /* avoid blowing an assertion in rrfree */
368 * check the age of resource records, free any that have timed out.
369 * call with dnlock held.
377 if (canlock(&dnlock))
378 abort(); /* dnage called with dnlock not held */
379 diff = now - dp->referenced;
380 if(diff < Reserved || dp->mark != 0)
384 while ((rp = *l) != nil){
385 assert(rp->magic == RRmagic && rp->cached);
386 if(!rp->db && ((long)(rp->expire - now) <= 0 || diff > dnvars.oldest))
387 rrdelhead(l); /* rp == *l before; *l == rp->next after */
393 #define MARK(dp) { if (dp) (dp)->mark |= 2; }
395 /* mark a domain name and those in its RRs as never to be aged */
403 /* mark all referenced domain names */
405 for(rp = dp->rr; rp; rp = rp->next){
408 MARK(rp->negsoaowner);
451 MARK(rp->sig->signer);
459 #define REF(dp) { if (dp) (dp)->mark |= 1; }
462 * periodicly sweep for old records and remove unreferenced domain names
464 * only called when all other threads are locked out
472 static ulong nextage;
474 if(dnvars.names < target || ((long)(nextage - now) > 0 && !doit)){
475 dnvars.oldest = maxage;
479 if(dnvars.names >= target) {
480 dnslog("more names (%lud) than target (%lud)", dnvars.names,
483 if (dnvars.oldest < Minage)
484 dnvars.oldest = Minage; /* don't be silly */
486 if (agefreq > dnvars.oldest / 2)
487 nextage = now + dnvars.oldest / 2;
489 nextage = now + (ulong)agefreq;
493 /* time out all old entries (and set refs to 0) */
494 for(i = 0; i < HTLEN; i++)
495 for(dp = ht[i]; dp; dp = dp->next){
500 /* mark all referenced domain names */
501 for(i = 0; i < HTLEN; i++)
502 for(dp = ht[i]; dp; dp = dp->next)
503 for(rp = dp->rr; rp; rp = rp->next){
506 REF(rp->negsoaowner);
549 REF(rp->sig->signer);
554 /* sweep and remove unreferenced domain names */
555 for(i = 0; i < HTLEN; i++){
557 for(dp = *l; dp; dp = *l){
558 if(dp->rr == nil && dp->mark == 0){
559 assert(dp->magic == DNmagic);
563 memset(dp, 0, sizeof *dp); /* cause trouble */
564 dp->magic = ~DNmagic;
578 * timeout all database records (used when rereading db)
589 /* time out all database entries */
590 for(i = 0; i < HTLEN; i++)
591 for(dp = ht[i]; dp; dp = dp->next) {
593 for(rp = dp->rr; rp; rp = rp->next)
602 * mark all local db records about my area as authoritative,
603 * delete timed out ones
616 /* time out all database entries */
617 for(i = 0; i < HTLEN; i++)
618 for(dp = ht[i]; dp; dp = dp->next){
619 area = inmyarea(dp->name);
621 for(rp = *l; rp; rp = *l){
628 minttl = area->soarr->soa->minttl;
642 * keep track of other processes to know if we can
643 * garbage collect. block while garbage collecting.
646 getactivity(Request *req, int recursive)
651 dnslog("get: %d active by pid %d from %p",
652 dnvars.active, getpid(), getcallerpc(&req));
655 * can't block here if we're already holding one
656 * of the dnvars.active (recursive). will deadlock.
658 while(!recursive && dnvars.mutex){
660 sleep(100); /* tune; was 200 */
663 rv = ++dnvars.active;
666 req->id = ++dnvars.id;
673 putactivity(int recursive)
675 static ulong lastclean;
678 dnslog("put: %d active by pid %d",
679 dnvars.active, getpid());
682 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
685 * clean out old entries and check for new db periodicly
686 * can't block here if being called to let go a "recursive" lock
687 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
689 if (recursive || dnvars.mutex ||
690 (needrefresh == 0 && dnvars.active > 0)){
695 /* wait till we're alone */
697 while(dnvars.active > 0){
699 sleep(100); /* tune; was 100 */
704 db2cache(needrefresh);
708 /* let others back in */
720 for(; rp; rp = rp->next)
726 * Attach a single resource record to a domain name (new->owner).
727 * - Avoid duplicates with already present RR's
728 * - Chain all RR's of the same type adjacent to one another
729 * - chain authoritative RR's ahead of non-authoritative ones
730 * - remove any expired RR's
731 * If new is a stale duplicate, rrfree it.
732 * Must be called with dnlock held.
735 rrattach1(RR *new, int auth)
742 assert(new->magic == RRmagic && !new->cached);
745 assert(dp != nil && dp->magic == DNmagic);
750 * try not to let responses expire before we
751 * can use them to complete this query, by extending
752 * past (or nearly past) expiration time.
760 new->expire = now + ttl;
763 * find first rr of the right type
766 for(rp = *l; rp; rp = *l){
767 assert(rp->magic == RRmagic && rp->cached);
768 if(rp->type == new->type)
774 * negative entries replace positive entries
775 * positive entries replace negative entries
776 * newer entries replace older entries with the same fields
778 * look farther ahead than just the next entry when looking
779 * for duplicates; RRs of a given type can have different rdata
780 * fields (e.g. multiple NS servers).
782 while ((rp = *l) != nil){
783 assert(rp->magic == RRmagic && rp->cached);
784 if(rp->type != new->type)
787 if(rp->db == new->db && rp->auth == new->auth){
788 /* negative drives out positive and vice versa */
789 if(rp->negative != new->negative) {
790 /* rp == *l before; *l == rp->next after */
794 /* all things equal, pick the newer one */
795 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
796 /* old drives out new */
797 if((long)(rp->expire - new->expire) > 0) {
801 /* rp == *l before; *l == rp->next after */
806 * Hack for pointer records. This makes sure
807 * the ordering in the list reflects the ordering
808 * received or read from the database
810 else if(rp->type == Tptr &&
811 !rp->negative && !new->negative &&
812 rp->ptr->ordinal > new->ptr->ordinal)
818 if (rronlist(new, rp)) {
819 /* should not happen; duplicates were processed above */
820 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
832 * Attach a list of resource records to a domain name.
833 * May rrfree any stale duplicate RRs; dismembers the list.
834 * Upon return, every RR in the list will have been rrfree-d
835 * or attached to its domain name.
836 * See rrattach1 for properties preserved.
839 rrattach(RR *rp, int auth)
845 for(; rp; rp = next){
849 /* avoid any outside spoofing */
850 if(cfg.cachedb && !rp->db && inmyarea(dp->name))
859 rrcopy(RR *rp, RR **last)
870 assert(rp->magic == RRmagic);
871 nrp = rralloc(rp->type);
878 soa->slaves = copyserverlist(rp->soa->slaves);
891 key->data = emalloc(key->dlen);
892 memmove(key->data, rp->key->data, rp->key->dlen);
899 cert->data = emalloc(cert->dlen);
900 memmove(cert->data, rp->cert->data, rp->cert->dlen);
907 sig->data = emalloc(sig->dlen);
908 memmove(sig->data, rp->sig->data, rp->sig->dlen);
915 null->data = emalloc(null->dlen);
916 memmove(null->data, rp->null->data, rp->null->dlen);
922 for(t = rp->txt; t != nil; t = t->next){
923 nt = emalloc(sizeof(*nt));
924 nt->p = estrdup(t->p);
934 nrp->pc = getcallerpc(&rp);
935 setmalloctag(nrp, nrp->pc);
943 * lookup a resource record of a particular type and
944 * class attached to a domain name. Return copies.
946 * Priority ordering is:
948 * not timed out network authoritative
949 * not timed out network unauthoritative
952 * if flag NOneg is set, don't return negative cached entries.
953 * return nothing instead.
956 rrlookup(DN *dp, int type, int flag)
958 RR *rp, *first, **last;
960 assert(dp->magic == DNmagic);
966 /* try for an authoritative db entry */
967 for(rp = dp->rr; rp; rp = rp->next){
968 assert(rp->magic == RRmagic && rp->cached);
971 if(tsame(type, rp->type))
972 last = rrcopy(rp, last);
977 /* try for a living authoritative network entry */
978 for(rp = dp->rr; rp; rp = rp->next){
981 if((long)(rp->expire - now) > 0)
982 if(tsame(type, rp->type)){
983 if(flag == NOneg && rp->negative)
985 last = rrcopy(rp, last);
991 /* try for a living unauthoritative network entry */
992 for(rp = dp->rr; rp; rp = rp->next){
994 if((long)(rp->expire - now) > 0)
995 if(tsame(type, rp->type)){
996 if(flag == NOneg && rp->negative)
998 last = rrcopy(rp, last);
1004 /* try for an unauthoritative db entry */
1005 for(rp = dp->rr; rp; rp = rp->next){
1007 if(tsame(type, rp->type))
1008 last = rrcopy(rp, last);
1013 /* otherwise, settle for anything we got (except for negative caches) */
1014 for(rp = dp->rr; rp; rp = rp->next)
1015 if(tsame(type, rp->type)){
1018 last = rrcopy(rp, last);
1028 * convert an ascii RR type name to its integer representation
1035 for(i = 0; i <= Tall; i++)
1036 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1039 /* make any a synonym for all */
1040 if(strcmp(atype, "any") == 0)
1042 else if(isascii(atype[0]) && isdigit(atype[0]))
1049 * return 0 if not a supported rr type
1052 rrsupported(int type)
1054 if(type < 0 || type >Tall)
1056 return rrtname[type] != nil;
1063 tsame(int t1, int t2)
1065 return t1 == t2 || t1 == Tall;
1069 * Add resource records to a list.
1072 rrcat(RR **start, RR *rp)
1077 /* check for duplicates */
1078 for (olp = *start; 0 && olp; olp = olp->next)
1079 for (nlp = rp; nlp; nlp = nlp->next)
1080 if (rrsame(nlp, olp))
1081 dnslog("rrcat: duplicate RR: %R", nlp);
1086 last = &(*last)->next;
1093 rrremfilter(RR **l, int (*filter)(RR*, void*), void *arg)
1102 if((*filter)(rp, arg)){
1115 filterneg(RR *rp, void*)
1117 return rp->negative;
1120 filtertype(RR *rp, void *arg)
1122 return rp->type == *((int*)arg);
1125 filterowner(RR *rp, void *arg)
1127 return rp->owner == (DN*)arg;
1131 * remove negative cache rr's from an rr list
1136 return rrremfilter(l, filterneg, nil);
1140 * remove rr's of a particular type from an rr list
1143 rrremtype(RR **l, int type)
1145 return rrremfilter(l, filtertype, &type);
1149 * remove rr's of a particular owner from an rr list
1152 rrremowner(RR **l, DN *owner)
1154 return rrremfilter(l, filterowner, owner);
1160 return dn? dn->name: "<null>";
1164 idnname(DN *dn, char *buf, int nbuf)
1169 if(idn2utf(name, buf, nbuf) != nil)
1175 * print conversion for rr records
1192 rp = va_arg(f->args, RR*);
1194 fmtprint(&fstr, "<null>");
1198 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1199 rrname(rp->type, buf, sizeof buf));
1202 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1208 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1215 fmtprint(&fstr, "\t%s", dnname(rp->host));
1219 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1222 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1225 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1229 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1232 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1236 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1237 dnname(rp->host), dnname(rp->rmb),
1238 (soa? soa->serial: 0),
1239 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1240 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1242 for(s = soa->slaves; s != nil; s = s->next)
1243 fmtprint(&fstr, " %s", s->name);
1247 fmtprint(&fstr, "\t%ud %ud %ud %s",
1248 (srv? srv->pri: 0), (srv? srv->weight: 0),
1249 rp->port, dnname(rp->host));
1252 if (rp->null == nil)
1253 fmtprint(&fstr, "\t<null>");
1255 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1259 fmtprint(&fstr, "\t");
1260 for(t = rp->txt; t != nil; t = t->next)
1261 fmtprint(&fstr, "%s", t->p);
1264 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1268 fmtprint(&fstr, "\t<null> <null> <null>");
1270 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1271 rp->key->proto, rp->key->alg);
1276 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1278 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1279 rp->sig->type, rp->sig->alg, rp->sig->labels,
1280 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1281 rp->sig->tag, dnname(rp->sig->signer));
1284 if (rp->cert == nil)
1285 fmtprint(&fstr, "\t<null> <null> <null>");
1287 fmtprint(&fstr, "\t%d %d %d",
1288 rp->cert->type, rp->cert->tag, rp->cert->alg);
1292 strp = fmtstrflush(&fstr);
1293 rv = fmtstrcpy(f, strp);
1299 * print conversion for rr records in attribute value form
1305 char buf[Domlen], *strp;
1315 rp = va_arg(f->args, RR*);
1317 fmtprint(&fstr, "<null>");
1321 if(rp->type == Tptr)
1322 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1324 fmtprint(&fstr, "dom=%s", idnname(rp->owner, buf, sizeof(buf)));
1328 fmtprint(&fstr, " cpu=%s os=%s",
1329 idnname(rp->cpu, buf, sizeof(buf)),
1330 idnname(rp->os, buf, sizeof(buf)));
1333 fmtprint(&fstr, " cname=%s", idnname(rp->host, buf, sizeof(buf)));
1338 fmtprint(&fstr, " mbox=%s", idnname(rp->host, buf, sizeof(buf)));
1341 fmtprint(&fstr, " ns=%s", idnname(rp->host, buf, sizeof(buf)));
1345 fmtprint(&fstr, " mbox=%s", idnname(rp->mb, buf, sizeof(buf)));
1348 fmtprint(&fstr, " mbox=%s mbox=%s",
1349 idnname(rp->mb, buf, sizeof(buf)),
1350 idnname(rp->rmb, buf, sizeof(buf)));
1353 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref,
1354 idnname(rp->host, buf, sizeof(buf)));
1358 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1361 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1366 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1367 idnname(rp->host, buf, sizeof(buf)),
1368 idnname(rp->rmb, buf, sizeof(buf)),
1369 (soa? soa->serial: 0),
1370 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1371 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1372 for(s = soa->slaves; s != nil; s = s->next)
1373 fmtprint(&fstr, " dnsslave=%s", s->name);
1377 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1378 (srv? srv->pri: 0), (srv? srv->weight: 0),
1379 rp->port, idnname(rp->host, buf, sizeof(buf)));
1382 if (rp->null == nil)
1383 fmtprint(&fstr, " null=<null>");
1385 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1389 fmtprint(&fstr, " txt=");
1391 for(t = rp->txt; t != nil; t = t->next)
1392 if(strchr(t->p, ' '))
1395 fmtprint(&fstr, "\"");
1396 for(t = rp->txt; t != nil; t = t->next)
1397 fmtprint(&fstr, "%s", t->p);
1399 fmtprint(&fstr, "\"");
1402 fmtprint(&fstr, " rp=%s txt=%s",
1403 idnname(rp->rmb, buf, sizeof(buf)),
1404 idnname(rp->rp, buf, sizeof(buf)));
1408 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1410 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1411 rp->key->flags, rp->key->proto, rp->key->alg);
1416 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1419 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1420 rp->sig->type, rp->sig->alg, rp->sig->labels,
1421 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1422 rp->sig->tag, idnname(rp->sig->signer, buf, sizeof(buf)));
1425 if (rp->cert == nil)
1426 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1428 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1429 rp->cert->type, rp->cert->tag, rp->cert->alg);
1433 strp = fmtstrflush(&fstr);
1434 rv = fmtstrcpy(f, strp);
1440 warning(char *fmt, ...)
1446 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1448 syslog(1, logfile, dnserr); /* on console too */
1452 dnslog(char *fmt, ...)
1458 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1460 syslog(0, logfile, dnserr);
1464 * based on libthread's threadsetname, but drags in less library code.
1465 * actually just sets the arguments displayed.
1468 procsetname(char *fmt, ...)
1476 cmdname = vsmprint(fmt, arg);
1480 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1481 if((fd = open(buf, OWRITE)) >= 0){
1482 write(fd, cmdname, strlen(cmdname)+1);
1489 * create a slave process to handle a request to avoid one request blocking
1498 return; /* we're already a slave process */
1501 * These calls to putactivity cannot block.
1502 * After getactivity(), the current process is counted
1503 * twice in dnvars.active (one will pass to the child).
1504 * If putactivity tries to wait for dnvars.active == 0,
1505 * it will never happen.
1508 /* limit parallelism */
1509 procs = getactivity(req, 1);
1510 if(procs > stats.slavehiwat)
1511 stats.slavehiwat = procs;
1512 if(procs > Maxactive){
1514 dnslog("[%d] too much activity", getpid());
1520 * parent returns to main loop, child does the work.
1521 * don't change note group.
1524 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1529 procsetname("request slave of pid %d", ppid);
1531 dnslog("[%d] take activity from %d", getpid(), ppid);
1532 req->isslave = 1; /* why not `= getpid()'? */
1536 * this relies on rfork producing separate, initially-identical
1537 * stacks, thus giving us two copies of `req', one in each
1541 longjmp(req->mret, 1);
1546 rrequiv(RR *r1, RR *r2)
1548 return r1->owner == r2->owner
1549 && r1->type == r2->type
1550 && r1->arg0 == r2->arg0
1551 && r1->arg1 == r2->arg1;
1559 for(; rp; rp = rp->next){
1561 for(nrp = *l; nrp; nrp = *l)
1562 if(rrequiv(rp, nrp)){
1571 * true if second domain is subsumed by the first
1574 subsume(char *higher, char *lower)
1579 hn = strlen(higher);
1580 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1581 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1587 * randomize the order we return items to provide some
1588 * load balancing for servers.
1590 * only randomize the first class of entries
1595 RR *first, *last, *x, *base;
1598 if(rp == nil || rp->next == nil)
1601 /* just randomize addresses, mx's and ns's */
1602 for(x = rp; x; x = x->next)
1603 if(x->type != Ta && x->type != Taaaa &&
1604 x->type != Tmx && x->type != Tns)
1612 /* stop randomizing if we've moved past our class */
1613 if(base->auth != rp->auth || base->db != rp->db){
1638 /* reroll the dice */
1648 int i, len, ilen, rv;
1651 char obuf[64]; /* rsc optimization */
1653 if(!(f->flags&FmtPrec) || f->prec < 1)
1656 b = va_arg(f->args, uchar*);
1660 /* if it's a printable, go for it */
1662 for(i = 0; i < len; i++)
1666 if(len >= sizeof obuf)
1667 len = sizeof(obuf)-1;
1668 memmove(obuf, b, len);
1676 f->flags &= ~FmtPrec;
1678 if(len > sizeof(obuf)){
1687 rv = enc16(out, len, b, ilen);
1697 return fmtstrcpy(f, "<encodefmt>");
1705 x = mallocz(size, 1);
1708 setmalloctag(x, getcallerpc(&size));
1719 p = mallocz(size+1, 0);
1722 memmove(p, s, size);
1724 setmalloctag(p, getcallerpc(&s));
1729 * create a pointer record
1732 mkptr(DN *dp, char *ptr, ulong ttl)
1737 ipdp = dnlookup(ptr, Cin, 1);
1748 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1751 * look for all ip addresses in this network and make
1752 * pointer records for them.
1755 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1761 uchar ip[IPaddrlen], nnet[IPaddrlen];
1762 uchar nibip[IPaddrlen*2];
1764 RR *rp, *nrp, *first, **l;
1768 for(i = 0; i < HTLEN; i++)
1769 for(dp = ht[i]; dp; dp = dp->next)
1770 for(rp = dp->rr; rp; rp = rp->next){
1771 if(rp->type != forwtype || rp->negative)
1773 parseip(ip, rp->ip->name);
1774 maskip(ip, mask, nnet);
1775 if(ipcmp(net, nnet) != 0)
1780 if (forwtype == Taaaa) {
1781 bytes2nibbles(nibip, ip, IPaddrlen);
1787 e = ptr+sizeof(ptr);
1788 for(j = len - 1; j >= len - subdoms; j--)
1789 p = seprint(p, e, (forwtype == Ta?
1790 "%d.": "%x."), ipp[j]);
1791 seprint(p, e, "%s", dom);
1793 nrp = mkptr(dp, ptr, ttl);
1798 for(rp = first; rp != nil; rp = nrp){
1802 rrattach(rp, Authoritative);
1808 addserver(Server **l, char *name)
1816 s = malloc(sizeof(Server)+n+1);
1819 s->name = (char*)(s+1);
1820 memmove(s->name, name, n);
1827 copyserverlist(Server *s)
1831 for(ns = nil; s != nil; s = s->next)
1832 addserver(&ns, s->name);
1837 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1840 * convert an integer RR type to it's ascii name
1843 rrname(int type, char *buf, int len)
1848 if(type >= 0 && type <= Tall)
1851 snprint(buf, len, "%d", type);
1858 * free a list of resource records and any related structs
1865 for(; rp; rp = next){
1872 freeserverlist(Server *s)
1876 for(; s != nil; s = next){
1878 memset(s, 0, sizeof *s); /* cause trouble */
1884 * allocate a resource record of a given type
1891 rp = emalloc(sizeof(*rp));
1892 rp->magic = RRmagic;
1893 rp->pc = getcallerpc(&type);
1895 if (rp->type != type)
1896 dnslog("rralloc: bogus type %d", type);
1897 setmalloctag(rp, rp->pc);
1900 rp->soa = emalloc(sizeof(*rp->soa));
1901 rp->soa->slaves = nil;
1902 setmalloctag(rp->soa, rp->pc);
1905 rp->srv = emalloc(sizeof(*rp->srv));
1906 setmalloctag(rp->srv, rp->pc);
1909 rp->key = emalloc(sizeof(*rp->key));
1910 setmalloctag(rp->key, rp->pc);
1913 rp->cert = emalloc(sizeof(*rp->cert));
1914 setmalloctag(rp->cert, rp->pc);
1917 rp->sig = emalloc(sizeof(*rp->sig));
1918 setmalloctag(rp->sig, rp->pc);
1921 rp->null = emalloc(sizeof(*rp->null));
1922 setmalloctag(rp->null, rp->pc);
1932 * free a resource record and any related structs
1939 assert(rp->magic == RRmagic && !rp->cached);
1943 freeserverlist(rp->soa->slaves);
1944 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
1948 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
1952 free(rp->key->data);
1953 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
1957 free(rp->cert->data);
1958 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
1962 free(rp->sig->data);
1963 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
1967 free(rp->null->data);
1968 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
1975 memset(t, 0, sizeof *t); /* cause trouble */
1981 memset(rp, 0, sizeof *rp); /* cause trouble */
1982 rp->magic = ~RRmagic;