9 * this comment used to say `our target is 4000 names cached, this should
10 * be larger on large servers'. dns at Bell Labs starts off with
13 * aging seems to corrupt the cache, so raise the trigger from 4000 until we
17 /* these settings will trigger frequent aging */
20 Defagefreq = 15*Min, /* age names this often (seconds) */
24 * Hash table for domain names. The hash is based only on the
25 * first element of the domain name.
31 ulong names; /* names allocated */
32 ulong oldest; /* longest we'll leave a name around */
35 ushort id; /* same size as in packet */
38 /* names of RR types */
63 [Tnsapptr] "nsap-ptr",
85 [Tipseckey] "ipseckey",
104 /* names of response codes */
105 char *rname[Rmask+1] =
108 [Rformat] "format error",
109 [Rserver] "server failure",
111 [Runimplimented] "unimplemented",
112 [Rrefused] "we don't like you",
113 [Ryxdomain] "name should not exist",
114 [Ryxrrset] "rr set should not exist",
115 [Rnxrrset] "rr set should exist",
116 [Rnotauth] "not authorative",
117 [Rnotzone] "not in zone",
118 [Rbadvers] "bad opt version",
119 /* [Rbadsig] "bad signature", */
121 [Rbadtime] "bad signature time",
122 [Rbadmode] "bad mode",
123 [Rbadname] "duplicate key name",
124 [Rbadalg] "bad algorithm",
126 unsigned nrname = nelem(rname);
128 /* names of op codes */
132 [Oinverse] "inverse query (retired)",
137 ulong target = Deftarget;
140 static ulong agefreq = Defagefreq;
142 static int rrequiv(RR *r1, RR *r2);
143 static int sencodefmt(Fmt*);
146 ding(void*, char *msg)
148 if(strstr(msg, "alarm") != nil) {
150 noted(NCONT); /* resume with system call error */
152 noted(NDFLT); /* die */
158 fmtinstall('E', eipfmt);
159 fmtinstall('I', eipfmt);
160 fmtinstall('V', eipfmt);
161 fmtinstall('R', rrfmt);
162 fmtinstall('Q', rravfmt);
163 fmtinstall('H', sencodefmt);
165 dnvars.oldest = maxage;
167 dnvars.id = truerand(); /* don't start with same id every time */
173 * hash for a domain name
179 uchar *val = (uchar*)name;
181 for(hash = 0; *val; val++)
182 hash = hash*13 + tolower(*val)-'a';
187 * lookup a symbol. if enter is not zero and the name is
188 * not found, create it.
191 dnlookup(char *name, int class, int enter)
196 l = &ht[dnhash(name)];
198 for(dp = *l; dp; dp = dp->next) {
199 assert(dp->magic == DNmagic);
200 if(dp->class == class && cistrcmp(dp->name, name) == 0){
201 dp->referenced = now;
213 dp = emalloc(sizeof(*dp));
215 dp->name = estrdup(name);
218 dp->referenced = now;
219 /* add new DN to tail of the hash list. *l points to last next ptr. */
228 idnlookup(char *name, int class, int enter)
232 if(utf2idn(name, dom, sizeof dom) != nil)
234 return dnlookup(name, class, enter);
238 rrsame(RR *rr1, RR *rr2)
240 return rr1 == rr2 || rr2 && rrequiv(rr1, rr2) &&
241 rr1->db == rr2->db && rr1->auth == rr2->auth;
245 rronlist(RR *rp, RR *lp)
247 for(; lp; lp = lp->next)
261 fd = create(file, OWRITE, 0666);
266 fprint(fd, "# system %s\n", sysname());
267 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
268 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
269 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
270 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
271 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
272 for (i = 0; i < nelem(stats.under10ths); i++)
273 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
274 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
275 (double)(i+1)/10, stats.under10ths[i]);
276 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
277 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
278 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
279 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
280 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
281 fprint(fd, "# negative answers w bad delegation\t%lud\n",
283 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
285 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
286 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
290 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
305 fd = create(file, OWRITE, 0666);
310 for(i = 0; i < HTLEN; i++)
311 for(dp = ht[i]; dp; dp = dp->next){
312 fprint(fd, "%s\n", dp->name);
313 for(rp = dp->rr; rp; rp = rp->next) {
314 fprint(fd, "\t%R %c%c %ld/%lud\n",
315 rp, rp->auth? 'A': 'U',
316 rp->db? 'D': 'N', (long)(rp->expire - now), rp->ttl);
317 if (rronlist(rp, rp->next))
318 fprint(fd, "*** duplicate:\n");
337 for(i = 0; i < HTLEN; i++)
338 for(dp = ht[i]; dp; dp = dp->next){
341 for(; rp != nil; rp = rp->next)
350 * delete head of *l and free the old head.
351 * call with dnlock held.
358 if (canlock(&dnlock))
359 abort(); /* rrdelhead called with dnlock not held */
363 *l = rp->next; /* unlink head */
364 rp->cached = 0; /* avoid blowing an assertion in rrfree */
369 * check the age of resource records, free any that have timed out.
370 * call with dnlock held.
378 if (canlock(&dnlock))
379 abort(); /* dnage called with dnlock not held */
380 diff = now - dp->referenced;
381 if(diff < Reserved || dp->mark != 0)
385 while ((rp = *l) != nil){
386 assert(rp->magic == RRmagic && rp->cached);
387 if(!rp->db && ((long)(rp->expire - now) <= 0 || diff > dnvars.oldest))
388 rrdelhead(l); /* rp == *l before; *l == rp->next after */
394 #define MARK(dp) { if (dp) (dp)->mark |= 2; }
396 /* mark a domain name and those in its RRs as never to be aged */
404 /* mark all referenced domain names */
406 for(rp = dp->rr; rp; rp = rp->next){
409 MARK(rp->negsoaowner);
452 MARK(rp->sig->signer);
460 #define REF(dp) { if (dp) (dp)->mark |= 1; }
463 * periodicly sweep for old records and remove unreferenced domain names
465 * only called when all other threads are locked out
473 static ulong nextage;
475 if(dnvars.names < target || ((long)(nextage - now) > 0 && !doit)){
476 dnvars.oldest = maxage;
480 if(dnvars.names >= target) {
481 dnslog("more names (%lud) than target (%lud)", dnvars.names,
484 if (dnvars.oldest < Minage)
485 dnvars.oldest = Minage; /* don't be silly */
487 if (agefreq > dnvars.oldest / 2)
488 nextage = now + dnvars.oldest / 2;
490 nextage = now + (ulong)agefreq;
494 /* time out all old entries (and set refs to 0) */
495 for(i = 0; i < HTLEN; i++)
496 for(dp = ht[i]; dp; dp = dp->next){
501 /* mark all referenced domain names */
502 for(i = 0; i < HTLEN; i++)
503 for(dp = ht[i]; dp; dp = dp->next)
504 for(rp = dp->rr; rp; rp = rp->next){
507 REF(rp->negsoaowner);
550 REF(rp->sig->signer);
555 /* sweep and remove unreferenced domain names */
556 for(i = 0; i < HTLEN; i++){
558 for(dp = *l; dp; dp = *l){
559 if(dp->rr == nil && dp->mark == 0){
560 assert(dp->magic == DNmagic);
564 memset(dp, 0, sizeof *dp); /* cause trouble */
565 dp->magic = ~DNmagic;
579 * timeout all database records (used when rereading db)
590 /* time out all database entries */
591 for(i = 0; i < HTLEN; i++)
592 for(dp = ht[i]; dp; dp = dp->next) {
594 for(rp = dp->rr; rp; rp = rp->next)
603 * mark all local db records about my area as authoritative,
604 * delete timed out ones
617 /* time out all database entries */
618 for(i = 0; i < HTLEN; i++)
619 for(dp = ht[i]; dp; dp = dp->next){
620 area = inmyarea(dp->name);
622 for(rp = *l; rp; rp = *l){
629 minttl = area->soarr->soa->minttl;
643 * keep track of other processes to know if we can
644 * garbage collect. block while garbage collecting.
647 getactivity(Request *req, int recursive)
652 dnslog("get: %d active by pid %d from %p",
653 dnvars.active, getpid(), getcallerpc(&req));
656 * can't block here if we're already holding one
657 * of the dnvars.active (recursive). will deadlock.
659 while(!recursive && dnvars.mutex){
661 sleep(100); /* tune; was 200 */
664 rv = ++dnvars.active;
667 req->id = ++dnvars.id;
674 putactivity(int recursive)
676 static ulong lastclean;
679 dnslog("put: %d active by pid %d",
680 dnvars.active, getpid());
683 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
686 * clean out old entries and check for new db periodicly
687 * can't block here if being called to let go a "recursive" lock
688 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
690 if (recursive || dnvars.mutex ||
691 (needrefresh == 0 && dnvars.active > 0)){
696 /* wait till we're alone */
698 while(dnvars.active > 0){
700 sleep(100); /* tune; was 100 */
707 db2cache(needrefresh);
714 /* let others back in */
726 for(; rp; rp = rp->next)
732 * Attach a single resource record to a domain name (new->owner).
733 * - Avoid duplicates with already present RR's
734 * - Chain all RR's of the same type adjacent to one another
735 * - chain authoritative RR's ahead of non-authoritative ones
736 * - remove any expired RR's
737 * If new is a stale duplicate, rrfree it.
738 * Must be called with dnlock held.
741 rrattach1(RR *new, int auth)
748 assert(new->magic == RRmagic && !new->cached);
751 assert(dp != nil && dp->magic == DNmagic);
756 * try not to let responses expire before we
757 * can use them to complete this query, by extending
758 * past (or nearly past) expiration time.
766 new->expire = now + ttl;
769 * find first rr of the right type
772 for(rp = *l; rp; rp = *l){
773 assert(rp->magic == RRmagic && rp->cached);
774 if(rp->type == new->type)
780 * negative entries replace positive entries
781 * positive entries replace negative entries
782 * newer entries replace older entries with the same fields
784 * look farther ahead than just the next entry when looking
785 * for duplicates; RRs of a given type can have different rdata
786 * fields (e.g. multiple NS servers).
788 while ((rp = *l) != nil){
789 assert(rp->magic == RRmagic && rp->cached);
790 if(rp->type != new->type)
793 if(rp->db == new->db && rp->auth == new->auth){
794 /* negative drives out positive and vice versa */
795 if(rp->negative != new->negative) {
796 /* rp == *l before; *l == rp->next after */
800 /* all things equal, pick the newer one */
801 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
802 /* old drives out new */
803 if((long)(rp->expire - new->expire) > 0) {
807 /* rp == *l before; *l == rp->next after */
812 * Hack for pointer records. This makes sure
813 * the ordering in the list reflects the ordering
814 * received or read from the database
816 else if(rp->type == Tptr &&
817 !rp->negative && !new->negative &&
818 rp->ptr->ordinal > new->ptr->ordinal)
824 if (rronlist(new, rp)) {
825 /* should not happen; duplicates were processed above */
826 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
838 * Attach a list of resource records to a domain name.
839 * May rrfree any stale duplicate RRs; dismembers the list.
840 * Upon return, every RR in the list will have been rrfree-d
841 * or attached to its domain name.
842 * See rrattach1 for properties preserved.
845 rrattach(RR *rp, int auth)
851 for(; rp; rp = next){
855 /* avoid any outside spoofing */
856 if(cfg.cachedb && !rp->db && inmyarea(dp->name))
865 rrcopy(RR *rp, RR **last)
876 assert(rp->magic == RRmagic);
877 nrp = rralloc(rp->type);
884 soa->slaves = copyserverlist(rp->soa->slaves);
897 key->data = emalloc(key->dlen);
898 memmove(key->data, rp->key->data, rp->key->dlen);
905 cert->data = emalloc(cert->dlen);
906 memmove(cert->data, rp->cert->data, rp->cert->dlen);
913 sig->data = emalloc(sig->dlen);
914 memmove(sig->data, rp->sig->data, rp->sig->dlen);
921 null->data = emalloc(null->dlen);
922 memmove(null->data, rp->null->data, rp->null->dlen);
928 for(t = rp->txt; t != nil; t = t->next){
929 nt = emalloc(sizeof(*nt));
930 nt->p = estrdup(t->p);
940 nrp->pc = getcallerpc(&rp);
941 setmalloctag(nrp, nrp->pc);
949 * lookup a resource record of a particular type and
950 * class attached to a domain name. Return copies.
952 * Priority ordering is:
954 * not timed out network authoritative
955 * not timed out network unauthoritative
958 * if flag NOneg is set, don't return negative cached entries.
959 * return nothing instead.
962 rrlookup(DN *dp, int type, int flag)
964 RR *rp, *first, **last;
966 assert(dp->magic == DNmagic);
972 /* try for an authoritative db entry */
973 for(rp = dp->rr; rp; rp = rp->next){
974 assert(rp->magic == RRmagic && rp->cached);
977 if(tsame(type, rp->type))
978 last = rrcopy(rp, last);
983 /* try for a living authoritative network entry */
984 for(rp = dp->rr; rp; rp = rp->next){
987 if((long)(rp->expire - now) > 0)
988 if(tsame(type, rp->type)){
989 if(flag == NOneg && rp->negative)
991 last = rrcopy(rp, last);
997 /* try for a living unauthoritative network entry */
998 for(rp = dp->rr; rp; rp = rp->next){
1000 if((long)(rp->expire - now) > 0)
1001 if(tsame(type, rp->type)){
1002 if(flag == NOneg && rp->negative)
1004 last = rrcopy(rp, last);
1010 /* try for an unauthoritative db entry */
1011 for(rp = dp->rr; rp; rp = rp->next){
1013 if(tsame(type, rp->type))
1014 last = rrcopy(rp, last);
1019 /* otherwise, settle for anything we got (except for negative caches) */
1020 for(rp = dp->rr; rp; rp = rp->next)
1021 if(tsame(type, rp->type)){
1024 last = rrcopy(rp, last);
1034 * convert an ascii RR type name to its integer representation
1041 for(i = 0; i <= Tall; i++)
1042 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1045 /* make any a synonym for all */
1046 if(strcmp(atype, "any") == 0)
1048 else if(isascii(atype[0]) && isdigit(atype[0]))
1055 * return 0 if not a supported rr type
1058 rrsupported(int type)
1060 if(type < 0 || type >Tall)
1062 return rrtname[type] != nil;
1069 tsame(int t1, int t2)
1071 return t1 == t2 || t1 == Tall;
1075 * Add resource records to a list.
1078 rrcat(RR **start, RR *rp)
1083 /* check for duplicates */
1084 for (olp = *start; 0 && olp; olp = olp->next)
1085 for (nlp = rp; nlp; nlp = nlp->next)
1086 if (rrsame(nlp, olp))
1087 dnslog("rrcat: duplicate RR: %R", nlp);
1092 last = &(*last)->next;
1099 rrremfilter(RR **l, int (*filter)(RR*, void*), void *arg)
1108 if((*filter)(rp, arg)){
1121 filterneg(RR *rp, void*)
1123 return rp->negative;
1126 filtertype(RR *rp, void *arg)
1128 return rp->type == *((int*)arg);
1131 filterowner(RR *rp, void *arg)
1133 return rp->owner == (DN*)arg;
1137 * remove negative cache rr's from an rr list
1142 return rrremfilter(l, filterneg, nil);
1146 * remove rr's of a particular type from an rr list
1149 rrremtype(RR **l, int type)
1151 return rrremfilter(l, filtertype, &type);
1155 * remove rr's of a particular owner from an rr list
1158 rrremowner(RR **l, DN *owner)
1160 return rrremfilter(l, filterowner, owner);
1166 return dn? dn->name: "<null>";
1170 idnname(DN *dn, char *buf, int nbuf)
1175 if(idn2utf(name, buf, nbuf) != nil)
1181 * print conversion for rr records
1198 rp = va_arg(f->args, RR*);
1200 fmtprint(&fstr, "<null>");
1204 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1205 rrname(rp->type, buf, sizeof buf));
1208 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1214 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1221 fmtprint(&fstr, "\t%s", dnname(rp->host));
1225 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1228 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1231 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1235 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1238 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1242 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1243 dnname(rp->host), dnname(rp->rmb),
1244 (soa? soa->serial: 0),
1245 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1246 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1248 for(s = soa->slaves; s != nil; s = s->next)
1249 fmtprint(&fstr, " %s", s->name);
1253 fmtprint(&fstr, "\t%ud %ud %ud %s",
1254 (srv? srv->pri: 0), (srv? srv->weight: 0),
1255 rp->port, dnname(rp->host));
1258 if (rp->null == nil)
1259 fmtprint(&fstr, "\t<null>");
1261 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1265 fmtprint(&fstr, "\t");
1266 for(t = rp->txt; t != nil; t = t->next)
1267 fmtprint(&fstr, "%s", t->p);
1270 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1274 fmtprint(&fstr, "\t<null> <null> <null>");
1276 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1277 rp->key->proto, rp->key->alg);
1282 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1284 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1285 rp->sig->type, rp->sig->alg, rp->sig->labels,
1286 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1287 rp->sig->tag, dnname(rp->sig->signer));
1290 if (rp->cert == nil)
1291 fmtprint(&fstr, "\t<null> <null> <null>");
1293 fmtprint(&fstr, "\t%d %d %d",
1294 rp->cert->type, rp->cert->tag, rp->cert->alg);
1298 strp = fmtstrflush(&fstr);
1299 rv = fmtstrcpy(f, strp);
1305 * print conversion for rr records in attribute value form
1311 char buf[Domlen], *strp;
1321 rp = va_arg(f->args, RR*);
1323 fmtprint(&fstr, "<null>");
1327 if(rp->type == Tptr)
1328 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1330 fmtprint(&fstr, "dom=%s", idnname(rp->owner, buf, sizeof(buf)));
1334 fmtprint(&fstr, " cpu=%s os=%s",
1335 idnname(rp->cpu, buf, sizeof(buf)),
1336 idnname(rp->os, buf, sizeof(buf)));
1339 fmtprint(&fstr, " cname=%s", idnname(rp->host, buf, sizeof(buf)));
1344 fmtprint(&fstr, " mbox=%s", idnname(rp->host, buf, sizeof(buf)));
1347 fmtprint(&fstr, " ns=%s", idnname(rp->host, buf, sizeof(buf)));
1351 fmtprint(&fstr, " mbox=%s", idnname(rp->mb, buf, sizeof(buf)));
1354 fmtprint(&fstr, " mbox=%s mbox=%s",
1355 idnname(rp->mb, buf, sizeof(buf)),
1356 idnname(rp->rmb, buf, sizeof(buf)));
1359 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref,
1360 idnname(rp->host, buf, sizeof(buf)));
1364 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1367 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1372 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1373 idnname(rp->host, buf, sizeof(buf)),
1374 idnname(rp->rmb, buf, sizeof(buf)),
1375 (soa? soa->serial: 0),
1376 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1377 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1378 for(s = soa->slaves; s != nil; s = s->next)
1379 fmtprint(&fstr, " dnsslave=%s", s->name);
1383 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1384 (srv? srv->pri: 0), (srv? srv->weight: 0),
1385 rp->port, idnname(rp->host, buf, sizeof(buf)));
1388 if (rp->null == nil)
1389 fmtprint(&fstr, " null=<null>");
1391 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1395 fmtprint(&fstr, " txt=");
1397 for(t = rp->txt; t != nil; t = t->next)
1398 if(strchr(t->p, ' '))
1401 fmtprint(&fstr, "\"");
1402 for(t = rp->txt; t != nil; t = t->next)
1403 fmtprint(&fstr, "%s", t->p);
1405 fmtprint(&fstr, "\"");
1408 fmtprint(&fstr, " rp=%s txt=%s",
1409 idnname(rp->rmb, buf, sizeof(buf)),
1410 idnname(rp->rp, buf, sizeof(buf)));
1414 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1416 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1417 rp->key->flags, rp->key->proto, rp->key->alg);
1422 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1425 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1426 rp->sig->type, rp->sig->alg, rp->sig->labels,
1427 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1428 rp->sig->tag, idnname(rp->sig->signer, buf, sizeof(buf)));
1431 if (rp->cert == nil)
1432 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1434 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1435 rp->cert->type, rp->cert->tag, rp->cert->alg);
1439 strp = fmtstrflush(&fstr);
1440 rv = fmtstrcpy(f, strp);
1446 warning(char *fmt, ...)
1452 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1454 syslog(1, logfile, dnserr); /* on console too */
1458 dnslog(char *fmt, ...)
1464 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1466 syslog(0, logfile, dnserr);
1470 * based on libthread's threadsetname, but drags in less library code.
1471 * actually just sets the arguments displayed.
1474 procsetname(char *fmt, ...)
1482 cmdname = vsmprint(fmt, arg);
1486 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1487 if((fd = open(buf, OWRITE)) >= 0){
1488 write(fd, cmdname, strlen(cmdname)+1);
1495 * create a slave process to handle a request to avoid one request blocking
1504 return; /* we're already a slave process */
1507 * These calls to putactivity cannot block.
1508 * After getactivity(), the current process is counted
1509 * twice in dnvars.active (one will pass to the child).
1510 * If putactivity tries to wait for dnvars.active == 0,
1511 * it will never happen.
1514 /* limit parallelism */
1515 procs = getactivity(req, 1);
1516 if(procs > stats.slavehiwat)
1517 stats.slavehiwat = procs;
1518 if(procs > Maxactive){
1520 dnslog("[%d] too much activity", getpid());
1526 * parent returns to main loop, child does the work.
1527 * don't change note group.
1530 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1535 procsetname("request slave of pid %d", ppid);
1537 dnslog("[%d] take activity from %d", getpid(), ppid);
1538 req->isslave = 1; /* why not `= getpid()'? */
1542 * this relies on rfork producing separate, initially-identical
1543 * stacks, thus giving us two copies of `req', one in each
1547 longjmp(req->mret, 1);
1552 * chasing down double free's
1566 for(i = 0; i < HTLEN; i++)
1567 for(dp = ht[i]; dp; dp = dp->next){
1568 assert(dp->magic == DNmagic);
1569 for(rp = dp->rr; rp; rp = rp->next){
1570 assert(rp->magic == RRmagic);
1572 assert(rp->owner == dp);
1573 /* also check for duplicate rrs */
1574 if (rronlist(rp, rp->next)) {
1575 dnslog("%R duplicates its next chain "
1576 "(%R); aborting", rp, rp->next);
1585 rrequiv(RR *r1, RR *r2)
1587 return r1->owner == r2->owner
1588 && r1->type == r2->type
1589 && r1->arg0 == r2->arg0
1590 && r1->arg1 == r2->arg1;
1598 for(; rp; rp = rp->next){
1600 for(nrp = *l; nrp; nrp = *l)
1601 if(rrequiv(rp, nrp)){
1610 * true if second domain is subsumed by the first
1613 subsume(char *higher, char *lower)
1618 hn = strlen(higher);
1619 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1620 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1626 * randomize the order we return items to provide some
1627 * load balancing for servers.
1629 * only randomize the first class of entries
1634 RR *first, *last, *x, *base;
1637 if(rp == nil || rp->next == nil)
1640 /* just randomize addresses, mx's and ns's */
1641 for(x = rp; x; x = x->next)
1642 if(x->type != Ta && x->type != Taaaa &&
1643 x->type != Tmx && x->type != Tns)
1651 /* stop randomizing if we've moved past our class */
1652 if(base->auth != rp->auth || base->db != rp->db){
1677 /* reroll the dice */
1687 int i, len, ilen, rv;
1690 char obuf[64]; /* rsc optimization */
1692 if(!(f->flags&FmtPrec) || f->prec < 1)
1695 b = va_arg(f->args, uchar*);
1699 /* if it's a printable, go for it */
1701 for(i = 0; i < len; i++)
1705 if(len >= sizeof obuf)
1706 len = sizeof(obuf)-1;
1707 memmove(obuf, b, len);
1715 f->flags &= ~FmtPrec;
1717 if(len > sizeof(obuf)){
1726 rv = enc16(out, len, b, ilen);
1736 return fmtstrcpy(f, "<encodefmt>");
1744 x = mallocz(size, 1);
1747 setmalloctag(x, getcallerpc(&size));
1758 p = mallocz(size+1, 0);
1761 memmove(p, s, size);
1763 setmalloctag(p, getcallerpc(&s));
1768 * create a pointer record
1771 mkptr(DN *dp, char *ptr, ulong ttl)
1776 ipdp = dnlookup(ptr, Cin, 1);
1787 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1790 * look for all ip addresses in this network and make
1791 * pointer records for them.
1794 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1800 uchar ip[IPaddrlen], nnet[IPaddrlen];
1801 uchar nibip[IPaddrlen*2];
1803 RR *rp, *nrp, *first, **l;
1807 for(i = 0; i < HTLEN; i++)
1808 for(dp = ht[i]; dp; dp = dp->next)
1809 for(rp = dp->rr; rp; rp = rp->next){
1810 if(rp->type != forwtype || rp->negative)
1812 parseip(ip, rp->ip->name);
1813 maskip(ip, mask, nnet);
1814 if(ipcmp(net, nnet) != 0)
1819 if (forwtype == Taaaa) {
1820 bytes2nibbles(nibip, ip, IPaddrlen);
1826 e = ptr+sizeof(ptr);
1827 for(j = len - 1; j >= len - subdoms; j--)
1828 p = seprint(p, e, (forwtype == Ta?
1829 "%d.": "%x."), ipp[j]);
1830 seprint(p, e, "%s", dom);
1832 nrp = mkptr(dp, ptr, ttl);
1837 for(rp = first; rp != nil; rp = nrp){
1841 rrattach(rp, Authoritative);
1847 addserver(Server **l, char *name)
1855 s = malloc(sizeof(Server)+n+1);
1858 s->name = (char*)(s+1);
1859 memmove(s->name, name, n);
1866 copyserverlist(Server *s)
1870 for(ns = nil; s != nil; s = s->next)
1871 addserver(&ns, s->name);
1876 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1879 * convert an integer RR type to it's ascii name
1882 rrname(int type, char *buf, int len)
1887 if(type >= 0 && type <= Tall)
1890 snprint(buf, len, "%d", type);
1897 * free a list of resource records and any related structs
1904 for(; rp; rp = next){
1911 freeserverlist(Server *s)
1915 for(; s != nil; s = next){
1917 memset(s, 0, sizeof *s); /* cause trouble */
1923 * allocate a resource record of a given type
1930 rp = emalloc(sizeof(*rp));
1931 rp->magic = RRmagic;
1932 rp->pc = getcallerpc(&type);
1934 if (rp->type != type)
1935 dnslog("rralloc: bogus type %d", type);
1936 setmalloctag(rp, rp->pc);
1939 rp->soa = emalloc(sizeof(*rp->soa));
1940 rp->soa->slaves = nil;
1941 setmalloctag(rp->soa, rp->pc);
1944 rp->srv = emalloc(sizeof(*rp->srv));
1945 setmalloctag(rp->srv, rp->pc);
1948 rp->key = emalloc(sizeof(*rp->key));
1949 setmalloctag(rp->key, rp->pc);
1952 rp->cert = emalloc(sizeof(*rp->cert));
1953 setmalloctag(rp->cert, rp->pc);
1956 rp->sig = emalloc(sizeof(*rp->sig));
1957 setmalloctag(rp->sig, rp->pc);
1960 rp->null = emalloc(sizeof(*rp->null));
1961 setmalloctag(rp->null, rp->pc);
1971 * free a resource record and any related structs
1978 assert(rp->magic == RRmagic && !rp->cached);
1982 freeserverlist(rp->soa->slaves);
1983 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
1987 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
1991 free(rp->key->data);
1992 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
1996 free(rp->cert->data);
1997 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
2001 free(rp->sig->data);
2002 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
2006 free(rp->null->data);
2007 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
2014 memset(t, 0, sizeof *t); /* cause trouble */
2020 memset(rp, 0, sizeof *rp); /* cause trouble */
2021 rp->magic = ~RRmagic;