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 * time out any others
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);
617 for(rp = dp->rr; rp; rp = rp->next)
620 minttl = area->soarr->soa->minttl;
627 dp->referenced = now-Reserved-1;
636 * keep track of other processes to know if we can
637 * garbage collect. block while garbage collecting.
640 getactivity(Request *req, int recursive)
645 dnslog("get: %d active by pid %d from %p",
646 dnvars.active, getpid(), getcallerpc(&req));
649 * can't block here if we're already holding one
650 * of the dnvars.active (recursive). will deadlock.
652 while(!recursive && dnvars.mutex){
654 sleep(100); /* tune; was 200 */
657 rv = ++dnvars.active;
660 req->id = ++dnvars.id;
666 putactivity(int recursive)
668 static ulong lastclean;
671 dnslog("put: %d active by pid %d",
672 dnvars.active, getpid());
675 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
678 * clean out old entries and check for new db periodicly
679 * can't block here if being called to let go a "recursive" lock
680 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
682 if (recursive || dnvars.mutex ||
683 (needrefresh == 0 && dnvars.active > 0)){
688 /* wait till we're alone */
690 while(dnvars.active > 0){
692 sleep(100); /* tune; was 100 */
699 db2cache(needrefresh);
706 /* let others back in */
718 for(; rp; rp = rp->next)
724 * Attach a single resource record to a domain name (new->owner).
725 * - Avoid duplicates with already present RR's
726 * - Chain all RR's of the same type adjacent to one another
727 * - chain authoritative RR's ahead of non-authoritative ones
728 * - remove any expired RR's
729 * If new is a stale duplicate, rrfree it.
730 * Must be called with dnlock held.
733 rrattach1(RR *new, int auth)
739 assert(new->magic == RRmagic && !new->cached);
742 * try not to let responses expire before we
743 * can use them to complete this query, by extending
744 * past (or nearly past) expiration time.
746 new->expire = new->ttl > now + Min? new->ttl: now + 10*Min;
748 new->expire = now + Year;
750 assert(dp != nil && dp->magic == DNmagic);
755 * find first rr of the right type
758 for(rp = *l; rp; rp = *l){
759 assert(rp->magic == RRmagic && rp->cached);
760 if(rp->type == new->type)
766 * negative entries replace positive entries
767 * positive entries replace negative entries
768 * newer entries replace older entries with the same fields
770 * look farther ahead than just the next entry when looking
771 * for duplicates; RRs of a given type can have different rdata
772 * fields (e.g. multiple NS servers).
774 while ((rp = *l) != nil){
775 assert(rp->magic == RRmagic && rp->cached);
776 if(rp->type != new->type)
779 if(rp->db == new->db && rp->auth == new->auth){
780 /* negative drives out positive and vice versa */
781 if(rp->negative != new->negative) {
782 /* rp == *l before; *l == rp->next after */
786 /* all things equal, pick the newer one */
787 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
788 /* new drives out old */
789 if (new->ttl <= rp->ttl &&
790 new->expire <= rp->expire) {
794 /* rp == *l before; *l == rp->next after */
799 * Hack for pointer records. This makes sure
800 * the ordering in the list reflects the ordering
801 * received or read from the database
803 else if(rp->type == Tptr &&
804 !rp->negative && !new->negative &&
805 rp->ptr->ordinal > new->ptr->ordinal)
811 if (rronlist(new, rp)) {
812 /* should not happen; duplicates were processed above */
813 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
825 * Attach a list of resource records to a domain name.
826 * May rrfree any stale duplicate RRs; dismembers the list.
827 * Upon return, every RR in the list will have been rrfree-d
828 * or attached to its domain name.
829 * See rrattach1 for properties preserved.
832 rrattach(RR *rp, int auth)
838 for(; rp; rp = next){
842 /* avoid any outside spoofing; leave keepers alone */
843 if(cfg.cachedb && !rp->db && (dp->keep || inmyarea(dp->name)))
852 rrcopy(RR *rp, RR **last)
863 assert(rp->magic == RRmagic);
864 nrp = rralloc(rp->type);
871 soa->slaves = copyserverlist(rp->soa->slaves);
884 key->data = emalloc(key->dlen);
885 memmove(key->data, rp->key->data, rp->key->dlen);
892 cert->data = emalloc(cert->dlen);
893 memmove(cert->data, rp->cert->data, rp->cert->dlen);
900 sig->data = emalloc(sig->dlen);
901 memmove(sig->data, rp->sig->data, rp->sig->dlen);
908 null->data = emalloc(null->dlen);
909 memmove(null->data, rp->null->data, rp->null->dlen);
915 for(t = rp->txt; t != nil; t = t->next){
916 nt = emalloc(sizeof(*nt));
917 nt->p = estrdup(t->p);
927 nrp->pc = getcallerpc(&rp);
928 setmalloctag(nrp, nrp->pc);
936 * lookup a resource record of a particular type and
937 * class attached to a domain name. Return copies.
939 * Priority ordering is:
941 * not timed out network authoritative
942 * not timed out network unauthoritative
945 * if flag NOneg is set, don't return negative cached entries.
946 * return nothing instead.
949 rrlookup(DN *dp, int type, int flag)
951 RR *rp, *first, **last;
953 assert(dp->magic == DNmagic);
959 /* try for an authoritative db entry */
960 for(rp = dp->rr; rp; rp = rp->next){
961 assert(rp->magic == RRmagic && rp->cached);
964 if(tsame(type, rp->type))
965 last = rrcopy(rp, last);
970 /* try for a living authoritative network entry */
971 for(rp = dp->rr; rp; rp = rp->next){
974 if(rp->ttl + 60 > now)
975 if(tsame(type, rp->type)){
976 if(flag == NOneg && rp->negative)
978 last = rrcopy(rp, last);
984 /* try for a living unauthoritative network entry */
985 for(rp = dp->rr; rp; rp = rp->next){
987 if(rp->ttl + 60 > now)
988 if(tsame(type, rp->type)){
989 if(flag == NOneg && rp->negative)
991 last = rrcopy(rp, last);
997 /* try for an unauthoritative db entry */
998 for(rp = dp->rr; rp; rp = rp->next){
1000 if(tsame(type, rp->type))
1001 last = rrcopy(rp, last);
1006 /* otherwise, settle for anything we got (except for negative caches) */
1007 for(rp = dp->rr; rp; rp = rp->next)
1008 if(tsame(type, rp->type)){
1011 last = rrcopy(rp, last);
1021 * convert an ascii RR type name to its integer representation
1028 for(i = 0; i <= Tall; i++)
1029 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1032 /* make any a synonym for all */
1033 if(strcmp(atype, "any") == 0)
1035 else if(isascii(atype[0]) && isdigit(atype[0]))
1042 * return 0 if not a supported rr type
1045 rrsupported(int type)
1047 if(type < 0 || type >Tall)
1049 return rrtname[type] != nil;
1056 tsame(int t1, int t2)
1058 return t1 == t2 || t1 == Tall;
1062 * Add resource records to a list.
1065 rrcat(RR **start, RR *rp)
1070 /* check for duplicates */
1071 for (olp = *start; 0 && olp; olp = olp->next)
1072 for (nlp = rp; nlp; nlp = nlp->next)
1073 if (rrsame(nlp, olp))
1074 dnslog("rrcat: duplicate RR: %R", nlp);
1079 last = &(*last)->next;
1086 * remove negative cache rr's from an rr list
1111 * remove rr's of a particular type from an rr list
1114 rrremtype(RR **l, int type)
1123 if(rp->type == type){
1138 return dn? dn->name: "<null>";
1142 * print conversion for rr records
1159 rp = va_arg(f->args, RR*);
1161 fmtprint(&fstr, "<null>");
1165 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1166 rrname(rp->type, buf, sizeof buf));
1169 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1175 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1182 fmtprint(&fstr, "\t%s", dnname(rp->host));
1186 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1189 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1192 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1196 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1199 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1203 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1204 dnname(rp->host), dnname(rp->rmb),
1205 (soa? soa->serial: 0),
1206 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1207 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1209 for(s = soa->slaves; s != nil; s = s->next)
1210 fmtprint(&fstr, " %s", s->name);
1214 fmtprint(&fstr, "\t%ud %ud %ud %s",
1215 (srv? srv->pri: 0), (srv? srv->weight: 0),
1216 rp->port, dnname(rp->host));
1219 if (rp->null == nil)
1220 fmtprint(&fstr, "\t<null>");
1222 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1226 fmtprint(&fstr, "\t");
1227 for(t = rp->txt; t != nil; t = t->next)
1228 fmtprint(&fstr, "%s", t->p);
1231 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1235 fmtprint(&fstr, "\t<null> <null> <null>");
1237 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1238 rp->key->proto, rp->key->alg);
1243 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1245 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1246 rp->sig->type, rp->sig->alg, rp->sig->labels,
1247 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1248 rp->sig->tag, dnname(rp->sig->signer));
1251 if (rp->cert == nil)
1252 fmtprint(&fstr, "\t<null> <null> <null>");
1254 fmtprint(&fstr, "\t%d %d %d",
1255 rp->cert->type, rp->cert->tag, rp->cert->alg);
1259 strp = fmtstrflush(&fstr);
1260 rv = fmtstrcpy(f, strp);
1266 * print conversion for rr records in attribute value form
1282 rp = va_arg(f->args, RR*);
1284 fmtprint(&fstr, "<null>");
1288 if(rp->type == Tptr)
1289 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1291 fmtprint(&fstr, "dom=%s", dnname(rp->owner));
1295 fmtprint(&fstr, " cpu=%s os=%s",
1296 dnname(rp->cpu), dnname(rp->os));
1299 fmtprint(&fstr, " cname=%s", dnname(rp->host));
1304 fmtprint(&fstr, " mbox=%s", dnname(rp->host));
1307 fmtprint(&fstr, " ns=%s", dnname(rp->host));
1311 fmtprint(&fstr, " mbox=%s", dnname(rp->mb));
1314 fmtprint(&fstr, " mbox=%s mbox=%s",
1315 dnname(rp->mb), dnname(rp->rmb));
1318 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, dnname(rp->host));
1322 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1325 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1330 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1331 dnname(rp->host), dnname(rp->rmb),
1332 (soa? soa->serial: 0),
1333 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1334 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1335 for(s = soa->slaves; s != nil; s = s->next)
1336 fmtprint(&fstr, " dnsslave=%s", s->name);
1340 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1341 (srv? srv->pri: 0), (srv? srv->weight: 0),
1342 rp->port, dnname(rp->host));
1345 if (rp->null == nil)
1346 fmtprint(&fstr, " null=<null>");
1348 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1352 fmtprint(&fstr, " txt=");
1354 for(t = rp->txt; t != nil; t = t->next)
1355 if(strchr(t->p, ' '))
1358 fmtprint(&fstr, "\"");
1359 for(t = rp->txt; t != nil; t = t->next)
1360 fmtprint(&fstr, "%s", t->p);
1362 fmtprint(&fstr, "\"");
1365 fmtprint(&fstr, " rp=%s txt=%s",
1366 dnname(rp->rmb), dnname(rp->rp));
1370 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1372 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1373 rp->key->flags, rp->key->proto, rp->key->alg);
1378 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1381 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1382 rp->sig->type, rp->sig->alg, rp->sig->labels,
1383 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1384 rp->sig->tag, dnname(rp->sig->signer));
1387 if (rp->cert == nil)
1388 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1390 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1391 rp->cert->type, rp->cert->tag, rp->cert->alg);
1395 strp = fmtstrflush(&fstr);
1396 rv = fmtstrcpy(f, strp);
1402 warning(char *fmt, ...)
1408 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1410 syslog(1, logfile, dnserr); /* on console too */
1414 dnslog(char *fmt, ...)
1420 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1422 syslog(0, logfile, dnserr);
1426 * based on libthread's threadsetname, but drags in less library code.
1427 * actually just sets the arguments displayed.
1430 procsetname(char *fmt, ...)
1438 cmdname = vsmprint(fmt, arg);
1442 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1443 if((fd = open(buf, OWRITE)) >= 0){
1444 write(fd, cmdname, strlen(cmdname)+1);
1451 * create a slave process to handle a request to avoid one request blocking
1460 return; /* we're already a slave process */
1463 * These calls to putactivity cannot block.
1464 * After getactivity(), the current process is counted
1465 * twice in dnvars.active (one will pass to the child).
1466 * If putactivity tries to wait for dnvars.active == 0,
1467 * it will never happen.
1470 /* limit parallelism */
1471 procs = getactivity(req, 1);
1472 if(procs > stats.slavehiwat)
1473 stats.slavehiwat = procs;
1474 if(procs > Maxactive){
1476 dnslog("[%d] too much activity", getpid());
1482 * parent returns to main loop, child does the work.
1483 * don't change note group.
1486 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1491 procsetname("request slave of pid %d", ppid);
1493 dnslog("[%d] take activity from %d", getpid(), ppid);
1494 req->isslave = 1; /* why not `= getpid()'? */
1498 * this relies on rfork producing separate, initially-identical
1499 * stacks, thus giving us two copies of `req', one in each
1503 longjmp(req->mret, 1);
1508 * chasing down double free's
1522 for(i = 0; i < HTLEN; i++)
1523 for(dp = ht[i]; dp; dp = dp->next){
1524 assert(dp->magic == DNmagic);
1525 for(rp = dp->rr; rp; rp = rp->next){
1526 assert(rp->magic == RRmagic);
1528 assert(rp->owner == dp);
1529 /* also check for duplicate rrs */
1530 if (rronlist(rp, rp->next)) {
1531 dnslog("%R duplicates its next chain "
1532 "(%R); aborting", rp, rp->next);
1541 rrequiv(RR *r1, RR *r2)
1543 return r1->owner == r2->owner
1544 && r1->type == r2->type
1545 && r1->arg0 == r2->arg0
1546 && r1->arg1 == r2->arg1;
1554 for(; rp; rp = rp->next){
1556 for(nrp = *l; nrp; nrp = *l)
1557 if(rrequiv(rp, nrp)){
1566 * true if second domain is subsumed by the first
1569 subsume(char *higher, char *lower)
1574 hn = strlen(higher);
1575 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1576 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1582 * randomize the order we return items to provide some
1583 * load balancing for servers.
1585 * only randomize the first class of entries
1590 RR *first, *last, *x, *base;
1593 if(rp == nil || rp->next == nil)
1596 /* just randomize addresses, mx's and ns's */
1597 for(x = rp; x; x = x->next)
1598 if(x->type != Ta && x->type != Taaaa &&
1599 x->type != Tmx && x->type != Tns)
1607 /* stop randomizing if we've moved past our class */
1608 if(base->auth != rp->auth || base->db != rp->db){
1633 /* reroll the dice */
1643 int i, len, ilen, rv;
1646 char obuf[64]; /* rsc optimization */
1648 if(!(f->flags&FmtPrec) || f->prec < 1)
1651 b = va_arg(f->args, uchar*);
1655 /* if it's a printable, go for it */
1657 for(i = 0; i < len; i++)
1661 if(len >= sizeof obuf)
1662 len = sizeof(obuf)-1;
1663 memmove(obuf, b, len);
1671 f->flags &= ~FmtPrec;
1674 len = (8*ilen+4)/5 + 3;
1677 len = (8*ilen+5)/6 + 4;
1686 if(len > sizeof(obuf)){
1697 rv = enc32(out, len, b, ilen);
1700 rv = enc64(out, len, b, ilen);
1703 rv = enc16(out, len, b, ilen);
1718 return fmtstrcpy(f, "<encodefmt>");
1726 x = mallocz(size, 1);
1729 setmalloctag(x, getcallerpc(&size));
1740 p = mallocz(size+1, 0);
1743 memmove(p, s, size);
1745 setmalloctag(p, getcallerpc(&s));
1750 * create a pointer record
1753 mkptr(DN *dp, char *ptr, ulong ttl)
1758 ipdp = dnlookup(ptr, Cin, 1);
1769 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1772 * look for all ip addresses in this network and make
1773 * pointer records for them.
1776 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1782 uchar ip[IPaddrlen], nnet[IPaddrlen];
1783 uchar nibip[IPaddrlen*2];
1785 RR *rp, *nrp, *first, **l;
1789 for(i = 0; i < HTLEN; i++)
1790 for(dp = ht[i]; dp; dp = dp->next)
1791 for(rp = dp->rr; rp; rp = rp->next){
1792 if(rp->type != forwtype || rp->negative)
1794 parseip(ip, rp->ip->name);
1795 maskip(ip, mask, nnet);
1796 if(ipcmp(net, nnet) != 0)
1801 if (forwtype == Taaaa) {
1802 bytes2nibbles(nibip, ip, IPaddrlen);
1808 e = ptr+sizeof(ptr);
1809 for(j = len - 1; j >= len - subdoms; j--)
1810 p = seprint(p, e, (forwtype == Ta?
1811 "%d.": "%x."), ipp[j]);
1812 seprint(p, e, "%s", dom);
1814 nrp = mkptr(dp, ptr, ttl);
1819 for(rp = first; rp != nil; rp = nrp){
1822 rrattach(rp, Authoritative);
1827 addserver(Server **l, char *name)
1835 s = malloc(sizeof(Server)+n+1);
1838 s->name = (char*)(s+1);
1839 memmove(s->name, name, n);
1846 copyserverlist(Server *s)
1850 for(ns = nil; s != nil; s = s->next)
1851 addserver(&ns, s->name);
1856 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1859 * convert an integer RR type to it's ascii name
1862 rrname(int type, char *buf, int len)
1867 if(type >= 0 && type <= Tall)
1870 snprint(buf, len, "%d", type);
1877 * free a list of resource records and any related structs
1884 for(; rp; rp = next){
1891 freeserverlist(Server *s)
1895 for(; s != nil; s = next){
1897 memset(s, 0, sizeof *s); /* cause trouble */
1903 * allocate a resource record of a given type
1910 rp = emalloc(sizeof(*rp));
1911 rp->magic = RRmagic;
1912 rp->pc = getcallerpc(&type);
1914 if (rp->type != type)
1915 dnslog("rralloc: bogus type %d", type);
1916 setmalloctag(rp, rp->pc);
1919 rp->soa = emalloc(sizeof(*rp->soa));
1920 rp->soa->slaves = nil;
1921 setmalloctag(rp->soa, rp->pc);
1924 rp->srv = emalloc(sizeof(*rp->srv));
1925 setmalloctag(rp->srv, rp->pc);
1928 rp->key = emalloc(sizeof(*rp->key));
1929 setmalloctag(rp->key, rp->pc);
1932 rp->cert = emalloc(sizeof(*rp->cert));
1933 setmalloctag(rp->cert, rp->pc);
1936 rp->sig = emalloc(sizeof(*rp->sig));
1937 setmalloctag(rp->sig, rp->pc);
1940 rp->null = emalloc(sizeof(*rp->null));
1941 setmalloctag(rp->null, rp->pc);
1951 * free a resource record and any related structs
1958 assert(rp->magic == RRmagic && !rp->cached);
1962 freeserverlist(rp->soa->slaves);
1963 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
1967 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
1971 free(rp->key->data);
1972 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
1976 free(rp->cert->data);
1977 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
1981 free(rp->sig->data);
1982 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
1986 free(rp->null->data);
1987 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
1994 memset(t, 0, sizeof *t); /* cause trouble */
2000 memset(rp, 0, sizeof *rp); /* cause trouble */
2001 rp->magic = ~RRmagic;