acpi.4: Add some missing references.
[dragonfly.git] / contrib / bind-9.3 / lib / dns / adb.c
1 /*
2  * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: adb.c,v 1.181.2.11.2.26 2006/01/04 23:50:20 marka Exp $ */
19
20 /*
21  * Implementation notes
22  * --------------------
23  *
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.
27  *
28  */
29
30 /*
31  * After we have cleaned all buckets, dump the database contents.
32  */
33 #if 0
34 #define DUMP_ADB_AFTER_CLEANING
35 #endif
36
37 #include <config.h>
38
39 #include <limits.h>
40
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?) */
45 #include <isc/task.h>
46 #include <isc/timer.h>
47 #include <isc/util.h>
48
49 #include <dns/adb.h>
50 #include <dns/db.h>
51 #include <dns/events.h>
52 #include <dns/log.h>
53 #include <dns/rdata.h>
54 #include <dns/rdataset.h>
55 #include <dns/rdatastruct.h>
56 #include <dns/resolver.h>
57 #include <dns/result.h>
58
59 #define DNS_ADB_MAGIC             ISC_MAGIC('D', 'a', 'd', 'b')
60 #define DNS_ADB_VALID(x)          ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
61 #define DNS_ADBNAME_MAGIC         ISC_MAGIC('a', 'd', 'b', 'N')
62 #define DNS_ADBNAME_VALID(x)      ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
63 #define DNS_ADBNAMEHOOK_MAGIC     ISC_MAGIC('a', 'd', 'N', 'H')
64 #define DNS_ADBNAMEHOOK_VALID(x)  ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
65 #define DNS_ADBZONEINFO_MAGIC     ISC_MAGIC('a', 'd', 'b', 'Z')
66 #define DNS_ADBZONEINFO_VALID(x)  ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
67 #define DNS_ADBENTRY_MAGIC        ISC_MAGIC('a', 'd', 'b', 'E')
68 #define DNS_ADBENTRY_VALID(x)     ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
69 #define DNS_ADBFETCH_MAGIC        ISC_MAGIC('a', 'd', 'F', '4')
70 #define DNS_ADBFETCH_VALID(x)     ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
71 #define DNS_ADBFETCH6_MAGIC       ISC_MAGIC('a', 'd', 'F', '6')
72 #define DNS_ADBFETCH6_VALID(x)    ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
73
74 /*
75  * The number of buckets needs to be a prime (for good hashing).
76  *
77  * XXXRTH  How many buckets do we need?
78  */
79 #define NBUCKETS               1009     /* how many buckets for names/addrs */
80
81 /*
82  * For type 3 negative cache entries, we will remember that the address is
83  * broken for this long.  XXXMLG This is also used for actual addresses, too.
84  * The intent is to keep us from constantly asking about A/AAAA records
85  * if the zone has extremely low TTLs.
86  */
87 #define ADB_CACHE_MINIMUM       10      /* seconds */
88 #define ADB_CACHE_MAXIMUM       86400   /* seconds (86400 = 24 hours) */
89 #define ADB_ENTRY_WINDOW        1800    /* seconds */
90
91 /*
92  * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
93  * buckets are cleaned in CLEAN_PERIOD seconds.
94  */
95 #define CLEAN_PERIOD            3600
96 #define CLEAN_SECONDS           30
97 #define CLEAN_BUCKETS           ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
98
99 #define FREE_ITEMS              64      /* free count for memory pools */
100 #define FILL_COUNT              16      /* fill count for memory pools */
101
102 #define DNS_ADB_INVALIDBUCKET (-1)      /* invalid bucket address */
103
104 #define DNS_ADB_MINADBSIZE      (1024*1024)     /* 1 Megabyte */
105
106 typedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
107 typedef struct dns_adbnamehook dns_adbnamehook_t;
108 typedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
109 typedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
110 typedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
111 typedef struct dns_adbfetch dns_adbfetch_t;
112 typedef struct dns_adbfetch6 dns_adbfetch6_t;
113
114 struct dns_adb {
115         unsigned int                    magic;
116
117         isc_mutex_t                     lock;
118         isc_mutex_t                     reflock; /* Covers irefcnt, erefcnt */
119         isc_mem_t                      *mctx;
120         dns_view_t                     *view;
121         isc_timermgr_t                 *timermgr;
122         isc_timer_t                    *timer;
123         isc_taskmgr_t                  *taskmgr;
124         isc_task_t                     *task;
125         isc_boolean_t                   overmem;
126
127         isc_interval_t                  tick_interval;
128         int                             next_cleanbucket;
129
130         unsigned int                    irefcnt;
131         unsigned int                    erefcnt;
132
133         isc_mutex_t                     mplock;
134         isc_mempool_t                  *nmp;    /* dns_adbname_t */
135         isc_mempool_t                  *nhmp;   /* dns_adbnamehook_t */
136         isc_mempool_t                  *zimp;   /* dns_adbzoneinfo_t */
137         isc_mempool_t                  *emp;    /* dns_adbentry_t */
138         isc_mempool_t                  *ahmp;   /* dns_adbfind_t */
139         isc_mempool_t                  *aimp;   /* dns_adbaddrinfo_t */
140         isc_mempool_t                  *afmp;   /* dns_adbfetch_t */
141
142         /*
143          * Bucketized locks and lists for names.
144          *
145          * XXXRTH  Have a per-bucket structure that contains all of these?
146          */
147         dns_adbnamelist_t               names[NBUCKETS];
148         isc_mutex_t                     namelocks[NBUCKETS];
149         isc_boolean_t                   name_sd[NBUCKETS];
150         unsigned int                    name_refcnt[NBUCKETS];
151
152         /*
153          * Bucketized locks for entries.
154          *
155          * XXXRTH  Have a per-bucket structure that contains all of these?
156          */
157         dns_adbentrylist_t              entries[NBUCKETS];
158         isc_mutex_t                     entrylocks[NBUCKETS];
159         isc_boolean_t                   entry_sd[NBUCKETS]; /* shutting down */
160         unsigned int                    entry_refcnt[NBUCKETS];
161
162         isc_event_t                     cevent;
163         isc_boolean_t                   cevent_sent;
164         isc_boolean_t                   shutting_down;
165         isc_eventlist_t                 whenshutdown;
166 };
167
168 /*
169  * XXXMLG  Document these structures.
170  */
171
172 struct dns_adbname {
173         unsigned int                    magic;
174         dns_name_t                      name;
175         dns_adb_t                      *adb;
176         unsigned int                    partial_result;
177         unsigned int                    flags;
178         int                             lock_bucket;
179         dns_name_t                      target;
180         isc_stdtime_t                   expire_target;
181         isc_stdtime_t                   expire_v4;
182         isc_stdtime_t                   expire_v6;
183         unsigned int                    chains;
184         dns_adbnamehooklist_t           v4;
185         dns_adbnamehooklist_t           v6;
186         dns_adbfetch_t                 *fetch_a;
187         dns_adbfetch_t                 *fetch_aaaa;
188         unsigned int                    fetch_err;
189         unsigned int                    fetch6_err;
190         dns_adbfindlist_t               finds;
191         ISC_LINK(dns_adbname_t)         plink;
192 };
193
194 struct dns_adbfetch {
195         unsigned int                    magic;
196         dns_adbnamehook_t              *namehook;
197         dns_adbentry_t                 *entry;
198         dns_fetch_t                    *fetch;
199         dns_rdataset_t                  rdataset;
200 };
201
202 /*
203  * dns_adbnamehook_t
204  *
205  * This is a small widget that dangles off a dns_adbname_t.  It contains a
206  * pointer to the address information about this host, and a link to the next
207  * namehook that will contain the next address this host has.
208  */
209 struct dns_adbnamehook {
210         unsigned int                    magic;
211         dns_adbentry_t                 *entry;
212         ISC_LINK(dns_adbnamehook_t)     plink;
213 };
214
215 /*
216  * dns_adbzoneinfo_t
217  *
218  * This is a small widget that holds zone-specific information about an
219  * address.  Currently limited to lameness, but could just as easily be
220  * extended to other types of information about zones.
221  */
222 struct dns_adbzoneinfo {
223         unsigned int                    magic;
224
225         dns_name_t                      zone;
226         isc_stdtime_t                   lame_timer;
227
228         ISC_LINK(dns_adbzoneinfo_t)     plink;
229 };
230
231 /*
232  * An address entry.  It holds quite a bit of information about addresses,
233  * including edns state (in "flags"), rtt, and of course the address of
234  * the host.
235  */
236 struct dns_adbentry {
237         unsigned int                    magic;
238
239         int                             lock_bucket;
240         unsigned int                    refcnt;
241
242         unsigned int                    flags;
243         unsigned int                    srtt;
244         isc_sockaddr_t                  sockaddr;
245
246         isc_stdtime_t                   expires;
247         /*
248          * A nonzero 'expires' field indicates that the entry should
249          * persist until that time.  This allows entries found
250          * using dns_adb_findaddrinfo() to persist for a limited time
251          * even though they are not necessarily associated with a
252          * name.
253          */
254
255         ISC_LIST(dns_adbzoneinfo_t)     zoneinfo;
256         ISC_LINK(dns_adbentry_t)        plink;
257 };
258
259 /*
260  * Internal functions (and prototypes).
261  */
262 static inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
263 static inline void free_adbname(dns_adb_t *, dns_adbname_t **);
264 static inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
265                                                  dns_adbentry_t *);
266 static inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
267 static inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
268 static inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
269 static inline dns_adbentry_t *new_adbentry(dns_adb_t *);
270 static inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
271 static inline dns_adbfind_t *new_adbfind(dns_adb_t *);
272 static inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
273 static inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
274                                                  in_port_t);
275 static inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
276 static inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
277 static inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
278                                                 unsigned int, int *);
279 static inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
280                                                   isc_sockaddr_t *, int *);
281 static void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
282 static void print_dns_name(FILE *, dns_name_t *);
283 static void print_namehook_list(FILE *, const char *legend,
284                                 dns_adbnamehooklist_t *list,
285                                 isc_boolean_t debug,
286                                 isc_stdtime_t now);
287 static void print_find_list(FILE *, dns_adbname_t *);
288 static void print_fetch_list(FILE *, dns_adbname_t *);
289 static inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
290 static inline void inc_adb_irefcnt(dns_adb_t *);
291 static inline void inc_adb_erefcnt(dns_adb_t *);
292 static inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
293                                     isc_boolean_t);
294 static inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
295                                              isc_boolean_t);
296 static inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
297 static isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
298 static void clean_target(dns_adb_t *, dns_name_t *);
299 static void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
300                                 unsigned int);
301 static isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
302                                             isc_boolean_t);
303 static void cancel_fetches_at_name(dns_adbname_t *);
304 static isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
305                                 dns_rdatatype_t);
306 static isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
307                                dns_rdatatype_t);
308 static inline void check_exit(dns_adb_t *);
309 static void timer_cleanup(isc_task_t *, isc_event_t *);
310 static void destroy(dns_adb_t *);
311 static isc_boolean_t shutdown_names(dns_adb_t *);
312 static isc_boolean_t shutdown_entries(dns_adb_t *);
313 static inline void link_name(dns_adb_t *, int, dns_adbname_t *);
314 static inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
315 static inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
316 static inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
317 static isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
318 static void water(void *, int);
319 static void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
320
321 /*
322  * MUST NOT overlap DNS_ADBFIND_* flags!
323  */
324 #define FIND_EVENT_SENT         0x40000000
325 #define FIND_EVENT_FREED        0x80000000
326 #define FIND_EVENTSENT(h)       (((h)->flags & FIND_EVENT_SENT) != 0)
327 #define FIND_EVENTFREED(h)      (((h)->flags & FIND_EVENT_FREED) != 0)
328
329 #define NAME_NEEDS_POKE         0x80000000
330 #define NAME_IS_DEAD            0x40000000
331 #define NAME_HINT_OK            DNS_ADBFIND_HINTOK
332 #define NAME_GLUE_OK            DNS_ADBFIND_GLUEOK
333 #define NAME_STARTATZONE        DNS_ADBFIND_STARTATZONE
334 #define NAME_DEAD(n)            (((n)->flags & NAME_IS_DEAD) != 0)
335 #define NAME_NEEDSPOKE(n)       (((n)->flags & NAME_NEEDS_POKE) != 0)
336 #define NAME_GLUEOK(n)          (((n)->flags & NAME_GLUE_OK) != 0)
337 #define NAME_HINTOK(n)          (((n)->flags & NAME_HINT_OK) != 0)
338
339 /*
340  * To the name, address classes are all that really exist.  If it has a
341  * V6 address it doesn't care if it came from a AAAA query.
342  */
343 #define NAME_HAS_V4(n)          (!ISC_LIST_EMPTY((n)->v4))
344 #define NAME_HAS_V6(n)          (!ISC_LIST_EMPTY((n)->v6))
345 #define NAME_HAS_ADDRS(n)       (NAME_HAS_V4(n) || NAME_HAS_V6(n))
346
347 /*
348  * Fetches are broken out into A and AAAA types.  In some cases,
349  * however, it makes more sense to test for a particular class of fetches,
350  * like V4 or V6 above.
351  * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
352  * are now equal to FETCH_V4 and FETCH_V6, respectively.
353  */
354 #define NAME_FETCH_A(n)         ((n)->fetch_a != NULL)
355 #define NAME_FETCH_AAAA(n)      ((n)->fetch_aaaa != NULL)
356 #define NAME_FETCH_V4(n)        (NAME_FETCH_A(n))
357 #define NAME_FETCH_V6(n)        (NAME_FETCH_AAAA(n))
358 #define NAME_FETCH(n)           (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
359
360 /*
361  * Find options and tests to see if there are addresses on the list.
362  */
363 #define FIND_WANTEVENT(fn)      (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
364 #define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
365 #define FIND_AVOIDFETCHES(fn)   (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
366                                  != 0)
367 #define FIND_STARTATZONE(fn)    (((fn)->options & DNS_ADBFIND_STARTATZONE) \
368                                  != 0)
369 #define FIND_HINTOK(fn)         (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
370 #define FIND_GLUEOK(fn)         (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
371 #define FIND_HAS_ADDRS(fn)      (!ISC_LIST_EMPTY((fn)->list))
372 #define FIND_RETURNLAME(fn)     (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
373
374 /*
375  * These are currently used on simple unsigned ints, so they are
376  * not really associated with any particular type.
377  */
378 #define WANT_INET(x)            (((x) & DNS_ADBFIND_INET) != 0)
379 #define WANT_INET6(x)           (((x) & DNS_ADBFIND_INET6) != 0)
380
381 #define EXPIRE_OK(exp, now)     ((exp == INT_MAX) || (exp < now))
382
383 /*
384  * Find out if the flags on a name (nf) indicate if it is a hint or
385  * glue, and compare this to the appropriate bits set in o, to see if
386  * this is ok.
387  */
388 #define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
389 #define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
390 #define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
391 #define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
392                                     ((o) & DNS_ADBFIND_STARTATZONE))
393
394 #define ENTER_LEVEL             ISC_LOG_DEBUG(50)
395 #define EXIT_LEVEL              ENTER_LEVEL
396 #define CLEAN_LEVEL             ISC_LOG_DEBUG(100)
397 #define DEF_LEVEL               ISC_LOG_DEBUG(5)
398 #define NCACHE_LEVEL            ISC_LOG_DEBUG(20)
399
400 #define NCACHE_RESULT(r)        ((r) == DNS_R_NCACHENXDOMAIN || \
401                                  (r) == DNS_R_NCACHENXRRSET)
402 #define AUTH_NX(r)              ((r) == DNS_R_NXDOMAIN || \
403                                  (r) == DNS_R_NXRRSET)
404 #define NXDOMAIN_RESULT(r)      ((r) == DNS_R_NXDOMAIN || \
405                                  (r) == DNS_R_NCACHENXDOMAIN)
406 #define NXRRSET_RESULT(r)       ((r) == DNS_R_NCACHENXRRSET || \
407                                  (r) == DNS_R_NXRRSET || \
408                                  (r) == DNS_R_HINTNXRRSET)
409
410 /*
411  * Error state rankings.
412  */
413
414 #define FIND_ERR_SUCCESS                0  /* highest rank */
415 #define FIND_ERR_CANCELED               1
416 #define FIND_ERR_FAILURE                2
417 #define FIND_ERR_NXDOMAIN               3
418 #define FIND_ERR_NXRRSET                4
419 #define FIND_ERR_UNEXPECTED             5
420 #define FIND_ERR_NOTFOUND               6
421 #define FIND_ERR_MAX                    7
422
423 static const char *errnames[] = {
424         "success",
425         "canceled",
426         "failure",
427         "nxdomain",
428         "nxrrset",
429         "unexpected",
430         "not_found"
431 };
432
433 #define NEWERR(old, new)        (ISC_MIN((old), (new)))
434
435 static isc_result_t find_err_map[FIND_ERR_MAX] = {
436         ISC_R_SUCCESS,
437         ISC_R_CANCELED,
438         ISC_R_FAILURE,
439         DNS_R_NXDOMAIN,
440         DNS_R_NXRRSET,
441         ISC_R_UNEXPECTED,
442         ISC_R_NOTFOUND          /* not YET found */
443 };
444
445 static void
446 DP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
447
448 static void
449 DP(int level, const char *format, ...) {
450         va_list args;
451
452         va_start(args, format);
453         isc_log_vwrite(dns_lctx,
454                        DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
455                        level, format, args);
456         va_end(args);
457 }
458
459 static inline dns_ttl_t
460 ttlclamp(dns_ttl_t ttl) {
461         if (ttl < ADB_CACHE_MINIMUM)
462                 ttl = ADB_CACHE_MINIMUM;
463         if (ttl > ADB_CACHE_MAXIMUM)
464                 ttl = ADB_CACHE_MAXIMUM;
465
466         return (ttl);
467 }
468
469 /*
470  * Requires the adbname bucket be locked and that no entry buckets be locked.
471  *
472  * This code handles A and AAAA rdatasets only.
473  */
474 static isc_result_t
475 import_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
476                 isc_stdtime_t now)
477 {
478         isc_result_t result;
479         dns_adb_t *adb;
480         dns_adbnamehook_t *nh;
481         dns_adbnamehook_t *anh;
482         dns_rdata_t rdata = DNS_RDATA_INIT;
483         struct in_addr ina;
484         struct in6_addr in6a;
485         isc_sockaddr_t sockaddr;
486         dns_adbentry_t *foundentry;  /* NO CLEAN UP! */
487         int addr_bucket;
488         isc_boolean_t new_addresses_added;
489         dns_rdatatype_t rdtype;
490         unsigned int findoptions;
491
492         INSIST(DNS_ADBNAME_VALID(adbname));
493         adb = adbname->adb;
494         INSIST(DNS_ADB_VALID(adb));
495
496         rdtype = rdataset->type;
497         INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
498         if (rdtype == dns_rdatatype_a)
499                 findoptions = DNS_ADBFIND_INET;
500         else
501                 findoptions = DNS_ADBFIND_INET6;
502
503         addr_bucket = DNS_ADB_INVALIDBUCKET;
504         new_addresses_added = ISC_FALSE;
505
506         nh = NULL;
507         result = dns_rdataset_first(rdataset);
508         while (result == ISC_R_SUCCESS) {
509                 dns_rdata_reset(&rdata);
510                 dns_rdataset_current(rdataset, &rdata);
511                 if (rdtype == dns_rdatatype_a) {
512                         INSIST(rdata.length == 4);
513                         memcpy(&ina.s_addr, rdata.data, 4);
514                         isc_sockaddr_fromin(&sockaddr, &ina, 0);
515                 } else {
516                         INSIST(rdata.length == 16);
517                         memcpy(in6a.s6_addr, rdata.data, 16);
518                         isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
519                 }
520
521                 INSIST(nh == NULL);
522                 nh = new_adbnamehook(adb, NULL);
523                 if (nh == NULL) {
524                         adbname->partial_result |= findoptions;
525                         result = ISC_R_NOMEMORY;
526                         goto fail;
527                 }
528
529                 foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
530                 if (foundentry == NULL) {
531                         dns_adbentry_t *entry;
532
533                         entry = new_adbentry(adb);
534                         if (entry == NULL) {
535                                 adbname->partial_result |= findoptions;
536                                 result = ISC_R_NOMEMORY;
537                                 goto fail;
538                         }
539
540                         entry->sockaddr = sockaddr;
541                         entry->refcnt = 1;
542
543                         nh->entry = entry;
544
545                         link_entry(adb, addr_bucket, entry);
546                 } else {
547                         for (anh = ISC_LIST_HEAD(adbname->v4);
548                              anh != NULL;
549                              anh = ISC_LIST_NEXT(anh, plink))
550                                 if (anh->entry == foundentry)
551                                         break;
552                         if (anh == NULL) {
553                                 foundentry->refcnt++;
554                                 nh->entry = foundentry;
555                         } else
556                                 free_adbnamehook(adb, &nh);
557                 }
558
559                 new_addresses_added = ISC_TRUE;
560                 if (nh != NULL) {
561                         if (rdtype == dns_rdatatype_a)
562                                 ISC_LIST_APPEND(adbname->v4, nh, plink);
563                         else
564                                 ISC_LIST_APPEND(adbname->v6, nh, plink);
565                 }
566                 nh = NULL;
567                 result = dns_rdataset_next(rdataset);
568         }
569
570  fail:
571         if (nh != NULL)
572                 free_adbnamehook(adb, &nh);
573
574         if (addr_bucket != DNS_ADB_INVALIDBUCKET)
575                 UNLOCK(&adb->entrylocks[addr_bucket]);
576
577         if (rdataset->trust == dns_trust_glue ||
578             rdataset->trust == dns_trust_additional)
579                 rdataset->ttl = ADB_CACHE_MINIMUM;
580         else
581                 rdataset->ttl = ttlclamp(rdataset->ttl);
582
583         if (rdtype == dns_rdatatype_a) {
584                 DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
585                    adbname->expire_v4, now + rdataset->ttl);
586                 adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
587                                              now + rdataset->ttl);
588         } else {
589                 DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
590                    adbname->expire_v6, now + rdataset->ttl);
591                 adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
592                                              now + rdataset->ttl);
593         }
594
595         if (new_addresses_added) {
596                 /*
597                  * Lie a little here.  This is more or less so code that cares
598                  * can find out if any new information was added or not.
599                  */
600                 return (ISC_R_SUCCESS);
601         }
602
603         return (result);
604 }
605
606 /*
607  * Requires the name's bucket be locked.
608  */
609 static isc_boolean_t
610 kill_name(dns_adbname_t **n, isc_eventtype_t ev) {
611         dns_adbname_t *name;
612         isc_boolean_t result = ISC_FALSE;
613         isc_boolean_t result4, result6;
614         dns_adb_t *adb;
615
616         INSIST(n != NULL);
617         name = *n;
618         *n = NULL;
619         INSIST(DNS_ADBNAME_VALID(name));
620         adb = name->adb;
621         INSIST(DNS_ADB_VALID(adb));
622
623         DP(DEF_LEVEL, "killing name %p", name);
624
625         /*
626          * If we're dead already, just check to see if we should go
627          * away now or not.
628          */
629         if (NAME_DEAD(name) && !NAME_FETCH(name)) {
630                 result = unlink_name(adb, name);
631                 free_adbname(adb, &name);
632                 if (result)
633                         result = dec_adb_irefcnt(adb);
634                 return (result);
635         }
636
637         /*
638          * Clean up the name's various lists.  These two are destructive
639          * in that they will always empty the list.
640          */
641         clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
642         result4 = clean_namehooks(adb, &name->v4);
643         result6 = clean_namehooks(adb, &name->v6);
644         clean_target(adb, &name->target);
645         result = ISC_TF(result4 || result6);
646
647         /*
648          * If fetches are running, cancel them.  If none are running, we can
649          * just kill the name here.
650          */
651         if (!NAME_FETCH(name)) {
652                 INSIST(result == ISC_FALSE);
653                 result = unlink_name(adb, name);
654                 free_adbname(adb, &name);
655                 if (result)
656                         result = dec_adb_irefcnt(adb);
657         } else {
658                 name->flags |= NAME_IS_DEAD;
659                 cancel_fetches_at_name(name);
660         }
661         return (result);
662 }
663
664 /*
665  * Requires the name's bucket be locked and no entry buckets be locked.
666  */
667 static isc_boolean_t
668 check_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
669                        isc_boolean_t overmem)
670 {
671         dns_adb_t *adb;
672         isc_boolean_t expire;
673         isc_boolean_t result4 = ISC_FALSE;
674         isc_boolean_t result6 = ISC_FALSE;
675
676         INSIST(DNS_ADBNAME_VALID(name));
677         adb = name->adb;
678         INSIST(DNS_ADB_VALID(adb));
679
680         if (overmem) {
681                 isc_uint32_t val;
682
683                 isc_random_get(&val);
684
685                 expire = ISC_TF((val % 4) == 0);
686         } else
687                 expire = ISC_FALSE;
688
689         /*
690          * Check to see if we need to remove the v4 addresses
691          */
692         if (!NAME_FETCH_V4(name) &&
693             (expire || EXPIRE_OK(name->expire_v4, now))) {
694                 if (NAME_HAS_V4(name)) {
695                         DP(DEF_LEVEL, "expiring v4 for name %p", name);
696                         result4 = clean_namehooks(adb, &name->v4);
697                         name->partial_result &= ~DNS_ADBFIND_INET;
698                 }
699                 name->expire_v4 = INT_MAX;
700                 name->fetch_err = FIND_ERR_UNEXPECTED;
701         }
702
703         /*
704          * Check to see if we need to remove the v6 addresses
705          */
706         if (!NAME_FETCH_V6(name) &&
707             (expire || EXPIRE_OK(name->expire_v6, now))) {
708                 if (NAME_HAS_V6(name)) {
709                         DP(DEF_LEVEL, "expiring v6 for name %p", name);
710                         result6 = clean_namehooks(adb, &name->v6);
711                         name->partial_result &= ~DNS_ADBFIND_INET6;
712                 }
713                 name->expire_v6 = INT_MAX;
714                 name->fetch6_err = FIND_ERR_UNEXPECTED;
715         }
716
717         /*
718          * Check to see if we need to remove the alias target.
719          */
720         if (expire || EXPIRE_OK(name->expire_target, now)) {
721                 clean_target(adb, &name->target);
722                 name->expire_target = INT_MAX;
723         }
724         return (ISC_TF(result4 || result6));
725 }
726
727 /*
728  * Requires the name's bucket be locked.
729  */
730 static inline void
731 link_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
732         INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
733
734         ISC_LIST_PREPEND(adb->names[bucket], name, plink);
735         name->lock_bucket = bucket;
736         adb->name_refcnt[bucket]++;
737 }
738
739 /*
740  * Requires the name's bucket be locked.
741  */
742 static inline isc_boolean_t
743 unlink_name(dns_adb_t *adb, dns_adbname_t *name) {
744         int bucket;
745         isc_boolean_t result = ISC_FALSE;
746
747         bucket = name->lock_bucket;
748         INSIST(bucket != DNS_ADB_INVALIDBUCKET);
749
750         ISC_LIST_UNLINK(adb->names[bucket], name, plink);
751         name->lock_bucket = DNS_ADB_INVALIDBUCKET;
752         INSIST(adb->name_refcnt[bucket] > 0);
753         adb->name_refcnt[bucket]--;
754         if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
755                 result = ISC_TRUE;
756         return (result);
757 }
758
759 /*
760  * Requires the entry's bucket be locked.
761  */
762 static inline void
763 link_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
764         ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
765         entry->lock_bucket = bucket;
766         adb->entry_refcnt[bucket]++;
767 }
768
769 /*
770  * Requires the entry's bucket be locked.
771  */
772 static inline isc_boolean_t
773 unlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
774         int bucket;
775         isc_boolean_t result = ISC_FALSE;
776
777         bucket = entry->lock_bucket;
778         INSIST(bucket != DNS_ADB_INVALIDBUCKET);
779
780         ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
781         entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
782         INSIST(adb->entry_refcnt[bucket] > 0);
783         adb->entry_refcnt[bucket]--;
784         if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
785                 result = ISC_TRUE;
786         return (result);
787 }
788
789 static inline void
790 violate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
791         if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
792                 UNLOCK(have);
793                 LOCK(want);
794                 LOCK(have);
795         }
796 }
797
798 /*
799  * The ADB _MUST_ be locked before calling.  Also, exit conditions must be
800  * checked after calling this function.
801  */
802 static isc_boolean_t
803 shutdown_names(dns_adb_t *adb) {
804         int bucket;
805         isc_boolean_t result = ISC_FALSE;
806         dns_adbname_t *name;
807         dns_adbname_t *next_name;
808
809         for (bucket = 0; bucket < NBUCKETS; bucket++) {
810                 LOCK(&adb->namelocks[bucket]);
811                 adb->name_sd[bucket] = ISC_TRUE;
812
813                 name = ISC_LIST_HEAD(adb->names[bucket]);
814                 if (name == NULL) {
815                         /*
816                          * This bucket has no names.  We must decrement the
817                          * irefcnt ourselves, since it will not be
818                          * automatically triggered by a name being unlinked.
819                          */
820                         INSIST(result == ISC_FALSE);
821                         result = dec_adb_irefcnt(adb);
822                 } else {
823                         /*
824                          * Run through the list.  For each name, clean up finds
825                          * found there, and cancel any fetches running.  When
826                          * all the fetches are canceled, the name will destroy
827                          * itself.
828                          */
829                         while (name != NULL) {
830                                 next_name = ISC_LIST_NEXT(name, plink);
831                                 INSIST(result == ISC_FALSE);
832                                 result = kill_name(&name,
833                                                    DNS_EVENT_ADBSHUTDOWN);
834                                 name = next_name;
835                         }
836                 }
837
838                 UNLOCK(&adb->namelocks[bucket]);
839         }
840         return (result);
841 }
842
843 /*
844  * The ADB _MUST_ be locked before calling.  Also, exit conditions must be
845  * checked after calling this function.
846  */
847 static isc_boolean_t
848 shutdown_entries(dns_adb_t *adb) {
849         int bucket;
850         isc_boolean_t result = ISC_FALSE;
851         dns_adbentry_t *entry;
852         dns_adbentry_t *next_entry;
853
854         for (bucket = 0; bucket < NBUCKETS; bucket++) {
855                 LOCK(&adb->entrylocks[bucket]);
856                 adb->entry_sd[bucket] = ISC_TRUE;
857
858                 entry = ISC_LIST_HEAD(adb->entries[bucket]);
859                 if (entry == NULL) {
860                         /*
861                          * This bucket has no entries.  We must decrement the
862                          * irefcnt ourselves, since it will not be
863                          * automatically triggered by an entry being unlinked.
864                          */
865                         result = dec_adb_irefcnt(adb);
866                 } else {
867                         /*
868                          * Run through the list.  Cleanup any entries not
869                          * associated with names, and which are not in use.
870                          */
871                         while (entry != NULL) {
872                                 next_entry = ISC_LIST_NEXT(entry, plink);
873                                 if (entry->refcnt == 0 &&
874                                     entry->expires != 0) {
875                                         result = unlink_entry(adb, entry);
876                                         free_adbentry(adb, &entry);
877                                         if (result)
878                                                 result = dec_adb_irefcnt(adb);
879                                 }
880                                 entry = next_entry;
881                         }
882                 }
883
884                 UNLOCK(&adb->entrylocks[bucket]);
885         }
886         return (result);
887 }
888
889 /*
890  * Name bucket must be locked
891  */
892 static void
893 cancel_fetches_at_name(dns_adbname_t *name) {
894         if (NAME_FETCH_A(name))
895             dns_resolver_cancelfetch(name->fetch_a->fetch);
896
897         if (NAME_FETCH_AAAA(name))
898             dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
899 }
900
901 /*
902  * Assumes the name bucket is locked.
903  */
904 static isc_boolean_t
905 clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
906         dns_adbentry_t *entry;
907         dns_adbnamehook_t *namehook;
908         int addr_bucket;
909         isc_boolean_t result = ISC_FALSE;
910
911         addr_bucket = DNS_ADB_INVALIDBUCKET;
912         namehook = ISC_LIST_HEAD(*namehooks);
913         while (namehook != NULL) {
914                 INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
915
916                 /*
917                  * Clean up the entry if needed.
918                  */
919                 entry = namehook->entry;
920                 if (entry != NULL) {
921                         INSIST(DNS_ADBENTRY_VALID(entry));
922
923                         if (addr_bucket != entry->lock_bucket) {
924                                 if (addr_bucket != DNS_ADB_INVALIDBUCKET)
925                                         UNLOCK(&adb->entrylocks[addr_bucket]);
926                                 addr_bucket = entry->lock_bucket;
927                                 LOCK(&adb->entrylocks[addr_bucket]);
928                         }
929
930                         result = dec_entry_refcnt(adb, entry, ISC_FALSE);
931                 }
932
933                 /*
934                  * Free the namehook
935                  */
936                 namehook->entry = NULL;
937                 ISC_LIST_UNLINK(*namehooks, namehook, plink);
938                 free_adbnamehook(adb, &namehook);
939
940                 namehook = ISC_LIST_HEAD(*namehooks);
941         }
942
943         if (addr_bucket != DNS_ADB_INVALIDBUCKET)
944                 UNLOCK(&adb->entrylocks[addr_bucket]);
945         return (result);
946 }
947
948 static void
949 clean_target(dns_adb_t *adb, dns_name_t *target) {
950         if (dns_name_countlabels(target) > 0) {
951                 dns_name_free(target, adb->mctx);
952                 dns_name_init(target, NULL);
953         }
954 }
955
956 static isc_result_t
957 set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
958            dns_rdataset_t *rdataset, dns_name_t *target)
959 {
960         isc_result_t result;
961         dns_namereln_t namereln;
962         unsigned int nlabels;
963         int order;
964         dns_rdata_t rdata = DNS_RDATA_INIT;
965         dns_fixedname_t fixed1, fixed2;
966         dns_name_t *prefix, *new_target;
967
968         REQUIRE(dns_name_countlabels(target) == 0);
969
970         if (rdataset->type == dns_rdatatype_cname) {
971                 dns_rdata_cname_t cname;
972
973                 /*
974                  * Copy the CNAME's target into the target name.
975                  */
976                 result = dns_rdataset_first(rdataset);
977                 if (result != ISC_R_SUCCESS)
978                         return (result);
979                 dns_rdataset_current(rdataset, &rdata);
980                 result = dns_rdata_tostruct(&rdata, &cname, NULL);
981                 if (result != ISC_R_SUCCESS)
982                         return (result);
983                 result = dns_name_dup(&cname.cname, adb->mctx, target);
984                 dns_rdata_freestruct(&cname);
985                 if (result != ISC_R_SUCCESS)
986                         return (result);
987         } else {
988                 dns_rdata_dname_t dname;
989
990                 INSIST(rdataset->type == dns_rdatatype_dname);
991                 namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
992                 INSIST(namereln == dns_namereln_subdomain);
993                 /*
994                  * Get the target name of the DNAME.
995                  */
996                 result = dns_rdataset_first(rdataset);
997                 if (result != ISC_R_SUCCESS)
998                         return (result);
999                 dns_rdataset_current(rdataset, &rdata);
1000                 result = dns_rdata_tostruct(&rdata, &dname, NULL);
1001                 if (result != ISC_R_SUCCESS)
1002                         return (result);
1003                 /*
1004                  * Construct the new target name.
1005                  */
1006                 dns_fixedname_init(&fixed1);
1007                 prefix = dns_fixedname_name(&fixed1);
1008                 dns_fixedname_init(&fixed2);
1009                 new_target = dns_fixedname_name(&fixed2);
1010                 dns_name_split(name, nlabels, prefix, NULL);
1011                 result = dns_name_concatenate(prefix, &dname.dname, new_target,
1012                                               NULL);
1013                 dns_rdata_freestruct(&dname);
1014                 if (result != ISC_R_SUCCESS)
1015                         return (result);
1016                 result = dns_name_dup(new_target, adb->mctx, target);
1017                 if (result != ISC_R_SUCCESS)
1018                         return (result);
1019         }
1020
1021         return (ISC_R_SUCCESS);
1022 }
1023
1024 /*
1025  * Assumes nothing is locked, since this is called by the client.
1026  */
1027 static void
1028 event_free(isc_event_t *event) {
1029         dns_adbfind_t *find;
1030
1031         INSIST(event != NULL);
1032         find = event->ev_destroy_arg;
1033         INSIST(DNS_ADBFIND_VALID(find));
1034
1035         LOCK(&find->lock);
1036         find->flags |= FIND_EVENT_FREED;
1037         event->ev_destroy_arg = NULL;
1038         UNLOCK(&find->lock);
1039 }
1040
1041 /*
1042  * Assumes the name bucket is locked.
1043  */
1044 static void
1045 clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
1046                     unsigned int addrs)
1047 {
1048         isc_event_t *ev;
1049         isc_task_t *task;
1050         dns_adbfind_t *find;
1051         dns_adbfind_t *next_find;
1052         isc_boolean_t process;
1053         unsigned int wanted, notify;
1054
1055         DP(ENTER_LEVEL,
1056            "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
1057            name, evtype, addrs);
1058
1059         find = ISC_LIST_HEAD(name->finds);
1060         while (find != NULL) {
1061                 LOCK(&find->lock);
1062                 next_find = ISC_LIST_NEXT(find, plink);
1063
1064                 process = ISC_FALSE;
1065                 wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1066                 notify = wanted & addrs;
1067
1068                 switch (evtype) {
1069                 case DNS_EVENT_ADBMOREADDRESSES:
1070                         DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
1071                         if ((notify) != 0) {
1072                                 find->flags &= ~addrs;
1073                                 process = ISC_TRUE;
1074                         }
1075                         break;
1076                 case DNS_EVENT_ADBNOMOREADDRESSES:
1077                         DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
1078                         find->flags &= ~addrs;
1079                         wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
1080                         if (wanted == 0)
1081                                 process = ISC_TRUE;
1082                         break;
1083                 default:
1084                         find->flags &= ~addrs;
1085                         process = ISC_TRUE;
1086                 }
1087
1088                 if (process) {
1089                         DP(DEF_LEVEL, "cfan: processing find %p", find);
1090                         /*
1091                          * Unlink the find from the name, letting the caller
1092                          * call dns_adb_destroyfind() on it to clean it up
1093                          * later.
1094                          */
1095                         ISC_LIST_UNLINK(name->finds, find, plink);
1096                         find->adbname = NULL;
1097                         find->name_bucket = DNS_ADB_INVALIDBUCKET;
1098
1099                         INSIST(!FIND_EVENTSENT(find));
1100
1101                         ev = &find->event;
1102                         task = ev->ev_sender;
1103                         ev->ev_sender = find;
1104                         find->result_v4 = find_err_map[name->fetch_err];
1105                         find->result_v6 = find_err_map[name->fetch6_err];
1106                         ev->ev_type = evtype;
1107                         ev->ev_destroy = event_free;
1108                         ev->ev_destroy_arg = find;
1109
1110                         DP(DEF_LEVEL,
1111                            "sending event %p to task %p for find %p",
1112                            ev, task, find);
1113
1114                         isc_task_sendanddetach(&task, (isc_event_t **)&ev);
1115                 } else {
1116                         DP(DEF_LEVEL, "cfan: skipping find %p", find);
1117                 }
1118
1119                 UNLOCK(&find->lock);
1120                 find = next_find;
1121         }
1122
1123         DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
1124 }
1125
1126 static inline void
1127 check_exit(dns_adb_t *adb) {
1128         isc_event_t *event;
1129         /*
1130          * The caller must be holding the adb lock.
1131          */
1132         if (adb->shutting_down) {
1133                 /*
1134                  * If there aren't any external references either, we're
1135                  * done.  Send the control event to initiate shutdown.
1136                  */
1137                 INSIST(!adb->cevent_sent);      /* Sanity check. */
1138                 event = &adb->cevent;
1139                 isc_task_send(adb->task, &event);
1140                 adb->cevent_sent = ISC_TRUE;
1141         }
1142 }
1143
1144 static inline isc_boolean_t
1145 dec_adb_irefcnt(dns_adb_t *adb) {
1146         isc_event_t *event;
1147         isc_task_t *etask;
1148         isc_boolean_t result = ISC_FALSE;
1149
1150         LOCK(&adb->reflock);
1151
1152         INSIST(adb->irefcnt > 0);
1153         adb->irefcnt--;
1154
1155         if (adb->irefcnt == 0) {
1156                 event = ISC_LIST_HEAD(adb->whenshutdown);
1157                 while (event != NULL) {
1158                         ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
1159                         etask = event->ev_sender;
1160                         event->ev_sender = adb;
1161                         isc_task_sendanddetach(&etask, &event);
1162                         event = ISC_LIST_HEAD(adb->whenshutdown);
1163                 }
1164         }
1165
1166         if (adb->irefcnt == 0 && adb->erefcnt == 0)
1167                 result = ISC_TRUE;
1168         UNLOCK(&adb->reflock);
1169         return (result);
1170 }
1171
1172 static inline void
1173 inc_adb_irefcnt(dns_adb_t *adb) {
1174         LOCK(&adb->reflock);
1175         adb->irefcnt++;
1176         UNLOCK(&adb->reflock);
1177 }
1178
1179 static inline void
1180 inc_adb_erefcnt(dns_adb_t *adb) {
1181         LOCK(&adb->reflock);
1182         adb->erefcnt++;
1183         UNLOCK(&adb->reflock);
1184 }
1185
1186 static inline void
1187 inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1188         int bucket;
1189
1190         bucket = entry->lock_bucket;
1191
1192         if (lock)
1193                 LOCK(&adb->entrylocks[bucket]);
1194
1195         entry->refcnt++;
1196
1197         if (lock)
1198                 UNLOCK(&adb->entrylocks[bucket]);
1199 }
1200
1201 static inline isc_boolean_t
1202 dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
1203         int bucket;
1204         isc_boolean_t destroy_entry;
1205         isc_boolean_t result = ISC_FALSE;
1206
1207         bucket = entry->lock_bucket;
1208
1209         if (lock)
1210                 LOCK(&adb->entrylocks[bucket]);
1211
1212         INSIST(entry->refcnt > 0);
1213         entry->refcnt--;
1214
1215         destroy_entry = ISC_FALSE;
1216         if (entry->refcnt == 0 &&
1217             (adb->entry_sd[bucket] || entry->expires == 0)) {
1218                 destroy_entry = ISC_TRUE;
1219                 result = unlink_entry(adb, entry);
1220         }
1221
1222         if (lock)
1223                 UNLOCK(&adb->entrylocks[bucket]);
1224
1225         if (!destroy_entry)
1226                 return (result);
1227
1228         entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
1229
1230         free_adbentry(adb, &entry);
1231         if (result)
1232                 result =dec_adb_irefcnt(adb);
1233
1234         return (result);
1235 }
1236
1237 static inline dns_adbname_t *
1238 new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
1239         dns_adbname_t *name;
1240
1241         name = isc_mempool_get(adb->nmp);
1242         if (name == NULL)
1243                 return (NULL);
1244
1245         dns_name_init(&name->name, NULL);
1246         if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
1247                 isc_mempool_put(adb->nmp, name);
1248                 return (NULL);
1249         }
1250         dns_name_init(&name->target, NULL);
1251         name->magic = DNS_ADBNAME_MAGIC;
1252         name->adb = adb;
1253         name->partial_result = 0;
1254         name->flags = 0;
1255         name->expire_v4 = INT_MAX;
1256         name->expire_v6 = INT_MAX;
1257         name->expire_target = INT_MAX;
1258         name->chains = 0;
1259         name->lock_bucket = DNS_ADB_INVALIDBUCKET;
1260         ISC_LIST_INIT(name->v4);
1261         ISC_LIST_INIT(name->v6);
1262         name->fetch_a = NULL;
1263         name->fetch_aaaa = NULL;
1264         name->fetch_err = FIND_ERR_UNEXPECTED;
1265         name->fetch6_err = FIND_ERR_UNEXPECTED;
1266         ISC_LIST_INIT(name->finds);
1267         ISC_LINK_INIT(name, plink);
1268
1269         return (name);
1270 }
1271
1272 static inline void
1273 free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
1274         dns_adbname_t *n;
1275
1276         INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
1277         n = *name;
1278         *name = NULL;
1279
1280         INSIST(!NAME_HAS_V4(n));
1281         INSIST(!NAME_HAS_V6(n));
1282         INSIST(!NAME_FETCH(n));
1283         INSIST(ISC_LIST_EMPTY(n->finds));
1284         INSIST(!ISC_LINK_LINKED(n, plink));
1285         INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
1286         INSIST(n->adb == adb);
1287
1288         n->magic = 0;
1289         dns_name_free(&n->name, adb->mctx);
1290
1291         isc_mempool_put(adb->nmp, n);
1292 }
1293
1294 static inline dns_adbnamehook_t *
1295 new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
1296         dns_adbnamehook_t *nh;
1297
1298         nh = isc_mempool_get(adb->nhmp);
1299         if (nh == NULL)
1300                 return (NULL);
1301
1302         nh->magic = DNS_ADBNAMEHOOK_MAGIC;
1303         nh->entry = entry;
1304         ISC_LINK_INIT(nh, plink);
1305
1306         return (nh);
1307 }
1308
1309 static inline void
1310 free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
1311         dns_adbnamehook_t *nh;
1312
1313         INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
1314         nh = *namehook;
1315         *namehook = NULL;
1316
1317         INSIST(nh->entry == NULL);
1318         INSIST(!ISC_LINK_LINKED(nh, plink));
1319
1320         nh->magic = 0;
1321         isc_mempool_put(adb->nhmp, nh);
1322 }
1323
1324 static inline dns_adbzoneinfo_t *
1325 new_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
1326         dns_adbzoneinfo_t *zi;
1327
1328         zi = isc_mempool_get(adb->zimp);
1329         if (zi == NULL)
1330                 return (NULL);
1331
1332         dns_name_init(&zi->zone, NULL);
1333         if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
1334                 isc_mempool_put(adb->zimp, zi);
1335                 return (NULL);
1336         }
1337
1338         zi->magic = DNS_ADBZONEINFO_MAGIC;
1339         zi->lame_timer = 0;
1340         ISC_LINK_INIT(zi, plink);
1341
1342         return (zi);
1343 }
1344
1345 static inline void
1346 free_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
1347         dns_adbzoneinfo_t *zi;
1348
1349         INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
1350         zi = *zoneinfo;
1351         *zoneinfo = NULL;
1352
1353         INSIST(!ISC_LINK_LINKED(zi, plink));
1354
1355         dns_name_free(&zi->zone, adb->mctx);
1356
1357         zi->magic = 0;
1358
1359         isc_mempool_put(adb->zimp, zi);
1360 }
1361
1362 static inline dns_adbentry_t *
1363 new_adbentry(dns_adb_t *adb) {
1364         dns_adbentry_t *e;
1365         isc_uint32_t r;
1366
1367         e = isc_mempool_get(adb->emp);
1368         if (e == NULL)
1369                 return (NULL);
1370
1371         e->magic = DNS_ADBENTRY_MAGIC;
1372         e->lock_bucket = DNS_ADB_INVALIDBUCKET;
1373         e->refcnt = 0;
1374         e->flags = 0;
1375         isc_random_get(&r);
1376         e->srtt = (r & 0x1f) + 1;
1377         e->expires = 0;
1378         ISC_LIST_INIT(e->zoneinfo);
1379         ISC_LINK_INIT(e, plink);
1380
1381         return (e);
1382 }
1383
1384 static inline void
1385 free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
1386         dns_adbentry_t *e;
1387         dns_adbzoneinfo_t *zi;
1388
1389         INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
1390         e = *entry;
1391         *entry = NULL;
1392
1393         INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
1394         INSIST(e->refcnt == 0);
1395         INSIST(!ISC_LINK_LINKED(e, plink));
1396
1397         e->magic = 0;
1398
1399         zi = ISC_LIST_HEAD(e->zoneinfo);
1400         while (zi != NULL) {
1401                 ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
1402                 free_adbzoneinfo(adb, &zi);
1403                 zi = ISC_LIST_HEAD(e->zoneinfo);
1404         }
1405
1406         isc_mempool_put(adb->emp, e);
1407 }
1408
1409 static inline dns_adbfind_t *
1410 new_adbfind(dns_adb_t *adb) {
1411         dns_adbfind_t *h;
1412         isc_result_t result;
1413
1414         h = isc_mempool_get(adb->ahmp);
1415         if (h == NULL)
1416                 return (NULL);
1417
1418         /*
1419          * Public members.
1420          */
1421         h->magic = 0;
1422         h->adb = adb;
1423         h->partial_result = 0;
1424         h->options = 0;
1425         h->flags = 0;
1426         h->result_v4 = ISC_R_UNEXPECTED;
1427         h->result_v6 = ISC_R_UNEXPECTED;
1428         ISC_LINK_INIT(h, publink);
1429         ISC_LINK_INIT(h, plink);
1430         ISC_LIST_INIT(h->list);
1431         h->adbname = NULL;
1432         h->name_bucket = DNS_ADB_INVALIDBUCKET;
1433
1434         /*
1435          * private members
1436          */
1437         result = isc_mutex_init(&h->lock);
1438         if (result != ISC_R_SUCCESS) {
1439                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1440                                  "isc_mutex_init failed in new_adbfind()");
1441                 isc_mempool_put(adb->ahmp, h);
1442                 return (NULL);
1443         }
1444
1445         ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
1446                        NULL, NULL, h);
1447
1448         inc_adb_irefcnt(adb);
1449         h->magic = DNS_ADBFIND_MAGIC;
1450         return (h);
1451 }
1452
1453 static inline dns_adbfetch_t *
1454 new_adbfetch(dns_adb_t *adb) {
1455         dns_adbfetch_t *f;
1456
1457         f = isc_mempool_get(adb->afmp);
1458         if (f == NULL)
1459                 return (NULL);
1460
1461         f->magic = 0;
1462         f->namehook = NULL;
1463         f->entry = NULL;
1464         f->fetch = NULL;
1465
1466         f->namehook = new_adbnamehook(adb, NULL);
1467         if (f->namehook == NULL)
1468                 goto err;
1469
1470         f->entry = new_adbentry(adb);
1471         if (f->entry == NULL)
1472                 goto err;
1473
1474         dns_rdataset_init(&f->rdataset);
1475
1476         f->magic = DNS_ADBFETCH_MAGIC;
1477
1478         return (f);
1479
1480  err:
1481         if (f->namehook != NULL)
1482                 free_adbnamehook(adb, &f->namehook);
1483         if (f->entry != NULL)
1484                 free_adbentry(adb, &f->entry);
1485         isc_mempool_put(adb->afmp, f);
1486         return (NULL);
1487 }
1488
1489 static inline void
1490 free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
1491         dns_adbfetch_t *f;
1492
1493         INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
1494         f = *fetch;
1495         *fetch = NULL;
1496
1497         f->magic = 0;
1498
1499         if (f->namehook != NULL)
1500                 free_adbnamehook(adb, &f->namehook);
1501         if (f->entry != NULL)
1502                 free_adbentry(adb, &f->entry);
1503
1504         if (dns_rdataset_isassociated(&f->rdataset))
1505                 dns_rdataset_disassociate(&f->rdataset);
1506
1507         isc_mempool_put(adb->afmp, f);
1508 }
1509
1510 static inline isc_boolean_t
1511 free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
1512         dns_adbfind_t *find;
1513
1514         INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
1515         find = *findp;
1516         *findp = NULL;
1517
1518         INSIST(!FIND_HAS_ADDRS(find));
1519         INSIST(!ISC_LINK_LINKED(find, publink));
1520         INSIST(!ISC_LINK_LINKED(find, plink));
1521         INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
1522         INSIST(find->adbname == NULL);
1523
1524         find->magic = 0;
1525
1526         DESTROYLOCK(&find->lock);
1527         isc_mempool_put(adb->ahmp, find);
1528         return (dec_adb_irefcnt(adb));
1529 }
1530
1531 /*
1532  * Copy bits from the entry into the newly allocated addrinfo.  The entry
1533  * must be locked, and the reference count must be bumped up by one
1534  * if this function returns a valid pointer.
1535  */
1536 static inline dns_adbaddrinfo_t *
1537 new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
1538         dns_adbaddrinfo_t *ai;
1539
1540         ai = isc_mempool_get(adb->aimp);
1541         if (ai == NULL)
1542                 return (NULL);
1543
1544         ai->magic = DNS_ADBADDRINFO_MAGIC;
1545         ai->sockaddr = entry->sockaddr;
1546         isc_sockaddr_setport(&ai->sockaddr, port);
1547         ai->srtt = entry->srtt;
1548         ai->flags = entry->flags;
1549         ai->entry = entry;
1550         ISC_LINK_INIT(ai, publink);
1551
1552         return (ai);
1553 }
1554
1555 static inline void
1556 free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1557         dns_adbaddrinfo_t *ai;
1558
1559         INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
1560         ai = *ainfo;
1561         *ainfo = NULL;
1562
1563         INSIST(ai->entry == NULL);
1564         INSIST(!ISC_LINK_LINKED(ai, publink));
1565
1566         ai->magic = 0;
1567
1568         isc_mempool_put(adb->aimp, ai);
1569 }
1570
1571 /*
1572  * Search for the name.  NOTE:  The bucket is kept locked on both
1573  * success and failure, so it must always be unlocked by the caller!
1574  *
1575  * On the first call to this function, *bucketp must be set to
1576  * DNS_ADB_INVALIDBUCKET.
1577  */
1578 static inline dns_adbname_t *
1579 find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
1580                    unsigned int options, int *bucketp)
1581 {
1582         dns_adbname_t *adbname;
1583         int bucket;
1584
1585         bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
1586
1587         if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1588                 LOCK(&adb->namelocks[bucket]);
1589                 *bucketp = bucket;
1590         } else if (*bucketp != bucket) {
1591                 UNLOCK(&adb->namelocks[*bucketp]);
1592                 LOCK(&adb->namelocks[bucket]);
1593                 *bucketp = bucket;
1594         }
1595
1596         adbname = ISC_LIST_HEAD(adb->names[bucket]);
1597         while (adbname != NULL) {
1598                 if (!NAME_DEAD(adbname)) {
1599                         if (dns_name_equal(name, &adbname->name)
1600                             && GLUEHINT_OK(adbname, options)
1601                             && STARTATZONE_MATCHES(adbname, options))
1602                                 return (adbname);
1603                 }
1604                 adbname = ISC_LIST_NEXT(adbname, plink);
1605         }
1606
1607         return (NULL);
1608 }
1609
1610 /*
1611  * Search for the address.  NOTE:  The bucket is kept locked on both
1612  * success and failure, so it must always be unlocked by the caller.
1613  *
1614  * On the first call to this function, *bucketp must be set to
1615  * DNS_ADB_INVALIDBUCKET.  This will cause a lock to occur.  On
1616  * later calls (within the same "lock path") it can be left alone, so
1617  * if this function is called multiple times locking is only done if
1618  * the bucket changes.
1619  */
1620 static inline dns_adbentry_t *
1621 find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
1622         dns_adbentry_t *entry;
1623         int bucket;
1624
1625         bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
1626
1627         if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1628                 LOCK(&adb->entrylocks[bucket]);
1629                 *bucketp = bucket;
1630         } else if (*bucketp != bucket) {
1631                 UNLOCK(&adb->entrylocks[*bucketp]);
1632                 LOCK(&adb->entrylocks[bucket]);
1633                 *bucketp = bucket;
1634         }
1635
1636         entry = ISC_LIST_HEAD(adb->entries[bucket]);
1637         while (entry != NULL) {
1638                 if (isc_sockaddr_equal(addr, &entry->sockaddr))
1639                         return (entry);
1640                 entry = ISC_LIST_NEXT(entry, plink);
1641         }
1642
1643         return (NULL);
1644 }
1645
1646 /*
1647  * Entry bucket MUST be locked!
1648  */
1649 static isc_boolean_t
1650 entry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
1651                       isc_stdtime_t now)
1652 {
1653         dns_adbzoneinfo_t *zi, *next_zi;
1654         isc_boolean_t is_bad;
1655
1656         is_bad = ISC_FALSE;
1657
1658         zi = ISC_LIST_HEAD(entry->zoneinfo);
1659         if (zi == NULL)
1660                 return (ISC_FALSE);
1661         while (zi != NULL) {
1662                 next_zi = ISC_LIST_NEXT(zi, plink);
1663
1664                 /*
1665                  * Has the entry expired?
1666                  */
1667                 if (zi->lame_timer < now) {
1668                         ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
1669                         free_adbzoneinfo(adb, &zi);
1670                 }
1671
1672                 /*
1673                  * Order tests from least to most expensive.
1674                  */
1675                 if (zi != NULL && !is_bad) {
1676                         if (dns_name_equal(zone, &zi->zone))
1677                                 is_bad = ISC_TRUE;
1678                 }
1679
1680                 zi = next_zi;
1681         }
1682
1683         return (is_bad);
1684 }
1685
1686 static void
1687 copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
1688                     dns_adbname_t *name, isc_stdtime_t now)
1689 {
1690         dns_adbnamehook_t *namehook;
1691         dns_adbaddrinfo_t *addrinfo;
1692         dns_adbentry_t *entry;
1693         int bucket;
1694
1695         bucket = DNS_ADB_INVALIDBUCKET;
1696
1697         if (find->options & DNS_ADBFIND_INET) {
1698                 namehook = ISC_LIST_HEAD(name->v4);
1699                 while (namehook != NULL) {
1700                         entry = namehook->entry;
1701                         bucket = entry->lock_bucket;
1702                         LOCK(&adb->entrylocks[bucket]);
1703
1704                         if (!FIND_RETURNLAME(find)
1705                             && entry_is_bad_for_zone(adb, entry, zone, now)) {
1706                                 find->options |= DNS_ADBFIND_LAMEPRUNED;
1707                                 goto nextv4;
1708                         }
1709                         addrinfo = new_adbaddrinfo(adb, entry, find->port);
1710                         if (addrinfo == NULL) {
1711                                 find->partial_result |= DNS_ADBFIND_INET;
1712                                 goto out;
1713                         }
1714                         /*
1715                          * Found a valid entry.  Add it to the find's list.
1716                          */
1717                         inc_entry_refcnt(adb, entry, ISC_FALSE);
1718                         ISC_LIST_APPEND(find->list, addrinfo, publink);
1719                         addrinfo = NULL;
1720                 nextv4:
1721                         UNLOCK(&adb->entrylocks[bucket]);
1722                         bucket = DNS_ADB_INVALIDBUCKET;
1723                         namehook = ISC_LIST_NEXT(namehook, plink);
1724                 }
1725         }
1726
1727         if (find->options & DNS_ADBFIND_INET6) {
1728                 namehook = ISC_LIST_HEAD(name->v6);
1729                 while (namehook != NULL) {
1730                         entry = namehook->entry;
1731                         bucket = entry->lock_bucket;
1732                         LOCK(&adb->entrylocks[bucket]);
1733
1734                         if (entry_is_bad_for_zone(adb, entry, zone, now))
1735                                 goto nextv6;
1736                         addrinfo = new_adbaddrinfo(adb, entry, find->port);
1737                         if (addrinfo == NULL) {
1738                                 find->partial_result |= DNS_ADBFIND_INET6;
1739                                 goto out;
1740                         }
1741                         /*
1742                          * Found a valid entry.  Add it to the find's list.
1743                          */
1744                         inc_entry_refcnt(adb, entry, ISC_FALSE);
1745                         ISC_LIST_APPEND(find->list, addrinfo, publink);
1746                         addrinfo = NULL;
1747                 nextv6:
1748                         UNLOCK(&adb->entrylocks[bucket]);
1749                         bucket = DNS_ADB_INVALIDBUCKET;
1750                         namehook = ISC_LIST_NEXT(namehook, plink);
1751                 }
1752         }
1753
1754  out:
1755         if (bucket != DNS_ADB_INVALIDBUCKET)
1756                 UNLOCK(&adb->entrylocks[bucket]);
1757 }
1758
1759 static void
1760 shutdown_task(isc_task_t *task, isc_event_t *ev) {
1761         dns_adb_t *adb;
1762
1763         UNUSED(task);
1764
1765         adb = ev->ev_arg;
1766         INSIST(DNS_ADB_VALID(adb));
1767
1768         /*
1769          * Kill the timer, and then the ADB itself.  Note that this implies
1770          * that this task was the one scheduled to get timer events.  If
1771          * this is not true (and it is unfortunate there is no way to INSIST()
1772          * this) badness will occur.
1773          */
1774         LOCK(&adb->lock);
1775         isc_timer_detach(&adb->timer);
1776         UNLOCK(&adb->lock);
1777         isc_event_free(&ev);
1778         destroy(adb);
1779 }
1780
1781 /*
1782  * Name bucket must be locked; adb may be locked; no other locks held.
1783  */
1784 static isc_boolean_t
1785 check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
1786         dns_adbname_t *name;
1787         isc_boolean_t result = ISC_FALSE;
1788
1789         INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
1790         name = *namep;
1791
1792         if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
1793                 return (result);
1794         if (NAME_FETCH(name))
1795                 return (result);
1796         if (!EXPIRE_OK(name->expire_v4, now))
1797                 return (result);
1798         if (!EXPIRE_OK(name->expire_v6, now))
1799                 return (result);
1800         if (!EXPIRE_OK(name->expire_target, now))
1801                 return (result);
1802
1803         /*
1804          * The name is empty.  Delete it.
1805          */
1806         result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
1807         *namep = NULL;
1808
1809         /*
1810          * Our caller, or one of its callers, will be calling check_exit() at
1811          * some point, so we don't need to do it here.
1812          */
1813         return (result);
1814 }
1815
1816 /*
1817  * Entry bucket must be locked; adb may be locked; no other locks held.
1818  */
1819 static isc_boolean_t
1820 check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
1821 {
1822         dns_adbentry_t *entry;
1823         isc_boolean_t expire;
1824         isc_boolean_t result = ISC_FALSE;
1825
1826         INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
1827         entry = *entryp;
1828
1829         if (entry->refcnt != 0)
1830                 return (result);
1831
1832         if (adb->overmem) {
1833                 isc_uint32_t val;
1834
1835                 isc_random_get(&val);
1836
1837                 expire = ISC_TF((val % 4) == 0);
1838         } else
1839                 expire = ISC_FALSE;
1840
1841         if (entry->expires == 0 || (! expire && entry->expires > now))
1842                 return (result);
1843
1844         /*
1845          * The entry is not in use.  Delete it.
1846          */
1847         DP(DEF_LEVEL, "killing entry %p", entry);
1848         INSIST(ISC_LINK_LINKED(entry, plink));
1849         result = unlink_entry(adb, entry);
1850         free_adbentry(adb, &entry);
1851         if (result)
1852                 dec_adb_irefcnt(adb);
1853         *entryp = NULL;
1854         return (result);
1855 }
1856
1857 /*
1858  * ADB must be locked, and no other locks held.
1859  */
1860 static isc_boolean_t
1861 cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1862         dns_adbname_t *name;
1863         dns_adbname_t *next_name;
1864         isc_boolean_t result = ISC_FALSE;
1865
1866         DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
1867
1868         LOCK(&adb->namelocks[bucket]);
1869         if (adb->name_sd[bucket]) {
1870                 UNLOCK(&adb->namelocks[bucket]);
1871                 return (result);
1872         }
1873
1874         name = ISC_LIST_HEAD(adb->names[bucket]);
1875         while (name != NULL) {
1876                 next_name = ISC_LIST_NEXT(name, plink);
1877                 INSIST(result == ISC_FALSE);
1878                 result = check_expire_namehooks(name, now, adb->overmem);
1879                 if (!result)
1880                         result = check_expire_name(&name, now);
1881                 name = next_name;
1882         }
1883         UNLOCK(&adb->namelocks[bucket]);
1884         return (result);
1885 }
1886
1887 /*
1888  * ADB must be locked, and no other locks held.
1889  */
1890 static isc_boolean_t
1891 cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
1892         dns_adbentry_t *entry, *next_entry;
1893         isc_boolean_t result = ISC_FALSE;
1894
1895         DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
1896
1897         LOCK(&adb->entrylocks[bucket]);
1898         entry = ISC_LIST_HEAD(adb->entries[bucket]);
1899         while (entry != NULL) {
1900                 next_entry = ISC_LIST_NEXT(entry, plink);
1901                 INSIST(result == ISC_FALSE);
1902                 result = check_expire_entry(adb, &entry, now);
1903                 entry = next_entry;
1904         }
1905         UNLOCK(&adb->entrylocks[bucket]);
1906         return (result);
1907 }
1908
1909 static void
1910 timer_cleanup(isc_task_t *task, isc_event_t *ev) {
1911         dns_adb_t *adb;
1912         isc_stdtime_t now;
1913         unsigned int i;
1914         isc_interval_t interval;
1915
1916         UNUSED(task);
1917
1918         adb = ev->ev_arg;
1919         INSIST(DNS_ADB_VALID(adb));
1920
1921         LOCK(&adb->lock);
1922
1923         isc_stdtime_get(&now);
1924
1925         for (i = 0; i < CLEAN_BUCKETS; i++) {
1926                 /*
1927                  * Call our cleanup routines.
1928                  */
1929                 RUNTIME_CHECK(cleanup_names(adb, adb->next_cleanbucket, now) ==
1930                               ISC_FALSE);
1931                 RUNTIME_CHECK(cleanup_entries(adb, adb->next_cleanbucket, now)
1932                               == ISC_FALSE);
1933
1934                 /*
1935                  * Set the next bucket to be cleaned.
1936                  */
1937                 adb->next_cleanbucket++;
1938                 if (adb->next_cleanbucket >= NBUCKETS) {
1939                         adb->next_cleanbucket = 0;
1940 #ifdef DUMP_ADB_AFTER_CLEANING
1941                         dump_adb(adb, stdout, ISC_TRUE, now);
1942 #endif
1943                 }
1944         }
1945
1946         /*
1947          * Reset the timer.
1948          * XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
1949          * ISC_R_NOMEMORY, but it isn't clear what could be done here
1950          * if either one of those things happened.
1951          */
1952         interval = adb->tick_interval;
1953         if (adb->overmem)
1954                 isc_interval_set(&interval, 0, 1);
1955         (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
1956                               &interval, ISC_FALSE);
1957
1958         UNLOCK(&adb->lock);
1959
1960         isc_event_free(&ev);
1961 }
1962
1963 static void
1964 destroy(dns_adb_t *adb) {
1965         adb->magic = 0;
1966
1967         /*
1968          * The timer is already dead, from the task's shutdown callback.
1969          */
1970         isc_task_detach(&adb->task);
1971
1972         isc_mempool_destroy(&adb->nmp);
1973         isc_mempool_destroy(&adb->nhmp);
1974         isc_mempool_destroy(&adb->zimp);
1975         isc_mempool_destroy(&adb->emp);
1976         isc_mempool_destroy(&adb->ahmp);
1977         isc_mempool_destroy(&adb->aimp);
1978         isc_mempool_destroy(&adb->afmp);
1979
1980         DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
1981         DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
1982
1983         DESTROYLOCK(&adb->reflock);
1984         DESTROYLOCK(&adb->lock);
1985         DESTROYLOCK(&adb->mplock);
1986
1987         isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
1988 }
1989
1990
1991 /*
1992  * Public functions.
1993  */
1994
1995 isc_result_t
1996 dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
1997                isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
1998 {
1999         dns_adb_t *adb;
2000         isc_result_t result;
2001         int i;
2002
2003         REQUIRE(mem != NULL);
2004         REQUIRE(view != NULL);
2005         REQUIRE(timermgr != NULL);
2006         REQUIRE(taskmgr != NULL);
2007         REQUIRE(newadb != NULL && *newadb == NULL);
2008
2009         adb = isc_mem_get(mem, sizeof(dns_adb_t));
2010         if (adb == NULL)
2011                 return (ISC_R_NOMEMORY);
2012
2013         /*
2014          * Initialize things here that cannot fail, and especially things
2015          * that must be NULL for the error return to work properly.
2016          */
2017         adb->magic = 0;
2018         adb->erefcnt = 1;
2019         adb->irefcnt = 0;
2020         adb->nmp = NULL;
2021         adb->nhmp = NULL;
2022         adb->zimp = NULL;
2023         adb->emp = NULL;
2024         adb->ahmp = NULL;
2025         adb->aimp = NULL;
2026         adb->afmp = NULL;
2027         adb->task = NULL;
2028         adb->timer = NULL;
2029         adb->mctx = NULL;
2030         adb->view = view;
2031         adb->timermgr = timermgr;
2032         adb->taskmgr = taskmgr;
2033         adb->next_cleanbucket = 0;
2034         ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
2035                        DNS_EVENT_ADBCONTROL, shutdown_task, adb,
2036                        adb, NULL, NULL);
2037         adb->cevent_sent = ISC_FALSE;
2038         adb->shutting_down = ISC_FALSE;
2039         adb->overmem = ISC_FALSE;
2040         ISC_LIST_INIT(adb->whenshutdown);
2041
2042         isc_mem_attach(mem, &adb->mctx);
2043
2044         result = isc_mutex_init(&adb->lock);
2045         if (result != ISC_R_SUCCESS)
2046                 goto fail0b;
2047
2048         result = isc_mutex_init(&adb->mplock);
2049         if (result != ISC_R_SUCCESS)
2050                 goto fail0c;
2051
2052         result = isc_mutex_init(&adb->reflock);
2053         if (result != ISC_R_SUCCESS)
2054                 goto fail0d;
2055
2056         /*
2057          * Initialize the bucket locks for names and elements.
2058          * May as well initialize the list heads, too.
2059          */
2060         result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
2061         if (result != ISC_R_SUCCESS)
2062                 goto fail1;
2063         for (i = 0; i < NBUCKETS; i++) {
2064                 ISC_LIST_INIT(adb->names[i]);
2065                 adb->name_sd[i] = ISC_FALSE;
2066                 adb->name_refcnt[i] = 0;
2067                 adb->irefcnt++;
2068         }
2069         for (i = 0; i < NBUCKETS; i++) {
2070                 ISC_LIST_INIT(adb->entries[i]);
2071                 adb->entry_sd[i] = ISC_FALSE;
2072                 adb->entry_refcnt[i] = 0;
2073                 adb->irefcnt++;
2074         }
2075         result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
2076         if (result != ISC_R_SUCCESS)
2077                 goto fail2;
2078
2079         /*
2080          * Memory pools
2081          */
2082 #define MPINIT(t, p, n) do { \
2083         result = isc_mempool_create(mem, sizeof(t), &(p)); \
2084         if (result != ISC_R_SUCCESS) \
2085                 goto fail3; \
2086         isc_mempool_setfreemax((p), FREE_ITEMS); \
2087         isc_mempool_setfillcount((p), FILL_COUNT); \
2088         isc_mempool_setname((p), n); \
2089         isc_mempool_associatelock((p), &adb->mplock); \
2090 } while (0)
2091
2092         MPINIT(dns_adbname_t, adb->nmp, "adbname");
2093         MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
2094         MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
2095         MPINIT(dns_adbentry_t, adb->emp, "adbentry");
2096         MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
2097         MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
2098         MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
2099
2100 #undef MPINIT
2101
2102         /*
2103          * Allocate a timer and a task for our periodic cleanup.
2104          */
2105         result = isc_task_create(adb->taskmgr, 0, &adb->task);
2106         if (result != ISC_R_SUCCESS)
2107                 goto fail3;
2108         isc_task_setname(adb->task, "ADB", adb);
2109         /*
2110          * XXXMLG When this is changed to be a config file option,
2111          */
2112         isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
2113         result = isc_timer_create(adb->timermgr, isc_timertype_once,
2114                                   NULL, &adb->tick_interval, adb->task,
2115                                   timer_cleanup, adb, &adb->timer);
2116         if (result != ISC_R_SUCCESS)
2117                 goto fail3;
2118
2119         DP(ISC_LOG_DEBUG(5), "cleaning interval for adb: "
2120            "%u buckets every %u seconds, %u buckets in system, %u cl.interval",
2121            CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
2122
2123         /*
2124          * Normal return.
2125          */
2126         adb->magic = DNS_ADB_MAGIC;
2127         *newadb = adb;
2128         return (ISC_R_SUCCESS);
2129
2130  fail3:
2131         if (adb->task != NULL)
2132                 isc_task_detach(&adb->task);
2133         if (adb->timer != NULL)
2134                 isc_timer_detach(&adb->timer);
2135
2136         /* clean up entrylocks */
2137         DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
2138
2139  fail2: /* clean up namelocks */
2140         DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
2141
2142  fail1: /* clean up only allocated memory */
2143         if (adb->nmp != NULL)
2144                 isc_mempool_destroy(&adb->nmp);
2145         if (adb->nhmp != NULL)
2146                 isc_mempool_destroy(&adb->nhmp);
2147         if (adb->zimp != NULL)
2148                 isc_mempool_destroy(&adb->zimp);
2149         if (adb->emp != NULL)
2150                 isc_mempool_destroy(&adb->emp);
2151         if (adb->ahmp != NULL)
2152                 isc_mempool_destroy(&adb->ahmp);
2153         if (adb->aimp != NULL)
2154                 isc_mempool_destroy(&adb->aimp);
2155         if (adb->afmp != NULL)
2156                 isc_mempool_destroy(&adb->afmp);
2157
2158         DESTROYLOCK(&adb->reflock);
2159  fail0d:
2160         DESTROYLOCK(&adb->mplock);
2161  fail0c:
2162         DESTROYLOCK(&adb->lock);
2163  fail0b:
2164         isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
2165
2166         return (result);
2167 }
2168
2169 void
2170 dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
2171
2172         REQUIRE(DNS_ADB_VALID(adb));
2173         REQUIRE(adbx != NULL && *adbx == NULL);
2174
2175         inc_adb_erefcnt(adb);
2176         *adbx = adb;
2177 }
2178
2179 void
2180 dns_adb_detach(dns_adb_t **adbx) {
2181         dns_adb_t *adb;
2182         isc_boolean_t need_exit_check;
2183
2184         REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
2185
2186         adb = *adbx;
2187         *adbx = NULL;
2188
2189         INSIST(adb->erefcnt > 0);
2190
2191         LOCK(&adb->reflock);
2192         adb->erefcnt--;
2193         need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
2194         UNLOCK(&adb->reflock);
2195
2196         if (need_exit_check) {
2197                 LOCK(&adb->lock);
2198                 INSIST(adb->shutting_down);
2199                 check_exit(adb);
2200                 UNLOCK(&adb->lock);
2201         }
2202 }
2203
2204 void
2205 dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
2206         isc_task_t *clone;
2207         isc_event_t *event;
2208         isc_boolean_t zeroirefcnt = ISC_FALSE;
2209
2210         /*
2211          * Send '*eventp' to 'task' when 'adb' has shutdown.
2212          */
2213
2214         REQUIRE(DNS_ADB_VALID(adb));
2215         REQUIRE(eventp != NULL);
2216
2217         event = *eventp;
2218         *eventp = NULL;
2219
2220         LOCK(&adb->lock);
2221
2222         LOCK(&adb->reflock);
2223         zeroirefcnt = ISC_TF(adb->irefcnt == 0);
2224
2225         if (adb->shutting_down && zeroirefcnt &&
2226             isc_mempool_getallocated(adb->ahmp) == 0) {
2227                 /*
2228                  * We're already shutdown.  Send the event.
2229                  */
2230                 event->ev_sender = adb;
2231                 isc_task_send(task, &event);
2232         } else {
2233                 clone = NULL;
2234                 isc_task_attach(task, &clone);
2235                 event->ev_sender = clone;
2236                 ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
2237         }
2238
2239         UNLOCK(&adb->reflock);
2240         UNLOCK(&adb->lock);
2241 }
2242
2243 void
2244 dns_adb_shutdown(dns_adb_t *adb) {
2245         isc_boolean_t need_check_exit;
2246
2247         /*
2248          * Shutdown 'adb'.
2249          */
2250
2251         LOCK(&adb->lock);
2252
2253         if (!adb->shutting_down) {
2254                 adb->shutting_down = ISC_TRUE;
2255                 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
2256                 need_check_exit = shutdown_names(adb);
2257                 if (!need_check_exit)
2258                         need_check_exit = shutdown_entries(adb);
2259                 if (need_check_exit)
2260                         check_exit(adb);
2261         }
2262
2263         UNLOCK(&adb->lock);
2264 }
2265
2266 isc_result_t
2267 dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
2268                    void *arg, dns_name_t *name, dns_name_t *zone,
2269                    unsigned int options, isc_stdtime_t now, dns_name_t *target,
2270                    in_port_t port, dns_adbfind_t **findp)
2271 {
2272         dns_adbfind_t *find;
2273         dns_adbname_t *adbname;
2274         int bucket;
2275         isc_boolean_t want_event, start_at_zone, alias, have_address;
2276         isc_result_t result;
2277         unsigned int wanted_addresses;
2278         unsigned int wanted_fetches;
2279         unsigned int query_pending;
2280
2281         REQUIRE(DNS_ADB_VALID(adb));
2282         if (task != NULL) {
2283                 REQUIRE(action != NULL);
2284         }
2285         REQUIRE(name != NULL);
2286         REQUIRE(zone != NULL);
2287         REQUIRE(findp != NULL && *findp == NULL);
2288         REQUIRE(target == NULL || dns_name_hasbuffer(target));
2289
2290         REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
2291
2292         result = ISC_R_UNEXPECTED;
2293         wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
2294         wanted_fetches = 0;
2295         query_pending = 0;
2296         want_event = ISC_FALSE;
2297         start_at_zone = ISC_FALSE;
2298         alias = ISC_FALSE;
2299
2300         if (now == 0)
2301                 isc_stdtime_get(&now);
2302
2303         /*
2304          * XXXMLG  Move this comment somewhere else!
2305          *
2306          * Look up the name in our internal database.
2307          *
2308          * Possibilities:  Note that these are not always exclusive.
2309          *
2310          *      No name found.  In this case, allocate a new name header and
2311          *      an initial namehook or two.  If any of these allocations
2312          *      fail, clean up and return ISC_R_NOMEMORY.
2313          *
2314          *      Name found, valid addresses present.  Allocate one addrinfo
2315          *      structure for each found and append it to the linked list
2316          *      of addresses for this header.
2317          *
2318          *      Name found, queries pending.  In this case, if a task was
2319          *      passed in, allocate a job id, attach it to the name's job
2320          *      list and remember to tell the caller that there will be
2321          *      more info coming later.
2322          */
2323
2324         find = new_adbfind(adb);
2325         if (find == NULL)
2326                 return (ISC_R_NOMEMORY);
2327
2328         find->port = port;
2329
2330         /*
2331          * Remember what types of addresses we are interested in.
2332          */
2333         find->options = options;
2334         find->flags |= wanted_addresses;
2335         if (FIND_WANTEVENT(find)) {
2336                 REQUIRE(task != NULL);
2337         }
2338
2339         /*
2340          * Try to see if we know anything about this name at all.
2341          */
2342         bucket = DNS_ADB_INVALIDBUCKET;
2343         adbname = find_name_and_lock(adb, name, find->options, &bucket);
2344         if (adb->name_sd[bucket]) {
2345                 DP(DEF_LEVEL,
2346                    "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
2347                 RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2348                 result = ISC_R_SHUTTINGDOWN;
2349                 goto out;
2350         }
2351
2352         /*
2353          * Nothing found.  Allocate a new adbname structure for this name.
2354          */
2355         if (adbname == NULL) {
2356                 adbname = new_adbname(adb, name);
2357                 if (adbname == NULL) {
2358                         RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
2359                         result = ISC_R_NOMEMORY;
2360                         goto out;
2361                 }
2362                 link_name(adb, bucket, adbname);
2363                 if (FIND_HINTOK(find))
2364                         adbname->flags |= NAME_HINT_OK;
2365                 if (FIND_GLUEOK(find))
2366                         adbname->flags |= NAME_GLUE_OK;
2367                 if (FIND_STARTATZONE(find))
2368                         adbname->flags |= NAME_STARTATZONE;
2369         }
2370
2371         /*
2372          * Expire old entries, etc.
2373          */
2374         RUNTIME_CHECK(check_expire_namehooks(adbname, now, adb->overmem) ==
2375                       ISC_FALSE);
2376
2377         /*
2378          * Do we know that the name is an alias?
2379          */
2380         if (!EXPIRE_OK(adbname->expire_target, now)) {
2381                 /*
2382                  * Yes, it is.
2383                  */
2384                 DP(DEF_LEVEL,
2385                    "dns_adb_createfind: name %p is an alias (cached)",
2386                    adbname);
2387                 alias = ISC_TRUE;
2388                 goto post_copy;
2389         }
2390
2391         /*
2392          * Try to populate the name from the database and/or
2393          * start fetches.  First try looking for an A record
2394          * in the database.
2395          */
2396         if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
2397             && WANT_INET(wanted_addresses)) {
2398                 result = dbfind_name(adbname, now, dns_rdatatype_a);
2399                 if (result == ISC_R_SUCCESS) {
2400                         DP(DEF_LEVEL,
2401                            "dns_adb_createfind: found A for name %p in db",
2402                            adbname);
2403                         goto v6;
2404                 }
2405
2406                 /*
2407                  * Did we get a CNAME or DNAME?
2408                  */
2409                 if (result == DNS_R_ALIAS) {
2410                         DP(DEF_LEVEL,
2411                            "dns_adb_createfind: name %p is an alias",
2412                            adbname);
2413                         alias = ISC_TRUE;
2414                         goto post_copy;
2415                 }
2416
2417                 /*
2418                  * If the name doesn't exist at all, don't bother with
2419                  * v6 queries; they won't work.
2420                  *
2421                  * If the name does exist but we didn't get our data, go
2422                  * ahead and try AAAA.
2423                  *
2424                  * If the result is neither of these, try a fetch for A.
2425                  */
2426                 if (NXDOMAIN_RESULT(result))
2427                         goto fetch;
2428                 else if (NXRRSET_RESULT(result))
2429                         goto v6;
2430
2431                 if (!NAME_FETCH_V4(adbname))
2432                         wanted_fetches |= DNS_ADBFIND_INET;
2433         }
2434
2435  v6:
2436         if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
2437             && WANT_INET6(wanted_addresses)) {
2438                 result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
2439                 if (result == ISC_R_SUCCESS) {
2440                         DP(DEF_LEVEL,
2441                            "dns_adb_createfind: found AAAA for name %p",
2442                            adbname);
2443                         goto fetch;
2444                 }
2445
2446                 /*
2447                  * Did we get a CNAME or DNAME?
2448                  */
2449                 if (result == DNS_R_ALIAS) {
2450                         DP(DEF_LEVEL,
2451                            "dns_adb_createfind: name %p is an alias",
2452                            adbname);
2453                         alias = ISC_TRUE;
2454                         goto post_copy;
2455                 }
2456
2457                 /*
2458                  * Listen to negative cache hints, and don't start
2459                  * another query.
2460                  */
2461                 if (NCACHE_RESULT(result) || AUTH_NX(result))
2462                         goto fetch;
2463
2464                 if (!NAME_FETCH_V6(adbname))
2465                         wanted_fetches |= DNS_ADBFIND_INET6;
2466         }
2467
2468  fetch:
2469         if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
2470             (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
2471                 have_address = ISC_TRUE;
2472         else
2473                 have_address = ISC_FALSE;
2474         if (wanted_fetches != 0 &&
2475             ! (FIND_AVOIDFETCHES(find) && have_address)) {
2476                 /*
2477                  * We're missing at least one address family.  Either the
2478                  * caller hasn't instructed us to avoid fetches, or we don't
2479                  * know anything about any of the address families that would
2480                  * be acceptable so we have to launch fetches.
2481                  */
2482
2483                 if (FIND_STARTATZONE(find))
2484                         start_at_zone = ISC_TRUE;
2485
2486                 /*
2487                  * Start V4.
2488                  */
2489                 if (WANT_INET(wanted_fetches) &&
2490                     fetch_name(adbname, start_at_zone,
2491                                dns_rdatatype_a) == ISC_R_SUCCESS) {
2492                         DP(DEF_LEVEL,
2493                            "dns_adb_createfind: started A fetch for name %p",
2494                            adbname);
2495                 }
2496
2497                 /*
2498                  * Start V6.
2499                  */
2500                 if (WANT_INET6(wanted_fetches) &&
2501                     fetch_name(adbname, start_at_zone,
2502                                dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
2503                         DP(DEF_LEVEL,
2504                            "dns_adb_createfind: "
2505                            "started AAAA fetch for name %p",
2506                            adbname);
2507                 }
2508         }
2509
2510         /*
2511          * Run through the name and copy out the bits we are
2512          * interested in.
2513          */
2514         copy_namehook_lists(adb, find, zone, adbname, now);
2515
2516  post_copy:
2517         if (NAME_FETCH_V4(adbname))
2518                 query_pending |= DNS_ADBFIND_INET;
2519         if (NAME_FETCH_V6(adbname))
2520                 query_pending |= DNS_ADBFIND_INET6;
2521
2522         /*
2523          * Attach to the name's query list if there are queries
2524          * already running, and we have been asked to.
2525          */
2526         want_event = ISC_TRUE;
2527         if (!FIND_WANTEVENT(find))
2528                 want_event = ISC_FALSE;
2529         if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
2530                 want_event = ISC_FALSE;
2531         if ((wanted_addresses & query_pending) == 0)
2532                 want_event = ISC_FALSE;
2533         if (alias)
2534                 want_event = ISC_FALSE;
2535         if (want_event) {
2536                 find->adbname = adbname;
2537                 find->name_bucket = bucket;
2538                 ISC_LIST_APPEND(adbname->finds, find, plink);
2539                 find->query_pending = (query_pending & wanted_addresses);
2540                 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2541                 find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
2542                 DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
2543                    find, adbname);
2544         } else {
2545                 /*
2546                  * Remove the flag so the caller knows there will never
2547                  * be an event, and set internal flags to fake that
2548                  * the event was sent and freed, so dns_adb_destroyfind() will
2549                  * do the right thing.
2550                  */
2551                 find->query_pending = (query_pending & wanted_addresses);
2552                 find->options &= ~DNS_ADBFIND_WANTEVENT;
2553                 find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
2554                 find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
2555         }
2556
2557         find->partial_result |= (adbname->partial_result & wanted_addresses);
2558         if (alias) {
2559                 if (target != NULL) {
2560                         result = dns_name_copy(&adbname->target, target, NULL);
2561                         if (result != ISC_R_SUCCESS)
2562                                 goto out;
2563                 }
2564                 result = DNS_R_ALIAS;
2565         } else
2566                 result = ISC_R_SUCCESS;
2567
2568         /*
2569          * Copy out error flags from the name structure into the find.
2570          */
2571         find->result_v4 = find_err_map[adbname->fetch_err];
2572         find->result_v6 = find_err_map[adbname->fetch6_err];
2573
2574  out:
2575         if (find != NULL) {
2576                 *findp = find;
2577
2578                 if (want_event) {
2579                         isc_task_t *taskp;
2580
2581                         INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
2582                         taskp = NULL;
2583                         isc_task_attach(task, &taskp);
2584                         find->event.ev_sender = taskp;
2585                         find->event.ev_action = action;
2586                         find->event.ev_arg = arg;
2587                 }
2588         }
2589
2590         UNLOCK(&adb->namelocks[bucket]);
2591
2592         return (result);
2593 }
2594
2595 void
2596 dns_adb_destroyfind(dns_adbfind_t **findp) {
2597         dns_adbfind_t *find;
2598         dns_adbentry_t *entry;
2599         dns_adbaddrinfo_t *ai;
2600         int bucket;
2601         dns_adb_t *adb;
2602
2603         REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
2604         find = *findp;
2605         *findp = NULL;
2606
2607         LOCK(&find->lock);
2608
2609         DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
2610
2611         adb = find->adb;
2612         REQUIRE(DNS_ADB_VALID(adb));
2613
2614         REQUIRE(FIND_EVENTFREED(find));
2615
2616         bucket = find->name_bucket;
2617         INSIST(bucket == DNS_ADB_INVALIDBUCKET);
2618
2619         UNLOCK(&find->lock);
2620
2621         /*
2622          * The find doesn't exist on any list, and nothing is locked.
2623          * Return the find to the memory pool, and decrement the adb's
2624          * reference count.
2625          */
2626         ai = ISC_LIST_HEAD(find->list);
2627         while (ai != NULL) {
2628                 ISC_LIST_UNLINK(find->list, ai, publink);
2629                 entry = ai->entry;
2630                 ai->entry = NULL;
2631                 INSIST(DNS_ADBENTRY_VALID(entry));
2632                 RUNTIME_CHECK(dec_entry_refcnt(adb, entry, ISC_TRUE) ==
2633                               ISC_FALSE);
2634                 free_adbaddrinfo(adb, &ai);
2635                 ai = ISC_LIST_HEAD(find->list);
2636         }
2637
2638         /*
2639          * WARNING:  The find is freed with the adb locked.  This is done
2640          * to avoid a race condition where we free the find, some other
2641          * thread tests to see if it should be destroyed, detects it should
2642          * be, destroys it, and then we try to lock it for our check, but the
2643          * lock is destroyed.
2644          */
2645         LOCK(&adb->lock);
2646         if (free_adbfind(adb, &find))
2647                 check_exit(adb);
2648         UNLOCK(&adb->lock);
2649 }
2650
2651 void
2652 dns_adb_cancelfind(dns_adbfind_t *find) {
2653         isc_event_t *ev;
2654         isc_task_t *task;
2655         dns_adb_t *adb;
2656         int bucket;
2657         int unlock_bucket;
2658
2659         LOCK(&find->lock);
2660
2661         DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
2662
2663         adb = find->adb;
2664         REQUIRE(DNS_ADB_VALID(adb));
2665
2666         REQUIRE(!FIND_EVENTFREED(find));
2667         REQUIRE(FIND_WANTEVENT(find));
2668
2669         bucket = find->name_bucket;
2670         if (bucket == DNS_ADB_INVALIDBUCKET)
2671                 goto cleanup;
2672
2673         /*
2674          * We need to get the adbname's lock to unlink the find.
2675          */
2676         unlock_bucket = bucket;
2677         violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
2678         bucket = find->name_bucket;
2679         if (bucket != DNS_ADB_INVALIDBUCKET) {
2680                 ISC_LIST_UNLINK(find->adbname->finds, find, plink);
2681                 find->adbname = NULL;
2682                 find->name_bucket = DNS_ADB_INVALIDBUCKET;
2683         }
2684         UNLOCK(&adb->namelocks[unlock_bucket]);
2685         bucket = DNS_ADB_INVALIDBUCKET;
2686
2687  cleanup:
2688
2689         if (!FIND_EVENTSENT(find)) {
2690                 ev = &find->event;
2691                 task = ev->ev_sender;
2692                 ev->ev_sender = find;
2693                 ev->ev_type = DNS_EVENT_ADBCANCELED;
2694                 ev->ev_destroy = event_free;
2695                 ev->ev_destroy_arg = find;
2696                 find->result_v4 = ISC_R_CANCELED;
2697                 find->result_v6 = ISC_R_CANCELED;
2698
2699                 DP(DEF_LEVEL, "sending event %p to task %p for find %p",
2700                    ev, task, find);
2701
2702                 isc_task_sendanddetach(&task, (isc_event_t **)&ev);
2703         }
2704
2705         UNLOCK(&find->lock);
2706 }
2707
2708 void
2709 dns_adb_dump(dns_adb_t *adb, FILE *f) {
2710         int i;
2711         isc_stdtime_t now;
2712
2713         REQUIRE(DNS_ADB_VALID(adb));
2714         REQUIRE(f != NULL);
2715
2716         /*
2717          * Lock the adb itself, lock all the name buckets, then lock all
2718          * the entry buckets.  This should put the adb into a state where
2719          * nothing can change, so we can iterate through everything and
2720          * print at our leisure.
2721          */
2722
2723         LOCK(&adb->lock);
2724         isc_stdtime_get(&now);
2725
2726         for (i = 0; i < NBUCKETS; i++)
2727                 RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
2728         for (i = 0; i < NBUCKETS; i++)
2729                 RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
2730
2731         dump_adb(adb, f, ISC_FALSE, now);
2732         UNLOCK(&adb->lock);
2733 }
2734
2735 static void
2736 dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
2737         if (value == INT_MAX)
2738                 return;
2739         fprintf(f, " [%s TTL %d]", legend, value - now);
2740 }
2741
2742 static void
2743 dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug, isc_stdtime_t now) {
2744         int i;
2745         dns_adbname_t *name;
2746         dns_adbentry_t *entry;
2747
2748         fprintf(f, ";\n; Address database dump\n;\n");
2749         if (debug)
2750                 fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
2751                         adb, adb->erefcnt, adb->irefcnt,
2752                         isc_mempool_getallocated(adb->nhmp));
2753
2754         for (i = 0; i < NBUCKETS; i++)
2755                 LOCK(&adb->namelocks[i]);
2756         for (i = 0; i < NBUCKETS; i++)
2757                 LOCK(&adb->entrylocks[i]);
2758
2759         /*
2760          * Dump the names
2761          */
2762         for (i = 0; i < NBUCKETS; i++) {
2763                 name = ISC_LIST_HEAD(adb->names[i]);
2764                 if (name == NULL)
2765                         continue;
2766                 if (debug)
2767                         fprintf(f, "; bucket %d\n", i);
2768                 for (;
2769                      name != NULL;
2770                      name = ISC_LIST_NEXT(name, plink))
2771                 {
2772                         if (debug)
2773                                 fprintf(f, "; name %p (flags %08x)\n",
2774                                         name, name->flags);
2775
2776                         fprintf(f, "; ");
2777                         print_dns_name(f, &name->name);
2778                         if (dns_name_countlabels(&name->target) > 0) {
2779                                 fprintf(f, " alias ");
2780                                 print_dns_name(f, &name->target);
2781                         }
2782
2783                         dump_ttl(f, "v4", name->expire_v4, now);
2784                         dump_ttl(f, "v6", name->expire_v6, now);
2785                         dump_ttl(f, "target", name->expire_target, now);
2786
2787                         fprintf(f, " [v4 %s] [v6 %s]",
2788                                 errnames[name->fetch_err],
2789                                 errnames[name->fetch6_err]);
2790
2791                         fprintf(f, "\n");
2792
2793                         print_namehook_list(f, "v4", &name->v4, debug, now);
2794                         print_namehook_list(f, "v6", &name->v6, debug, now);
2795
2796                         if (debug)
2797                                 print_fetch_list(f, name);
2798                         if (debug)
2799                                 print_find_list(f, name);
2800
2801                 }
2802         }
2803
2804         fprintf(f, ";\n; Unassociated entries\n;\n");
2805
2806         for (i = 0; i < NBUCKETS; i++) {
2807                 entry = ISC_LIST_HEAD(adb->entries[i]);
2808                 while (entry != NULL) {
2809                         if (entry->refcnt == 0)
2810                                 dump_entry(f, entry, debug, now);
2811                         entry = ISC_LIST_NEXT(entry, plink);
2812                 }
2813         }
2814
2815         /*
2816          * Unlock everything
2817          */
2818         for (i = 0; i < NBUCKETS; i++)
2819                 UNLOCK(&adb->entrylocks[i]);
2820         for (i = 0; i < NBUCKETS; i++)
2821                 UNLOCK(&adb->namelocks[i]);
2822 }
2823
2824 static void
2825 dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
2826            isc_stdtime_t now)
2827 {
2828         char addrbuf[ISC_NETADDR_FORMATSIZE];
2829         isc_netaddr_t netaddr;
2830         dns_adbzoneinfo_t *zi;
2831
2832         isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
2833         isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
2834
2835         if (debug)
2836                 fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
2837
2838         fprintf(f, ";\t%s [srtt %u] [flags %08x]",
2839                 addrbuf, entry->srtt, entry->flags);
2840         if (entry->expires != 0)
2841                 fprintf(f, " [ttl %d]", entry->expires - now);
2842         fprintf(f, "\n");
2843         for (zi = ISC_LIST_HEAD(entry->zoneinfo);
2844              zi != NULL;
2845              zi = ISC_LIST_NEXT(zi, plink)) {
2846                 fprintf(f, ";\t\t");
2847                 print_dns_name(f, &zi->zone);
2848                 fprintf(f, " [lame TTL %d]\n", zi->lame_timer - now);
2849         }
2850 }
2851
2852 void
2853 dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
2854         char tmp[512];
2855         const char *tmpp;
2856         dns_adbaddrinfo_t *ai;
2857         isc_sockaddr_t *sa;
2858
2859         /*
2860          * Not used currently, in the API Just In Case we
2861          * want to dump out the name and/or entries too.
2862          */
2863
2864         LOCK(&find->lock);
2865
2866         fprintf(f, ";Find %p\n", find);
2867         fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
2868                 find->query_pending, find->partial_result,
2869                 find->options, find->flags);
2870         fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
2871                 find->name_bucket, find->adbname, find->event.ev_sender);
2872
2873         ai = ISC_LIST_HEAD(find->list);
2874         if (ai != NULL)
2875                 fprintf(f, "\tAddresses:\n");
2876         while (ai != NULL) {
2877                 sa = &ai->sockaddr;
2878                 switch (sa->type.sa.sa_family) {
2879                 case AF_INET:
2880                         tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
2881                                          tmp, sizeof(tmp));
2882                         break;
2883                 case AF_INET6:
2884                         tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
2885                                          tmp, sizeof(tmp));
2886                         break;
2887                 default:
2888                         tmpp = "UnkFamily";
2889                 }
2890
2891                 if (tmpp == NULL)
2892                         tmpp = "BadAddress";
2893
2894                 fprintf(f, "\t\tentry %p, flags %08x"
2895                         " srtt %u addr %s\n",
2896                         ai->entry, ai->flags, ai->srtt, tmpp);
2897
2898                 ai = ISC_LIST_NEXT(ai, publink);
2899         }
2900
2901         UNLOCK(&find->lock);
2902 }
2903
2904 static void
2905 print_dns_name(FILE *f, dns_name_t *name) {
2906         char buf[DNS_NAME_FORMATSIZE];
2907
2908         INSIST(f != NULL);
2909
2910         dns_name_format(name, buf, sizeof(buf));
2911         fprintf(f, "%s", buf);
2912 }
2913
2914 static void
2915 print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
2916                     isc_boolean_t debug, isc_stdtime_t now)
2917 {
2918         dns_adbnamehook_t *nh;
2919
2920         for (nh = ISC_LIST_HEAD(*list);
2921              nh != NULL;
2922              nh = ISC_LIST_NEXT(nh, plink))
2923         {
2924                 if (debug)
2925                         fprintf(f, ";\tHook(%s) %p\n", legend, nh);
2926                 dump_entry(f, nh->entry, debug, now);
2927         }
2928 }
2929
2930 static inline void
2931 print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
2932         fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
2933                 type, ft, ft->namehook, ft->entry, ft->fetch);
2934 }
2935
2936 static void
2937 print_fetch_list(FILE *f, dns_adbname_t *n) {
2938         if (NAME_FETCH_A(n))
2939                 print_fetch(f, n->fetch_a, "A");
2940         if (NAME_FETCH_AAAA(n))
2941                 print_fetch(f, n->fetch_aaaa, "AAAA");
2942 }
2943
2944 static void
2945 print_find_list(FILE *f, dns_adbname_t *name) {
2946         dns_adbfind_t *find;
2947
2948         find = ISC_LIST_HEAD(name->finds);
2949         while (find != NULL) {
2950                 dns_adb_dumpfind(find, f);
2951                 find = ISC_LIST_NEXT(find, plink);
2952         }
2953 }
2954
2955 static isc_result_t
2956 dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
2957 {
2958         isc_result_t result;
2959         dns_rdataset_t rdataset;
2960         dns_adb_t *adb;
2961         dns_fixedname_t foundname;
2962         dns_name_t *fname;
2963
2964         INSIST(DNS_ADBNAME_VALID(adbname));
2965         adb = adbname->adb;
2966         INSIST(DNS_ADB_VALID(adb));
2967         INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
2968
2969         dns_fixedname_init(&foundname);
2970         fname = dns_fixedname_name(&foundname);
2971         dns_rdataset_init(&rdataset);
2972
2973         if (rdtype == dns_rdatatype_a)
2974                 adbname->fetch_err = FIND_ERR_UNEXPECTED;
2975         else
2976                 adbname->fetch6_err = FIND_ERR_UNEXPECTED;
2977
2978         result = dns_view_find(adb->view, &adbname->name, rdtype, now,
2979                                NAME_GLUEOK(adbname),
2980                                ISC_TF(NAME_HINTOK(adbname)),
2981                                NULL, NULL, fname, &rdataset, NULL);
2982
2983         /* XXXVIX this switch statement is too sparse to gen a jump table. */
2984         switch (result) {
2985         case DNS_R_GLUE:
2986         case DNS_R_HINT:
2987         case ISC_R_SUCCESS:
2988                 /*
2989                  * Found in the database.  Even if we can't copy out
2990                  * any information, return success, or else a fetch
2991                  * will be made, which will only make things worse.
2992                  */
2993                 if (rdtype == dns_rdatatype_a)
2994                         adbname->fetch_err = FIND_ERR_SUCCESS;
2995                 else
2996                         adbname->fetch6_err = FIND_ERR_SUCCESS;
2997                 result = import_rdataset(adbname, &rdataset, now);
2998                 break;
2999         case DNS_R_NXDOMAIN:
3000         case DNS_R_NXRRSET:
3001                 /*
3002                  * We're authoritative and the data doesn't exist.
3003                  * Make up a negative cache entry so we don't ask again
3004                  * for a while.
3005                  *
3006                  * XXXRTH  What time should we use?  I'm putting in 30 seconds
3007                  * for now.
3008                  */
3009                 if (rdtype == dns_rdatatype_a) {
3010                         adbname->expire_v4 = now + 30;
3011                         DP(NCACHE_LEVEL,
3012                            "adb name %p: Caching auth negative entry for A",
3013                            adbname);
3014                         if (result == DNS_R_NXDOMAIN)
3015                                 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3016                         else
3017                                 adbname->fetch_err = FIND_ERR_NXRRSET;
3018                 } else {
3019                         DP(NCACHE_LEVEL,
3020                            "adb name %p: Caching auth negative entry for AAAA",
3021                            adbname);
3022                         adbname->expire_v6 = now + 30;
3023                         if (result == DNS_R_NXDOMAIN)
3024                                 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3025                         else
3026                                 adbname->fetch6_err = FIND_ERR_NXRRSET;
3027                 }
3028                 break;
3029         case DNS_R_NCACHENXDOMAIN:
3030         case DNS_R_NCACHENXRRSET:
3031                 /*
3032                  * We found a negative cache entry.  Pull the TTL from it
3033                  * so we won't ask again for a while.
3034                  */
3035                 rdataset.ttl = ttlclamp(rdataset.ttl);
3036                 if (rdtype == dns_rdatatype_a) {
3037                         adbname->expire_v4 = rdataset.ttl + now;
3038                         if (result == DNS_R_NCACHENXDOMAIN)
3039                                 adbname->fetch_err = FIND_ERR_NXDOMAIN;
3040                         else
3041                                 adbname->fetch_err = FIND_ERR_NXRRSET;
3042                         DP(NCACHE_LEVEL,
3043                           "adb name %p: Caching negative entry for A (ttl %u)",
3044                            adbname, rdataset.ttl);
3045                 } else {
3046                         DP(NCACHE_LEVEL,
3047                        "adb name %p: Caching negative entry for AAAA (ttl %u)",
3048                            adbname, rdataset.ttl);
3049                         adbname->expire_v6 = rdataset.ttl + now;
3050                         if (result == DNS_R_NCACHENXDOMAIN)
3051                                 adbname->fetch6_err = FIND_ERR_NXDOMAIN;
3052                         else
3053                                 adbname->fetch6_err = FIND_ERR_NXRRSET;
3054                 }
3055                 break;
3056         case DNS_R_CNAME:
3057         case DNS_R_DNAME:
3058                 /*
3059                  * Clear the hint and glue flags, so this will match
3060                  * more often.
3061                  */
3062                 adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
3063
3064                 rdataset.ttl = ttlclamp(rdataset.ttl);
3065                 clean_target(adb, &adbname->target);
3066                 adbname->expire_target = INT_MAX;
3067                 result = set_target(adb, &adbname->name, fname, &rdataset,
3068                                     &adbname->target);
3069                 if (result == ISC_R_SUCCESS) {
3070                         result = DNS_R_ALIAS;
3071                         DP(NCACHE_LEVEL,
3072                            "adb name %p: caching alias target",
3073                            adbname);
3074                         adbname->expire_target = rdataset.ttl + now;
3075                 }
3076                 if (rdtype == dns_rdatatype_a)
3077                         adbname->fetch_err = FIND_ERR_SUCCESS;
3078                 else
3079                         adbname->fetch6_err = FIND_ERR_SUCCESS;
3080                 break;
3081         }
3082
3083         if (dns_rdataset_isassociated(&rdataset))
3084                 dns_rdataset_disassociate(&rdataset);
3085
3086         return (result);
3087 }
3088
3089 static void
3090 fetch_callback(isc_task_t *task, isc_event_t *ev) {
3091         dns_fetchevent_t *dev;
3092         dns_adbname_t *name;
3093         dns_adb_t *adb;
3094         dns_adbfetch_t *fetch;
3095         int bucket;
3096         isc_eventtype_t ev_status;
3097         isc_stdtime_t now;
3098         isc_result_t result;
3099         unsigned int address_type;
3100         isc_boolean_t want_check_exit = ISC_FALSE;
3101
3102         UNUSED(task);
3103
3104         INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
3105         dev = (dns_fetchevent_t *)ev;
3106         name = ev->ev_arg;
3107         INSIST(DNS_ADBNAME_VALID(name));
3108         adb = name->adb;
3109         INSIST(DNS_ADB_VALID(adb));
3110
3111         bucket = name->lock_bucket;
3112         LOCK(&adb->namelocks[bucket]);
3113
3114         INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
3115         address_type = 0;
3116         if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
3117                 address_type = DNS_ADBFIND_INET;
3118                 fetch = name->fetch_a;
3119                 name->fetch_a = NULL;
3120         } else if (NAME_FETCH_AAAA(name)
3121                    && (name->fetch_aaaa->fetch == dev->fetch)) {
3122                 address_type = DNS_ADBFIND_INET6;
3123                 fetch = name->fetch_aaaa;
3124                 name->fetch_aaaa = NULL;
3125         }
3126         INSIST(address_type != 0);
3127
3128         dns_resolver_destroyfetch(&fetch->fetch);
3129         dev->fetch = NULL;
3130
3131         ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
3132
3133         /*
3134          * Cleanup things we don't care about.
3135          */
3136         if (dev->node != NULL)
3137                 dns_db_detachnode(dev->db, &dev->node);
3138         if (dev->db != NULL)
3139                 dns_db_detach(&dev->db);
3140
3141         /*
3142          * If this name is marked as dead, clean up, throwing away
3143          * potentially good data.
3144          */
3145         if (NAME_DEAD(name)) {
3146                 free_adbfetch(adb, &fetch);
3147                 isc_event_free(&ev);
3148
3149                 want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
3150
3151                 UNLOCK(&adb->namelocks[bucket]);
3152
3153                 if (want_check_exit) {
3154                         LOCK(&adb->lock);
3155                         check_exit(adb);
3156                         UNLOCK(&adb->lock);
3157                 }
3158
3159                 return;
3160         }
3161
3162         isc_stdtime_get(&now);
3163
3164         /*
3165          * If we got a negative cache response, remember it.
3166          */
3167         if (NCACHE_RESULT(dev->result)) {
3168                 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3169                 if (address_type == DNS_ADBFIND_INET) {
3170                         DP(NCACHE_LEVEL, "adb fetch name %p: "
3171                            "caching negative entry for A (ttl %u)",
3172                            name, dev->rdataset->ttl);
3173                         name->expire_v4 = ISC_MIN(name->expire_v4,
3174                                                   dev->rdataset->ttl + now);
3175                         if (dev->result == DNS_R_NCACHENXDOMAIN)
3176                                 name->fetch_err = FIND_ERR_NXDOMAIN;
3177                         else
3178                                 name->fetch_err = FIND_ERR_NXRRSET;
3179                 } else {
3180                         DP(NCACHE_LEVEL, "adb fetch name %p: "
3181                            "caching negative entry for AAAA (ttl %u)",
3182                            name, dev->rdataset->ttl);
3183                         name->expire_v6 = ISC_MIN(name->expire_v6,
3184                                                   dev->rdataset->ttl + now);
3185                         if (dev->result == DNS_R_NCACHENXDOMAIN)
3186                                 name->fetch6_err = FIND_ERR_NXDOMAIN;
3187                         else
3188                                 name->fetch6_err = FIND_ERR_NXRRSET;
3189                 }
3190                 goto out;
3191         }
3192
3193         /*
3194          * Handle CNAME/DNAME.
3195          */
3196         if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
3197                 dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
3198                 clean_target(adb, &name->target);
3199                 name->expire_target = INT_MAX;
3200                 result = set_target(adb, &name->name,
3201                                     dns_fixedname_name(&dev->foundname),
3202                                     dev->rdataset,
3203                                     &name->target);
3204                 if (result == ISC_R_SUCCESS) {
3205                         DP(NCACHE_LEVEL,
3206                            "adb fetch name %p: caching alias target",
3207                            name);
3208                         name->expire_target = dev->rdataset->ttl + now;
3209                 }
3210                 goto check_result;
3211         }
3212
3213         /*
3214          * Did we get back junk?  If so, and there are no more fetches
3215          * sitting out there, tell all the finds about it.
3216          */
3217         if (dev->result != ISC_R_SUCCESS) {
3218                 char buf[DNS_NAME_FORMATSIZE];
3219
3220                 dns_name_format(&name->name, buf, sizeof(buf));
3221                 DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
3222                    buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
3223                    dns_result_totext(dev->result));
3224                 /* XXXMLG Don't pound on bad servers. */
3225                 if (address_type == DNS_ADBFIND_INET) {
3226                         name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
3227                         name->fetch_err = FIND_ERR_FAILURE;
3228                 } else {
3229                         name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
3230                         name->fetch6_err = FIND_ERR_FAILURE;
3231                 }
3232                 goto out;
3233         }
3234
3235         /*
3236          * We got something potentially useful.
3237          */
3238         result = import_rdataset(name, &fetch->rdataset, now);
3239
3240  check_result:
3241         if (result == ISC_R_SUCCESS) {
3242                 ev_status = DNS_EVENT_ADBMOREADDRESSES;
3243                 if (address_type == DNS_ADBFIND_INET)
3244                         name->fetch_err = FIND_ERR_SUCCESS;
3245                 else
3246                         name->fetch6_err = FIND_ERR_SUCCESS;
3247         }
3248
3249  out:
3250         free_adbfetch(adb, &fetch);
3251         isc_event_free(&ev);
3252
3253         clean_finds_at_name(name, ev_status, address_type);
3254
3255         UNLOCK(&adb->namelocks[bucket]);
3256 }
3257
3258 static isc_result_t
3259 fetch_name(dns_adbname_t *adbname,
3260            isc_boolean_t start_at_zone,
3261            dns_rdatatype_t type)
3262 {
3263         isc_result_t result;
3264         dns_adbfetch_t *fetch = NULL;
3265         dns_adb_t *adb;
3266         dns_fixedname_t fixed;
3267         dns_name_t *name;
3268         dns_rdataset_t rdataset;
3269         dns_rdataset_t *nameservers;
3270         unsigned int options;
3271
3272         INSIST(DNS_ADBNAME_VALID(adbname));
3273         adb = adbname->adb;
3274         INSIST(DNS_ADB_VALID(adb));
3275
3276         INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
3277                (type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
3278
3279         adbname->fetch_err = FIND_ERR_NOTFOUND;
3280
3281         name = NULL;
3282         nameservers = NULL;
3283         dns_rdataset_init(&rdataset);
3284
3285         options = DNS_FETCHOPT_NOVALIDATE;
3286         if (start_at_zone) {
3287                 DP(ENTER_LEVEL,
3288                    "fetch_name: starting at zone for name %p",
3289                    adbname);
3290                 dns_fixedname_init(&fixed);
3291                 name = dns_fixedname_name(&fixed);
3292                 result = dns_view_findzonecut2(adb->view, &adbname->name, name,
3293                                                0, 0, ISC_TRUE, ISC_FALSE,
3294                                                &rdataset, NULL);
3295                 if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
3296                         goto cleanup;
3297                 nameservers = &rdataset;
3298                 options |= DNS_FETCHOPT_UNSHARED;
3299         }
3300
3301         fetch = new_adbfetch(adb);
3302         if (fetch == NULL) {
3303                 result = ISC_R_NOMEMORY;
3304                 goto cleanup;
3305         }
3306
3307         result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
3308                                           type, name, nameservers, NULL,
3309                                           options, adb->task, fetch_callback,
3310                                           adbname, &fetch->rdataset, NULL,
3311                                           &fetch->fetch);
3312         if (result != ISC_R_SUCCESS)
3313                 goto cleanup;
3314
3315         if (type == dns_rdatatype_a)
3316                 adbname->fetch_a = fetch;
3317         else
3318                 adbname->fetch_aaaa = fetch;
3319         fetch = NULL;  /* Keep us from cleaning this up below. */
3320
3321  cleanup:
3322         if (fetch != NULL)
3323                 free_adbfetch(adb, &fetch);
3324         if (dns_rdataset_isassociated(&rdataset))
3325                 dns_rdataset_disassociate(&rdataset);
3326
3327         return (result);
3328 }
3329
3330 /*
3331  * XXXMLG Needs to take a find argument and an address info, no zone or adb,
3332  * since these can be extracted from the find itself.
3333  */
3334 isc_result_t
3335 dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
3336                  isc_stdtime_t expire_time)
3337 {
3338         dns_adbzoneinfo_t *zi;
3339         int bucket;
3340         isc_result_t result = ISC_R_SUCCESS;
3341
3342         REQUIRE(DNS_ADB_VALID(adb));
3343         REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3344         REQUIRE(zone != NULL);
3345
3346         bucket = addr->entry->lock_bucket;
3347         LOCK(&adb->entrylocks[bucket]);
3348         zi = ISC_LIST_HEAD(addr->entry->zoneinfo);
3349         while (zi != NULL && !dns_name_equal(zone, &zi->zone))
3350                 zi = ISC_LIST_NEXT(zi, plink);
3351         if (zi != NULL) {
3352                 if (expire_time > zi->lame_timer)
3353                         zi->lame_timer = expire_time;
3354                 goto unlock;
3355         }
3356         zi = new_adbzoneinfo(adb, zone);
3357         if (zi == NULL) {
3358                 result = ISC_R_NOMEMORY;
3359                 goto unlock;
3360         }
3361
3362         zi->lame_timer = expire_time;
3363
3364         ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, plink);
3365  unlock:
3366         UNLOCK(&adb->entrylocks[bucket]);
3367
3368         return (result);
3369 }
3370
3371 void
3372 dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3373                    unsigned int rtt, unsigned int factor)
3374 {
3375         int bucket;
3376         unsigned int new_srtt;
3377         isc_stdtime_t now;
3378
3379         REQUIRE(DNS_ADB_VALID(adb));
3380         REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3381         REQUIRE(factor <= 10);
3382
3383         bucket = addr->entry->lock_bucket;
3384         LOCK(&adb->entrylocks[bucket]);
3385
3386         if (factor == DNS_ADB_RTTADJAGE)
3387                 new_srtt = addr->entry->srtt * 98 / 100;
3388         else
3389                 new_srtt = (addr->entry->srtt / 10 * factor)
3390                         + (rtt / 10 * (10 - factor));
3391
3392         addr->entry->srtt = new_srtt;
3393         addr->srtt = new_srtt;
3394
3395         isc_stdtime_get(&now);
3396         addr->entry->expires = now + ADB_ENTRY_WINDOW;
3397
3398         UNLOCK(&adb->entrylocks[bucket]);
3399 }
3400
3401 void
3402 dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
3403                     unsigned int bits, unsigned int mask)
3404 {
3405         int bucket;
3406
3407         REQUIRE(DNS_ADB_VALID(adb));
3408         REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3409
3410         bucket = addr->entry->lock_bucket;
3411         LOCK(&adb->entrylocks[bucket]);
3412
3413         addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
3414         /*
3415          * Note that we do not update the other bits in addr->flags with
3416          * the most recent values from addr->entry->flags.
3417          */
3418         addr->flags = (addr->flags & ~mask) | (bits & mask);
3419
3420         UNLOCK(&adb->entrylocks[bucket]);
3421 }
3422
3423 isc_result_t
3424 dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
3425                      dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
3426 {
3427         int bucket;
3428         dns_adbentry_t *entry;
3429         dns_adbaddrinfo_t *addr;
3430         isc_result_t result;
3431         in_port_t port;
3432
3433         REQUIRE(DNS_ADB_VALID(adb));
3434         REQUIRE(addrp != NULL && *addrp == NULL);
3435
3436         UNUSED(now);
3437
3438         result = ISC_R_SUCCESS;
3439         bucket = DNS_ADB_INVALIDBUCKET;
3440         entry = find_entry_and_lock(adb, sa, &bucket);
3441         if (adb->entry_sd[bucket]) {
3442                 result = ISC_R_SHUTTINGDOWN;
3443                 goto unlock;
3444         }
3445         if (entry == NULL) {
3446                 /*
3447                  * We don't know anything about this address.
3448                  */
3449                 entry = new_adbentry(adb);
3450                 if (entry == NULL) {
3451                         result = ISC_R_NOMEMORY;
3452                         goto unlock;
3453                 }
3454                 entry->sockaddr = *sa;
3455                 link_entry(adb, bucket, entry);
3456                 DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
3457         } else
3458                 DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
3459
3460         port = isc_sockaddr_getport(sa);
3461         addr = new_adbaddrinfo(adb, entry, port);
3462         if (addr != NULL) {
3463                 inc_entry_refcnt(adb, entry, ISC_FALSE);
3464                 *addrp = addr;
3465         }
3466
3467  unlock:
3468         UNLOCK(&adb->entrylocks[bucket]);
3469
3470         return (result);
3471 }
3472
3473 void
3474 dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
3475         dns_adbaddrinfo_t *addr;
3476         dns_adbentry_t *entry;
3477         int bucket;
3478         isc_stdtime_t now;
3479         isc_boolean_t want_check_exit = ISC_FALSE;
3480
3481         REQUIRE(DNS_ADB_VALID(adb));
3482         REQUIRE(addrp != NULL);
3483         addr = *addrp;
3484         REQUIRE(DNS_ADBADDRINFO_VALID(addr));
3485         entry = addr->entry;
3486         REQUIRE(DNS_ADBENTRY_VALID(entry));
3487
3488         isc_stdtime_get(&now);
3489
3490         *addrp = NULL;
3491
3492         bucket = addr->entry->lock_bucket;
3493         LOCK(&adb->entrylocks[bucket]);
3494
3495         entry->expires = now + ADB_ENTRY_WINDOW;
3496
3497         want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
3498
3499         UNLOCK(&adb->entrylocks[bucket]);
3500
3501         addr->entry = NULL;
3502         free_adbaddrinfo(adb, &addr);
3503
3504         if (want_check_exit) {
3505                 LOCK(&adb->lock);
3506                 check_exit(adb);
3507                 UNLOCK(&adb->lock);
3508         }
3509 }
3510
3511 void
3512 dns_adb_flush(dns_adb_t *adb) {
3513         unsigned int i;
3514
3515         INSIST(DNS_ADB_VALID(adb));
3516
3517         LOCK(&adb->lock);
3518
3519         /*
3520          * Call our cleanup routines.
3521          */
3522         for (i = 0; i < NBUCKETS; i++)
3523                 RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
3524         for (i = 0; i < NBUCKETS; i++)
3525                 RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
3526
3527 #ifdef DUMP_ADB_AFTER_CLEANING
3528         dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
3529 #endif
3530
3531         UNLOCK(&adb->lock);
3532 }
3533
3534 void
3535 dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
3536         dns_adbname_t *adbname;
3537         dns_adbname_t *nextname;
3538         int bucket;
3539
3540         INSIST(DNS_ADB_VALID(adb));
3541
3542         LOCK(&adb->lock);
3543         bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
3544         LOCK(&adb->namelocks[bucket]);
3545         adbname = ISC_LIST_HEAD(adb->names[bucket]);
3546         while (adbname != NULL) {
3547                 nextname = ISC_LIST_NEXT(adbname, plink);
3548                 if (!NAME_DEAD(adbname) &&
3549                     dns_name_equal(name, &adbname->name)) {
3550                         RUNTIME_CHECK(kill_name(&adbname,
3551                                                 DNS_EVENT_ADBCANCELED) ==
3552                                       ISC_FALSE);
3553                 }
3554                 adbname = nextname;
3555         }
3556         UNLOCK(&adb->namelocks[bucket]);
3557         UNLOCK(&adb->lock);
3558 }
3559
3560 static void
3561 water(void *arg, int mark) {
3562         dns_adb_t *adb = arg;
3563         isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
3564         isc_interval_t interval;
3565
3566         REQUIRE(DNS_ADB_VALID(adb));
3567
3568         DP(ISC_LOG_DEBUG(1),
3569            "adb reached %s water mark", overmem ? "high" : "low");
3570
3571         adb->overmem = overmem;
3572         if (overmem) {
3573                 isc_interval_set(&interval, 0, 1);
3574                 (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
3575                                       &interval, ISC_TRUE);
3576         }
3577 }
3578
3579 void
3580 dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
3581         isc_uint32_t hiwater;
3582         isc_uint32_t lowater;
3583
3584         INSIST(DNS_ADB_VALID(adb));
3585
3586         if (size != 0 && size < DNS_ADB_MINADBSIZE)
3587                 size = DNS_ADB_MINADBSIZE;
3588
3589         hiwater = size - (size >> 3);   /* Approximately 7/8ths. */
3590         lowater = size - (size >> 2);   /* Approximately 3/4ths. */
3591
3592         if (size == 0 || hiwater == 0 || lowater == 0)
3593                 isc_mem_setwater(adb->mctx, water, adb, 0, 0);
3594         else
3595                 isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);
3596 }