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 /* these settings will trigger frequent aging */
20 Defagefreq = 15*Min, /* age names this often (seconds) */
24 * Hash table for domain names. The hash is based only on the
25 * first element of the domain name.
31 ulong names; /* names allocated */
32 ulong oldest; /* longest we'll leave a name around */
35 ushort id; /* same size as in packet */
38 /* names of RR types */
63 [Tnsapptr] "nsap-ptr",
85 [Tipseckey] "ipseckey",
104 /* names of response codes */
105 char *rname[Rmask+1] =
108 [Rformat] "format error",
109 [Rserver] "server failure",
111 [Runimplimented] "unimplemented",
112 [Rrefused] "we don't like you",
113 [Ryxdomain] "name should not exist",
114 [Ryxrrset] "rr set should not exist",
115 [Rnxrrset] "rr set should exist",
116 [Rnotauth] "not authorative",
117 [Rnotzone] "not in zone",
118 [Rbadvers] "bad opt version",
119 /* [Rbadsig] "bad signature", */
121 [Rbadtime] "bad signature time",
122 [Rbadmode] "bad mode",
123 [Rbadname] "duplicate key name",
124 [Rbadalg] "bad algorithm",
126 unsigned nrname = nelem(rname);
128 /* names of op codes */
132 [Oinverse] "inverse query (retired)",
137 ulong target = Deftarget;
140 static ulong agefreq = Defagefreq;
142 static int rrequiv(RR *r1, RR *r2);
143 static int sencodefmt(Fmt*);
146 ding(void*, char *msg)
148 if(strstr(msg, "alarm") != nil) {
150 noted(NCONT); /* resume with system call error */
152 noted(NDFLT); /* die */
158 fmtinstall('E', eipfmt);
159 fmtinstall('I', eipfmt);
160 fmtinstall('V', eipfmt);
161 fmtinstall('R', rrfmt);
162 fmtinstall('Q', rravfmt);
163 fmtinstall('H', sencodefmt);
165 dnvars.oldest = maxage;
167 dnvars.id = truerand(); /* don't start with same id every time */
173 * hash for a domain name
179 uchar *val = (uchar*)name;
181 for(hash = 0; *val; val++)
182 hash = hash*13 + tolower(*val)-'a';
187 * lookup a symbol. if enter is not zero and the name is
188 * not found, create it.
191 dnlookup(char *name, int class, int enter)
196 l = &ht[dnhash(name)];
198 for(dp = *l; dp; dp = dp->next) {
199 assert(dp->magic == DNmagic);
200 if(dp->class == class && cistrcmp(dp->name, name) == 0){
201 dp->referenced = now;
213 dp = emalloc(sizeof(*dp));
215 dp->name = estrdup(name);
218 dp->referenced = now;
219 /* add new DN to tail of the hash list. *l points to last next ptr. */
228 rrsame(RR *rr1, RR *rr2)
230 return rr1 == rr2 || rr2 && rrequiv(rr1, rr2) &&
231 rr1->db == rr2->db && rr1->auth == rr2->auth;
235 rronlist(RR *rp, RR *lp)
237 for(; lp; lp = lp->next)
251 fd = create(file, OWRITE, 0666);
256 fprint(fd, "# system %s\n", sysname());
257 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
258 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
259 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
260 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
261 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
262 for (i = 0; i < nelem(stats.under10ths); i++)
263 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
264 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
265 (double)(i+1)/10, stats.under10ths[i]);
266 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
267 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
268 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
269 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
270 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
271 fprint(fd, "# negative answers w bad delegation\t%lud\n",
273 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
275 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
276 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
280 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
295 fd = create(file, OWRITE, 0666);
300 for(i = 0; i < HTLEN; i++)
301 for(dp = ht[i]; dp; dp = dp->next){
302 fprint(fd, "%s\n", dp->name);
303 for(rp = dp->rr; rp; rp = rp->next) {
304 fprint(fd, "\t%R %c%c %ld/%lud\n",
305 rp, rp->auth? 'A': 'U',
306 rp->db? 'D': 'N', (long)(rp->expire - now), rp->ttl);
307 if (rronlist(rp, rp->next))
308 fprint(fd, "*** duplicate:\n");
327 for(i = 0; i < HTLEN; i++)
328 for(dp = ht[i]; dp; dp = dp->next){
331 for(; rp != nil; rp = rp->next)
340 * delete head of *l and free the old head.
341 * call with dnlock held.
348 if (canlock(&dnlock))
349 abort(); /* rrdelhead called with dnlock not held */
353 *l = rp->next; /* unlink head */
354 rp->cached = 0; /* avoid blowing an assertion in rrfree */
359 * check the age of resource records, free any that have timed out.
360 * call with dnlock held.
368 if (canlock(&dnlock))
369 abort(); /* dnage called with dnlock not held */
370 diff = now - dp->referenced;
371 if(diff < Reserved || dp->mark != 0)
375 while ((rp = *l) != nil){
376 assert(rp->magic == RRmagic && rp->cached);
377 if(!rp->db && ((long)(rp->expire - now) <= 0 || diff > dnvars.oldest))
378 rrdelhead(l); /* rp == *l before; *l == rp->next after */
384 #define MARK(dp) { if (dp) (dp)->mark |= 2; }
386 /* mark a domain name and those in its RRs as never to be aged */
394 /* mark all referenced domain names */
396 for(rp = dp->rr; rp; rp = rp->next){
399 MARK(rp->negsoaowner);
442 MARK(rp->sig->signer);
450 #define REF(dp) { if (dp) (dp)->mark |= 1; }
453 * periodicly sweep for old records and remove unreferenced domain names
455 * only called when all other threads are locked out
463 static ulong nextage;
465 if(dnvars.names < target || ((long)(nextage - now) > 0 && !doit)){
466 dnvars.oldest = maxage;
470 if(dnvars.names >= target) {
471 dnslog("more names (%lud) than target (%lud)", dnvars.names,
474 if (dnvars.oldest < Minage)
475 dnvars.oldest = Minage; /* don't be silly */
477 if (agefreq > dnvars.oldest / 2)
478 nextage = now + dnvars.oldest / 2;
480 nextage = now + (ulong)agefreq;
484 /* time out all old entries (and set refs to 0) */
485 for(i = 0; i < HTLEN; i++)
486 for(dp = ht[i]; dp; dp = dp->next){
491 /* mark all referenced domain names */
492 for(i = 0; i < HTLEN; i++)
493 for(dp = ht[i]; dp; dp = dp->next)
494 for(rp = dp->rr; rp; rp = rp->next){
497 REF(rp->negsoaowner);
540 REF(rp->sig->signer);
545 /* sweep and remove unreferenced domain names */
546 for(i = 0; i < HTLEN; i++){
548 for(dp = *l; dp; dp = *l){
549 if(dp->rr == nil && dp->mark == 0){
550 assert(dp->magic == DNmagic);
554 memset(dp, 0, sizeof *dp); /* cause trouble */
555 dp->magic = ~DNmagic;
569 * timeout all database records (used when rereading db)
580 /* time out all database entries */
581 for(i = 0; i < HTLEN; i++)
582 for(dp = ht[i]; dp; dp = dp->next) {
584 for(rp = dp->rr; rp; rp = rp->next)
593 * mark all local db records about my area as authoritative,
594 * delete timed out ones
607 /* time out all database entries */
608 for(i = 0; i < HTLEN; i++)
609 for(dp = ht[i]; dp; dp = dp->next){
610 area = inmyarea(dp->name);
612 for(rp = *l; rp; rp = *l){
619 minttl = area->soarr->soa->minttl;
633 * keep track of other processes to know if we can
634 * garbage collect. block while garbage collecting.
637 getactivity(Request *req, int recursive)
642 dnslog("get: %d active by pid %d from %p",
643 dnvars.active, getpid(), getcallerpc(&req));
646 * can't block here if we're already holding one
647 * of the dnvars.active (recursive). will deadlock.
649 while(!recursive && dnvars.mutex){
651 sleep(100); /* tune; was 200 */
654 rv = ++dnvars.active;
657 req->id = ++dnvars.id;
664 putactivity(int recursive)
666 static ulong lastclean;
669 dnslog("put: %d active by pid %d",
670 dnvars.active, getpid());
673 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
676 * clean out old entries and check for new db periodicly
677 * can't block here if being called to let go a "recursive" lock
678 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
680 if (recursive || dnvars.mutex ||
681 (needrefresh == 0 && dnvars.active > 0)){
686 /* wait till we're alone */
688 while(dnvars.active > 0){
690 sleep(100); /* tune; was 100 */
697 db2cache(needrefresh);
704 /* let others back in */
716 for(; rp; rp = rp->next)
722 * Attach a single resource record to a domain name (new->owner).
723 * - Avoid duplicates with already present RR's
724 * - Chain all RR's of the same type adjacent to one another
725 * - chain authoritative RR's ahead of non-authoritative ones
726 * - remove any expired RR's
727 * If new is a stale duplicate, rrfree it.
728 * Must be called with dnlock held.
731 rrattach1(RR *new, int auth)
738 assert(new->magic == RRmagic && !new->cached);
741 assert(dp != nil && dp->magic == DNmagic);
746 * try not to let responses expire before we
747 * can use them to complete this query, by extending
748 * past (or nearly past) expiration time.
756 new->expire = now + ttl;
759 * find first rr of the right type
762 for(rp = *l; rp; rp = *l){
763 assert(rp->magic == RRmagic && rp->cached);
764 if(rp->type == new->type)
770 * negative entries replace positive entries
771 * positive entries replace negative entries
772 * newer entries replace older entries with the same fields
774 * look farther ahead than just the next entry when looking
775 * for duplicates; RRs of a given type can have different rdata
776 * fields (e.g. multiple NS servers).
778 while ((rp = *l) != nil){
779 assert(rp->magic == RRmagic && rp->cached);
780 if(rp->type != new->type)
783 if(rp->db == new->db && rp->auth == new->auth){
784 /* negative drives out positive and vice versa */
785 if(rp->negative != new->negative) {
786 /* rp == *l before; *l == rp->next after */
790 /* all things equal, pick the newer one */
791 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
792 /* old drives out new */
793 if((long)(rp->expire - new->expire) > 0) {
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 */
846 if(cfg.cachedb && !rp->db && 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((long)(rp->expire - now) > 0)
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((long)(rp->expire - now) > 0)
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 rrremfilter(RR **l, int (*filter)(RR*, void*), void *arg)
1098 if((*filter)(rp, arg)){
1111 filterneg(RR *rp, void*)
1113 return rp->negative;
1116 filtertype(RR *rp, void *arg)
1118 return rp->type == *((int*)arg);
1121 filterowner(RR *rp, void *arg)
1123 return rp->owner == (DN*)arg;
1127 * remove negative cache rr's from an rr list
1132 return rrremfilter(l, filterneg, nil);
1136 * remove rr's of a particular type from an rr list
1139 rrremtype(RR **l, int type)
1141 return rrremfilter(l, filtertype, &type);
1145 * remove rr's of a particular owner from an rr list
1148 rrremowner(RR **l, DN *owner)
1150 return rrremfilter(l, filterowner, owner);
1156 return dn? dn->name: "<null>";
1160 * print conversion for rr records
1177 rp = va_arg(f->args, RR*);
1179 fmtprint(&fstr, "<null>");
1183 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1184 rrname(rp->type, buf, sizeof buf));
1187 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1193 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1200 fmtprint(&fstr, "\t%s", dnname(rp->host));
1204 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1207 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1210 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1214 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1217 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1221 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1222 dnname(rp->host), dnname(rp->rmb),
1223 (soa? soa->serial: 0),
1224 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1225 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1227 for(s = soa->slaves; s != nil; s = s->next)
1228 fmtprint(&fstr, " %s", s->name);
1232 fmtprint(&fstr, "\t%ud %ud %ud %s",
1233 (srv? srv->pri: 0), (srv? srv->weight: 0),
1234 rp->port, dnname(rp->host));
1237 if (rp->null == nil)
1238 fmtprint(&fstr, "\t<null>");
1240 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1244 fmtprint(&fstr, "\t");
1245 for(t = rp->txt; t != nil; t = t->next)
1246 fmtprint(&fstr, "%s", t->p);
1249 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1253 fmtprint(&fstr, "\t<null> <null> <null>");
1255 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1256 rp->key->proto, rp->key->alg);
1261 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1263 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1264 rp->sig->type, rp->sig->alg, rp->sig->labels,
1265 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1266 rp->sig->tag, dnname(rp->sig->signer));
1269 if (rp->cert == nil)
1270 fmtprint(&fstr, "\t<null> <null> <null>");
1272 fmtprint(&fstr, "\t%d %d %d",
1273 rp->cert->type, rp->cert->tag, rp->cert->alg);
1277 strp = fmtstrflush(&fstr);
1278 rv = fmtstrcpy(f, strp);
1284 * print conversion for rr records in attribute value form
1300 rp = va_arg(f->args, RR*);
1302 fmtprint(&fstr, "<null>");
1306 if(rp->type == Tptr)
1307 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1309 fmtprint(&fstr, "dom=%s", dnname(rp->owner));
1313 fmtprint(&fstr, " cpu=%s os=%s",
1314 dnname(rp->cpu), dnname(rp->os));
1317 fmtprint(&fstr, " cname=%s", dnname(rp->host));
1322 fmtprint(&fstr, " mbox=%s", dnname(rp->host));
1325 fmtprint(&fstr, " ns=%s", dnname(rp->host));
1329 fmtprint(&fstr, " mbox=%s", dnname(rp->mb));
1332 fmtprint(&fstr, " mbox=%s mbox=%s",
1333 dnname(rp->mb), dnname(rp->rmb));
1336 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, dnname(rp->host));
1340 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1343 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1348 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1349 dnname(rp->host), dnname(rp->rmb),
1350 (soa? soa->serial: 0),
1351 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1352 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1353 for(s = soa->slaves; s != nil; s = s->next)
1354 fmtprint(&fstr, " dnsslave=%s", s->name);
1358 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1359 (srv? srv->pri: 0), (srv? srv->weight: 0),
1360 rp->port, dnname(rp->host));
1363 if (rp->null == nil)
1364 fmtprint(&fstr, " null=<null>");
1366 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1370 fmtprint(&fstr, " txt=");
1372 for(t = rp->txt; t != nil; t = t->next)
1373 if(strchr(t->p, ' '))
1376 fmtprint(&fstr, "\"");
1377 for(t = rp->txt; t != nil; t = t->next)
1378 fmtprint(&fstr, "%s", t->p);
1380 fmtprint(&fstr, "\"");
1383 fmtprint(&fstr, " rp=%s txt=%s",
1384 dnname(rp->rmb), dnname(rp->rp));
1388 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1390 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1391 rp->key->flags, rp->key->proto, rp->key->alg);
1396 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1399 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1400 rp->sig->type, rp->sig->alg, rp->sig->labels,
1401 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1402 rp->sig->tag, dnname(rp->sig->signer));
1405 if (rp->cert == nil)
1406 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1408 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1409 rp->cert->type, rp->cert->tag, rp->cert->alg);
1413 strp = fmtstrflush(&fstr);
1414 rv = fmtstrcpy(f, strp);
1420 warning(char *fmt, ...)
1426 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1428 syslog(1, logfile, dnserr); /* on console too */
1432 dnslog(char *fmt, ...)
1438 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1440 syslog(0, logfile, dnserr);
1444 * based on libthread's threadsetname, but drags in less library code.
1445 * actually just sets the arguments displayed.
1448 procsetname(char *fmt, ...)
1456 cmdname = vsmprint(fmt, arg);
1460 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1461 if((fd = open(buf, OWRITE)) >= 0){
1462 write(fd, cmdname, strlen(cmdname)+1);
1469 * create a slave process to handle a request to avoid one request blocking
1478 return; /* we're already a slave process */
1481 * These calls to putactivity cannot block.
1482 * After getactivity(), the current process is counted
1483 * twice in dnvars.active (one will pass to the child).
1484 * If putactivity tries to wait for dnvars.active == 0,
1485 * it will never happen.
1488 /* limit parallelism */
1489 procs = getactivity(req, 1);
1490 if(procs > stats.slavehiwat)
1491 stats.slavehiwat = procs;
1492 if(procs > Maxactive){
1494 dnslog("[%d] too much activity", getpid());
1500 * parent returns to main loop, child does the work.
1501 * don't change note group.
1504 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1509 procsetname("request slave of pid %d", ppid);
1511 dnslog("[%d] take activity from %d", getpid(), ppid);
1512 req->isslave = 1; /* why not `= getpid()'? */
1516 * this relies on rfork producing separate, initially-identical
1517 * stacks, thus giving us two copies of `req', one in each
1521 longjmp(req->mret, 1);
1526 * chasing down double free's
1540 for(i = 0; i < HTLEN; i++)
1541 for(dp = ht[i]; dp; dp = dp->next){
1542 assert(dp->magic == DNmagic);
1543 for(rp = dp->rr; rp; rp = rp->next){
1544 assert(rp->magic == RRmagic);
1546 assert(rp->owner == dp);
1547 /* also check for duplicate rrs */
1548 if (rronlist(rp, rp->next)) {
1549 dnslog("%R duplicates its next chain "
1550 "(%R); aborting", rp, rp->next);
1559 rrequiv(RR *r1, RR *r2)
1561 return r1->owner == r2->owner
1562 && r1->type == r2->type
1563 && r1->arg0 == r2->arg0
1564 && r1->arg1 == r2->arg1;
1572 for(; rp; rp = rp->next){
1574 for(nrp = *l; nrp; nrp = *l)
1575 if(rrequiv(rp, nrp)){
1584 * true if second domain is subsumed by the first
1587 subsume(char *higher, char *lower)
1592 hn = strlen(higher);
1593 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1594 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1600 * randomize the order we return items to provide some
1601 * load balancing for servers.
1603 * only randomize the first class of entries
1608 RR *first, *last, *x, *base;
1611 if(rp == nil || rp->next == nil)
1614 /* just randomize addresses, mx's and ns's */
1615 for(x = rp; x; x = x->next)
1616 if(x->type != Ta && x->type != Taaaa &&
1617 x->type != Tmx && x->type != Tns)
1625 /* stop randomizing if we've moved past our class */
1626 if(base->auth != rp->auth || base->db != rp->db){
1651 /* reroll the dice */
1661 int i, len, ilen, rv;
1664 char obuf[64]; /* rsc optimization */
1666 if(!(f->flags&FmtPrec) || f->prec < 1)
1669 b = va_arg(f->args, uchar*);
1673 /* if it's a printable, go for it */
1675 for(i = 0; i < len; i++)
1679 if(len >= sizeof obuf)
1680 len = sizeof(obuf)-1;
1681 memmove(obuf, b, len);
1689 f->flags &= ~FmtPrec;
1692 len = (8*ilen+4)/5 + 3;
1695 len = (8*ilen+5)/6 + 4;
1704 if(len > sizeof(obuf)){
1715 rv = enc32(out, len, b, ilen);
1718 rv = enc64(out, len, b, ilen);
1721 rv = enc16(out, len, b, ilen);
1736 return fmtstrcpy(f, "<encodefmt>");
1744 x = mallocz(size, 1);
1747 setmalloctag(x, getcallerpc(&size));
1758 p = mallocz(size+1, 0);
1761 memmove(p, s, size);
1763 setmalloctag(p, getcallerpc(&s));
1768 * create a pointer record
1771 mkptr(DN *dp, char *ptr, ulong ttl)
1776 ipdp = dnlookup(ptr, Cin, 1);
1787 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1790 * look for all ip addresses in this network and make
1791 * pointer records for them.
1794 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1800 uchar ip[IPaddrlen], nnet[IPaddrlen];
1801 uchar nibip[IPaddrlen*2];
1803 RR *rp, *nrp, *first, **l;
1807 for(i = 0; i < HTLEN; i++)
1808 for(dp = ht[i]; dp; dp = dp->next)
1809 for(rp = dp->rr; rp; rp = rp->next){
1810 if(rp->type != forwtype || rp->negative)
1812 parseip(ip, rp->ip->name);
1813 maskip(ip, mask, nnet);
1814 if(ipcmp(net, nnet) != 0)
1819 if (forwtype == Taaaa) {
1820 bytes2nibbles(nibip, ip, IPaddrlen);
1826 e = ptr+sizeof(ptr);
1827 for(j = len - 1; j >= len - subdoms; j--)
1828 p = seprint(p, e, (forwtype == Ta?
1829 "%d.": "%x."), ipp[j]);
1830 seprint(p, e, "%s", dom);
1832 nrp = mkptr(dp, ptr, ttl);
1837 for(rp = first; rp != nil; rp = nrp){
1841 rrattach(rp, Authoritative);
1847 addserver(Server **l, char *name)
1855 s = malloc(sizeof(Server)+n+1);
1858 s->name = (char*)(s+1);
1859 memmove(s->name, name, n);
1866 copyserverlist(Server *s)
1870 for(ns = nil; s != nil; s = s->next)
1871 addserver(&ns, s->name);
1876 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1879 * convert an integer RR type to it's ascii name
1882 rrname(int type, char *buf, int len)
1887 if(type >= 0 && type <= Tall)
1890 snprint(buf, len, "%d", type);
1897 * free a list of resource records and any related structs
1904 for(; rp; rp = next){
1911 freeserverlist(Server *s)
1915 for(; s != nil; s = next){
1917 memset(s, 0, sizeof *s); /* cause trouble */
1923 * allocate a resource record of a given type
1930 rp = emalloc(sizeof(*rp));
1931 rp->magic = RRmagic;
1932 rp->pc = getcallerpc(&type);
1934 if (rp->type != type)
1935 dnslog("rralloc: bogus type %d", type);
1936 setmalloctag(rp, rp->pc);
1939 rp->soa = emalloc(sizeof(*rp->soa));
1940 rp->soa->slaves = nil;
1941 setmalloctag(rp->soa, rp->pc);
1944 rp->srv = emalloc(sizeof(*rp->srv));
1945 setmalloctag(rp->srv, rp->pc);
1948 rp->key = emalloc(sizeof(*rp->key));
1949 setmalloctag(rp->key, rp->pc);
1952 rp->cert = emalloc(sizeof(*rp->cert));
1953 setmalloctag(rp->cert, rp->pc);
1956 rp->sig = emalloc(sizeof(*rp->sig));
1957 setmalloctag(rp->sig, rp->pc);
1960 rp->null = emalloc(sizeof(*rp->null));
1961 setmalloctag(rp->null, rp->pc);
1971 * free a resource record and any related structs
1978 assert(rp->magic == RRmagic && !rp->cached);
1982 freeserverlist(rp->soa->slaves);
1983 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
1987 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
1991 free(rp->key->data);
1992 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
1996 free(rp->cert->data);
1997 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
2001 free(rp->sig->data);
2002 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
2006 free(rp->null->data);
2007 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
2014 memset(t, 0, sizeof *t); /* cause trouble */
2020 memset(rp, 0, sizeof *rp); /* cause trouble */
2021 rp->magic = ~RRmagic;