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.
373 if (canlock(&dnlock))
374 abort(); /* dnage called with dnlock not held */
375 diff = now - dp->referenced;
376 if(diff < Reserved || dp->keep)
380 for(rp = dp->rr; rp; rp = next){
381 assert(rp->magic == RRmagic && rp->cached);
383 if(!rp->db && (rp->expire < now || diff > dnvars.oldest))
384 rrdelhead(l); /* rp == *l before; *l == rp->next after */
390 #define MARK(dp) { if (dp) (dp)->keep = 1; }
392 /* mark a domain name and those in its RRs as never to be aged */
394 dnagenever(DN *dp, int dolock)
401 /* mark all referenced domain names */
403 for(rp = dp->rr; rp; rp = rp->next){
406 MARK(rp->negsoaowner);
449 MARK(rp->sig->signer);
458 /* mark all current domain names as never to be aged */
467 /* mark all referenced domain names */
468 for(i = 0; i < HTLEN; i++)
469 for(dp = ht[i]; dp; dp = dp->next)
474 dnslog("%ld initial domain names; target is %ld", dnvars.names, target);
475 if(dnvars.names >= target)
476 dnslog("more initial domain names (%ld) than target (%ld)",
477 dnvars.names, target);
480 #define REF(dp) { if (dp) (dp)->refs++; }
483 * periodicly sweep for old records and remove unreferenced domain names
485 * only called when all other threads are locked out
493 static ulong nextage;
495 if(dnvars.names < target || (now < nextage && !doit)){
496 dnvars.oldest = maxage;
500 if(dnvars.names >= target) {
501 dnslog("more names (%lud) than target (%lud)", dnvars.names,
504 if (dnvars.oldest < Minage)
505 dnvars.oldest = Minage; /* don't be silly */
507 if (agefreq > dnvars.oldest / 2)
508 nextage = now + dnvars.oldest / 2;
510 nextage = now + agefreq;
514 /* time out all old entries (and set refs to 0) */
515 for(i = 0; i < HTLEN; i++)
516 for(dp = ht[i]; dp; dp = dp->next){
521 /* mark all referenced domain names */
522 for(i = 0; i < HTLEN; i++)
523 for(dp = ht[i]; dp; dp = dp->next)
524 for(rp = dp->rr; rp; rp = rp->next){
527 REF(rp->negsoaowner);
570 REF(rp->sig->signer);
575 /* sweep and remove unreferenced domain names */
576 for(i = 0; i < HTLEN; i++){
578 for(dp = *l; dp; dp = *l){
579 if(dp->rr == 0 && dp->refs == 0 && dp->keep == 0){
580 assert(dp->magic == DNmagic);
585 dp->magic = ~dp->magic;
587 memset(dp, 0, sizeof *dp); /* cause trouble */
600 * timeout all database records (used when rereading db)
611 /* time out all database entries */
612 for(i = 0; i < HTLEN; i++)
613 for(dp = ht[i]; dp; dp = dp->next) {
615 for(rp = dp->rr; rp; rp = rp->next)
624 * mark all local db records about my area as authoritative,
625 * time out any others
638 /* time out all database entries */
639 for(i = 0; i < HTLEN; i++)
640 for(dp = ht[i]; dp; dp = dp->next){
641 area = inmyarea(dp->name);
642 for(rp = dp->rr; rp; rp = rp->next)
645 minttl = area->soarr->soa->minttl;
652 dp->referenced = now-Reserved-1;
661 * keep track of other processes to know if we can
662 * garbage collect. block while garbage collecting.
665 getactivity(Request *req, int recursive)
670 dnslog("get: %d active by pid %d from %p",
671 dnvars.active, getpid(), getcallerpc(&req));
674 * can't block here if we're already holding one
675 * of the dnvars.active (recursive). will deadlock.
677 while(!recursive && dnvars.mutex){
679 sleep(100); /* tune; was 200 */
682 rv = ++dnvars.active;
685 req->id = ++dnvars.id;
691 putactivity(int recursive)
693 static ulong lastclean;
696 dnslog("put: %d active by pid %d",
697 dnvars.active, getpid());
700 assert(dnvars.active >= 0); /* "dnvars.active %d", dnvars.active */
703 * clean out old entries and check for new db periodicly
704 * can't block here if being called to let go a "recursive" lock
705 * or we'll deadlock waiting for ourselves to give up the dnvars.active.
707 if (recursive || dnvars.mutex ||
708 (needrefresh == 0 && dnvars.active > 0)){
713 /* wait till we're alone */
715 while(dnvars.active > 0){
717 sleep(100); /* tune; was 100 */
724 db2cache(needrefresh);
728 /* let others back in */
740 for(; rp; rp = rp->next)
746 * Attach a single resource record to a domain name (new->owner).
747 * - Avoid duplicates with already present RR's
748 * - Chain all RR's of the same type adjacent to one another
749 * - chain authoritative RR's ahead of non-authoritative ones
750 * - remove any expired RR's
751 * If new is a stale duplicate, rrfree it.
752 * Must be called with dnlock held.
755 rrattach1(RR *new, int auth)
761 assert(new->magic == RRmagic && !new->cached);
764 * try not to let responses expire before we
765 * can use them to complete this query, by extending
766 * past (or nearly past) expiration time.
768 new->expire = new->ttl > now + Min? new->ttl: now + 10*Min;
770 new->expire = now + Year;
772 assert(dp != nil && dp->magic == DNmagic);
777 * find first rr of the right type
780 for(rp = *l; rp; rp = *l){
781 assert(rp->magic == RRmagic && rp->cached);
782 if(rp->type == new->type)
788 * negative entries replace positive entries
789 * positive entries replace negative entries
790 * newer entries replace older entries with the same fields
792 * look farther ahead than just the next entry when looking
793 * for duplicates; RRs of a given type can have different rdata
794 * fields (e.g. multiple NS servers).
796 while ((rp = *l) != nil){
797 assert(rp->magic == RRmagic && rp->cached);
798 if(rp->type != new->type)
801 if(rp->db == new->db && rp->auth == new->auth){
802 /* negative drives out positive and vice versa */
803 if(rp->negative != new->negative) {
804 /* rp == *l before; *l == rp->next after */
808 /* all things equal, pick the newer one */
809 else if(rp->arg0 == new->arg0 && rp->arg1 == new->arg1){
810 /* new drives out old */
811 if (new->ttl <= rp->ttl &&
812 new->expire <= rp->expire) {
816 /* rp == *l before; *l == rp->next after */
821 * Hack for pointer records. This makes sure
822 * the ordering in the list reflects the ordering
823 * received or read from the database
825 else if(rp->type == Tptr &&
826 !rp->negative && !new->negative &&
827 rp->ptr->ordinal > new->ptr->ordinal)
833 if (rronlist(new, rp)) {
834 /* should not happen; duplicates were processed above */
835 dnslog("adding duplicate %R to list of %R; aborting", new, rp);
847 * Attach a list of resource records to a domain name.
848 * May rrfree any stale duplicate RRs; dismembers the list.
849 * Upon return, every RR in the list will have been rrfree-d
850 * or attached to its domain name.
851 * See rrattach1 for properties preserved.
854 rrattach(RR *rp, int auth)
860 for(; rp; rp = next){
864 /* avoid any outside spoofing; leave keepers alone */
865 if(cfg.cachedb && !rp->db && inmyarea(dp->name)
866 // || dp->keep /* TODO: make this work */
875 /* should be called with dnlock held */
877 rrcopy(RR *rp, RR **last)
888 assert(rp->magic == RRmagic);
889 if (canlock(&dnlock))
890 abort(); /* rrcopy called with dnlock not held */
891 nrp = rralloc(rp->type);
898 soa->slaves = copyserverlist(rp->soa->slaves);
911 key->data = emalloc(key->dlen);
912 memmove(key->data, rp->key->data, rp->key->dlen);
919 cert->data = emalloc(cert->dlen);
920 memmove(cert->data, rp->cert->data, rp->cert->dlen);
927 sig->data = emalloc(sig->dlen);
928 memmove(sig->data, rp->sig->data, rp->sig->dlen);
935 null->data = emalloc(null->dlen);
936 memmove(null->data, rp->null->data, rp->null->dlen);
942 for(t = rp->txt; t != nil; t = t->next){
943 nt = emalloc(sizeof(*nt));
944 nt->p = estrdup(t->p);
954 nrp->pc = getcallerpc(&rp);
955 setmalloctag(nrp, nrp->pc);
963 * lookup a resource record of a particular type and
964 * class attached to a domain name. Return copies.
966 * Priority ordering is:
968 * not timed out network authoritative
969 * not timed out network unauthoritative
972 * if flag NOneg is set, don't return negative cached entries.
973 * return nothing instead.
976 rrlookup(DN *dp, int type, int flag)
978 RR *rp, *first, **last;
980 assert(dp->magic == DNmagic);
986 /* try for an authoritative db entry */
987 for(rp = dp->rr; rp; rp = rp->next){
988 assert(rp->magic == RRmagic && rp->cached);
991 if(tsame(type, rp->type))
992 last = rrcopy(rp, last);
997 /* try for a living authoritative network entry */
998 for(rp = dp->rr; rp; rp = rp->next){
1001 if(rp->ttl + 60 > now)
1002 if(tsame(type, rp->type)){
1003 if(flag == NOneg && rp->negative)
1005 last = rrcopy(rp, last);
1011 /* try for a living unauthoritative network entry */
1012 for(rp = dp->rr; rp; rp = rp->next){
1014 if(rp->ttl + 60 > now)
1015 if(tsame(type, rp->type)){
1016 if(flag == NOneg && rp->negative)
1018 last = rrcopy(rp, last);
1024 /* try for an unauthoritative db entry */
1025 for(rp = dp->rr; rp; rp = rp->next){
1027 if(tsame(type, rp->type))
1028 last = rrcopy(rp, last);
1033 /* otherwise, settle for anything we got (except for negative caches) */
1034 for(rp = dp->rr; rp; rp = rp->next)
1035 if(tsame(type, rp->type)){
1038 last = rrcopy(rp, last);
1048 * convert an ascii RR type name to its integer representation
1055 for(i = 0; i <= Tall; i++)
1056 if(rrtname[i] && strcmp(rrtname[i], atype) == 0)
1059 /* make any a synonym for all */
1060 if(strcmp(atype, "any") == 0)
1062 else if(isascii(atype[0]) && isdigit(atype[0]))
1069 * return 0 if not a supported rr type
1072 rrsupported(int type)
1074 if(type < 0 || type >Tall)
1076 return rrtname[type] != nil;
1083 tsame(int t1, int t2)
1085 return t1 == t2 || t1 == Tall;
1089 * Add resource records to a list, duplicate them if they are cached
1090 * RR's since these are shared. should be called with dnlock held
1091 * to avoid racing down the start chain.
1094 rrcat(RR **start, RR *rp)
1099 if (canlock(&dnlock))
1100 abort(); /* rrcat called with dnlock not held */
1101 /* check for duplicates */
1102 for (olp = *start; 0 && olp; olp = olp->next)
1103 for (nlp = rp; nlp; nlp = nlp->next)
1104 if (rrsame(nlp, olp))
1105 dnslog("rrcat: duplicate RR: %R", nlp);
1110 last = &(*last)->next;
1117 * remove negative cache rr's from an rr list
1125 if (canlock(&dnlock))
1126 abort(); /* rrremneg called with dnlock not held */
1144 * remove rr's of a particular type from an rr list
1147 rrremtype(RR **l, int type)
1156 if(rp->type == type){
1171 return dn? dn->name: "<null>";
1175 * print conversion for rr records
1192 rp = va_arg(f->args, RR*);
1194 fmtprint(&fstr, "<null>");
1198 fmtprint(&fstr, "%s %s", dnname(rp->owner),
1199 rrname(rp->type, buf, sizeof buf));
1202 fmtprint(&fstr, "\tnegative - rcode %d", rp->negrcode);
1208 fmtprint(&fstr, "\t%s %s", dnname(rp->cpu), dnname(rp->os));
1215 fmtprint(&fstr, "\t%s", dnname(rp->host));
1219 fmtprint(&fstr, "\t%s", dnname(rp->mb));
1222 fmtprint(&fstr, "\t%s %s", dnname(rp->mb), dnname(rp->rmb));
1225 fmtprint(&fstr, "\t%lud %s", rp->pref, dnname(rp->host));
1229 fmtprint(&fstr, "\t%s", dnname(rp->ip));
1232 fmtprint(&fstr, "\t%s", dnname(rp->ptr));
1236 fmtprint(&fstr, "\t%s %s %lud %lud %lud %lud %lud",
1237 dnname(rp->host), dnname(rp->rmb),
1238 (soa? soa->serial: 0),
1239 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1240 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1242 for(s = soa->slaves; s != nil; s = s->next)
1243 fmtprint(&fstr, " %s", s->name);
1247 fmtprint(&fstr, "\t%ud %ud %ud %s",
1248 (srv? srv->pri: 0), (srv? srv->weight: 0),
1249 rp->port, dnname(rp->host));
1252 if (rp->null == nil)
1253 fmtprint(&fstr, "\t<null>");
1255 fmtprint(&fstr, "\t%.*H", rp->null->dlen,
1259 fmtprint(&fstr, "\t");
1260 for(t = rp->txt; t != nil; t = t->next)
1261 fmtprint(&fstr, "%s", t->p);
1264 fmtprint(&fstr, "\t%s %s", dnname(rp->rmb), dnname(rp->rp));
1268 fmtprint(&fstr, "\t<null> <null> <null>");
1270 fmtprint(&fstr, "\t%d %d %d", rp->key->flags,
1271 rp->key->proto, rp->key->alg);
1276 "\t<null> <null> <null> <null> <null> <null> <null> <null>");
1278 fmtprint(&fstr, "\t%d %d %d %lud %lud %lud %d %s",
1279 rp->sig->type, rp->sig->alg, rp->sig->labels,
1280 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1281 rp->sig->tag, dnname(rp->sig->signer));
1284 if (rp->cert == nil)
1285 fmtprint(&fstr, "\t<null> <null> <null>");
1287 fmtprint(&fstr, "\t%d %d %d",
1288 rp->cert->type, rp->cert->tag, rp->cert->alg);
1292 strp = fmtstrflush(&fstr);
1293 rv = fmtstrcpy(f, strp);
1299 * print conversion for rr records in attribute value form
1315 rp = va_arg(f->args, RR*);
1317 fmtprint(&fstr, "<null>");
1321 if(rp->type == Tptr)
1322 fmtprint(&fstr, "ptr=%s", dnname(rp->owner));
1324 fmtprint(&fstr, "dom=%s", dnname(rp->owner));
1328 fmtprint(&fstr, " cpu=%s os=%s",
1329 dnname(rp->cpu), dnname(rp->os));
1332 fmtprint(&fstr, " cname=%s", dnname(rp->host));
1337 fmtprint(&fstr, " mbox=%s", dnname(rp->host));
1340 fmtprint(&fstr, " ns=%s", dnname(rp->host));
1344 fmtprint(&fstr, " mbox=%s", dnname(rp->mb));
1347 fmtprint(&fstr, " mbox=%s mbox=%s",
1348 dnname(rp->mb), dnname(rp->rmb));
1351 fmtprint(&fstr, " pref=%lud mx=%s", rp->pref, dnname(rp->host));
1355 fmtprint(&fstr, " ip=%s", dnname(rp->ip));
1358 fmtprint(&fstr, " dom=%s", dnname(rp->ptr));
1363 " ns=%s mbox=%s serial=%lud refresh=%lud retry=%lud expire=%lud ttl=%lud",
1364 dnname(rp->host), dnname(rp->rmb),
1365 (soa? soa->serial: 0),
1366 (soa? soa->refresh: 0), (soa? soa->retry: 0),
1367 (soa? soa->expire: 0), (soa? soa->minttl: 0));
1368 for(s = soa->slaves; s != nil; s = s->next)
1369 fmtprint(&fstr, " dnsslave=%s", s->name);
1373 fmtprint(&fstr, " pri=%ud weight=%ud port=%ud target=%s",
1374 (srv? srv->pri: 0), (srv? srv->weight: 0),
1375 rp->port, dnname(rp->host));
1378 if (rp->null == nil)
1379 fmtprint(&fstr, " null=<null>");
1381 fmtprint(&fstr, " null=%.*H", rp->null->dlen,
1385 fmtprint(&fstr, " txt=");
1387 for(t = rp->txt; t != nil; t = t->next)
1388 if(strchr(t->p, ' '))
1391 fmtprint(&fstr, "\"");
1392 for(t = rp->txt; t != nil; t = t->next)
1393 fmtprint(&fstr, "%s", t->p);
1395 fmtprint(&fstr, "\"");
1398 fmtprint(&fstr, " rp=%s txt=%s",
1399 dnname(rp->rmb), dnname(rp->rp));
1403 fmtprint(&fstr, " flags=<null> proto=<null> alg=<null>");
1405 fmtprint(&fstr, " flags=%d proto=%d alg=%d",
1406 rp->key->flags, rp->key->proto, rp->key->alg);
1411 " type=<null> alg=<null> labels=<null> ttl=<null> exp=<null> incep=<null> tag=<null> signer=<null>");
1414 " type=%d alg=%d labels=%d ttl=%lud exp=%lud incep=%lud tag=%d signer=%s",
1415 rp->sig->type, rp->sig->alg, rp->sig->labels,
1416 rp->sig->ttl, rp->sig->exp, rp->sig->incep,
1417 rp->sig->tag, dnname(rp->sig->signer));
1420 if (rp->cert == nil)
1421 fmtprint(&fstr, " type=<null> tag=<null> alg=<null>");
1423 fmtprint(&fstr, " type=%d tag=%d alg=%d",
1424 rp->cert->type, rp->cert->tag, rp->cert->alg);
1428 strp = fmtstrflush(&fstr);
1429 rv = fmtstrcpy(f, strp);
1435 warning(char *fmt, ...)
1441 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1443 syslog(1, logfile, dnserr); /* on console too */
1447 dnslog(char *fmt, ...)
1453 vseprint(dnserr, dnserr+sizeof(dnserr), fmt, arg);
1455 syslog(0, logfile, dnserr);
1459 * based on libthread's threadsetname, but drags in less library code.
1460 * actually just sets the arguments displayed.
1463 procsetname(char *fmt, ...)
1471 cmdname = vsmprint(fmt, arg);
1475 snprint(buf, sizeof buf, "#p/%d/args", getpid());
1476 if((fd = open(buf, OWRITE)) >= 0){
1477 write(fd, cmdname, strlen(cmdname)+1);
1484 * create a slave process to handle a request to avoid one request blocking
1493 return; /* we're already a slave process */
1496 * These calls to putactivity cannot block.
1497 * After getactivity(), the current process is counted
1498 * twice in dnvars.active (one will pass to the child).
1499 * If putactivity tries to wait for dnvars.active == 0,
1500 * it will never happen.
1503 /* limit parallelism */
1504 procs = getactivity(req, 1);
1505 if(procs > stats.slavehiwat)
1506 stats.slavehiwat = procs;
1507 if(procs > Maxactive){
1509 dnslog("[%d] too much activity", getpid());
1515 * parent returns to main loop, child does the work.
1516 * don't change note group.
1519 switch(rfork(RFPROC|RFMEM|RFNOWAIT)){
1524 procsetname("request slave of pid %d", ppid);
1526 dnslog("[%d] take activity from %d", getpid(), ppid);
1527 req->isslave = 1; /* why not `= getpid()'? */
1531 * this relies on rfork producing separate, initially-identical
1532 * stacks, thus giving us two copies of `req', one in each
1536 longjmp(req->mret, 1);
1541 * chasing down double free's
1544 dncheck(void *p, int dolock)
1552 assert(dp->magic == DNmagic);
1561 for(i = 0; i < HTLEN; i++)
1562 for(dp = ht[i]; dp; dp = dp->next){
1564 assert(dp->magic == DNmagic);
1565 for(rp = dp->rr; rp; rp = rp->next){
1566 assert(rp->magic == RRmagic);
1568 assert(rp->owner == dp);
1569 /* also check for duplicate rrs */
1570 if (dolock && rronlist(rp, rp->next)) {
1571 dnslog("%R duplicates its next chain "
1572 "(%R); aborting", rp, rp->next);
1582 rrequiv(RR *r1, RR *r2)
1584 return r1->owner == r2->owner
1585 && r1->type == r2->type
1586 && r1->arg0 == r2->arg0
1587 && r1->arg1 == r2->arg1;
1590 /* called with dnlock held */
1596 for(; rp; rp = rp->next){
1598 for(nrp = *l; nrp; nrp = *l)
1599 if(rrequiv(rp, nrp)){
1608 * true if second domain is subsumed by the first
1611 subsume(char *higher, char *lower)
1616 hn = strlen(higher);
1617 if (ln < hn || cistrcmp(lower + ln - hn, higher) != 0 ||
1618 ln > hn && hn != 0 && lower[ln - hn - 1] != '.')
1624 * randomize the order we return items to provide some
1625 * load balancing for servers.
1627 * only randomize the first class of entries
1632 RR *first, *last, *x, *base;
1635 if(rp == nil || rp->next == nil)
1638 /* just randomize addresses, mx's and ns's */
1639 for(x = rp; x; x = x->next)
1640 if(x->type != Ta && x->type != Taaaa &&
1641 x->type != Tmx && x->type != Tns)
1649 /* stop randomizing if we've moved past our class */
1650 if(base->auth != rp->auth || base->db != rp->db){
1675 /* reroll the dice */
1685 int i, len, ilen, rv;
1688 char obuf[64]; /* rsc optimization */
1690 if(!(f->flags&FmtPrec) || f->prec < 1)
1693 b = va_arg(f->args, uchar*);
1697 /* if it's a printable, go for it */
1699 for(i = 0; i < len; i++)
1703 if(len >= sizeof obuf)
1704 len = sizeof(obuf)-1;
1705 memmove(obuf, b, len);
1713 f->flags &= ~FmtPrec;
1716 len = (8*ilen+4)/5 + 3;
1719 len = (8*ilen+5)/6 + 4;
1728 if(len > sizeof(obuf)){
1739 rv = enc32(out, len, b, ilen);
1742 rv = enc64(out, len, b, ilen);
1745 rv = enc16(out, len, b, ilen);
1760 return fmtstrcpy(f, "<encodefmt>");
1768 x = mallocz(size, 1);
1771 setmalloctag(x, getcallerpc(&size));
1782 p = mallocz(size+1, 0);
1785 memmove(p, s, size);
1787 setmalloctag(p, getcallerpc(&s));
1792 * create a pointer record
1795 mkptr(DN *dp, char *ptr, ulong ttl)
1800 ipdp = dnlookup(ptr, Cin, 1);
1811 void bytes2nibbles(uchar *nibbles, uchar *bytes, int nbytes);
1814 * look for all ip addresses in this network and make
1815 * pointer records for them.
1818 dnptr(uchar *net, uchar *mask, char *dom, int forwtype, int subdoms, int ttl)
1824 uchar ip[IPaddrlen], nnet[IPaddrlen];
1825 uchar nibip[IPaddrlen*2];
1827 RR *rp, *nrp, *first, **l;
1831 for(i = 0; i < HTLEN; i++)
1832 for(dp = ht[i]; dp; dp = dp->next)
1833 for(rp = dp->rr; rp; rp = rp->next){
1834 if(rp->type != forwtype || rp->negative)
1836 parseip(ip, rp->ip->name);
1837 maskip(ip, mask, nnet);
1838 if(ipcmp(net, nnet) != 0)
1843 if (forwtype == Taaaa) {
1844 bytes2nibbles(nibip, ip, IPaddrlen);
1850 e = ptr+sizeof(ptr);
1851 for(j = len - 1; j >= len - subdoms; j--)
1852 p = seprint(p, e, (forwtype == Ta?
1853 "%d.": "%x."), ipp[j]);
1854 seprint(p, e, "%s", dom);
1856 nrp = mkptr(dp, ptr, ttl);
1861 for(rp = first; rp != nil; rp = nrp){
1864 rrattach(rp, Authoritative);
1869 addserver(Server **l, char *name)
1877 s = malloc(sizeof(Server)+n+1);
1880 s->name = (char*)(s+1);
1881 memmove(s->name, name, n);
1888 copyserverlist(Server *s)
1892 for(ns = nil; s != nil; s = s->next)
1893 addserver(&ns, s->name);
1898 /* from here down is copied to ip/snoopy/dns.c periodically to update it */
1901 * convert an integer RR type to it's ascii name
1904 rrname(int type, char *buf, int len)
1909 if(type >= 0 && type <= Tall)
1912 snprint(buf, len, "%d", type);
1919 * free a list of resource records and any related structs
1926 for(; rp; rp = next){
1933 freeserverlist(Server *s)
1937 for(; s != nil; s = next){
1939 memset(s, 0, sizeof *s); /* cause trouble */
1945 * allocate a resource record of a given type
1952 rp = emalloc(sizeof(*rp));
1953 rp->magic = RRmagic;
1954 rp->pc = getcallerpc(&type);
1956 if (rp->type != type)
1957 dnslog("rralloc: bogus type %d", type);
1958 setmalloctag(rp, rp->pc);
1961 rp->soa = emalloc(sizeof(*rp->soa));
1962 rp->soa->slaves = nil;
1963 setmalloctag(rp->soa, rp->pc);
1966 rp->srv = emalloc(sizeof(*rp->srv));
1967 setmalloctag(rp->srv, rp->pc);
1970 rp->key = emalloc(sizeof(*rp->key));
1971 setmalloctag(rp->key, rp->pc);
1974 rp->cert = emalloc(sizeof(*rp->cert));
1975 setmalloctag(rp->cert, rp->pc);
1978 rp->sig = emalloc(sizeof(*rp->sig));
1979 setmalloctag(rp->sig, rp->pc);
1982 rp->null = emalloc(sizeof(*rp->null));
1983 setmalloctag(rp->null, rp->pc);
1993 * free a resource record and any related structs
2002 assert(rp->magic == RRmagic && !rp->cached);
2006 assert(dp->magic == DNmagic);
2007 for(nrp = dp->rr; nrp; nrp = nrp->next)
2008 assert(nrp != rp); /* "rrfree of live rr" */
2013 freeserverlist(rp->soa->slaves);
2014 memset(rp->soa, 0, sizeof *rp->soa); /* cause trouble */
2018 memset(rp->srv, 0, sizeof *rp->srv); /* cause trouble */
2022 free(rp->key->data);
2023 memset(rp->key, 0, sizeof *rp->key); /* cause trouble */
2027 free(rp->cert->data);
2028 memset(rp->cert, 0, sizeof *rp->cert); /* cause trouble */
2032 free(rp->sig->data);
2033 memset(rp->sig, 0, sizeof *rp->sig); /* cause trouble */
2037 free(rp->null->data);
2038 memset(rp->null, 0, sizeof *rp->null); /* cause trouble */
2045 memset(t, 0, sizeof *t); /* cause trouble */
2051 rp->magic = ~rp->magic;
2052 memset(rp, 0, sizeof *rp); /* cause trouble */