2 * Copyright (C) 2004 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.17 2004/03/09 06:10:59 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>
52 #include <dns/events.h>
54 #include <dns/rdata.h>
55 #include <dns/rdataset.h>
56 #include <dns/rdatastruct.h>
57 #include <dns/resolver.h>
58 #include <dns/result.h>
60 #define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
61 #define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
62 #define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
63 #define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
64 #define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
65 #define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
66 #define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
67 #define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
68 #define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
69 #define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
70 #define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
71 #define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
72 #define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
73 #define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
76 * The number of buckets needs to be a prime (for good hashing).
78 * XXXRTH How many buckets do we need?
80 #define NBUCKETS 1009 /* how many buckets for names/addrs */
83 * For type 3 negative cache entries, we will remember that the address is
84 * broken for this long. XXXMLG This is also used for actual addresses, too.
85 * The intent is to keep us from constantly asking about A/A6/AAAA records
86 * if the zone has extremely low TTLs.
88 #define ADB_CACHE_MINIMUM 10 /* seconds */
89 #define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
90 #define ADB_ENTRY_WINDOW 1800 /* seconds */
93 * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
94 * buckets are cleaned in CLEAN_PERIOD seconds.
96 #define CLEAN_PERIOD 3600
97 #define CLEAN_SECONDS 30
98 #define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
100 #define FREE_ITEMS 64 /* free count for memory pools */
101 #define FILL_COUNT 16 /* fill count for memory pools */
103 #define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
105 typedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
106 typedef struct dns_adbnamehook dns_adbnamehook_t;
107 typedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
108 typedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
109 typedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
110 typedef struct dns_adbfetch dns_adbfetch_t;
111 typedef struct dns_adbfetch6 dns_adbfetch6_t;
117 isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
120 isc_timermgr_t *timermgr;
122 isc_taskmgr_t *taskmgr;
125 isc_interval_t tick_interval;
126 int next_cleanbucket;
128 unsigned int irefcnt;
129 unsigned int erefcnt;
132 isc_mempool_t *nmp; /* dns_adbname_t */
133 isc_mempool_t *nhmp; /* dns_adbnamehook_t */
134 isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
135 isc_mempool_t *emp; /* dns_adbentry_t */
136 isc_mempool_t *ahmp; /* dns_adbfind_t */
137 isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
138 isc_mempool_t *afmp; /* dns_adbfetch_t */
139 isc_mempool_t *af6mp; /* dns_adbfetch6_t */
142 * Bucketized locks and lists for names.
144 * XXXRTH Have a per-bucket structure that contains all of these?
146 dns_adbnamelist_t names[NBUCKETS];
147 isc_mutex_t namelocks[NBUCKETS];
148 isc_boolean_t name_sd[NBUCKETS];
149 unsigned int name_refcnt[NBUCKETS];
152 * Bucketized locks for entries.
154 * XXXRTH Have a per-bucket structure that contains all of these?
156 dns_adbentrylist_t entries[NBUCKETS];
157 isc_mutex_t entrylocks[NBUCKETS];
158 isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
159 unsigned int entry_refcnt[NBUCKETS];
162 isc_boolean_t cevent_sent;
163 isc_boolean_t shutting_down;
164 isc_eventlist_t whenshutdown;
168 * XXXMLG Document these structures.
175 unsigned int partial_result;
179 isc_stdtime_t expire_target;
180 isc_stdtime_t expire_v4;
181 isc_stdtime_t expire_v6;
183 dns_adbnamehooklist_t v4;
184 dns_adbnamehooklist_t v6;
185 dns_adbfetch_t *fetch_a;
186 dns_adbfetch_t *fetch_aaaa;
187 ISC_LIST(dns_adbfetch6_t) fetches_a6;
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;
202 struct dns_adbfetch6 {
205 dns_adbnamehook_t *namehook;
206 dns_adbentry_t *entry;
208 dns_rdataset_t rdataset;
209 dns_a6context_t a6ctx;
210 ISC_LINK(dns_adbfetch6_t) plink;
216 * This is a small widget that dangles off a dns_adbname_t. It contains a
217 * pointer to the address information about this host, and a link to the next
218 * namehook that will contain the next address this host has.
220 struct dns_adbnamehook {
222 dns_adbentry_t *entry;
223 ISC_LINK(dns_adbnamehook_t) plink;
229 * This is a small widget that holds zone-specific information about an
230 * address. Currently limited to lameness, but could just as easily be
231 * extended to other types of information about zones.
233 struct dns_adbzoneinfo {
237 isc_stdtime_t lame_timer;
239 ISC_LINK(dns_adbzoneinfo_t) plink;
243 * An address entry. It holds quite a bit of information about addresses,
244 * including edns state (in "flags"), rtt, and of course the address of
247 struct dns_adbentry {
255 isc_sockaddr_t sockaddr;
257 isc_stdtime_t expires;
259 * A nonzero 'expires' field indicates that the entry should
260 * persist until that time. This allows entries found
261 * using dns_adb_findaddrinfo() to persist for a limited time
262 * even though they are not necessarily associated with a
266 ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
267 ISC_LINK(dns_adbentry_t) plink;
271 * Internal functions (and prototypes).
273 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
274 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
275 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
277 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
278 static inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
279 static inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
280 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
281 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
282 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
283 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
284 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
286 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
287 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
288 static inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
290 static inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
291 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
292 unsigned int, int *);
293 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
294 isc_sockaddr_t *, int *);
295 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
296 static void print_dns_name(FILE *, dns_name_t *);
297 static void print_namehook_list(FILE *, const char *legend,
298 dns_adbnamehooklist_t *list,
299 isc_boolean_t debug);
300 static void print_find_list(FILE *, dns_adbname_t *);
301 static void print_fetch_list(FILE *, dns_adbname_t *);
302 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
303 static inline void inc_adb_erefcnt(dns_adb_t *);
304 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
306 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
308 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
309 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
310 static void clean_target(dns_adb_t *, dns_name_t *);
311 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
313 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
314 static void cancel_fetches_at_name(dns_adbname_t *);
315 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
317 static isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
318 static isc_result_t fetch_name_aaaa(dns_adbname_t *);
319 static isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t);
320 static inline void check_exit(dns_adb_t *);
321 static void timer_cleanup(isc_task_t *, isc_event_t *);
322 static void destroy(dns_adb_t *);
323 static isc_boolean_t shutdown_names(dns_adb_t *);
324 static isc_boolean_t shutdown_entries(dns_adb_t *);
325 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
326 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
327 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
328 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
329 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
330 static void fetch_callback_a6(isc_task_t *, isc_event_t *);
331 static isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t);
334 * MUST NOT overlap DNS_ADBFIND_* flags!
336 #define FIND_EVENT_SENT 0x40000000
337 #define FIND_EVENT_FREED 0x80000000
338 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
339 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
341 #define NAME_NEEDS_POKE 0x80000000
342 #define NAME_IS_DEAD 0x40000000
343 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
344 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
345 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
346 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
347 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
348 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
349 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
352 * To the name, address classes are all that really exist. If it has a
353 * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
355 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
356 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
357 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
360 * Fetches are broken out into A, AAAA, and A6 types. In some cases,
361 * however, it makes more sense to test for a particular class of fetches,
362 * like V4 or V6 above.
364 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
365 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
366 #define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
367 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
368 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
369 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
372 * Was this fetch started using the hints database?
373 * Was this the initial fetch for the A6 record? If so, we might want to
374 * start AAAA queries if it fails.
376 #define FETCH_FIRST_A6 0x80000000
377 #define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
380 * Find options and tests to see if there are addresses on the list.
382 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
383 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
384 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
386 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
388 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
389 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
390 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
391 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
394 * These are currently used on simple unsigned ints, so they are
395 * not really associated with any particular type.
397 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
398 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
400 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
403 * Find out if the flags on a name (nf) indicate if it is a hint or
404 * glue, and compare this to the appropriate bits set in o, to see if
407 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
408 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
409 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
410 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
411 ((o) & DNS_ADBFIND_STARTATZONE))
413 #define ENTER_LEVEL 50
414 #define EXIT_LEVEL ENTER_LEVEL
415 #define CLEAN_LEVEL 100
417 #define NCACHE_LEVEL 20
419 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
420 (r) == DNS_R_NCACHENXRRSET)
421 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
422 (r) == DNS_R_NXRRSET)
423 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
424 (r) == DNS_R_NCACHENXDOMAIN)
425 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
426 (r) == DNS_R_NXRRSET || \
427 (r) == DNS_R_HINTNXRRSET)
430 * Error state rankings.
433 #define FIND_ERR_SUCCESS 0 /* highest rank */
434 #define FIND_ERR_CANCELED 1
435 #define FIND_ERR_FAILURE 2
436 #define FIND_ERR_NXDOMAIN 3
437 #define FIND_ERR_NXRRSET 4
438 #define FIND_ERR_UNEXPECTED 5
439 #define FIND_ERR_NOTFOUND 6
440 #define FIND_ERR_MAX 7
442 static const char *errnames[] = {
452 #define NEWERR(old, new) (ISC_MIN((old), (new)))
454 static isc_result_t find_err_map[FIND_ERR_MAX] = {
461 ISC_R_NOTFOUND /* not YET found */
465 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
468 DP(int level, const char *format, ...) {
471 va_start(args, format);
472 isc_log_vwrite(dns_lctx,
473 DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
474 ISC_LOG_DEBUG(level), format, args);
478 static inline dns_ttl_t
479 ttlclamp(dns_ttl_t ttl) {
480 if (ttl < ADB_CACHE_MINIMUM)
481 ttl = ADB_CACHE_MINIMUM;
482 if (ttl > ADB_CACHE_MAXIMUM)
483 ttl = ADB_CACHE_MAXIMUM;
489 * Requires the adbname bucket be locked and that no entry buckets be locked.
491 * This code handles A and AAAA rdatasets only.
494 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
499 dns_adbnamehook_t *nh;
500 dns_adbnamehook_t *anh;
501 dns_rdata_t rdata = DNS_RDATA_INIT;
503 struct in6_addr in6a;
504 isc_sockaddr_t sockaddr;
505 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
507 isc_boolean_t new_addresses_added;
508 dns_rdatatype_t rdtype;
509 unsigned int findoptions;
511 INSIST(DNS_ADBNAME_VALID(adbname));
513 INSIST(DNS_ADB_VALID(adb));
515 rdtype = rdataset->type;
516 INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
517 if (rdtype == dns_rdatatype_a)
518 findoptions = DNS_ADBFIND_INET;
520 findoptions = DNS_ADBFIND_INET6;
522 addr_bucket = DNS_ADB_INVALIDBUCKET;
523 new_addresses_added = ISC_FALSE;
526 result = dns_rdataset_first(rdataset);
527 while (result == ISC_R_SUCCESS) {
528 dns_rdata_reset(&rdata);
529 dns_rdataset_current(rdataset, &rdata);
530 if (rdtype == dns_rdatatype_a) {
531 INSIST(rdata.length == 4);
532 memcpy(&ina.s_addr, rdata.data, 4);
533 isc_sockaddr_fromin(&sockaddr, &ina, 0);
535 INSIST(rdata.length == 16);
536 memcpy(in6a.s6_addr, rdata.data, 16);
537 isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
541 nh = new_adbnamehook(adb, NULL);
543 adbname->partial_result |= findoptions;
544 result = ISC_R_NOMEMORY;
548 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
549 if (foundentry == NULL) {
550 dns_adbentry_t *entry;
552 entry = new_adbentry(adb);
554 adbname->partial_result |= findoptions;
555 result = ISC_R_NOMEMORY;
559 entry->sockaddr = sockaddr;
564 link_entry(adb, addr_bucket, entry);
566 for (anh = ISC_LIST_HEAD(adbname->v4);
568 anh = ISC_LIST_NEXT(anh, plink))
569 if (anh->entry == foundentry)
572 foundentry->refcnt++;
573 nh->entry = foundentry;
575 free_adbnamehook(adb, &nh);
578 new_addresses_added = ISC_TRUE;
580 if (rdtype == dns_rdatatype_a)
581 ISC_LIST_APPEND(adbname->v4, nh, plink);
583 ISC_LIST_APPEND(adbname->v6, nh, plink);
586 result = dns_rdataset_next(rdataset);
591 free_adbnamehook(adb, &nh);
593 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
594 UNLOCK(&adb->entrylocks[addr_bucket]);
596 if (rdataset->trust == dns_trust_glue ||
597 rdataset->trust == dns_trust_additional)
598 rdataset->ttl = ADB_CACHE_MINIMUM;
600 rdataset->ttl = ttlclamp(rdataset->ttl);
602 if (rdtype == dns_rdatatype_a) {
603 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
604 adbname->expire_v4, now + rdataset->ttl);
605 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
606 now + rdataset->ttl);
608 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
609 adbname->expire_v6, now + rdataset->ttl);
610 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
611 now + rdataset->ttl);
614 if (new_addresses_added) {
616 * Lie a little here. This is more or less so code that cares
617 * can find out if any new information was added or not.
619 return (ISC_R_SUCCESS);
626 import_a6(dns_a6context_t *a6ctx) {
629 dns_adbnamehook_t *nh;
630 dns_adbentry_t *foundentry; /* NO CLEAN UP! */
632 isc_sockaddr_t sockaddr;
635 INSIST(DNS_ADBNAME_VALID(name));
637 INSIST(DNS_ADB_VALID(adb));
639 addr_bucket = DNS_ADB_INVALIDBUCKET;
641 DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
643 nh = new_adbnamehook(adb, NULL);
645 name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
649 isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 0);
651 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
652 if (foundentry == NULL) {
653 dns_adbentry_t *entry;
654 entry = new_adbentry(adb);
656 name->partial_result |= DNS_ADBFIND_INET6;
660 entry->sockaddr = sockaddr;
663 link_entry(adb, addr_bucket, entry);
665 foundentry->refcnt++;
666 nh->entry = foundentry;
669 ISC_LIST_APPEND(name->v6, nh, plink);
673 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
674 name->expire_v6, a6ctx->expiration);
675 name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
677 name->flags |= NAME_NEEDS_POKE;
680 free_adbnamehook(adb, &nh);
682 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
683 UNLOCK(&adb->entrylocks[addr_bucket]);
687 * Requires the name's bucket be locked.
690 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
692 isc_boolean_t result = ISC_FALSE;
693 isc_boolean_t result4, result6;
699 INSIST(DNS_ADBNAME_VALID(name));
701 INSIST(DNS_ADB_VALID(adb));
703 DP(DEF_LEVEL, "killing name %p", name);
706 * If we're dead already, just check to see if we should go
709 if (NAME_DEAD(name) && !NAME_FETCH(name)) {
710 result = unlink_name(adb, name);
711 free_adbname(adb, &name);
713 result = dec_adb_irefcnt(adb);
718 * Clean up the name's various lists. These two are destructive
719 * in that they will always empty the list.
721 clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
722 result4 = clean_namehooks(adb, &name->v4);
723 result6 = clean_namehooks(adb, &name->v6);
724 clean_target(adb, &name->target);
725 result = ISC_TF(result4 || result6);
728 * If fetches are running, cancel them. If none are running, we can
729 * just kill the name here.
731 if (!NAME_FETCH(name)) {
732 INSIST(result == ISC_FALSE);
733 result = unlink_name(adb, name);
734 free_adbname(adb, &name);
736 result = dec_adb_irefcnt(adb);
738 name->flags |= NAME_IS_DEAD;
739 cancel_fetches_at_name(name);
745 * Requires the name's bucket be locked and no entry buckets be locked.
748 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
750 isc_boolean_t result4 = ISC_FALSE;
751 isc_boolean_t result6 = ISC_FALSE;
753 INSIST(DNS_ADBNAME_VALID(name));
755 INSIST(DNS_ADB_VALID(adb));
758 * Check to see if we need to remove the v4 addresses
760 if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
761 if (NAME_HAS_V4(name)) {
762 DP(DEF_LEVEL, "expiring v4 for name %p", name);
763 result4 = clean_namehooks(adb, &name->v4);
764 name->partial_result &= ~DNS_ADBFIND_INET;
766 name->expire_v4 = INT_MAX;
767 name->fetch_err = FIND_ERR_UNEXPECTED;
771 * Check to see if we need to remove the v6 addresses
773 if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
774 if (NAME_HAS_V6(name)) {
775 DP(DEF_LEVEL, "expiring v6 for name %p", name);
776 result6 = clean_namehooks(adb, &name->v6);
777 name->partial_result &= ~DNS_ADBFIND_INET6;
779 name->expire_v6 = INT_MAX;
780 name->fetch6_err = FIND_ERR_UNEXPECTED;
784 * Check to see if we need to remove the alias target.
786 if (EXPIRE_OK(name->expire_target, now)) {
787 clean_target(adb, &name->target);
788 name->expire_target = INT_MAX;
790 return (ISC_TF(result4 || result6));
794 * Requires the name's bucket be locked.
797 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
798 INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
800 ISC_LIST_PREPEND(adb->names[bucket], name, plink);
801 name->lock_bucket = bucket;
802 adb->name_refcnt[bucket]++;
806 * Requires the name's bucket be locked.
808 static inline isc_boolean_t
809 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
811 isc_boolean_t result = ISC_FALSE;
813 bucket = name->lock_bucket;
814 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
816 ISC_LIST_UNLINK(adb->names[bucket], name, plink);
817 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
818 INSIST(adb->name_refcnt[bucket] > 0);
819 adb->name_refcnt[bucket]--;
820 if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
826 * Requires the entry's bucket be locked.
829 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
830 ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
831 entry->lock_bucket = bucket;
832 adb->entry_refcnt[bucket]++;
836 * Requires the entry's bucket be locked.
838 static inline isc_boolean_t
839 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
841 isc_boolean_t result = ISC_FALSE;
843 bucket = entry->lock_bucket;
844 INSIST(bucket != DNS_ADB_INVALIDBUCKET);
846 ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
847 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
848 INSIST(adb->entry_refcnt[bucket] > 0);
849 adb->entry_refcnt[bucket]--;
850 if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
856 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
857 if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
865 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
866 * checked after calling this function.
869 shutdown_names(dns_adb_t *adb) {
871 isc_boolean_t result = ISC_FALSE;
873 dns_adbname_t *next_name;
875 for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
876 LOCK(&adb->namelocks[bucket]);
877 adb->name_sd[bucket] = ISC_TRUE;
879 name = ISC_LIST_HEAD(adb->names[bucket]);
882 * This bucket has no names. We must decrement the
883 * irefcnt ourselves, since it will not be
884 * automatically triggered by a name being unlinked.
886 INSIST(result == ISC_FALSE);
887 result = dec_adb_irefcnt(adb);
890 * Run through the list. For each name, clean up finds
891 * found there, and cancel any fetches running. When
892 * all the fetches are canceled, the name will destroy
895 while (name != NULL) {
896 next_name = ISC_LIST_NEXT(name, plink);
897 INSIST(result == ISC_FALSE);
898 result = kill_name(&name,
899 DNS_EVENT_ADBSHUTDOWN);
904 UNLOCK(&adb->namelocks[bucket]);
910 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
911 * checked after calling this function.
914 shutdown_entries(dns_adb_t *adb) {
916 isc_boolean_t result = ISC_FALSE;
917 dns_adbentry_t *entry;
918 dns_adbentry_t *next_entry;
920 for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
921 LOCK(&adb->entrylocks[bucket]);
922 adb->entry_sd[bucket] = ISC_TRUE;
924 entry = ISC_LIST_HEAD(adb->entries[bucket]);
927 * This bucket has no entries. We must decrement the
928 * irefcnt ourselves, since it will not be
929 * automatically triggered by an entry being unlinked.
931 result = dec_adb_irefcnt(adb);
934 * Run through the list. Cleanup any entries not
935 * associated with names, and which are not in use.
937 while (entry != NULL) {
938 next_entry = ISC_LIST_NEXT(entry, plink);
939 if (entry->refcnt == 0 &&
940 entry->expires != 0) {
941 result = unlink_entry(adb, entry);
942 free_adbentry(adb, &entry);
944 result = dec_adb_irefcnt(adb);
950 UNLOCK(&adb->entrylocks[bucket]);
956 * Name bucket must be locked
959 cancel_fetches_at_name(dns_adbname_t *name) {
960 dns_adbfetch6_t *fetch6;
962 if (NAME_FETCH_A(name))
963 dns_resolver_cancelfetch(name->fetch_a->fetch);
966 if (NAME_FETCH_AAAA(name))
967 dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
970 fetch6 = ISC_LIST_HEAD(name->fetches_a6);
971 while (fetch6 != NULL) {
972 dns_resolver_cancelfetch(fetch6->fetch);
973 fetch6 = ISC_LIST_NEXT(fetch6, plink);
978 * Assumes the name bucket is locked.
981 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
982 dns_adbentry_t *entry;
983 dns_adbnamehook_t *namehook;
985 isc_boolean_t result = ISC_FALSE;
987 addr_bucket = DNS_ADB_INVALIDBUCKET;
988 namehook = ISC_LIST_HEAD(*namehooks);
989 while (namehook != NULL) {
990 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
993 * Clean up the entry if needed.
995 entry = namehook->entry;
997 INSIST(DNS_ADBENTRY_VALID(entry));
999 if (addr_bucket != entry->lock_bucket) {
1000 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
1001 UNLOCK(&adb->entrylocks[addr_bucket]);
1002 addr_bucket = entry->lock_bucket;
1003 LOCK(&adb->entrylocks[addr_bucket]);
1006 result = dec_entry_refcnt(adb, entry, ISC_FALSE);
1012 namehook->entry = NULL;
1013 ISC_LIST_UNLINK(*namehooks, namehook, plink);
1014 free_adbnamehook(adb, &namehook);
1016 namehook = ISC_LIST_HEAD(*namehooks);
1019 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
1020 UNLOCK(&adb->entrylocks[addr_bucket]);
1025 clean_target(dns_adb_t *adb, dns_name_t *target) {
1026 if (dns_name_countlabels(target) > 0) {
1027 dns_name_free(target, adb->mctx);
1028 dns_name_init(target, NULL);
1033 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
1034 dns_rdataset_t *rdataset, dns_name_t *target)
1036 isc_result_t result;
1037 dns_namereln_t namereln;
1038 unsigned int nlabels, nbits;
1040 dns_rdata_t rdata = DNS_RDATA_INIT;
1041 dns_fixedname_t fixed1, fixed2;
1042 dns_name_t *prefix, *new_target;
1044 REQUIRE(dns_name_countlabels(target) == 0);
1046 if (rdataset->type == dns_rdatatype_cname) {
1047 dns_rdata_cname_t cname;
1050 * Copy the CNAME's target into the target name.
1052 result = dns_rdataset_first(rdataset);
1053 if (result != ISC_R_SUCCESS)
1055 dns_rdataset_current(rdataset, &rdata);
1056 result = dns_rdata_tostruct(&rdata, &cname, NULL);
1057 if (result != ISC_R_SUCCESS)
1059 result = dns_name_dup(&cname.cname, adb->mctx, target);
1060 dns_rdata_freestruct(&cname);
1061 if (result != ISC_R_SUCCESS)
1064 dns_rdata_dname_t dname;
1066 INSIST(rdataset->type == dns_rdatatype_dname);
1067 namereln = dns_name_fullcompare(name, fname, &order,
1069 INSIST(namereln == dns_namereln_subdomain);
1071 * Get the target name of the DNAME.
1073 result = dns_rdataset_first(rdataset);
1074 if (result != ISC_R_SUCCESS)
1076 dns_rdataset_current(rdataset, &rdata);
1077 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1078 if (result != ISC_R_SUCCESS)
1081 * Construct the new target name.
1083 dns_fixedname_init(&fixed1);
1084 prefix = dns_fixedname_name(&fixed1);
1085 dns_fixedname_init(&fixed2);
1086 new_target = dns_fixedname_name(&fixed2);
1087 result = dns_name_split(name, nlabels, nbits, prefix, NULL);
1088 if (result != ISC_R_SUCCESS) {
1089 dns_rdata_freestruct(&dname);
1092 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1094 dns_rdata_freestruct(&dname);
1095 if (result != ISC_R_SUCCESS)
1097 result = dns_name_dup(new_target, adb->mctx, target);
1098 if (result != ISC_R_SUCCESS)
1102 return (ISC_R_SUCCESS);
1106 * Assumes nothing is locked, since this is called by the client.
1109 event_free(isc_event_t *event) {
1110 dns_adbfind_t *find;
1112 INSIST(event != NULL);
1113 find = event->ev_destroy_arg;
1114 INSIST(DNS_ADBFIND_VALID(find));
1117 find->flags |= FIND_EVENT_FREED;
1118 event->ev_destroy_arg = NULL;
1119 UNLOCK(&find->lock);
1123 * Assumes the name bucket is locked.
1126 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1131 dns_adbfind_t *find;
1132 dns_adbfind_t *next_find;
1133 isc_boolean_t process;
1134 unsigned int wanted, notify;
1137 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1138 name, evtype, addrs);
1140 find = ISC_LIST_HEAD(name->finds);
1141 while (find != NULL) {
1143 next_find = ISC_LIST_NEXT(find, plink);
1145 process = ISC_FALSE;
1146 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1147 notify = wanted & addrs;
1150 case DNS_EVENT_ADBMOREADDRESSES:
1151 DP(3, "DNS_EVENT_ADBMOREADDRESSES");
1152 if ((notify) != 0) {
1153 find->flags &= ~addrs;
1157 case DNS_EVENT_ADBNOMOREADDRESSES:
1158 DP(3, "DNS_EVENT_ADBNOMOREADDRESSES");
1159 find->flags &= ~addrs;
1160 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1165 find->flags &= ~addrs;
1170 DP(DEF_LEVEL, "cfan: processing find %p", find);
1172 * Unlink the find from the name, letting the caller
1173 * call dns_adb_destroyfind() on it to clean it up
1176 ISC_LIST_UNLINK(name->finds, find, plink);
1177 find->adbname = NULL;
1178 find->name_bucket = DNS_ADB_INVALIDBUCKET;
1180 INSIST(!FIND_EVENTSENT(find));
1183 task = ev->ev_sender;
1184 ev->ev_sender = find;
1185 find->result_v4 = find_err_map[name->fetch_err];
1186 find->result_v6 = find_err_map[name->fetch6_err];
1187 ev->ev_type = evtype;
1188 ev->ev_destroy = event_free;
1189 ev->ev_destroy_arg = find;
1192 "Sending event %p to task %p for find %p",
1195 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1197 DP(DEF_LEVEL, "cfan: skipping find %p", find);
1200 UNLOCK(&find->lock);
1204 DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1208 check_exit(dns_adb_t *adb) {
1211 * The caller must be holding the adb lock.
1213 if (adb->shutting_down) {
1215 * If there aren't any external references either, we're
1216 * done. Send the control event to initiate shutdown.
1218 INSIST(!adb->cevent_sent); /* Sanity check. */
1219 event = &adb->cevent;
1220 isc_task_send(adb->task, &event);
1221 adb->cevent_sent = ISC_TRUE;
1225 static inline isc_boolean_t
1226 dec_adb_irefcnt(dns_adb_t *adb) {
1229 isc_boolean_t result = ISC_FALSE;
1231 LOCK(&adb->reflock);
1233 INSIST(adb->irefcnt > 0);
1236 if (adb->irefcnt == 0) {
1237 event = ISC_LIST_HEAD(adb->whenshutdown);
1238 while (event != NULL) {
1239 ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1240 etask = event->ev_sender;
1241 event->ev_sender = adb;
1242 isc_task_sendanddetach(&etask, &event);
1243 event = ISC_LIST_HEAD(adb->whenshutdown);
1247 if (adb->irefcnt == 0 && adb->erefcnt == 0)
1249 UNLOCK(&adb->reflock);
1254 inc_adb_irefcnt(dns_adb_t *adb) {
1255 LOCK(&adb->reflock);
1257 UNLOCK(&adb->reflock);
1261 inc_adb_erefcnt(dns_adb_t *adb) {
1262 LOCK(&adb->reflock);
1264 UNLOCK(&adb->reflock);
1268 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1271 bucket = entry->lock_bucket;
1274 LOCK(&adb->entrylocks[bucket]);
1279 UNLOCK(&adb->entrylocks[bucket]);
1282 static inline isc_boolean_t
1283 dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1285 isc_boolean_t destroy_entry;
1286 isc_boolean_t result = ISC_FALSE;
1288 bucket = entry->lock_bucket;
1291 LOCK(&adb->entrylocks[bucket]);
1293 INSIST(entry->refcnt > 0);
1296 destroy_entry = ISC_FALSE;
1297 if (entry->refcnt == 0 &&
1298 (adb->entry_sd[bucket] || entry->expires == 0)) {
1299 destroy_entry = ISC_TRUE;
1300 result = unlink_entry(adb, entry);
1304 UNLOCK(&adb->entrylocks[bucket]);
1309 entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1311 free_adbentry(adb, &entry);
1313 result =dec_adb_irefcnt(adb);
1318 static inline dns_adbname_t *
1319 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1320 dns_adbname_t *name;
1322 name = isc_mempool_get(adb->nmp);
1326 dns_name_init(&name->name, NULL);
1327 if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1328 isc_mempool_put(adb->nmp, name);
1331 dns_name_init(&name->target, NULL);
1332 name->magic = DNS_ADBNAME_MAGIC;
1334 name->partial_result = 0;
1336 name->expire_v4 = INT_MAX;
1337 name->expire_v6 = INT_MAX;
1338 name->expire_target = INT_MAX;
1340 name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1341 ISC_LIST_INIT(name->v4);
1342 ISC_LIST_INIT(name->v6);
1343 name->fetch_a = NULL;
1344 name->fetch_aaaa = NULL;
1345 ISC_LIST_INIT(name->fetches_a6);
1346 name->fetch_err = FIND_ERR_UNEXPECTED;
1347 name->fetch6_err = FIND_ERR_UNEXPECTED;
1348 ISC_LIST_INIT(name->finds);
1349 ISC_LINK_INIT(name, plink);
1355 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1358 INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1362 INSIST(!NAME_HAS_V4(n));
1363 INSIST(!NAME_HAS_V6(n));
1364 INSIST(!NAME_FETCH(n));
1365 INSIST(ISC_LIST_EMPTY(n->finds));
1366 INSIST(!ISC_LINK_LINKED(n, plink));
1367 INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1368 INSIST(n->adb == adb);
1371 dns_name_free(&n->name, adb->mctx);
1373 isc_mempool_put(adb->nmp, n);
1376 static inline dns_adbnamehook_t *
1377 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1378 dns_adbnamehook_t *nh;
1380 nh = isc_mempool_get(adb->nhmp);
1384 nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1386 ISC_LINK_INIT(nh, plink);
1392 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1393 dns_adbnamehook_t *nh;
1395 INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1399 INSIST(nh->entry == NULL);
1400 INSIST(!ISC_LINK_LINKED(nh, plink));
1403 isc_mempool_put(adb->nhmp, nh);
1406 static inline dns_adbzoneinfo_t *
1407 new_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
1408 dns_adbzoneinfo_t *zi;
1410 zi = isc_mempool_get(adb->zimp);
1414 dns_name_init(&zi->zone, NULL);
1415 if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
1416 isc_mempool_put(adb->zimp, zi);
1420 zi->magic = DNS_ADBZONEINFO_MAGIC;
1422 ISC_LINK_INIT(zi, plink);
1428 free_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
1429 dns_adbzoneinfo_t *zi;
1431 INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
1435 INSIST(!ISC_LINK_LINKED(zi, plink));
1437 dns_name_free(&zi->zone, adb->mctx);
1441 isc_mempool_put(adb->zimp, zi);
1444 static inline dns_adbentry_t *
1445 new_adbentry(dns_adb_t *adb) {
1449 e = isc_mempool_get(adb->emp);
1453 e->magic = DNS_ADBENTRY_MAGIC;
1454 e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1458 e->srtt = (r & 0x1f) + 1;
1460 ISC_LIST_INIT(e->zoneinfo);
1461 ISC_LINK_INIT(e, plink);
1467 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1469 dns_adbzoneinfo_t *zi;
1471 INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1475 INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1476 INSIST(e->refcnt == 0);
1477 INSIST(!ISC_LINK_LINKED(e, plink));
1481 zi = ISC_LIST_HEAD(e->zoneinfo);
1482 while (zi != NULL) {
1483 ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
1484 free_adbzoneinfo(adb, &zi);
1485 zi = ISC_LIST_HEAD(e->zoneinfo);
1488 isc_mempool_put(adb->emp, e);
1491 static inline dns_adbfind_t *
1492 new_adbfind(dns_adb_t *adb) {
1494 isc_result_t result;
1496 h = isc_mempool_get(adb->ahmp);
1505 h->partial_result = 0;
1508 h->result_v4 = ISC_R_UNEXPECTED;
1509 h->result_v6 = ISC_R_UNEXPECTED;
1510 ISC_LINK_INIT(h, publink);
1511 ISC_LINK_INIT(h, plink);
1512 ISC_LIST_INIT(h->list);
1514 h->name_bucket = DNS_ADB_INVALIDBUCKET;
1519 result = isc_mutex_init(&h->lock);
1520 if (result != ISC_R_SUCCESS) {
1521 UNEXPECTED_ERROR(__FILE__, __LINE__,
1522 "isc_mutex_init failed in new_adbfind()");
1523 isc_mempool_put(adb->ahmp, h);
1527 ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
1530 inc_adb_irefcnt(adb);
1531 h->magic = DNS_ADBFIND_MAGIC;
1535 static inline dns_adbfetch_t *
1536 new_adbfetch(dns_adb_t *adb) {
1539 f = isc_mempool_get(adb->afmp);
1548 f->namehook = new_adbnamehook(adb, NULL);
1549 if (f->namehook == NULL)
1552 f->entry = new_adbentry(adb);
1553 if (f->entry == NULL)
1556 dns_rdataset_init(&f->rdataset);
1558 f->magic = DNS_ADBFETCH_MAGIC;
1563 if (f->namehook != NULL)
1564 free_adbnamehook(adb, &f->namehook);
1565 if (f->entry != NULL)
1566 free_adbentry(adb, &f->entry);
1567 isc_mempool_put(adb->afmp, f);
1572 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1575 INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1581 if (f->namehook != NULL)
1582 free_adbnamehook(adb, &f->namehook);
1583 if (f->entry != NULL)
1584 free_adbentry(adb, &f->entry);
1586 if (dns_rdataset_isassociated(&f->rdataset))
1587 dns_rdataset_disassociate(&f->rdataset);
1589 isc_mempool_put(adb->afmp, f);
1593 * Caller must be holding the name lock.
1596 a6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type, isc_stdtime_t now,
1597 dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
1599 dns_adbname_t *name;
1601 isc_result_t result;
1604 INSIST(DNS_ADBNAME_VALID(name));
1606 INSIST(DNS_ADB_VALID(adb));
1608 result = dns_view_simplefind(adb->view, a6name, type, now,
1609 DNS_DBFIND_GLUEOK, ISC_FALSE,
1610 rdataset, sigrdataset);
1611 if (result == DNS_R_GLUE)
1612 result = ISC_R_SUCCESS;
1617 * Caller must be holding the name lock.
1620 a6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
1621 dns_adbname_t *name;
1623 dns_adbfetch6_t *fetch;
1624 isc_result_t result;
1627 INSIST(DNS_ADBNAME_VALID(name));
1629 INSIST(DNS_ADB_VALID(adb));
1631 fetch = new_adbfetch6(adb, name, a6ctx);
1632 if (fetch == NULL) {
1633 name->partial_result |= DNS_ADBFIND_INET6;
1637 result = dns_resolver_createfetch(adb->view->resolver, a6name,
1639 NULL, NULL, NULL, 0,
1640 adb->task, fetch_callback_a6,
1641 name, &fetch->rdataset, NULL,
1643 if (result != ISC_R_SUCCESS)
1646 name->chains = a6ctx->chains;
1647 ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
1650 if (result != ISC_R_SUCCESS) {
1651 free_adbfetch6(adb, &fetch);
1652 name->partial_result |= DNS_ADBFIND_INET6;
1656 static inline dns_adbfetch6_t *
1657 new_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx) {
1660 f = isc_mempool_get(adb->af6mp);
1670 f->namehook = new_adbnamehook(adb, NULL);
1671 if (f->namehook == NULL)
1674 f->entry = new_adbentry(adb);
1675 if (f->entry == NULL)
1678 dns_rdataset_init(&f->rdataset);
1680 dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
1683 dns_a6_copy(a6ctx, &f->a6ctx);
1685 ISC_LINK_INIT(f, plink);
1686 f->magic = DNS_ADBFETCH6_MAGIC;
1691 if (f->namehook != NULL)
1692 free_adbnamehook(adb, &f->namehook);
1693 if (f->entry != NULL)
1694 free_adbentry(adb, &f->entry);
1695 isc_mempool_put(adb->af6mp, f);
1700 free_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch) {
1703 INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
1709 if (f->namehook != NULL)
1710 free_adbnamehook(adb, &f->namehook);
1711 if (f->entry != NULL)
1712 free_adbentry(adb, &f->entry);
1714 if (dns_rdataset_isassociated(&f->rdataset))
1715 dns_rdataset_disassociate(&f->rdataset);
1717 isc_mempool_put(adb->af6mp, f);
1720 static inline isc_boolean_t
1721 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1722 dns_adbfind_t *find;
1724 INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1728 INSIST(!FIND_HAS_ADDRS(find));
1729 INSIST(!ISC_LINK_LINKED(find, publink));
1730 INSIST(!ISC_LINK_LINKED(find, plink));
1731 INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1732 INSIST(find->adbname == NULL);
1736 DESTROYLOCK(&find->lock);
1737 isc_mempool_put(adb->ahmp, find);
1738 return (dec_adb_irefcnt(adb));
1742 * Copy bits from the entry into the newly allocated addrinfo. The entry
1743 * must be locked, and the reference count must be bumped up by one
1744 * if this function returns a valid pointer.
1746 static inline dns_adbaddrinfo_t *
1747 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1748 dns_adbaddrinfo_t *ai;
1750 ai = isc_mempool_get(adb->aimp);
1754 ai->magic = DNS_ADBADDRINFO_MAGIC;
1755 ai->sockaddr = entry->sockaddr;
1756 isc_sockaddr_setport(&ai->sockaddr, port);
1757 ai->srtt = entry->srtt;
1758 ai->flags = entry->flags;
1760 ISC_LINK_INIT(ai, publink);
1766 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1767 dns_adbaddrinfo_t *ai;
1769 INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1773 INSIST(ai->entry == NULL);
1774 INSIST(!ISC_LINK_LINKED(ai, publink));
1778 isc_mempool_put(adb->aimp, ai);
1782 * Search for the name. NOTE: The bucket is kept locked on both
1783 * success and failure, so it must always be unlocked by the caller!
1785 * On the first call to this function, *bucketp must be set to
1786 * DNS_ADB_INVALIDBUCKET.
1788 static inline dns_adbname_t *
1789 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1790 unsigned int options, int *bucketp)
1792 dns_adbname_t *adbname;
1795 bucket = dns_fullname_hash(name, ISC_FALSE) % NBUCKETS;
1797 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1798 LOCK(&adb->namelocks[bucket]);
1800 } else if (*bucketp != bucket) {
1801 UNLOCK(&adb->namelocks[*bucketp]);
1802 LOCK(&adb->namelocks[bucket]);
1806 adbname = ISC_LIST_HEAD(adb->names[bucket]);
1807 while (adbname != NULL) {
1808 if (!NAME_DEAD(adbname)) {
1809 if (dns_name_equal(name, &adbname->name)
1810 && GLUEHINT_OK(adbname, options)
1811 && STARTATZONE_MATCHES(adbname, options))
1814 adbname = ISC_LIST_NEXT(adbname, plink);
1821 * Search for the address. NOTE: The bucket is kept locked on both
1822 * success and failure, so it must always be unlocked by the caller.
1824 * On the first call to this function, *bucketp must be set to
1825 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1826 * later calls (within the same "lock path") it can be left alone, so
1827 * if this function is called multiple times locking is only done if
1828 * the bucket changes.
1830 static inline dns_adbentry_t *
1831 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
1832 dns_adbentry_t *entry;
1835 bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1837 if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1838 LOCK(&adb->entrylocks[bucket]);
1840 } else if (*bucketp != bucket) {
1841 UNLOCK(&adb->entrylocks[*bucketp]);
1842 LOCK(&adb->entrylocks[bucket]);
1846 entry = ISC_LIST_HEAD(adb->entries[bucket]);
1847 while (entry != NULL) {
1848 if (isc_sockaddr_equal(addr, &entry->sockaddr))
1850 entry = ISC_LIST_NEXT(entry, plink);
1857 * Entry bucket MUST be locked!
1859 static isc_boolean_t
1860 entry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
1863 dns_adbzoneinfo_t *zi, *next_zi;
1864 isc_boolean_t is_bad;
1868 zi = ISC_LIST_HEAD(entry->zoneinfo);
1871 while (zi != NULL) {
1872 next_zi = ISC_LIST_NEXT(zi, plink);
1875 * Has the entry expired?
1877 if (zi->lame_timer < now) {
1878 ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
1879 free_adbzoneinfo(adb, &zi);
1883 * Order tests from least to most expensive.
1885 if (zi != NULL && !is_bad) {
1886 if (dns_name_equal(zone, &zi->zone))
1897 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
1898 dns_adbname_t *name, isc_stdtime_t now)
1900 dns_adbnamehook_t *namehook;
1901 dns_adbaddrinfo_t *addrinfo;
1902 dns_adbentry_t *entry;
1905 bucket = DNS_ADB_INVALIDBUCKET;
1907 if (find->options & DNS_ADBFIND_INET) {
1908 namehook = ISC_LIST_HEAD(name->v4);
1909 while (namehook != NULL) {
1910 entry = namehook->entry;
1911 bucket = entry->lock_bucket;
1912 LOCK(&adb->entrylocks[bucket]);
1914 if (!FIND_RETURNLAME(find)
1915 && entry_is_bad_for_zone(adb, entry, zone, now)) {
1916 find->options |= DNS_ADBFIND_LAMEPRUNED;
1919 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1920 if (addrinfo == NULL) {
1921 find->partial_result |= DNS_ADBFIND_INET;
1925 * Found a valid entry. Add it to the find's list.
1927 inc_entry_refcnt(adb, entry, ISC_FALSE);
1928 ISC_LIST_APPEND(find->list, addrinfo, publink);
1931 UNLOCK(&adb->entrylocks[bucket]);
1932 bucket = DNS_ADB_INVALIDBUCKET;
1933 namehook = ISC_LIST_NEXT(namehook, plink);
1937 if (find->options & DNS_ADBFIND_INET6) {
1938 namehook = ISC_LIST_HEAD(name->v6);
1939 while (namehook != NULL) {
1940 entry = namehook->entry;
1941 bucket = entry->lock_bucket;
1942 LOCK(&adb->entrylocks[bucket]);
1944 if (entry_is_bad_for_zone(adb, entry, zone, now))
1946 addrinfo = new_adbaddrinfo(adb, entry, find->port);
1947 if (addrinfo == NULL) {
1948 find->partial_result |= DNS_ADBFIND_INET6;
1952 * Found a valid entry. Add it to the find's list.
1954 inc_entry_refcnt(adb, entry, ISC_FALSE);
1955 ISC_LIST_APPEND(find->list, addrinfo, publink);
1958 UNLOCK(&adb->entrylocks[bucket]);
1959 bucket = DNS_ADB_INVALIDBUCKET;
1960 namehook = ISC_LIST_NEXT(namehook, plink);
1965 if (bucket != DNS_ADB_INVALIDBUCKET)
1966 UNLOCK(&adb->entrylocks[bucket]);
1970 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1976 INSIST(DNS_ADB_VALID(adb));
1979 * Kill the timer, and then the ADB itself. Note that this implies
1980 * that this task was the one scheduled to get timer events. If
1981 * this is not true (and it is unfortunate there is no way to INSIST()
1982 * this) badness will occur.
1985 isc_timer_detach(&adb->timer);
1987 isc_event_free(&ev);
1992 * name bucket must be locked; adb may be locked; no other locks held.
1994 static isc_boolean_t
1995 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1996 dns_adbname_t *name;
1997 isc_result_t result = ISC_FALSE;
1999 INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
2002 if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
2004 if (NAME_FETCH(name))
2006 if (!EXPIRE_OK(name->expire_v4, now))
2008 if (!EXPIRE_OK(name->expire_v6, now))
2010 if (!EXPIRE_OK(name->expire_target, now))
2014 * The name is empty. Delete it.
2016 result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
2020 * Our caller, or one of its callers, will be calling check_exit() at
2021 * some point, so we don't need to do it here.
2027 * entry bucket must be locked; adb may be locked; no other locks held.
2029 static isc_boolean_t
2030 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
2032 dns_adbentry_t *entry;
2033 isc_boolean_t result = ISC_FALSE;
2035 INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
2038 if (entry->refcnt != 0)
2040 if (entry->expires == 0 || entry->expires > now)
2044 * The entry is not in use. Delete it.
2046 DP(DEF_LEVEL, "killing entry %p", entry);
2047 INSIST(ISC_LINK_LINKED(entry, plink));
2048 result = unlink_entry(adb, entry);
2049 free_adbentry(adb, &entry);
2051 dec_adb_irefcnt(adb);
2057 * ADB must be locked, and no other locks held.
2059 static isc_boolean_t
2060 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
2061 dns_adbname_t *name;
2062 dns_adbname_t *next_name;
2063 isc_result_t result = ISC_FALSE;
2065 DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
2067 LOCK(&adb->namelocks[bucket]);
2068 if (adb->name_sd[bucket]) {
2069 UNLOCK(&adb->namelocks[bucket]);
2073 name = ISC_LIST_HEAD(adb->names[bucket]);
2074 while (name != NULL) {
2075 next_name = ISC_LIST_NEXT(name, plink);
2076 INSIST(result == ISC_FALSE);
2077 result = check_expire_namehooks(name, now);
2079 result = check_expire_name(&name, now);
2082 UNLOCK(&adb->namelocks[bucket]);
2087 * ADB must be locked, and no other locks held.
2089 static isc_boolean_t
2090 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
2091 dns_adbentry_t *entry, *next_entry;
2092 isc_boolean_t result = ISC_FALSE;
2094 DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
2096 LOCK(&adb->entrylocks[bucket]);
2097 entry = ISC_LIST_HEAD(adb->entries[bucket]);
2098 while (entry != NULL) {
2099 next_entry = ISC_LIST_NEXT(entry, plink);
2100 INSIST(result == ISC_FALSE);
2101 result = check_expire_entry(adb, &entry, now);
2104 UNLOCK(&adb->entrylocks[bucket]);
2109 timer_cleanup(isc_task_t *task, isc_event_t *ev) {
2117 INSIST(DNS_ADB_VALID(adb));
2121 isc_stdtime_get(&now);
2123 for (i = 0 ; i < CLEAN_BUCKETS ; i++) {
2125 * Call our cleanup routines.
2127 RUNTIME_CHECK(cleanup_names(adb, adb->next_cleanbucket, now) ==
2129 RUNTIME_CHECK(cleanup_entries(adb, adb->next_cleanbucket, now)
2133 * Set the next bucket to be cleaned.
2135 adb->next_cleanbucket++;
2136 if (adb->next_cleanbucket >= NBUCKETS) {
2137 adb->next_cleanbucket = 0;
2138 #ifdef DUMP_ADB_AFTER_CLEANING
2139 dump_adb(adb, stdout, ISC_TRUE);
2146 * XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
2147 * ISC_R_NOMEMORY, but it isn't clear what could be done here
2148 * if either one of those things happened.
2150 (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
2151 &adb->tick_interval, ISC_FALSE);
2155 isc_event_free(&ev);
2159 destroy(dns_adb_t *adb) {
2163 * The timer is already dead, from the task's shutdown callback.
2165 isc_task_detach(&adb->task);
2167 isc_mempool_destroy(&adb->nmp);
2168 isc_mempool_destroy(&adb->nhmp);
2169 isc_mempool_destroy(&adb->zimp);
2170 isc_mempool_destroy(&adb->emp);
2171 isc_mempool_destroy(&adb->ahmp);
2172 isc_mempool_destroy(&adb->aimp);
2173 isc_mempool_destroy(&adb->afmp);
2174 isc_mempool_destroy(&adb->af6mp);
2176 isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
2177 isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
2179 DESTROYLOCK(&adb->reflock);
2180 DESTROYLOCK(&adb->lock);
2181 DESTROYLOCK(&adb->mplock);
2183 isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
2192 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
2193 isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
2196 isc_result_t result;
2199 REQUIRE(mem != NULL);
2200 REQUIRE(view != NULL);
2201 REQUIRE(timermgr != NULL);
2202 REQUIRE(taskmgr != NULL);
2203 REQUIRE(newadb != NULL && *newadb == NULL);
2205 adb = isc_mem_get(mem, sizeof (dns_adb_t));
2207 return (ISC_R_NOMEMORY);
2210 * Initialize things here that cannot fail, and especially things
2211 * that must be NULL for the error return to work properly.
2228 adb->timermgr = timermgr;
2229 adb->taskmgr = taskmgr;
2230 adb->next_cleanbucket = 0;
2231 ISC_EVENT_INIT(&adb->cevent, sizeof adb->cevent, 0, NULL,
2232 DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2234 adb->cevent_sent = ISC_FALSE;
2235 adb->shutting_down = ISC_FALSE;
2236 ISC_LIST_INIT(adb->whenshutdown);
2238 result = isc_mutex_init(&adb->lock);
2239 if (result != ISC_R_SUCCESS)
2242 result = isc_mutex_init(&adb->mplock);
2243 if (result != ISC_R_SUCCESS)
2246 result = isc_mutex_init(&adb->reflock);
2247 if (result != ISC_R_SUCCESS)
2251 * Initialize the bucket locks for names and elements.
2252 * May as well initialize the list heads, too.
2254 result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2255 if (result != ISC_R_SUCCESS)
2257 for (i = 0 ; i < NBUCKETS ; i++) {
2258 ISC_LIST_INIT(adb->names[i]);
2259 adb->name_sd[i] = ISC_FALSE;
2260 adb->name_refcnt[i] = 0;
2263 for (i = 0 ; i < NBUCKETS ; i++) {
2264 ISC_LIST_INIT(adb->entries[i]);
2265 adb->entry_sd[i] = ISC_FALSE;
2266 adb->entry_refcnt[i] = 0;
2269 result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2270 if (result != ISC_R_SUCCESS)
2276 #define MPINIT(t, p, n) do { \
2277 result = isc_mempool_create(mem, sizeof (t), &(p)); \
2278 if (result != ISC_R_SUCCESS) \
2280 isc_mempool_setfreemax((p), FREE_ITEMS); \
2281 isc_mempool_setfillcount((p), FILL_COUNT); \
2282 isc_mempool_setname((p), n); \
2283 isc_mempool_associatelock((p), &adb->mplock); \
2286 MPINIT(dns_adbname_t, adb->nmp, "adbname");
2287 MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2288 MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
2289 MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2290 MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2291 MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2292 MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2293 MPINIT(dns_adbfetch6_t, adb->af6mp, "adbfetch6");
2298 * Allocate a timer and a task for our periodic cleanup.
2300 result = isc_task_create(adb->taskmgr, 0, &adb->task);
2301 if (result != ISC_R_SUCCESS)
2303 isc_task_setname(adb->task, "ADB", adb);
2305 * XXXMLG When this is changed to be a config file option,
2307 isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
2308 result = isc_timer_create(adb->timermgr, isc_timertype_once,
2309 NULL, &adb->tick_interval, adb->task,
2310 timer_cleanup, adb, &adb->timer);
2311 if (result != ISC_R_SUCCESS)
2315 "Cleaning interval for adb: "
2316 "%u buckets every %u seconds, %u buckets in system, %u cl.interval",
2317 CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
2322 adb->magic = DNS_ADB_MAGIC;
2324 return (ISC_R_SUCCESS);
2327 if (adb->task != NULL)
2328 isc_task_detach(&adb->task);
2329 if (adb->timer != NULL)
2330 isc_timer_detach(&adb->timer);
2332 /* clean up entrylocks */
2333 isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
2335 fail2: /* clean up namelocks */
2336 isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
2338 fail1: /* clean up only allocated memory */
2339 if (adb->nmp != NULL)
2340 isc_mempool_destroy(&adb->nmp);
2341 if (adb->nhmp != NULL)
2342 isc_mempool_destroy(&adb->nhmp);
2343 if (adb->zimp != NULL)
2344 isc_mempool_destroy(&adb->zimp);
2345 if (adb->emp != NULL)
2346 isc_mempool_destroy(&adb->emp);
2347 if (adb->ahmp != NULL)
2348 isc_mempool_destroy(&adb->ahmp);
2349 if (adb->aimp != NULL)
2350 isc_mempool_destroy(&adb->aimp);
2351 if (adb->afmp != NULL)
2352 isc_mempool_destroy(&adb->afmp);
2353 if (adb->af6mp != NULL)
2354 isc_mempool_destroy(&adb->af6mp);
2356 DESTROYLOCK(&adb->reflock);
2358 DESTROYLOCK(&adb->mplock);
2360 DESTROYLOCK(&adb->lock);
2362 isc_mem_put(mem, adb, sizeof (dns_adb_t));
2368 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2370 REQUIRE(DNS_ADB_VALID(adb));
2371 REQUIRE(adbx != NULL && *adbx == NULL);
2373 inc_adb_erefcnt(adb);
2378 dns_adb_detach(dns_adb_t **adbx) {
2380 isc_boolean_t need_exit_check;
2382 REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2387 INSIST(adb->erefcnt > 0);
2389 LOCK(&adb->reflock);
2391 need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2392 UNLOCK(&adb->reflock);
2394 if (need_exit_check) {
2396 INSIST(adb->shutting_down);
2403 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2406 isc_boolean_t zeroirefcnt = ISC_FALSE;
2409 * Send '*eventp' to 'task' when 'adb' has shutdown.
2412 REQUIRE(DNS_ADB_VALID(adb));
2413 REQUIRE(eventp != NULL);
2420 LOCK(&adb->reflock);
2421 zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2423 if (adb->shutting_down && zeroirefcnt &&
2424 isc_mempool_getallocated(adb->ahmp) == 0) {
2426 * We're already shutdown. Send the event.
2428 event->ev_sender = adb;
2429 isc_task_send(task, &event);
2432 isc_task_attach(task, &clone);
2433 event->ev_sender = clone;
2434 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2437 UNLOCK(&adb->reflock);
2442 dns_adb_shutdown(dns_adb_t *adb) {
2443 isc_boolean_t need_check_exit;
2451 if (!adb->shutting_down) {
2452 adb->shutting_down = ISC_TRUE;
2453 need_check_exit = shutdown_names(adb);
2454 if (!need_check_exit)
2455 need_check_exit = shutdown_entries(adb);
2456 if (need_check_exit)
2464 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2465 void *arg, dns_name_t *name, dns_name_t *zone,
2466 unsigned int options, isc_stdtime_t now, dns_name_t *target,
2467 in_port_t port, dns_adbfind_t **findp)
2469 dns_adbfind_t *find;
2470 dns_adbname_t *adbname;
2472 isc_boolean_t want_event, start_at_zone, alias, have_address;
2473 isc_result_t result;
2474 unsigned int wanted_addresses;
2475 unsigned int wanted_fetches;
2476 unsigned int query_pending;
2478 REQUIRE(DNS_ADB_VALID(adb));
2480 REQUIRE(action != NULL);
2482 REQUIRE(name != NULL);
2483 REQUIRE(zone != NULL);
2484 REQUIRE(findp != NULL && *findp == NULL);
2485 REQUIRE(target == NULL || dns_name_hasbuffer(target));
2487 REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2489 result = ISC_R_UNEXPECTED;
2490 wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2493 want_event = ISC_FALSE;
2494 start_at_zone = ISC_FALSE;
2498 isc_stdtime_get(&now);
2501 * XXXMLG Move this comment somewhere else!
2503 * Look up the name in our internal database.
2505 * Possibilities: Note that these are not always exclusive.
2507 * No name found. In this case, allocate a new name header and
2508 * an initial namehook or two. If any of these allocations
2509 * fail, clean up and return ISC_R_NOMEMORY.
2511 * Name found, valid addresses present. Allocate one addrinfo
2512 * structure for each found and append it to the linked list
2513 * of addresses for this header.
2515 * Name found, queries pending. In this case, if a task was
2516 * passed in, allocate a job id, attach it to the name's job
2517 * list and remember to tell the caller that there will be
2518 * more info coming later.
2521 find = new_adbfind(adb);
2523 return (ISC_R_NOMEMORY);
2528 * Remember what types of addresses we are interested in.
2530 find->options = options;
2531 find->flags |= wanted_addresses;
2532 if (FIND_WANTEVENT(find)) {
2533 REQUIRE(task != NULL);
2537 * Try to see if we know anything about this name at all.
2539 bucket = DNS_ADB_INVALIDBUCKET;
2540 adbname = find_name_and_lock(adb, name, find->options, &bucket);
2541 if (adb->name_sd[bucket]) {
2543 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2544 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2545 result = ISC_R_SHUTTINGDOWN;
2550 * Nothing found. Allocate a new adbname structure for this name.
2552 if (adbname == NULL) {
2553 adbname = new_adbname(adb, name);
2554 if (adbname == NULL) {
2555 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2556 result = ISC_R_NOMEMORY;
2559 link_name(adb, bucket, adbname);
2560 if (FIND_HINTOK(find))
2561 adbname->flags |= NAME_HINT_OK;
2562 if (FIND_GLUEOK(find))
2563 adbname->flags |= NAME_GLUE_OK;
2564 if (FIND_STARTATZONE(find))
2565 adbname->flags |= NAME_STARTATZONE;
2569 * Expire old entries, etc.
2571 RUNTIME_CHECK(check_expire_namehooks(adbname, now) == ISC_FALSE);
2574 * Do we know that the name is an alias?
2576 if (!EXPIRE_OK(adbname->expire_target, now)) {
2581 "dns_adb_createfind: name %p is an alias (cached)",
2588 * Try to populate the name from the database and/or
2589 * start fetches. First try looking for an A record
2592 if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2593 && WANT_INET(wanted_addresses)) {
2594 result = dbfind_name(adbname, now, dns_rdatatype_a);
2595 if (result == ISC_R_SUCCESS) {
2597 "dns_adb_createfind: found A for name %p in db",
2603 * Did we get a CNAME or DNAME?
2605 if (result == DNS_R_ALIAS) {
2607 "dns_adb_createfind: name %p is an alias",
2614 * If the name doesn't exist at all, don't bother with
2615 * v6 queries; they won't work.
2617 * If the name does exist but we didn't get our data, go
2620 * If the result is neither of these, try a fetch for A.
2622 if (NXDOMAIN_RESULT(result))
2624 else if (NXRRSET_RESULT(result))
2627 if (!NAME_FETCH_V4(adbname))
2628 wanted_fetches |= DNS_ADBFIND_INET;
2632 if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2633 && WANT_INET6(wanted_addresses)) {
2634 result = dbfind_a6(adbname, now);
2635 if (result == ISC_R_SUCCESS) {
2637 "dns_adb_createfind: found A6 for name %p",
2643 * Did we get a CNAME or DNAME?
2645 if (result == DNS_R_ALIAS) {
2647 "dns_adb_createfind: name %p is an alias",
2654 * If the name doesn't exist at all, jump to the fetch
2655 * code. Otherwise, we'll try AAAA.
2657 if (NXDOMAIN_RESULT(result))
2660 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2661 if (result == ISC_R_SUCCESS) {
2663 "dns_adb_createfind: found AAAA for name %p",
2669 * Did we get a CNAME or DNAME? This should have hit
2670 * during the A6 query, but we'll reproduce it here Just
2673 if (result == DNS_R_ALIAS) {
2675 "dns_adb_createfind: name %p is an alias",
2682 * Listen to negative cache hints, and don't start
2685 if (NCACHE_RESULT(result) || AUTH_NX(result))
2688 if (!NAME_FETCH_V6(adbname))
2689 wanted_fetches |= DNS_ADBFIND_INET6;
2693 if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2694 (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2695 have_address = ISC_TRUE;
2697 have_address = ISC_FALSE;
2698 if (wanted_fetches != 0 &&
2699 ! (FIND_AVOIDFETCHES(find) && have_address)) {
2701 * We're missing at least one address family. Either the
2702 * caller hasn't instructed us to avoid fetches, or we don't
2703 * know anything about any of the address families that would
2704 * be acceptable so we have to launch fetches.
2707 if (FIND_STARTATZONE(find))
2708 start_at_zone = ISC_TRUE;
2713 if (WANT_INET(wanted_fetches) &&
2714 fetch_name_v4(adbname, start_at_zone) == ISC_R_SUCCESS) {
2716 "dns_adb_createfind: started A fetch for name %p",
2723 if (WANT_INET6(wanted_fetches) &&
2724 fetch_name_a6(adbname, start_at_zone) == ISC_R_SUCCESS) {
2726 "dns_adb_createfind: started A6 fetch for name %p",
2732 * Run through the name and copy out the bits we are
2735 copy_namehook_lists(adb, find, zone, adbname, now);
2738 if (NAME_FETCH_V4(adbname))
2739 query_pending |= DNS_ADBFIND_INET;
2740 if (NAME_FETCH_V6(adbname))
2741 query_pending |= DNS_ADBFIND_INET6;
2744 * Attach to the name's query list if there are queries
2745 * already running, and we have been asked to.
2747 want_event = ISC_TRUE;
2748 if (!FIND_WANTEVENT(find))
2749 want_event = ISC_FALSE;
2750 if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2751 want_event = ISC_FALSE;
2752 if ((wanted_addresses & query_pending) == 0)
2753 want_event = ISC_FALSE;
2755 want_event = ISC_FALSE;
2757 find->adbname = adbname;
2758 find->name_bucket = bucket;
2759 ISC_LIST_APPEND(adbname->finds, find, plink);
2760 find->query_pending = (query_pending & wanted_addresses);
2761 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2762 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2763 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2767 * Remove the flag so the caller knows there will never
2768 * be an event, and set internal flags to fake that
2769 * the event was sent and freed, so dns_adb_destroyfind() will
2770 * do the right thing.
2772 find->query_pending = (query_pending & wanted_addresses);
2773 find->options &= ~DNS_ADBFIND_WANTEVENT;
2774 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2775 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2778 find->partial_result |= (adbname->partial_result & wanted_addresses);
2780 if (target != NULL) {
2781 result = dns_name_copy(&adbname->target, target, NULL);
2782 if (result != ISC_R_SUCCESS)
2785 result = DNS_R_ALIAS;
2787 result = ISC_R_SUCCESS;
2790 * Copy out error flags from the name structure into the find.
2792 find->result_v4 = find_err_map[adbname->fetch_err];
2793 find->result_v6 = find_err_map[adbname->fetch6_err];
2802 INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2804 isc_task_attach(task, &taskp);
2805 find->event.ev_sender = taskp;
2806 find->event.ev_action = action;
2807 find->event.ev_arg = arg;
2811 if (bucket != DNS_ADB_INVALIDBUCKET)
2812 UNLOCK(&adb->namelocks[bucket]);
2818 dns_adb_destroyfind(dns_adbfind_t **findp) {
2819 dns_adbfind_t *find;
2820 dns_adbentry_t *entry;
2821 dns_adbaddrinfo_t *ai;
2825 REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2831 DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2834 REQUIRE(DNS_ADB_VALID(adb));
2836 REQUIRE(FIND_EVENTFREED(find));
2838 bucket = find->name_bucket;
2839 INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2841 UNLOCK(&find->lock);
2844 * The find doesn't exist on any list, and nothing is locked.
2845 * Return the find to the memory pool, and decrement the adb's
2848 ai = ISC_LIST_HEAD(find->list);
2849 while (ai != NULL) {
2850 ISC_LIST_UNLINK(find->list, ai, publink);
2853 INSIST(DNS_ADBENTRY_VALID(entry));
2854 RUNTIME_CHECK(dec_entry_refcnt(adb, entry, ISC_TRUE) ==
2856 free_adbaddrinfo(adb, &ai);
2857 ai = ISC_LIST_HEAD(find->list);
2861 * WARNING: The find is freed with the adb locked. This is done
2862 * to avoid a race condition where we free the find, some other
2863 * thread tests to see if it should be destroyed, detects it should
2864 * be, destroys it, and then we try to lock it for our check, but the
2865 * lock is destroyed.
2868 if (free_adbfind(adb, &find))
2874 dns_adb_cancelfind(dns_adbfind_t *find) {
2883 DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2886 REQUIRE(DNS_ADB_VALID(adb));
2888 REQUIRE(!FIND_EVENTFREED(find));
2889 REQUIRE(FIND_WANTEVENT(find));
2891 bucket = find->name_bucket;
2892 if (bucket == DNS_ADB_INVALIDBUCKET)
2896 * We need to get the adbname's lock to unlink the find.
2898 unlock_bucket = bucket;
2899 violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2900 bucket = find->name_bucket;
2901 if (bucket != DNS_ADB_INVALIDBUCKET) {
2902 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2903 find->adbname = NULL;
2904 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2906 UNLOCK(&adb->namelocks[unlock_bucket]);
2907 bucket = DNS_ADB_INVALIDBUCKET;
2911 if (!FIND_EVENTSENT(find)) {
2913 task = ev->ev_sender;
2914 ev->ev_sender = find;
2915 ev->ev_type = DNS_EVENT_ADBCANCELED;
2916 ev->ev_destroy = event_free;
2917 ev->ev_destroy_arg = find;
2918 find->result_v4 = ISC_R_CANCELED;
2919 find->result_v6 = ISC_R_CANCELED;
2921 DP(DEF_LEVEL, "Sending event %p to task %p for find %p",
2924 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2927 UNLOCK(&find->lock);
2931 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2932 REQUIRE(DNS_ADB_VALID(adb));
2936 * Lock the adb itself, lock all the name buckets, then lock all
2937 * the entry buckets. This should put the adb into a state where
2938 * nothing can change, so we can iterate through everything and
2939 * print at our leisure.
2943 dump_adb(adb, f, ISC_FALSE);
2948 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2949 if (value == INT_MAX)
2951 fprintf(f, " [%s TTL %d]", legend, value - now);
2955 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug) {
2957 dns_adbname_t *name;
2960 isc_stdtime_get(&now);
2962 fprintf(f, ";\n; Address database dump\n;\n");
2964 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2965 adb, adb->erefcnt, adb->irefcnt,
2966 isc_mempool_getallocated(adb->nhmp));
2968 for (i = 0 ; i < NBUCKETS ; i++)
2969 LOCK(&adb->namelocks[i]);
2970 for (i = 0 ; i < NBUCKETS ; i++)
2971 LOCK(&adb->entrylocks[i]);
2976 for (i = 0 ; i < NBUCKETS ; i++) {
2977 name = ISC_LIST_HEAD(adb->names[i]);
2981 fprintf(f, "; bucket %d\n", i);
2984 name = ISC_LIST_NEXT(name, plink))
2987 fprintf(f, "; name %p (flags %08x)\n",
2991 print_dns_name(f, &name->name);
2992 if (dns_name_countlabels(&name->target) > 0) {
2993 fprintf(f, " alias ");
2994 print_dns_name(f, &name->target);
2997 dump_ttl(f, "v4", name->expire_v4, now);
2998 dump_ttl(f, "v6", name->expire_v6, now);
2999 dump_ttl(f, "target", name->expire_target, now);
3001 fprintf(f, " [v4 %s] [v6 %s]",
3002 errnames[name->fetch_err],
3003 errnames[name->fetch6_err]);
3007 print_namehook_list(f, "v4", &name->v4, debug);
3008 print_namehook_list(f, "v6", &name->v6, debug);
3011 print_fetch_list(f, name);
3013 print_find_list(f, name);
3021 for (i = 0; i < NBUCKETS; i++)
3022 UNLOCK(&adb->entrylocks[i]);
3023 for (i = 0; i < NBUCKETS; i++)
3024 UNLOCK(&adb->namelocks[i]);
3028 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug)
3030 char addrbuf[ISC_NETADDR_FORMATSIZE];
3031 isc_netaddr_t netaddr;
3033 isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
3034 isc_netaddr_format(&netaddr, addrbuf, sizeof addrbuf);
3037 fprintf(f, ";\t%p: refcnt %u flags %08x \n",
3038 entry, entry->refcnt, entry->flags);
3040 fprintf(f, ";\t%s [srtt %u]", addrbuf, entry->srtt);
3045 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
3048 dns_adbaddrinfo_t *ai;
3052 * Not used currently, in the API Just In Case we
3053 * want to dump out the name and/or entries too.
3058 fprintf(f, ";Find %p\n", find);
3059 fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
3060 find->query_pending, find->partial_result,
3061 find->options, find->flags);
3062 fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
3063 find->name_bucket, find->adbname, find->event.ev_sender);
3065 ai = ISC_LIST_HEAD(find->list);
3067 fprintf(f, "\tAddresses:\n");
3068 while (ai != NULL) {
3070 switch (sa->type.sa.sa_family) {
3072 tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
3076 tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
3084 tmpp = "BadAddress";
3086 fprintf(f, "\t\tentry %p, flags %08x"
3087 " srtt %u addr %s\n",
3088 ai->entry, ai->flags, ai->srtt, tmpp);
3090 ai = ISC_LIST_NEXT(ai, publink);
3093 UNLOCK(&find->lock);
3097 print_dns_name(FILE *f, dns_name_t *name) {
3098 char buf[DNS_NAME_FORMATSIZE];
3102 dns_name_format(name, buf, sizeof(buf));
3103 fprintf(f, "%s", buf);
3107 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
3108 isc_boolean_t debug)
3110 dns_adbnamehook_t *nh;
3112 for (nh = ISC_LIST_HEAD(*list);
3114 nh = ISC_LIST_NEXT(nh, plink))
3117 fprintf(f, ";\tHook(%s) %p\n", legend, nh);
3118 dump_entry(f, nh->entry, debug);
3123 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
3124 fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
3125 type, ft, ft->namehook, ft->entry, ft->fetch);
3129 print_fetch6(FILE *f, dns_adbfetch6_t *ft) {
3130 fprintf(f, "\t\tFetch(A6): %p -> { nh %p, entry %p, fetch %p }\n",
3131 ft, ft->namehook, ft->entry, ft->fetch);
3135 print_fetch_list(FILE *f, dns_adbname_t *n) {
3136 dns_adbfetch6_t *fetch6;
3138 if (NAME_FETCH_A(n))
3139 print_fetch(f, n->fetch_a, "A");
3140 if (NAME_FETCH_AAAA(n))
3141 print_fetch(f, n->fetch_aaaa, "AAAA");
3143 fetch6 = ISC_LIST_HEAD(n->fetches_a6);
3144 while (fetch6 != NULL) {
3145 print_fetch6(f, fetch6);
3146 fetch6 = ISC_LIST_NEXT(fetch6, plink);
3151 print_find_list(FILE *f, dns_adbname_t *name) {
3152 dns_adbfind_t *find;
3154 find = ISC_LIST_HEAD(name->finds);
3155 while (find != NULL) {
3156 dns_adb_dumpfind(find, f);
3157 find = ISC_LIST_NEXT(find, plink);
3162 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
3164 isc_result_t result;
3165 dns_rdataset_t rdataset;
3167 dns_fixedname_t foundname;
3170 INSIST(DNS_ADBNAME_VALID(adbname));
3172 INSIST(DNS_ADB_VALID(adb));
3173 INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
3175 dns_fixedname_init(&foundname);
3176 fname = dns_fixedname_name(&foundname);
3177 dns_rdataset_init(&rdataset);
3179 if (rdtype == dns_rdatatype_a)
3180 adbname->fetch_err = FIND_ERR_UNEXPECTED;
3182 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
3184 result = dns_view_find(adb->view, &adbname->name, rdtype, now,
3185 NAME_GLUEOK(adbname),
3186 ISC_TF(NAME_HINTOK(adbname)),
3187 NULL, NULL, fname, &rdataset, NULL);
3189 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3195 * Found in the database. Even if we can't copy out
3196 * any information, return success, or else a fetch
3197 * will be made, which will only make things worse.
3199 if (rdtype == dns_rdatatype_a)
3200 adbname->fetch_err = FIND_ERR_SUCCESS;
3202 adbname->fetch6_err = FIND_ERR_SUCCESS;
3203 result = import_rdataset(adbname, &rdataset, now);
3205 case DNS_R_NXDOMAIN:
3208 * We're authoritative and the data doesn't exist.
3209 * Make up a negative cache entry so we don't ask again
3212 * XXXRTH What time should we use? I'm putting in 30 seconds
3215 if (rdtype == dns_rdatatype_a) {
3216 adbname->expire_v4 = now + 30;
3218 "adb name %p: Caching auth negative entry for A",
3220 if (result == DNS_R_NXDOMAIN)
3221 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3223 adbname->fetch_err = FIND_ERR_NXRRSET;
3226 "adb name %p: Caching auth negative entry for AAAA",
3228 adbname->expire_v6 = now + 30;
3229 if (result == DNS_R_NXDOMAIN)
3230 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3232 adbname->fetch6_err = FIND_ERR_NXRRSET;
3235 case DNS_R_NCACHENXDOMAIN:
3236 case DNS_R_NCACHENXRRSET:
3238 * We found a negative cache entry. Pull the TTL from it
3239 * so we won't ask again for a while.
3241 rdataset.ttl = ttlclamp(rdataset.ttl);
3242 if (rdtype == dns_rdatatype_a) {
3243 adbname->expire_v4 = rdataset.ttl + now;
3244 if (result == DNS_R_NCACHENXDOMAIN)
3245 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3247 adbname->fetch_err = FIND_ERR_NXRRSET;
3249 "adb name %p: Caching negative entry for A (ttl %u)",
3250 adbname, rdataset.ttl);
3253 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3254 adbname, rdataset.ttl);
3255 adbname->expire_v6 = rdataset.ttl + now;
3256 if (result == DNS_R_NCACHENXDOMAIN)
3257 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3259 adbname->fetch6_err = FIND_ERR_NXRRSET;
3265 * Clear the hint and glue flags, so this will match
3268 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3270 rdataset.ttl = ttlclamp(rdataset.ttl);
3271 clean_target(adb, &adbname->target);
3272 adbname->expire_target = INT_MAX;
3273 result = set_target(adb, &adbname->name, fname, &rdataset,
3275 if (result == ISC_R_SUCCESS) {
3276 result = DNS_R_ALIAS;
3278 "adb name %p: caching alias target",
3280 adbname->expire_target = rdataset.ttl + now;
3282 if (rdtype == dns_rdatatype_a)
3283 adbname->fetch_err = FIND_ERR_SUCCESS;
3285 adbname->fetch6_err = FIND_ERR_SUCCESS;
3289 if (dns_rdataset_isassociated(&rdataset))
3290 dns_rdataset_disassociate(&rdataset);
3296 dbfind_a6(dns_adbname_t *adbname, isc_stdtime_t now) {
3297 isc_result_t result;
3298 dns_rdataset_t rdataset;
3300 dns_a6context_t a6ctx;
3301 dns_fixedname_t foundname;
3304 INSIST(DNS_ADBNAME_VALID(adbname));
3306 INSIST(DNS_ADB_VALID(adb));
3308 result = ISC_R_UNEXPECTED;
3310 dns_fixedname_init(&foundname);
3311 fname = dns_fixedname_name(&foundname);
3312 dns_rdataset_init(&rdataset);
3314 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
3316 result = dns_view_find(adb->view, &adbname->name, dns_rdatatype_a6,
3317 now, NAME_GLUEOK(adbname),
3318 ISC_TF(NAME_HINTOK(adbname)),
3319 NULL, NULL, fname, &rdataset, NULL);
3326 * Start a6 chain follower. There is no need to poke people
3327 * who might be waiting, since this is call requires there
3330 adbname->fetch6_err = FIND_ERR_SUCCESS;
3331 dns_a6_init(&a6ctx, a6find, NULL, import_a6,
3332 a6missing, adbname);
3333 (void)dns_a6_foreach(&a6ctx, &rdataset, now);
3334 adbname->flags &= ~NAME_NEEDS_POKE;
3335 result = ISC_R_SUCCESS;
3337 case DNS_R_NXDOMAIN:
3340 * We're authoritative and the data doesn't exist.
3341 * Make up a negative cache entry so we don't ask again
3344 * XXXRTH What time should we use? I'm putting in 30 seconds
3348 "adb name %p: Caching auth negative entry for A6",
3350 adbname->expire_v6 = now + 30;
3351 if (result == DNS_R_NXDOMAIN)
3352 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3354 adbname->fetch6_err = FIND_ERR_NXRRSET;
3356 case DNS_R_NCACHENXDOMAIN:
3357 case DNS_R_NCACHENXRRSET:
3359 * We found a negative cache entry. Pull the TTL from it
3360 * so we won't ask again for a while.
3363 "adb name %p: Caching negative entry for A6 (ttl %u)",
3364 adbname, rdataset.ttl);
3365 adbname->expire_v6 = ISC_MIN(rdataset.ttl + now,
3366 adbname->expire_v6);
3367 if (result == DNS_R_NCACHENXDOMAIN)
3368 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3370 adbname->fetch6_err = FIND_ERR_NXRRSET;
3374 rdataset.ttl = ttlclamp(rdataset.ttl);
3375 clean_target(adb, &adbname->target);
3376 adbname->expire_target = INT_MAX;
3377 result = set_target(adb, &adbname->name, fname, &rdataset,
3379 if (result == ISC_R_SUCCESS) {
3380 result = DNS_R_ALIAS;
3382 "adb name %p: caching alias target",
3384 adbname->expire_target = rdataset.ttl + now;
3389 if (dns_rdataset_isassociated(&rdataset))
3390 dns_rdataset_disassociate(&rdataset);
3396 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3397 dns_fetchevent_t *dev;
3398 dns_adbname_t *name;
3400 dns_adbfetch_t *fetch;
3402 isc_eventtype_t ev_status;
3404 isc_result_t result;
3405 unsigned int address_type;
3406 isc_boolean_t want_check_exit = ISC_FALSE;
3410 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3411 dev = (dns_fetchevent_t *)ev;
3413 INSIST(DNS_ADBNAME_VALID(name));
3415 INSIST(DNS_ADB_VALID(adb));
3417 bucket = name->lock_bucket;
3418 LOCK(&adb->namelocks[bucket]);
3420 INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3422 if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3423 address_type = DNS_ADBFIND_INET;
3424 fetch = name->fetch_a;
3425 name->fetch_a = NULL;
3426 } else if (NAME_FETCH_AAAA(name)
3427 && (name->fetch_aaaa->fetch == dev->fetch)) {
3428 address_type = DNS_ADBFIND_INET6;
3429 fetch = name->fetch_aaaa;
3430 name->fetch_aaaa = NULL;
3432 INSIST(address_type != 0);
3434 dns_resolver_destroyfetch(&fetch->fetch);
3437 ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3440 * Cleanup things we don't care about.
3442 if (dev->node != NULL)
3443 dns_db_detachnode(dev->db, &dev->node);
3444 if (dev->db != NULL)
3445 dns_db_detach(&dev->db);
3448 * If this name is marked as dead, clean up, throwing away
3449 * potentially good data.
3451 if (NAME_DEAD(name)) {
3452 free_adbfetch(adb, &fetch);
3453 isc_event_free(&ev);
3455 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3457 UNLOCK(&adb->namelocks[bucket]);
3459 if (want_check_exit) {
3468 isc_stdtime_get(&now);
3471 * If we got a negative cache response, remember it.
3473 if (NCACHE_RESULT(dev->result)) {
3474 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3475 if (address_type == DNS_ADBFIND_INET) {
3476 DP(NCACHE_LEVEL, "adb fetch name %p: "
3477 "Caching negative entry for A (ttl %u)",
3478 name, dev->rdataset->ttl);
3479 name->expire_v4 = ISC_MIN(name->expire_v4,
3480 dev->rdataset->ttl + now);
3481 if (dev->result == DNS_R_NCACHENXDOMAIN)
3482 name->fetch_err = FIND_ERR_NXDOMAIN;
3484 name->fetch_err = FIND_ERR_NXRRSET;
3486 DP(NCACHE_LEVEL, "adb fetch name %p: "
3487 "Caching negative entry for AAAA (ttl %u)",
3488 name, dev->rdataset->ttl);
3489 name->expire_v6 = ISC_MIN(name->expire_v6,
3490 dev->rdataset->ttl + now);
3491 if (dev->result == DNS_R_NCACHENXDOMAIN)
3492 name->fetch6_err = FIND_ERR_NXDOMAIN;
3494 name->fetch6_err = FIND_ERR_NXRRSET;
3500 * Handle CNAME/DNAME.
3502 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3503 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3504 clean_target(adb, &name->target);
3505 name->expire_target = INT_MAX;
3506 result = set_target(adb, &name->name,
3507 dns_fixedname_name(&dev->foundname),
3510 if (result == ISC_R_SUCCESS) {
3512 "adb fetch name %p: caching alias target",
3514 name->expire_target = dev->rdataset->ttl + now;
3520 * Did we get back junk? If so, and there are no more fetches
3521 * sitting out there, tell all the finds about it.
3523 if (dev->result != ISC_R_SUCCESS) {
3524 /* XXXMLG Don't pound on bad servers. */
3525 if (address_type == DNS_ADBFIND_INET) {
3526 name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3527 name->fetch_err = FIND_ERR_FAILURE;
3529 name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3530 name->fetch6_err = FIND_ERR_FAILURE;
3536 * We got something potentially useful.
3538 result = import_rdataset(name, &fetch->rdataset, now);
3541 if (result == ISC_R_SUCCESS) {
3542 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3543 if (address_type == DNS_ADBFIND_INET)
3544 name->fetch_err = FIND_ERR_SUCCESS;
3546 name->fetch6_err = FIND_ERR_SUCCESS;
3550 free_adbfetch(adb, &fetch);
3551 isc_event_free(&ev);
3553 clean_finds_at_name(name, ev_status, address_type);
3555 UNLOCK(&adb->namelocks[bucket]);
3559 fetch_callback_a6(isc_task_t *task, isc_event_t *ev) {
3560 dns_fetchevent_t *dev;
3561 dns_adbname_t *name;
3563 dns_adbfetch6_t *fetch;
3566 isc_result_t result;
3567 isc_boolean_t want_check_exit = ISC_FALSE;
3571 INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3572 dev = (dns_fetchevent_t *)ev;
3574 INSIST(DNS_ADBNAME_VALID(name));
3576 INSIST(DNS_ADB_VALID(adb));
3578 bucket = name->lock_bucket;
3579 LOCK(&adb->namelocks[bucket]);
3581 INSIST(!NAME_NEEDSPOKE(name));
3583 for (fetch = ISC_LIST_HEAD(name->fetches_a6);
3585 fetch = ISC_LIST_NEXT(fetch, plink))
3586 if (fetch->fetch == dev->fetch)
3588 INSIST(fetch != NULL);
3589 ISC_LIST_UNLINK(name->fetches_a6, fetch, plink);
3591 DP(ENTER_LEVEL, "ENTER: fetch_callback_a6() name %p", name);
3593 dns_resolver_destroyfetch(&fetch->fetch);
3597 * Cleanup things we don't care about.
3599 if (dev->node != NULL)
3600 dns_db_detachnode(dev->db, &dev->node);
3601 if (dev->db != NULL)
3602 dns_db_detach(&dev->db);
3605 * If this name is marked as dead, clean up, throwing away
3606 * potentially good data.
3608 if (NAME_DEAD(name)) {
3609 free_adbfetch6(adb, &fetch);
3610 isc_event_free(&ev);
3612 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3614 UNLOCK(&adb->namelocks[bucket]);
3616 if (want_check_exit) {
3625 isc_stdtime_get(&now);
3628 * If the A6 query didn't succeed, and this is the first query
3629 * in the A6 chain, try AAAA records instead. For later failures,
3632 if (dev->result != ISC_R_SUCCESS) {
3633 DP(DEF_LEVEL, "name %p: A6 failed: %s",
3634 name, isc_result_totext(dev->result));
3637 * If we got a negative cache response, remember it.
3639 if (NCACHE_RESULT(dev->result)) {
3640 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3641 DP(NCACHE_LEVEL, "adb fetch name %p: "
3642 "Caching negative entry for A6 (ttl %u)",
3643 name, dev->rdataset->ttl);
3644 name->expire_v6 = ISC_MIN(name->expire_v6,
3645 dev->rdataset->ttl + now);
3646 if (dev->result == DNS_R_NCACHENXDOMAIN)
3647 name->fetch6_err = FIND_ERR_NXDOMAIN;
3649 name->fetch6_err = FIND_ERR_NXRRSET;
3653 * Handle CNAME/DNAME.
3655 if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3656 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3657 clean_target(adb, &name->target);
3658 name->expire_target = INT_MAX;
3659 result = set_target(adb, &name->name,
3660 dns_fixedname_name(&dev->foundname),
3663 if (result == ISC_R_SUCCESS) {
3665 "adb A6 fetch name %p: caching alias target",
3667 name->expire_target = dev->rdataset->ttl + now;
3668 if (FETCH_FIRSTA6(fetch)) {
3670 * Make this name 'pokeable', since
3671 * we've learned that this name is an
3674 name->flags |= NAME_NEEDS_POKE;
3680 if (FETCH_FIRSTA6(fetch) && !NAME_HAS_V6(name)) {
3682 "name %p: A6 query failed, starting AAAA", name);
3685 * Since this is the very first fetch, and it
3686 * failed, we know there are no more running.
3688 result = dbfind_name(name, now, dns_rdatatype_aaaa);
3689 if (result == ISC_R_SUCCESS) {
3691 "name %p: callback_a6: Found AAAA for",
3693 name->flags |= NAME_NEEDS_POKE;
3698 * Listen to negative cache hints, and don't start
3701 if (NCACHE_RESULT(result) || AUTH_NX(result)) {
3702 if (NXDOMAIN_RESULT(result))
3703 name->fetch6_err = NEWERR(name->fetch6_err, FIND_ERR_NXDOMAIN);
3705 name->fetch6_err = NEWERR(name->fetch6_err, FIND_ERR_NXRRSET);
3710 * Try to start fetches for AAAA.
3712 result = fetch_name_aaaa(name);
3713 if (result == ISC_R_SUCCESS) {
3715 "name %p: callback_a6: Started AAAA fetch",
3725 * We got something potentially useful. Run the A6 chain
3726 * follower on this A6 rdataset.
3729 fetch->a6ctx.chains = name->chains;
3730 (void)dns_a6_foreach(&fetch->a6ctx, dev->rdataset, now);
3733 free_adbfetch6(adb, &fetch);
3734 isc_event_free(&ev);
3736 if (NAME_NEEDSPOKE(name)) {
3737 clean_finds_at_name(name, DNS_EVENT_ADBMOREADDRESSES,
3739 name->fetch6_err = FIND_ERR_SUCCESS;
3740 } else if (!NAME_FETCH_V6(name))
3741 clean_finds_at_name(name, DNS_EVENT_ADBNOMOREADDRESSES,
3744 name->flags &= ~NAME_NEEDS_POKE;
3746 UNLOCK(&adb->namelocks[bucket]);
3752 fetch_name_v4(dns_adbname_t *adbname, isc_boolean_t start_at_zone) {
3753 isc_result_t result;
3754 dns_adbfetch_t *fetch = NULL;
3756 dns_fixedname_t fixed;
3758 dns_rdataset_t rdataset;
3759 dns_rdataset_t *nameservers;
3760 unsigned int options;
3762 INSIST(DNS_ADBNAME_VALID(adbname));
3764 INSIST(DNS_ADB_VALID(adb));
3766 INSIST(!NAME_FETCH_V4(adbname));
3768 adbname->fetch_err = FIND_ERR_NOTFOUND;
3772 dns_rdataset_init(&rdataset);
3775 if (start_at_zone) {
3776 DP(50, "fetch_name_v4: starting at zone for name %p",
3778 dns_fixedname_init(&fixed);
3779 name = dns_fixedname_name(&fixed);
3780 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3781 0, 0, ISC_TRUE, ISC_FALSE,
3783 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3785 nameservers = &rdataset;
3786 options |= DNS_FETCHOPT_UNSHARED;
3789 fetch = new_adbfetch(adb);
3790 if (fetch == NULL) {
3791 result = ISC_R_NOMEMORY;
3795 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3797 name, nameservers, NULL, options,
3798 adb->task, fetch_callback,
3799 adbname, &fetch->rdataset, NULL,
3801 if (result != ISC_R_SUCCESS)
3804 adbname->fetch_a = fetch;
3805 fetch = NULL; /* keep us from cleaning this up below */
3809 free_adbfetch(adb, &fetch);
3810 if (dns_rdataset_isassociated(&rdataset))
3811 dns_rdataset_disassociate(&rdataset);
3816 /* XXXMLG Why doesn't this look a lot like fetch_name_a and fetch_name_a6? */
3818 fetch_name_aaaa(dns_adbname_t *adbname) {
3819 isc_result_t result;
3820 dns_adbfetch_t *fetch;
3823 INSIST(DNS_ADBNAME_VALID(adbname));
3825 INSIST(DNS_ADB_VALID(adb));
3827 INSIST(!NAME_FETCH_AAAA(adbname));
3829 adbname->fetch6_err = FIND_ERR_NOTFOUND;
3831 fetch = new_adbfetch(adb);
3832 if (fetch == NULL) {
3833 result = ISC_R_NOMEMORY;
3837 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3839 NULL, NULL, NULL, 0,
3840 adb->task, fetch_callback,
3841 adbname, &fetch->rdataset, NULL,
3843 if (result != ISC_R_SUCCESS)
3846 adbname->fetch_aaaa = fetch;
3847 fetch = NULL; /* keep us from cleaning this up below */
3851 free_adbfetch(adb, &fetch);
3857 fetch_name_a6(dns_adbname_t *adbname, isc_boolean_t start_at_zone) {
3858 isc_result_t result;
3859 dns_adbfetch6_t *fetch = NULL;
3861 dns_fixedname_t fixed;
3863 dns_rdataset_t rdataset;
3864 dns_rdataset_t *nameservers;
3865 unsigned int options;
3867 INSIST(DNS_ADBNAME_VALID(adbname));
3869 INSIST(DNS_ADB_VALID(adb));
3871 INSIST(!NAME_FETCH_V6(adbname));
3873 adbname->fetch6_err = FIND_ERR_NOTFOUND;
3877 dns_rdataset_init(&rdataset);
3880 if (start_at_zone) {
3881 DP(50, "fetch_name_a6: starting at zone for name %p",
3883 dns_fixedname_init(&fixed);
3884 name = dns_fixedname_name(&fixed);
3885 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3886 0, 0, ISC_TRUE, ISC_FALSE,
3888 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3890 nameservers = &rdataset;
3891 options |= DNS_FETCHOPT_UNSHARED;
3894 fetch = new_adbfetch6(adb, adbname, NULL);
3895 if (fetch == NULL) {
3896 result = ISC_R_NOMEMORY;
3899 fetch->flags |= FETCH_FIRST_A6;
3901 result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3903 name, nameservers, NULL, options,
3904 adb->task, fetch_callback_a6,
3905 adbname, &fetch->rdataset, NULL,
3907 if (result != ISC_R_SUCCESS)
3910 ISC_LIST_APPEND(adbname->fetches_a6, fetch, plink);
3911 fetch = NULL; /* keep us from cleaning this up below */
3915 free_adbfetch6(adb, &fetch);
3916 if (dns_rdataset_isassociated(&rdataset))
3917 dns_rdataset_disassociate(&rdataset);
3923 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3924 * since these can be extracted from the find itself.
3927 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
3928 isc_stdtime_t expire_time)
3930 dns_adbzoneinfo_t *zi;
3932 isc_result_t result = ISC_R_SUCCESS;
3934 REQUIRE(DNS_ADB_VALID(adb));
3935 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3936 REQUIRE(zone != NULL);
3938 bucket = addr->entry->lock_bucket;
3939 LOCK(&adb->entrylocks[bucket]);
3940 zi = ISC_LIST_HEAD(addr->entry->zoneinfo);
3941 while (zi != NULL && dns_name_equal(zone, &zi->zone))
3942 zi = ISC_LIST_NEXT(zi, plink);
3944 if (expire_time > zi->lame_timer)
3945 zi->lame_timer = expire_time;
3948 zi = new_adbzoneinfo(adb, zone);
3950 result = ISC_R_NOMEMORY;
3954 zi->lame_timer = expire_time;
3956 ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, plink);
3958 UNLOCK(&adb->entrylocks[bucket]);
3960 return (ISC_R_SUCCESS);
3964 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3965 unsigned int rtt, unsigned int factor)
3968 unsigned int new_srtt;
3971 REQUIRE(DNS_ADB_VALID(adb));
3972 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3973 REQUIRE(factor <= 10);
3975 bucket = addr->entry->lock_bucket;
3976 LOCK(&adb->entrylocks[bucket]);
3978 if (factor == DNS_ADB_RTTADJAGE)
3979 new_srtt = addr->entry->srtt * 98 / 100;
3981 new_srtt = (addr->entry->srtt / 10 * factor)
3982 + (rtt / 10 * (10 - factor));
3984 addr->entry->srtt = new_srtt;
3985 addr->srtt = new_srtt;
3987 isc_stdtime_get(&now);
3988 addr->entry->expires = now + ADB_ENTRY_WINDOW;
3990 UNLOCK(&adb->entrylocks[bucket]);
3994 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3995 unsigned int bits, unsigned int mask)
3999 REQUIRE(DNS_ADB_VALID(adb));
4000 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
4002 bucket = addr->entry->lock_bucket;
4003 LOCK(&adb->entrylocks[bucket]);
4005 addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
4007 * Note that we do not update the other bits in addr->flags with
4008 * the most recent values from addr->entry->flags.
4010 addr->flags = (addr->flags & ~mask) | (bits & mask);
4012 UNLOCK(&adb->entrylocks[bucket]);
4016 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
4017 dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
4020 dns_adbentry_t *entry;
4021 dns_adbaddrinfo_t *addr;
4022 isc_result_t result;
4025 REQUIRE(DNS_ADB_VALID(adb));
4026 REQUIRE(addrp != NULL && *addrp == NULL);
4030 result = ISC_R_SUCCESS;
4031 bucket = DNS_ADB_INVALIDBUCKET;
4032 entry = find_entry_and_lock(adb, sa, &bucket);
4033 if (adb->entry_sd[bucket]) {
4034 result = ISC_R_SHUTTINGDOWN;
4037 if (entry == NULL) {
4039 * We don't know anything about this address.
4041 entry = new_adbentry(adb);
4042 if (entry == NULL) {
4043 result = ISC_R_NOMEMORY;
4046 entry->sockaddr = *sa;
4047 link_entry(adb, bucket, entry);
4048 DP(50, "findaddrinfo: new entry %p", entry);
4050 DP(50, "findaddrinfo: found entry %p", entry);
4052 port = isc_sockaddr_getport(sa);
4053 addr = new_adbaddrinfo(adb, entry, port);
4055 inc_entry_refcnt(adb, entry, ISC_FALSE);
4060 UNLOCK(&adb->entrylocks[bucket]);
4066 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
4067 dns_adbaddrinfo_t *addr;
4068 dns_adbentry_t *entry;
4071 isc_boolean_t want_check_exit = ISC_FALSE;
4073 REQUIRE(DNS_ADB_VALID(adb));
4074 REQUIRE(addrp != NULL);
4076 REQUIRE(DNS_ADBADDRINFO_VALID(addr));
4077 entry = addr->entry;
4078 REQUIRE(DNS_ADBENTRY_VALID(entry));
4080 isc_stdtime_get(&now);
4084 bucket = addr->entry->lock_bucket;
4085 LOCK(&adb->entrylocks[bucket]);
4087 entry->expires = now + ADB_ENTRY_WINDOW;
4089 want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
4091 UNLOCK(&adb->entrylocks[bucket]);
4094 free_adbaddrinfo(adb, &addr);
4096 if (want_check_exit) {
4104 dns_adb_flush(dns_adb_t *adb) {
4107 INSIST(DNS_ADB_VALID(adb));
4111 for (i = 0 ; i < NBUCKETS ; i++) {
4113 * Call our cleanup routines.
4115 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
4116 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
4119 #ifdef DUMP_ADB_AFTER_CLEANING
4120 dump_adb(adb, stdout, ISC_TRUE);