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 static ulong lastclean;
467 if(traceactivity) syslog(0, "dns", "put %d by %d.%d", dnvars.active, getpid(), threadid());
470 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */;
473 * clean out old entries and check for new db periodicly
475 if(dnvars.mutex || (needrefresh == 0 && dnvars.active > 0)){
480 /* wait till we're alone */
482 while(dnvars.active > 0){
489 db2cache(needrefresh);
492 /* let others back in */
499 * Attach a single resource record to a domain name.
500 * - Avoid duplicates with already present RR's
501 * - Chain all RR's of the same type adjacent to one another
502 * - chain authoritative RR's ahead of non-authoritative ones
505 rrattach1(RR *new, int auth)
511 assert(new->magic == RRmagic && !new->cached);
514 new->expire = new->ttl;
516 new->expire = now + Year;
518 assert(dp->magic == DNmagic);
523 * find first rr of the right type
526 for(rp = *l; rp; rp = *l){
527 assert(rp->magic == RRmagic && rp->cached);
528 if(rp->type == new->type)
534 * negative entries replace positive entries
535 * positive entries replace negative entries
536 * newer entries replace older entries with the same fields
538 for(rp = *l; rp; rp = *l){
539 assert(rp->magic == RRmagic && rp->cached);
540 if(rp->type != new->type)
543 if(rp->db == new->db && rp->auth == new->auth){
544 /* negative drives out positive and vice versa */
545 if(rp->negative != new->negative){
552 /* all things equal, pick the newer one */
553 if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
554 /* new drives out old */
555 if(new->ttl > rp->ttl || new->expire > rp->expire){
566 /* Hack for pointer records. This makes sure
567 * the ordering in the list reflects the ordering
568 * received or read from the database
570 if(rp->type == Tptr){
571 if(!rp->negative && !new->negative
572 && rp->ptr->ordinal > new->ptr->ordinal)
588 * Attach a list of resource records to a domain name.
589 * - Avoid duplicates with already present RR's
590 * - Chain all RR's of the same type adjacent to one another
591 * - chain authoritative RR's ahead of non-authoritative ones
592 * - remove any expired RR's
595 rrattach(RR *rp, int auth)
600 for(; rp; rp = next){
604 /* avoid any outside spoofing */
605 if(cachedb && !rp->db && inmyarea(rp->owner->name))
614 * allocate a resource record of a given type
621 rp = emalloc(sizeof(*rp));
623 rp->pc = getcallerpc(&type);
627 rp->soa = emalloc(sizeof(*rp->soa));
628 rp->soa->slaves = nil;
631 rp->key = emalloc(sizeof(*rp->key));
634 rp->cert = emalloc(sizeof(*rp->cert));
637 rp->sig = emalloc(sizeof(*rp->sig));
640 rp->null = emalloc(sizeof(*rp->null));
650 * free a resource record and any related structs
659 assert(rp->magic = RRmagic);
664 assert(dp->magic == DNmagic);
665 for(nrp = dp->rr; nrp; nrp = nrp->next)
666 assert(nrp != rp); /* "rrfree of live rr" */;
671 freeserverlist(rp->soa->slaves);
679 free(rp->cert->data);
687 free(rp->null->data);
691 while(rp->txt != nil){
700 rp->magic = ~rp->magic;
705 * free a list of resource records and any related structs
712 for(; rp; rp = next){
719 rrcopy(RR *rp, RR **last)
729 nrp = rralloc(rp->type);
735 for(t = rp->txt; t != nil; t = t->next){
736 nt = emalloc(sizeof(*nt));
737 nt->p = estrdup(t->p);
747 *nrp->soa = *rp->soa;
748 nrp->soa->slaves = copyserverlist(rp->soa->slaves);
755 key->data = emalloc(key->dlen);
756 memmove(key->data, rp->key->data, rp->key->dlen);
763 sig->data = emalloc(sig->dlen);
764 memmove(sig->data, rp->sig->data, rp->sig->dlen);
771 cert->data = emalloc(cert->dlen);
772 memmove(cert->data, rp->cert->data, rp->cert->dlen);
779 null->data = emalloc(null->dlen);
780 memmove(null->data, rp->null->data, rp->null->dlen);
793 * lookup a resource record of a particular type and
794 * class attached to a domain name. Return copies.
796 * Priority ordering is:
798 * not timed out network authoritative
799 * not timed out network unauthoritative
802 * if flag NOneg is set, don't return negative cached entries.
803 * return nothing instead.
806 rrlookup(DN *dp, int type, int flag)
808 RR *rp, *first, **last;
810 assert(dp->magic == DNmagic);
816 /* try for an authoritative db entry */
817 for(rp = dp->rr; rp; rp = rp->next){
818 assert(rp->magic == RRmagic && rp->cached);
821 if(tsame(type, rp->type))
822 last = rrcopy(rp, last);
827 /* try for an living authoritative network entry */
828 for(rp = dp->rr; rp; rp = rp->next){
831 if(rp->ttl + 60 > now)
832 if(tsame(type, rp->type)){
833 if(flag == NOneg && rp->negative)
835 last = rrcopy(rp, last);
841 /* try for an living unauthoritative network entry */
842 for(rp = dp->rr; rp; rp = rp->next){
844 if(rp->ttl + 60 > now)
845 if(tsame(type, rp->type)){
846 if(flag == NOneg && rp->negative)
848 last = rrcopy(rp, last);
854 /* try for an unauthoritative db entry */
855 for(rp = dp->rr; rp; rp = rp->next){
857 if(tsame(type, rp->type))
858 last = rrcopy(rp, last);
863 /* otherwise, settle for anything we got (except for negative caches) */
864 for(rp = dp->rr; rp; rp = rp->next){
865 if(tsame(type, rp->type)){
868 last = rrcopy(rp, last);
879 * convert an ascii RR type name to its integer representation
886 for(i = 0; i <= Tall; i++)
887 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
890 /* make any a synonym for all */
891 if(strcmp(atype, "any") == 0)
897 * convert an integer RR type to it's ascii name
900 rrname(int type, char *buf, int len)
908 snprint(buf, len, "%d", type);
915 * return 0 if not a supported rr type
918 rrsupported(int type)
920 if(type < 0 || type >Tall)
922 return rrtname[type] != 0;
929 tsame(int t1, int t2)
931 return t1 == t2 || t1 == Tall;
935 * Add resource records to a list, duplicate them if they are cached
936 * RR's since these are shared.
939 rrcat(RR **start, RR *rp)
945 last = &(*last)->next;
952 * remove negative cache rr's from an rr list
977 * remove rr's of a particular type from an rr list
980 rrremtype(RR **l, int type)
989 if(rp->type == type){
1002 * print conversion for rr records
1017 rp = va_arg(f->args, RR*);
1019 fmtprint(&fstr, "<null>");
1023 fmtprint(&fstr, "%s %s", rp->owner->name,
1024 rrname(rp->type, buf, sizeof buf));
1027 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1033 fmtprint(&fstr, "\t%s %s", rp->cpu->name, rp->os->name);
1040 fmtprint(&fstr, "\t%s", rp->host->name);
1044 fmtprint(&fstr, "\t%s", rp->mb->name);
1047 fmtprint(&fstr, "\t%s %s", rp->mb->name, rp->rmb->name);
1050 fmtprint(&fstr, "\t%lud %s", rp->pref, rp->host->name);
1054 fmtprint(&fstr, "\t%s", rp->ip->name);
1057 /* fmtprint(&fstr, "\t%s(%lud)", rp->ptr->name, rp->ptr->ordinal); */
1058 fmtprint(&fstr, "\t%s", rp->ptr->name);
1061 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud", rp->host->name,
1062 rp->rmb->name, rp->soa->serial, rp->soa->refresh, rp->soa->retry,
1063 rp->soa->expire, rp->soa->minttl);
1064 for(s = rp->soa->slaves; s != nil; s = s->next)
1065 fmtprint(&fstr, " %s", s->name);
1068 fmtprint(&fstr, "\t%.*H", rp->null->dlen, rp->null->data);
1071 fmtprint(&fstr, "\t");
1072 for(t = rp->txt; t != nil; t = t->next)
1073 fmtprint(&fstr, "%s", t->p);
1076 fmtprint(&fstr, "\t%s %s", rp->rmb->name, rp->rp->name);
1079 fmtprint(&fstr, "\t%d %d %d", rp->key->flags, rp->key->proto,
1083 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1084 rp->sig->type, rp->sig->alg, rp->sig->labels, rp->sig->ttl,
1085 rp->sig->exp, rp->sig->incep, rp->sig->tag, rp->sig->signer->name);
1088 fmtprint(&fstr, "\t%d %d %d",
1089 rp->sig->type, rp->sig->tag, rp->sig->alg);
1095 strp = fmtstrflush(&fstr);
1096 rv = fmtstrcpy(f, strp);
1102 * print conversion for rr records in attribute value form
1117 rp = va_arg(f->args, RR*);
1119 fmtprint(&fstr, "<null>");
1123 if(rp->type == Tptr)
1124 fmtprint(&fstr, "ptr=%s", rp->owner->name);
1126 fmtprint(&fstr, "dom=%s", rp->owner->name);
1130 fmtprint(&fstr, " cpu=%s os=%s", rp->cpu->name, rp->os->name);
1133 fmtprint(&fstr, " cname=%s", rp->host->name);
1138 fmtprint(&fstr, " mbox=%s", rp->host->name);
1141 fmtprint(&fstr, " ns=%s", rp->host->name);
1145 fmtprint(&fstr, " mbox=%s", rp->mb->name);
1148 fmtprint(&fstr, " mbox=%s mbox=%s", rp->mb->name, rp->rmb->name);
1151 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, rp->host->name);
1155 fmtprint(&fstr, " ip=%s", rp->ip->name);
1158 fmtprint(&fstr, " dom=%s", rp->ptr->name);
1161 fmtprint(&fstr, " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1162 rp->host->name, rp->rmb->name, rp->soa->serial,
1163 rp->soa->refresh, rp->soa->retry,
1164 rp->soa->expire, rp->soa->minttl);
1165 for(s = rp->soa->slaves; s != nil; s = s->next)
1166 fmtprint(&fstr, " dnsslave=%s", s->name);
1169 fmtprint(&fstr, " null=%.*H", rp->null->dlen, rp->null->data);
1172 fmtprint(&fstr, " txt=");
1174 for(t = rp->txt; t != nil; t = t->next)
1175 if(strchr(t->p, ' '))
1178 fmtprint(&fstr, "\"");
1179 for(t = rp->txt; t != nil; t = t->next)
1180 fmtprint(&fstr, "%s", t->p);
1182 fmtprint(&fstr, "\"");
1185 fmtprint(&fstr, " rp=%s txt=%s", rp->rmb->name, rp->rp->name);
1188 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1189 rp->key->flags, rp->key->proto, rp->key->alg);
1192 fmtprint(&fstr, " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1193 rp->sig->type, rp->sig->alg, rp->sig->labels, rp->sig->ttl,
1194 rp->sig->exp, rp->sig->incep, rp->sig->tag, rp->sig->signer->name);
1197 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1198 rp->sig->type, rp->sig->tag, rp->sig->alg);
1204 strp = fmtstrflush(&fstr);
1205 rv = fmtstrcpy(f, strp);
1211 warning(char *fmt, ...)
1217 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1219 syslog(1, "dns", dnserr);
1223 * chasing down double free's
1226 dncheck(void *p, int dolock)
1234 assert(dp->magic == DNmagic);
1242 for(i = 0; i < HTLEN; i++)
1243 for(dp = ht[i]; dp; dp = dp->next){
1245 assert(dp->magic == DNmagic);
1246 for(rp = dp->rr; rp; rp = rp->next){
1247 assert(rp->magic == RRmagic);
1249 assert(rp->owner == dp);
1257 rrequiv(RR *r1, RR *r2)
1259 return r1->owner == r2->owner
1260 && r1->type == r2->type
1261 && r1->arg0 == r2->arg0
1262 && r1->arg1 == r2->arg1;
1270 for(; rp; rp = rp->next){
1272 for(nrp = *l; nrp; nrp = *l){
1273 if(rrequiv(rp, nrp)){
1283 * true if second domain is subsumed by the first
1286 subsume(char *higher, char *lower)
1291 hn = strlen(higher);
1295 if(cistrcmp(lower + ln - hn, higher) != 0)
1298 if(ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1305 * randomize the order we return items to provide some
1306 * load balancing for servers.
1308 * only randomize the first class of entries
1313 RR *first, *last, *x, *base;
1316 if(rp == nil || rp->next == nil)
1319 /* just randomize addresses and mx's */
1320 for(x = rp; x; x = x->next)
1321 if(x->type != Ta && x->type != Tmx && x->type != Tns)
1329 /* stop randomizing if we've moved past our class */
1330 if(base->auth != rp->auth || base->db != rp->db){
1355 /* reroll the dice */
1370 char obuf[64]; /* rsc optimization */
1372 if(!(f->flags&FmtPrec) || f->prec < 1)
1375 b = va_arg(f->args, uchar*);
1379 /* if it's a printable, go for it */
1381 for(i = 0; i < len; i++)
1385 if(len >= sizeof obuf)
1386 len = sizeof(obuf)-1;
1387 memmove(obuf, b, len);
1395 f->flags &= ~FmtPrec;
1398 len = (8*ilen+4)/5 + 3;
1401 len = (8*ilen+5)/6 + 4;
1410 if(len > sizeof(obuf)){
1421 rv = enc32(out, len, b, ilen);
1424 rv = enc64(out, len, b, ilen);
1427 rv = enc16(out, len, b, ilen);
1442 return fmtstrcpy(f, "<encodefmt>");
1451 x = mallocz(size, 1);
1454 setmalloctag(x, getcallerpc(&size));
1465 p = mallocz(size, 0);
1468 memmove(p, s, size);
1469 setmalloctag(p, getcallerpc(&s));
1474 * create a pointer record
1477 mkptr(DN *dp, char *ptr, ulong ttl)
1482 ipdp = dnlookup(ptr, Cin, 1);
1494 * look for all ip addresses in this network and make
1495 * pointer records for them.
1498 dnptr(uchar *net, uchar *mask, char *dom, int bytes, int ttl)
1502 RR *rp, *nrp, *first, **l;
1503 uchar ip[IPaddrlen];
1504 uchar nnet[IPaddrlen];
1510 for(i = 0; i < HTLEN; i++){
1511 for(dp = ht[i]; dp; dp = dp->next){
1512 for(rp = dp->rr; rp; rp = rp->next){
1513 if(rp->type != Ta || rp->negative)
1515 parseip(ip, rp->ip->name);
1516 maskip(ip, mask, nnet);
1517 if(ipcmp(net, nnet) != 0)
1520 e = ptr+sizeof(ptr);
1521 for(j = IPaddrlen-1; j >= IPaddrlen-bytes; j--)
1522 p = seprint(p, e, "%d.", ip[j]);
1523 seprint(p, e, "%s", dom);
1524 nrp = mkptr(dp, ptr, ttl);
1531 for(rp = first; rp != nil; rp = nrp){
1539 freeserverlist(Server *s)
1543 for(; s != nil; s = next){
1550 addserver(Server **l, char *name)
1556 s = malloc(sizeof(Server)+strlen(name)+1);
1559 s->name = (char*)(s+1);
1560 strcpy(s->name, name);
1566 copyserverlist(Server *s)
1571 for(ns = nil; s != nil; s = s->next)
1572 addserver(&ns, s->name);