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 */
38 static char *rrtname[] =
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) >= 0)
233 return dnlookup(name, class, enter);
237 ipalookup(uchar *ip, int class, int enter)
241 snprint(addr, sizeof(addr), "%I", ip);
242 return dnlookup(addr, class, enter);
246 rrsame(RR *rr1, RR *rr2)
249 rr1 != nil && rr2 != nil &&
250 rr1->db == rr2->db &&
251 rr1->auth == rr2->auth &&
256 rronlist(RR *rp, RR *lp)
258 for(; lp; lp = lp->next)
272 fd = create(file, OWRITE, 0666);
277 fprint(fd, "# system %s\n", sysname());
278 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
279 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
280 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
281 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
282 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
283 for (i = 0; i < nelem(stats.under10ths); i++)
284 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
285 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
286 (double)(i+1)/10, stats.under10ths[i]);
287 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
288 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
289 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
290 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
291 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
292 fprint(fd, "# negative answers w bad delegation\t%lud\n",
294 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
296 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
297 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
301 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
316 fd = create(file, OWRITE, 0666);
321 for(i = 0; i < HTLEN; i++)
322 for(dp = ht[i]; dp; dp = dp->next){
323 fprint(fd, "%s\n", dp->name);
324 for(rp = dp->rr; rp; rp = rp->next) {
325 fprint(fd, "\t%R %c%c %ld/%lud\n",
326 rp, rp->auth? 'A': 'U',
327 rp->db? 'D': 'N', (long)(rp->expire - now), rp->ttl);
328 if (rronlist(rp, rp->next))
329 fprint(fd, "*** duplicate:\n");
348 for(i = 0; i < HTLEN; i++)
349 for(dp = ht[i]; dp; dp = dp->next){
352 for(; rp != nil; rp = rp->next)
361 * delete head of *l and free the old head.
362 * call with dnlock held.
369 if (canlock(&dnlock))
370 abort(); /* rrdelhead called with dnlock not held */
374 *l = rp->next; /* unlink head */
375 rp->cached = 0; /* avoid blowing an assertion in rrfree */
380 * check the age of resource records, free any that have timed out.
381 * call with dnlock held.
389 if (canlock(&dnlock))
390 abort(); /* dnage called with dnlock not held */
391 diff = now - dp->referenced;
392 if(diff < Reserved || dp->mark != 0)
396 while ((rp = *l) != nil){
397 assert(rp->magic == RRmagic && rp->cached);
398 if(!rp->db && ((long)(rp->expire - now) <= 0 || diff > dnvars.oldest))
399 rrdelhead(l); /* rp == *l before; *l == rp->next after */
405 #define MARK(dp) { if (dp) (dp)->mark |= 2; }
407 /* mark a domain name and those in its RRs as never to be aged */
415 /* mark all referenced domain names */
417 for(rp = dp->rr; rp; rp = rp->next){
420 MARK(rp->negsoaowner);
463 MARK(rp->sig->signer);
474 #define REF(dp) { if (dp) (dp)->mark |= 1; }
477 * periodicly sweep for old records and remove unreferenced domain names
479 * only called when all other threads are locked out
487 static ulong nextage;
489 if(dnvars.names < target || ((long)(nextage - now) > 0 && !doit)){
490 dnvars.oldest = maxage;
494 if(dnvars.names >= target) {
495 dnslog("more names (%lud) than target (%lud)", dnvars.names,
498 if (dnvars.oldest < Minage)
499 dnvars.oldest = Minage; /* don't be silly */
501 if (agefreq > dnvars.oldest / 2)
502 nextage = now + dnvars.oldest / 2;
504 nextage = now + (ulong)agefreq;
508 /* time out all old entries (and set refs to 0) */
509 for(i = 0; i < HTLEN; i++)
510 for(dp = ht[i]; dp; dp = dp->next){
515 /* mark all referenced domain names */
516 for(i = 0; i < HTLEN; i++)
517 for(dp = ht[i]; dp; dp = dp->next)
518 for(rp = dp->rr; rp; rp = rp->next){
521 REF(rp->negsoaowner);
564 REF(rp->sig->signer);
569 /* sweep and remove unreferenced domain names */
570 for(i = 0; i < HTLEN; i++){
572 for(dp = *l; dp; dp = *l){
573 if(dp->rr == nil && dp->mark == 0){
574 assert(dp->magic == DNmagic);
578 memset(dp, 0, sizeof *dp); /* cause trouble */
579 dp->magic = ~DNmagic;
593 * timeout all database records (used when rereading db)
604 /* time out all database entries */
605 for(i = 0; i < HTLEN; i++)
606 for(dp = ht[i]; dp; dp = dp->next) {
608 for(rp = dp->rr; rp; rp = rp->next)
617 * mark all local db records about my area as authoritative,
618 * delete timed out ones
631 /* time out all database entries */
632 for(i = 0; i < HTLEN; i++)
633 for(dp = ht[i]; dp; dp = dp->next){
634 area = inmyarea(dp->name);
636 for(rp = *l; rp; rp = *l){
643 minttl = area->soarr->soa->minttl;
647 } else if(rp->type == Tns && inmyarea(rp->host->name))
658 * keep track of other processes to know if we can
659 * garbage collect. block while garbage collecting.
662 getactivity(Request *req, int recursive)
667 dnslog("get: %d active by pid %d from %p",
668 dnvars.active, getpid(), getcallerpc(&req));
671 * can't block here if we're already holding one
672 * of the dnvars.active (recursive). will deadlock.
674 while(!recursive && dnvars.mutex){
676 sleep(100); /* tune; was 200 */
679 rv = ++dnvars.active;
682 req->id = ++dnvars.id;
689 putactivity(int recursive)
692 dnslog("put: %d active by pid %d",
693 dnvars.active, getpid());
696 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
699 * clean out old entries and check for new db periodicly
700 * can't block here if being called to let go a "recursive" lock
701 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
703 if (recursive || dnvars.mutex ||
704 (needrefresh == 0 && dnvars.active > 0)){
709 /* wait till we're alone */
711 while(dnvars.active > 0){
713 sleep(100); /* tune; was 100 */
718 db2cache(needrefresh);
722 /* let others back in */
733 for(; rp; rp = rp->next)
739 * Attach a single resource record to a domain name (new->owner).
740 * - Avoid duplicates with already present RR's
741 * - Chain all RR's of the same type adjacent to one another
742 * - chain authoritative RR's ahead of non-authoritative ones
743 * - remove any expired RR's
744 * If new is a stale duplicate, rrfree it.
745 * Must be called with dnlock held.
748 rrattach1(RR *new, int auth)
755 assert(new->magic == RRmagic && !new->cached);
758 assert(dp != nil && dp->magic == DNmagic);
763 * try not to let responses expire before we
764 * can use them to complete this query, by extending
765 * past (or nearly past) expiration time.
773 new->expire = now + ttl;
776 * find first rr of the right type
779 for(rp = *l; rp; rp = *l){
780 assert(rp->magic == RRmagic && rp->cached);
781 if(rp->type == new->type)
787 * negative entries replace positive entries
788 * positive entries replace negative entries
789 * newer entries replace older entries with the same fields
791 * look farther ahead than just the next entry when looking
792 * for duplicates; RRs of a given type can have different rdata
793 * fields (e.g. multiple NS servers).
795 while ((rp = *l) != nil){
796 assert(rp->magic == RRmagic && rp->cached);
797 if(rp->type != new->type)
800 if(rp->db == new->db && rp->auth == new->auth){
801 /* negative drives out positive and vice versa */
802 if(rp->negative != new->negative) {
803 /* rp == *l before; *l == rp->next after */
807 /* all things equal, pick the newer one */
808 else if(rrequiv(rp, new)){
809 /* old drives out new */
810 if((long)(rp->expire - new->expire) > 0) {
814 /* rp == *l before; *l == rp->next after */
819 * Hack for pointer records. This makes sure
820 * the ordering in the list reflects the ordering
821 * received or read from the database
823 else if(rp->type == Tptr &&
824 !rp->negative && !new->negative &&
825 rp->ptr->ordinal > new->ptr->ordinal)
831 if (rronlist(new, rp)) {
832 /* should not happen; duplicates were processed above */
833 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
845 * Attach a list of resource records to a domain name.
846 * May rrfree any stale duplicate RRs; dismembers the list.
847 * Upon return, every RR in the list will have been rrfree-d
848 * or attached to its domain name.
849 * See rrattach1 for properties preserved.
852 rrattach(RR *rp, int auth)
858 for(; rp; rp = next){
862 /* avoid any outside spoofing */
863 if(cfg.cachedb && !rp->db && inmyarea(dp->name))
872 rrcopy(RR *rp, RR **last)
884 assert(rp->magic == RRmagic);
885 nrp = rralloc(rp->type);
892 soa->slaves = copyserverlist(rp->soa->slaves);
906 key->data = emalloc(key->dlen);
907 memmove(key->data, rp->key->data, rp->key->dlen);
914 caa->data = emalloc(caa->dlen);
915 memmove(caa->data, rp->caa->data, rp->caa->dlen);
922 cert->data = emalloc(cert->dlen);
923 memmove(cert->data, rp->cert->data, rp->cert->dlen);
930 sig->data = emalloc(sig->dlen);
931 memmove(sig->data, rp->sig->data, rp->sig->dlen);
938 null->data = emalloc(null->dlen);
939 memmove(null->data, rp->null->data, rp->null->dlen);
945 for(t = rp->txt; t != nil; t = t->next){
946 nt = emalloc(sizeof(*nt));
947 nt->p = estrdup(t->p);
957 nrp->pc = getcallerpc(&rp);
958 setmalloctag(nrp, nrp->pc);
966 * lookup a resource record of a particular type and
967 * class attached to a domain name. Return copies.
969 * Priority ordering is:
971 * not timed out network authoritative
972 * not timed out network unauthoritative
975 * if flag NOneg is set, don't return negative cached entries.
976 * return nothing instead.
979 rrlookup(DN *dp, int type, int flag)
981 RR *rp, *first, **last;
983 assert(dp->magic == DNmagic);
989 /* try for an authoritative db entry */
990 for(rp = dp->rr; rp; rp = rp->next){
991 assert(rp->magic == RRmagic && rp->cached);
994 if(tsame(type, rp->type))
995 last = rrcopy(rp, last);
1000 /* try for a living authoritative network entry */
1001 for(rp = dp->rr; rp; rp = rp->next){
1004 if((long)(rp->expire - now) > 0)
1005 if(tsame(type, rp->type)){
1006 if(flag == NOneg && rp->negative)
1008 last = rrcopy(rp, last);
1014 /* try for a living unauthoritative network entry */
1015 for(rp = dp->rr; rp; rp = rp->next){
1017 if((long)(rp->expire - now) > 0)
1018 if(tsame(type, rp->type)){
1019 if(flag == NOneg && rp->negative)
1021 last = rrcopy(rp, last);
1027 /* try for an unauthoritative db entry */
1028 for(rp = dp->rr; rp; rp = rp->next){
1030 if(tsame(type, rp->type))
1031 last = rrcopy(rp, last);
1036 /* otherwise, settle for anything we got (except for negative caches) */
1037 for(rp = dp->rr; rp; rp = rp->next)
1038 if(tsame(type, rp->type)){
1041 last = rrcopy(rp, last);
1051 * convert an ascii RR type name to its integer representation
1058 for(i = 0; i < nelem(rrtname); i++)
1059 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1062 /* make any a synonym for all */
1063 if(strcmp(atype, "any") == 0)
1065 else if(isascii(atype[0]) && isdigit(atype[0]))
1072 * return 0 if not a supported rr type
1075 rrsupported(int type)
1077 if(type < 0 || type >= nelem(rrtname))
1079 return rrtname[type] != nil;
1086 tsame(int t1, int t2)
1088 return t1 == t2 || t1 == Tall;
1092 * Add resource records to a list.
1095 rrcat(RR **start, RR *rp)
1100 /* check for duplicates */
1101 for (olp = *start; 0 && olp; olp = olp->next)
1102 for (nlp = rp; nlp; nlp = nlp->next)
1103 if (rrsame(nlp, olp))
1104 dnslog("rrcat: duplicate RR: %R", nlp);
1109 last = &(*last)->next;
1116 rrremfilter(RR **l, int (*filter)(RR*, void*), void *arg)
1125 if((*filter)(rp, arg)){
1138 filterneg(RR *rp, void*)
1140 return rp->negative;
1143 filtertype(RR *rp, void *arg)
1145 return rp->type == *((int*)arg);
1148 filterowner(RR *rp, void *arg)
1150 return rp->owner == (DN*)arg;
1154 * remove negative cache rr's from an rr list
1159 return rrremfilter(l, filterneg, nil);
1163 * remove rr's of a particular type from an rr list
1166 rrremtype(RR **l, int type)
1168 return rrremfilter(l, filtertype, &type);
1172 * remove rr's of a particular owner from an rr list
1175 rrremowner(RR **l, DN *owner)
1177 return rrremfilter(l, filterowner, owner);
1183 return dn? dn->name: "<null>";
1187 idnname(DN *dn, char *buf, int nbuf)
1192 if(idn2utf(name, buf, nbuf) >= 0)
1198 * print conversion for rr records
1215 rp = va_arg(f->args, RR*);
1217 fmtprint(&fstr, "<null>");
1221 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1222 rrname(rp->type, buf, sizeof buf));
1225 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1231 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1238 fmtprint(&fstr, "\t%s", dnname(rp->host));
1242 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1245 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1248 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1252 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1255 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1259 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1260 dnname(rp->host), dnname(rp->rmb),
1261 (soa? soa->serial: 0),
1262 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1263 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1265 for(s = soa->slaves; s != nil; s = s->next)
1266 fmtprint(&fstr, " %s", s->name);
1270 fmtprint(&fstr, "\t%ud %ud %ud %s",
1271 (srv? srv->pri: 0), (srv? srv->weight: 0),
1272 rp->port, dnname(rp->host));
1275 if (rp->null == nil)
1276 fmtprint(&fstr, "\t<null>");
1278 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1282 fmtprint(&fstr, "\t");
1283 for(t = rp->txt; t != nil; t = t->next)
1284 fmtprint(&fstr, "%s", t->p);
1287 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1292 fmtprint(&fstr, "\t<null> <null> <null>");
1294 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1295 rp->key->proto, rp->key->alg);
1300 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1302 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1303 rp->sig->type, rp->sig->alg, rp->sig->labels,
1304 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1305 rp->sig->tag, dnname(rp->sig->signer));
1308 if (rp->cert == nil)
1309 fmtprint(&fstr, "\t<null> <null> <null>");
1311 fmtprint(&fstr, "\t%d %d %d",
1312 rp->cert->type, rp->cert->tag, rp->cert->alg);
1316 fmtprint(&fstr, "\t<null> <null> <null>");
1318 fmtprint(&fstr, "\t%d %s %.*s",
1319 rp->caa->flags, dnname(rp->caa->tag),
1320 rp->caa->dlen, (char*)rp->caa->data);
1324 strp = fmtstrflush(&fstr);
1325 rv = fmtstrcpy(f, strp);
1331 * print conversion for rr records in attribute value form
1337 char buf[Domlen], *strp;
1347 rp = va_arg(f->args, RR*);
1349 fmtprint(&fstr, "<null>");
1353 if(rp->type == Tptr)
1354 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1356 fmtprint(&fstr, "dom=%s", idnname(rp->owner, buf, sizeof(buf)));
1360 fmtprint(&fstr, " cpu=%s os=%s",
1361 idnname(rp->cpu, buf, sizeof(buf)),
1362 idnname(rp->os, buf, sizeof(buf)));
1365 fmtprint(&fstr, " cname=%s", idnname(rp->host, buf, sizeof(buf)));
1370 fmtprint(&fstr, " mbox=%s", idnname(rp->host, buf, sizeof(buf)));
1373 fmtprint(&fstr, " ns=%s", idnname(rp->host, buf, sizeof(buf)));
1377 fmtprint(&fstr, " mbox=%s", idnname(rp->mb, buf, sizeof(buf)));
1380 fmtprint(&fstr, " mbox=%s mbox=%s",
1381 idnname(rp->mb, buf, sizeof(buf)),
1382 idnname(rp->rmb, buf, sizeof(buf)));
1385 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref,
1386 idnname(rp->host, buf, sizeof(buf)));
1390 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1393 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1398 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1399 idnname(rp->host, buf, sizeof(buf)),
1400 idnname(rp->rmb, buf, sizeof(buf)),
1401 (soa? soa->serial: 0),
1402 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1403 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1404 for(s = soa->slaves; s != nil; s = s->next)
1405 fmtprint(&fstr, " dnsslave=%s", s->name);
1409 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1410 (srv? srv->pri: 0), (srv? srv->weight: 0),
1411 rp->port, idnname(rp->host, buf, sizeof(buf)));
1414 if (rp->null == nil)
1415 fmtprint(&fstr, " null=<null>");
1417 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1421 fmtprint(&fstr, " txt=");
1423 for(t = rp->txt; t != nil; t = t->next)
1424 if(strchr(t->p, ' '))
1427 fmtprint(&fstr, "\"");
1428 for(t = rp->txt; t != nil; t = t->next)
1429 fmtprint(&fstr, "%s", t->p);
1431 fmtprint(&fstr, "\"");
1434 fmtprint(&fstr, " rp=%s txt=%s",
1435 idnname(rp->rmb, buf, sizeof(buf)),
1436 idnname(rp->rp, buf, sizeof(buf)));
1441 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1443 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1444 rp->key->flags, rp->key->proto, rp->key->alg);
1449 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1452 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1453 rp->sig->type, rp->sig->alg, rp->sig->labels,
1454 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1455 rp->sig->tag, idnname(rp->sig->signer, buf, sizeof(buf)));
1458 if (rp->cert == nil)
1459 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1461 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1462 rp->cert->type, rp->cert->tag, rp->cert->alg);
1466 fmtprint(&fstr, " flags=<null> tag=<null> value=<null>");
1468 fmtprint(&fstr, " flags=%d tag=%s value=%.*s",
1469 rp->caa->flags, dnname(rp->caa->tag),
1470 rp->caa->dlen, (char*)rp->caa->data);
1474 strp = fmtstrflush(&fstr);
1475 rv = fmtstrcpy(f, strp);
1481 warning(char *fmt, ...)
1487 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1489 syslog(1, logfile, dnserr); /* on console too */
1493 dnslog(char *fmt, ...)
1499 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1501 syslog(0, logfile, dnserr);
1505 * create a slave process to handle a request to avoid one request blocking
1514 return; /* we're already a slave process */
1517 * These calls to putactivity cannot block.
1518 * After getactivity(), the current process is counted
1519 * twice in dnvars.active (one will pass to the child).
1520 * If putactivity tries to wait for dnvars.active == 0,
1521 * it will never happen.
1524 /* limit parallelism */
1525 procs = getactivity(req, 1);
1526 if(procs > stats.slavehiwat)
1527 stats.slavehiwat = procs;
1528 if(procs > Maxactive){
1530 dnslog("[%d] too much activity", getpid());
1536 * parent returns to main loop, child does the work.
1537 * don't change note group.
1540 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1545 procsetname("request slave of pid %d", ppid);
1547 dnslog("[%d] take activity from %d", getpid(), ppid);
1548 req->isslave = 1; /* why not `= getpid()'? */
1552 * this relies on rfork producing separate, initially-identical
1553 * stacks, thus giving us two copies of `req', one in each
1557 longjmp(req->mret, 1);
1562 blockequiv(Block *a, Block *b)
1564 return a->dlen == b->dlen &&
1565 memcmp(a->data, b->data, a->dlen) == 0;
1569 keyequiv(Key *a, Key *b)
1571 return a->flags == b->flags &&
1572 a->proto == b->proto &&
1578 certequiv(Cert *a, Cert *b)
1580 return a->type == a->type &&
1587 txtequiv(Txt *a, Txt *b)
1589 char *ap, *ae, *bp, *be;
1592 for(ap = ae = bp = be = nil;;ap += n, bp += n){
1593 while(a != nil && (ap == nil || (ap == ae && (a = a->next) != nil)))
1594 ap = a->p, ae = ap + strlen(ap);
1595 while(b != nil && (bp == nil || (bp == be && (b = b->next) != nil)))
1596 bp = b->p, be = bp + strlen(bp);
1597 if(a == b || a == nil || b == nil)
1602 if(memcmp(ap, bp, n) != 0)
1609 rrequiv(RR *r1, RR *r2)
1611 if(r1->owner != r2->owner
1612 || r1->type != r2->type
1613 || r1->arg0 != r2->arg0
1614 || r1->arg1 != r2->arg1)
1618 return keyequiv(r1->key, r2->key);
1620 return certequiv(r1->cert, r2->cert);
1622 return r1->sig->signer == r2->sig->signer && certequiv(r1->sig, r2->sig);
1624 return blockequiv(r1->null, r2->null);
1626 return txtequiv(r1->txt, r2->txt);
1628 return r1->caa->flags == r2->caa->flags && r1->caa->tag == r2->caa->tag && blockequiv(r1->caa, r2->caa);
1638 for(; rp; rp = rp->next){
1640 for(nrp = *l; nrp; nrp = *l)
1641 if(rrequiv(rp, nrp)){
1650 * true if second domain is subsumed by the first
1653 subsume(char *higher, char *lower)
1658 hn = strlen(higher);
1659 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1660 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1666 * randomize the order we return items to provide some
1667 * load balancing for servers.
1669 * only randomize the first class of entries
1674 RR *first, *last, *x, *base;
1677 if(rp == nil || rp->next == nil)
1680 /* just randomize addresses, mx's and ns's */
1681 for(x = rp; x; x = x->next)
1682 if(x->type != Ta && x->type != Taaaa &&
1683 x->type != Tmx && x->type != Tns)
1691 /* stop randomizing if we've moved past our class */
1692 if(base->auth != rp->auth || base->db != rp->db){
1717 /* reroll the dice */
1727 int i, len, ilen, rv;
1730 char obuf[64]; /* rsc optimization */
1732 if(!(f->flags&FmtPrec) || f->prec < 1)
1735 b = va_arg(f->args, uchar*);
1739 /* if it's a printable, go for it */
1741 for(i = 0; i < len; i++)
1745 if(len >= sizeof obuf)
1746 len = sizeof(obuf)-1;
1747 memmove(obuf, b, len);
1755 f->flags &= ~FmtPrec;
1757 if(len > sizeof(obuf)){
1766 rv = enc16(out, len, b, ilen);
1776 return fmtstrcpy(f, "<encodefmt>");
1784 x = mallocz(size, 1);
1787 setmalloctag(x, getcallerpc(&size));
1798 p = mallocz(size+1, 0);
1801 memmove(p, s, size);
1803 setmalloctag(p, getcallerpc(&s));
1808 * create a pointer record
1811 mkptr(DN *dp, char *ptr, ulong ttl)
1816 ipdp = dnlookup(ptr, Cin, 1);
1827 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1830 * look for all ip addresses in this network and make
1831 * pointer records for them.
1834 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1840 uchar ip[IPaddrlen], nnet[IPaddrlen];
1841 uchar nibip[IPaddrlen*2];
1843 RR *rp, *nrp, *first, **l;
1847 for(i = 0; i < HTLEN; i++)
1848 for(dp = ht[i]; dp; dp = dp->next)
1849 for(rp = dp->rr; rp; rp = rp->next){
1850 if(rp->type != forwtype || rp->negative)
1852 parseip(ip, rp->ip->name);
1853 maskip(ip, mask, nnet);
1854 if(ipcmp(net, nnet) != 0)
1859 if (forwtype == Taaaa) {
1860 bytes2nibbles(nibip, ip, IPaddrlen);
1866 e = ptr+sizeof(ptr);
1867 for(j = len - 1; j >= len - subdoms; j--)
1868 p = seprint(p, e, (forwtype == Ta?
1869 "%d.": "%x."), ipp[j]);
1870 seprint(p, e, "%s", dom);
1872 nrp = mkptr(dp, ptr, ttl);
1877 for(rp = first; rp != nil; rp = nrp){
1881 rrattach(rp, Authoritative);
1887 addserver(Server **l, char *name)
1895 s = malloc(sizeof(Server)+n+1);
1898 s->name = (char*)(s+1);
1899 memmove(s->name, name, n);
1906 copyserverlist(Server *s)
1910 for(ns = nil; s != nil; s = s->next)
1911 addserver(&ns, s->name);
1916 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1919 * convert an integer RR type to it's ascii name
1922 rrname(int type, char *buf, int len)
1927 if(type >= 0 && type < nelem(rrtname))
1930 snprint(buf, len, "%d", type);
1937 * free a list of resource records and any related structs
1944 for(; rp; rp = next){
1951 freeserverlist(Server *s)
1955 for(; s != nil; s = next){
1957 memset(s, 0, sizeof *s); /* cause trouble */
1963 * allocate a resource record of a given type
1970 rp = emalloc(sizeof(*rp));
1971 rp->magic = RRmagic;
1972 rp->pc = getcallerpc(&type);
1974 if (rp->type != type)
1975 dnslog("rralloc: bogus type %d", type);
1976 setmalloctag(rp, rp->pc);
1979 rp->soa = emalloc(sizeof(*rp->soa));
1980 rp->soa->slaves = nil;
1981 setmalloctag(rp->soa, rp->pc);
1984 rp->srv = emalloc(sizeof(*rp->srv));
1985 setmalloctag(rp->srv, rp->pc);
1989 rp->key = emalloc(sizeof(*rp->key));
1990 setmalloctag(rp->key, rp->pc);
1993 rp->caa = emalloc(sizeof(*rp->caa));
1994 setmalloctag(rp->caa, rp->pc);
1997 rp->cert = emalloc(sizeof(*rp->cert));
1998 setmalloctag(rp->cert, rp->pc);
2001 rp->sig = emalloc(sizeof(*rp->sig));
2002 setmalloctag(rp->sig, rp->pc);
2005 rp->null = emalloc(sizeof(*rp->null));
2006 setmalloctag(rp->null, rp->pc);
2016 * free a resource record and any related structs
2023 assert(rp->magic == RRmagic && !rp->cached);
2027 freeserverlist(rp->soa->slaves);
2028 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
2032 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
2037 free(rp->key->data);
2038 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
2042 free(rp->cert->data);
2043 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
2047 free(rp->sig->data);
2048 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
2052 free(rp->null->data);
2053 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
2057 free(rp->caa->data);
2058 memset(rp->caa, 0, sizeof *rp->caa); /* cause trouble */
2064 memset(t, 0, sizeof *t); /* cause trouble */
2070 memset(rp, 0, sizeof *rp); /* cause trouble */
2071 rp->magic = ~RRmagic;