4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1999-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: adb.c,v 1.247 2009/02/03 22:33:13 jinmei Exp */
25 * In finds, if task == NULL, no events will be generated, and no events
26 * have been sent. If task != NULL but taskaction == NULL, an event has been
27 * posted but not yet freed. If neither are NULL, no event was posted.
35 #include <isc/mutexblock.h>
36 #include <isc/netaddr.h>
37 #include <isc/random.h>
38 #include <isc/stats.h>
39 #include <isc/string.h> /* Required for HP/UX (and others?) */
45 #include <dns/events.h>
47 #include <dns/rdata.h>
48 #include <dns/rdataset.h>
49 #include <dns/rdatastruct.h>
50 #include <dns/rdatatype.h>
51 #include <dns/resolver.h>
52 #include <dns/result.h>
53 #include <dns/stats.h>
55 #define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
56 #define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
57 #define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
58 #define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
59 #define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
60 #define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
61 #define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
62 #define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
63 #define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
64 #define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
65 #define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
66 #define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
67 #define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
68 #define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
71 * The number of buckets needs to be a prime (for good hashing).
73 * XXXRTH How many buckets do we need?
75 #define NBUCKETS 1009 /*%< how many buckets for names/addrs */
78 * For type 3 negative cache entries, we will remember that the address is
79 * broken for this long. XXXMLG This is also used for actual addresses, too.
80 * The intent is to keep us from constantly asking about A/AAAA records
81 * if the zone has extremely low TTLs.
83 #define ADB_CACHE_MINIMUM 10 /*%< seconds */
84 #define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
85 #define ADB_ENTRY_WINDOW 1800 /*%< seconds */
88 * The period in seconds after which an ADB name entry is regarded as stale
89 * and forced to be cleaned up.
90 * TODO: This should probably be configurable at run-time.
92 #ifndef ADB_STALE_MARGIN
93 #define ADB_STALE_MARGIN 1800
96 #define FREE_ITEMS 64 /*%< free count for memory pools */
97 #define FILL_COUNT 16 /*%< fill count for memory pools */
99 #define DNS_ADB_INVALIDBUCKET (-1) /*%< invalid bucket address */
101 #define DNS_ADB_MINADBSIZE (1024*1024) /*%< 1 Megabyte */
103 typedef ISC_LIST(dns_adbname_t
) dns_adbnamelist_t
;
104 typedef struct dns_adbnamehook dns_adbnamehook_t
;
105 typedef ISC_LIST(dns_adbnamehook_t
) dns_adbnamehooklist_t
;
106 typedef struct dns_adblameinfo dns_adblameinfo_t
;
107 typedef ISC_LIST(dns_adbentry_t
) dns_adbentrylist_t
;
108 typedef struct dns_adbfetch dns_adbfetch_t
;
109 typedef struct dns_adbfetch6 dns_adbfetch6_t
;
111 /*% dns adb structure */
116 isc_mutex_t reflock
; /*%< Covers irefcnt, erefcnt */
117 isc_mutex_t overmemlock
; /*%< Covers overmem */
121 isc_taskmgr_t
*taskmgr
;
123 isc_boolean_t overmem
;
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
*limp
; /*%< dns_adblameinfo_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 */
141 * Bucketized locks and lists for names.
143 * XXXRTH Have a per-bucket structure that contains all of these?
145 dns_adbnamelist_t names
[NBUCKETS
];
146 dns_adbnamelist_t deadnames
[NBUCKETS
];
147 /*% See dns_adbnamelist_t */
148 isc_mutex_t namelocks
[NBUCKETS
];
149 /*% See dns_adbnamelist_t */
150 isc_boolean_t name_sd
[NBUCKETS
];
151 /*% See dns_adbnamelist_t */
152 unsigned int name_refcnt
[NBUCKETS
];
155 * Bucketized locks for entries.
157 * XXXRTH Have a per-bucket structure that contains all of these?
159 dns_adbentrylist_t entries
[NBUCKETS
];
160 dns_adbentrylist_t deadentries
[NBUCKETS
];
161 isc_mutex_t entrylocks
[NBUCKETS
];
162 isc_boolean_t entry_sd
[NBUCKETS
]; /*%< shutting down */
163 unsigned int entry_refcnt
[NBUCKETS
];
166 isc_boolean_t cevent_sent
;
167 isc_boolean_t shutting_down
;
168 isc_eventlist_t whenshutdown
;
172 * XXXMLG Document these structures.
175 /*% dns_adbname structure */
180 unsigned int partial_result
;
184 isc_stdtime_t expire_target
;
185 isc_stdtime_t expire_v4
;
186 isc_stdtime_t expire_v6
;
188 dns_adbnamehooklist_t v4
;
189 dns_adbnamehooklist_t v6
;
190 dns_adbfetch_t
*fetch_a
;
191 dns_adbfetch_t
*fetch_aaaa
;
192 unsigned int fetch_err
;
193 unsigned int fetch6_err
;
194 dns_adbfindlist_t finds
;
195 /* for LRU-based management */
196 isc_stdtime_t last_used
;
198 ISC_LINK(dns_adbname_t
) plink
;
201 /*% The adbfetch structure */
202 struct dns_adbfetch
{
205 dns_rdataset_t rdataset
;
209 * This is a small widget that dangles off a dns_adbname_t. It contains a
210 * pointer to the address information about this host, and a link to the next
211 * namehook that will contain the next address this host has.
213 struct dns_adbnamehook
{
215 dns_adbentry_t
*entry
;
216 ISC_LINK(dns_adbnamehook_t
) plink
;
220 * This is a small widget that holds qname-specific information about an
221 * address. Currently limited to lameness, but could just as easily be
222 * extended to other types of information about zones.
224 struct dns_adblameinfo
{
228 dns_rdatatype_t qtype
;
229 isc_stdtime_t lame_timer
;
231 ISC_LINK(dns_adblameinfo_t
) plink
;
235 * An address entry. It holds quite a bit of information about addresses,
236 * including edns state (in "flags"), rtt, and of course the address of
239 struct dns_adbentry
{
247 isc_sockaddr_t sockaddr
;
249 isc_stdtime_t expires
;
251 * A nonzero 'expires' field indicates that the entry should
252 * persist until that time. This allows entries found
253 * using dns_adb_findaddrinfo() to persist for a limited time
254 * even though they are not necessarily associated with a
258 ISC_LIST(dns_adblameinfo_t
) lameinfo
;
259 ISC_LINK(dns_adbentry_t
) plink
;
263 * Internal functions (and prototypes).
265 static inline dns_adbname_t
*new_adbname(dns_adb_t
*, dns_name_t
*);
266 static inline void free_adbname(dns_adb_t
*, dns_adbname_t
**);
267 static inline dns_adbnamehook_t
*new_adbnamehook(dns_adb_t
*,
269 static inline void free_adbnamehook(dns_adb_t
*, dns_adbnamehook_t
**);
270 static inline dns_adblameinfo_t
*new_adblameinfo(dns_adb_t
*, dns_name_t
*,
272 static inline void free_adblameinfo(dns_adb_t
*, dns_adblameinfo_t
**);
273 static inline dns_adbentry_t
*new_adbentry(dns_adb_t
*);
274 static inline void free_adbentry(dns_adb_t
*, dns_adbentry_t
**);
275 static inline dns_adbfind_t
*new_adbfind(dns_adb_t
*);
276 static inline isc_boolean_t
free_adbfind(dns_adb_t
*, dns_adbfind_t
**);
277 static inline dns_adbaddrinfo_t
*new_adbaddrinfo(dns_adb_t
*, dns_adbentry_t
*,
279 static inline dns_adbfetch_t
*new_adbfetch(dns_adb_t
*);
280 static inline void free_adbfetch(dns_adb_t
*, dns_adbfetch_t
**);
281 static inline dns_adbname_t
*find_name_and_lock(dns_adb_t
*, dns_name_t
*,
282 unsigned int, int *);
283 static inline dns_adbentry_t
*find_entry_and_lock(dns_adb_t
*,
284 isc_sockaddr_t
*, int *,
286 static void dump_adb(dns_adb_t
*, FILE *, isc_boolean_t debug
, isc_stdtime_t
);
287 static void print_dns_name(FILE *, dns_name_t
*);
288 static void print_namehook_list(FILE *, const char *legend
,
289 dns_adbnamehooklist_t
*list
,
292 static void print_find_list(FILE *, dns_adbname_t
*);
293 static void print_fetch_list(FILE *, dns_adbname_t
*);
294 static inline isc_boolean_t
dec_adb_irefcnt(dns_adb_t
*);
295 static inline void inc_adb_irefcnt(dns_adb_t
*);
296 static inline void inc_adb_erefcnt(dns_adb_t
*);
297 static inline void inc_entry_refcnt(dns_adb_t
*, dns_adbentry_t
*,
299 static inline isc_boolean_t
dec_entry_refcnt(dns_adb_t
*, dns_adbentry_t
*,
301 static inline void violate_locking_hierarchy(isc_mutex_t
*, isc_mutex_t
*);
302 static isc_boolean_t
clean_namehooks(dns_adb_t
*, dns_adbnamehooklist_t
*);
303 static void clean_target(dns_adb_t
*, dns_name_t
*);
304 static void clean_finds_at_name(dns_adbname_t
*, isc_eventtype_t
,
306 static isc_boolean_t
check_expire_namehooks(dns_adbname_t
*, isc_stdtime_t
);
307 static isc_boolean_t
check_expire_entry(dns_adb_t
*, dns_adbentry_t
**,
309 static void cancel_fetches_at_name(dns_adbname_t
*);
310 static isc_result_t
dbfind_name(dns_adbname_t
*, isc_stdtime_t
,
312 static isc_result_t
fetch_name(dns_adbname_t
*, isc_boolean_t
,
314 static inline void check_exit(dns_adb_t
*);
315 static void destroy(dns_adb_t
*);
316 static isc_boolean_t
shutdown_names(dns_adb_t
*);
317 static isc_boolean_t
shutdown_entries(dns_adb_t
*);
318 static inline void link_name(dns_adb_t
*, int, dns_adbname_t
*);
319 static inline isc_boolean_t
unlink_name(dns_adb_t
*, dns_adbname_t
*);
320 static inline void link_entry(dns_adb_t
*, int, dns_adbentry_t
*);
321 static inline isc_boolean_t
unlink_entry(dns_adb_t
*, dns_adbentry_t
*);
322 static isc_boolean_t
kill_name(dns_adbname_t
**, isc_eventtype_t
);
323 static void water(void *, int);
324 static void dump_entry(FILE *, dns_adbentry_t
*, isc_boolean_t
, isc_stdtime_t
);
327 * MUST NOT overlap DNS_ADBFIND_* flags!
329 #define FIND_EVENT_SENT 0x40000000
330 #define FIND_EVENT_FREED 0x80000000
331 #define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
332 #define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
334 #define NAME_NEEDS_POKE 0x80000000
335 #define NAME_IS_DEAD 0x40000000
336 #define NAME_HINT_OK DNS_ADBFIND_HINTOK
337 #define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
338 #define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
339 #define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
340 #define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
341 #define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
342 #define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
345 * Private flag(s) for entries.
346 * MUST NOT overlap FCTX_ADDRINFO_xxx and DNS_FETCHOPT_NOEDNS0.
348 #define ENTRY_IS_DEAD 0x80000000
351 * To the name, address classes are all that really exist. If it has a
352 * V6 address it doesn't care if it came from a AAAA query.
354 #define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
355 #define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
356 #define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
359 * Fetches are broken out into A and AAAA types. In some cases,
360 * however, it makes more sense to test for a particular class of fetches,
361 * like V4 or V6 above.
362 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
363 * are now equal to FETCH_V4 and FETCH_V6, respectively.
365 #define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
366 #define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
367 #define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
368 #define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
369 #define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
372 * Find options and tests to see if there are addresses on the list.
374 #define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
375 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
376 #define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
378 #define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
380 #define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
381 #define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
382 #define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
383 #define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
386 * These are currently used on simple unsigned ints, so they are
387 * not really associated with any particular type.
389 #define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
390 #define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
392 #define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
395 * Find out if the flags on a name (nf) indicate if it is a hint or
396 * glue, and compare this to the appropriate bits set in o, to see if
399 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
400 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
401 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
402 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
403 ((o) & DNS_ADBFIND_STARTATZONE))
405 #define ENTER_LEVEL ISC_LOG_DEBUG(50)
406 #define EXIT_LEVEL ENTER_LEVEL
407 #define CLEAN_LEVEL ISC_LOG_DEBUG(100)
408 #define DEF_LEVEL ISC_LOG_DEBUG(5)
409 #define NCACHE_LEVEL ISC_LOG_DEBUG(20)
411 #define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
412 (r) == DNS_R_NCACHENXRRSET)
413 #define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
414 (r) == DNS_R_NXRRSET)
415 #define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
416 (r) == DNS_R_NCACHENXDOMAIN)
417 #define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
418 (r) == DNS_R_NXRRSET || \
419 (r) == DNS_R_HINTNXRRSET)
422 * Error state rankings.
425 #define FIND_ERR_SUCCESS 0 /* highest rank */
426 #define FIND_ERR_CANCELED 1
427 #define FIND_ERR_FAILURE 2
428 #define FIND_ERR_NXDOMAIN 3
429 #define FIND_ERR_NXRRSET 4
430 #define FIND_ERR_UNEXPECTED 5
431 #define FIND_ERR_NOTFOUND 6
432 #define FIND_ERR_MAX 7
434 static const char *errnames
[] = {
444 #define NEWERR(old, new) (ISC_MIN((old), (new)))
446 static isc_result_t find_err_map
[FIND_ERR_MAX
] = {
453 ISC_R_NOTFOUND
/* not YET found */
457 DP(int level
, const char *format
, ...) ISC_FORMAT_PRINTF(2, 3);
460 DP(int level
, const char *format
, ...) {
463 va_start(args
, format
);
464 isc_log_vwrite(dns_lctx
,
465 DNS_LOGCATEGORY_DATABASE
, DNS_LOGMODULE_ADB
,
466 level
, format
, args
);
471 * Increment resolver-related statistics counters.
474 inc_stats(dns_adb_t
*adb
, isc_statscounter_t counter
) {
475 if (adb
->view
->resstats
!= NULL
)
476 isc_stats_increment(adb
->view
->resstats
, counter
);
479 static inline dns_ttl_t
480 ttlclamp(dns_ttl_t ttl
) {
481 if (ttl
< ADB_CACHE_MINIMUM
)
482 ttl
= ADB_CACHE_MINIMUM
;
483 if (ttl
> ADB_CACHE_MAXIMUM
)
484 ttl
= ADB_CACHE_MAXIMUM
;
490 * Requires the adbname bucket be locked and that no entry buckets be locked.
492 * This code handles A and AAAA rdatasets only.
495 import_rdataset(dns_adbname_t
*adbname
, dns_rdataset_t
*rdataset
,
500 dns_adbnamehook_t
*nh
;
501 dns_adbnamehook_t
*anh
;
502 dns_rdata_t rdata
= DNS_RDATA_INIT
;
504 struct in6_addr in6a
;
505 isc_sockaddr_t sockaddr
;
506 dns_adbentry_t
*foundentry
; /* NO CLEAN UP! */
508 isc_boolean_t new_addresses_added
;
509 dns_rdatatype_t rdtype
;
510 unsigned int findoptions
;
511 dns_adbnamehooklist_t
*hookhead
;
513 INSIST(DNS_ADBNAME_VALID(adbname
));
515 INSIST(DNS_ADB_VALID(adb
));
517 rdtype
= rdataset
->type
;
518 INSIST((rdtype
== dns_rdatatype_a
) || (rdtype
== dns_rdatatype_aaaa
));
519 if (rdtype
== dns_rdatatype_a
)
520 findoptions
= DNS_ADBFIND_INET
;
522 findoptions
= DNS_ADBFIND_INET6
;
524 addr_bucket
= DNS_ADB_INVALIDBUCKET
;
525 new_addresses_added
= ISC_FALSE
;
528 result
= dns_rdataset_first(rdataset
);
529 while (result
== ISC_R_SUCCESS
) {
530 dns_rdata_reset(&rdata
);
531 dns_rdataset_current(rdataset
, &rdata
);
532 if (rdtype
== dns_rdatatype_a
) {
533 INSIST(rdata
.length
== 4);
534 memcpy(&ina
.s_addr
, rdata
.data
, 4);
535 isc_sockaddr_fromin(&sockaddr
, &ina
, 0);
536 hookhead
= &adbname
->v4
;
538 INSIST(rdata
.length
== 16);
539 memcpy(in6a
.s6_addr
, rdata
.data
, 16);
540 isc_sockaddr_fromin6(&sockaddr
, &in6a
, 0);
541 hookhead
= &adbname
->v6
;
545 nh
= new_adbnamehook(adb
, NULL
);
547 adbname
->partial_result
|= findoptions
;
548 result
= ISC_R_NOMEMORY
;
552 foundentry
= find_entry_and_lock(adb
, &sockaddr
, &addr_bucket
,
554 if (foundentry
== NULL
) {
555 dns_adbentry_t
*entry
;
557 entry
= new_adbentry(adb
);
559 adbname
->partial_result
|= findoptions
;
560 result
= ISC_R_NOMEMORY
;
564 entry
->sockaddr
= sockaddr
;
569 link_entry(adb
, addr_bucket
, entry
);
571 for (anh
= ISC_LIST_HEAD(*hookhead
);
573 anh
= ISC_LIST_NEXT(anh
, plink
))
574 if (anh
->entry
== foundentry
)
577 foundentry
->refcnt
++;
578 nh
->entry
= foundentry
;
580 free_adbnamehook(adb
, &nh
);
583 new_addresses_added
= ISC_TRUE
;
585 ISC_LIST_APPEND(*hookhead
, nh
, plink
);
587 result
= dns_rdataset_next(rdataset
);
592 free_adbnamehook(adb
, &nh
);
594 if (addr_bucket
!= DNS_ADB_INVALIDBUCKET
)
595 UNLOCK(&adb
->entrylocks
[addr_bucket
]);
597 if (rdataset
->trust
== dns_trust_glue
||
598 rdataset
->trust
== dns_trust_additional
)
599 rdataset
->ttl
= ADB_CACHE_MINIMUM
;
601 rdataset
->ttl
= ttlclamp(rdataset
->ttl
);
603 if (rdtype
== dns_rdatatype_a
) {
604 DP(NCACHE_LEVEL
, "expire_v4 set to MIN(%u,%u) import_rdataset",
605 adbname
->expire_v4
, now
+ rdataset
->ttl
);
606 adbname
->expire_v4
= ISC_MIN(adbname
->expire_v4
,
607 now
+ rdataset
->ttl
);
609 DP(NCACHE_LEVEL
, "expire_v6 set to MIN(%u,%u) import_rdataset",
610 adbname
->expire_v6
, now
+ rdataset
->ttl
);
611 adbname
->expire_v6
= ISC_MIN(adbname
->expire_v6
,
612 now
+ rdataset
->ttl
);
615 if (new_addresses_added
) {
617 * Lie a little here. This is more or less so code that cares
618 * can find out if any new information was added or not.
620 return (ISC_R_SUCCESS
);
627 * Requires the name's bucket be locked.
630 kill_name(dns_adbname_t
**n
, isc_eventtype_t ev
) {
632 isc_boolean_t result
= ISC_FALSE
;
633 isc_boolean_t result4
, result6
;
640 INSIST(DNS_ADBNAME_VALID(name
));
642 INSIST(DNS_ADB_VALID(adb
));
644 DP(DEF_LEVEL
, "killing name %p", name
);
647 * If we're dead already, just check to see if we should go
650 if (NAME_DEAD(name
) && !NAME_FETCH(name
)) {
651 result
= unlink_name(adb
, name
);
652 free_adbname(adb
, &name
);
654 result
= dec_adb_irefcnt(adb
);
659 * Clean up the name's various lists. These two are destructive
660 * in that they will always empty the list.
662 clean_finds_at_name(name
, ev
, DNS_ADBFIND_ADDRESSMASK
);
663 result4
= clean_namehooks(adb
, &name
->v4
);
664 result6
= clean_namehooks(adb
, &name
->v6
);
665 clean_target(adb
, &name
->target
);
666 result
= ISC_TF(result4
|| result6
);
669 * If fetches are running, cancel them. If none are running, we can
670 * just kill the name here.
672 if (!NAME_FETCH(name
)) {
673 INSIST(result
== ISC_FALSE
);
674 result
= unlink_name(adb
, name
);
675 free_adbname(adb
, &name
);
677 result
= dec_adb_irefcnt(adb
);
679 cancel_fetches_at_name(name
);
680 if (!NAME_DEAD(name
)) {
681 bucket
= name
->lock_bucket
;
682 ISC_LIST_UNLINK(adb
->names
[bucket
], name
, plink
);
683 ISC_LIST_APPEND(adb
->deadnames
[bucket
], name
, plink
);
684 name
->flags
|= NAME_IS_DEAD
;
691 * Requires the name's bucket be locked and no entry buckets be locked.
694 check_expire_namehooks(dns_adbname_t
*name
, isc_stdtime_t now
) {
696 isc_boolean_t result4
= ISC_FALSE
;
697 isc_boolean_t result6
= ISC_FALSE
;
699 INSIST(DNS_ADBNAME_VALID(name
));
701 INSIST(DNS_ADB_VALID(adb
));
704 * Check to see if we need to remove the v4 addresses
706 if (!NAME_FETCH_V4(name
) && EXPIRE_OK(name
->expire_v4
, now
)) {
707 if (NAME_HAS_V4(name
)) {
708 DP(DEF_LEVEL
, "expiring v4 for name %p", name
);
709 result4
= clean_namehooks(adb
, &name
->v4
);
710 name
->partial_result
&= ~DNS_ADBFIND_INET
;
712 name
->expire_v4
= INT_MAX
;
713 name
->fetch_err
= FIND_ERR_UNEXPECTED
;
717 * Check to see if we need to remove the v6 addresses
719 if (!NAME_FETCH_V6(name
) && EXPIRE_OK(name
->expire_v6
, now
)) {
720 if (NAME_HAS_V6(name
)) {
721 DP(DEF_LEVEL
, "expiring v6 for name %p", name
);
722 result6
= clean_namehooks(adb
, &name
->v6
);
723 name
->partial_result
&= ~DNS_ADBFIND_INET6
;
725 name
->expire_v6
= INT_MAX
;
726 name
->fetch6_err
= FIND_ERR_UNEXPECTED
;
730 * Check to see if we need to remove the alias target.
732 if (EXPIRE_OK(name
->expire_target
, now
)) {
733 clean_target(adb
, &name
->target
);
734 name
->expire_target
= INT_MAX
;
736 return (ISC_TF(result4
|| result6
));
740 * Requires the name's bucket be locked.
743 link_name(dns_adb_t
*adb
, int bucket
, dns_adbname_t
*name
) {
744 INSIST(name
->lock_bucket
== DNS_ADB_INVALIDBUCKET
);
746 ISC_LIST_PREPEND(adb
->names
[bucket
], name
, plink
);
747 name
->lock_bucket
= bucket
;
748 adb
->name_refcnt
[bucket
]++;
752 * Requires the name's bucket be locked.
754 static inline isc_boolean_t
755 unlink_name(dns_adb_t
*adb
, dns_adbname_t
*name
) {
757 isc_boolean_t result
= ISC_FALSE
;
759 bucket
= name
->lock_bucket
;
760 INSIST(bucket
!= DNS_ADB_INVALIDBUCKET
);
763 ISC_LIST_UNLINK(adb
->deadnames
[bucket
], name
, plink
);
765 ISC_LIST_UNLINK(adb
->names
[bucket
], name
, plink
);
766 name
->lock_bucket
= DNS_ADB_INVALIDBUCKET
;
767 INSIST(adb
->name_refcnt
[bucket
] > 0);
768 adb
->name_refcnt
[bucket
]--;
769 if (adb
->name_sd
[bucket
] && adb
->name_refcnt
[bucket
] == 0)
775 * Requires the entry's bucket be locked.
778 link_entry(dns_adb_t
*adb
, int bucket
, dns_adbentry_t
*entry
) {
783 for (i
= 0; i
< 2; i
++) {
784 e
= ISC_LIST_TAIL(adb
->entries
[bucket
]);
787 if (e
->refcnt
== 0) {
788 unlink_entry(adb
, e
);
789 free_adbentry(adb
, &e
);
792 INSIST((e
->flags
& ENTRY_IS_DEAD
) == 0);
793 e
->flags
|= ENTRY_IS_DEAD
;
794 ISC_LIST_UNLINK(adb
->entries
[bucket
], e
, plink
);
795 ISC_LIST_PREPEND(adb
->deadentries
[bucket
], e
, plink
);
799 ISC_LIST_PREPEND(adb
->entries
[bucket
], entry
, plink
);
800 entry
->lock_bucket
= bucket
;
801 adb
->entry_refcnt
[bucket
]++;
805 * Requires the entry's bucket be locked.
807 static inline isc_boolean_t
808 unlink_entry(dns_adb_t
*adb
, dns_adbentry_t
*entry
) {
810 isc_boolean_t result
= ISC_FALSE
;
812 bucket
= entry
->lock_bucket
;
813 INSIST(bucket
!= DNS_ADB_INVALIDBUCKET
);
815 if ((entry
->flags
& ENTRY_IS_DEAD
) != 0)
816 ISC_LIST_UNLINK(adb
->deadentries
[bucket
], entry
, plink
);
818 ISC_LIST_UNLINK(adb
->entries
[bucket
], entry
, plink
);
819 entry
->lock_bucket
= DNS_ADB_INVALIDBUCKET
;
820 INSIST(adb
->entry_refcnt
[bucket
] > 0);
821 adb
->entry_refcnt
[bucket
]--;
822 if (adb
->entry_sd
[bucket
] && adb
->entry_refcnt
[bucket
] == 0)
828 violate_locking_hierarchy(isc_mutex_t
*have
, isc_mutex_t
*want
) {
829 if (isc_mutex_trylock(want
) != ISC_R_SUCCESS
) {
837 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
838 * checked after calling this function.
841 shutdown_names(dns_adb_t
*adb
) {
843 isc_boolean_t result
= ISC_FALSE
;
845 dns_adbname_t
*next_name
;
847 for (bucket
= 0; bucket
< NBUCKETS
; bucket
++) {
848 LOCK(&adb
->namelocks
[bucket
]);
849 adb
->name_sd
[bucket
] = ISC_TRUE
;
851 name
= ISC_LIST_HEAD(adb
->names
[bucket
]);
854 * This bucket has no names. We must decrement the
855 * irefcnt ourselves, since it will not be
856 * automatically triggered by a name being unlinked.
858 INSIST(result
== ISC_FALSE
);
859 result
= dec_adb_irefcnt(adb
);
862 * Run through the list. For each name, clean up finds
863 * found there, and cancel any fetches running. When
864 * all the fetches are canceled, the name will destroy
867 while (name
!= NULL
) {
868 next_name
= ISC_LIST_NEXT(name
, plink
);
869 INSIST(result
== ISC_FALSE
);
870 result
= kill_name(&name
,
871 DNS_EVENT_ADBSHUTDOWN
);
876 UNLOCK(&adb
->namelocks
[bucket
]);
882 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
883 * checked after calling this function.
886 shutdown_entries(dns_adb_t
*adb
) {
888 isc_boolean_t result
= ISC_FALSE
;
889 dns_adbentry_t
*entry
;
890 dns_adbentry_t
*next_entry
;
892 for (bucket
= 0; bucket
< NBUCKETS
; bucket
++) {
893 LOCK(&adb
->entrylocks
[bucket
]);
894 adb
->entry_sd
[bucket
] = ISC_TRUE
;
896 entry
= ISC_LIST_HEAD(adb
->entries
[bucket
]);
897 if (adb
->entry_refcnt
[bucket
] == 0) {
899 * This bucket has no entries. We must decrement the
900 * irefcnt ourselves, since it will not be
901 * automatically triggered by an entry being unlinked.
903 result
= dec_adb_irefcnt(adb
);
906 * Run through the list. Cleanup any entries not
907 * associated with names, and which are not in use.
909 while (entry
!= NULL
) {
910 next_entry
= ISC_LIST_NEXT(entry
, plink
);
911 if (entry
->refcnt
== 0 &&
912 entry
->expires
!= 0) {
913 result
= unlink_entry(adb
, entry
);
914 free_adbentry(adb
, &entry
);
916 result
= dec_adb_irefcnt(adb
);
922 UNLOCK(&adb
->entrylocks
[bucket
]);
928 * Name bucket must be locked
931 cancel_fetches_at_name(dns_adbname_t
*name
) {
932 if (NAME_FETCH_A(name
))
933 dns_resolver_cancelfetch(name
->fetch_a
->fetch
);
935 if (NAME_FETCH_AAAA(name
))
936 dns_resolver_cancelfetch(name
->fetch_aaaa
->fetch
);
940 * Assumes the name bucket is locked.
943 clean_namehooks(dns_adb_t
*adb
, dns_adbnamehooklist_t
*namehooks
) {
944 dns_adbentry_t
*entry
;
945 dns_adbnamehook_t
*namehook
;
947 isc_boolean_t result
= ISC_FALSE
;
949 addr_bucket
= DNS_ADB_INVALIDBUCKET
;
950 namehook
= ISC_LIST_HEAD(*namehooks
);
951 while (namehook
!= NULL
) {
952 INSIST(DNS_ADBNAMEHOOK_VALID(namehook
));
955 * Clean up the entry if needed.
957 entry
= namehook
->entry
;
959 INSIST(DNS_ADBENTRY_VALID(entry
));
961 if (addr_bucket
!= entry
->lock_bucket
) {
962 if (addr_bucket
!= DNS_ADB_INVALIDBUCKET
)
963 UNLOCK(&adb
->entrylocks
[addr_bucket
]);
964 addr_bucket
= entry
->lock_bucket
;
965 LOCK(&adb
->entrylocks
[addr_bucket
]);
968 result
= dec_entry_refcnt(adb
, entry
, ISC_FALSE
);
974 namehook
->entry
= NULL
;
975 ISC_LIST_UNLINK(*namehooks
, namehook
, plink
);
976 free_adbnamehook(adb
, &namehook
);
978 namehook
= ISC_LIST_HEAD(*namehooks
);
981 if (addr_bucket
!= DNS_ADB_INVALIDBUCKET
)
982 UNLOCK(&adb
->entrylocks
[addr_bucket
]);
987 clean_target(dns_adb_t
*adb
, dns_name_t
*target
) {
988 if (dns_name_countlabels(target
) > 0) {
989 dns_name_free(target
, adb
->mctx
);
990 dns_name_init(target
, NULL
);
995 set_target(dns_adb_t
*adb
, dns_name_t
*name
, dns_name_t
*fname
,
996 dns_rdataset_t
*rdataset
, dns_name_t
*target
)
999 dns_namereln_t namereln
;
1000 unsigned int nlabels
;
1002 dns_rdata_t rdata
= DNS_RDATA_INIT
;
1003 dns_fixedname_t fixed1
, fixed2
;
1004 dns_name_t
*prefix
, *new_target
;
1006 REQUIRE(dns_name_countlabels(target
) == 0);
1008 if (rdataset
->type
== dns_rdatatype_cname
) {
1009 dns_rdata_cname_t cname
;
1012 * Copy the CNAME's target into the target name.
1014 result
= dns_rdataset_first(rdataset
);
1015 if (result
!= ISC_R_SUCCESS
)
1017 dns_rdataset_current(rdataset
, &rdata
);
1018 result
= dns_rdata_tostruct(&rdata
, &cname
, NULL
);
1019 if (result
!= ISC_R_SUCCESS
)
1021 result
= dns_name_dup(&cname
.cname
, adb
->mctx
, target
);
1022 dns_rdata_freestruct(&cname
);
1023 if (result
!= ISC_R_SUCCESS
)
1026 dns_rdata_dname_t dname
;
1028 INSIST(rdataset
->type
== dns_rdatatype_dname
);
1029 namereln
= dns_name_fullcompare(name
, fname
, &order
, &nlabels
);
1030 INSIST(namereln
== dns_namereln_subdomain
);
1032 * Get the target name of the DNAME.
1034 result
= dns_rdataset_first(rdataset
);
1035 if (result
!= ISC_R_SUCCESS
)
1037 dns_rdataset_current(rdataset
, &rdata
);
1038 result
= dns_rdata_tostruct(&rdata
, &dname
, NULL
);
1039 if (result
!= ISC_R_SUCCESS
)
1042 * Construct the new target name.
1044 dns_fixedname_init(&fixed1
);
1045 prefix
= dns_fixedname_name(&fixed1
);
1046 dns_fixedname_init(&fixed2
);
1047 new_target
= dns_fixedname_name(&fixed2
);
1048 dns_name_split(name
, nlabels
, prefix
, NULL
);
1049 result
= dns_name_concatenate(prefix
, &dname
.dname
, new_target
,
1051 dns_rdata_freestruct(&dname
);
1052 if (result
!= ISC_R_SUCCESS
)
1054 result
= dns_name_dup(new_target
, adb
->mctx
, target
);
1055 if (result
!= ISC_R_SUCCESS
)
1059 return (ISC_R_SUCCESS
);
1063 * Assumes nothing is locked, since this is called by the client.
1066 event_free(isc_event_t
*event
) {
1067 dns_adbfind_t
*find
;
1069 INSIST(event
!= NULL
);
1070 find
= event
->ev_destroy_arg
;
1071 INSIST(DNS_ADBFIND_VALID(find
));
1074 find
->flags
|= FIND_EVENT_FREED
;
1075 event
->ev_destroy_arg
= NULL
;
1076 UNLOCK(&find
->lock
);
1080 * Assumes the name bucket is locked.
1083 clean_finds_at_name(dns_adbname_t
*name
, isc_eventtype_t evtype
,
1088 dns_adbfind_t
*find
;
1089 dns_adbfind_t
*next_find
;
1090 isc_boolean_t process
;
1091 unsigned int wanted
, notify
;
1094 "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1095 name
, evtype
, addrs
);
1097 find
= ISC_LIST_HEAD(name
->finds
);
1098 while (find
!= NULL
) {
1100 next_find
= ISC_LIST_NEXT(find
, plink
);
1102 process
= ISC_FALSE
;
1103 wanted
= find
->flags
& DNS_ADBFIND_ADDRESSMASK
;
1104 notify
= wanted
& addrs
;
1107 case DNS_EVENT_ADBMOREADDRESSES
:
1108 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1109 if ((notify
) != 0) {
1110 find
->flags
&= ~addrs
;
1114 case DNS_EVENT_ADBNOMOREADDRESSES
:
1115 DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1116 find
->flags
&= ~addrs
;
1117 wanted
= find
->flags
& DNS_ADBFIND_ADDRESSMASK
;
1122 find
->flags
&= ~addrs
;
1127 DP(DEF_LEVEL
, "cfan: processing find %p", find
);
1129 * Unlink the find from the name, letting the caller
1130 * call dns_adb_destroyfind() on it to clean it up
1133 ISC_LIST_UNLINK(name
->finds
, find
, plink
);
1134 find
->adbname
= NULL
;
1135 find
->name_bucket
= DNS_ADB_INVALIDBUCKET
;
1137 INSIST(!FIND_EVENTSENT(find
));
1140 task
= ev
->ev_sender
;
1141 ev
->ev_sender
= find
;
1142 find
->result_v4
= find_err_map
[name
->fetch_err
];
1143 find
->result_v6
= find_err_map
[name
->fetch6_err
];
1144 ev
->ev_type
= evtype
;
1145 ev
->ev_destroy
= event_free
;
1146 ev
->ev_destroy_arg
= find
;
1149 "sending event %p to task %p for find %p",
1152 isc_task_sendanddetach(&task
, (isc_event_t
**)&ev
);
1154 DP(DEF_LEVEL
, "cfan: skipping find %p", find
);
1157 UNLOCK(&find
->lock
);
1161 DP(ENTER_LEVEL
, "EXIT clean_finds_at_name, name %p", name
);
1165 check_exit(dns_adb_t
*adb
) {
1168 * The caller must be holding the adb lock.
1170 if (adb
->shutting_down
) {
1172 * If there aren't any external references either, we're
1173 * done. Send the control event to initiate shutdown.
1175 INSIST(!adb
->cevent_sent
); /* Sanity check. */
1176 event
= &adb
->cevent
;
1177 isc_task_send(adb
->task
, &event
);
1178 adb
->cevent_sent
= ISC_TRUE
;
1182 static inline isc_boolean_t
1183 dec_adb_irefcnt(dns_adb_t
*adb
) {
1186 isc_boolean_t result
= ISC_FALSE
;
1188 LOCK(&adb
->reflock
);
1190 INSIST(adb
->irefcnt
> 0);
1193 if (adb
->irefcnt
== 0) {
1194 event
= ISC_LIST_HEAD(adb
->whenshutdown
);
1195 while (event
!= NULL
) {
1196 ISC_LIST_UNLINK(adb
->whenshutdown
, event
, ev_link
);
1197 etask
= event
->ev_sender
;
1198 event
->ev_sender
= adb
;
1199 isc_task_sendanddetach(&etask
, &event
);
1200 event
= ISC_LIST_HEAD(adb
->whenshutdown
);
1204 if (adb
->irefcnt
== 0 && adb
->erefcnt
== 0)
1206 UNLOCK(&adb
->reflock
);
1211 inc_adb_irefcnt(dns_adb_t
*adb
) {
1212 LOCK(&adb
->reflock
);
1214 UNLOCK(&adb
->reflock
);
1218 inc_adb_erefcnt(dns_adb_t
*adb
) {
1219 LOCK(&adb
->reflock
);
1221 UNLOCK(&adb
->reflock
);
1225 inc_entry_refcnt(dns_adb_t
*adb
, dns_adbentry_t
*entry
, isc_boolean_t lock
) {
1228 bucket
= entry
->lock_bucket
;
1231 LOCK(&adb
->entrylocks
[bucket
]);
1236 UNLOCK(&adb
->entrylocks
[bucket
]);
1239 static inline isc_boolean_t
1240 dec_entry_refcnt(dns_adb_t
*adb
, dns_adbentry_t
*entry
, isc_boolean_t lock
) {
1242 isc_boolean_t destroy_entry
;
1243 isc_boolean_t result
= ISC_FALSE
;
1245 bucket
= entry
->lock_bucket
;
1248 LOCK(&adb
->entrylocks
[bucket
]);
1250 INSIST(entry
->refcnt
> 0);
1253 destroy_entry
= ISC_FALSE
;
1254 if (entry
->refcnt
== 0 &&
1255 (adb
->entry_sd
[bucket
] || entry
->expires
== 0 || adb
->overmem
||
1256 (entry
->flags
& ENTRY_IS_DEAD
) != 0)) {
1257 destroy_entry
= ISC_TRUE
;
1258 result
= unlink_entry(adb
, entry
);
1262 UNLOCK(&adb
->entrylocks
[bucket
]);
1267 entry
->lock_bucket
= DNS_ADB_INVALIDBUCKET
;
1269 free_adbentry(adb
, &entry
);
1271 result
= dec_adb_irefcnt(adb
);
1276 static inline dns_adbname_t
*
1277 new_adbname(dns_adb_t
*adb
, dns_name_t
*dnsname
) {
1278 dns_adbname_t
*name
;
1280 name
= isc_mempool_get(adb
->nmp
);
1284 dns_name_init(&name
->name
, NULL
);
1285 if (dns_name_dup(dnsname
, adb
->mctx
, &name
->name
) != ISC_R_SUCCESS
) {
1286 isc_mempool_put(adb
->nmp
, name
);
1289 dns_name_init(&name
->target
, NULL
);
1290 name
->magic
= DNS_ADBNAME_MAGIC
;
1292 name
->partial_result
= 0;
1294 name
->expire_v4
= INT_MAX
;
1295 name
->expire_v6
= INT_MAX
;
1296 name
->expire_target
= INT_MAX
;
1298 name
->lock_bucket
= DNS_ADB_INVALIDBUCKET
;
1299 ISC_LIST_INIT(name
->v4
);
1300 ISC_LIST_INIT(name
->v6
);
1301 name
->fetch_a
= NULL
;
1302 name
->fetch_aaaa
= NULL
;
1303 name
->fetch_err
= FIND_ERR_UNEXPECTED
;
1304 name
->fetch6_err
= FIND_ERR_UNEXPECTED
;
1305 ISC_LIST_INIT(name
->finds
);
1306 ISC_LINK_INIT(name
, plink
);
1312 free_adbname(dns_adb_t
*adb
, dns_adbname_t
**name
) {
1315 INSIST(name
!= NULL
&& DNS_ADBNAME_VALID(*name
));
1319 INSIST(!NAME_HAS_V4(n
));
1320 INSIST(!NAME_HAS_V6(n
));
1321 INSIST(!NAME_FETCH(n
));
1322 INSIST(ISC_LIST_EMPTY(n
->finds
));
1323 INSIST(!ISC_LINK_LINKED(n
, plink
));
1324 INSIST(n
->lock_bucket
== DNS_ADB_INVALIDBUCKET
);
1325 INSIST(n
->adb
== adb
);
1328 dns_name_free(&n
->name
, adb
->mctx
);
1330 isc_mempool_put(adb
->nmp
, n
);
1333 static inline dns_adbnamehook_t
*
1334 new_adbnamehook(dns_adb_t
*adb
, dns_adbentry_t
*entry
) {
1335 dns_adbnamehook_t
*nh
;
1337 nh
= isc_mempool_get(adb
->nhmp
);
1341 nh
->magic
= DNS_ADBNAMEHOOK_MAGIC
;
1343 ISC_LINK_INIT(nh
, plink
);
1349 free_adbnamehook(dns_adb_t
*adb
, dns_adbnamehook_t
**namehook
) {
1350 dns_adbnamehook_t
*nh
;
1352 INSIST(namehook
!= NULL
&& DNS_ADBNAMEHOOK_VALID(*namehook
));
1356 INSIST(nh
->entry
== NULL
);
1357 INSIST(!ISC_LINK_LINKED(nh
, plink
));
1360 isc_mempool_put(adb
->nhmp
, nh
);
1363 static inline dns_adblameinfo_t
*
1364 new_adblameinfo(dns_adb_t
*adb
, dns_name_t
*qname
, dns_rdatatype_t qtype
) {
1365 dns_adblameinfo_t
*li
;
1367 li
= isc_mempool_get(adb
->limp
);
1371 dns_name_init(&li
->qname
, NULL
);
1372 if (dns_name_dup(qname
, adb
->mctx
, &li
->qname
) != ISC_R_SUCCESS
) {
1373 isc_mempool_put(adb
->limp
, li
);
1376 li
->magic
= DNS_ADBLAMEINFO_MAGIC
;
1379 ISC_LINK_INIT(li
, plink
);
1385 free_adblameinfo(dns_adb_t
*adb
, dns_adblameinfo_t
**lameinfo
) {
1386 dns_adblameinfo_t
*li
;
1388 INSIST(lameinfo
!= NULL
&& DNS_ADBLAMEINFO_VALID(*lameinfo
));
1392 INSIST(!ISC_LINK_LINKED(li
, plink
));
1394 dns_name_free(&li
->qname
, adb
->mctx
);
1398 isc_mempool_put(adb
->limp
, li
);
1401 static inline dns_adbentry_t
*
1402 new_adbentry(dns_adb_t
*adb
) {
1406 e
= isc_mempool_get(adb
->emp
);
1410 e
->magic
= DNS_ADBENTRY_MAGIC
;
1411 e
->lock_bucket
= DNS_ADB_INVALIDBUCKET
;
1415 e
->srtt
= (r
& 0x1f) + 1;
1417 ISC_LIST_INIT(e
->lameinfo
);
1418 ISC_LINK_INIT(e
, plink
);
1424 free_adbentry(dns_adb_t
*adb
, dns_adbentry_t
**entry
) {
1426 dns_adblameinfo_t
*li
;
1428 INSIST(entry
!= NULL
&& DNS_ADBENTRY_VALID(*entry
));
1432 INSIST(e
->lock_bucket
== DNS_ADB_INVALIDBUCKET
);
1433 INSIST(e
->refcnt
== 0);
1434 INSIST(!ISC_LINK_LINKED(e
, plink
));
1438 li
= ISC_LIST_HEAD(e
->lameinfo
);
1439 while (li
!= NULL
) {
1440 ISC_LIST_UNLINK(e
->lameinfo
, li
, plink
);
1441 free_adblameinfo(adb
, &li
);
1442 li
= ISC_LIST_HEAD(e
->lameinfo
);
1445 isc_mempool_put(adb
->emp
, e
);
1448 static inline dns_adbfind_t
*
1449 new_adbfind(dns_adb_t
*adb
) {
1451 isc_result_t result
;
1453 h
= isc_mempool_get(adb
->ahmp
);
1462 h
->partial_result
= 0;
1465 h
->result_v4
= ISC_R_UNEXPECTED
;
1466 h
->result_v6
= ISC_R_UNEXPECTED
;
1467 ISC_LINK_INIT(h
, publink
);
1468 ISC_LINK_INIT(h
, plink
);
1469 ISC_LIST_INIT(h
->list
);
1471 h
->name_bucket
= DNS_ADB_INVALIDBUCKET
;
1476 result
= isc_mutex_init(&h
->lock
);
1477 if (result
!= ISC_R_SUCCESS
) {
1478 isc_mempool_put(adb
->ahmp
, h
);
1482 ISC_EVENT_INIT(&h
->event
, sizeof(isc_event_t
), 0, 0, 0, NULL
, NULL
,
1485 inc_adb_irefcnt(adb
);
1486 h
->magic
= DNS_ADBFIND_MAGIC
;
1490 static inline dns_adbfetch_t
*
1491 new_adbfetch(dns_adb_t
*adb
) {
1494 f
= isc_mempool_get(adb
->afmp
);
1501 dns_rdataset_init(&f
->rdataset
);
1503 f
->magic
= DNS_ADBFETCH_MAGIC
;
1509 free_adbfetch(dns_adb_t
*adb
, dns_adbfetch_t
**fetch
) {
1512 INSIST(fetch
!= NULL
&& DNS_ADBFETCH_VALID(*fetch
));
1518 if (dns_rdataset_isassociated(&f
->rdataset
))
1519 dns_rdataset_disassociate(&f
->rdataset
);
1521 isc_mempool_put(adb
->afmp
, f
);
1524 static inline isc_boolean_t
1525 free_adbfind(dns_adb_t
*adb
, dns_adbfind_t
**findp
) {
1526 dns_adbfind_t
*find
;
1528 INSIST(findp
!= NULL
&& DNS_ADBFIND_VALID(*findp
));
1532 INSIST(!FIND_HAS_ADDRS(find
));
1533 INSIST(!ISC_LINK_LINKED(find
, publink
));
1534 INSIST(!ISC_LINK_LINKED(find
, plink
));
1535 INSIST(find
->name_bucket
== DNS_ADB_INVALIDBUCKET
);
1536 INSIST(find
->adbname
== NULL
);
1540 DESTROYLOCK(&find
->lock
);
1541 isc_mempool_put(adb
->ahmp
, find
);
1542 return (dec_adb_irefcnt(adb
));
1546 * Copy bits from the entry into the newly allocated addrinfo. The entry
1547 * must be locked, and the reference count must be bumped up by one
1548 * if this function returns a valid pointer.
1550 static inline dns_adbaddrinfo_t
*
1551 new_adbaddrinfo(dns_adb_t
*adb
, dns_adbentry_t
*entry
, in_port_t port
) {
1552 dns_adbaddrinfo_t
*ai
;
1554 ai
= isc_mempool_get(adb
->aimp
);
1558 ai
->magic
= DNS_ADBADDRINFO_MAGIC
;
1559 ai
->sockaddr
= entry
->sockaddr
;
1560 isc_sockaddr_setport(&ai
->sockaddr
, port
);
1561 ai
->srtt
= entry
->srtt
;
1562 ai
->flags
= entry
->flags
;
1564 ISC_LINK_INIT(ai
, publink
);
1570 free_adbaddrinfo(dns_adb_t
*adb
, dns_adbaddrinfo_t
**ainfo
) {
1571 dns_adbaddrinfo_t
*ai
;
1573 INSIST(ainfo
!= NULL
&& DNS_ADBADDRINFO_VALID(*ainfo
));
1577 INSIST(ai
->entry
== NULL
);
1578 INSIST(!ISC_LINK_LINKED(ai
, publink
));
1582 isc_mempool_put(adb
->aimp
, ai
);
1586 * Search for the name. NOTE: The bucket is kept locked on both
1587 * success and failure, so it must always be unlocked by the caller!
1589 * On the first call to this function, *bucketp must be set to
1590 * DNS_ADB_INVALIDBUCKET.
1592 static inline dns_adbname_t
*
1593 find_name_and_lock(dns_adb_t
*adb
, dns_name_t
*name
,
1594 unsigned int options
, int *bucketp
)
1596 dns_adbname_t
*adbname
;
1599 bucket
= dns_name_fullhash(name
, ISC_FALSE
) % NBUCKETS
;
1601 if (*bucketp
== DNS_ADB_INVALIDBUCKET
) {
1602 LOCK(&adb
->namelocks
[bucket
]);
1604 } else if (*bucketp
!= bucket
) {
1605 UNLOCK(&adb
->namelocks
[*bucketp
]);
1606 LOCK(&adb
->namelocks
[bucket
]);
1610 adbname
= ISC_LIST_HEAD(adb
->names
[bucket
]);
1611 while (adbname
!= NULL
) {
1612 if (!NAME_DEAD(adbname
)) {
1613 if (dns_name_equal(name
, &adbname
->name
)
1614 && GLUEHINT_OK(adbname
, options
)
1615 && STARTATZONE_MATCHES(adbname
, options
))
1618 adbname
= ISC_LIST_NEXT(adbname
, plink
);
1625 * Search for the address. NOTE: The bucket is kept locked on both
1626 * success and failure, so it must always be unlocked by the caller.
1628 * On the first call to this function, *bucketp must be set to
1629 * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
1630 * later calls (within the same "lock path") it can be left alone, so
1631 * if this function is called multiple times locking is only done if
1632 * the bucket changes.
1634 static inline dns_adbentry_t
*
1635 find_entry_and_lock(dns_adb_t
*adb
, isc_sockaddr_t
*addr
, int *bucketp
,
1638 dns_adbentry_t
*entry
, *entry_next
;
1641 bucket
= isc_sockaddr_hash(addr
, ISC_TRUE
) % NBUCKETS
;
1643 if (*bucketp
== DNS_ADB_INVALIDBUCKET
) {
1644 LOCK(&adb
->entrylocks
[bucket
]);
1646 } else if (*bucketp
!= bucket
) {
1647 UNLOCK(&adb
->entrylocks
[*bucketp
]);
1648 LOCK(&adb
->entrylocks
[bucket
]);
1652 /* Search the list, while cleaning up expired entries. */
1653 for (entry
= ISC_LIST_HEAD(adb
->entries
[bucket
]);
1655 entry
= entry_next
) {
1656 entry_next
= ISC_LIST_NEXT(entry
, plink
);
1657 (void)check_expire_entry(adb
, &entry
, now
);
1658 if (entry
!= NULL
&&
1659 isc_sockaddr_equal(addr
, &entry
->sockaddr
)) {
1660 ISC_LIST_UNLINK(adb
->entries
[bucket
], entry
, plink
);
1661 ISC_LIST_PREPEND(adb
->entries
[bucket
], entry
, plink
);
1670 * Entry bucket MUST be locked!
1672 static isc_boolean_t
1673 entry_is_lame(dns_adb_t
*adb
, dns_adbentry_t
*entry
, dns_name_t
*qname
,
1674 dns_rdatatype_t qtype
, isc_stdtime_t now
)
1676 dns_adblameinfo_t
*li
, *next_li
;
1677 isc_boolean_t is_bad
;
1681 li
= ISC_LIST_HEAD(entry
->lameinfo
);
1684 while (li
!= NULL
) {
1685 next_li
= ISC_LIST_NEXT(li
, plink
);
1688 * Has the entry expired?
1690 if (li
->lame_timer
< now
) {
1691 ISC_LIST_UNLINK(entry
->lameinfo
, li
, plink
);
1692 free_adblameinfo(adb
, &li
);
1696 * Order tests from least to most expensive.
1698 * We do not break out of the main loop here as
1699 * we use the loop for house keeping.
1701 if (li
!= NULL
&& !is_bad
&& li
->qtype
== qtype
&&
1702 dns_name_equal(qname
, &li
->qname
))
1712 copy_namehook_lists(dns_adb_t
*adb
, dns_adbfind_t
*find
, dns_name_t
*qname
,
1713 dns_rdatatype_t qtype
, dns_adbname_t
*name
,
1716 dns_adbnamehook_t
*namehook
;
1717 dns_adbaddrinfo_t
*addrinfo
;
1718 dns_adbentry_t
*entry
;
1721 bucket
= DNS_ADB_INVALIDBUCKET
;
1723 if (find
->options
& DNS_ADBFIND_INET
) {
1724 namehook
= ISC_LIST_HEAD(name
->v4
);
1725 while (namehook
!= NULL
) {
1726 entry
= namehook
->entry
;
1727 bucket
= entry
->lock_bucket
;
1728 LOCK(&adb
->entrylocks
[bucket
]);
1730 if (!FIND_RETURNLAME(find
)
1731 && entry_is_lame(adb
, entry
, qname
, qtype
, now
)) {
1732 find
->options
|= DNS_ADBFIND_LAMEPRUNED
;
1735 addrinfo
= new_adbaddrinfo(adb
, entry
, find
->port
);
1736 if (addrinfo
== NULL
) {
1737 find
->partial_result
|= DNS_ADBFIND_INET
;
1741 * Found a valid entry. Add it to the find's list.
1743 inc_entry_refcnt(adb
, entry
, ISC_FALSE
);
1744 ISC_LIST_APPEND(find
->list
, addrinfo
, publink
);
1747 UNLOCK(&adb
->entrylocks
[bucket
]);
1748 bucket
= DNS_ADB_INVALIDBUCKET
;
1749 namehook
= ISC_LIST_NEXT(namehook
, plink
);
1753 if (find
->options
& DNS_ADBFIND_INET6
) {
1754 namehook
= ISC_LIST_HEAD(name
->v6
);
1755 while (namehook
!= NULL
) {
1756 entry
= namehook
->entry
;
1757 bucket
= entry
->lock_bucket
;
1758 LOCK(&adb
->entrylocks
[bucket
]);
1760 if (!FIND_RETURNLAME(find
)
1761 && entry_is_lame(adb
, entry
, qname
, qtype
, now
)) {
1762 find
->options
|= DNS_ADBFIND_LAMEPRUNED
;
1765 addrinfo
= new_adbaddrinfo(adb
, entry
, find
->port
);
1766 if (addrinfo
== NULL
) {
1767 find
->partial_result
|= DNS_ADBFIND_INET6
;
1771 * Found a valid entry. Add it to the find's list.
1773 inc_entry_refcnt(adb
, entry
, ISC_FALSE
);
1774 ISC_LIST_APPEND(find
->list
, addrinfo
, publink
);
1777 UNLOCK(&adb
->entrylocks
[bucket
]);
1778 bucket
= DNS_ADB_INVALIDBUCKET
;
1779 namehook
= ISC_LIST_NEXT(namehook
, plink
);
1784 if (bucket
!= DNS_ADB_INVALIDBUCKET
)
1785 UNLOCK(&adb
->entrylocks
[bucket
]);
1789 shutdown_task(isc_task_t
*task
, isc_event_t
*ev
) {
1795 INSIST(DNS_ADB_VALID(adb
));
1797 isc_event_free(&ev
);
1799 * Wait for lock around check_exit() call to be released.
1807 * Name bucket must be locked; adb may be locked; no other locks held.
1809 static isc_boolean_t
1810 check_expire_name(dns_adbname_t
**namep
, isc_stdtime_t now
) {
1811 dns_adbname_t
*name
;
1812 isc_boolean_t result
= ISC_FALSE
;
1814 INSIST(namep
!= NULL
&& DNS_ADBNAME_VALID(*namep
));
1817 if (NAME_HAS_V4(name
) || NAME_HAS_V6(name
))
1819 if (NAME_FETCH(name
))
1821 if (!EXPIRE_OK(name
->expire_v4
, now
))
1823 if (!EXPIRE_OK(name
->expire_v6
, now
))
1825 if (!EXPIRE_OK(name
->expire_target
, now
))
1829 * The name is empty. Delete it.
1831 result
= kill_name(&name
, DNS_EVENT_ADBEXPIRED
);
1835 * Our caller, or one of its callers, will be calling check_exit() at
1836 * some point, so we don't need to do it here.
1842 * Examine the tail entry of the LRU list to see if it expires or is stale
1843 * (unused for some period); if so, the name entry will be freed. If the ADB
1844 * is in the overmem condition, the tail and the next to tail entries
1845 * will be unconditionally removed (unless they have an outstanding fetch).
1846 * We don't care about a race on 'overmem' at the risk of causing some
1847 * collateral damage or a small delay in starting cleanup, so we don't bother
1848 * to lock ADB (if it's not locked).
1850 * Name bucket must be locked; adb may be locked; no other locks held.
1853 check_stale_name(dns_adb_t
*adb
, int bucket
, isc_stdtime_t now
) {
1854 int victims
, max_victims
;
1855 isc_boolean_t result
;
1856 dns_adbname_t
*victim
, *next_victim
;
1857 isc_boolean_t overmem
= adb
->overmem
;
1860 INSIST(bucket
!= DNS_ADB_INVALIDBUCKET
);
1862 max_victims
= overmem
? 2 : 1;
1865 * We limit the number of scanned entries to 10 (arbitrary choice)
1866 * in order to avoid examining too many entries when there are many
1867 * tail entries that have fetches (this should be rare, but could
1870 victim
= ISC_LIST_TAIL(adb
->names
[bucket
]);
1872 victim
!= NULL
&& victims
< max_victims
&& scans
< 10;
1873 victim
= next_victim
) {
1874 INSIST(!NAME_DEAD(victim
));
1876 next_victim
= ISC_LIST_PREV(victim
, plink
);
1877 result
= check_expire_name(&victim
, now
);
1878 if (victim
== NULL
) {
1883 if (!NAME_FETCH(victim
) &&
1884 (overmem
|| victim
->last_used
+ ADB_STALE_MARGIN
<= now
)) {
1885 RUNTIME_CHECK(kill_name(&victim
,
1886 DNS_EVENT_ADBCANCELED
) ==
1898 * Entry bucket must be locked; adb may be locked; no other locks held.
1900 static isc_boolean_t
1901 check_expire_entry(dns_adb_t
*adb
, dns_adbentry_t
**entryp
, isc_stdtime_t now
)
1903 dns_adbentry_t
*entry
;
1904 isc_boolean_t result
= ISC_FALSE
;
1906 INSIST(entryp
!= NULL
&& DNS_ADBENTRY_VALID(*entryp
));
1909 if (entry
->refcnt
!= 0)
1912 if (entry
->expires
== 0 || entry
->expires
> now
)
1916 * The entry is not in use. Delete it.
1918 DP(DEF_LEVEL
, "killing entry %p", entry
);
1919 INSIST(ISC_LINK_LINKED(entry
, plink
));
1920 result
= unlink_entry(adb
, entry
);
1921 free_adbentry(adb
, &entry
);
1923 dec_adb_irefcnt(adb
);
1929 * ADB must be locked, and no other locks held.
1931 static isc_boolean_t
1932 cleanup_names(dns_adb_t
*adb
, int bucket
, isc_stdtime_t now
) {
1933 dns_adbname_t
*name
;
1934 dns_adbname_t
*next_name
;
1935 isc_boolean_t result
= ISC_FALSE
;
1937 DP(CLEAN_LEVEL
, "cleaning name bucket %d", bucket
);
1939 LOCK(&adb
->namelocks
[bucket
]);
1940 if (adb
->name_sd
[bucket
]) {
1941 UNLOCK(&adb
->namelocks
[bucket
]);
1945 name
= ISC_LIST_HEAD(adb
->names
[bucket
]);
1946 while (name
!= NULL
) {
1947 next_name
= ISC_LIST_NEXT(name
, plink
);
1948 INSIST(result
== ISC_FALSE
);
1949 result
= check_expire_namehooks(name
, now
);
1951 result
= check_expire_name(&name
, now
);
1954 UNLOCK(&adb
->namelocks
[bucket
]);
1959 * ADB must be locked, and no other locks held.
1961 static isc_boolean_t
1962 cleanup_entries(dns_adb_t
*adb
, int bucket
, isc_stdtime_t now
) {
1963 dns_adbentry_t
*entry
, *next_entry
;
1964 isc_boolean_t result
= ISC_FALSE
;
1966 DP(CLEAN_LEVEL
, "cleaning entry bucket %d", bucket
);
1968 LOCK(&adb
->entrylocks
[bucket
]);
1969 entry
= ISC_LIST_HEAD(adb
->entries
[bucket
]);
1970 while (entry
!= NULL
) {
1971 next_entry
= ISC_LIST_NEXT(entry
, plink
);
1972 INSIST(result
== ISC_FALSE
);
1973 result
= check_expire_entry(adb
, &entry
, now
);
1976 UNLOCK(&adb
->entrylocks
[bucket
]);
1981 destroy(dns_adb_t
*adb
) {
1984 isc_task_detach(&adb
->task
);
1986 isc_mempool_destroy(&adb
->nmp
);
1987 isc_mempool_destroy(&adb
->nhmp
);
1988 isc_mempool_destroy(&adb
->limp
);
1989 isc_mempool_destroy(&adb
->emp
);
1990 isc_mempool_destroy(&adb
->ahmp
);
1991 isc_mempool_destroy(&adb
->aimp
);
1992 isc_mempool_destroy(&adb
->afmp
);
1994 DESTROYMUTEXBLOCK(adb
->entrylocks
, NBUCKETS
);
1995 DESTROYMUTEXBLOCK(adb
->namelocks
, NBUCKETS
);
1997 DESTROYLOCK(&adb
->reflock
);
1998 DESTROYLOCK(&adb
->lock
);
1999 DESTROYLOCK(&adb
->mplock
);
2000 DESTROYLOCK(&adb
->overmemlock
);
2002 isc_mem_putanddetach(&adb
->mctx
, adb
, sizeof(dns_adb_t
));
2011 dns_adb_create(isc_mem_t
*mem
, dns_view_t
*view
, isc_timermgr_t
*timermgr
,
2012 isc_taskmgr_t
*taskmgr
, dns_adb_t
**newadb
)
2015 isc_result_t result
;
2018 REQUIRE(mem
!= NULL
);
2019 REQUIRE(view
!= NULL
);
2020 REQUIRE(timermgr
!= NULL
); /* this is actually unused */
2021 REQUIRE(taskmgr
!= NULL
);
2022 REQUIRE(newadb
!= NULL
&& *newadb
== NULL
);
2026 adb
= isc_mem_get(mem
, sizeof(dns_adb_t
));
2028 return (ISC_R_NOMEMORY
);
2031 * Initialize things here that cannot fail, and especially things
2032 * that must be NULL for the error return to work properly.
2047 adb
->taskmgr
= taskmgr
;
2048 adb
->next_cleanbucket
= 0;
2049 ISC_EVENT_INIT(&adb
->cevent
, sizeof(adb
->cevent
), 0, NULL
,
2050 DNS_EVENT_ADBCONTROL
, shutdown_task
, adb
,
2052 adb
->cevent_sent
= ISC_FALSE
;
2053 adb
->shutting_down
= ISC_FALSE
;
2054 adb
->overmem
= ISC_FALSE
;
2055 ISC_LIST_INIT(adb
->whenshutdown
);
2057 isc_mem_attach(mem
, &adb
->mctx
);
2059 result
= isc_mutex_init(&adb
->lock
);
2060 if (result
!= ISC_R_SUCCESS
)
2063 result
= isc_mutex_init(&adb
->mplock
);
2064 if (result
!= ISC_R_SUCCESS
)
2067 result
= isc_mutex_init(&adb
->reflock
);
2068 if (result
!= ISC_R_SUCCESS
)
2071 result
= isc_mutex_init(&adb
->overmemlock
);
2072 if (result
!= ISC_R_SUCCESS
)
2076 * Initialize the bucket locks for names and elements.
2077 * May as well initialize the list heads, too.
2079 result
= isc_mutexblock_init(adb
->namelocks
, NBUCKETS
);
2080 if (result
!= ISC_R_SUCCESS
)
2082 for (i
= 0; i
< NBUCKETS
; i
++) {
2083 ISC_LIST_INIT(adb
->names
[i
]);
2084 ISC_LIST_INIT(adb
->deadnames
[i
]);
2085 adb
->name_sd
[i
] = ISC_FALSE
;
2086 adb
->name_refcnt
[i
] = 0;
2089 for (i
= 0; i
< NBUCKETS
; i
++) {
2090 ISC_LIST_INIT(adb
->entries
[i
]);
2091 ISC_LIST_INIT(adb
->deadentries
[i
]);
2092 adb
->entry_sd
[i
] = ISC_FALSE
;
2093 adb
->entry_refcnt
[i
] = 0;
2096 result
= isc_mutexblock_init(adb
->entrylocks
, NBUCKETS
);
2097 if (result
!= ISC_R_SUCCESS
)
2103 #define MPINIT(t, p, n) do { \
2104 result = isc_mempool_create(mem, sizeof(t), &(p)); \
2105 if (result != ISC_R_SUCCESS) \
2107 isc_mempool_setfreemax((p), FREE_ITEMS); \
2108 isc_mempool_setfillcount((p), FILL_COUNT); \
2109 isc_mempool_setname((p), n); \
2110 isc_mempool_associatelock((p), &adb->mplock); \
2113 MPINIT(dns_adbname_t
, adb
->nmp
, "adbname");
2114 MPINIT(dns_adbnamehook_t
, adb
->nhmp
, "adbnamehook");
2115 MPINIT(dns_adblameinfo_t
, adb
->limp
, "adblameinfo");
2116 MPINIT(dns_adbentry_t
, adb
->emp
, "adbentry");
2117 MPINIT(dns_adbfind_t
, adb
->ahmp
, "adbfind");
2118 MPINIT(dns_adbaddrinfo_t
, adb
->aimp
, "adbaddrinfo");
2119 MPINIT(dns_adbfetch_t
, adb
->afmp
, "adbfetch");
2124 * Allocate an internal task.
2126 result
= isc_task_create(adb
->taskmgr
, 0, &adb
->task
);
2127 if (result
!= ISC_R_SUCCESS
)
2129 isc_task_setname(adb
->task
, "ADB", adb
);
2134 adb
->magic
= DNS_ADB_MAGIC
;
2136 return (ISC_R_SUCCESS
);
2139 if (adb
->task
!= NULL
)
2140 isc_task_detach(&adb
->task
);
2142 /* clean up entrylocks */
2143 DESTROYMUTEXBLOCK(adb
->entrylocks
, NBUCKETS
);
2145 fail2
: /* clean up namelocks */
2146 DESTROYMUTEXBLOCK(adb
->namelocks
, NBUCKETS
);
2148 fail1
: /* clean up only allocated memory */
2149 if (adb
->nmp
!= NULL
)
2150 isc_mempool_destroy(&adb
->nmp
);
2151 if (adb
->nhmp
!= NULL
)
2152 isc_mempool_destroy(&adb
->nhmp
);
2153 if (adb
->limp
!= NULL
)
2154 isc_mempool_destroy(&adb
->limp
);
2155 if (adb
->emp
!= NULL
)
2156 isc_mempool_destroy(&adb
->emp
);
2157 if (adb
->ahmp
!= NULL
)
2158 isc_mempool_destroy(&adb
->ahmp
);
2159 if (adb
->aimp
!= NULL
)
2160 isc_mempool_destroy(&adb
->aimp
);
2161 if (adb
->afmp
!= NULL
)
2162 isc_mempool_destroy(&adb
->afmp
);
2164 DESTROYLOCK(&adb
->overmemlock
);
2166 DESTROYLOCK(&adb
->reflock
);
2168 DESTROYLOCK(&adb
->mplock
);
2170 DESTROYLOCK(&adb
->lock
);
2172 isc_mem_putanddetach(&adb
->mctx
, adb
, sizeof(dns_adb_t
));
2178 dns_adb_attach(dns_adb_t
*adb
, dns_adb_t
**adbx
) {
2180 REQUIRE(DNS_ADB_VALID(adb
));
2181 REQUIRE(adbx
!= NULL
&& *adbx
== NULL
);
2183 inc_adb_erefcnt(adb
);
2188 dns_adb_detach(dns_adb_t
**adbx
) {
2190 isc_boolean_t need_exit_check
;
2192 REQUIRE(adbx
!= NULL
&& DNS_ADB_VALID(*adbx
));
2197 INSIST(adb
->erefcnt
> 0);
2199 LOCK(&adb
->reflock
);
2201 need_exit_check
= ISC_TF(adb
->erefcnt
== 0 && adb
->irefcnt
== 0);
2202 UNLOCK(&adb
->reflock
);
2204 if (need_exit_check
) {
2206 INSIST(adb
->shutting_down
);
2213 dns_adb_whenshutdown(dns_adb_t
*adb
, isc_task_t
*task
, isc_event_t
**eventp
) {
2216 isc_boolean_t zeroirefcnt
= ISC_FALSE
;
2219 * Send '*eventp' to 'task' when 'adb' has shutdown.
2222 REQUIRE(DNS_ADB_VALID(adb
));
2223 REQUIRE(eventp
!= NULL
);
2230 LOCK(&adb
->reflock
);
2231 zeroirefcnt
= ISC_TF(adb
->irefcnt
== 0);
2233 if (adb
->shutting_down
&& zeroirefcnt
&&
2234 isc_mempool_getallocated(adb
->ahmp
) == 0) {
2236 * We're already shutdown. Send the event.
2238 event
->ev_sender
= adb
;
2239 isc_task_send(task
, &event
);
2242 isc_task_attach(task
, &clone
);
2243 event
->ev_sender
= clone
;
2244 ISC_LIST_APPEND(adb
->whenshutdown
, event
, ev_link
);
2247 UNLOCK(&adb
->reflock
);
2252 dns_adb_shutdown(dns_adb_t
*adb
) {
2253 isc_boolean_t need_check_exit
;
2261 if (!adb
->shutting_down
) {
2262 adb
->shutting_down
= ISC_TRUE
;
2263 isc_mem_setwater(adb
->mctx
, water
, adb
, 0, 0);
2264 need_check_exit
= shutdown_names(adb
);
2265 if (!need_check_exit
)
2266 need_check_exit
= shutdown_entries(adb
);
2267 if (need_check_exit
)
2275 dns_adb_createfind(dns_adb_t
*adb
, isc_task_t
*task
, isc_taskaction_t action
,
2276 void *arg
, dns_name_t
*name
, dns_name_t
*qname
,
2277 dns_rdatatype_t qtype
, unsigned int options
,
2278 isc_stdtime_t now
, dns_name_t
*target
,
2279 in_port_t port
, dns_adbfind_t
**findp
)
2281 dns_adbfind_t
*find
;
2282 dns_adbname_t
*adbname
;
2284 isc_boolean_t want_event
, start_at_zone
, alias
, have_address
;
2285 isc_result_t result
;
2286 unsigned int wanted_addresses
;
2287 unsigned int wanted_fetches
;
2288 unsigned int query_pending
;
2290 REQUIRE(DNS_ADB_VALID(adb
));
2292 REQUIRE(action
!= NULL
);
2294 REQUIRE(name
!= NULL
);
2295 REQUIRE(qname
!= NULL
);
2296 REQUIRE(findp
!= NULL
&& *findp
== NULL
);
2297 REQUIRE(target
== NULL
|| dns_name_hasbuffer(target
));
2299 REQUIRE((options
& DNS_ADBFIND_ADDRESSMASK
) != 0);
2301 result
= ISC_R_UNEXPECTED
;
2302 wanted_addresses
= (options
& DNS_ADBFIND_ADDRESSMASK
);
2305 want_event
= ISC_FALSE
;
2306 start_at_zone
= ISC_FALSE
;
2310 isc_stdtime_get(&now
);
2313 * XXXMLG Move this comment somewhere else!
2315 * Look up the name in our internal database.
2317 * Possibilities: Note that these are not always exclusive.
2319 * No name found. In this case, allocate a new name header and
2320 * an initial namehook or two. If any of these allocations
2321 * fail, clean up and return ISC_R_NOMEMORY.
2323 * Name found, valid addresses present. Allocate one addrinfo
2324 * structure for each found and append it to the linked list
2325 * of addresses for this header.
2327 * Name found, queries pending. In this case, if a task was
2328 * passed in, allocate a job id, attach it to the name's job
2329 * list and remember to tell the caller that there will be
2330 * more info coming later.
2333 find
= new_adbfind(adb
);
2335 return (ISC_R_NOMEMORY
);
2340 * Remember what types of addresses we are interested in.
2342 find
->options
= options
;
2343 find
->flags
|= wanted_addresses
;
2344 if (FIND_WANTEVENT(find
)) {
2345 REQUIRE(task
!= NULL
);
2349 * Try to see if we know anything about this name at all.
2351 bucket
= DNS_ADB_INVALIDBUCKET
;
2352 adbname
= find_name_and_lock(adb
, name
, find
->options
, &bucket
);
2353 if (adb
->name_sd
[bucket
]) {
2355 "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2356 RUNTIME_CHECK(free_adbfind(adb
, &find
) == ISC_FALSE
);
2357 result
= ISC_R_SHUTTINGDOWN
;
2362 * Nothing found. Allocate a new adbname structure for this name.
2364 if (adbname
== NULL
) {
2366 * See if there is any stale name at the end of list, and purge
2369 check_stale_name(adb
, bucket
, now
);
2371 adbname
= new_adbname(adb
, name
);
2372 if (adbname
== NULL
) {
2373 RUNTIME_CHECK(free_adbfind(adb
, &find
) == ISC_FALSE
);
2374 result
= ISC_R_NOMEMORY
;
2377 link_name(adb
, bucket
, adbname
);
2378 if (FIND_HINTOK(find
))
2379 adbname
->flags
|= NAME_HINT_OK
;
2380 if (FIND_GLUEOK(find
))
2381 adbname
->flags
|= NAME_GLUE_OK
;
2382 if (FIND_STARTATZONE(find
))
2383 adbname
->flags
|= NAME_STARTATZONE
;
2385 /* Move this name forward in the LRU list */
2386 ISC_LIST_UNLINK(adb
->names
[bucket
], adbname
, plink
);
2387 ISC_LIST_PREPEND(adb
->names
[bucket
], adbname
, plink
);
2389 adbname
->last_used
= now
;
2392 * Expire old entries, etc.
2394 RUNTIME_CHECK(check_expire_namehooks(adbname
, now
) == ISC_FALSE
);
2397 * Do we know that the name is an alias?
2399 if (!EXPIRE_OK(adbname
->expire_target
, now
)) {
2404 "dns_adb_createfind: name %p is an alias (cached)",
2411 * Try to populate the name from the database and/or
2412 * start fetches. First try looking for an A record
2415 if (!NAME_HAS_V4(adbname
) && EXPIRE_OK(adbname
->expire_v4
, now
)
2416 && WANT_INET(wanted_addresses
)) {
2417 result
= dbfind_name(adbname
, now
, dns_rdatatype_a
);
2418 if (result
== ISC_R_SUCCESS
) {
2420 "dns_adb_createfind: found A for name %p in db",
2426 * Did we get a CNAME or DNAME?
2428 if (result
== DNS_R_ALIAS
) {
2430 "dns_adb_createfind: name %p is an alias",
2437 * If the name doesn't exist at all, don't bother with
2438 * v6 queries; they won't work.
2440 * If the name does exist but we didn't get our data, go
2441 * ahead and try AAAA.
2443 * If the result is neither of these, try a fetch for A.
2445 if (NXDOMAIN_RESULT(result
))
2447 else if (NXRRSET_RESULT(result
))
2450 if (!NAME_FETCH_V4(adbname
))
2451 wanted_fetches
|= DNS_ADBFIND_INET
;
2455 if (!NAME_HAS_V6(adbname
) && EXPIRE_OK(adbname
->expire_v6
, now
)
2456 && WANT_INET6(wanted_addresses
)) {
2457 result
= dbfind_name(adbname
, now
, dns_rdatatype_aaaa
);
2458 if (result
== ISC_R_SUCCESS
) {
2460 "dns_adb_createfind: found AAAA for name %p",
2466 * Did we get a CNAME or DNAME?
2468 if (result
== DNS_R_ALIAS
) {
2470 "dns_adb_createfind: name %p is an alias",
2477 * Listen to negative cache hints, and don't start
2480 if (NCACHE_RESULT(result
) || AUTH_NX(result
))
2483 if (!NAME_FETCH_V6(adbname
))
2484 wanted_fetches
|= DNS_ADBFIND_INET6
;
2488 if ((WANT_INET(wanted_addresses
) && NAME_HAS_V4(adbname
)) ||
2489 (WANT_INET6(wanted_addresses
) && NAME_HAS_V6(adbname
)))
2490 have_address
= ISC_TRUE
;
2492 have_address
= ISC_FALSE
;
2493 if (wanted_fetches
!= 0 &&
2494 ! (FIND_AVOIDFETCHES(find
) && have_address
)) {
2496 * We're missing at least one address family. Either the
2497 * caller hasn't instructed us to avoid fetches, or we don't
2498 * know anything about any of the address families that would
2499 * be acceptable so we have to launch fetches.
2502 if (FIND_STARTATZONE(find
))
2503 start_at_zone
= ISC_TRUE
;
2508 if (WANT_INET(wanted_fetches
) &&
2509 fetch_name(adbname
, start_at_zone
,
2510 dns_rdatatype_a
) == ISC_R_SUCCESS
) {
2512 "dns_adb_createfind: started A fetch for name %p",
2519 if (WANT_INET6(wanted_fetches
) &&
2520 fetch_name(adbname
, start_at_zone
,
2521 dns_rdatatype_aaaa
) == ISC_R_SUCCESS
) {
2523 "dns_adb_createfind: "
2524 "started AAAA fetch for name %p",
2530 * Run through the name and copy out the bits we are
2533 copy_namehook_lists(adb
, find
, qname
, qtype
, adbname
, now
);
2536 if (NAME_FETCH_V4(adbname
))
2537 query_pending
|= DNS_ADBFIND_INET
;
2538 if (NAME_FETCH_V6(adbname
))
2539 query_pending
|= DNS_ADBFIND_INET6
;
2542 * Attach to the name's query list if there are queries
2543 * already running, and we have been asked to.
2545 want_event
= ISC_TRUE
;
2546 if (!FIND_WANTEVENT(find
))
2547 want_event
= ISC_FALSE
;
2548 if (FIND_WANTEMPTYEVENT(find
) && FIND_HAS_ADDRS(find
))
2549 want_event
= ISC_FALSE
;
2550 if ((wanted_addresses
& query_pending
) == 0)
2551 want_event
= ISC_FALSE
;
2553 want_event
= ISC_FALSE
;
2555 find
->adbname
= adbname
;
2556 find
->name_bucket
= bucket
;
2557 ISC_LIST_APPEND(adbname
->finds
, find
, plink
);
2558 find
->query_pending
= (query_pending
& wanted_addresses
);
2559 find
->flags
&= ~DNS_ADBFIND_ADDRESSMASK
;
2560 find
->flags
|= (find
->query_pending
& DNS_ADBFIND_ADDRESSMASK
);
2561 DP(DEF_LEVEL
, "createfind: attaching find %p to adbname %p",
2565 * Remove the flag so the caller knows there will never
2566 * be an event, and set internal flags to fake that
2567 * the event was sent and freed, so dns_adb_destroyfind() will
2568 * do the right thing.
2570 find
->query_pending
= (query_pending
& wanted_addresses
);
2571 find
->options
&= ~DNS_ADBFIND_WANTEVENT
;
2572 find
->flags
|= (FIND_EVENT_SENT
| FIND_EVENT_FREED
);
2573 find
->flags
&= ~DNS_ADBFIND_ADDRESSMASK
;
2576 find
->partial_result
|= (adbname
->partial_result
& wanted_addresses
);
2578 if (target
!= NULL
) {
2579 result
= dns_name_copy(&adbname
->target
, target
, NULL
);
2580 if (result
!= ISC_R_SUCCESS
)
2583 result
= DNS_R_ALIAS
;
2585 result
= ISC_R_SUCCESS
;
2588 * Copy out error flags from the name structure into the find.
2590 find
->result_v4
= find_err_map
[adbname
->fetch_err
];
2591 find
->result_v6
= find_err_map
[adbname
->fetch6_err
];
2600 INSIST((find
->flags
& DNS_ADBFIND_ADDRESSMASK
) != 0);
2602 isc_task_attach(task
, &taskp
);
2603 find
->event
.ev_sender
= taskp
;
2604 find
->event
.ev_action
= action
;
2605 find
->event
.ev_arg
= arg
;
2609 UNLOCK(&adb
->namelocks
[bucket
]);
2615 dns_adb_destroyfind(dns_adbfind_t
**findp
) {
2616 dns_adbfind_t
*find
;
2617 dns_adbentry_t
*entry
;
2618 dns_adbaddrinfo_t
*ai
;
2622 REQUIRE(findp
!= NULL
&& DNS_ADBFIND_VALID(*findp
));
2628 DP(DEF_LEVEL
, "dns_adb_destroyfind on find %p", find
);
2631 REQUIRE(DNS_ADB_VALID(adb
));
2633 REQUIRE(FIND_EVENTFREED(find
));
2635 bucket
= find
->name_bucket
;
2636 INSIST(bucket
== DNS_ADB_INVALIDBUCKET
);
2638 UNLOCK(&find
->lock
);
2641 * The find doesn't exist on any list, and nothing is locked.
2642 * Return the find to the memory pool, and decrement the adb's
2645 ai
= ISC_LIST_HEAD(find
->list
);
2646 while (ai
!= NULL
) {
2647 ISC_LIST_UNLINK(find
->list
, ai
, publink
);
2650 INSIST(DNS_ADBENTRY_VALID(entry
));
2651 RUNTIME_CHECK(dec_entry_refcnt(adb
, entry
, ISC_TRUE
) ==
2653 free_adbaddrinfo(adb
, &ai
);
2654 ai
= ISC_LIST_HEAD(find
->list
);
2658 * WARNING: The find is freed with the adb locked. This is done
2659 * to avoid a race condition where we free the find, some other
2660 * thread tests to see if it should be destroyed, detects it should
2661 * be, destroys it, and then we try to lock it for our check, but the
2662 * lock is destroyed.
2665 if (free_adbfind(adb
, &find
))
2671 dns_adb_cancelfind(dns_adbfind_t
*find
) {
2680 DP(DEF_LEVEL
, "dns_adb_cancelfind on find %p", find
);
2683 REQUIRE(DNS_ADB_VALID(adb
));
2685 REQUIRE(!FIND_EVENTFREED(find
));
2686 REQUIRE(FIND_WANTEVENT(find
));
2688 bucket
= find
->name_bucket
;
2689 if (bucket
== DNS_ADB_INVALIDBUCKET
)
2693 * We need to get the adbname's lock to unlink the find.
2695 unlock_bucket
= bucket
;
2696 violate_locking_hierarchy(&find
->lock
, &adb
->namelocks
[unlock_bucket
]);
2697 bucket
= find
->name_bucket
;
2698 if (bucket
!= DNS_ADB_INVALIDBUCKET
) {
2699 ISC_LIST_UNLINK(find
->adbname
->finds
, find
, plink
);
2700 find
->adbname
= NULL
;
2701 find
->name_bucket
= DNS_ADB_INVALIDBUCKET
;
2703 UNLOCK(&adb
->namelocks
[unlock_bucket
]);
2704 bucket
= DNS_ADB_INVALIDBUCKET
;
2708 if (!FIND_EVENTSENT(find
)) {
2710 task
= ev
->ev_sender
;
2711 ev
->ev_sender
= find
;
2712 ev
->ev_type
= DNS_EVENT_ADBCANCELED
;
2713 ev
->ev_destroy
= event_free
;
2714 ev
->ev_destroy_arg
= find
;
2715 find
->result_v4
= ISC_R_CANCELED
;
2716 find
->result_v6
= ISC_R_CANCELED
;
2718 DP(DEF_LEVEL
, "sending event %p to task %p for find %p",
2721 isc_task_sendanddetach(&task
, (isc_event_t
**)&ev
);
2724 UNLOCK(&find
->lock
);
2728 dns_adb_dump(dns_adb_t
*adb
, FILE *f
) {
2732 REQUIRE(DNS_ADB_VALID(adb
));
2736 * Lock the adb itself, lock all the name buckets, then lock all
2737 * the entry buckets. This should put the adb into a state where
2738 * nothing can change, so we can iterate through everything and
2739 * print at our leisure.
2743 isc_stdtime_get(&now
);
2745 for (i
= 0; i
< NBUCKETS
; i
++)
2746 RUNTIME_CHECK(cleanup_names(adb
, i
, now
) == ISC_FALSE
);
2747 for (i
= 0; i
< NBUCKETS
; i
++)
2748 RUNTIME_CHECK(cleanup_entries(adb
, i
, now
) == ISC_FALSE
);
2750 dump_adb(adb
, f
, ISC_FALSE
, now
);
2755 dump_ttl(FILE *f
, const char *legend
, isc_stdtime_t value
, isc_stdtime_t now
) {
2756 if (value
== INT_MAX
)
2758 fprintf(f
, " [%s TTL %d]", legend
, value
- now
);
2762 dump_adb(dns_adb_t
*adb
, FILE *f
, isc_boolean_t debug
, isc_stdtime_t now
) {
2764 dns_adbname_t
*name
;
2765 dns_adbentry_t
*entry
;
2767 fprintf(f
, ";\n; Address database dump\n;\n");
2769 fprintf(f
, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2770 adb
, adb
->erefcnt
, adb
->irefcnt
,
2771 isc_mempool_getallocated(adb
->nhmp
));
2773 for (i
= 0; i
< NBUCKETS
; i
++)
2774 LOCK(&adb
->namelocks
[i
]);
2775 for (i
= 0; i
< NBUCKETS
; i
++)
2776 LOCK(&adb
->entrylocks
[i
]);
2781 for (i
= 0; i
< NBUCKETS
; i
++) {
2782 name
= ISC_LIST_HEAD(adb
->names
[i
]);
2786 fprintf(f
, "; bucket %d\n", i
);
2789 name
= ISC_LIST_NEXT(name
, plink
))
2792 fprintf(f
, "; name %p (flags %08x)\n",
2796 print_dns_name(f
, &name
->name
);
2797 if (dns_name_countlabels(&name
->target
) > 0) {
2798 fprintf(f
, " alias ");
2799 print_dns_name(f
, &name
->target
);
2802 dump_ttl(f
, "v4", name
->expire_v4
, now
);
2803 dump_ttl(f
, "v6", name
->expire_v6
, now
);
2804 dump_ttl(f
, "target", name
->expire_target
, now
);
2806 fprintf(f
, " [v4 %s] [v6 %s]",
2807 errnames
[name
->fetch_err
],
2808 errnames
[name
->fetch6_err
]);
2812 print_namehook_list(f
, "v4", &name
->v4
, debug
, now
);
2813 print_namehook_list(f
, "v6", &name
->v6
, debug
, now
);
2816 print_fetch_list(f
, name
);
2818 print_find_list(f
, name
);
2823 fprintf(f
, ";\n; Unassociated entries\n;\n");
2825 for (i
= 0; i
< NBUCKETS
; i
++) {
2826 entry
= ISC_LIST_HEAD(adb
->entries
[i
]);
2827 while (entry
!= NULL
) {
2828 if (entry
->refcnt
== 0)
2829 dump_entry(f
, entry
, debug
, now
);
2830 entry
= ISC_LIST_NEXT(entry
, plink
);
2837 for (i
= 0; i
< NBUCKETS
; i
++)
2838 UNLOCK(&adb
->entrylocks
[i
]);
2839 for (i
= 0; i
< NBUCKETS
; i
++)
2840 UNLOCK(&adb
->namelocks
[i
]);
2844 dump_entry(FILE *f
, dns_adbentry_t
*entry
, isc_boolean_t debug
,
2847 char addrbuf
[ISC_NETADDR_FORMATSIZE
];
2848 char typebuf
[DNS_RDATATYPE_FORMATSIZE
];
2849 isc_netaddr_t netaddr
;
2850 dns_adblameinfo_t
*li
;
2852 isc_netaddr_fromsockaddr(&netaddr
, &entry
->sockaddr
);
2853 isc_netaddr_format(&netaddr
, addrbuf
, sizeof(addrbuf
));
2856 fprintf(f
, ";\t%p: refcnt %u\n", entry
, entry
->refcnt
);
2858 fprintf(f
, ";\t%s [srtt %u] [flags %08x]",
2859 addrbuf
, entry
->srtt
, entry
->flags
);
2860 if (entry
->expires
!= 0)
2861 fprintf(f
, " [ttl %d]", entry
->expires
- now
);
2863 for (li
= ISC_LIST_HEAD(entry
->lameinfo
);
2865 li
= ISC_LIST_NEXT(li
, plink
)) {
2866 fprintf(f
, ";\t\t");
2867 print_dns_name(f
, &li
->qname
);
2868 dns_rdatatype_format(li
->qtype
, typebuf
, sizeof(typebuf
));
2869 fprintf(f
, " %s [lame TTL %d]\n", typebuf
,
2870 li
->lame_timer
- now
);
2875 dns_adb_dumpfind(dns_adbfind_t
*find
, FILE *f
) {
2878 dns_adbaddrinfo_t
*ai
;
2882 * Not used currently, in the API Just In Case we
2883 * want to dump out the name and/or entries too.
2888 fprintf(f
, ";Find %p\n", find
);
2889 fprintf(f
, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2890 find
->query_pending
, find
->partial_result
,
2891 find
->options
, find
->flags
);
2892 fprintf(f
, ";\tname_bucket %d, name %p, event sender %p\n",
2893 find
->name_bucket
, find
->adbname
, find
->event
.ev_sender
);
2895 ai
= ISC_LIST_HEAD(find
->list
);
2897 fprintf(f
, "\tAddresses:\n");
2898 while (ai
!= NULL
) {
2900 switch (sa
->type
.sa
.sa_family
) {
2902 tmpp
= inet_ntop(AF_INET
, &sa
->type
.sin
.sin_addr
,
2906 tmpp
= inet_ntop(AF_INET6
, &sa
->type
.sin6
.sin6_addr
,
2914 tmpp
= "BadAddress";
2916 fprintf(f
, "\t\tentry %p, flags %08x"
2917 " srtt %u addr %s\n",
2918 ai
->entry
, ai
->flags
, ai
->srtt
, tmpp
);
2920 ai
= ISC_LIST_NEXT(ai
, publink
);
2923 UNLOCK(&find
->lock
);
2927 print_dns_name(FILE *f
, dns_name_t
*name
) {
2928 char buf
[DNS_NAME_FORMATSIZE
];
2932 dns_name_format(name
, buf
, sizeof(buf
));
2933 fprintf(f
, "%s", buf
);
2937 print_namehook_list(FILE *f
, const char *legend
, dns_adbnamehooklist_t
*list
,
2938 isc_boolean_t debug
, isc_stdtime_t now
)
2940 dns_adbnamehook_t
*nh
;
2942 for (nh
= ISC_LIST_HEAD(*list
);
2944 nh
= ISC_LIST_NEXT(nh
, plink
))
2947 fprintf(f
, ";\tHook(%s) %p\n", legend
, nh
);
2948 dump_entry(f
, nh
->entry
, debug
, now
);
2953 print_fetch(FILE *f
, dns_adbfetch_t
*ft
, const char *type
) {
2954 fprintf(f
, "\t\tFetch(%s): %p -> { fetch %p }\n",
2955 type
, ft
, ft
->fetch
);
2959 print_fetch_list(FILE *f
, dns_adbname_t
*n
) {
2960 if (NAME_FETCH_A(n
))
2961 print_fetch(f
, n
->fetch_a
, "A");
2962 if (NAME_FETCH_AAAA(n
))
2963 print_fetch(f
, n
->fetch_aaaa
, "AAAA");
2967 print_find_list(FILE *f
, dns_adbname_t
*name
) {
2968 dns_adbfind_t
*find
;
2970 find
= ISC_LIST_HEAD(name
->finds
);
2971 while (find
!= NULL
) {
2972 dns_adb_dumpfind(find
, f
);
2973 find
= ISC_LIST_NEXT(find
, plink
);
2978 dbfind_name(dns_adbname_t
*adbname
, isc_stdtime_t now
, dns_rdatatype_t rdtype
)
2980 isc_result_t result
;
2981 dns_rdataset_t rdataset
;
2983 dns_fixedname_t foundname
;
2986 INSIST(DNS_ADBNAME_VALID(adbname
));
2988 INSIST(DNS_ADB_VALID(adb
));
2989 INSIST(rdtype
== dns_rdatatype_a
|| rdtype
== dns_rdatatype_aaaa
);
2991 dns_fixedname_init(&foundname
);
2992 fname
= dns_fixedname_name(&foundname
);
2993 dns_rdataset_init(&rdataset
);
2995 if (rdtype
== dns_rdatatype_a
)
2996 adbname
->fetch_err
= FIND_ERR_UNEXPECTED
;
2998 adbname
->fetch6_err
= FIND_ERR_UNEXPECTED
;
3000 result
= dns_view_find(adb
->view
, &adbname
->name
, rdtype
, now
,
3001 NAME_GLUEOK(adbname
) ? DNS_DBFIND_GLUEOK
: 0,
3002 ISC_TF(NAME_HINTOK(adbname
)),
3003 NULL
, NULL
, fname
, &rdataset
, NULL
);
3005 /* XXXVIX this switch statement is too sparse to gen a jump table. */
3011 * Found in the database. Even if we can't copy out
3012 * any information, return success, or else a fetch
3013 * will be made, which will only make things worse.
3015 if (rdtype
== dns_rdatatype_a
)
3016 adbname
->fetch_err
= FIND_ERR_SUCCESS
;
3018 adbname
->fetch6_err
= FIND_ERR_SUCCESS
;
3019 result
= import_rdataset(adbname
, &rdataset
, now
);
3021 case DNS_R_NXDOMAIN
:
3024 * We're authoritative and the data doesn't exist.
3025 * Make up a negative cache entry so we don't ask again
3028 * XXXRTH What time should we use? I'm putting in 30 seconds
3031 if (rdtype
== dns_rdatatype_a
) {
3032 adbname
->expire_v4
= now
+ 30;
3034 "adb name %p: Caching auth negative entry for A",
3036 if (result
== DNS_R_NXDOMAIN
)
3037 adbname
->fetch_err
= FIND_ERR_NXDOMAIN
;
3039 adbname
->fetch_err
= FIND_ERR_NXRRSET
;
3042 "adb name %p: Caching auth negative entry for AAAA",
3044 adbname
->expire_v6
= now
+ 30;
3045 if (result
== DNS_R_NXDOMAIN
)
3046 adbname
->fetch6_err
= FIND_ERR_NXDOMAIN
;
3048 adbname
->fetch6_err
= FIND_ERR_NXRRSET
;
3051 case DNS_R_NCACHENXDOMAIN
:
3052 case DNS_R_NCACHENXRRSET
:
3054 * We found a negative cache entry. Pull the TTL from it
3055 * so we won't ask again for a while.
3057 rdataset
.ttl
= ttlclamp(rdataset
.ttl
);
3058 if (rdtype
== dns_rdatatype_a
) {
3059 adbname
->expire_v4
= rdataset
.ttl
+ now
;
3060 if (result
== DNS_R_NCACHENXDOMAIN
)
3061 adbname
->fetch_err
= FIND_ERR_NXDOMAIN
;
3063 adbname
->fetch_err
= FIND_ERR_NXRRSET
;
3065 "adb name %p: Caching negative entry for A (ttl %u)",
3066 adbname
, rdataset
.ttl
);
3069 "adb name %p: Caching negative entry for AAAA (ttl %u)",
3070 adbname
, rdataset
.ttl
);
3071 adbname
->expire_v6
= rdataset
.ttl
+ now
;
3072 if (result
== DNS_R_NCACHENXDOMAIN
)
3073 adbname
->fetch6_err
= FIND_ERR_NXDOMAIN
;
3075 adbname
->fetch6_err
= FIND_ERR_NXRRSET
;
3081 * Clear the hint and glue flags, so this will match
3084 adbname
->flags
&= ~(DNS_ADBFIND_GLUEOK
| DNS_ADBFIND_HINTOK
);
3086 rdataset
.ttl
= ttlclamp(rdataset
.ttl
);
3087 clean_target(adb
, &adbname
->target
);
3088 adbname
->expire_target
= INT_MAX
;
3089 result
= set_target(adb
, &adbname
->name
, fname
, &rdataset
,
3091 if (result
== ISC_R_SUCCESS
) {
3092 result
= DNS_R_ALIAS
;
3094 "adb name %p: caching alias target",
3096 adbname
->expire_target
= rdataset
.ttl
+ now
;
3098 if (rdtype
== dns_rdatatype_a
)
3099 adbname
->fetch_err
= FIND_ERR_SUCCESS
;
3101 adbname
->fetch6_err
= FIND_ERR_SUCCESS
;
3105 if (dns_rdataset_isassociated(&rdataset
))
3106 dns_rdataset_disassociate(&rdataset
);
3112 fetch_callback(isc_task_t
*task
, isc_event_t
*ev
) {
3113 dns_fetchevent_t
*dev
;
3114 dns_adbname_t
*name
;
3116 dns_adbfetch_t
*fetch
;
3118 isc_eventtype_t ev_status
;
3120 isc_result_t result
;
3121 unsigned int address_type
;
3122 isc_boolean_t want_check_exit
= ISC_FALSE
;
3126 INSIST(ev
->ev_type
== DNS_EVENT_FETCHDONE
);
3127 dev
= (dns_fetchevent_t
*)ev
;
3129 INSIST(DNS_ADBNAME_VALID(name
));
3131 INSIST(DNS_ADB_VALID(adb
));
3133 bucket
= name
->lock_bucket
;
3134 LOCK(&adb
->namelocks
[bucket
]);
3136 INSIST(NAME_FETCH_A(name
) || NAME_FETCH_AAAA(name
));
3138 if (NAME_FETCH_A(name
) && (name
->fetch_a
->fetch
== dev
->fetch
)) {
3139 address_type
= DNS_ADBFIND_INET
;
3140 fetch
= name
->fetch_a
;
3141 name
->fetch_a
= NULL
;
3142 } else if (NAME_FETCH_AAAA(name
)
3143 && (name
->fetch_aaaa
->fetch
== dev
->fetch
)) {
3144 address_type
= DNS_ADBFIND_INET6
;
3145 fetch
= name
->fetch_aaaa
;
3146 name
->fetch_aaaa
= NULL
;
3150 INSIST(address_type
!= 0 && fetch
!= NULL
);
3152 dns_resolver_destroyfetch(&fetch
->fetch
);
3155 ev_status
= DNS_EVENT_ADBNOMOREADDRESSES
;
3158 * Cleanup things we don't care about.
3160 if (dev
->node
!= NULL
)
3161 dns_db_detachnode(dev
->db
, &dev
->node
);
3162 if (dev
->db
!= NULL
)
3163 dns_db_detach(&dev
->db
);
3166 * If this name is marked as dead, clean up, throwing away
3167 * potentially good data.
3169 if (NAME_DEAD(name
)) {
3170 free_adbfetch(adb
, &fetch
);
3171 isc_event_free(&ev
);
3173 want_check_exit
= kill_name(&name
, DNS_EVENT_ADBCANCELED
);
3175 UNLOCK(&adb
->namelocks
[bucket
]);
3177 if (want_check_exit
) {
3186 isc_stdtime_get(&now
);
3189 * If we got a negative cache response, remember it.
3191 if (NCACHE_RESULT(dev
->result
)) {
3192 dev
->rdataset
->ttl
= ttlclamp(dev
->rdataset
->ttl
);
3193 if (address_type
== DNS_ADBFIND_INET
) {
3194 DP(NCACHE_LEVEL
, "adb fetch name %p: "
3195 "caching negative entry for A (ttl %u)",
3196 name
, dev
->rdataset
->ttl
);
3197 name
->expire_v4
= ISC_MIN(name
->expire_v4
,
3198 dev
->rdataset
->ttl
+ now
);
3199 if (dev
->result
== DNS_R_NCACHENXDOMAIN
)
3200 name
->fetch_err
= FIND_ERR_NXDOMAIN
;
3202 name
->fetch_err
= FIND_ERR_NXRRSET
;
3203 inc_stats(adb
, dns_resstatscounter_gluefetchv4fail
);
3205 DP(NCACHE_LEVEL
, "adb fetch name %p: "
3206 "caching negative entry for AAAA (ttl %u)",
3207 name
, dev
->rdataset
->ttl
);
3208 name
->expire_v6
= ISC_MIN(name
->expire_v6
,
3209 dev
->rdataset
->ttl
+ now
);
3210 if (dev
->result
== DNS_R_NCACHENXDOMAIN
)
3211 name
->fetch6_err
= FIND_ERR_NXDOMAIN
;
3213 name
->fetch6_err
= FIND_ERR_NXRRSET
;
3214 inc_stats(adb
, dns_resstatscounter_gluefetchv6fail
);
3220 * Handle CNAME/DNAME.
3222 if (dev
->result
== DNS_R_CNAME
|| dev
->result
== DNS_R_DNAME
) {
3223 dev
->rdataset
->ttl
= ttlclamp(dev
->rdataset
->ttl
);
3224 clean_target(adb
, &name
->target
);
3225 name
->expire_target
= INT_MAX
;
3226 result
= set_target(adb
, &name
->name
,
3227 dns_fixedname_name(&dev
->foundname
),
3230 if (result
== ISC_R_SUCCESS
) {
3232 "adb fetch name %p: caching alias target",
3234 name
->expire_target
= dev
->rdataset
->ttl
+ now
;
3240 * Did we get back junk? If so, and there are no more fetches
3241 * sitting out there, tell all the finds about it.
3243 if (dev
->result
!= ISC_R_SUCCESS
) {
3244 char buf
[DNS_NAME_FORMATSIZE
];
3246 dns_name_format(&name
->name
, buf
, sizeof(buf
));
3247 DP(DEF_LEVEL
, "adb: fetch of '%s' %s failed: %s",
3248 buf
, address_type
== DNS_ADBFIND_INET
? "A" : "AAAA",
3249 dns_result_totext(dev
->result
));
3250 /* XXXMLG Don't pound on bad servers. */
3251 if (address_type
== DNS_ADBFIND_INET
) {
3252 name
->expire_v4
= ISC_MIN(name
->expire_v4
, now
+ 300);
3253 name
->fetch_err
= FIND_ERR_FAILURE
;
3254 inc_stats(adb
, dns_resstatscounter_gluefetchv4fail
);
3256 name
->expire_v6
= ISC_MIN(name
->expire_v6
, now
+ 300);
3257 name
->fetch6_err
= FIND_ERR_FAILURE
;
3258 inc_stats(adb
, dns_resstatscounter_gluefetchv6fail
);
3264 * We got something potentially useful.
3266 result
= import_rdataset(name
, &fetch
->rdataset
, now
);
3269 if (result
== ISC_R_SUCCESS
) {
3270 ev_status
= DNS_EVENT_ADBMOREADDRESSES
;
3271 if (address_type
== DNS_ADBFIND_INET
)
3272 name
->fetch_err
= FIND_ERR_SUCCESS
;
3274 name
->fetch6_err
= FIND_ERR_SUCCESS
;
3278 free_adbfetch(adb
, &fetch
);
3279 isc_event_free(&ev
);
3281 clean_finds_at_name(name
, ev_status
, address_type
);
3283 UNLOCK(&adb
->namelocks
[bucket
]);
3287 fetch_name(dns_adbname_t
*adbname
,
3288 isc_boolean_t start_at_zone
,
3289 dns_rdatatype_t type
)
3291 isc_result_t result
;
3292 dns_adbfetch_t
*fetch
= NULL
;
3294 dns_fixedname_t fixed
;
3296 dns_rdataset_t rdataset
;
3297 dns_rdataset_t
*nameservers
;
3298 unsigned int options
;
3300 INSIST(DNS_ADBNAME_VALID(adbname
));
3302 INSIST(DNS_ADB_VALID(adb
));
3304 INSIST((type
== dns_rdatatype_a
&& !NAME_FETCH_V4(adbname
)) ||
3305 (type
== dns_rdatatype_aaaa
&& !NAME_FETCH_V6(adbname
)));
3307 adbname
->fetch_err
= FIND_ERR_NOTFOUND
;
3311 dns_rdataset_init(&rdataset
);
3313 options
= DNS_FETCHOPT_NOVALIDATE
;
3314 if (start_at_zone
) {
3316 "fetch_name: starting at zone for name %p",
3318 dns_fixedname_init(&fixed
);
3319 name
= dns_fixedname_name(&fixed
);
3320 result
= dns_view_findzonecut2(adb
->view
, &adbname
->name
, name
,
3321 0, 0, ISC_TRUE
, ISC_FALSE
,
3323 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_HINT
)
3325 nameservers
= &rdataset
;
3326 options
|= DNS_FETCHOPT_UNSHARED
;
3329 fetch
= new_adbfetch(adb
);
3330 if (fetch
== NULL
) {
3331 result
= ISC_R_NOMEMORY
;
3335 result
= dns_resolver_createfetch(adb
->view
->resolver
, &adbname
->name
,
3336 type
, name
, nameservers
, NULL
,
3337 options
, adb
->task
, fetch_callback
,
3338 adbname
, &fetch
->rdataset
, NULL
,
3340 if (result
!= ISC_R_SUCCESS
)
3343 if (type
== dns_rdatatype_a
) {
3344 adbname
->fetch_a
= fetch
;
3345 inc_stats(adb
, dns_resstatscounter_gluefetchv4
);
3347 adbname
->fetch_aaaa
= fetch
;
3348 inc_stats(adb
, dns_resstatscounter_gluefetchv6
);
3350 fetch
= NULL
; /* Keep us from cleaning this up below. */
3354 free_adbfetch(adb
, &fetch
);
3355 if (dns_rdataset_isassociated(&rdataset
))
3356 dns_rdataset_disassociate(&rdataset
);
3362 * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3363 * since these can be extracted from the find itself.
3366 dns_adb_marklame(dns_adb_t
*adb
, dns_adbaddrinfo_t
*addr
, dns_name_t
*qname
,
3367 dns_rdatatype_t qtype
, isc_stdtime_t expire_time
)
3369 dns_adblameinfo_t
*li
;
3371 isc_result_t result
= ISC_R_SUCCESS
;
3373 REQUIRE(DNS_ADB_VALID(adb
));
3374 REQUIRE(DNS_ADBADDRINFO_VALID(addr
));
3375 REQUIRE(qname
!= NULL
);
3377 bucket
= addr
->entry
->lock_bucket
;
3378 LOCK(&adb
->entrylocks
[bucket
]);
3379 li
= ISC_LIST_HEAD(addr
->entry
->lameinfo
);
3380 while (li
!= NULL
&&
3381 (li
->qtype
!= qtype
|| !dns_name_equal(qname
, &li
->qname
)))
3382 li
= ISC_LIST_NEXT(li
, plink
);
3384 if (expire_time
> li
->lame_timer
)
3385 li
->lame_timer
= expire_time
;
3388 li
= new_adblameinfo(adb
, qname
, qtype
);
3390 result
= ISC_R_NOMEMORY
;
3394 li
->lame_timer
= expire_time
;
3396 ISC_LIST_PREPEND(addr
->entry
->lameinfo
, li
, plink
);
3398 UNLOCK(&adb
->entrylocks
[bucket
]);
3404 dns_adb_adjustsrtt(dns_adb_t
*adb
, dns_adbaddrinfo_t
*addr
,
3405 unsigned int rtt
, unsigned int factor
)
3408 unsigned int new_srtt
;
3411 REQUIRE(DNS_ADB_VALID(adb
));
3412 REQUIRE(DNS_ADBADDRINFO_VALID(addr
));
3413 REQUIRE(factor
<= 10);
3415 bucket
= addr
->entry
->lock_bucket
;
3416 LOCK(&adb
->entrylocks
[bucket
]);
3418 if (factor
== DNS_ADB_RTTADJAGE
)
3419 new_srtt
= addr
->entry
->srtt
* 98 / 100;
3421 new_srtt
= (addr
->entry
->srtt
/ 10 * factor
)
3422 + (rtt
/ 10 * (10 - factor
));
3424 addr
->entry
->srtt
= new_srtt
;
3425 addr
->srtt
= new_srtt
;
3427 isc_stdtime_get(&now
);
3428 addr
->entry
->expires
= now
+ ADB_ENTRY_WINDOW
;
3430 UNLOCK(&adb
->entrylocks
[bucket
]);
3434 dns_adb_changeflags(dns_adb_t
*adb
, dns_adbaddrinfo_t
*addr
,
3435 unsigned int bits
, unsigned int mask
)
3439 REQUIRE(DNS_ADB_VALID(adb
));
3440 REQUIRE(DNS_ADBADDRINFO_VALID(addr
));
3442 bucket
= addr
->entry
->lock_bucket
;
3443 LOCK(&adb
->entrylocks
[bucket
]);
3445 addr
->entry
->flags
= (addr
->entry
->flags
& ~mask
) | (bits
& mask
);
3447 * Note that we do not update the other bits in addr->flags with
3448 * the most recent values from addr->entry->flags.
3450 addr
->flags
= (addr
->flags
& ~mask
) | (bits
& mask
);
3452 UNLOCK(&adb
->entrylocks
[bucket
]);
3456 dns_adb_findaddrinfo(dns_adb_t
*adb
, isc_sockaddr_t
*sa
,
3457 dns_adbaddrinfo_t
**addrp
, isc_stdtime_t now
)
3460 dns_adbentry_t
*entry
;
3461 dns_adbaddrinfo_t
*addr
;
3462 isc_result_t result
;
3465 REQUIRE(DNS_ADB_VALID(adb
));
3466 REQUIRE(addrp
!= NULL
&& *addrp
== NULL
);
3470 result
= ISC_R_SUCCESS
;
3471 bucket
= DNS_ADB_INVALIDBUCKET
;
3472 entry
= find_entry_and_lock(adb
, sa
, &bucket
, now
);
3473 if (adb
->entry_sd
[bucket
]) {
3474 result
= ISC_R_SHUTTINGDOWN
;
3477 if (entry
== NULL
) {
3479 * We don't know anything about this address.
3481 entry
= new_adbentry(adb
);
3482 if (entry
== NULL
) {
3483 result
= ISC_R_NOMEMORY
;
3486 entry
->sockaddr
= *sa
;
3487 link_entry(adb
, bucket
, entry
);
3488 DP(ENTER_LEVEL
, "findaddrinfo: new entry %p", entry
);
3490 DP(ENTER_LEVEL
, "findaddrinfo: found entry %p", entry
);
3492 port
= isc_sockaddr_getport(sa
);
3493 addr
= new_adbaddrinfo(adb
, entry
, port
);
3495 result
= ISC_R_NOMEMORY
;
3497 inc_entry_refcnt(adb
, entry
, ISC_FALSE
);
3502 UNLOCK(&adb
->entrylocks
[bucket
]);
3508 dns_adb_freeaddrinfo(dns_adb_t
*adb
, dns_adbaddrinfo_t
**addrp
) {
3509 dns_adbaddrinfo_t
*addr
;
3510 dns_adbentry_t
*entry
;
3513 isc_boolean_t want_check_exit
= ISC_FALSE
;
3515 REQUIRE(DNS_ADB_VALID(adb
));
3516 REQUIRE(addrp
!= NULL
);
3518 REQUIRE(DNS_ADBADDRINFO_VALID(addr
));
3519 entry
= addr
->entry
;
3520 REQUIRE(DNS_ADBENTRY_VALID(entry
));
3522 isc_stdtime_get(&now
);
3526 bucket
= addr
->entry
->lock_bucket
;
3527 LOCK(&adb
->entrylocks
[bucket
]);
3529 entry
->expires
= now
+ ADB_ENTRY_WINDOW
;
3531 want_check_exit
= dec_entry_refcnt(adb
, entry
, ISC_FALSE
);
3533 UNLOCK(&adb
->entrylocks
[bucket
]);
3536 free_adbaddrinfo(adb
, &addr
);
3538 if (want_check_exit
) {
3546 dns_adb_flush(dns_adb_t
*adb
) {
3549 INSIST(DNS_ADB_VALID(adb
));
3554 * Call our cleanup routines.
3556 for (i
= 0; i
< NBUCKETS
; i
++)
3557 RUNTIME_CHECK(cleanup_names(adb
, i
, INT_MAX
) == ISC_FALSE
);
3558 for (i
= 0; i
< NBUCKETS
; i
++)
3559 RUNTIME_CHECK(cleanup_entries(adb
, i
, INT_MAX
) == ISC_FALSE
);
3561 #ifdef DUMP_ADB_AFTER_CLEANING
3562 dump_adb(adb
, stdout
, ISC_TRUE
, INT_MAX
);
3569 dns_adb_flushname(dns_adb_t
*adb
, dns_name_t
*name
) {
3570 dns_adbname_t
*adbname
;
3571 dns_adbname_t
*nextname
;
3574 INSIST(DNS_ADB_VALID(adb
));
3577 bucket
= dns_name_hash(name
, ISC_FALSE
) % NBUCKETS
;
3578 LOCK(&adb
->namelocks
[bucket
]);
3579 adbname
= ISC_LIST_HEAD(adb
->names
[bucket
]);
3580 while (adbname
!= NULL
) {
3581 nextname
= ISC_LIST_NEXT(adbname
, plink
);
3582 if (!NAME_DEAD(adbname
) &&
3583 dns_name_equal(name
, &adbname
->name
)) {
3584 RUNTIME_CHECK(kill_name(&adbname
,
3585 DNS_EVENT_ADBCANCELED
) ==
3590 UNLOCK(&adb
->namelocks
[bucket
]);
3595 water(void *arg
, int mark
) {
3596 dns_adb_t
*adb
= arg
;
3597 isc_boolean_t overmem
= ISC_TF(mark
== ISC_MEM_HIWATER
);
3599 REQUIRE(DNS_ADB_VALID(adb
));
3601 DP(ISC_LOG_DEBUG(1),
3602 "adb reached %s water mark", overmem
? "high" : "low");
3605 * We can't use adb->lock as there is potential for water
3606 * to be called when adb->lock is held.
3608 LOCK(&adb
->overmemlock
);
3609 if (adb
->overmem
!= overmem
) {
3610 adb
->overmem
= overmem
;
3611 isc_mem_waterack(adb
->mctx
, mark
);
3613 UNLOCK(&adb
->overmemlock
);
3617 dns_adb_setadbsize(dns_adb_t
*adb
, isc_uint32_t size
) {
3618 isc_uint32_t hiwater
;
3619 isc_uint32_t lowater
;
3621 INSIST(DNS_ADB_VALID(adb
));
3623 if (size
!= 0 && size
< DNS_ADB_MINADBSIZE
)
3624 size
= DNS_ADB_MINADBSIZE
;
3626 hiwater
= size
- (size
>> 3); /* Approximately 7/8ths. */
3627 lowater
= size
- (size
>> 2); /* Approximately 3/4ths. */
3629 if (size
== 0 || hiwater
== 0 || lowater
== 0)
3630 isc_mem_setwater(adb
->mctx
, water
, adb
, 0, 0);
3632 isc_mem_setwater(adb
->mctx
, water
, adb
, hiwater
, lowater
);