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);
222 dp->referenced = now;
223 /* add new DN to tail of the hash list. *l points to last next ptr. */
232 rrsame(RR *rr1, RR *rr2)
234 return rr1 == rr2 || rr2 && rrequiv(rr1, rr2) &&
235 rr1->db == rr2->db && rr1->auth == rr2->auth;
239 rronlist(RR *rp, RR *lp)
241 for(; lp; lp = lp->next)
255 fd = create(file, OWRITE, 0666);
260 fprint(fd, "# system %s\n", sysname());
261 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
262 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
263 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
264 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
265 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
266 for (i = 0; i < nelem(stats.under10ths); i++)
267 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
268 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
269 (double)(i+1)/10, stats.under10ths[i]);
270 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
271 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
272 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
273 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
274 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
275 fprint(fd, "# negative answers w bad delegation\t%lud\n",
277 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
279 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
280 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
284 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
299 fd = create(file, OWRITE, 0666);
304 for(i = 0; i < HTLEN; i++)
305 for(dp = ht[i]; dp; dp = dp->next){
306 fprint(fd, "%s\n", dp->name);
307 for(rp = dp->rr; rp; rp = rp->next) {
308 fprint(fd, "\t%R %c%c %lud/%lud\n",
309 rp, rp->auth? 'A': 'U',
310 rp->db? 'D': 'N', rp->expire, rp->ttl);
311 if (rronlist(rp, rp->next))
312 fprint(fd, "*** duplicate:\n");
331 for(i = 0; i < HTLEN; i++)
332 for(dp = ht[i]; dp; dp = dp->next){
335 for(; rp != nil; rp = rp->next)
344 * delete head of *l and free the old head.
345 * call with dnlock held.
352 if (canlock(&dnlock))
353 abort(); /* rrdelhead called with dnlock not held */
357 *l = rp->next; /* unlink head */
358 rp->cached = 0; /* avoid blowing an assertion in rrfree */
363 * check the age of resource records, free any that have timed out.
364 * call with dnlock held.
372 if (canlock(&dnlock))
373 abort(); /* dnage called with dnlock not held */
374 diff = now - dp->referenced;
375 if(diff < Reserved || dp->keep)
379 while ((rp = *l) != nil){
380 assert(rp->magic == RRmagic && rp->cached);
381 if(!rp->db && (rp->expire < now || diff > dnvars.oldest))
382 rrdelhead(l); /* rp == *l before; *l == rp->next after */
388 #define MARK(dp) { if (dp) (dp)->keep = 1; }
390 /* mark a domain name and those in its RRs as never to be aged */
392 dnagenever(DN *dp, int dolock)
399 /* mark all referenced domain names */
401 for(rp = dp->rr; rp; rp = rp->next){
404 MARK(rp->negsoaowner);
447 MARK(rp->sig->signer);
456 #define REF(dp) { if (dp) (dp)->refs++; }
459 * periodicly sweep for old records and remove unreferenced domain names
461 * only called when all other threads are locked out
469 static ulong nextage;
471 if(dnvars.names < target || (now < nextage && !doit)){
472 dnvars.oldest = maxage;
476 if(dnvars.names >= target) {
477 dnslog("more names (%lud) than target (%lud)", dnvars.names,
480 if (dnvars.oldest < Minage)
481 dnvars.oldest = Minage; /* don't be silly */
483 if (agefreq > dnvars.oldest / 2)
484 nextage = now + dnvars.oldest / 2;
486 nextage = now + agefreq;
490 /* time out all old entries (and set refs to 0) */
491 for(i = 0; i < HTLEN; i++)
492 for(dp = ht[i]; dp; dp = dp->next){
497 /* mark all referenced domain names */
498 for(i = 0; i < HTLEN; i++)
499 for(dp = ht[i]; dp; dp = dp->next)
500 for(rp = dp->rr; rp; rp = rp->next){
503 REF(rp->negsoaowner);
546 REF(rp->sig->signer);
551 /* sweep and remove unreferenced domain names */
552 for(i = 0; i < HTLEN; i++){
554 for(dp = *l; dp; dp = *l){
555 if(dp->rr == nil && dp->refs == 0 && dp->keep == 0){
556 assert(dp->magic == DNmagic);
560 memset(dp, 0, sizeof *dp); /* cause trouble */
561 dp->magic = ~DNmagic;
575 * timeout all database records (used when rereading db)
586 /* time out all database entries */
587 for(i = 0; i < HTLEN; i++)
588 for(dp = ht[i]; dp; dp = dp->next) {
590 for(rp = dp->rr; rp; rp = rp->next)
599 * mark all local db records about my area as authoritative,
600 * delete timed out ones
613 /* time out all database entries */
614 for(i = 0; i < HTLEN; i++)
615 for(dp = ht[i]; dp; dp = dp->next){
616 area = inmyarea(dp->name);
618 for(rp = *l; rp; rp = *l){
625 minttl = area->soarr->soa->minttl;
639 * keep track of other processes to know if we can
640 * garbage collect. block while garbage collecting.
643 getactivity(Request *req, int recursive)
648 dnslog("get: %d active by pid %d from %p",
649 dnvars.active, getpid(), getcallerpc(&req));
652 * can't block here if we're already holding one
653 * of the dnvars.active (recursive). will deadlock.
655 while(!recursive && dnvars.mutex){
657 sleep(100); /* tune; was 200 */
660 rv = ++dnvars.active;
663 req->id = ++dnvars.id;
669 putactivity(int recursive)
671 static ulong lastclean;
674 dnslog("put: %d active by pid %d",
675 dnvars.active, getpid());
678 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
681 * clean out old entries and check for new db periodicly
682 * can't block here if being called to let go a "recursive" lock
683 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
685 if (recursive || dnvars.mutex ||
686 (needrefresh == 0 && dnvars.active > 0)){
691 /* wait till we're alone */
693 while(dnvars.active > 0){
695 sleep(100); /* tune; was 100 */
702 db2cache(needrefresh);
709 /* let others back in */
721 for(; rp; rp = rp->next)
727 * Attach a single resource record to a domain name (new->owner).
728 * - Avoid duplicates with already present RR's
729 * - Chain all RR's of the same type adjacent to one another
730 * - chain authoritative RR's ahead of non-authoritative ones
731 * - remove any expired RR's
732 * If new is a stale duplicate, rrfree it.
733 * Must be called with dnlock held.
736 rrattach1(RR *new, int auth)
742 assert(new->magic == RRmagic && !new->cached);
745 * try not to let responses expire before we
746 * can use them to complete this query, by extending
747 * past (or nearly past) expiration time.
749 new->expire = new->ttl > now + Min? new->ttl: now + 10*Min;
751 new->expire = now + Year;
753 assert(dp != nil && dp->magic == DNmagic);
758 * find first rr of the right type
761 for(rp = *l; rp; rp = *l){
762 assert(rp->magic == RRmagic && rp->cached);
763 if(rp->type == new->type)
769 * negative entries replace positive entries
770 * positive entries replace negative entries
771 * newer entries replace older entries with the same fields
773 * look farther ahead than just the next entry when looking
774 * for duplicates; RRs of a given type can have different rdata
775 * fields (e.g. multiple NS servers).
777 while ((rp = *l) != nil){
778 assert(rp->magic == RRmagic && rp->cached);
779 if(rp->type != new->type)
782 if(rp->db == new->db && rp->auth == new->auth){
783 /* negative drives out positive and vice versa */
784 if(rp->negative != new->negative) {
785 /* rp == *l before; *l == rp->next after */
789 /* all things equal, pick the newer one */
790 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
791 /* new drives out old */
792 if (new->ttl <= rp->ttl &&
793 new->expire <= rp->expire) {
797 /* rp == *l before; *l == rp->next after */
802 * Hack for pointer records. This makes sure
803 * the ordering in the list reflects the ordering
804 * received or read from the database
806 else if(rp->type == Tptr &&
807 !rp->negative && !new->negative &&
808 rp->ptr->ordinal > new->ptr->ordinal)
814 if (rronlist(new, rp)) {
815 /* should not happen; duplicates were processed above */
816 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
828 * Attach a list of resource records to a domain name.
829 * May rrfree any stale duplicate RRs; dismembers the list.
830 * Upon return, every RR in the list will have been rrfree-d
831 * or attached to its domain name.
832 * See rrattach1 for properties preserved.
835 rrattach(RR *rp, int auth)
841 for(; rp; rp = next){
845 /* avoid any outside spoofing; leave keepers alone */
846 if(cfg.cachedb && !rp->db && (dp->keep || inmyarea(dp->name)))
855 rrcopy(RR *rp, RR **last)
866 assert(rp->magic == RRmagic);
867 nrp = rralloc(rp->type);
874 soa->slaves = copyserverlist(rp->soa->slaves);
887 key->data = emalloc(key->dlen);
888 memmove(key->data, rp->key->data, rp->key->dlen);
895 cert->data = emalloc(cert->dlen);
896 memmove(cert->data, rp->cert->data, rp->cert->dlen);
903 sig->data = emalloc(sig->dlen);
904 memmove(sig->data, rp->sig->data, rp->sig->dlen);
911 null->data = emalloc(null->dlen);
912 memmove(null->data, rp->null->data, rp->null->dlen);
918 for(t = rp->txt; t != nil; t = t->next){
919 nt = emalloc(sizeof(*nt));
920 nt->p = estrdup(t->p);
930 nrp->pc = getcallerpc(&rp);
931 setmalloctag(nrp, nrp->pc);
939 * lookup a resource record of a particular type and
940 * class attached to a domain name. Return copies.
942 * Priority ordering is:
944 * not timed out network authoritative
945 * not timed out network unauthoritative
948 * if flag NOneg is set, don't return negative cached entries.
949 * return nothing instead.
952 rrlookup(DN *dp, int type, int flag)
954 RR *rp, *first, **last;
956 assert(dp->magic == DNmagic);
962 /* try for an authoritative db entry */
963 for(rp = dp->rr; rp; rp = rp->next){
964 assert(rp->magic == RRmagic && rp->cached);
967 if(tsame(type, rp->type))
968 last = rrcopy(rp, last);
973 /* try for a living authoritative network entry */
974 for(rp = dp->rr; rp; rp = rp->next){
977 if(rp->ttl + 60 > now)
978 if(tsame(type, rp->type)){
979 if(flag == NOneg && rp->negative)
981 last = rrcopy(rp, last);
987 /* try for a living unauthoritative network entry */
988 for(rp = dp->rr; rp; rp = rp->next){
990 if(rp->ttl + 60 > now)
991 if(tsame(type, rp->type)){
992 if(flag == NOneg && rp->negative)
994 last = rrcopy(rp, last);
1000 /* try for an unauthoritative db entry */
1001 for(rp = dp->rr; rp; rp = rp->next){
1003 if(tsame(type, rp->type))
1004 last = rrcopy(rp, last);
1009 /* otherwise, settle for anything we got (except for negative caches) */
1010 for(rp = dp->rr; rp; rp = rp->next)
1011 if(tsame(type, rp->type)){
1014 last = rrcopy(rp, last);
1024 * convert an ascii RR type name to its integer representation
1031 for(i = 0; i <= Tall; i++)
1032 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1035 /* make any a synonym for all */
1036 if(strcmp(atype, "any") == 0)
1038 else if(isascii(atype[0]) && isdigit(atype[0]))
1045 * return 0 if not a supported rr type
1048 rrsupported(int type)
1050 if(type < 0 || type >Tall)
1052 return rrtname[type] != nil;
1059 tsame(int t1, int t2)
1061 return t1 == t2 || t1 == Tall;
1065 * Add resource records to a list.
1068 rrcat(RR **start, RR *rp)
1073 /* check for duplicates */
1074 for (olp = *start; 0 && olp; olp = olp->next)
1075 for (nlp = rp; nlp; nlp = nlp->next)
1076 if (rrsame(nlp, olp))
1077 dnslog("rrcat: duplicate RR: %R", nlp);
1082 last = &(*last)->next;
1089 * remove negative cache rr's from an rr list
1114 * remove rr's of a particular type from an rr list
1117 rrremtype(RR **l, int type)
1126 if(rp->type == type){
1141 return dn? dn->name: "<null>";
1145 * print conversion for rr records
1162 rp = va_arg(f->args, RR*);
1164 fmtprint(&fstr, "<null>");
1168 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1169 rrname(rp->type, buf, sizeof buf));
1172 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1178 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1185 fmtprint(&fstr, "\t%s", dnname(rp->host));
1189 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1192 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1195 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1199 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1202 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1206 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1207 dnname(rp->host), dnname(rp->rmb),
1208 (soa? soa->serial: 0),
1209 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1210 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1212 for(s = soa->slaves; s != nil; s = s->next)
1213 fmtprint(&fstr, " %s", s->name);
1217 fmtprint(&fstr, "\t%ud %ud %ud %s",
1218 (srv? srv->pri: 0), (srv? srv->weight: 0),
1219 rp->port, dnname(rp->host));
1222 if (rp->null == nil)
1223 fmtprint(&fstr, "\t<null>");
1225 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1229 fmtprint(&fstr, "\t");
1230 for(t = rp->txt; t != nil; t = t->next)
1231 fmtprint(&fstr, "%s", t->p);
1234 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1238 fmtprint(&fstr, "\t<null> <null> <null>");
1240 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1241 rp->key->proto, rp->key->alg);
1246 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1248 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1249 rp->sig->type, rp->sig->alg, rp->sig->labels,
1250 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1251 rp->sig->tag, dnname(rp->sig->signer));
1254 if (rp->cert == nil)
1255 fmtprint(&fstr, "\t<null> <null> <null>");
1257 fmtprint(&fstr, "\t%d %d %d",
1258 rp->cert->type, rp->cert->tag, rp->cert->alg);
1262 strp = fmtstrflush(&fstr);
1263 rv = fmtstrcpy(f, strp);
1269 * print conversion for rr records in attribute value form
1285 rp = va_arg(f->args, RR*);
1287 fmtprint(&fstr, "<null>");
1291 if(rp->type == Tptr)
1292 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1294 fmtprint(&fstr, "dom=%s", dnname(rp->owner));
1298 fmtprint(&fstr, " cpu=%s os=%s",
1299 dnname(rp->cpu), dnname(rp->os));
1302 fmtprint(&fstr, " cname=%s", dnname(rp->host));
1307 fmtprint(&fstr, " mbox=%s", dnname(rp->host));
1310 fmtprint(&fstr, " ns=%s", dnname(rp->host));
1314 fmtprint(&fstr, " mbox=%s", dnname(rp->mb));
1317 fmtprint(&fstr, " mbox=%s mbox=%s",
1318 dnname(rp->mb), dnname(rp->rmb));
1321 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, dnname(rp->host));
1325 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1328 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1333 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1334 dnname(rp->host), dnname(rp->rmb),
1335 (soa? soa->serial: 0),
1336 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1337 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1338 for(s = soa->slaves; s != nil; s = s->next)
1339 fmtprint(&fstr, " dnsslave=%s", s->name);
1343 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1344 (srv? srv->pri: 0), (srv? srv->weight: 0),
1345 rp->port, dnname(rp->host));
1348 if (rp->null == nil)
1349 fmtprint(&fstr, " null=<null>");
1351 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1355 fmtprint(&fstr, " txt=");
1357 for(t = rp->txt; t != nil; t = t->next)
1358 if(strchr(t->p, ' '))
1361 fmtprint(&fstr, "\"");
1362 for(t = rp->txt; t != nil; t = t->next)
1363 fmtprint(&fstr, "%s", t->p);
1365 fmtprint(&fstr, "\"");
1368 fmtprint(&fstr, " rp=%s txt=%s",
1369 dnname(rp->rmb), dnname(rp->rp));
1373 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1375 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1376 rp->key->flags, rp->key->proto, rp->key->alg);
1381 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1384 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1385 rp->sig->type, rp->sig->alg, rp->sig->labels,
1386 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1387 rp->sig->tag, dnname(rp->sig->signer));
1390 if (rp->cert == nil)
1391 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1393 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1394 rp->cert->type, rp->cert->tag, rp->cert->alg);
1398 strp = fmtstrflush(&fstr);
1399 rv = fmtstrcpy(f, strp);
1405 warning(char *fmt, ...)
1411 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1413 syslog(1, logfile, dnserr); /* on console too */
1417 dnslog(char *fmt, ...)
1423 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1425 syslog(0, logfile, dnserr);
1429 * based on libthread's threadsetname, but drags in less library code.
1430 * actually just sets the arguments displayed.
1433 procsetname(char *fmt, ...)
1441 cmdname = vsmprint(fmt, arg);
1445 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1446 if((fd = open(buf, OWRITE)) >= 0){
1447 write(fd, cmdname, strlen(cmdname)+1);
1454 * create a slave process to handle a request to avoid one request blocking
1463 return; /* we're already a slave process */
1466 * These calls to putactivity cannot block.
1467 * After getactivity(), the current process is counted
1468 * twice in dnvars.active (one will pass to the child).
1469 * If putactivity tries to wait for dnvars.active == 0,
1470 * it will never happen.
1473 /* limit parallelism */
1474 procs = getactivity(req, 1);
1475 if(procs > stats.slavehiwat)
1476 stats.slavehiwat = procs;
1477 if(procs > Maxactive){
1479 dnslog("[%d] too much activity", getpid());
1485 * parent returns to main loop, child does the work.
1486 * don't change note group.
1489 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1494 procsetname("request slave of pid %d", ppid);
1496 dnslog("[%d] take activity from %d", getpid(), ppid);
1497 req->isslave = 1; /* why not `= getpid()'? */
1501 * this relies on rfork producing separate, initially-identical
1502 * stacks, thus giving us two copies of `req', one in each
1506 longjmp(req->mret, 1);
1511 * chasing down double free's
1525 for(i = 0; i < HTLEN; i++)
1526 for(dp = ht[i]; dp; dp = dp->next){
1527 assert(dp->magic == DNmagic);
1528 for(rp = dp->rr; rp; rp = rp->next){
1529 assert(rp->magic == RRmagic);
1531 assert(rp->owner == dp);
1532 /* also check for duplicate rrs */
1533 if (rronlist(rp, rp->next)) {
1534 dnslog("%R duplicates its next chain "
1535 "(%R); aborting", rp, rp->next);
1544 rrequiv(RR *r1, RR *r2)
1546 return r1->owner == r2->owner
1547 && r1->type == r2->type
1548 && r1->arg0 == r2->arg0
1549 && r1->arg1 == r2->arg1;
1557 for(; rp; rp = rp->next){
1559 for(nrp = *l; nrp; nrp = *l)
1560 if(rrequiv(rp, nrp)){
1569 * true if second domain is subsumed by the first
1572 subsume(char *higher, char *lower)
1577 hn = strlen(higher);
1578 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1579 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1585 * randomize the order we return items to provide some
1586 * load balancing for servers.
1588 * only randomize the first class of entries
1593 RR *first, *last, *x, *base;
1596 if(rp == nil || rp->next == nil)
1599 /* just randomize addresses, mx's and ns's */
1600 for(x = rp; x; x = x->next)
1601 if(x->type != Ta && x->type != Taaaa &&
1602 x->type != Tmx && x->type != Tns)
1610 /* stop randomizing if we've moved past our class */
1611 if(base->auth != rp->auth || base->db != rp->db){
1636 /* reroll the dice */
1646 int i, len, ilen, rv;
1649 char obuf[64]; /* rsc optimization */
1651 if(!(f->flags&FmtPrec) || f->prec < 1)
1654 b = va_arg(f->args, uchar*);
1658 /* if it's a printable, go for it */
1660 for(i = 0; i < len; i++)
1664 if(len >= sizeof obuf)
1665 len = sizeof(obuf)-1;
1666 memmove(obuf, b, len);
1674 f->flags &= ~FmtPrec;
1677 len = (8*ilen+4)/5 + 3;
1680 len = (8*ilen+5)/6 + 4;
1689 if(len > sizeof(obuf)){
1700 rv = enc32(out, len, b, ilen);
1703 rv = enc64(out, len, b, ilen);
1706 rv = enc16(out, len, b, ilen);
1721 return fmtstrcpy(f, "<encodefmt>");
1729 x = mallocz(size, 1);
1732 setmalloctag(x, getcallerpc(&size));
1743 p = mallocz(size+1, 0);
1746 memmove(p, s, size);
1748 setmalloctag(p, getcallerpc(&s));
1753 * create a pointer record
1756 mkptr(DN *dp, char *ptr, ulong ttl)
1761 ipdp = dnlookup(ptr, Cin, 1);
1772 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1775 * look for all ip addresses in this network and make
1776 * pointer records for them.
1779 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1785 uchar ip[IPaddrlen], nnet[IPaddrlen];
1786 uchar nibip[IPaddrlen*2];
1788 RR *rp, *nrp, *first, **l;
1792 for(i = 0; i < HTLEN; i++)
1793 for(dp = ht[i]; dp; dp = dp->next)
1794 for(rp = dp->rr; rp; rp = rp->next){
1795 if(rp->type != forwtype || rp->negative)
1797 parseip(ip, rp->ip->name);
1798 maskip(ip, mask, nnet);
1799 if(ipcmp(net, nnet) != 0)
1804 if (forwtype == Taaaa) {
1805 bytes2nibbles(nibip, ip, IPaddrlen);
1811 e = ptr+sizeof(ptr);
1812 for(j = len - 1; j >= len - subdoms; j--)
1813 p = seprint(p, e, (forwtype == Ta?
1814 "%d.": "%x."), ipp[j]);
1815 seprint(p, e, "%s", dom);
1817 nrp = mkptr(dp, ptr, ttl);
1822 for(rp = first; rp != nil; rp = nrp){
1825 rrattach(rp, Authoritative);
1830 addserver(Server **l, char *name)
1838 s = malloc(sizeof(Server)+n+1);
1841 s->name = (char*)(s+1);
1842 memmove(s->name, name, n);
1849 copyserverlist(Server *s)
1853 for(ns = nil; s != nil; s = s->next)
1854 addserver(&ns, s->name);
1859 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1862 * convert an integer RR type to it's ascii name
1865 rrname(int type, char *buf, int len)
1870 if(type >= 0 && type <= Tall)
1873 snprint(buf, len, "%d", type);
1880 * free a list of resource records and any related structs
1887 for(; rp; rp = next){
1894 freeserverlist(Server *s)
1898 for(; s != nil; s = next){
1900 memset(s, 0, sizeof *s); /* cause trouble */
1906 * allocate a resource record of a given type
1913 rp = emalloc(sizeof(*rp));
1914 rp->magic = RRmagic;
1915 rp->pc = getcallerpc(&type);
1917 if (rp->type != type)
1918 dnslog("rralloc: bogus type %d", type);
1919 setmalloctag(rp, rp->pc);
1922 rp->soa = emalloc(sizeof(*rp->soa));
1923 rp->soa->slaves = nil;
1924 setmalloctag(rp->soa, rp->pc);
1927 rp->srv = emalloc(sizeof(*rp->srv));
1928 setmalloctag(rp->srv, rp->pc);
1931 rp->key = emalloc(sizeof(*rp->key));
1932 setmalloctag(rp->key, rp->pc);
1935 rp->cert = emalloc(sizeof(*rp->cert));
1936 setmalloctag(rp->cert, rp->pc);
1939 rp->sig = emalloc(sizeof(*rp->sig));
1940 setmalloctag(rp->sig, rp->pc);
1943 rp->null = emalloc(sizeof(*rp->null));
1944 setmalloctag(rp->null, rp->pc);
1954 * free a resource record and any related structs
1961 assert(rp->magic == RRmagic && !rp->cached);
1965 freeserverlist(rp->soa->slaves);
1966 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
1970 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
1974 free(rp->key->data);
1975 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
1979 free(rp->cert->data);
1980 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
1984 free(rp->sig->data);
1985 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
1989 free(rp->null->data);
1990 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
1997 memset(t, 0, sizeof *t); /* cause trouble */
2003 memset(rp, 0, sizeof *rp); /* cause trouble */
2004 rp->magic = ~RRmagic;