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 Deftarget = 1<<30, /* effectively disable aging */
19 Defagefreq = 1<<30, /* age names this often (seconds) */
21 /* these settings will trigger frequent aging */
24 // Defagefreq = 15*60, /* age names this often (seconds) */
28 * Hash table for domain names. The hash is based only on the
29 * first element of the domain name.
35 ulong names; /* names allocated */
36 ulong oldest; /* longest we'll leave a name around */
39 ushort id; /* same size as in packet */
42 /* names of RR types */
67 [Tnsapptr] "nsap-ptr",
89 [Tipseckey] "ipseckey",
108 /* names of response codes */
109 char *rname[Rmask+1] =
112 [Rformat] "format error",
113 [Rserver] "server failure",
115 [Runimplimented] "unimplemented",
116 [Rrefused] "we don't like you",
117 [Ryxdomain] "name should not exist",
118 [Ryxrrset] "rr set should not exist",
119 [Rnxrrset] "rr set should exist",
120 [Rnotauth] "not authorative",
121 [Rnotzone] "not in zone",
122 [Rbadvers] "bad opt version",
123 /* [Rbadsig] "bad signature", */
125 [Rbadtime] "bad signature time",
126 [Rbadmode] "bad mode",
127 [Rbadname] "duplicate key name",
128 [Rbadalg] "bad algorithm",
130 unsigned nrname = nelem(rname);
132 /* names of op codes */
136 [Oinverse] "inverse query (retired)",
141 ulong target = Deftarget;
144 static ulong agefreq = Defagefreq;
146 static int rrequiv(RR *r1, RR *r2);
147 static int sencodefmt(Fmt*);
150 ding(void*, char *msg)
152 if(strstr(msg, "alarm") != nil) {
154 noted(NCONT); /* resume with system call error */
156 noted(NDFLT); /* die */
162 fmtinstall('E', eipfmt);
163 fmtinstall('I', eipfmt);
164 fmtinstall('V', eipfmt);
165 fmtinstall('R', rrfmt);
166 fmtinstall('Q', rravfmt);
167 fmtinstall('H', sencodefmt);
169 dnvars.oldest = maxage;
171 dnvars.id = truerand(); /* don't start with same id every time */
177 * hash for a domain name
183 uchar *val = (uchar*)name;
185 for(hash = 0; *val; val++)
186 hash = hash*13 + tolower(*val)-'a';
191 * lookup a symbol. if enter is not zero and the name is
192 * not found, create it.
195 dnlookup(char *name, int class, int enter)
200 l = &ht[dnhash(name)];
202 for(dp = *l; dp; dp = dp->next) {
203 assert(dp->magic == DNmagic);
204 if(dp->class == class && cistrcmp(dp->name, name) == 0){
205 dp->referenced = now;
217 dp = emalloc(sizeof(*dp));
219 dp->name = estrdup(name);
220 assert(dp->name != nil);
223 dp->referenced = now;
224 /* add new DN to tail of the hash list. *l points to last next ptr. */
233 rrsame(RR *rr1, RR *rr2)
235 return rr1 == rr2 || rr2 && rrequiv(rr1, rr2) &&
236 rr1->db == rr2->db && rr1->auth == rr2->auth;
240 rronlist(RR *rp, RR *lp)
242 for(; lp; lp = lp->next)
256 fd = create(file, OWRITE, 0666);
261 fprint(fd, "# system %s\n", sysname());
262 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
263 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
264 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
265 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
266 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
267 for (i = 0; i < nelem(stats.under10ths); i++)
268 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
269 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
270 (double)(i+1)/10, stats.under10ths[i]);
271 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
272 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
273 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
274 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
275 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
276 fprint(fd, "# negative answers w bad delegation\t%lud\n",
278 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
280 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
281 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
285 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
300 fd = create(file, OWRITE, 0666);
305 for(i = 0; i < HTLEN; i++)
306 for(dp = ht[i]; dp; dp = dp->next){
307 fprint(fd, "%s\n", dp->name);
308 for(rp = dp->rr; rp; rp = rp->next) {
309 fprint(fd, "\t%R %c%c %lud/%lud\n",
310 rp, rp->auth? 'A': 'U',
311 rp->db? 'D': 'N', rp->expire, rp->ttl);
312 if (rronlist(rp, rp->next))
313 fprint(fd, "*** duplicate:\n");
332 for(i = 0; i < HTLEN; i++)
333 for(dp = ht[i]; dp; dp = dp->next){
336 for(; rp != nil; rp = rp->next)
345 * delete head of *l and free the old head.
346 * call with dnlock held.
353 if (canlock(&dnlock))
354 abort(); /* rrdelhead called with dnlock not held */
358 *l = rp->next; /* unlink head */
359 rp->cached = 0; /* avoid blowing an assertion in rrfree */
364 * check the age of resource records, free any that have timed out.
365 * call with dnlock held.
374 if (canlock(&dnlock))
375 abort(); /* dnage called with dnlock not held */
376 diff = now - dp->referenced;
377 if(diff < Reserved || dp->keep)
381 for(rp = dp->rr; rp; rp = next){
382 assert(rp->magic == RRmagic && rp->cached);
384 if(!rp->db && (rp->expire < now || diff > dnvars.oldest))
385 rrdelhead(l); /* rp == *l before; *l == rp->next after */
391 #define MARK(dp) { if (dp) (dp)->keep = 1; }
393 /* mark a domain name and those in its RRs as never to be aged */
395 dnagenever(DN *dp, int dolock)
402 /* mark all referenced domain names */
404 for(rp = dp->rr; rp; rp = rp->next){
407 MARK(rp->negsoaowner);
456 /* mark all current domain names as never to be aged */
465 /* mark all referenced domain names */
466 for(i = 0; i < HTLEN; i++)
467 for(dp = ht[i]; dp; dp = dp->next)
472 dnslog("%ld initial domain names; target is %ld", dnvars.names, target);
473 if(dnvars.names >= target)
474 dnslog("more initial domain names (%ld) than target (%ld)",
475 dnvars.names, target);
478 #define REF(dp) { if (dp) (dp)->refs++; }
481 * periodicly sweep for old records and remove unreferenced domain names
483 * only called when all other threads are locked out
491 static ulong nextage;
493 if(dnvars.names < target || (now < nextage && !doit)){
494 dnvars.oldest = maxage;
498 if(dnvars.names >= target) {
499 dnslog("more names (%lud) than target (%lud)", dnvars.names,
502 if (dnvars.oldest < Minage)
503 dnvars.oldest = Minage; /* don't be silly */
505 if (agefreq > dnvars.oldest / 2)
506 nextage = now + dnvars.oldest / 2;
508 nextage = now + agefreq;
512 /* time out all old entries (and set refs to 0) */
513 for(i = 0; i < HTLEN; i++)
514 for(dp = ht[i]; dp; dp = dp->next){
519 /* mark all referenced domain names */
520 for(i = 0; i < HTLEN; i++)
521 for(dp = ht[i]; dp; dp = dp->next)
522 for(rp = dp->rr; rp; rp = rp->next){
525 REF(rp->negsoaowner);
570 /* sweep and remove unreferenced domain names */
571 for(i = 0; i < HTLEN; i++){
573 for(dp = *l; dp; dp = *l){
574 if(dp->rr == 0 && dp->refs == 0 && !dp->keep){
575 assert(dp->magic == DNmagic);
580 dp->magic = ~dp->magic;
582 memset(dp, 0, sizeof *dp); /* cause trouble */
595 * timeout all database records (used when rereading db)
606 /* time out all database entries */
607 for(i = 0; i < HTLEN; i++)
608 for(dp = ht[i]; dp; dp = dp->next) {
610 for(rp = dp->rr; rp; rp = rp->next)
619 * mark all local db records about my area as authoritative,
620 * time out any others
633 /* time out all database entries */
634 for(i = 0; i < HTLEN; i++)
635 for(dp = ht[i]; dp; dp = dp->next){
636 area = inmyarea(dp->name);
637 for(rp = dp->rr; rp; rp = rp->next)
640 minttl = area->soarr->soa->minttl;
647 dp->referenced = now-Reserved-1;
656 * keep track of other processes to know if we can
657 * garbage collect. block while garbage collecting.
660 getactivity(Request *req, int recursive)
665 dnslog("get: %d active by pid %d from %p",
666 dnvars.active, getpid(), getcallerpc(&req));
669 * can't block here if we're already holding one
670 * of the dnvars.active (recursive). will deadlock.
672 while(!recursive && dnvars.mutex){
674 sleep(100); /* tune; was 200 */
677 rv = ++dnvars.active;
680 req->id = ++dnvars.id;
686 putactivity(int recursive)
688 static ulong lastclean;
691 dnslog("put: %d active by pid %d",
692 dnvars.active, getpid());
695 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
698 * clean out old entries and check for new db periodicly
699 * can't block here if being called to let go a "recursive" lock
700 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
702 if (recursive || dnvars.mutex ||
703 (needrefresh == 0 && dnvars.active > 0)){
708 /* wait till we're alone */
710 while(dnvars.active > 0){
712 sleep(100); /* tune; was 100 */
717 db2cache(needrefresh);
720 /* let others back in */
732 for(; rp; rp = rp->next)
738 * Attach a single resource record to a domain name (new->owner).
739 * - Avoid duplicates with already present RR's
740 * - Chain all RR's of the same type adjacent to one another
741 * - chain authoritative RR's ahead of non-authoritative ones
742 * - remove any expired RR's
743 * If new is a stale duplicate, rrfree it.
744 * Must be called with dnlock held.
747 rrattach1(RR *new, int auth)
753 assert(new->magic == RRmagic && !new->cached);
755 // dnslog("rrattach1: %s", new->owner->name);
758 * try not to let responses expire before we
759 * can use them to complete this query, by extending
760 * past (or nearly past) expiration time.
762 new->expire = new->ttl > now + Min? new->ttl: now + 10*Min;
764 new->expire = now + Year;
766 assert(dp->magic == DNmagic);
771 * find first rr of the right type
774 for(rp = *l; rp; rp = *l){
775 assert(rp->magic == RRmagic && rp->cached);
776 if(rp->type == new->type)
782 * negative entries replace positive entries
783 * positive entries replace negative entries
784 * newer entries replace older entries with the same fields
786 * look farther ahead than just the next entry when looking
787 * for duplicates; RRs of a given type can have different rdata
788 * fields (e.g. multiple NS servers).
790 while ((rp = *l) != nil){
791 assert(rp->magic == RRmagic && rp->cached);
792 if(rp->type != new->type)
795 if(rp->db == new->db && rp->auth == new->auth){
796 /* negative drives out positive and vice versa */
797 if(rp->negative != new->negative) {
798 /* rp == *l before; *l == rp->next after */
802 /* all things equal, pick the newer one */
803 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
804 /* new drives out old */
805 if (new->ttl <= rp->ttl &&
806 new->expire <= rp->expire) {
810 /* rp == *l before; *l == rp->next after */
815 * Hack for pointer records. This makes sure
816 * the ordering in the list reflects the ordering
817 * received or read from the database
819 else if(rp->type == Tptr &&
820 !rp->negative && !new->negative &&
821 rp->ptr->ordinal > new->ptr->ordinal)
827 if (rronlist(new, rp)) {
828 /* should not happen; duplicates were processed above */
829 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
841 * Attach a list of resource records to a domain name.
842 * May rrfree any stale duplicate RRs; dismembers the list.
843 * Upon return, every RR in the list will have been rrfree-d
844 * or attached to its domain name.
845 * See rrattach1 for properties preserved.
848 rrattach(RR *rp, int auth)
854 for(; rp; rp = next){
859 // dnslog("rrattach: %s", rp->owner->name);
860 /* avoid any outside spoofing; leave keepers alone */
861 if(cfg.cachedb && !rp->db && inmyarea(rp->owner->name)
862 // || dp->keep /* TODO: make this work */
866 /* ameliorate the memory leak (someday delete this) */
867 if (0 && rrlistlen(dp->rr) > 50 && !dp->keep) {
868 dnslog("rrattach(%s): rr list too long; "
869 "freeing it", dp->name);
881 /* should be called with dnlock held */
883 rrcopy(RR *rp, RR **last)
893 if (canlock(&dnlock))
894 abort(); /* rrcopy called with dnlock not held */
895 nrp = rralloc(rp->type);
896 setmalloctag(nrp, getcallerpc(&rp));
902 for(t = rp->txt; t != nil; t = t->next){
903 nt = emalloc(sizeof(*nt));
904 nt->p = estrdup(t->p);
914 *nrp->soa = *rp->soa;
915 nrp->soa->slaves = copyserverlist(rp->soa->slaves);
919 nrp->srv = emalloc(sizeof *nrp->srv);
920 *nrp->srv = *rp->srv;
927 key->data = emalloc(key->dlen);
928 memmove(key->data, rp->key->data, rp->key->dlen);
935 sig->data = emalloc(sig->dlen);
936 memmove(sig->data, rp->sig->data, rp->sig->dlen);
943 cert->data = emalloc(cert->dlen);
944 memmove(cert->data, rp->cert->data, rp->cert->dlen);
951 null->data = emalloc(null->dlen);
952 memmove(null->data, rp->null->data, rp->null->dlen);
965 * lookup a resource record of a particular type and
966 * class attached to a domain name. Return copies.
968 * Priority ordering is:
970 * not timed out network authoritative
971 * not timed out network unauthoritative
974 * if flag NOneg is set, don't return negative cached entries.
975 * return nothing instead.
978 rrlookup(DN *dp, int type, int flag)
980 RR *rp, *first, **last;
982 assert(dp->magic == DNmagic);
988 /* try for an authoritative db entry */
989 for(rp = dp->rr; rp; rp = rp->next){
990 assert(rp->magic == RRmagic && rp->cached);
993 if(tsame(type, rp->type))
994 last = rrcopy(rp, last);
999 /* try for a living authoritative network entry */
1000 for(rp = dp->rr; rp; rp = rp->next){
1003 if(rp->ttl + 60 > now)
1004 if(tsame(type, rp->type)){
1005 if(flag == NOneg && rp->negative)
1007 last = rrcopy(rp, last);
1013 /* try for a living unauthoritative network entry */
1014 for(rp = dp->rr; rp; rp = rp->next){
1016 if(rp->ttl + 60 > now)
1017 if(tsame(type, rp->type)){
1018 if(flag == NOneg && rp->negative)
1020 last = rrcopy(rp, last);
1026 /* try for an unauthoritative db entry */
1027 for(rp = dp->rr; rp; rp = rp->next){
1029 if(tsame(type, rp->type))
1030 last = rrcopy(rp, last);
1035 /* otherwise, settle for anything we got (except for negative caches) */
1036 for(rp = dp->rr; rp; rp = rp->next)
1037 if(tsame(type, rp->type)){
1040 last = rrcopy(rp, last);
1050 * convert an ascii RR type name to its integer representation
1057 for(i = 0; i <= Tall; i++)
1058 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1061 /* make any a synonym for all */
1062 if(strcmp(atype, "any") == 0)
1064 else if(isascii(atype[0]) && isdigit(atype[0]))
1071 * return 0 if not a supported rr type
1074 rrsupported(int type)
1076 if(type < 0 || type >Tall)
1078 return rrtname[type] != nil;
1085 tsame(int t1, int t2)
1087 return t1 == t2 || t1 == Tall;
1091 * Add resource records to a list, duplicate them if they are cached
1092 * RR's since these are shared. should be called with dnlock held
1093 * to avoid racing down the start chain.
1096 rrcat(RR **start, RR *rp)
1101 if (canlock(&dnlock))
1102 abort(); /* rrcat called with dnlock not held */
1103 /* check for duplicates */
1104 for (olp = *start; 0 && olp; olp = olp->next)
1105 for (nlp = rp; nlp; nlp = nlp->next)
1106 if (rrsame(nlp, olp))
1107 dnslog("rrcat: duplicate RR: %R", nlp);
1112 last = &(*last)->next;
1119 * remove negative cache rr's from an rr list
1127 if (canlock(&dnlock))
1128 abort(); /* rrremneg called with dnlock not held */
1146 * remove rr's of a particular type from an rr list
1149 rrremtype(RR **l, int type)
1158 if(rp->type == type){
1173 return dn? dn->name: "<null>";
1177 * print conversion for rr records
1194 rp = va_arg(f->args, RR*);
1196 fmtprint(&fstr, "<null>");
1200 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1201 rrname(rp->type, buf, sizeof buf));
1204 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1210 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1217 fmtprint(&fstr, "\t%s", dnname(rp->host));
1221 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1224 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1227 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1231 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1234 // fmtprint(&fstr, "\t%s(%lud)", dnname(rp->ptr),
1235 // rp->ptr? rp->ptr->ordinal: "<null>");
1236 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1240 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1241 dnname(rp->host), dnname(rp->rmb),
1242 (soa? soa->serial: 0),
1243 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1244 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1246 for(s = soa->slaves; s != nil; s = s->next)
1247 fmtprint(&fstr, " %s", s->name);
1251 fmtprint(&fstr, "\t%ud %ud %ud %s",
1252 (srv? srv->pri: 0), (srv? srv->weight: 0),
1253 rp->port, dnname(rp->host));
1256 if (rp->null == nil)
1257 fmtprint(&fstr, "\t<null>");
1259 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1263 fmtprint(&fstr, "\t");
1264 for(t = rp->txt; t != nil; t = t->next)
1265 fmtprint(&fstr, "%s", t->p);
1268 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1272 fmtprint(&fstr, "\t<null> <null> <null>");
1274 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1275 rp->key->proto, rp->key->alg);
1280 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1282 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1283 rp->sig->type, rp->sig->alg, rp->sig->labels,
1284 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1285 rp->sig->tag, dnname(rp->sig->signer));
1288 if (rp->cert == nil)
1289 fmtprint(&fstr, "\t<null> <null> <null>");
1291 fmtprint(&fstr, "\t%d %d %d",
1292 rp->cert->type, rp->cert->tag, rp->cert->alg);
1296 strp = fmtstrflush(&fstr);
1297 rv = fmtstrcpy(f, strp);
1303 * print conversion for rr records in attribute value form
1319 rp = va_arg(f->args, RR*);
1321 fmtprint(&fstr, "<null>");
1325 if(rp->type == Tptr)
1326 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1328 fmtprint(&fstr, "dom=%s", dnname(rp->owner));
1332 fmtprint(&fstr, " cpu=%s os=%s",
1333 dnname(rp->cpu), dnname(rp->os));
1336 fmtprint(&fstr, " cname=%s", dnname(rp->host));
1341 fmtprint(&fstr, " mbox=%s", dnname(rp->host));
1344 fmtprint(&fstr, " ns=%s", dnname(rp->host));
1348 fmtprint(&fstr, " mbox=%s", dnname(rp->mb));
1351 fmtprint(&fstr, " mbox=%s mbox=%s",
1352 dnname(rp->mb), dnname(rp->rmb));
1355 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, dnname(rp->host));
1359 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1362 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1367 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1368 dnname(rp->host), dnname(rp->rmb),
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, dnname(rp->host));
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 dnname(rp->rmb), dnname(rp->rp));
1407 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1409 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1410 rp->key->flags, rp->key->proto, rp->key->alg);
1415 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1418 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1419 rp->sig->type, rp->sig->alg, rp->sig->labels,
1420 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1421 rp->sig->tag, dnname(rp->sig->signer));
1424 if (rp->cert == nil)
1425 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1427 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1428 rp->cert->type, rp->cert->tag, rp->cert->alg);
1432 strp = fmtstrflush(&fstr);
1433 rv = fmtstrcpy(f, strp);
1439 warning(char *fmt, ...)
1445 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1447 syslog(1, logfile, dnserr); /* on console too */
1451 dnslog(char *fmt, ...)
1457 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1459 syslog(0, logfile, dnserr);
1463 * based on libthread's threadsetname, but drags in less library code.
1464 * actually just sets the arguments displayed.
1467 procsetname(char *fmt, ...)
1475 cmdname = vsmprint(fmt, arg);
1479 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1480 if((fd = open(buf, OWRITE)) >= 0){
1481 write(fd, cmdname, strlen(cmdname)+1);
1488 * create a slave process to handle a request to avoid one request blocking
1497 return; /* we're already a slave process */
1500 * These calls to putactivity cannot block.
1501 * After getactivity(), the current process is counted
1502 * twice in dnvars.active (one will pass to the child).
1503 * If putactivity tries to wait for dnvars.active == 0,
1504 * it will never happen.
1507 /* limit parallelism */
1508 procs = getactivity(req, 1);
1509 if (procs > stats.slavehiwat)
1510 stats.slavehiwat = procs;
1511 if(procs > Maxactive){
1513 dnslog("[%d] too much activity", getpid());
1519 * parent returns to main loop, child does the work.
1520 * don't change note group.
1523 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1528 procsetname("request slave of pid %d", ppid);
1530 dnslog("[%d] take activity from %d", getpid(), ppid);
1531 req->isslave = 1; /* why not `= getpid()'? */
1535 * this relies on rfork producing separate, initially-identical
1536 * stacks, thus giving us two copies of `req', one in each
1540 longjmp(req->mret, 1);
1545 * chasing down double free's
1548 dncheck(void *p, int dolock)
1556 assert(dp->magic == DNmagic);
1565 for(i = 0; i < HTLEN; i++)
1566 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 (dolock && rronlist(rp, rp->next)) {
1575 dnslog("%R duplicates its next chain "
1576 "(%R); aborting", rp, rp->next);
1586 rrequiv(RR *r1, RR *r2)
1588 return r1->owner == r2->owner
1589 && r1->type == r2->type
1590 && r1->arg0 == r2->arg0
1591 && r1->arg1 == r2->arg1;
1594 /* called with dnlock held */
1600 for(; rp; rp = rp->next){
1602 for(nrp = *l; nrp; nrp = *l)
1603 if(rrequiv(rp, nrp)){
1612 * true if second domain is subsumed by the first
1615 subsume(char *higher, char *lower)
1620 hn = strlen(higher);
1621 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1622 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1628 * randomize the order we return items to provide some
1629 * load balancing for servers.
1631 * only randomize the first class of entries
1636 RR *first, *last, *x, *base;
1639 if(rp == nil || rp->next == nil)
1642 /* just randomize addresses, mx's and ns's */
1643 for(x = rp; x; x = x->next)
1644 if(x->type != Ta && x->type != Taaaa &&
1645 x->type != Tmx && x->type != Tns)
1653 /* stop randomizing if we've moved past our class */
1654 if(base->auth != rp->auth || base->db != rp->db){
1679 /* reroll the dice */
1689 int i, len, ilen, rv;
1692 char obuf[64]; /* rsc optimization */
1694 if(!(f->flags&FmtPrec) || f->prec < 1)
1697 b = va_arg(f->args, uchar*);
1701 /* if it's a printable, go for it */
1703 for(i = 0; i < len; i++)
1707 if(len >= sizeof obuf)
1708 len = sizeof(obuf)-1;
1709 memmove(obuf, b, len);
1717 f->flags &= ~FmtPrec;
1720 len = (8*ilen+4)/5 + 3;
1723 len = (8*ilen+5)/6 + 4;
1732 if(len > sizeof(obuf)){
1743 rv = enc32(out, len, b, ilen);
1746 rv = enc64(out, len, b, ilen);
1749 rv = enc16(out, len, b, ilen);
1764 return fmtstrcpy(f, "<encodefmt>");
1772 x = mallocz(size, 1);
1775 setmalloctag(x, getcallerpc(&size));
1786 p = mallocz(size+1, 0);
1789 memmove(p, s, size);
1791 setmalloctag(p, getcallerpc(&s));
1796 * create a pointer record
1799 mkptr(DN *dp, char *ptr, ulong ttl)
1804 ipdp = dnlookup(ptr, Cin, 1);
1815 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1818 * look for all ip addresses in this network and make
1819 * pointer records for them.
1822 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1828 uchar ip[IPaddrlen], nnet[IPaddrlen];
1829 uchar nibip[IPaddrlen*2];
1831 RR *rp, *nrp, *first, **l;
1835 for(i = 0; i < HTLEN; i++)
1836 for(dp = ht[i]; dp; dp = dp->next)
1837 for(rp = dp->rr; rp; rp = rp->next){
1838 if(rp->type != forwtype || rp->negative)
1840 parseip(ip, rp->ip->name);
1841 maskip(ip, mask, nnet);
1842 if(ipcmp(net, nnet) != 0)
1847 if (forwtype == Taaaa) {
1848 bytes2nibbles(nibip, ip, IPaddrlen);
1854 e = ptr+sizeof(ptr);
1855 for(j = len - 1; j >= len - subdoms; j--)
1856 p = seprint(p, e, (forwtype == Ta?
1857 "%d.": "%x."), ipp[j]);
1858 seprint(p, e, "%s", dom);
1860 nrp = mkptr(dp, ptr, ttl);
1865 for(rp = first; rp != nil; rp = nrp){
1868 rrattach(rp, Authoritative);
1873 addserver(Server **l, char *name)
1881 s = malloc(sizeof(Server)+n+1);
1884 s->name = (char*)(s+1);
1885 memmove(s->name, name, n);
1892 copyserverlist(Server *s)
1896 for(ns = nil; s != nil; s = s->next)
1897 addserver(&ns, s->name);
1902 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1905 * convert an integer RR type to it's ascii name
1908 rrname(int type, char *buf, int len)
1913 if(type >= 0 && type <= Tall)
1916 snprint(buf, len, "%d", type);
1923 * free a list of resource records and any related structs
1930 for(; rp; rp = next){
1937 freeserverlist(Server *s)
1941 for(; s != nil; s = next){
1943 memset(s, 0, sizeof *s); /* cause trouble */
1949 * allocate a resource record of a given type
1956 rp = emalloc(sizeof(*rp));
1957 rp->magic = RRmagic;
1958 rp->pc = getcallerpc(&type);
1960 if (rp->type != type)
1961 dnslog("rralloc: bogus type %d", type);
1962 setmalloctag(rp, rp->pc);
1965 rp->soa = emalloc(sizeof(*rp->soa));
1966 rp->soa->slaves = nil;
1967 setmalloctag(rp->soa, rp->pc);
1970 rp->srv = emalloc(sizeof(*rp->srv));
1971 setmalloctag(rp->srv, rp->pc);
1974 rp->key = emalloc(sizeof(*rp->key));
1975 setmalloctag(rp->key, rp->pc);
1978 rp->cert = emalloc(sizeof(*rp->cert));
1979 setmalloctag(rp->cert, rp->pc);
1982 rp->sig = emalloc(sizeof(*rp->sig));
1983 setmalloctag(rp->sig, rp->pc);
1986 rp->null = emalloc(sizeof(*rp->null));
1987 setmalloctag(rp->null, rp->pc);
1997 * free a resource record and any related structs
2006 assert(rp->magic == RRmagic);
2007 assert(!rp->cached);
2011 assert(dp->magic == DNmagic);
2012 for(nrp = dp->rr; nrp; nrp = nrp->next)
2013 assert(nrp != rp); /* "rrfree of live rr" */
2018 freeserverlist(rp->soa->slaves);
2019 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
2023 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
2027 free(rp->key->data);
2028 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
2032 free(rp->cert->data);
2033 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
2037 free(rp->sig->data);
2038 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
2042 free(rp->null->data);
2043 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
2047 while(rp->txt != nil){
2051 memset(t, 0, sizeof *t); /* cause trouble */
2057 rp->magic = ~rp->magic;
2058 memset(rp, 0, sizeof *rp); /* cause trouble */