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
21 Defagefreq = 30*60, /* age names this often (seconds) */
25 * Hash table for domain names. The hash is based only on the
26 * first element of the domain name.
32 ulong names; /* names allocated */
33 ulong oldest; /* longest we'll leave a name around */
36 ushort id; /* same size as in packet */
39 /* names of RR types */
64 [Tnsapptr] "nsap-ptr",
86 [Tipseckey] "ipseckey",
105 /* names of response codes */
106 char *rname[Rmask+1] =
109 [Rformat] "format error",
110 [Rserver] "server failure",
112 [Runimplimented] "unimplemented",
113 [Rrefused] "we don't like you",
114 [Ryxdomain] "name should not exist",
115 [Ryxrrset] "rr set should not exist",
116 [Rnxrrset] "rr set should exist",
117 [Rnotauth] "not authorative",
118 [Rnotzone] "not in zone",
119 [Rbadvers] "bad opt version",
120 /* [Rbadsig] "bad signature", */
122 [Rbadtime] "bad signature time",
123 [Rbadmode] "bad mode",
124 [Rbadname] "duplicate key name",
125 [Rbadalg] "bad algorithm",
127 unsigned nrname = nelem(rname);
129 /* names of op codes */
133 [Oinverse] "inverse query (retired)",
138 ulong target = Deftarget;
141 static ulong agefreq = Defagefreq;
143 static int rrequiv(RR *r1, RR *r2);
144 static int sencodefmt(Fmt*);
147 ding(void*, char *msg)
149 if(strstr(msg, "alarm") != nil) {
151 noted(NCONT); /* resume with system call error */
153 noted(NDFLT); /* die */
159 fmtinstall('E', eipfmt);
160 fmtinstall('I', eipfmt);
161 fmtinstall('V', eipfmt);
162 fmtinstall('R', rrfmt);
163 fmtinstall('Q', rravfmt);
164 fmtinstall('H', sencodefmt);
166 dnvars.oldest = maxage;
168 dnvars.id = truerand(); /* don't start with same id every time */
174 * hash for a domain name
180 uchar *val = (uchar*)name;
182 for(hash = 0; *val; val++)
183 hash = hash*13 + tolower(*val)-'a';
188 * lookup a symbol. if enter is not zero and the name is
189 * not found, create it.
192 dnlookup(char *name, int class, int enter)
197 l = &ht[dnhash(name)];
199 for(dp = *l; dp; dp = dp->next) {
200 assert(dp->magic == DNmagic);
201 if(dp->class == class && cistrcmp(dp->name, name) == 0){
202 dp->referenced = now;
214 dp = emalloc(sizeof(*dp));
216 dp->name = estrdup(name);
217 assert(dp->name != nil);
220 dp->referenced = now;
221 /* add new DN to tail of the hash list. *l points to last next ptr. */
230 rrsame(RR *rr1, RR *rr2)
232 return rr1 == rr2 || rr2 && rrequiv(rr1, rr2) &&
233 rr1->db == rr2->db && rr1->auth == rr2->auth;
237 rronlist(RR *rp, RR *lp)
239 for(; lp; lp = lp->next)
253 fd = create(file, OWRITE, 0666);
258 fprint(fd, "# system %s\n", sysname());
259 fprint(fd, "# slave procs high-water mark\t%lud\n", stats.slavehiwat);
260 fprint(fd, "# queries received by 9p\t%lud\n", stats.qrecvd9p);
261 fprint(fd, "# queries received by udp\t%lud\n", stats.qrecvdudp);
262 fprint(fd, "# queries answered from memory\t%lud\n", stats.answinmem);
263 fprint(fd, "# queries sent by udp\t%lud\n", stats.qsent);
264 for (i = 0; i < nelem(stats.under10ths); i++)
265 if (stats.under10ths[i] || i == nelem(stats.under10ths) - 1)
266 fprint(fd, "# responses arriving within %.1f s.\t%lud\n",
267 (double)(i+1)/10, stats.under10ths[i]);
268 fprint(fd, "\n# queries sent & timed-out\t%lud\n", stats.tmout);
269 fprint(fd, "# cname queries timed-out\t%lud\n", stats.tmoutcname);
270 fprint(fd, "# ipv6 queries timed-out\t%lud\n", stats.tmoutv6);
271 fprint(fd, "\n# negative answers received\t%lud\n", stats.negans);
272 fprint(fd, "# negative answers w Rserver set\t%lud\n", stats.negserver);
273 fprint(fd, "# negative answers w bad delegation\t%lud\n",
275 fprint(fd, "# negative answers w bad delegation & no answers\t%lud\n",
277 fprint(fd, "# negative answers w no Rname set\t%lud\n", stats.negnorname);
278 fprint(fd, "# negative answers cached\t%lud\n", stats.negcached);
282 fprint(fd, "\n# domain names %lud target %lud\n", dnvars.names, target);
297 fd = create(file, OWRITE, 0666);
302 for(i = 0; i < HTLEN; i++)
303 for(dp = ht[i]; dp; dp = dp->next){
304 fprint(fd, "%s\n", dp->name);
305 for(rp = dp->rr; rp; rp = rp->next) {
306 fprint(fd, "\t%R %c%c %lud/%lud\n",
307 rp, rp->auth? 'A': 'U',
308 rp->db? 'D': 'N', rp->expire, rp->ttl);
309 if (rronlist(rp, rp->next))
310 fprint(fd, "*** duplicate:\n");
329 for(i = 0; i < HTLEN; i++)
330 for(dp = ht[i]; dp; dp = dp->next){
333 for(; rp != nil; rp = rp->next)
342 * delete rp from *l, free rp.
343 * call with dnlock held.
346 rrdelete(RR **l, RR *rp)
349 rp->cached = 0; /* avoid blowing an assertion in rrfree */
354 * check the age of resource records, free any that have timed out.
355 * call with dnlock held.
364 diff = now - dp->referenced;
365 if(diff < Reserved || dp->keep)
369 for(rp = dp->rr; rp; rp = next){
370 assert(rp->magic == RRmagic && rp->cached);
372 if(!rp->db && (rp->expire < now || diff > dnvars.oldest))
379 #define MARK(dp) { if (dp) (dp)->keep = 1; }
381 /* mark a domain name and those in its RRs as never to be aged */
383 dnagenever(DN *dp, int dolock)
390 /* mark all referenced domain names */
392 for(rp = dp->rr; rp; rp = rp->next){
395 MARK(rp->negsoaowner);
444 /* mark all current domain names as never to be aged */
453 /* mark all referenced domain names */
454 for(i = 0; i < HTLEN; i++)
455 for(dp = ht[i]; dp; dp = dp->next)
460 dnslog("%ld initial domain names; target is %ld", dnvars.names, target);
461 if(dnvars.names >= target)
462 dnslog("more initial domain names (%ld) than target (%ld)",
463 dnvars.names, target);
466 #define REF(dp) { if (dp) (dp)->refs++; }
469 * periodicly sweep for old records and remove unreferenced domain names
471 * only called when all other threads are locked out
479 static ulong nextage;
481 if(dnvars.names < target || (now < nextage && !doit)){
482 dnvars.oldest = maxage;
486 if(dnvars.names >= target) {
487 dnslog("more names (%lud) than target (%lud)", dnvars.names,
490 if (dnvars.oldest < Minage)
491 dnvars.oldest = Minage; /* don't be silly */
493 if (agefreq > dnvars.oldest / 2)
494 nextage = now + dnvars.oldest / 2;
496 nextage = now + agefreq;
500 /* time out all old entries (and set refs to 0) */
501 for(i = 0; i < HTLEN; i++)
502 for(dp = ht[i]; dp; dp = dp->next){
507 /* mark all referenced domain names */
508 for(i = 0; i < HTLEN; i++)
509 for(dp = ht[i]; dp; dp = dp->next)
510 for(rp = dp->rr; rp; rp = rp->next){
513 REF(rp->negsoaowner);
558 /* sweep and remove unreferenced domain names */
559 for(i = 0; i < HTLEN; i++){
561 for(dp = *l; dp; dp = *l){
562 if(dp->rr == 0 && dp->refs == 0 && !dp->keep){
563 assert(dp->magic == DNmagic);
568 dp->magic = ~dp->magic;
570 memset(dp, 0, sizeof *dp); /* cause trouble */
583 * timeout all database records (used when rereading db)
594 /* time out all database entries */
595 for(i = 0; i < HTLEN; i++)
596 for(dp = ht[i]; dp; dp = dp->next) {
598 for(rp = dp->rr; rp; rp = rp->next)
607 * mark all local db records about my area as authoritative,
608 * time out any others
621 /* time out all database entries */
622 for(i = 0; i < HTLEN; i++)
623 for(dp = ht[i]; dp; dp = dp->next){
624 area = inmyarea(dp->name);
625 for(rp = dp->rr; rp; rp = rp->next)
628 minttl = area->soarr->soa->minttl;
635 dp->referenced = now-Reserved-1;
644 * keep track of other processes to know if we can
645 * garbage collect. block while garbage collecting.
648 getactivity(Request *req, int recursive)
653 dnslog("get: %d active by pid %d from %p",
654 dnvars.active, getpid(), getcallerpc(&req));
657 * can't block here if we're already holding one
658 * of the dnvars.active (recursive). will deadlock.
660 while(!recursive && dnvars.mutex){
662 sleep(100); /* tune; was 200 */
665 rv = ++dnvars.active;
668 req->id = ++dnvars.id;
674 putactivity(int recursive)
676 static ulong lastclean;
679 dnslog("put: %d active by pid %d",
680 dnvars.active, getpid());
683 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
686 * clean out old entries and check for new db periodicly
687 * can't block here if being called to let go a "recursive" lock
688 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
690 if (recursive || dnvars.mutex ||
691 (needrefresh == 0 && dnvars.active > 0)){
696 /* wait till we're alone */
698 while(dnvars.active > 0){
700 sleep(100); /* tune; was 100 */
705 db2cache(needrefresh);
708 /* let others back in */
720 for(; rp; rp = rp->next)
726 * Attach a single resource record to a domain name (new->owner).
727 * - Avoid duplicates with already present RR's
728 * - Chain all RR's of the same type adjacent to one another
729 * - chain authoritative RR's ahead of non-authoritative ones
730 * - remove any expired RR's
731 * If new is a stale duplicate, rrfree it.
732 * Must be called with dnlock held.
735 rrattach1(RR *new, int auth)
741 assert(new->magic == RRmagic && !new->cached);
743 // dnslog("rrattach1: %s", new->owner->name);
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.
750 new->expire = new->ttl > now + Min? new->ttl: now + 10*Min;
752 new->expire = now + Year;
754 assert(dp->magic == DNmagic);
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) {
787 continue; /* *l == rp->next */
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) {
798 continue; /* *l == rp->next */
801 * Hack for pointer records. This makes sure
802 * the ordering in the list reflects the ordering
803 * received or read from the database
805 else if(rp->type == Tptr &&
806 !rp->negative && !new->negative &&
807 rp->ptr->ordinal > new->ptr->ordinal)
813 if (rronlist(new, rp)) {
814 /* should not happen; duplicates were processed above */
815 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
827 * Attach a list of resource records to a domain name.
828 * May rrfree any stale duplicate RRs; dismembers the list.
829 * Upon return, every RR in the list will have been rrfree-d
830 * or attached to its domain name.
831 * See rrattach1 for properties preserved.
834 rrattach(RR *rp, int auth)
840 for(; rp; rp = next){
845 /* avoid any outside spoofing */
846 // dnslog("rrattach: %s", rp->owner->name);
847 if(cfg.cachedb && !rp->db && inmyarea(rp->owner->name))
850 /* ameliorate the memory leak */
851 if (0 && rrlistlen(dp->rr) > 50 && !dp->keep) {
852 dnslog("rrattach(%s): rr list too long; "
853 "freeing it", dp->name);
864 /* should be called with dnlock held */
866 rrcopy(RR *rp, RR **last)
876 nrp = rralloc(rp->type);
877 setmalloctag(nrp, getcallerpc(&rp));
883 for(t = rp->txt; t != nil; t = t->next){
884 nt = emalloc(sizeof(*nt));
885 nt->p = estrdup(t->p);
895 *nrp->soa = *rp->soa;
896 nrp->soa->slaves = copyserverlist(rp->soa->slaves);
900 nrp->srv = emalloc(sizeof *nrp->srv);
901 *nrp->srv = *rp->srv;
908 key->data = emalloc(key->dlen);
909 memmove(key->data, rp->key->data, rp->key->dlen);
916 sig->data = emalloc(sig->dlen);
917 memmove(sig->data, rp->sig->data, rp->sig->dlen);
924 cert->data = emalloc(cert->dlen);
925 memmove(cert->data, rp->cert->data, rp->cert->dlen);
932 null->data = emalloc(null->dlen);
933 memmove(null->data, rp->null->data, rp->null->dlen);
946 * lookup a resource record of a particular type and
947 * class attached to a domain name. Return copies.
949 * Priority ordering is:
951 * not timed out network authoritative
952 * not timed out network unauthoritative
955 * if flag NOneg is set, don't return negative cached entries.
956 * return nothing instead.
959 rrlookup(DN *dp, int type, int flag)
961 RR *rp, *first, **last;
963 assert(dp->magic == DNmagic);
969 /* try for an authoritative db entry */
970 for(rp = dp->rr; rp; rp = rp->next){
971 assert(rp->magic == RRmagic && rp->cached);
974 if(tsame(type, rp->type)) {
975 last = rrcopy(rp, last);
976 // setmalloctag(*last, getcallerpc(&dp));
982 /* try for a living authoritative network entry */
983 for(rp = dp->rr; rp; rp = rp->next){
986 if(rp->ttl + 60 > now)
987 if(tsame(type, rp->type)){
988 if(flag == NOneg && rp->negative)
990 last = rrcopy(rp, last);
996 /* try for a living unauthoritative network entry */
997 for(rp = dp->rr; rp; rp = rp->next){
999 if(rp->ttl + 60 > now)
1000 if(tsame(type, rp->type)){
1001 if(flag == NOneg && rp->negative)
1003 last = rrcopy(rp, last);
1009 /* try for an unauthoritative db entry */
1010 for(rp = dp->rr; rp; rp = rp->next){
1012 if(tsame(type, rp->type))
1013 last = rrcopy(rp, last);
1018 /* otherwise, settle for anything we got (except for negative caches) */
1019 for(rp = dp->rr; rp; rp = rp->next)
1020 if(tsame(type, rp->type)){
1023 last = rrcopy(rp, last);
1029 // dnslog("rrlookup(%s) -> %#p\t# in-core only", dp->name, first);
1031 // setmalloctag(first, getcallerpc(&dp));
1036 * convert an ascii RR type name to its integer representation
1043 for(i = 0; i <= Tall; i++)
1044 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1047 /* make any a synonym for all */
1048 if(strcmp(atype, "any") == 0)
1050 else if(isascii(atype[0]) && isdigit(atype[0]))
1057 * return 0 if not a supported rr type
1060 rrsupported(int type)
1062 if(type < 0 || type >Tall)
1064 return rrtname[type] != nil;
1071 tsame(int t1, int t2)
1073 return t1 == t2 || t1 == Tall;
1077 * Add resource records to a list, duplicate them if they are cached
1078 * RR's since these are shared. should be called with dnlock held
1079 * to avoid racing down the start chain.
1082 rrcat(RR **start, RR *rp)
1087 /* check for duplicates */
1088 for (olp = *start; 0 && olp; olp = olp->next)
1089 for (nlp = rp; nlp; nlp = nlp->next)
1090 if (rrsame(nlp, olp))
1091 dnslog("rrcat: duplicate RR: %R", nlp);
1096 last = &(*last)->next;
1103 * remove negative cache rr's from an rr list
1128 * remove rr's of a particular type from an rr list
1131 rrremtype(RR **l, int type)
1140 if(rp->type == type){
1155 return dn? dn->name: "<null>";
1159 * print conversion for rr records
1176 rp = va_arg(f->args, RR*);
1178 fmtprint(&fstr, "<null>");
1182 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1183 rrname(rp->type, buf, sizeof buf));
1186 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1192 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1199 fmtprint(&fstr, "\t%s", dnname(rp->host));
1203 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1206 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1209 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1213 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1216 // fmtprint(&fstr, "\t%s(%lud)", dnname(rp->ptr),
1217 // rp->ptr? rp->ptr->ordinal: "<null>");
1218 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1222 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1223 dnname(rp->host), dnname(rp->rmb),
1224 (soa? soa->serial: 0),
1225 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1226 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1228 for(s = soa->slaves; s != nil; s = s->next)
1229 fmtprint(&fstr, " %s", s->name);
1233 fmtprint(&fstr, "\t%ud %ud %ud %s",
1234 (srv? srv->pri: 0), (srv? srv->weight: 0),
1235 rp->port, dnname(rp->host));
1238 if (rp->null == nil)
1239 fmtprint(&fstr, "\t<null>");
1241 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1245 fmtprint(&fstr, "\t");
1246 for(t = rp->txt; t != nil; t = t->next)
1247 fmtprint(&fstr, "%s", t->p);
1250 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1254 fmtprint(&fstr, "\t<null> <null> <null>");
1256 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1257 rp->key->proto, rp->key->alg);
1262 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1264 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1265 rp->sig->type, rp->sig->alg, rp->sig->labels,
1266 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1267 rp->sig->tag, dnname(rp->sig->signer));
1270 if (rp->cert == nil)
1271 fmtprint(&fstr, "\t<null> <null> <null>");
1273 fmtprint(&fstr, "\t%d %d %d",
1274 rp->cert->type, rp->cert->tag, rp->cert->alg);
1278 strp = fmtstrflush(&fstr);
1279 rv = fmtstrcpy(f, strp);
1285 * print conversion for rr records in attribute value form
1301 rp = va_arg(f->args, RR*);
1303 fmtprint(&fstr, "<null>");
1307 if(rp->type == Tptr)
1308 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1310 fmtprint(&fstr, "dom=%s", dnname(rp->owner));
1314 fmtprint(&fstr, " cpu=%s os=%s",
1315 dnname(rp->cpu), dnname(rp->os));
1318 fmtprint(&fstr, " cname=%s", dnname(rp->host));
1323 fmtprint(&fstr, " mbox=%s", dnname(rp->host));
1326 fmtprint(&fstr, " ns=%s", dnname(rp->host));
1330 fmtprint(&fstr, " mbox=%s", dnname(rp->mb));
1333 fmtprint(&fstr, " mbox=%s mbox=%s",
1334 dnname(rp->mb), dnname(rp->rmb));
1337 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, dnname(rp->host));
1341 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1344 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1349 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1350 dnname(rp->host), dnname(rp->rmb),
1351 (soa? soa->serial: 0),
1352 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1353 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1354 for(s = soa->slaves; s != nil; s = s->next)
1355 fmtprint(&fstr, " dnsslave=%s", s->name);
1359 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1360 (srv? srv->pri: 0), (srv? srv->weight: 0),
1361 rp->port, dnname(rp->host));
1364 if (rp->null == nil)
1365 fmtprint(&fstr, " null=<null>");
1367 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1371 fmtprint(&fstr, " txt=");
1373 for(t = rp->txt; t != nil; t = t->next)
1374 if(strchr(t->p, ' '))
1377 fmtprint(&fstr, "\"");
1378 for(t = rp->txt; t != nil; t = t->next)
1379 fmtprint(&fstr, "%s", t->p);
1381 fmtprint(&fstr, "\"");
1384 fmtprint(&fstr, " rp=%s txt=%s",
1385 dnname(rp->rmb), dnname(rp->rp));
1389 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1391 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1392 rp->key->flags, rp->key->proto, rp->key->alg);
1397 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1400 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1401 rp->sig->type, rp->sig->alg, rp->sig->labels,
1402 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1403 rp->sig->tag, dnname(rp->sig->signer));
1406 if (rp->cert == nil)
1407 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1409 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1410 rp->cert->type, rp->cert->tag, rp->cert->alg);
1414 strp = fmtstrflush(&fstr);
1415 rv = fmtstrcpy(f, strp);
1421 warning(char *fmt, ...)
1427 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1429 syslog(1, logfile, dnserr); /* on console too */
1433 dnslog(char *fmt, ...)
1439 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1441 syslog(0, logfile, dnserr);
1445 * based on libthread's threadsetname, but drags in less library code.
1446 * actually just sets the arguments displayed.
1449 procsetname(char *fmt, ...)
1457 cmdname = vsmprint(fmt, arg);
1461 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1462 if((fd = open(buf, OWRITE)) >= 0){
1463 write(fd, cmdname, strlen(cmdname)+1);
1470 * create a slave process to handle a request to avoid one request blocking
1479 return; /* we're already a slave process */
1482 * These calls to putactivity cannot block.
1483 * After getactivity(), the current process is counted
1484 * twice in dnvars.active (one will pass to the child).
1485 * If putactivity tries to wait for dnvars.active == 0,
1486 * it will never happen.
1489 /* limit parallelism */
1490 procs = getactivity(req, 1);
1491 if (procs > stats.slavehiwat)
1492 stats.slavehiwat = procs;
1493 if(procs > Maxactive){
1495 dnslog("[%d] too much activity", getpid());
1501 * parent returns to main loop, child does the work.
1502 * don't change note group.
1505 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1510 procsetname("request slave of pid %d", ppid);
1512 dnslog("[%d] take activity from %d", getpid(), ppid);
1513 req->isslave = 1; /* why not `= getpid()'? */
1517 * this relies on rfork producing separate, initially-identical
1518 * stacks, thus giving us two copies of `req', one in each
1522 longjmp(req->mret, 1);
1527 * chasing down double free's
1530 dncheck(void *p, int dolock)
1538 assert(dp->magic == DNmagic);
1547 for(i = 0; i < HTLEN; i++)
1548 for(dp = ht[i]; dp; dp = dp->next){
1550 assert(dp->magic == DNmagic);
1551 for(rp = dp->rr; rp; rp = rp->next){
1552 assert(rp->magic == RRmagic);
1554 assert(rp->owner == dp);
1555 /* also check for duplicate rrs */
1556 if (dolock && rronlist(rp, rp->next)) {
1557 dnslog("%R duplicates its next chain "
1558 "(%R); aborting", rp, rp->next);
1568 rrequiv(RR *r1, RR *r2)
1570 return r1->owner == r2->owner
1571 && r1->type == r2->type
1572 && r1->arg0 == r2->arg0
1573 && r1->arg1 == r2->arg1;
1581 for(; rp; rp = rp->next){
1583 for(nrp = *l; nrp; nrp = *l)
1584 if(rrequiv(rp, nrp)){
1593 * true if second domain is subsumed by the first
1596 subsume(char *higher, char *lower)
1601 hn = strlen(higher);
1602 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1603 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1609 * randomize the order we return items to provide some
1610 * load balancing for servers.
1612 * only randomize the first class of entries
1617 RR *first, *last, *x, *base;
1620 if(rp == nil || rp->next == nil)
1623 /* just randomize addresses, mx's and ns's */
1624 for(x = rp; x; x = x->next)
1625 if(x->type != Ta && x->type != Taaaa &&
1626 x->type != Tmx && x->type != Tns)
1634 /* stop randomizing if we've moved past our class */
1635 if(base->auth != rp->auth || base->db != rp->db){
1660 /* reroll the dice */
1670 int i, len, ilen, rv;
1673 char obuf[64]; /* rsc optimization */
1675 if(!(f->flags&FmtPrec) || f->prec < 1)
1678 b = va_arg(f->args, uchar*);
1682 /* if it's a printable, go for it */
1684 for(i = 0; i < len; i++)
1688 if(len >= sizeof obuf)
1689 len = sizeof(obuf)-1;
1690 memmove(obuf, b, len);
1698 f->flags &= ~FmtPrec;
1701 len = (8*ilen+4)/5 + 3;
1704 len = (8*ilen+5)/6 + 4;
1713 if(len > sizeof(obuf)){
1724 rv = enc32(out, len, b, ilen);
1727 rv = enc64(out, len, b, ilen);
1730 rv = enc16(out, len, b, ilen);
1745 return fmtstrcpy(f, "<encodefmt>");
1753 x = mallocz(size, 1);
1756 setmalloctag(x, getcallerpc(&size));
1767 p = mallocz(size, 0);
1770 memmove(p, s, size);
1771 setmalloctag(p, getcallerpc(&s));
1776 * create a pointer record
1779 mkptr(DN *dp, char *ptr, ulong ttl)
1784 ipdp = dnlookup(ptr, Cin, 1);
1795 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1798 * look for all ip addresses in this network and make
1799 * pointer records for them.
1802 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1808 uchar ip[IPaddrlen], nnet[IPaddrlen];
1809 uchar nibip[IPaddrlen*2];
1811 RR *rp, *nrp, *first, **l;
1815 for(i = 0; i < HTLEN; i++)
1816 for(dp = ht[i]; dp; dp = dp->next)
1817 for(rp = dp->rr; rp; rp = rp->next){
1818 if(rp->type != forwtype || rp->negative)
1820 parseip(ip, rp->ip->name);
1821 maskip(ip, mask, nnet);
1822 if(ipcmp(net, nnet) != 0)
1827 if (forwtype == Taaaa) {
1828 bytes2nibbles(nibip, ip, IPaddrlen);
1834 e = ptr+sizeof(ptr);
1835 for(j = len - 1; j >= len - subdoms; j--)
1836 p = seprint(p, e, (forwtype == Ta?
1837 "%d.": "%x."), ipp[j]);
1838 seprint(p, e, "%s", dom);
1840 nrp = mkptr(dp, ptr, ttl);
1845 for(rp = first; rp != nil; rp = nrp){
1848 rrattach(rp, Authoritative);
1853 addserver(Server **l, char *name)
1859 s = malloc(sizeof(Server)+strlen(name)+1);
1862 s->name = (char*)(s+1);
1863 strcpy(s->name, name);
1869 copyserverlist(Server *s)
1873 for(ns = nil; s != nil; s = s->next)
1874 addserver(&ns, s->name);
1879 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1882 * convert an integer RR type to it's ascii name
1885 rrname(int type, char *buf, int len)
1890 if(type >= 0 && type <= Tall)
1893 snprint(buf, len, "%d", type);
1900 * free a list of resource records and any related structs
1907 for(; rp; rp = next){
1914 freeserverlist(Server *s)
1918 for(; s != nil; s = next){
1925 * allocate a resource record of a given type
1932 rp = emalloc(sizeof(*rp));
1933 rp->magic = RRmagic;
1934 rp->pc = getcallerpc(&type);
1936 if (rp->type != type)
1937 dnslog("rralloc: bogus type %d", type);
1938 setmalloctag(rp, rp->pc);
1941 rp->soa = emalloc(sizeof(*rp->soa));
1942 rp->soa->slaves = nil;
1943 setmalloctag(rp->soa, rp->pc);
1946 rp->srv = emalloc(sizeof(*rp->srv));
1947 setmalloctag(rp->srv, rp->pc);
1950 rp->key = emalloc(sizeof(*rp->key));
1951 setmalloctag(rp->key, rp->pc);
1954 rp->cert = emalloc(sizeof(*rp->cert));
1955 setmalloctag(rp->cert, rp->pc);
1958 rp->sig = emalloc(sizeof(*rp->sig));
1959 setmalloctag(rp->sig, rp->pc);
1962 rp->null = emalloc(sizeof(*rp->null));
1963 setmalloctag(rp->null, rp->pc);
1973 * free a resource record and any related structs
1982 assert(rp->magic == RRmagic);
1983 assert(!rp->cached);
1987 assert(dp->magic == DNmagic);
1988 for(nrp = dp->rr; nrp; nrp = nrp->next)
1989 assert(nrp != rp); /* "rrfree of live rr" */
1994 freeserverlist(rp->soa->slaves);
1995 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
1999 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
2003 free(rp->key->data);
2004 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
2008 free(rp->cert->data);
2009 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
2013 free(rp->sig->data);
2014 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
2018 free(rp->null->data);
2019 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
2023 while(rp->txt != nil){
2027 memset(t, 0, sizeof *t); /* cause trouble */
2033 rp->magic = ~rp->magic;
2034 memset(rp, 0, sizeof *rp); /* cause trouble */