2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: adb.c,v 1.181.2.11.2.26 2006/01/04 23:50:20 marka Exp $ */
21 * Implementation notes
22 * --------------------
24 * In finds, if task == NULL, no events will be generated, and no events
25 * have been sent. If task != NULL but taskaction == NULL, an event has been
26 * posted but not yet freed. If neither are NULL, no event was posted.
31 * After we have cleaned all buckets, dump the database contents.
34 #define DUMP_ADB_AFTER_CLEANING
41 #include <isc/mutexblock.h>
42 #include <isc/netaddr.h>
43 #include <isc/random.h>
44 #include <isc/string.h> /* Required for HP/UX (and others?) */
46 #include <isc/timer.h>
51 #include <dns/events.h>
53 #include <dns/rdata.h>
54 #include <dns/rdataset.h>
55 #include <dns/rdatastruct.h>
56 #include <dns/resolver.h>
57 #include <dns/result.h>
59 #define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
60 #define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
61 #define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
62 #define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
63 #define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
64 #define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
65 #define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
66 #define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
67 #define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
68 #define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
69 #define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
70 #define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
71 #define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
72 #define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
75 * The number of buckets needs to be a prime (for good hashing).
77 * XXXRTH How many buckets do we need?
79 #define NBUCKETS 1009 /* how many buckets for names/addrs */
82 * For type 3 negative cache entries, we will remember that the address is
83 * broken for this long. XXXMLG This is also used for actual addresses, too.
84 * The intent is to keep us from constantly asking about A/AAAA records
85 * if the zone has extremely low TTLs.
87 #define ADB_CACHE_MINIMUM 10 /* seconds */
88 #define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
89 #define ADB_ENTRY_WINDOW 1800 /* seconds */
92 * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
93 * buckets are cleaned in CLEAN_PERIOD seconds.
95 #define CLEAN_PERIOD 3600
96 #define CLEAN_SECONDS 30
97 #define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
99 #define FREE_ITEMS 64 /* free count for memory pools */
100 #define FILL_COUNT 16 /* fill count for memory pools */
102 #define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
104 #define DNS_ADB_MINADBSIZE (1024*1024) /* 1 Megabyte */
106 typedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
107 typedef struct dns_adbnamehook dns_adbnamehook_t;
108 typedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
109 typedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
110 typedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
111 typedef struct dns_adbfetch dns_adbfetch_t;
112 typedef struct dns_adbfetch6 dns_adbfetch6_t;
118 isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
121 isc_timermgr_t *timermgr;
123 isc_taskmgr_t *taskmgr;
125 isc_boolean_t overmem;
127 isc_interval_t tick_interval;
128 int next_cleanbucket;
130 unsigned int irefcnt;
131 unsigned int erefcnt;
134 isc_mempool_t *nmp; /* dns_adbname_t */
135 isc_mempool_t *nhmp; /* dns_adbnamehook_t */
136 isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
137 isc_mempool_t *emp; /* dns_adbentry_t */
138 isc_mempool_t *ahmp; /* dns_adbfind_t */
139 isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
140 isc_mempool_t *afmp; /* dns_adbfetch_t */
143 * Bucketized locks and lists for names.
145 * XXXRTH Have a per-bucket structure that contains all of these?
147 dns_adbnamelist_t names[NBUCKETS];
148 isc_mutex_t namelocks[NBUCKETS];
149 isc_boolean_t name_sd[NBUCKETS];
150 unsigned int name_refcnt[NBUCKETS];
153 * Bucketized locks for entries.
155 * XXXRTH Have a per-bucket structure that contains all of these?
157 dns_adbentrylist_t entries[NBUCKETS];
158 isc_mutex_t entrylocks[NBUCKETS];
159 isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
160 unsigned int entry_refcnt[NBUCKETS];
163 isc_boolean_t cevent_sent;
164 isc_boolean_t shutting_down;
165 isc_eventlist_t whenshutdown;
169 * XXXMLG Document these structures.
176 unsigned int partial_result;
180 isc_stdtime_t expire_target;
181 isc_stdtime_t expire_v4;
182 isc_stdtime_t expire_v6;
184 dns_adbnamehooklist_t v4;
185 dns_adbnamehooklist_t v6;
186 dns_adbfetch_t *fetch_a;
187 dns_adbfetch_t *fetch_aaaa;
188 unsigned int fetch_err;
189 unsigned int fetch6_err;
190 dns_adbfindlist_t finds;
191 ISC_LINK(dns_adbname_t) plink;
194 struct dns_adbfetch {
196 dns_adbnamehook_t *namehook;
197 dns_adbentry_t *entry;
199 dns_rdataset_t rdataset;
205 * This is a small widget that dangles off a dns_adbname_t. It contains a
206 * pointer to the address information about this host, and a link to the next
207 * namehook that will contain the next address this host has.
209 struct dns_adbnamehook {
211 dns_adbentry_t *entry;
212 ISC_LINK(dns_adbnamehook_t) plink;
218 * This is a small widget that holds zone-specific information about an
219 * address. Currently limited to lameness, but could just as easily be
220 * extended to other types of information about zones.
222 struct dns_adbzoneinfo {
226 isc_stdtime_t lame_timer;
228 ISC_LINK(dns_adbzoneinfo_t) plink;
232 * An address entry. It holds quite a bit of information about addresses,
233 * including edns state (in "flags"), rtt, and of course the address of
236 struct dns_adbentry {
244 isc_sockaddr_t sockaddr;
246 isc_stdtime_t expires;
248 * A nonzero 'expires' field indicates that the entry should
249 * persist until that time. This allows entries found
250 * using dns_adb_findaddrinfo() to persist for a limited time
251 * even though they are not necessarily associated with a
255 ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
256 ISC_LINK(dns_adbentry_t) plink;
260 * Internal functions (and prototypes).
262 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
263 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
264 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
266 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
267 static inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
268 static inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
269 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
270 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
271 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
272 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
273 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
275 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
276 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
277 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
278 unsigned int, int *);
279 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
280 isc_sockaddr_t *, int *);
281 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
282 static void print_dns_name(FILE *, dns_name_t *);
283 static void print_namehook_list(FILE *, const char *legend,
284 dns_adbnamehooklist_t *list,
287 static void print_find_list(FILE *, dns_adbname_t *);
288 static void print_fetch_list(FILE *, dns_adbname_t *);
289 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
290 static inline void inc_adb_irefcnt(dns_adb_t *);
291 static inline void inc_adb_erefcnt(dns_adb_t *);
292 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
294 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
296 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
297 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
298 static void clean_target(dns_adb_t *, dns_name_t *);
299 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
301 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
303 static void cancel_fetches_at_name(dns_adbname_t *);
304 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
306 static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
308 static inline void check_exit(dns_adb_t *);
309 static void timer_cleanup(isc_task_t *, isc_event_t *);
310 static void destroy(dns_adb_t *);
311 static isc_boolean_t shutdown_names(dns_adb_t *);
312 static isc_boolean_t shutdown_entries(dns_adb_t *);
313 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
314 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
315 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
316 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
317 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
318 static void water(void *, int);
319 static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
322 * MUST NOT overlap DNS_ADBFIND_* flags!
324 #define FIND_EVENT_SENT 0x40000000
325 #define FIND_EVENT_FREED 0x80000000
326 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
327 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
329 #define NAME_NEEDS_POKE 0x80000000
330 #define NAME_IS_DEAD 0x40000000
331 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
332 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
333 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
334 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
335 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
336 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
337 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
340 * To the name, address classes are all that really exist. If it has a
341 * V6 address it doesn't care if it came from a AAAA query.
343 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
344 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
345 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
348 * Fetches are broken out into A and AAAA types. In some cases,
349 * however, it makes more sense to test for a particular class of fetches,
350 * like V4 or V6 above.
351 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
352 * are now equal to FETCH_V4 and FETCH_V6, respectively.
354 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
355 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
356 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
357 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
358 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
361 * Find options and tests to see if there are addresses on the list.
363 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
364 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
365 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
367 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
369 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
370 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
371 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
372 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
375 * These are currently used on simple unsigned ints, so they are
376 * not really associated with any particular type.
378 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
379 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
381 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
384 * Find out if the flags on a name (nf) indicate if it is a hint or
385 * glue, and compare this to the appropriate bits set in o, to see if
388 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
389 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
390 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
391 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
392 ((o) & DNS_ADBFIND_STARTATZONE))
394 #define ENTER_LEVEL ISC_LOG_DEBUG(50)
395 #define EXIT_LEVEL ENTER_LEVEL
396 #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
397 #define DEF_LEVEL ISC_LOG_DEBUG(5)
398 #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
400 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
401 (r) == DNS_R_NCACHENXRRSET)
402 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
403 (r) == DNS_R_NXRRSET)
404 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
405 (r) == DNS_R_NCACHENXDOMAIN)
406 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
407 (r) == DNS_R_NXRRSET || \
408 (r) == DNS_R_HINTNXRRSET)
411 * Error state rankings.
414 #define FIND_ERR_SUCCESS 0 /* highest rank */
415 #define FIND_ERR_CANCELED 1
416 #define FIND_ERR_FAILURE 2
417 #define FIND_ERR_NXDOMAIN 3
418 #define FIND_ERR_NXRRSET 4
419 #define FIND_ERR_UNEXPECTED 5
420 #define FIND_ERR_NOTFOUND 6
421 #define FIND_ERR_MAX 7
423 static const char *errnames[] = {
433 #define NEWERR(old, new) (ISC_MIN((old), (new)))
435 static isc_result_t find_err_map[FIND_ERR_MAX] = {
442 ISC_R_NOTFOUND /* not YET found */
446 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
449 DP(int level, const char *format, ...) {
452 va_start(args, format);
453 isc_log_vwrite(dns_lctx,
454 DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
455 level, format, args);
459 static inline dns_ttl_t
460 ttlclamp(dns_ttl_t ttl) {
461 if (ttl < ADB_CACHE_MINIMUM)
462 ttl = ADB_CACHE_MINIMUM;
463 if (ttl > ADB_CACHE_MAXIMUM)
464 ttl = ADB_CACHE_MAXIMUM;
470 * Requires the adbname bucket be locked and that no entry buckets be locked.
472 * This code handles A and AAAA rdatasets only.
475 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
480 dns_adbnamehook_t *nh;
481 dns_adbnamehook_t *anh;
482 dns_rdata_t rdata = DNS_RDATA_INIT;
484 struct in6_addr in6a;
485 isc_sockaddr_t sockaddr;
486 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
488 isc_boolean_t new_addresses_added;
489 dns_rdatatype_t rdtype;
490 unsigned int findoptions;
492 INSIST(DNS_ADBNAME_VALID(adbname));
494 INSIST(DNS_ADB_VALID(adb));
496 rdtype = rdataset->type;
497 INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
498 if (rdtype == dns_rdatatype_a)
499 findoptions = DNS_ADBFIND_INET;
501 findoptions = DNS_ADBFIND_INET6;
503 addr_bucket = DNS_ADB_INVALIDBUCKET;
504 new_addresses_added = ISC_FALSE;
507 result = dns_rdataset_first(rdataset);
508 while (result == ISC_R_SUCCESS) {
509 dns_rdata_reset(&rdata);
510 dns_rdataset_current(rdataset, &rdata);
511 if (rdtype == dns_rdatatype_a) {
512 INSIST(rdata.length == 4);
513 memcpy(&ina.s_addr, rdata.data, 4);
514 isc_sockaddr_fromin(&sockaddr, &ina, 0);
516 INSIST(rdata.length == 16);
517 memcpy(in6a.s6_addr, rdata.data, 16);
518 isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
522 nh = new_adbnamehook(adb, NULL);
524 adbname->partial_result |= findoptions;
525 result = ISC_R_NOMEMORY;
529 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
530 if (foundentry == NULL) {
531 dns_adbentry_t *entry;
533 entry = new_adbentry(adb);
535 adbname->partial_result |= findoptions;
536 result = ISC_R_NOMEMORY;
540 entry->sockaddr = sockaddr;
545 link_entry(adb, addr_bucket, entry);
547 for (anh = ISC_LIST_HEAD(adbname->v4);
549 anh = ISC_LIST_NEXT(anh, plink))
550 if (anh->entry == foundentry)
553 foundentry->refcnt++;
554 nh->entry = foundentry;
556 free_adbnamehook(adb, &nh);
559 new_addresses_added = ISC_TRUE;
561 if (rdtype == dns_rdatatype_a)
562 ISC_LIST_APPEND(adbname->v4, nh, plink);
564 ISC_LIST_APPEND(adbname->v6, nh, plink);
567 result = dns_rdataset_next(rdataset);
572 free_adbnamehook(adb, &nh);
574 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
575 UNLOCK(&adb->entrylocks[addr_bucket]);
577 if (rdataset->trust == dns_trust_glue ||
578 rdataset->trust == dns_trust_additional)
579 rdataset->ttl = ADB_CACHE_MINIMUM;
581 rdataset->ttl = ttlclamp(rdataset->ttl);
583 if (rdtype == dns_rdatatype_a) {
584 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
585 adbname->expire_v4, now + rdataset->ttl);
586 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
587 now + rdataset->ttl);
589 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
590 adbname->expire_v6, now + rdataset->ttl);
591 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
592 now + rdataset->ttl);
595 if (new_addresses_added) {
597 * Lie a little here. This is more or less so code that cares
598 * can find out if any new information was added or not.
600 return (ISC_R_SUCCESS);
607 * Requires the name's bucket be locked.
610 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
612 isc_boolean_t result = ISC_FALSE;
613 isc_boolean_t result4, result6;
619 INSIST(DNS_ADBNAME_VALID(name));
621 INSIST(DNS_ADB_VALID(adb));
623 DP(DEF_LEVEL, "killing name %p", name);
626 * If we're dead already, just check to see if we should go
629 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
630 result = unlink_name(adb, name);
631 free_adbname(adb, &name);
633 result = dec_adb_irefcnt(adb);
638 * Clean up the name's various lists. These two are destructive
639 * in that they will always empty the list.
641 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
642 result4 = clean_namehooks(adb, &name->v4);
643 result6 = clean_namehooks(adb, &name->v6);
644 clean_target(adb, &name->target);
645 result = ISC_TF(result4 || result6);
648 * If fetches are running, cancel them. If none are running, we can
649 * just kill the name here.
651 if (!NAME_FETCH(name)) {
652 INSIST(result == ISC_FALSE);
653 result = unlink_name(adb, name);
654 free_adbname(adb, &name);
656 result = dec_adb_irefcnt(adb);
658 name->flags |= NAME_IS_DEAD;
659 cancel_fetches_at_name(name);
665 * Requires the name's bucket be locked and no entry buckets be locked.
668 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
669 isc_boolean_t overmem)
672 isc_boolean_t expire;
673 isc_boolean_t result4 = ISC_FALSE;
674 isc_boolean_t result6 = ISC_FALSE;
676 INSIST(DNS_ADBNAME_VALID(name));
678 INSIST(DNS_ADB_VALID(adb));
683 isc_random_get(&val);
685 expire = ISC_TF((val % 4) == 0);
690 * Check to see if we need to remove the v4 addresses
692 if (!NAME_FETCH_V4(name) &&
693 (expire || EXPIRE_OK(name->expire_v4, now))) {
694 if (NAME_HAS_V4(name)) {
695 DP(DEF_LEVEL, "expiring v4 for name %p", name);
696 result4 = clean_namehooks(adb, &name->v4);
697 name->partial_result &= ~DNS_ADBFIND_INET;
699 name->expire_v4 = INT_MAX;
700 name->fetch_err = FIND_ERR_UNEXPECTED;
704 * Check to see if we need to remove the v6 addresses
706 if (!NAME_FETCH_V6(name) &&
707 (expire || EXPIRE_OK(name->expire_v6, now))) {
708 if (NAME_HAS_V6(name)) {
709 DP(DEF_LEVEL, "expiring v6 for name %p", name);
710 result6 = clean_namehooks(adb, &name->v6);
711 name->partial_result &= ~DNS_ADBFIND_INET6;
713 name->expire_v6 = INT_MAX;
714 name->fetch6_err = FIND_ERR_UNEXPECTED;
718 * Check to see if we need to remove the alias target.
720 if (expire || EXPIRE_OK(name->expire_target, now)) {
721 clean_target(adb, &name->target);
722 name->expire_target = INT_MAX;
724 return (ISC_TF(result4 || result6));
728 * Requires the name's bucket be locked.
731 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
732 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
734 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
735 name->lock_bucket = bucket;
736 adb->name_refcnt[bucket]++;
740 * Requires the name's bucket be locked.
742 static inline isc_boolean_t
743 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
745 isc_boolean_t result = ISC_FALSE;
747 bucket = name->lock_bucket;
748 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
750 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
751 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
752 INSIST(adb->name_refcnt[bucket] > 0);
753 adb->name_refcnt[bucket]--;
754 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
760 * Requires the entry's bucket be locked.
763 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
764 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
765 entry->lock_bucket = bucket;
766 adb->entry_refcnt[bucket]++;
770 * Requires the entry's bucket be locked.
772 static inline isc_boolean_t
773 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
775 isc_boolean_t result = ISC_FALSE;
777 bucket = entry->lock_bucket;
778 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
780 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
781 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
782 INSIST(adb->entry_refcnt[bucket] > 0);
783 adb->entry_refcnt[bucket]--;
784 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
790 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
791 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
799 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
800 * checked after calling this function.
803 shutdown_names(dns_adb_t *adb) {
805 isc_boolean_t result = ISC_FALSE;
807 dns_adbname_t *next_name;
809 for (bucket = 0; bucket < NBUCKETS; bucket++) {
810 LOCK(&adb->namelocks[bucket]);
811 adb->name_sd[bucket] = ISC_TRUE;
813 name = ISC_LIST_HEAD(adb->names[bucket]);
816 * This bucket has no names. We must decrement the
817 * irefcnt ourselves, since it will not be
818 * automatically triggered by a name being unlinked.
820 INSIST(result == ISC_FALSE);
821 result = dec_adb_irefcnt(adb);
824 * Run through the list. For each name, clean up finds
825 * found there, and cancel any fetches running. When
826 * all the fetches are canceled, the name will destroy
829 while (name != NULL) {
830 next_name = ISC_LIST_NEXT(name, plink);
831 INSIST(result == ISC_FALSE);
832 result = kill_name(&name,
833 DNS_EVENT_ADBSHUTDOWN);
838 UNLOCK(&adb->namelocks[bucket]);
844 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
845 * checked after calling this function.
848 shutdown_entries(dns_adb_t *adb) {
850 isc_boolean_t result = ISC_FALSE;
851 dns_adbentry_t *entry;
852 dns_adbentry_t *next_entry;
854 for (bucket = 0; bucket < NBUCKETS; bucket++) {
855 LOCK(&adb->entrylocks[bucket]);
856 adb->entry_sd[bucket] = ISC_TRUE;
858 entry = ISC_LIST_HEAD(adb->entries[bucket]);
861 * This bucket has no entries. We must decrement the
862 * irefcnt ourselves, since it will not be
863 * automatically triggered by an entry being unlinked.
865 result = dec_adb_irefcnt(adb);
868 * Run through the list. Cleanup any entries not
869 * associated with names, and which are not in use.
871 while (entry != NULL) {
872 next_entry = ISC_LIST_NEXT(entry, plink);
873 if (entry->refcnt == 0 &&
874 entry->expires != 0) {
875 result = unlink_entry(adb, entry);
876 free_adbentry(adb, &entry);
878 result = dec_adb_irefcnt(adb);
884 UNLOCK(&adb->entrylocks[bucket]);
890 * Name bucket must be locked
893 cancel_fetches_at_name(dns_adbname_t *name) {
894 if (NAME_FETCH_A(name))
895 dns_resolver_cancelfetch(name->fetch_a->fetch);
897 if (NAME_FETCH_AAAA(name))
898 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
902 * Assumes the name bucket is locked.
905 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
906 dns_adbentry_t *entry;
907 dns_adbnamehook_t *namehook;
909 isc_boolean_t result = ISC_FALSE;
911 addr_bucket = DNS_ADB_INVALIDBUCKET;
912 namehook = ISC_LIST_HEAD(*namehooks);
913 while (namehook != NULL) {
914 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
917 * Clean up the entry if needed.
919 entry = namehook->entry;
921 INSIST(DNS_ADBENTRY_VALID(entry));
923 if (addr_bucket != entry->lock_bucket) {
924 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
925 UNLOCK(&adb->entrylocks[addr_bucket]);
926 addr_bucket = entry->lock_bucket;
927 LOCK(&adb->entrylocks[addr_bucket]);
930 result = dec_entry_refcnt(adb, entry, ISC_FALSE);
936 namehook->entry = NULL;
937 ISC_LIST_UNLINK(*namehooks, namehook, plink);
938 free_adbnamehook(adb, &namehook);
940 namehook = ISC_LIST_HEAD(*namehooks);
943 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
944 UNLOCK(&adb->entrylocks[addr_bucket]);
949 clean_target(dns_adb_t *adb, dns_name_t *target) {
950 if (dns_name_countlabels(target) > 0) {
951 dns_name_free(target, adb->mctx);
952 dns_name_init(target, NULL);
957 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
958 dns_rdataset_t *rdataset, dns_name_t *target)
961 dns_namereln_t namereln;
962 unsigned int nlabels;
964 dns_rdata_t rdata = DNS_RDATA_INIT;
965 dns_fixedname_t fixed1, fixed2;
966 dns_name_t *prefix, *new_target;
968 REQUIRE(dns_name_countlabels(target) == 0);
970 if (rdataset->type == dns_rdatatype_cname) {
971 dns_rdata_cname_t cname;
974 * Copy the CNAME's target into the target name.
976 result = dns_rdataset_first(rdataset);
977 if (result != ISC_R_SUCCESS)
979 dns_rdataset_current(rdataset, &rdata);
980 result = dns_rdata_tostruct(&rdata, &cname, NULL);
981 if (result != ISC_R_SUCCESS)
983 result = dns_name_dup(&cname.cname, adb->mctx, target);
984 dns_rdata_freestruct(&cname);
985 if (result != ISC_R_SUCCESS)
988 dns_rdata_dname_t dname;
990 INSIST(rdataset->type == dns_rdatatype_dname);
991 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
992 INSIST(namereln == dns_namereln_subdomain);
994 * Get the target name of the DNAME.
996 result = dns_rdataset_first(rdataset);
997 if (result != ISC_R_SUCCESS)
999 dns_rdataset_current(rdataset, &rdata);
1000 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1001 if (result != ISC_R_SUCCESS)
1004 * Construct the new target name.
1006 dns_fixedname_init(&fixed1);
1007 prefix = dns_fixedname_name(&fixed1);
1008 dns_fixedname_init(&fixed2);
1009 new_target = dns_fixedname_name(&fixed2);
1010 dns_name_split(name, nlabels, prefix, NULL);
1011 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1013 dns_rdata_freestruct(&dname);
1014 if (result != ISC_R_SUCCESS)
1016 result = dns_name_dup(new_target, adb->mctx, target);
1017 if (result != ISC_R_SUCCESS)
1021 return (ISC_R_SUCCESS);
1025 * Assumes nothing is locked, since this is called by the client.
1028 event_free(isc_event_t *event) {
1029 dns_adbfind_t *find;
1031 INSIST(event != NULL);
1032 find = event->ev_destroy_arg;
1033 INSIST(DNS_ADBFIND_VALID(find));
1036 find->flags |= FIND_EVENT_FREED;
1037 event->ev_destroy_arg = NULL;
1038 UNLOCK(&find->lock);
1042 * Assumes the name bucket is locked.
1045 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1050 dns_adbfind_t *find;
1051 dns_adbfind_t *next_find;
1052 isc_boolean_t process;
1053 unsigned int wanted, notify;
1056 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1057 name, evtype, addrs);
1059 find = ISC_LIST_HEAD(name->finds);
1060 while (find != NULL) {
1062 next_find = ISC_LIST_NEXT(find, plink);
1064 process = ISC_FALSE;
1065 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1066 notify = wanted & addrs;
1069 case DNS_EVENT_ADBMOREADDRESSES:
1070 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1071 if ((notify) != 0) {
1072 find->flags &= ~addrs;
1076 case DNS_EVENT_ADBNOMOREADDRESSES:
1077 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1078 find->flags &= ~addrs;
1079 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1084 find->flags &= ~addrs;
1089 DP(DEF_LEVEL, "cfan: processing find %p", find);
1091 * Unlink the find from the name, letting the caller
1092 * call dns_adb_destroyfind() on it to clean it up
1095 ISC_LIST_UNLINK(name->finds, find, plink);
1096 find->adbname = NULL;
1097 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1099 INSIST(!FIND_EVENTSENT(find));
1102 task = ev->ev_sender;
1103 ev->ev_sender = find;
1104 find->result_v4 = find_err_map[name->fetch_err];
1105 find->result_v6 = find_err_map[name->fetch6_err];
1106 ev->ev_type = evtype;
1107 ev->ev_destroy = event_free;
1108 ev->ev_destroy_arg = find;
1111 "sending event %p to task %p for find %p",
1114 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1116 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1119 UNLOCK(&find->lock);
1123 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1127 check_exit(dns_adb_t *adb) {
1130 * The caller must be holding the adb lock.
1132 if (adb->shutting_down) {
1134 * If there aren't any external references either, we're
1135 * done. Send the control event to initiate shutdown.
1137 INSIST(!adb->cevent_sent); /* Sanity check. */
1138 event = &adb->cevent;
1139 isc_task_send(adb->task, &event);
1140 adb->cevent_sent = ISC_TRUE;
1144 static inline isc_boolean_t
1145 dec_adb_irefcnt(dns_adb_t *adb) {
1148 isc_boolean_t result = ISC_FALSE;
1150 LOCK(&adb->reflock);
1152 INSIST(adb->irefcnt > 0);
1155 if (adb->irefcnt == 0) {
1156 event = ISC_LIST_HEAD(adb->whenshutdown);
1157 while (event != NULL) {
1158 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1159 etask = event->ev_sender;
1160 event->ev_sender = adb;
1161 isc_task_sendanddetach(&etask, &event);
1162 event = ISC_LIST_HEAD(adb->whenshutdown);
1166 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1168 UNLOCK(&adb->reflock);
1173 inc_adb_irefcnt(dns_adb_t *adb) {
1174 LOCK(&adb->reflock);
1176 UNLOCK(&adb->reflock);
1180 inc_adb_erefcnt(dns_adb_t *adb) {
1181 LOCK(&adb->reflock);
1183 UNLOCK(&adb->reflock);
1187 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1190 bucket = entry->lock_bucket;
1193 LOCK(&adb->entrylocks[bucket]);
1198 UNLOCK(&adb->entrylocks[bucket]);
1201 static inline isc_boolean_t
1202 dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1204 isc_boolean_t destroy_entry;
1205 isc_boolean_t result = ISC_FALSE;
1207 bucket = entry->lock_bucket;
1210 LOCK(&adb->entrylocks[bucket]);
1212 INSIST(entry->refcnt > 0);
1215 destroy_entry = ISC_FALSE;
1216 if (entry->refcnt == 0 &&
1217 (adb->entry_sd[bucket] || entry->expires == 0)) {
1218 destroy_entry = ISC_TRUE;
1219 result = unlink_entry(adb, entry);
1223 UNLOCK(&adb->entrylocks[bucket]);
1228 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1230 free_adbentry(adb, &entry);
1232 result =dec_adb_irefcnt(adb);
1237 static inline dns_adbname_t *
1238 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1239 dns_adbname_t *name;
1241 name = isc_mempool_get(adb->nmp);
1245 dns_name_init(&name->name, NULL);
1246 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1247 isc_mempool_put(adb->nmp, name);
1250 dns_name_init(&name->target, NULL);
1251 name->magic = DNS_ADBNAME_MAGIC;
1253 name->partial_result = 0;
1255 name->expire_v4 = INT_MAX;
1256 name->expire_v6 = INT_MAX;
1257 name->expire_target = INT_MAX;
1259 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1260 ISC_LIST_INIT(name->v4);
1261 ISC_LIST_INIT(name->v6);
1262 name->fetch_a = NULL;
1263 name->fetch_aaaa = NULL;
1264 name->fetch_err = FIND_ERR_UNEXPECTED;
1265 name->fetch6_err = FIND_ERR_UNEXPECTED;
1266 ISC_LIST_INIT(name->finds);
1267 ISC_LINK_INIT(name, plink);
1273 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1276 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1280 INSIST(!NAME_HAS_V4(n));
1281 INSIST(!NAME_HAS_V6(n));
1282 INSIST(!NAME_FETCH(n));
1283 INSIST(ISC_LIST_EMPTY(n->finds));
1284 INSIST(!ISC_LINK_LINKED(n, plink));
1285 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1286 INSIST(n->adb == adb);
1289 dns_name_free(&n->name, adb->mctx);
1291 isc_mempool_put(adb->nmp, n);
1294 static inline dns_adbnamehook_t *
1295 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1296 dns_adbnamehook_t *nh;
1298 nh = isc_mempool_get(adb->nhmp);
1302 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1304 ISC_LINK_INIT(nh, plink);
1310 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1311 dns_adbnamehook_t *nh;
1313 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1317 INSIST(nh->entry == NULL);
1318 INSIST(!ISC_LINK_LINKED(nh, plink));
1321 isc_mempool_put(adb->nhmp, nh);
1324 static inline dns_adbzoneinfo_t *
1325 new_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
1326 dns_adbzoneinfo_t *zi;
1328 zi = isc_mempool_get(adb->zimp);
1332 dns_name_init(&zi->zone, NULL);
1333 if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
1334 isc_mempool_put(adb->zimp, zi);
1338 zi->magic = DNS_ADBZONEINFO_MAGIC;
1340 ISC_LINK_INIT(zi, plink);
1346 free_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
1347 dns_adbzoneinfo_t *zi;
1349 INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
1353 INSIST(!ISC_LINK_LINKED(zi, plink));
1355 dns_name_free(&zi->zone, adb->mctx);
1359 isc_mempool_put(adb->zimp, zi);
1362 static inline dns_adbentry_t *
1363 new_adbentry(dns_adb_t *adb) {
1367 e = isc_mempool_get(adb->emp);
1371 e->magic = DNS_ADBENTRY_MAGIC;
1372 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1376 e->srtt = (r & 0x1f) + 1;
1378 ISC_LIST_INIT(e->zoneinfo);
1379 ISC_LINK_INIT(e, plink);
1385 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1387 dns_adbzoneinfo_t *zi;
1389 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1393 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1394 INSIST(e->refcnt == 0);
1395 INSIST(!ISC_LINK_LINKED(e, plink));
1399 zi = ISC_LIST_HEAD(e->zoneinfo);
1400 while (zi != NULL) {
1401 ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
1402 free_adbzoneinfo(adb, &zi);
1403 zi = ISC_LIST_HEAD(e->zoneinfo);
1406 isc_mempool_put(adb->emp, e);
1409 static inline dns_adbfind_t *
1410 new_adbfind(dns_adb_t *adb) {
1412 isc_result_t result;
1414 h = isc_mempool_get(adb->ahmp);
1423 h->partial_result = 0;
1426 h->result_v4 = ISC_R_UNEXPECTED;
1427 h->result_v6 = ISC_R_UNEXPECTED;
1428 ISC_LINK_INIT(h, publink);
1429 ISC_LINK_INIT(h, plink);
1430 ISC_LIST_INIT(h->list);
1432 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1437 result = isc_mutex_init(&h->lock);
1438 if (result != ISC_R_SUCCESS) {
1439 UNEXPECTED_ERROR(__FILE__, __LINE__,
1440 "isc_mutex_init failed in new_adbfind()");
1441 isc_mempool_put(adb->ahmp, h);
1445 ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1448 inc_adb_irefcnt(adb);
1449 h->magic = DNS_ADBFIND_MAGIC;
1453 static inline dns_adbfetch_t *
1454 new_adbfetch(dns_adb_t *adb) {
1457 f = isc_mempool_get(adb->afmp);
1466 f->namehook = new_adbnamehook(adb, NULL);
1467 if (f->namehook == NULL)
1470 f->entry = new_adbentry(adb);
1471 if (f->entry == NULL)
1474 dns_rdataset_init(&f->rdataset);
1476 f->magic = DNS_ADBFETCH_MAGIC;
1481 if (f->namehook != NULL)
1482 free_adbnamehook(adb, &f->namehook);
1483 if (f->entry != NULL)
1484 free_adbentry(adb, &f->entry);
1485 isc_mempool_put(adb->afmp, f);
1490 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1493 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1499 if (f->namehook != NULL)
1500 free_adbnamehook(adb, &f->namehook);
1501 if (f->entry != NULL)
1502 free_adbentry(adb, &f->entry);
1504 if (dns_rdataset_isassociated(&f->rdataset))
1505 dns_rdataset_disassociate(&f->rdataset);
1507 isc_mempool_put(adb->afmp, f);
1510 static inline isc_boolean_t
1511 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1512 dns_adbfind_t *find;
1514 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1518 INSIST(!FIND_HAS_ADDRS(find));
1519 INSIST(!ISC_LINK_LINKED(find, publink));
1520 INSIST(!ISC_LINK_LINKED(find, plink));
1521 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1522 INSIST(find->adbname == NULL);
1526 DESTROYLOCK(&find->lock);
1527 isc_mempool_put(adb->ahmp, find);
1528 return (dec_adb_irefcnt(adb));
1532 * Copy bits from the entry into the newly allocated addrinfo. The entry
1533 * must be locked, and the reference count must be bumped up by one
1534 * if this function returns a valid pointer.
1536 static inline dns_adbaddrinfo_t *
1537 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1538 dns_adbaddrinfo_t *ai;
1540 ai = isc_mempool_get(adb->aimp);
1544 ai->magic = DNS_ADBADDRINFO_MAGIC;
1545 ai->sockaddr = entry->sockaddr;
1546 isc_sockaddr_setport(&ai->sockaddr, port);
1547 ai->srtt = entry->srtt;
1548 ai->flags = entry->flags;
1550 ISC_LINK_INIT(ai, publink);
1556 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1557 dns_adbaddrinfo_t *ai;
1559 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1563 INSIST(ai->entry == NULL);
1564 INSIST(!ISC_LINK_LINKED(ai, publink));
1568 isc_mempool_put(adb->aimp, ai);
1572 * Search for the name. NOTE: The bucket is kept locked on both
1573 * success and failure, so it must always be unlocked by the caller!
1575 * On the first call to this function, *bucketp must be set to
1576 * DNS_ADB_INVALIDBUCKET.
1578 static inline dns_adbname_t *
1579 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1580 unsigned int options, int *bucketp)
1582 dns_adbname_t *adbname;
1585 bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1587 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1588 LOCK(&adb->namelocks[bucket]);
1590 } else if (*bucketp != bucket) {
1591 UNLOCK(&adb->namelocks[*bucketp]);
1592 LOCK(&adb->namelocks[bucket]);
1596 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1597 while (adbname != NULL) {
1598 if (!NAME_DEAD(adbname)) {
1599 if (dns_name_equal(name, &adbname->name)
1600 && GLUEHINT_OK(adbname, options)
1601 && STARTATZONE_MATCHES(adbname, options))
1604 adbname = ISC_LIST_NEXT(adbname, plink);
1611 * Search for the address. NOTE: The bucket is kept locked on both
1612 * success and failure, so it must always be unlocked by the caller.
1614 * On the first call to this function, *bucketp must be set to
1615 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1616 * later calls (within the same "lock path") it can be left alone, so
1617 * if this function is called multiple times locking is only done if
1618 * the bucket changes.
1620 static inline dns_adbentry_t *
1621 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
1622 dns_adbentry_t *entry;
1625 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1627 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1628 LOCK(&adb->entrylocks[bucket]);
1630 } else if (*bucketp != bucket) {
1631 UNLOCK(&adb->entrylocks[*bucketp]);
1632 LOCK(&adb->entrylocks[bucket]);
1636 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1637 while (entry != NULL) {
1638 if (isc_sockaddr_equal(addr, &entry->sockaddr))
1640 entry = ISC_LIST_NEXT(entry, plink);
1647 * Entry bucket MUST be locked!
1649 static isc_boolean_t
1650 entry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
1653 dns_adbzoneinfo_t *zi, *next_zi;
1654 isc_boolean_t is_bad;
1658 zi = ISC_LIST_HEAD(entry->zoneinfo);
1661 while (zi != NULL) {
1662 next_zi = ISC_LIST_NEXT(zi, plink);
1665 * Has the entry expired?
1667 if (zi->lame_timer < now) {
1668 ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
1669 free_adbzoneinfo(adb, &zi);
1673 * Order tests from least to most expensive.
1675 if (zi != NULL && !is_bad) {
1676 if (dns_name_equal(zone, &zi->zone))
1687 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
1688 dns_adbname_t *name, isc_stdtime_t now)
1690 dns_adbnamehook_t *namehook;
1691 dns_adbaddrinfo_t *addrinfo;
1692 dns_adbentry_t *entry;
1695 bucket = DNS_ADB_INVALIDBUCKET;
1697 if (find->options & DNS_ADBFIND_INET) {
1698 namehook = ISC_LIST_HEAD(name->v4);
1699 while (namehook != NULL) {
1700 entry = namehook->entry;
1701 bucket = entry->lock_bucket;
1702 LOCK(&adb->entrylocks[bucket]);
1704 if (!FIND_RETURNLAME(find)
1705 && entry_is_bad_for_zone(adb, entry, zone, now)) {
1706 find->options |= DNS_ADBFIND_LAMEPRUNED;
1709 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1710 if (addrinfo == NULL) {
1711 find->partial_result |= DNS_ADBFIND_INET;
1715 * Found a valid entry. Add it to the find's list.
1717 inc_entry_refcnt(adb, entry, ISC_FALSE);
1718 ISC_LIST_APPEND(find->list, addrinfo, publink);
1721 UNLOCK(&adb->entrylocks[bucket]);
1722 bucket = DNS_ADB_INVALIDBUCKET;
1723 namehook = ISC_LIST_NEXT(namehook, plink);
1727 if (find->options & DNS_ADBFIND_INET6) {
1728 namehook = ISC_LIST_HEAD(name->v6);
1729 while (namehook != NULL) {
1730 entry = namehook->entry;
1731 bucket = entry->lock_bucket;
1732 LOCK(&adb->entrylocks[bucket]);
1734 if (entry_is_bad_for_zone(adb, entry, zone, now))
1736 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1737 if (addrinfo == NULL) {
1738 find->partial_result |= DNS_ADBFIND_INET6;
1742 * Found a valid entry. Add it to the find's list.
1744 inc_entry_refcnt(adb, entry, ISC_FALSE);
1745 ISC_LIST_APPEND(find->list, addrinfo, publink);
1748 UNLOCK(&adb->entrylocks[bucket]);
1749 bucket = DNS_ADB_INVALIDBUCKET;
1750 namehook = ISC_LIST_NEXT(namehook, plink);
1755 if (bucket != DNS_ADB_INVALIDBUCKET)
1756 UNLOCK(&adb->entrylocks[bucket]);
1760 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1766 INSIST(DNS_ADB_VALID(adb));
1769 * Kill the timer, and then the ADB itself. Note that this implies
1770 * that this task was the one scheduled to get timer events. If
1771 * this is not true (and it is unfortunate there is no way to INSIST()
1772 * this) badness will occur.
1775 isc_timer_detach(&adb->timer);
1777 isc_event_free(&ev);
1782 * Name bucket must be locked; adb may be locked; no other locks held.
1784 static isc_boolean_t
1785 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1786 dns_adbname_t *name;
1787 isc_boolean_t result = ISC_FALSE;
1789 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1792 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1794 if (NAME_FETCH(name))
1796 if (!EXPIRE_OK(name->expire_v4, now))
1798 if (!EXPIRE_OK(name->expire_v6, now))
1800 if (!EXPIRE_OK(name->expire_target, now))
1804 * The name is empty. Delete it.
1806 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1810 * Our caller, or one of its callers, will be calling check_exit() at
1811 * some point, so we don't need to do it here.
1817 * Entry bucket must be locked; adb may be locked; no other locks held.
1819 static isc_boolean_t
1820 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1822 dns_adbentry_t *entry;
1823 isc_boolean_t expire;
1824 isc_boolean_t result = ISC_FALSE;
1826 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1829 if (entry->refcnt != 0)
1835 isc_random_get(&val);
1837 expire = ISC_TF((val % 4) == 0);
1841 if (entry->expires == 0 || (! expire && entry->expires > now))
1845 * The entry is not in use. Delete it.
1847 DP(DEF_LEVEL, "killing entry %p", entry);
1848 INSIST(ISC_LINK_LINKED(entry, plink));
1849 result = unlink_entry(adb, entry);
1850 free_adbentry(adb, &entry);
1852 dec_adb_irefcnt(adb);
1858 * ADB must be locked, and no other locks held.
1860 static isc_boolean_t
1861 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1862 dns_adbname_t *name;
1863 dns_adbname_t *next_name;
1864 isc_boolean_t result = ISC_FALSE;
1866 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1868 LOCK(&adb->namelocks[bucket]);
1869 if (adb->name_sd[bucket]) {
1870 UNLOCK(&adb->namelocks[bucket]);
1874 name = ISC_LIST_HEAD(adb->names[bucket]);
1875 while (name != NULL) {
1876 next_name = ISC_LIST_NEXT(name, plink);
1877 INSIST(result == ISC_FALSE);
1878 result = check_expire_namehooks(name, now, adb->overmem);
1880 result = check_expire_name(&name, now);
1883 UNLOCK(&adb->namelocks[bucket]);
1888 * ADB must be locked, and no other locks held.
1890 static isc_boolean_t
1891 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1892 dns_adbentry_t *entry, *next_entry;
1893 isc_boolean_t result = ISC_FALSE;
1895 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1897 LOCK(&adb->entrylocks[bucket]);
1898 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1899 while (entry != NULL) {
1900 next_entry = ISC_LIST_NEXT(entry, plink);
1901 INSIST(result == ISC_FALSE);
1902 result = check_expire_entry(adb, &entry, now);
1905 UNLOCK(&adb->entrylocks[bucket]);
1910 timer_cleanup(isc_task_t *task, isc_event_t *ev) {
1914 isc_interval_t interval;
1919 INSIST(DNS_ADB_VALID(adb));
1923 isc_stdtime_get(&now);
1925 for (i = 0; i < CLEAN_BUCKETS; i++) {
1927 * Call our cleanup routines.
1929 RUNTIME_CHECK(cleanup_names(adb, adb->next_cleanbucket, now) ==
1931 RUNTIME_CHECK(cleanup_entries(adb, adb->next_cleanbucket, now)
1935 * Set the next bucket to be cleaned.
1937 adb->next_cleanbucket++;
1938 if (adb->next_cleanbucket >= NBUCKETS) {
1939 adb->next_cleanbucket = 0;
1940 #ifdef DUMP_ADB_AFTER_CLEANING
1941 dump_adb(adb, stdout, ISC_TRUE, now);
1948 * XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
1949 * ISC_R_NOMEMORY, but it isn't clear what could be done here
1950 * if either one of those things happened.
1952 interval = adb->tick_interval;
1954 isc_interval_set(&interval, 0, 1);
1955 (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
1956 &interval, ISC_FALSE);
1960 isc_event_free(&ev);
1964 destroy(dns_adb_t *adb) {
1968 * The timer is already dead, from the task's shutdown callback.
1970 isc_task_detach(&adb->task);
1972 isc_mempool_destroy(&adb->nmp);
1973 isc_mempool_destroy(&adb->nhmp);
1974 isc_mempool_destroy(&adb->zimp);
1975 isc_mempool_destroy(&adb->emp);
1976 isc_mempool_destroy(&adb->ahmp);
1977 isc_mempool_destroy(&adb->aimp);
1978 isc_mempool_destroy(&adb->afmp);
1980 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1981 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1983 DESTROYLOCK(&adb->reflock);
1984 DESTROYLOCK(&adb->lock);
1985 DESTROYLOCK(&adb->mplock);
1987 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
1996 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
1997 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2000 isc_result_t result;
2003 REQUIRE(mem != NULL);
2004 REQUIRE(view != NULL);
2005 REQUIRE(timermgr != NULL);
2006 REQUIRE(taskmgr != NULL);
2007 REQUIRE(newadb != NULL && *newadb == NULL);
2009 adb = isc_mem_get(mem, sizeof(dns_adb_t));
2011 return (ISC_R_NOMEMORY);
2014 * Initialize things here that cannot fail, and especially things
2015 * that must be NULL for the error return to work properly.
2031 adb->timermgr = timermgr;
2032 adb->taskmgr = taskmgr;
2033 adb->next_cleanbucket = 0;
2034 ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2035 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2037 adb->cevent_sent = ISC_FALSE;
2038 adb->shutting_down = ISC_FALSE;
2039 adb->overmem = ISC_FALSE;
2040 ISC_LIST_INIT(adb->whenshutdown);
2042 isc_mem_attach(mem, &adb->mctx);
2044 result = isc_mutex_init(&adb->lock);
2045 if (result != ISC_R_SUCCESS)
2048 result = isc_mutex_init(&adb->mplock);
2049 if (result != ISC_R_SUCCESS)
2052 result = isc_mutex_init(&adb->reflock);
2053 if (result != ISC_R_SUCCESS)
2057 * Initialize the bucket locks for names and elements.
2058 * May as well initialize the list heads, too.
2060 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2061 if (result != ISC_R_SUCCESS)
2063 for (i = 0; i < NBUCKETS; i++) {
2064 ISC_LIST_INIT(adb->names[i]);
2065 adb->name_sd[i] = ISC_FALSE;
2066 adb->name_refcnt[i] = 0;
2069 for (i = 0; i < NBUCKETS; i++) {
2070 ISC_LIST_INIT(adb->entries[i]);
2071 adb->entry_sd[i] = ISC_FALSE;
2072 adb->entry_refcnt[i] = 0;
2075 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2076 if (result != ISC_R_SUCCESS)
2082 #define MPINIT(t, p, n) do { \
2083 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2084 if (result != ISC_R_SUCCESS) \
2086 isc_mempool_setfreemax((p), FREE_ITEMS); \
2087 isc_mempool_setfillcount((p), FILL_COUNT); \
2088 isc_mempool_setname((p), n); \
2089 isc_mempool_associatelock((p), &adb->mplock); \
2092 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2093 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2094 MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
2095 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2096 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2097 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2098 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2103 * Allocate a timer and a task for our periodic cleanup.
2105 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2106 if (result != ISC_R_SUCCESS)
2108 isc_task_setname(adb->task, "ADB", adb);
2110 * XXXMLG When this is changed to be a config file option,
2112 isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
2113 result = isc_timer_create(adb->timermgr, isc_timertype_once,
2114 NULL, &adb->tick_interval, adb->task,
2115 timer_cleanup, adb, &adb->timer);
2116 if (result != ISC_R_SUCCESS)
2119 DP(ISC_LOG_DEBUG(5), "cleaning interval for adb: "
2120 "%u buckets every %u seconds, %u buckets in system, %u cl.interval",
2121 CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
2126 adb->magic = DNS_ADB_MAGIC;
2128 return (ISC_R_SUCCESS);
2131 if (adb->task != NULL)
2132 isc_task_detach(&adb->task);
2133 if (adb->timer != NULL)
2134 isc_timer_detach(&adb->timer);
2136 /* clean up entrylocks */
2137 DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2139 fail2: /* clean up namelocks */
2140 DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2142 fail1: /* clean up only allocated memory */
2143 if (adb->nmp != NULL)
2144 isc_mempool_destroy(&adb->nmp);
2145 if (adb->nhmp != NULL)
2146 isc_mempool_destroy(&adb->nhmp);
2147 if (adb->zimp != NULL)
2148 isc_mempool_destroy(&adb->zimp);
2149 if (adb->emp != NULL)
2150 isc_mempool_destroy(&adb->emp);
2151 if (adb->ahmp != NULL)
2152 isc_mempool_destroy(&adb->ahmp);
2153 if (adb->aimp != NULL)
2154 isc_mempool_destroy(&adb->aimp);
2155 if (adb->afmp != NULL)
2156 isc_mempool_destroy(&adb->afmp);
2158 DESTROYLOCK(&adb->reflock);
2160 DESTROYLOCK(&adb->mplock);
2162 DESTROYLOCK(&adb->lock);
2164 isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2170 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2172 REQUIRE(DNS_ADB_VALID(adb));
2173 REQUIRE(adbx != NULL && *adbx == NULL);
2175 inc_adb_erefcnt(adb);
2180 dns_adb_detach(dns_adb_t **adbx) {
2182 isc_boolean_t need_exit_check;
2184 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2189 INSIST(adb->erefcnt > 0);
2191 LOCK(&adb->reflock);
2193 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2194 UNLOCK(&adb->reflock);
2196 if (need_exit_check) {
2198 INSIST(adb->shutting_down);
2205 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2208 isc_boolean_t zeroirefcnt = ISC_FALSE;
2211 * Send '*eventp' to 'task' when 'adb' has shutdown.
2214 REQUIRE(DNS_ADB_VALID(adb));
2215 REQUIRE(eventp != NULL);
2222 LOCK(&adb->reflock);
2223 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2225 if (adb->shutting_down && zeroirefcnt &&
2226 isc_mempool_getallocated(adb->ahmp) == 0) {
2228 * We're already shutdown. Send the event.
2230 event->ev_sender = adb;
2231 isc_task_send(task, &event);
2234 isc_task_attach(task, &clone);
2235 event->ev_sender = clone;
2236 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2239 UNLOCK(&adb->reflock);
2244 dns_adb_shutdown(dns_adb_t *adb) {
2245 isc_boolean_t need_check_exit;
2253 if (!adb->shutting_down) {
2254 adb->shutting_down = ISC_TRUE;
2255 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2256 need_check_exit = shutdown_names(adb);
2257 if (!need_check_exit)
2258 need_check_exit = shutdown_entries(adb);
2259 if (need_check_exit)
2267 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2268 void *arg, dns_name_t *name, dns_name_t *zone,
2269 unsigned int options, isc_stdtime_t now, dns_name_t *target,
2270 in_port_t port, dns_adbfind_t **findp)
2272 dns_adbfind_t *find;
2273 dns_adbname_t *adbname;
2275 isc_boolean_t want_event, start_at_zone, alias, have_address;
2276 isc_result_t result;
2277 unsigned int wanted_addresses;
2278 unsigned int wanted_fetches;
2279 unsigned int query_pending;
2281 REQUIRE(DNS_ADB_VALID(adb));
2283 REQUIRE(action != NULL);
2285 REQUIRE(name != NULL);
2286 REQUIRE(zone != NULL);
2287 REQUIRE(findp != NULL && *findp == NULL);
2288 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2290 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2292 result = ISC_R_UNEXPECTED;
2293 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2296 want_event = ISC_FALSE;
2297 start_at_zone = ISC_FALSE;
2301 isc_stdtime_get(&now);
2304 * XXXMLG Move this comment somewhere else!
2306 * Look up the name in our internal database.
2308 * Possibilities: Note that these are not always exclusive.
2310 * No name found. In this case, allocate a new name header and
2311 * an initial namehook or two. If any of these allocations
2312 * fail, clean up and return ISC_R_NOMEMORY.
2314 * Name found, valid addresses present. Allocate one addrinfo
2315 * structure for each found and append it to the linked list
2316 * of addresses for this header.
2318 * Name found, queries pending. In this case, if a task was
2319 * passed in, allocate a job id, attach it to the name's job
2320 * list and remember to tell the caller that there will be
2321 * more info coming later.
2324 find = new_adbfind(adb);
2326 return (ISC_R_NOMEMORY);
2331 * Remember what types of addresses we are interested in.
2333 find->options = options;
2334 find->flags |= wanted_addresses;
2335 if (FIND_WANTEVENT(find)) {
2336 REQUIRE(task != NULL);
2340 * Try to see if we know anything about this name at all.
2342 bucket = DNS_ADB_INVALIDBUCKET;
2343 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2344 if (adb->name_sd[bucket]) {
2346 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2347 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2348 result = ISC_R_SHUTTINGDOWN;
2353 * Nothing found. Allocate a new adbname structure for this name.
2355 if (adbname == NULL) {
2356 adbname = new_adbname(adb, name);
2357 if (adbname == NULL) {
2358 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2359 result = ISC_R_NOMEMORY;
2362 link_name(adb, bucket, adbname);
2363 if (FIND_HINTOK(find))
2364 adbname->flags |= NAME_HINT_OK;
2365 if (FIND_GLUEOK(find))
2366 adbname->flags |= NAME_GLUE_OK;
2367 if (FIND_STARTATZONE(find))
2368 adbname->flags |= NAME_STARTATZONE;
2372 * Expire old entries, etc.
2374 RUNTIME_CHECK(check_expire_namehooks(adbname, now, adb->overmem) ==
2378 * Do we know that the name is an alias?
2380 if (!EXPIRE_OK(adbname->expire_target, now)) {
2385 "dns_adb_createfind: name %p is an alias (cached)",
2392 * Try to populate the name from the database and/or
2393 * start fetches. First try looking for an A record
2396 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2397 && WANT_INET(wanted_addresses)) {
2398 result = dbfind_name(adbname, now, dns_rdatatype_a);
2399 if (result == ISC_R_SUCCESS) {
2401 "dns_adb_createfind: found A for name %p in db",
2407 * Did we get a CNAME or DNAME?
2409 if (result == DNS_R_ALIAS) {
2411 "dns_adb_createfind: name %p is an alias",
2418 * If the name doesn't exist at all, don't bother with
2419 * v6 queries; they won't work.
2421 * If the name does exist but we didn't get our data, go
2422 * ahead and try AAAA.
2424 * If the result is neither of these, try a fetch for A.
2426 if (NXDOMAIN_RESULT(result))
2428 else if (NXRRSET_RESULT(result))
2431 if (!NAME_FETCH_V4(adbname))
2432 wanted_fetches |= DNS_ADBFIND_INET;
2436 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2437 && WANT_INET6(wanted_addresses)) {
2438 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2439 if (result == ISC_R_SUCCESS) {
2441 "dns_adb_createfind: found AAAA for name %p",
2447 * Did we get a CNAME or DNAME?
2449 if (result == DNS_R_ALIAS) {
2451 "dns_adb_createfind: name %p is an alias",
2458 * Listen to negative cache hints, and don't start
2461 if (NCACHE_RESULT(result) || AUTH_NX(result))
2464 if (!NAME_FETCH_V6(adbname))
2465 wanted_fetches |= DNS_ADBFIND_INET6;
2469 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2470 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2471 have_address = ISC_TRUE;
2473 have_address = ISC_FALSE;
2474 if (wanted_fetches != 0 &&
2475 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2477 * We're missing at least one address family. Either the
2478 * caller hasn't instructed us to avoid fetches, or we don't
2479 * know anything about any of the address families that would
2480 * be acceptable so we have to launch fetches.
2483 if (FIND_STARTATZONE(find))
2484 start_at_zone = ISC_TRUE;
2489 if (WANT_INET(wanted_fetches) &&
2490 fetch_name(adbname, start_at_zone,
2491 dns_rdatatype_a) == ISC_R_SUCCESS) {
2493 "dns_adb_createfind: started A fetch for name %p",
2500 if (WANT_INET6(wanted_fetches) &&
2501 fetch_name(adbname, start_at_zone,
2502 dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2504 "dns_adb_createfind: "
2505 "started AAAA fetch for name %p",
2511 * Run through the name and copy out the bits we are
2514 copy_namehook_lists(adb, find, zone, adbname, now);
2517 if (NAME_FETCH_V4(adbname))
2518 query_pending |= DNS_ADBFIND_INET;
2519 if (NAME_FETCH_V6(adbname))
2520 query_pending |= DNS_ADBFIND_INET6;
2523 * Attach to the name's query list if there are queries
2524 * already running, and we have been asked to.
2526 want_event = ISC_TRUE;
2527 if (!FIND_WANTEVENT(find))
2528 want_event = ISC_FALSE;
2529 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2530 want_event = ISC_FALSE;
2531 if ((wanted_addresses & query_pending) == 0)
2532 want_event = ISC_FALSE;
2534 want_event = ISC_FALSE;
2536 find->adbname = adbname;
2537 find->name_bucket = bucket;
2538 ISC_LIST_APPEND(adbname->finds, find, plink);
2539 find->query_pending = (query_pending & wanted_addresses);
2540 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2541 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2542 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2546 * Remove the flag so the caller knows there will never
2547 * be an event, and set internal flags to fake that
2548 * the event was sent and freed, so dns_adb_destroyfind() will
2549 * do the right thing.
2551 find->query_pending = (query_pending & wanted_addresses);
2552 find->options &= ~DNS_ADBFIND_WANTEVENT;
2553 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2554 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2557 find->partial_result |= (adbname->partial_result & wanted_addresses);
2559 if (target != NULL) {
2560 result = dns_name_copy(&adbname->target, target, NULL);
2561 if (result != ISC_R_SUCCESS)
2564 result = DNS_R_ALIAS;
2566 result = ISC_R_SUCCESS;
2569 * Copy out error flags from the name structure into the find.
2571 find->result_v4 = find_err_map[adbname->fetch_err];
2572 find->result_v6 = find_err_map[adbname->fetch6_err];
2581 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2583 isc_task_attach(task, &taskp);
2584 find->event.ev_sender = taskp;
2585 find->event.ev_action = action;
2586 find->event.ev_arg = arg;
2590 UNLOCK(&adb->namelocks[bucket]);
2596 dns_adb_destroyfind(dns_adbfind_t **findp) {
2597 dns_adbfind_t *find;
2598 dns_adbentry_t *entry;
2599 dns_adbaddrinfo_t *ai;
2603 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2609 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2612 REQUIRE(DNS_ADB_VALID(adb));
2614 REQUIRE(FIND_EVENTFREED(find));
2616 bucket = find->name_bucket;
2617 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2619 UNLOCK(&find->lock);
2622 * The find doesn't exist on any list, and nothing is locked.
2623 * Return the find to the memory pool, and decrement the adb's
2626 ai = ISC_LIST_HEAD(find->list);
2627 while (ai != NULL) {
2628 ISC_LIST_UNLINK(find->list, ai, publink);
2631 INSIST(DNS_ADBENTRY_VALID(entry));
2632 RUNTIME_CHECK(dec_entry_refcnt(adb, entry, ISC_TRUE) ==
2634 free_adbaddrinfo(adb, &ai);
2635 ai = ISC_LIST_HEAD(find->list);
2639 * WARNING: The find is freed with the adb locked. This is done
2640 * to avoid a race condition where we free the find, some other
2641 * thread tests to see if it should be destroyed, detects it should
2642 * be, destroys it, and then we try to lock it for our check, but the
2643 * lock is destroyed.
2646 if (free_adbfind(adb, &find))
2652 dns_adb_cancelfind(dns_adbfind_t *find) {
2661 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2664 REQUIRE(DNS_ADB_VALID(adb));
2666 REQUIRE(!FIND_EVENTFREED(find));
2667 REQUIRE(FIND_WANTEVENT(find));
2669 bucket = find->name_bucket;
2670 if (bucket == DNS_ADB_INVALIDBUCKET)
2674 * We need to get the adbname's lock to unlink the find.
2676 unlock_bucket = bucket;
2677 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2678 bucket = find->name_bucket;
2679 if (bucket != DNS_ADB_INVALIDBUCKET) {
2680 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2681 find->adbname = NULL;
2682 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2684 UNLOCK(&adb->namelocks[unlock_bucket]);
2685 bucket = DNS_ADB_INVALIDBUCKET;
2689 if (!FIND_EVENTSENT(find)) {
2691 task = ev->ev_sender;
2692 ev->ev_sender = find;
2693 ev->ev_type = DNS_EVENT_ADBCANCELED;
2694 ev->ev_destroy = event_free;
2695 ev->ev_destroy_arg = find;
2696 find->result_v4 = ISC_R_CANCELED;
2697 find->result_v6 = ISC_R_CANCELED;
2699 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2702 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2705 UNLOCK(&find->lock);
2709 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2713 REQUIRE(DNS_ADB_VALID(adb));
2717 * Lock the adb itself, lock all the name buckets, then lock all
2718 * the entry buckets. This should put the adb into a state where
2719 * nothing can change, so we can iterate through everything and
2720 * print at our leisure.
2724 isc_stdtime_get(&now);
2726 for (i = 0; i < NBUCKETS; i++)
2727 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2728 for (i = 0; i < NBUCKETS; i++)
2729 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2731 dump_adb(adb, f, ISC_FALSE, now);
2736 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2737 if (value == INT_MAX)
2739 fprintf(f, " [%s TTL %d]", legend, value - now);
2743 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2745 dns_adbname_t *name;
2746 dns_adbentry_t *entry;
2748 fprintf(f, ";\n; Address database dump\n;\n");
2750 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2751 adb, adb->erefcnt, adb->irefcnt,
2752 isc_mempool_getallocated(adb->nhmp));
2754 for (i = 0; i < NBUCKETS; i++)
2755 LOCK(&adb->namelocks[i]);
2756 for (i = 0; i < NBUCKETS; i++)
2757 LOCK(&adb->entrylocks[i]);
2762 for (i = 0; i < NBUCKETS; i++) {
2763 name = ISC_LIST_HEAD(adb->names[i]);
2767 fprintf(f, "; bucket %d\n", i);
2770 name = ISC_LIST_NEXT(name, plink))
2773 fprintf(f, "; name %p (flags %08x)\n",
2777 print_dns_name(f, &name->name);
2778 if (dns_name_countlabels(&name->target) > 0) {
2779 fprintf(f, " alias ");
2780 print_dns_name(f, &name->target);
2783 dump_ttl(f, "v4", name->expire_v4, now);
2784 dump_ttl(f, "v6", name->expire_v6, now);
2785 dump_ttl(f, "target", name->expire_target, now);
2787 fprintf(f, " [v4 %s] [v6 %s]",
2788 errnames[name->fetch_err],
2789 errnames[name->fetch6_err]);
2793 print_namehook_list(f, "v4", &name->v4, debug, now);
2794 print_namehook_list(f, "v6", &name->v6, debug, now);
2797 print_fetch_list(f, name);
2799 print_find_list(f, name);
2804 fprintf(f, ";\n; Unassociated entries\n;\n");
2806 for (i = 0; i < NBUCKETS; i++) {
2807 entry = ISC_LIST_HEAD(adb->entries[i]);
2808 while (entry != NULL) {
2809 if (entry->refcnt == 0)
2810 dump_entry(f, entry, debug, now);
2811 entry = ISC_LIST_NEXT(entry, plink);
2818 for (i = 0; i < NBUCKETS; i++)
2819 UNLOCK(&adb->entrylocks[i]);
2820 for (i = 0; i < NBUCKETS; i++)
2821 UNLOCK(&adb->namelocks[i]);
2825 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2828 char addrbuf[ISC_NETADDR_FORMATSIZE];
2829 isc_netaddr_t netaddr;
2830 dns_adbzoneinfo_t *zi;
2832 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2833 isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2836 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2838 fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2839 addrbuf, entry->srtt, entry->flags);
2840 if (entry->expires != 0)
2841 fprintf(f, " [ttl %d]", entry->expires - now);
2843 for (zi = ISC_LIST_HEAD(entry->zoneinfo);
2845 zi = ISC_LIST_NEXT(zi, plink)) {
2846 fprintf(f, ";\t\t");
2847 print_dns_name(f, &zi->zone);
2848 fprintf(f, " [lame TTL %d]\n", zi->lame_timer - now);
2853 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2856 dns_adbaddrinfo_t *ai;
2860 * Not used currently, in the API Just In Case we
2861 * want to dump out the name and/or entries too.
2866 fprintf(f, ";Find %p\n", find);
2867 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2868 find->query_pending, find->partial_result,
2869 find->options, find->flags);
2870 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2871 find->name_bucket, find->adbname, find->event.ev_sender);
2873 ai = ISC_LIST_HEAD(find->list);
2875 fprintf(f, "\tAddresses:\n");
2876 while (ai != NULL) {
2878 switch (sa->type.sa.sa_family) {
2880 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2884 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2892 tmpp = "BadAddress";
2894 fprintf(f, "\t\tentry %p, flags %08x"
2895 " srtt %u addr %s\n",
2896 ai->entry, ai->flags, ai->srtt, tmpp);
2898 ai = ISC_LIST_NEXT(ai, publink);
2901 UNLOCK(&find->lock);
2905 print_dns_name(FILE *f, dns_name_t *name) {
2906 char buf[DNS_NAME_FORMATSIZE];
2910 dns_name_format(name, buf, sizeof(buf));
2911 fprintf(f, "%s", buf);
2915 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2916 isc_boolean_t debug, isc_stdtime_t now)
2918 dns_adbnamehook_t *nh;
2920 for (nh = ISC_LIST_HEAD(*list);
2922 nh = ISC_LIST_NEXT(nh, plink))
2925 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2926 dump_entry(f, nh->entry, debug, now);
2931 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2932 fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
2933 type, ft, ft->namehook, ft->entry, ft->fetch);
2937 print_fetch_list(FILE *f, dns_adbname_t *n) {
2938 if (NAME_FETCH_A(n))
2939 print_fetch(f, n->fetch_a, "A");
2940 if (NAME_FETCH_AAAA(n))
2941 print_fetch(f, n->fetch_aaaa, "AAAA");
2945 print_find_list(FILE *f, dns_adbname_t *name) {
2946 dns_adbfind_t *find;
2948 find = ISC_LIST_HEAD(name->finds);
2949 while (find != NULL) {
2950 dns_adb_dumpfind(find, f);
2951 find = ISC_LIST_NEXT(find, plink);
2956 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2958 isc_result_t result;
2959 dns_rdataset_t rdataset;
2961 dns_fixedname_t foundname;
2964 INSIST(DNS_ADBNAME_VALID(adbname));
2966 INSIST(DNS_ADB_VALID(adb));
2967 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2969 dns_fixedname_init(&foundname);
2970 fname = dns_fixedname_name(&foundname);
2971 dns_rdataset_init(&rdataset);
2973 if (rdtype == dns_rdatatype_a)
2974 adbname->fetch_err = FIND_ERR_UNEXPECTED;
2976 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
2978 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
2979 NAME_GLUEOK(adbname),
2980 ISC_TF(NAME_HINTOK(adbname)),
2981 NULL, NULL, fname, &rdataset, NULL);
2983 /* XXXVIX this switch statement is too sparse to gen a jump table. */
2989 * Found in the database. Even if we can't copy out
2990 * any information, return success, or else a fetch
2991 * will be made, which will only make things worse.
2993 if (rdtype == dns_rdatatype_a)
2994 adbname->fetch_err = FIND_ERR_SUCCESS;
2996 adbname->fetch6_err = FIND_ERR_SUCCESS;
2997 result = import_rdataset(adbname, &rdataset, now);
2999 case DNS_R_NXDOMAIN:
3002 * We're authoritative and the data doesn't exist.
3003 * Make up a negative cache entry so we don't ask again
3006 * XXXRTH What time should we use? I'm putting in 30 seconds
3009 if (rdtype == dns_rdatatype_a) {
3010 adbname->expire_v4 = now + 30;
3012 "adb name %p: Caching auth negative entry for A",
3014 if (result == DNS_R_NXDOMAIN)
3015 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3017 adbname->fetch_err = FIND_ERR_NXRRSET;
3020 "adb name %p: Caching auth negative entry for AAAA",
3022 adbname->expire_v6 = now + 30;
3023 if (result == DNS_R_NXDOMAIN)
3024 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3026 adbname->fetch6_err = FIND_ERR_NXRRSET;
3029 case DNS_R_NCACHENXDOMAIN:
3030 case DNS_R_NCACHENXRRSET:
3032 * We found a negative cache entry. Pull the TTL from it
3033 * so we won't ask again for a while.
3035 rdataset.ttl = ttlclamp(rdataset.ttl);
3036 if (rdtype == dns_rdatatype_a) {
3037 adbname->expire_v4 = rdataset.ttl + now;
3038 if (result == DNS_R_NCACHENXDOMAIN)
3039 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3041 adbname->fetch_err = FIND_ERR_NXRRSET;
3043 "adb name %p: Caching negative entry for A (ttl %u)",
3044 adbname, rdataset.ttl);
3047 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3048 adbname, rdataset.ttl);
3049 adbname->expire_v6 = rdataset.ttl + now;
3050 if (result == DNS_R_NCACHENXDOMAIN)
3051 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3053 adbname->fetch6_err = FIND_ERR_NXRRSET;
3059 * Clear the hint and glue flags, so this will match
3062 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3064 rdataset.ttl = ttlclamp(rdataset.ttl);
3065 clean_target(adb, &adbname->target);
3066 adbname->expire_target = INT_MAX;
3067 result = set_target(adb, &adbname->name, fname, &rdataset,
3069 if (result == ISC_R_SUCCESS) {
3070 result = DNS_R_ALIAS;
3072 "adb name %p: caching alias target",
3074 adbname->expire_target = rdataset.ttl + now;
3076 if (rdtype == dns_rdatatype_a)
3077 adbname->fetch_err = FIND_ERR_SUCCESS;
3079 adbname->fetch6_err = FIND_ERR_SUCCESS;
3083 if (dns_rdataset_isassociated(&rdataset))
3084 dns_rdataset_disassociate(&rdataset);
3090 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3091 dns_fetchevent_t *dev;
3092 dns_adbname_t *name;
3094 dns_adbfetch_t *fetch;
3096 isc_eventtype_t ev_status;
3098 isc_result_t result;
3099 unsigned int address_type;
3100 isc_boolean_t want_check_exit = ISC_FALSE;
3104 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3105 dev = (dns_fetchevent_t *)ev;
3107 INSIST(DNS_ADBNAME_VALID(name));
3109 INSIST(DNS_ADB_VALID(adb));
3111 bucket = name->lock_bucket;
3112 LOCK(&adb->namelocks[bucket]);
3114 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3116 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3117 address_type = DNS_ADBFIND_INET;
3118 fetch = name->fetch_a;
3119 name->fetch_a = NULL;
3120 } else if (NAME_FETCH_AAAA(name)
3121 && (name->fetch_aaaa->fetch == dev->fetch)) {
3122 address_type = DNS_ADBFIND_INET6;
3123 fetch = name->fetch_aaaa;
3124 name->fetch_aaaa = NULL;
3126 INSIST(address_type != 0);
3128 dns_resolver_destroyfetch(&fetch->fetch);
3131 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3134 * Cleanup things we don't care about.
3136 if (dev->node != NULL)
3137 dns_db_detachnode(dev->db, &dev->node);
3138 if (dev->db != NULL)
3139 dns_db_detach(&dev->db);
3142 * If this name is marked as dead, clean up, throwing away
3143 * potentially good data.
3145 if (NAME_DEAD(name)) {
3146 free_adbfetch(adb, &fetch);
3147 isc_event_free(&ev);
3149 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3151 UNLOCK(&adb->namelocks[bucket]);
3153 if (want_check_exit) {
3162 isc_stdtime_get(&now);
3165 * If we got a negative cache response, remember it.
3167 if (NCACHE_RESULT(dev->result)) {
3168 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3169 if (address_type == DNS_ADBFIND_INET) {
3170 DP(NCACHE_LEVEL, "adb fetch name %p: "
3171 "caching negative entry for A (ttl %u)",
3172 name, dev->rdataset->ttl);
3173 name->expire_v4 = ISC_MIN(name->expire_v4,
3174 dev->rdataset->ttl + now);
3175 if (dev->result == DNS_R_NCACHENXDOMAIN)
3176 name->fetch_err = FIND_ERR_NXDOMAIN;
3178 name->fetch_err = FIND_ERR_NXRRSET;
3180 DP(NCACHE_LEVEL, "adb fetch name %p: "
3181 "caching negative entry for AAAA (ttl %u)",
3182 name, dev->rdataset->ttl);
3183 name->expire_v6 = ISC_MIN(name->expire_v6,
3184 dev->rdataset->ttl + now);
3185 if (dev->result == DNS_R_NCACHENXDOMAIN)
3186 name->fetch6_err = FIND_ERR_NXDOMAIN;
3188 name->fetch6_err = FIND_ERR_NXRRSET;
3194 * Handle CNAME/DNAME.
3196 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3197 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3198 clean_target(adb, &name->target);
3199 name->expire_target = INT_MAX;
3200 result = set_target(adb, &name->name,
3201 dns_fixedname_name(&dev->foundname),
3204 if (result == ISC_R_SUCCESS) {
3206 "adb fetch name %p: caching alias target",
3208 name->expire_target = dev->rdataset->ttl + now;
3214 * Did we get back junk? If so, and there are no more fetches
3215 * sitting out there, tell all the finds about it.
3217 if (dev->result != ISC_R_SUCCESS) {
3218 char buf[DNS_NAME_FORMATSIZE];
3220 dns_name_format(&name->name, buf, sizeof(buf));
3221 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3222 buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3223 dns_result_totext(dev->result));
3224 /* XXXMLG Don't pound on bad servers. */
3225 if (address_type == DNS_ADBFIND_INET) {
3226 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3227 name->fetch_err = FIND_ERR_FAILURE;
3229 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3230 name->fetch6_err = FIND_ERR_FAILURE;
3236 * We got something potentially useful.
3238 result = import_rdataset(name, &fetch->rdataset, now);
3241 if (result == ISC_R_SUCCESS) {
3242 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3243 if (address_type == DNS_ADBFIND_INET)
3244 name->fetch_err = FIND_ERR_SUCCESS;
3246 name->fetch6_err = FIND_ERR_SUCCESS;
3250 free_adbfetch(adb, &fetch);
3251 isc_event_free(&ev);
3253 clean_finds_at_name(name, ev_status, address_type);
3255 UNLOCK(&adb->namelocks[bucket]);
3259 fetch_name(dns_adbname_t *adbname,
3260 isc_boolean_t start_at_zone,
3261 dns_rdatatype_t type)
3263 isc_result_t result;
3264 dns_adbfetch_t *fetch = NULL;
3266 dns_fixedname_t fixed;
3268 dns_rdataset_t rdataset;
3269 dns_rdataset_t *nameservers;
3270 unsigned int options;
3272 INSIST(DNS_ADBNAME_VALID(adbname));
3274 INSIST(DNS_ADB_VALID(adb));
3276 INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3277 (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3279 adbname->fetch_err = FIND_ERR_NOTFOUND;
3283 dns_rdataset_init(&rdataset);
3285 options = DNS_FETCHOPT_NOVALIDATE;
3286 if (start_at_zone) {
3288 "fetch_name: starting at zone for name %p",
3290 dns_fixedname_init(&fixed);
3291 name = dns_fixedname_name(&fixed);
3292 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3293 0, 0, ISC_TRUE, ISC_FALSE,
3295 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3297 nameservers = &rdataset;
3298 options |= DNS_FETCHOPT_UNSHARED;
3301 fetch = new_adbfetch(adb);
3302 if (fetch == NULL) {
3303 result = ISC_R_NOMEMORY;
3307 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3308 type, name, nameservers, NULL,
3309 options, adb->task, fetch_callback,
3310 adbname, &fetch->rdataset, NULL,
3312 if (result != ISC_R_SUCCESS)
3315 if (type == dns_rdatatype_a)
3316 adbname->fetch_a = fetch;
3318 adbname->fetch_aaaa = fetch;
3319 fetch = NULL; /* Keep us from cleaning this up below. */
3323 free_adbfetch(adb, &fetch);
3324 if (dns_rdataset_isassociated(&rdataset))
3325 dns_rdataset_disassociate(&rdataset);
3331 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3332 * since these can be extracted from the find itself.
3335 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
3336 isc_stdtime_t expire_time)
3338 dns_adbzoneinfo_t *zi;
3340 isc_result_t result = ISC_R_SUCCESS;
3342 REQUIRE(DNS_ADB_VALID(adb));
3343 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3344 REQUIRE(zone != NULL);
3346 bucket = addr->entry->lock_bucket;
3347 LOCK(&adb->entrylocks[bucket]);
3348 zi = ISC_LIST_HEAD(addr->entry->zoneinfo);
3349 while (zi != NULL && !dns_name_equal(zone, &zi->zone))
3350 zi = ISC_LIST_NEXT(zi, plink);
3352 if (expire_time > zi->lame_timer)
3353 zi->lame_timer = expire_time;
3356 zi = new_adbzoneinfo(adb, zone);
3358 result = ISC_R_NOMEMORY;
3362 zi->lame_timer = expire_time;
3364 ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, plink);
3366 UNLOCK(&adb->entrylocks[bucket]);
3372 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3373 unsigned int rtt, unsigned int factor)
3376 unsigned int new_srtt;
3379 REQUIRE(DNS_ADB_VALID(adb));
3380 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3381 REQUIRE(factor <= 10);
3383 bucket = addr->entry->lock_bucket;
3384 LOCK(&adb->entrylocks[bucket]);
3386 if (factor == DNS_ADB_RTTADJAGE)
3387 new_srtt = addr->entry->srtt * 98 / 100;
3389 new_srtt = (addr->entry->srtt / 10 * factor)
3390 + (rtt / 10 * (10 - factor));
3392 addr->entry->srtt = new_srtt;
3393 addr->srtt = new_srtt;
3395 isc_stdtime_get(&now);
3396 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3398 UNLOCK(&adb->entrylocks[bucket]);
3402 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3403 unsigned int bits, unsigned int mask)
3407 REQUIRE(DNS_ADB_VALID(adb));
3408 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3410 bucket = addr->entry->lock_bucket;
3411 LOCK(&adb->entrylocks[bucket]);
3413 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3415 * Note that we do not update the other bits in addr->flags with
3416 * the most recent values from addr->entry->flags.
3418 addr->flags = (addr->flags & ~mask) | (bits & mask);
3420 UNLOCK(&adb->entrylocks[bucket]);
3424 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3425 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3428 dns_adbentry_t *entry;
3429 dns_adbaddrinfo_t *addr;
3430 isc_result_t result;
3433 REQUIRE(DNS_ADB_VALID(adb));
3434 REQUIRE(addrp != NULL && *addrp == NULL);
3438 result = ISC_R_SUCCESS;
3439 bucket = DNS_ADB_INVALIDBUCKET;
3440 entry = find_entry_and_lock(adb, sa, &bucket);
3441 if (adb->entry_sd[bucket]) {
3442 result = ISC_R_SHUTTINGDOWN;
3445 if (entry == NULL) {
3447 * We don't know anything about this address.
3449 entry = new_adbentry(adb);
3450 if (entry == NULL) {
3451 result = ISC_R_NOMEMORY;
3454 entry->sockaddr = *sa;
3455 link_entry(adb, bucket, entry);
3456 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3458 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3460 port = isc_sockaddr_getport(sa);
3461 addr = new_adbaddrinfo(adb, entry, port);
3463 inc_entry_refcnt(adb, entry, ISC_FALSE);
3468 UNLOCK(&adb->entrylocks[bucket]);
3474 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3475 dns_adbaddrinfo_t *addr;
3476 dns_adbentry_t *entry;
3479 isc_boolean_t want_check_exit = ISC_FALSE;
3481 REQUIRE(DNS_ADB_VALID(adb));
3482 REQUIRE(addrp != NULL);
3484 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3485 entry = addr->entry;
3486 REQUIRE(DNS_ADBENTRY_VALID(entry));
3488 isc_stdtime_get(&now);
3492 bucket = addr->entry->lock_bucket;
3493 LOCK(&adb->entrylocks[bucket]);
3495 entry->expires = now + ADB_ENTRY_WINDOW;
3497 want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
3499 UNLOCK(&adb->entrylocks[bucket]);
3502 free_adbaddrinfo(adb, &addr);
3504 if (want_check_exit) {
3512 dns_adb_flush(dns_adb_t *adb) {
3515 INSIST(DNS_ADB_VALID(adb));
3520 * Call our cleanup routines.
3522 for (i = 0; i < NBUCKETS; i++)
3523 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3524 for (i = 0; i < NBUCKETS; i++)
3525 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3527 #ifdef DUMP_ADB_AFTER_CLEANING
3528 dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3535 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3536 dns_adbname_t *adbname;
3537 dns_adbname_t *nextname;
3540 INSIST(DNS_ADB_VALID(adb));
3543 bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3544 LOCK(&adb->namelocks[bucket]);
3545 adbname = ISC_LIST_HEAD(adb->names[bucket]);
3546 while (adbname != NULL) {
3547 nextname = ISC_LIST_NEXT(adbname, plink);
3548 if (!NAME_DEAD(adbname) &&
3549 dns_name_equal(name, &adbname->name)) {
3550 RUNTIME_CHECK(kill_name(&adbname,
3551 DNS_EVENT_ADBCANCELED) ==
3556 UNLOCK(&adb->namelocks[bucket]);
3561 water(void *arg, int mark) {
3562 dns_adb_t *adb = arg;
3563 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3564 isc_interval_t interval;
3566 REQUIRE(DNS_ADB_VALID(adb));
3568 DP(ISC_LOG_DEBUG(1),
3569 "adb reached %s water mark", overmem ? "high" : "low");
3571 adb->overmem = overmem;
3573 isc_interval_set(&interval, 0, 1);
3574 (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
3575 &interval, ISC_TRUE);
3580 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3581 isc_uint32_t hiwater;
3582 isc_uint32_t lowater;
3584 INSIST(DNS_ADB_VALID(adb));
3586 if (size != 0 && size < DNS_ADB_MINADBSIZE)
3587 size = DNS_ADB_MINADBSIZE;
3589 hiwater = size - (size >> 3); /* Approximately 7/8ths. */
3590 lowater = size - (size >> 2); /* Approximately 3/4ths. */
3592 if (size == 0 || hiwater == 0 || lowater == 0)
3593 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3595 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);