11 * Hash table for domain names. The hash is based only on the
12 * first element of the domain name.
20 ulong names; /* names allocated */
21 ulong oldest; /* longest we'll leave a name around */
27 /* names of RR types */
28 char *rrtname[Tall+2] =
71 /* 40 */ nil, nil, nil, nil, nil, nil, nil, nil,
72 /* 48 */ nil, nil, nil, nil, nil, nil, nil, nil,
73 /* 56 */ nil, nil, nil, nil, nil, nil, nil, nil,
74 /* 64 */ nil, nil, nil, nil, nil, nil, nil, nil,
75 /* 72 */ nil, nil, nil, nil, nil, nil, nil, nil,
76 /* 80 */ nil, nil, nil, nil, nil, nil, nil, nil,
77 /* 88 */ nil, nil, nil, nil, nil, nil, nil, nil,
78 /* 96 */ nil, nil, nil, nil, nil, nil, nil, nil,
79 /* 104 */ nil, nil, nil, nil, nil, nil, nil, nil,
80 /* 112 */ nil, nil, nil, nil, nil, nil, nil, nil,
81 /* 120 */ nil, nil, nil, nil, nil, nil, nil, nil,
82 /* 128 */ nil, nil, nil, nil, nil, nil, nil, nil,
83 /* 136 */ nil, nil, nil, nil, nil, nil, nil, nil,
84 /* 144 */ nil, nil, nil, nil, nil, nil, nil, nil,
85 /* 152 */ nil, nil, nil, nil, nil, nil, nil, nil,
86 /* 160 */ nil, nil, nil, nil, nil, nil, nil, nil,
87 /* 168 */ nil, nil, nil, nil, nil, nil, nil, nil,
88 /* 176 */ nil, nil, nil, nil, nil, nil, nil, nil,
89 /* 184 */ nil, nil, nil, nil, nil, nil, nil, nil,
90 /* 192 */ nil, nil, nil, nil, nil, nil, nil, nil,
91 /* 200 */ nil, nil, nil, nil, nil, nil, nil, nil,
92 /* 208 */ nil, nil, nil, nil, nil, nil, nil, nil,
93 /* 216 */ nil, nil, nil, nil, nil, nil, nil, nil,
94 /* 224 */ nil, nil, nil, nil, nil, nil, nil, nil,
95 /* 232 */ nil, nil, nil, nil, nil, nil, nil, nil,
96 /* 240 */ nil, nil, nil, nil, nil, nil, nil, nil,
97 /* 248 */ nil, nil, nil,
107 /* names of response codes */
108 char *rname[Rmask+1] =
120 static int sencodefmt(Fmt*);
123 * set up a pipe to use as a lock
128 fmtinstall('E', eipfmt);
129 fmtinstall('I', eipfmt);
130 fmtinstall('V', eipfmt);
131 fmtinstall('R', rrfmt);
132 fmtinstall('Q', rravfmt);
133 fmtinstall('H', sencodefmt);
135 dnvars.oldest = maxage;
140 * hash for a domain name
146 uchar *val = (uchar*)name;
148 for(hash = 0; *val; val++)
149 hash = (hash*13) + tolower(*val)-'a';
154 * lookup a symbol. if enter is not zero and the name is
155 * not found, create it.
158 dnlookup(char *name, int class, int enter)
163 l = &ht[dnhash(name)];
165 for(dp = *l; dp; dp = dp->next) {
166 assert(dp->magic == DNmagic);
167 if(dp->class == class && cistrcmp(dp->name, name) == 0){
168 dp->referenced = now;
179 dp = emalloc(sizeof(*dp));
181 dp->name = estrdup(name);
182 assert(dp->name != 0);
186 dp->referenced = now;
203 fd = open(file, OWRITE|OTRUNC);
207 for(i = 0; i < HTLEN; i++){
208 for(dp = ht[i]; dp; dp = dp->next){
209 fprint(fd, "%s\n", dp->name);
210 for(rp = dp->rr; rp; rp = rp->next)
211 fprint(fd, " %R %c%c %lud/%lud\n", rp, rp->auth?'A':'U',
212 rp->db?'D':'N', rp->expire, rp->ttl);
231 for(i = 0; i < HTLEN; i++)
232 for(dp = ht[i]; dp; dp = dp->next){
235 for(; rp != nil; rp = rp->next)
244 * check the age of resource records, free any that have timed out
253 diff = now - dp->referenced;
258 for(rp = dp->rr; rp; rp = next){
259 assert(rp->magic == RRmagic && rp->cached);
262 if(rp->expire < now || diff > dnvars.oldest){
272 #define REF(x) if(x) x->refs++
275 * our target is 4000 names cached, this should be larger on large servers
280 * periodicly sweep for old records and remove unreferenced domain names
282 * only called when all other threads are locked out
290 static ulong nextage;
292 if(dnvars.names < TARGET && now < nextage && !doit){
293 dnvars.oldest = maxage;
297 if(dnvars.names > TARGET)
299 nextage = now + maxage;
303 /* time out all old entries (and set refs to 0) */
304 for(i = 0; i < HTLEN; i++)
305 for(dp = ht[i]; dp; dp = dp->next){
310 /* mark all referenced domain names */
311 for(i = 0; i < HTLEN; i++)
312 for(dp = ht[i]; dp; dp = dp->next)
313 for(rp = dp->rr; rp; rp = rp->next){
316 REF(rp->negsoaowner);
362 /* sweep and remove unreferenced domain names */
363 for(i = 0; i < HTLEN; i++){
365 for(dp = *l; dp; dp = *l){
366 if(dp->rr == 0 && dp->refs == 0){
367 assert(dp->magic == DNmagic);
371 dp->magic = ~dp->magic;
384 * timeout all database records (used when rereading db)
395 /* time out all database entries */
396 for(i = 0; i < HTLEN; i++)
397 for(dp = ht[i]; dp; dp = dp->next)
398 for(rp = dp->rr; rp; rp = rp->next)
406 * mark all local db records about my area as authoritative, time out any others
418 /* time out all database entries */
419 for(i = 0; i < HTLEN; i++)
420 for(dp = ht[i]; dp; dp = dp->next){
421 area = inmyarea(dp->name);
422 for(rp = dp->rr; rp; rp = rp->next)
425 if(rp->ttl < area->soarr->soa->minttl)
426 rp->ttl = area->soarr->soa->minttl;
431 dp->referenced = now - Reserved - 1;
440 * keep track of other processes to know if we can
441 * garbage collect. block while garbage collecting.
444 getactivity(Request *req)
448 if(traceactivity) syslog(0, "dns", "get %d by %d.%d", dnvars.active, getpid(), threadid());
455 rv = ++dnvars.active;
457 req->id = ++dnvars.id;
465 if(traceactivity) syslog(0, "dns", "put %d by %d.%d", dnvars.active, getpid(), threadid());
468 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */;
471 * clean out old entries and check for new db periodicly
473 if(dnvars.mutex || (needrefresh == 0 && dnvars.active > 0)){
478 /* wait till we're alone */
480 while(dnvars.active > 0){
487 db2cache(needrefresh);
490 /* let others back in */
496 * Attach a single resource record to a domain name.
497 * - Avoid duplicates with already present RR's
498 * - Chain all RR's of the same type adjacent to one another
499 * - chain authoritative RR's ahead of non-authoritative ones
502 rrattach1(RR *new, int auth)
508 assert(new->magic == RRmagic && !new->cached);
511 new->expire = new->ttl;
513 new->expire = now + Year;
515 assert(dp->magic == DNmagic);
520 * find first rr of the right type
523 for(rp = *l; rp; rp = *l){
524 assert(rp->magic == RRmagic && rp->cached);
525 if(rp->type == new->type)
531 * negative entries replace positive entries
532 * positive entries replace negative entries
533 * newer entries replace older entries with the same fields
535 for(rp = *l; rp; rp = *l){
536 assert(rp->magic == RRmagic && rp->cached);
537 if(rp->type != new->type)
540 if(rp->db == new->db && rp->auth == new->auth){
541 /* negative drives out positive and vice versa */
542 if(rp->negative != new->negative){
549 /* all things equal, pick the newer one */
550 if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
551 /* new drives out old */
552 if(new->ttl > rp->ttl || new->expire > rp->expire){
563 /* Hack for pointer records. This makes sure
564 * the ordering in the list reflects the ordering
565 * received or read from the database
567 if(rp->type == Tptr){
568 if(!rp->negative && !new->negative
569 && rp->ptr->ordinal > new->ptr->ordinal)
585 * Attach a list of resource records to a domain name.
586 * - Avoid duplicates with already present RR's
587 * - Chain all RR's of the same type adjacent to one another
588 * - chain authoritative RR's ahead of non-authoritative ones
589 * - remove any expired RR's
592 rrattach(RR *rp, int auth)
597 for(; rp; rp = next){
601 /* avoid any outside spoofing */
602 if(cachedb && !rp->db && inmyarea(rp->owner->name))
611 * allocate a resource record of a given type
618 rp = emalloc(sizeof(*rp));
620 rp->pc = getcallerpc(&type);
624 rp->soa = emalloc(sizeof(*rp->soa));
625 rp->soa->slaves = nil;
628 rp->key = emalloc(sizeof(*rp->key));
631 rp->cert = emalloc(sizeof(*rp->cert));
634 rp->sig = emalloc(sizeof(*rp->sig));
637 rp->null = emalloc(sizeof(*rp->null));
647 * free a resource record and any related structs
656 assert(rp->magic = RRmagic);
661 assert(dp->magic == DNmagic);
662 for(nrp = dp->rr; nrp; nrp = nrp->next)
663 assert(nrp != rp); /* "rrfree of live rr" */;
668 freeserverlist(rp->soa->slaves);
676 free(rp->cert->data);
684 free(rp->null->data);
688 while(rp->txt != nil){
697 rp->magic = ~rp->magic;
702 * free a list of resource records and any related structs
709 for(; rp; rp = next){
716 rrcopy(RR *rp, RR **last)
726 nrp = rralloc(rp->type);
732 for(t = rp->txt; t != nil; t = t->next){
733 nt = emalloc(sizeof(*nt));
734 nt->p = estrdup(t->p);
744 *nrp->soa = *rp->soa;
745 nrp->soa->slaves = copyserverlist(rp->soa->slaves);
752 key->data = emalloc(key->dlen);
753 memmove(key->data, rp->key->data, rp->key->dlen);
760 sig->data = emalloc(sig->dlen);
761 memmove(sig->data, rp->sig->data, rp->sig->dlen);
768 cert->data = emalloc(cert->dlen);
769 memmove(cert->data, rp->cert->data, rp->cert->dlen);
776 null->data = emalloc(null->dlen);
777 memmove(null->data, rp->null->data, rp->null->dlen);
790 * lookup a resource record of a particular type and
791 * class attached to a domain name. Return copies.
793 * Priority ordering is:
795 * not timed out network authoritative
796 * not timed out network unauthoritative
799 * if flag NOneg is set, don't return negative cached entries.
800 * return nothing instead.
803 rrlookup(DN *dp, int type, int flag)
805 RR *rp, *first, **last;
807 assert(dp->magic == DNmagic);
813 /* try for an authoritative db entry */
814 for(rp = dp->rr; rp; rp = rp->next){
815 assert(rp->magic == RRmagic && rp->cached);
818 if(tsame(type, rp->type))
819 last = rrcopy(rp, last);
824 /* try for an living authoritative network entry */
825 for(rp = dp->rr; rp; rp = rp->next){
828 if(rp->ttl + 60 > now)
829 if(tsame(type, rp->type)){
830 if(flag == NOneg && rp->negative)
832 last = rrcopy(rp, last);
838 /* try for an living unauthoritative network entry */
839 for(rp = dp->rr; rp; rp = rp->next){
841 if(rp->ttl + 60 > now)
842 if(tsame(type, rp->type)){
843 if(flag == NOneg && rp->negative)
845 last = rrcopy(rp, last);
851 /* try for an unauthoritative db entry */
852 for(rp = dp->rr; rp; rp = rp->next){
854 if(tsame(type, rp->type))
855 last = rrcopy(rp, last);
860 /* otherwise, settle for anything we got (except for negative caches) */
861 for(rp = dp->rr; rp; rp = rp->next){
862 if(tsame(type, rp->type)){
865 last = rrcopy(rp, last);
876 * convert an ascii RR type name to its integer representation
883 for(i = 0; i <= Tall; i++)
884 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
887 /* make any a synonym for all */
888 if(strcmp(atype, "any") == 0)
894 * convert an integer RR type to it's ascii name
897 rrname(int type, char *buf, int len)
905 snprint(buf, len, "%d", type);
912 * return 0 if not a supported rr type
915 rrsupported(int type)
917 if(type < 0 || type >Tall)
919 return rrtname[type] != 0;
926 tsame(int t1, int t2)
928 return t1 == t2 || t1 == Tall;
932 * Add resource records to a list, duplicate them if they are cached
933 * RR's since these are shared.
936 rrcat(RR **start, RR *rp)
942 last = &(*last)->next;
949 * remove negative cache rr's from an rr list
974 * remove rr's of a particular type from an rr list
977 rrremtype(RR **l, int type)
986 if(rp->type == type){
999 * print conversion for rr records
1014 rp = va_arg(f->args, RR*);
1016 fmtprint(&fstr, "<null>");
1020 fmtprint(&fstr, "%s %s", rp->owner->name,
1021 rrname(rp->type, buf, sizeof buf));
1024 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1030 fmtprint(&fstr, "\t%s %s", rp->cpu->name, rp->os->name);
1037 fmtprint(&fstr, "\t%s", rp->host->name);
1041 fmtprint(&fstr, "\t%s", rp->mb->name);
1044 fmtprint(&fstr, "\t%s %s", rp->mb->name, rp->rmb->name);
1047 fmtprint(&fstr, "\t%lud %s", rp->pref, rp->host->name);
1051 fmtprint(&fstr, "\t%s", rp->ip->name);
1054 /* fmtprint(&fstr, "\t%s(%lud)", rp->ptr->name, rp->ptr->ordinal); */
1055 fmtprint(&fstr, "\t%s", rp->ptr->name);
1058 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud", rp->host->name,
1059 rp->rmb->name, rp->soa->serial, rp->soa->refresh, rp->soa->retry,
1060 rp->soa->expire, rp->soa->minttl);
1061 for(s = rp->soa->slaves; s != nil; s = s->next)
1062 fmtprint(&fstr, " %s", s->name);
1065 fmtprint(&fstr, "\t%.*H", rp->null->dlen, rp->null->data);
1068 fmtprint(&fstr, "\t");
1069 for(t = rp->txt; t != nil; t = t->next)
1070 fmtprint(&fstr, "%s", t->p);
1073 fmtprint(&fstr, "\t%s %s", rp->rmb->name, rp->rp->name);
1076 fmtprint(&fstr, "\t%d %d %d", rp->key->flags, rp->key->proto,
1080 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1081 rp->sig->type, rp->sig->alg, rp->sig->labels, rp->sig->ttl,
1082 rp->sig->exp, rp->sig->incep, rp->sig->tag, rp->sig->signer->name);
1085 fmtprint(&fstr, "\t%d %d %d",
1086 rp->sig->type, rp->sig->tag, rp->sig->alg);
1092 strp = fmtstrflush(&fstr);
1093 rv = fmtstrcpy(f, strp);
1099 * print conversion for rr records in attribute value form
1114 rp = va_arg(f->args, RR*);
1116 fmtprint(&fstr, "<null>");
1120 if(rp->type == Tptr)
1121 fmtprint(&fstr, "ptr=%s", rp->owner->name);
1123 fmtprint(&fstr, "dom=%s", rp->owner->name);
1127 fmtprint(&fstr, " cpu=%s os=%s", rp->cpu->name, rp->os->name);
1130 fmtprint(&fstr, " cname=%s", rp->host->name);
1135 fmtprint(&fstr, " mbox=%s", rp->host->name);
1138 fmtprint(&fstr, " ns=%s", rp->host->name);
1142 fmtprint(&fstr, " mbox=%s", rp->mb->name);
1145 fmtprint(&fstr, " mbox=%s mbox=%s", rp->mb->name, rp->rmb->name);
1148 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, rp->host->name);
1152 fmtprint(&fstr, " ip=%s", rp->ip->name);
1155 fmtprint(&fstr, " dom=%s", rp->ptr->name);
1158 fmtprint(&fstr, " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1159 rp->host->name, rp->rmb->name, rp->soa->serial,
1160 rp->soa->refresh, rp->soa->retry,
1161 rp->soa->expire, rp->soa->minttl);
1162 for(s = rp->soa->slaves; s != nil; s = s->next)
1163 fmtprint(&fstr, " dnsslave=%s", s->name);
1166 fmtprint(&fstr, " null=%.*H", rp->null->dlen, rp->null->data);
1169 fmtprint(&fstr, " txt=");
1171 for(t = rp->txt; t != nil; t = t->next)
1172 if(strchr(t->p, ' '))
1175 fmtprint(&fstr, "\"");
1176 for(t = rp->txt; t != nil; t = t->next)
1177 fmtprint(&fstr, "%s", t->p);
1179 fmtprint(&fstr, "\"");
1182 fmtprint(&fstr, " rp=%s txt=%s", rp->rmb->name, rp->rp->name);
1185 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1186 rp->key->flags, rp->key->proto, rp->key->alg);
1189 fmtprint(&fstr, " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1190 rp->sig->type, rp->sig->alg, rp->sig->labels, rp->sig->ttl,
1191 rp->sig->exp, rp->sig->incep, rp->sig->tag, rp->sig->signer->name);
1194 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1195 rp->sig->type, rp->sig->tag, rp->sig->alg);
1201 strp = fmtstrflush(&fstr);
1202 rv = fmtstrcpy(f, strp);
1208 warning(char *fmt, ...)
1214 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1216 syslog(1, "dns", dnserr);
1220 * chasing down double free's
1223 dncheck(void *p, int dolock)
1231 assert(dp->magic == DNmagic);
1239 for(i = 0; i < HTLEN; i++)
1240 for(dp = ht[i]; dp; dp = dp->next){
1242 assert(dp->magic == DNmagic);
1243 for(rp = dp->rr; rp; rp = rp->next){
1244 assert(rp->magic == RRmagic);
1246 assert(rp->owner == dp);
1254 rrequiv(RR *r1, RR *r2)
1256 return r1->owner == r2->owner
1257 && r1->type == r2->type
1258 && r1->arg0 == r2->arg0
1259 && r1->arg1 == r2->arg1;
1267 for(; rp; rp = rp->next){
1269 for(nrp = *l; nrp; nrp = *l){
1270 if(rrequiv(rp, nrp)){
1280 * true if second domain is subsumed by the first
1283 subsume(char *higher, char *lower)
1288 hn = strlen(higher);
1292 if(cistrcmp(lower + ln - hn, higher) != 0)
1295 if(ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1302 * randomize the order we return items to provide some
1303 * load balancing for servers.
1305 * only randomize the first class of entries
1310 RR *first, *last, *x, *base;
1313 if(rp == nil || rp->next == nil)
1316 /* just randomize addresses and mx's */
1317 for(x = rp; x; x = x->next)
1318 if(x->type != Ta && x->type != Tmx && x->type != Tns)
1326 /* stop randomizing if we've moved past our class */
1327 if(base->auth != rp->auth || base->db != rp->db){
1352 /* reroll the dice */
1367 char obuf[64]; /* rsc optimization */
1369 if(!(f->flags&FmtPrec) || f->prec < 1)
1372 b = va_arg(f->args, uchar*);
1376 /* if it's a printable, go for it */
1378 for(i = 0; i < len; i++)
1382 if(len >= sizeof obuf)
1383 len = sizeof(obuf)-1;
1384 memmove(obuf, b, len);
1392 f->flags &= ~FmtPrec;
1395 len = (8*ilen+4)/5 + 3;
1398 len = (8*ilen+5)/6 + 4;
1407 if(len > sizeof(obuf)){
1418 rv = enc32(out, len, b, ilen);
1421 rv = enc64(out, len, b, ilen);
1424 rv = enc16(out, len, b, ilen);
1439 return fmtstrcpy(f, "<encodefmt>");
1448 x = mallocz(size, 1);
1451 setmalloctag(x, getcallerpc(&size));
1462 p = mallocz(size, 0);
1465 memmove(p, s, size);
1466 setmalloctag(p, getcallerpc(&s));
1471 * create a pointer record
1474 mkptr(DN *dp, char *ptr, ulong ttl)
1479 ipdp = dnlookup(ptr, Cin, 1);
1491 * look for all ip addresses in this network and make
1492 * pointer records for them.
1495 dnptr(uchar *net, uchar *mask, char *dom, int bytes, int ttl)
1499 RR *rp, *nrp, *first, **l;
1500 uchar ip[IPaddrlen];
1501 uchar nnet[IPaddrlen];
1507 for(i = 0; i < HTLEN; i++){
1508 for(dp = ht[i]; dp; dp = dp->next){
1509 for(rp = dp->rr; rp; rp = rp->next){
1510 if(rp->type != Ta || rp->negative)
1512 parseip(ip, rp->ip->name);
1513 maskip(ip, mask, nnet);
1514 if(ipcmp(net, nnet) != 0)
1517 e = ptr+sizeof(ptr);
1518 for(j = IPaddrlen-1; j >= IPaddrlen-bytes; j--)
1519 p = seprint(p, e, "%d.", ip[j]);
1520 seprint(p, e, "%s", dom);
1521 nrp = mkptr(dp, ptr, ttl);
1528 for(rp = first; rp != nil; rp = nrp){
1536 freeserverlist(Server *s)
1540 for(; s != nil; s = next){
1547 addserver(Server **l, char *name)
1553 s = malloc(sizeof(Server)+strlen(name)+1);
1556 s->name = (char*)(s+1);
1557 strcpy(s->name, name);
1563 copyserverlist(Server *s)
1568 for(ns = nil; s != nil; s = s->next)
1569 addserver(&ns, s->name);