adb.c revision a14eb88840e06b8d458c1556e5452b6d2a50012e
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 1999, 2000 Internet Software Consortium.
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Implementation notes
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * --------------------
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff *
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff * In finds, if task == NULL, no events will be generated, and no events
de153390f5a1f6d4fa86af91d4cae772d9846ca0Mark Andrews * have been sent. If task != NULL but taskaction == NULL, an event has been
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff * posted but not yet freed. If neither are NULL, no event was posted.
822f6cdabb1edd44472c7a758b5cae71376fa9beBrian Wellington *
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*
242bba8991b030b7764f0bdca3922d75c34ea51eAndreas Gustafsson * After we have cleaned all buckets, dump the database contents.
25a66b4e41e2b0a2af4840749bac80ae78c678bfMark Andrews */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence#define DUMP_ADB_AFTER_CLEANING
21f1794606dce19928cf455029e173321f166380Mark Andrews
973a19342597823f111fce6a8cd5adfd0e2e7c0dMark Andrews#include <config.h>
0c310d16b05ee94743d33f6920907edee6084fc8Michael Graff
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <limits.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/mutexblock.h>
058e44186b74531402c1f99088eb9dbe4926f8daMark Andrews#include <isc/random.h>
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews#include <isc/string.h> /* Required for HP/UX (and others?) */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/task.h>
21825a8d005ccc2dfaf12889bf9eef3413555277Brian Wellington#include <isc/timer.h>
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#include <isc/util.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/a6.h>
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews#include <dns/adb.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/db.h>
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews#include <dns/events.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/log.h>
54c26ab21c61c6d6b1e484bb88dc3ac263845d17Mark Andrews#include <dns/rdata.h>
3ddd92da6651bc72aa79a04195ad389d86fd1a66Andreas Gustafsson#include <dns/rdataset.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/resolver.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <dns/result.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBFETCH_MAGIC 0x61644634 /* adF4. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBFETCH6_MAGIC 0x61644636 /* adF6. */
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
eb6bd543c7d072efdca509eb17f8f301c1467b53Mark Andrews
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews/*
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews * The number of buckets needs to be a prime (for good hashing).
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews *
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence * XXXRTH How many buckets do we need?
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence *
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * This value must be coordinated with CLEAN_SECONDS (below).
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define NBUCKETS 1009 /* how many buckets for names/addrs */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence/*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * For type 3 negative cache entries, we will remember that the address is
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence * broken for this long. XXXMLG This is also used for actual addresses, too.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * The intent is to keep us from constantly asking about A/A6/AAAA records
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * if the zone has extremely low TTLs.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define ADB_CACHE_MINIMUM 10 /* seconds */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence/*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Clean CLEAN_BUCKETS buckets every CLEAN_SECONDS.
0b056755b2f423ba5f6adac8f7851d78f7d11437David Lawrence */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define CLEAN_PERIOD 3600 /* one pass through every N seconds */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define CLEAN_BUCKETS ((NBUCKETS / CLEAN_PERIOD) + 0.5)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#if CLEAN_BUCKET < 1
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#undef CLEAN_BUCKETS
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define CLEAN_BUCKETS 1
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#endif
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define CLEAN_SECONDS (CLEAN_PERIOD * CLEAN_BUCKETS / NBUCKETS)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#if CLEAN_SECONDS < 1
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#undef CLEAN_SECONDS
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define CLEAN_SECONDS 1
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#endif
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FREE_ITEMS 64 /* free count for memory pools */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FILL_COUNT 16 /* fill count for memory pools */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeintypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencetypedef struct dns_adbnamehook dns_adbnamehook_t;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafssontypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafssontypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafssontypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafssontypedef struct dns_adbfetch dns_adbfetch_t;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeintypedef struct dns_adbfetch6 dns_adbfetch6_t;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstruct dns_adb {
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater unsigned int magic;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson isc_mutex_t lock;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mutex_t ilock;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mem_t *mctx;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_view_t *view;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_timermgr_t *timermgr;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_timer_t *timer;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_taskmgr_t *taskmgr;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_task_t *task;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_interval_t tick_interval;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont int next_cleanbucket;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont unsigned int irefcnt;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont unsigned int erefcnt;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mutex_t mplock;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_t *nmp; /* dns_adbname_t */
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_t *nhmp; /* dns_adbnamehook_t */
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mempool_t *emp; /* dns_adbentry_t */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mempool_t *ahmp; /* dns_adbfind_t */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mempool_t *afmp; /* dns_adbfetch_t */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mempool_t *af6mp; /* dns_adbfetch6_t */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_random_t rand;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence /*
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington * Bucketized locks and lists for names.
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington *
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbnamelist_t names[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mutex_t namelocks[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_boolean_t name_sd[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int name_refcnt[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence /*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Bucketized locks for entries.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * XXXRTH Have a per-bucket structure that contains all of these?
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbentrylist_t entries[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_mutex_t entrylocks[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_boolean_t entry_sd[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int entry_refcnt[NBUCKETS];
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_event_t cevent;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_boolean_t cevent_sent;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_boolean_t shutting_down;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_eventlist_t whenshutdown;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence};
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence/*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * XXXMLG Document these structures.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestruct dns_adbname {
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews unsigned int magic;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews dns_name_t name;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews dns_adb_t *adb;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int partial_result;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int flags;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence int lock_bucket;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_name_t target;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_stdtime_t expire_target;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_stdtime_t expire_v4;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_stdtime_t expire_v6;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int chains;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbnamehooklist_t v4;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbnamehooklist_t v6;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbfetch_t *fetch_a;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbfetch_t *fetch_aaaa;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ISC_LIST(dns_adbfetch6_t) fetches_a6;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbfindlist_t finds;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ISC_LINK(dns_adbname_t) plink;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence};
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssonstruct dns_adbfetch {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int magic;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbnamehook_t *namehook;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_adbentry_t *entry;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_fetch_t *fetch;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_rdataset_t rdataset;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence};
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrewsstruct dns_adbfetch6 {
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews unsigned int magic;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence unsigned int flags;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbnamehook_t *namehook;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbentry_t *entry;
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson dns_fetch_t *fetch;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_rdataset_t rdataset;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_a6context_t a6ctx;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews ISC_LINK(dns_adbfetch6_t) plink;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews};
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews/*
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews * dns_adbnamehook_t
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews *
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * This is a small widget that dangles off a dns_adbname_t. It contains a
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * pointer to the address information about this host, and a link to the next
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews * namehook that will contain the next address this host has.
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews */
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsstruct dns_adbnamehook {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews unsigned int magic;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews dns_adbentry_t *entry;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews ISC_LINK(dns_adbnamehook_t) plink;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews};
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews/*
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews * dns_adbzoneinfo_t
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews *
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews * This is a small widget that holds zone-specific information about an
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews * address. Currently limited to lameness, but could just as easily be
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews * extended to other types of information about zones.
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews */
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrewsstruct dns_adbzoneinfo {
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews unsigned int magic;
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews
aa05bbdef7f7827dde158dcc913f4dade84c8511Brian Wellington dns_name_t zone;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews isc_stdtime_t lame_timer;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews ISC_LINK(dns_adbzoneinfo_t) plink;
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews};
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews
bddfe77128b0f16af263ff149db40f0d885f43d0Mark Andrews/*
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews * An address entry. It holds quite a bit of information about addresses,
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews * including edns state, rtt, and of course the address of the host.
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews */
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrewsstruct dns_adbentry {
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington unsigned int magic;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews int lock_bucket;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews unsigned int refcnt;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews unsigned int flags;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews int edns_level; /* must be int! */
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews int goodness; /* bad < 0 <= good */
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews unsigned int srtt;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews isc_sockaddr_t sockaddr;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews isc_stdtime_t expires;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews isc_stdtime_t avoid_bitstring;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
f6407f9a0b890bebbfd5f738d9c4aef3d3315fe9Michael Graff ISC_LINK(dns_adbentry_t) plink;
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews};
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews
2002be4f65776451676df6ee21a2e28f52bcad6dMark Andrews/*
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews * Internal functions (and prototypes).
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrews dns_adbentry_t *);
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrewsstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
bed8e84810a80dad3d37870be927d1dfd015f480Mark Andrewsstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void free_adbfind(dns_adb_t *, dns_adbfind_t **);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews in_port_t);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_a6context_t *);
d981ca645597116d227a48bf37cc5edc061c854dBob Halleystatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
e6bd97dded968f82e26b270842b789bff7bca422Mark Andrews unsigned int, int *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_sockaddr_t *, int *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void dump_adb(dns_adb_t *, FILE *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void print_dns_name(FILE *, dns_name_t *);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic void print_namehook_list(FILE *, dns_adbname_t *);
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrewsstatic void print_find_list(FILE *, dns_adbname_t *);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic void print_fetch_list(FILE *, dns_adbname_t *);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic inline void dec_adb_irefcnt(dns_adb_t *);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic inline void dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews isc_boolean_t);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews isc_boolean_t);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic void clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic void clean_target(dns_adb_t *, dns_name_t *);
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrewsstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
389c749a5ee18f1c0d6278ae49f2aae5d5f0d2dcMark Andrews unsigned int);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void cancel_fetches_at_name(dns_adbname_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_rdatatype_t);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic isc_result_t fetch_name_aaaa(dns_adbname_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic inline void check_exit(dns_adb_t *);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic void timer_cleanup(isc_task_t *, isc_event_t *);
cabaeca9ae5b98c80586b91e89cf552e17a75a9bBrian Wellingtonstatic void destroy(dns_adb_t *);
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellingtonstatic void shutdown_names(dns_adb_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void shutdown_entries(dns_adb_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic inline void unlink_name(dns_adb_t *, dns_adbname_t *);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic inline void unlink_entry(dns_adb_t *, dns_adbentry_t *);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic void kill_name(dns_adbname_t **, isc_eventtype_t);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrewsstatic isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t);
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews/*
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
29c818c7d40fc8898b062903ec703851328a4deaMark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define FIND_EVENT_SENT 0x40000000
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define FIND_EVENT_FREED 0x80000000
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define NAME_NEEDS_POKE 0x80000000
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews#define NAME_IS_DEAD 0x40000000
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews#define NAME_HINT_OK DNS_ADBFIND_HINTOK
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * To the name, address classes are all that really exist. If it has a
d981ca645597116d227a48bf37cc5edc061c854dBob Halley * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
d981ca645597116d227a48bf37cc5edc061c854dBob Halley */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Fetches are broken out into A, AAAA, and A6 types. In some cases,
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * however, it makes more sense to test for a particular class of fetches,
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * like V4 or V6 above.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews#define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
90e303b114e56db5809fdd19805243457fa43cd9Olafur Gudmundsson#define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * Was this fetch started using the hints database?
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * Was this the initial fetch for the A6 record? If so, we might want to
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * start AAAA queries if it fails.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FETCH_FIRST_A6 0x80000000
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews/*
735ca24fa61ad9cd0285776c2fc4c14032f8f194Mark Andrews * Find options and tests to see if there are addresses on the list.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_STARTATROOT(fn) (((fn)->options & DNS_ADBFIND_STARTATROOT) \
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews/*
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * These are currently used on simple unsigned ints, so they are
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews * not really associated with any particular type.
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews */
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Find out if the flags on a name (nf) indicate if it is a hint or
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * glue, and compare this to the appropriate bits set in o, to see if
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * this is ok.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
d981ca645597116d227a48bf37cc5edc061c854dBob Halley#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#define ENTER_LEVEL 50
19d365e4448f1782611280b020987988b7ac3210Mark Andrews#define EXIT_LEVEL ENTER_LEVEL
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington#define CLEAN_LEVEL 100
19d365e4448f1782611280b020987988b7ac3210Mark Andrews#define DEF_LEVEL 5
19d365e4448f1782611280b020987988b7ac3210Mark Andrews#define NCACHE_LEVEL 20
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews (r) == DNS_R_NCACHENXRRSET)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
d981ca645597116d227a48bf37cc5edc061c854dBob Halley (r) == DNS_R_NXRRSET)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark AndrewsDP(int level, const char *format, ...) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews va_list args;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews va_start(args, format);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews isc_log_vwrite(dns_lctx,
19d365e4448f1782611280b020987988b7ac3210Mark Andrews DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews ISC_LOG_DEBUG(level), format, args);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews va_end(args);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_ttl_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsttlclamp(dns_ttl_t ttl) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (ttl < ADB_CACHE_MINIMUM)
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ttl = ADB_CACHE_MINIMUM;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews if (ttl > ADB_CACHE_MAXIMUM)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews ttl = ADB_CACHE_MAXIMUM;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews return (ttl);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Requires the adbname bucket be locked and that no entry buckets be locked.
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews *
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews * This code handles A and AAAA rdatasets only.
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic isc_result_t
19d365e4448f1782611280b020987988b7ac3210Mark Andrewsimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews isc_stdtime_t now)
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington{
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews isc_result_t result;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_adb_t *adb;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews dns_adbnamehook_t *nh;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews dns_rdata_t rdata;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews struct in_addr ina;
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson struct in6_addr in6a;
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson isc_sockaddr_t sockaddr;
6bebabb376b93e7d12f53a253b197a3fc0e0b001Andreas Gustafsson dns_adbentry_t *foundentry; /* NO CLEAN UP! */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int addr_bucket;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_boolean_t new_addresses_added;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdatatype_t rdtype;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews unsigned int findoptions;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews INSIST(DNS_ADBNAME_VALID(adbname));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews adb = adbname->adb;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(DNS_ADB_VALID(adb));
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews rdtype = rdataset->type;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (rdtype == dns_rdatatype_a)
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews findoptions = DNS_ADBFIND_INET;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews else
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews findoptions = DNS_ADBFIND_INET6;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews addr_bucket = DNS_ADB_INVALIDBUCKET;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews new_addresses_added = ISC_FALSE;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews nh = NULL;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews result = dns_rdataset_first(rdataset);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews while (result == ISC_R_SUCCESS) {
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews dns_rdataset_current(rdataset, &rdata);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews if (rdtype == dns_rdatatype_a) {
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews INSIST(rdata.length == 4);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews memcpy(&ina.s_addr, rdata.data, 4);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews isc_sockaddr_fromin(&sockaddr, &ina, 0);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence } else {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence INSIST(rdata.length == 16);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence memcpy(in6a.s6_addr, rdata.data, 16);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
ad7209ea70d346527ffdcda335153831341d9dcfAndreas Gustafsson INSIST(nh == NULL);
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews nh = new_adbnamehook(adb, NULL);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews if (nh == NULL) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley adbname->partial_result |= findoptions;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = ISC_R_NOMEMORY;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews goto fail;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (foundentry == NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_adbentry_t *entry;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews entry = new_adbentry(adb);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (entry == NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews adbname->partial_result |= findoptions;
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington result = ISC_R_NOMEMORY;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews goto fail;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley entry->sockaddr = sockaddr;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews entry->refcnt = 1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
19d365e4448f1782611280b020987988b7ac3210Mark Andrews nh->entry = entry;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence link_entry(adb, addr_bucket, entry);
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson } else {
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson foundentry->refcnt++;
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson nh->entry = foundentry;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews }
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson new_addresses_added = ISC_TRUE;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews if (rdtype == dns_rdatatype_a)
693ddf84daa745a0ea8ca311a8154dfa03eabc43Andreas Gustafsson ISC_LIST_APPEND(adbname->v4, nh, plink);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews else
19d365e4448f1782611280b020987988b7ac3210Mark Andrews ISC_LIST_APPEND(adbname->v6, nh, plink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews nh = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews result = dns_rdataset_next(rdataset);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff fail:
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley if (nh != NULL)
f0a5bb8f86631ce638cb2b6c65bbb9bcf9b0cdc0Bob Halley free_adbnamehook(adb, &nh);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (addr_bucket != DNS_ADB_INVALIDBUCKET)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff UNLOCK(&adb->entrylocks[addr_bucket]);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence rdataset->ttl = ttlclamp(rdataset->ttl);
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (rdtype == dns_rdatatype_a) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews adbname->expire_v4, now + rdataset->ttl);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson now + rdataset->ttl);
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson } else {
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson adbname->expire_v6, now + rdataset->ttl);
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
3a16668468060842e847ea6556e80e1405d35cd6Brian Wellington now + rdataset->ttl);
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews if (new_addresses_added) {
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington /*
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington * Lie a little here. This is more or less so code that cares
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington * can find out if any new information was added or not.
942d1a339b1fe617f7d17d66cb5fccce798d15aeBrian Wellington */
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington return (ISC_R_SUCCESS);
7e9d637131516486630290d36c4c0db544cb700eMark Andrews }
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews return (result);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews}
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrewsstatic void
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellingtonimport_a6(dns_a6context_t *a6ctx) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_adbname_t *name;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_adb_t *adb;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_adbnamehook_t *nh;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington int addr_bucket;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington isc_sockaddr_t sockaddr;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
de5247ae1683ce145662180ee50272d2214a0232Andreas Gustafsson name = a6ctx->arg;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington INSIST(DNS_ADBNAME_VALID(name));
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington adb = name->adb;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington INSIST(DNS_ADB_VALID(adb));
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews addr_bucket = DNS_ADB_INVALIDBUCKET;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
f16495732753175e4a9fc144323a12fdcc29b561Brian Wellington DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington nh = new_adbnamehook(adb, NULL);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater if (nh == NULL) {
e086935752b6e2f51ef2985fee21ccfff617b115David Lawrence name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
e086935752b6e2f51ef2985fee21ccfff617b115David Lawrence goto fail;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 0);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington if (foundentry == NULL) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington dns_adbentry_t *entry;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington entry = new_adbentry(adb);
7e4cda9965e2edf2ec43c57967eec8eff7061ab0Andreas Gustafsson if (entry == NULL) {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington name->partial_result |= DNS_ADBFIND_INET6;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington goto fail;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington entry->sockaddr = sockaddr;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington entry->refcnt = 1;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington nh->entry = entry;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington link_entry(adb, addr_bucket, entry);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington } else {
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington foundentry->refcnt++;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington nh->entry = foundentry;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington }
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington ISC_LIST_APPEND(name->v6, nh, plink);
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington nh = NULL;
93ed317bb43658ed48ee7439f7a36bb9bcf80c94Brian Wellington
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff fail:
d981ca645597116d227a48bf37cc5edc061c854dBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews name->expire_v6, a6ctx->expiration);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews name->flags |= NAME_NEEDS_POKE;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (nh != NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews free_adbnamehook(adb, &nh);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (addr_bucket != DNS_ADB_INVALIDBUCKET)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNLOCK(&adb->entrylocks[addr_bucket]);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews}
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence/*
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews * Requires the name's bucket be locked.
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews */
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrewsstatic void
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_adbname_t *name;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_adb_t *adb;
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews INSIST(n != NULL);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews name = *n;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews *n = NULL;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews INSIST(DNS_ADBNAME_VALID(name));
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews adb = name->adb;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews INSIST(DNS_ADB_VALID(adb));
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews DP(DEF_LEVEL, "killing name %p", name);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews /*
228c679d7a269423019f7c528db92e855f08240bMark Andrews * If we're dead already, just check to see if we should go
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews * away now or not.
228c679d7a269423019f7c528db92e855f08240bMark Andrews */
228c679d7a269423019f7c528db92e855f08240bMark Andrews if (NAME_DEAD(name) && !NAME_FETCH(name)) {
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington unlink_name(adb, name);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington free_adbname(adb, &name);
228c679d7a269423019f7c528db92e855f08240bMark Andrews return;
228c679d7a269423019f7c528db92e855f08240bMark Andrews }
228c679d7a269423019f7c528db92e855f08240bMark Andrews
c51f2c9a529b2130a8058cb7a32bd284fa6cfdb9Mark Andrews /*
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington * Clean up the name's various lists. These two are destructive
228c679d7a269423019f7c528db92e855f08240bMark Andrews * in that they will always empty the list.
228c679d7a269423019f7c528db92e855f08240bMark Andrews */
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews clean_namehooks(adb, &name->v4);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews clean_namehooks(adb, &name->v6);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews clean_target(adb, &name->target);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews /*
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews * If fetches are running, cancel them. If none are running, we can
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews * just kill the name here.
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews */
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews if (!NAME_FETCH(name)) {
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews unlink_name(adb, name);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews free_adbname(adb, &name);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews } else {
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews name->flags |= NAME_IS_DEAD;
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews cancel_fetches_at_name(name);
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews }
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews}
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews/*
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews * Requires the name's bucket be locked and no entry buckets be locked.
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews */
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrewsstatic void
b8e2e5dd861bdc72a5496a745a85f8761bef7dc4Tinderbox Usercheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington dns_adb_t *adb;
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington
e85702ce5be33d7c07eff6487c6bb4730165f331Mark Andrews INSIST(DNS_ADBNAME_VALID(name));
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington adb = name->adb;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews INSIST(DNS_ADB_VALID(adb));
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews /*
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews * Check to see if we need to remove the v4 addresses
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (NAME_HAS_V4(name)) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews DP(DEF_LEVEL, "expiring v4 for name %p", name);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews clean_namehooks(adb, &name->v4);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews name->partial_result &= ~DNS_ADBFIND_INET;
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews }
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews name->expire_v4 = INT_MAX;
8dd5237a27e2e824d18f835dc711573aeb23a173Mark Andrews }
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews /*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * Check to see if we need to remove the v6 addresses
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews */
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (NAME_HAS_V6(name)) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff DP(DEF_LEVEL, "expiring v6 for name %p", name);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews clean_namehooks(adb, &name->v6);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews name->partial_result &= ~DNS_ADBFIND_INET6;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews }
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews name->expire_v6 = INT_MAX;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews }
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff /*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * Check to see if we need to remove the alias target.
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (EXPIRE_OK(name->expire_target, now)) {
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews clean_target(adb, &name->target);
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews name->expire_target = INT_MAX;
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews }
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews}
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Requires the name's bucket be locked.
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews */
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewsstatic inline void
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrewslink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews ISC_LIST_PREPEND(adb->names[bucket], name, plink);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews name->lock_bucket = bucket;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley adb->name_refcnt[bucket]++;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Requires the name's bucket be locked.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews int bucket;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews bucket = name->lock_bucket;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews INSIST(bucket != DNS_ADB_INVALIDBUCKET);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews name->lock_bucket = DNS_ADB_INVALIDBUCKET;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews INSIST(adb->name_refcnt[bucket] > 0);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews adb->name_refcnt[bucket]--;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews dec_adb_irefcnt(adb);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews/*
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews * Requires the entry's bucket be locked.
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews */
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsstatic inline void
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewslink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews entry->lock_bucket = bucket;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews adb->entry_refcnt[bucket]++;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews/*
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews * Requires the entry's bucket be locked.
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews */
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsstatic inline void
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews int bucket;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews bucket = entry->lock_bucket;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews INSIST(bucket != DNS_ADB_INVALIDBUCKET);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews INSIST(adb->entry_refcnt[bucket] > 0);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews adb->entry_refcnt[bucket]--;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews dec_adb_irefcnt(adb);
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews}
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrewsstatic inline void
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley UNLOCK(have);
19d365e4448f1782611280b020987988b7ac3210Mark Andrews LOCK(want);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff LOCK(have);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence/*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * The ADB _MUST_ be locked before calling. Also, exit conditions must be
19d365e4448f1782611280b020987988b7ac3210Mark Andrews * checked after calling this function.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestatic void
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceshutdown_names(dns_adb_t *adb) {
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews int bucket;
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews dns_adbname_t *name;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_adbname_t *next_name;
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews
f8aae502686e2448c48f56697c212a50e2a1cbaeAndreas Gustafsson for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews LOCK(&adb->namelocks[bucket]);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews adb->name_sd[bucket] = ISC_TRUE;
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews
fe3472c80b76b6fed0ae674fd63471d02477a03aMark Andrews name = ISC_LIST_HEAD(adb->names[bucket]);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (name == NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * This bucket has no names. We must decrement the
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * irefcnt ourselves, since it will not be
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * automatically triggered by a name being unlinked.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson dec_adb_irefcnt(adb);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews } else {
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews /*
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews * Run through the list. For each name, clean up finds
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews * found there, and cancel any fetches running. When
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * all the fetches are canceled, the name will destroy
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * itself.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson while (name != NULL) {
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson next_name = ISC_LIST_NEXT(name, plink);
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson kill_name(&name, DNS_EVENT_ADBSHUTDOWN);
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson name = next_name;
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley }
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson }
7ab0e69f61e61e81d489c95c7ebd981e74e7ef16Andreas Gustafsson
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff UNLOCK(&adb->namelocks[bucket]);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson }
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson}
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt/*
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * The ADB _MUST_ be locked before calling. Also, exit conditions must be
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews * checked after calling this function.
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsstatic void
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrewsshutdown_entries(dns_adb_t *adb) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence int bucket;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbentry_t *entry;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbentry_t *next_entry;
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt LOCK(&adb->entrylocks[bucket]);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson adb->entry_sd[bucket] = ISC_TRUE;
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt entry = ISC_LIST_HEAD(adb->entries[bucket]);
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt if (entry == NULL) {
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt /*
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * This bucket has no entries. We must decrement the
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt * irefcnt ourselves, since it will not be
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt * automatically triggered by an entry being unlinked.
9a859983d7059a6eb9c877c1d2ac6a3a5b7170f7Evan Hunt */
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson dec_adb_irefcnt(adb);
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson } else {
ae70d32b67cf30e06553c01479e71c87b21d984cBob Halley /*
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * Run through the list. Cleanup any entries not
0c8649cea98afc061dd2938fd315df53b8fc35caAndreas Gustafsson * associated with names, and which are not in use.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley while (entry != NULL) {
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews next_entry = ISC_LIST_NEXT(entry, plink);
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews if (entry->refcnt == 0 &&
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews entry->expires != 0) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff unlink_entry(adb, entry);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews free_adbentry(adb, &entry);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews entry = next_entry;
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
19d365e4448f1782611280b020987988b7ac3210Mark Andrews
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews UNLOCK(&adb->entrylocks[bucket]);
f1cae4bcb7ee3060d893f5ab3ba55c1820bf3e4aBrian Wellington }
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews}
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
19d365e4448f1782611280b020987988b7ac3210Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Name bucket must be locked
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic void
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewscancel_fetches_at_name(dns_adbname_t *name) {
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews dns_adbfetch6_t *fetch6;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (NAME_FETCH_A(name))
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews dns_resolver_cancelfetch(name->fetch_a->fetch);
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews if (NAME_FETCH_AAAA(name))
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
7d8cdd869ed2162a5befda7cc1600136110f54d6Mark Andrews
1bbd36c4db2c86762b582db4b0504e6e68a183f6Mark Andrews
d981ca645597116d227a48bf37cc5edc061c854dBob Halley fetch6 = ISC_LIST_HEAD(name->fetches_a6);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews while (fetch6 != NULL) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_resolver_cancelfetch(fetch6->fetch);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews fetch6 = ISC_LIST_NEXT(fetch6, plink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff/*
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews * Assumes the name bucket is locked.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff */
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrewsstatic void
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
19d365e4448f1782611280b020987988b7ac3210Mark Andrews dns_adbentry_t *entry;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_adbnamehook_t *namehook;
19d365e4448f1782611280b020987988b7ac3210Mark Andrews int addr_bucket;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews addr_bucket = DNS_ADB_INVALIDBUCKET;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews namehook = ISC_LIST_HEAD(*namehooks);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews while (namehook != NULL) {
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Clean up the entry if needed.
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews */
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews entry = namehook->entry;
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews if (entry != NULL) {
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews INSIST(DNS_ADBENTRY_VALID(entry));
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews if (addr_bucket != entry->lock_bucket) {
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews if (addr_bucket != DNS_ADB_INVALIDBUCKET)
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews UNLOCK(&adb->entrylocks[addr_bucket]);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff addr_bucket = entry->lock_bucket;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley LOCK(&adb->entrylocks[addr_bucket]);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dec_entry_refcnt(adb, entry, ISC_FALSE);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley /*
d981ca645597116d227a48bf37cc5edc061c854dBob Halley * Free the namehook
d981ca645597116d227a48bf37cc5edc061c854dBob Halley */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley namehook->entry = NULL;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ISC_LIST_UNLINK(*namehooks, namehook, plink);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley free_adbnamehook(adb, &namehook);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews namehook = ISC_LIST_HEAD(*namehooks);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews UNLOCK(&adb->entrylocks[addr_bucket]);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
d981ca645597116d227a48bf37cc5edc061c854dBob Halleystatic void
d981ca645597116d227a48bf37cc5edc061c854dBob Halleyclean_target(dns_adb_t *adb, dns_name_t *target) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (dns_name_countlabels(target) > 0) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley dns_name_free(target, adb->mctx);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff dns_name_init(target, NULL);
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrewsstatic isc_result_t
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halleyset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_rdataset_t *rdataset, dns_name_t *target)
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley{
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley isc_result_t result;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_namereln_t namereln;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley unsigned int nlabels, nbits;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley int order;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews dns_rdata_t rdata;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley isc_region_t r;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_name_t tname;
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley dns_fixedname_t fixed1, fixed2;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews dns_name_t *prefix, *new_target;
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews REQUIRE(dns_name_countlabels(target) == 0);
9916239908343b3eb17f0578de4c3cd6a313d85fMark Andrews
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley if (rdataset->type == dns_rdatatype_cname) {
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley /*
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley * Copy the CNAME's target into the target name.
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews result = dns_rdataset_first(rdataset);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (result != ISC_R_SUCCESS)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (result);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_rdataset_current(rdataset, &rdata);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews r.base = rdata.data;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews r.length = rdata.length;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_name_init(&tname, NULL);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_name_fromregion(&tname, &r);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews result = dns_name_dup(&tname, adb->mctx, target);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (result != ISC_R_SUCCESS)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (result);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews } else {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews INSIST(rdataset->type == dns_rdatatype_dname);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews namereln = dns_name_fullcompare(name, fname, &order,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews &nlabels, &nbits);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews INSIST(namereln == dns_namereln_subdomain);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews /*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Get the target name of the DNAME.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff result = dns_rdataset_first(rdataset);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff if (result != ISC_R_SUCCESS)
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff return (result);
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews dns_rdataset_current(rdataset, &rdata);
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews r.base = rdata.data;
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews r.length = rdata.length;
471e0563c7965c556c759775882cd3448dae78eaMark Andrews dns_name_init(&tname, NULL);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff dns_name_fromregion(&tname, &r);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff /*
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * Construct the new target name.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff dns_fixedname_init(&fixed1);
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews prefix = dns_fixedname_name(&fixed1);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff dns_fixedname_init(&fixed2);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff new_target = dns_fixedname_name(&fixed2);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff result = dns_name_split(name, nlabels, nbits, prefix, NULL);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff if (result != ISC_R_SUCCESS)
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff return (result);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff result = dns_name_concatenate(prefix, &tname, new_target,
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff NULL);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence if (result != ISC_R_SUCCESS)
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence return (result);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff result = dns_name_dup(new_target, adb->mctx, target);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff if (result != ISC_R_SUCCESS)
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff return (result);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff }
29747dfe5e073a299b3681e01f5c55540f8bfed7Mark Andrews
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff return (ISC_R_SUCCESS);
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff}
94a537e6ab3069f8d34e12e5ea722250be2b89c8Michael Graff
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews/*
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews * Assumes nothing is locked, since this is called by the client.
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews */
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrewsstatic void
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrewsevent_free(isc_event_t *event) {
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington dns_adbfind_t *find;
9be230cfca126bea6b666f506a230ffcdfba60dbMark Andrews
8695d7b357789bedff63e5b19c5ab25cd58fcd4bMark Andrews INSIST(event != NULL);
beed6a0e226fbd0c18c19a3e341a2003cba020a5Mark Andrews find = event->ev_destroy_arg;
beed6a0e226fbd0c18c19a3e341a2003cba020a5Mark Andrews INSIST(DNS_ADBFIND_VALID(find));
beed6a0e226fbd0c18c19a3e341a2003cba020a5Mark Andrews
9be230cfca126bea6b666f506a230ffcdfba60dbMark Andrews LOCK(&find->lock);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington find->flags |= FIND_EVENT_FREED;
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington event->ev_destroy_arg = NULL;
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington UNLOCK(&find->lock);
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington}
ebfcb6cf66283096ebda1503b6cc042ce86b6bedBrian Wellington
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews/*
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews * Assumes the name bucket is locked.
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic void
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrenceclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
471e0563c7965c556c759775882cd3448dae78eaMark Andrews unsigned int addrs)
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff{
28b863e609ff2d97b78663b46894494cfa2ea411Mark Andrews isc_event_t *ev;
cc083bb7031c04d57cbad41b2f5a796a4fd1865cMark Andrews isc_task_t *task;
471e0563c7965c556c759775882cd3448dae78eaMark Andrews dns_adbfind_t *find;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews dns_adbfind_t *next_find;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews isc_boolean_t process;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson unsigned int wanted;
8c962eba3d6faebc008806ebb6bb9d08089118e9Andreas Gustafsson
8c962eba3d6faebc008806ebb6bb9d08089118e9Andreas Gustafsson DP(ENTER_LEVEL,
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson name, evtype, addrs);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson find = ISC_LIST_HEAD(name->finds);
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews while (find != NULL) {
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews LOCK(&find->lock);
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews next_find = ISC_LIST_NEXT(find, plink);
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson
8c962eba3d6faebc008806ebb6bb9d08089118e9Andreas Gustafsson process = ISC_FALSE;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson switch (evtype) {
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson case DNS_EVENT_ADBMOREADDRESSES:
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson DP(3, "DNS_EVENT_ADBMOREADDRESSES");
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson if ((wanted & addrs) != 0) {
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson DP(3, "processing");
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson find->flags &= ~addrs;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson process = ISC_TRUE;
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews }
e01f55daa4b611190a11a40299007e5e55018854Mark Andrews break;
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson case DNS_EVENT_ADBNOMOREADDRESSES:
d3a86da2e8f09e2c3f55721aae537b9cacc7e537Andreas Gustafsson DP(3, "DNS_EVENT_ADBNOMOREADDRESSES");
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence find->flags &= ~addrs;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (wanted == 0) {
a98551ef592e9be6008e0141ceeb32efd586c5efMark Andrews process = ISC_TRUE;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews DP(3, "processing");
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews break;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews default:
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews find->flags &= ~addrs;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff process = ISC_TRUE;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (process) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews DP(DEF_LEVEL, "cfan: processing find %p", find);
fe47f41b13620bfafc4f8cf65d5df24f1e568764Bob Halley /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Unlink the find from the name, letting the caller
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * call dns_adb_destroyfind() on it to clean it up
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * later.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
fe47f41b13620bfafc4f8cf65d5df24f1e568764Bob Halley ISC_LIST_UNLINK(name->finds, find, plink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews find->adbname = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews find->name_bucket = DNS_ADB_INVALIDBUCKET;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(!FIND_EVENTSENT(find));
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ev = &find->event;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews task = ev->ev_sender;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ev->ev_sender = find;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ev->ev_type = evtype;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ev->ev_destroy = event_free;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ev->ev_destroy_arg = find;
ca2912424b1d7c348186a325dd2078a37bb8d818Andreas Gustafsson
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews DP(DEF_LEVEL,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff "Sending event %p to task %p for find %p",
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews ev, task, find);
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews isc_task_sendanddetach(&task, &ev);
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews } else {
c2b217ea1135f070da3856d3276895b3026e932aMark Andrews DP(DEF_LEVEL, "cfan: skipping find %p", find);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNLOCK(&find->lock);
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews find = next_find;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewscheck_exit(dns_adb_t *adb) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_event_t *event;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_task_t *etask;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_boolean_t zeroirefcnt;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * The caller must be holding the adb lock.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
fe47f41b13620bfafc4f8cf65d5df24f1e568764Bob Halley LOCK(&adb->ilock);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (adb->irefcnt == 0)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff zeroirefcnt = ISC_TRUE;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews else
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews zeroirefcnt = ISC_FALSE;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff UNLOCK(&adb->ilock);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (adb->shutting_down && zeroirefcnt &&
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews isc_mempool_getallocated(adb->ahmp) == 0) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews /*
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews * We're now shutdown. Send any whenshutdown events.
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews */
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews event = ISC_LIST_HEAD(adb->whenshutdown);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews while (event != NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence etask = event->ev_sender;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews event->ev_sender = adb;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews isc_task_sendanddetach(&etask, &event);
f0ff273b530afa730025e1c5ad311950f7ff4328Mark Andrews event = ISC_LIST_HEAD(adb->whenshutdown);
f0ff273b530afa730025e1c5ad311950f7ff4328Mark Andrews }
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews /*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * If there aren't any external references either, we're
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * done. Send the control event to initiate shutdown.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (adb->erefcnt == 0) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews INSIST(!adb->cevent_sent); /* Sanity check. */
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews event = &adb->cevent;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence isc_task_send(adb->task, &event);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence adb->cevent_sent = ISC_TRUE;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence }
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews }
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews}
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
ad493ef9ddb5a3e78e9d99f57abe75552f36a8f4Andreas Gustafssonstatic inline void
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrencedec_adb_irefcnt(dns_adb_t *adb) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews LOCK(&adb->ilock);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews INSIST(adb->irefcnt > 0);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews adb->irefcnt--;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews UNLOCK(&adb->ilock);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews}
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrewsstatic inline void
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrewsinc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (lock)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews LOCK(&adb->lock);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews adb->erefcnt++;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (lock)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews UNLOCK(&adb->lock);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews}
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrewsstatic inline void
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrewsdec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock) {
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (lock)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence LOCK(&adb->lock);
a5a30ae7c0636a57d8575acb98fdf906c0ea818fMark Andrews
a5a30ae7c0636a57d8575acb98fdf906c0ea818fMark Andrews INSIST(adb->erefcnt > 0);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews adb->erefcnt--;
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (adb->erefcnt == 0)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews check_exit(adb);
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews if (lock)
7d62ddffbb4d1cc97b8d80b7ee4944554a57523eMark Andrews UNLOCK(&adb->lock);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int bucket;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews bucket = entry->lock_bucket;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (lock)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence LOCK(&adb->entrylocks[bucket]);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff entry->refcnt++;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (lock)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff UNLOCK(&adb->entrylocks[bucket]);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
bfb2a81b65579882a80855c279cedc45aebd62e8Mark Andrewsstatic inline void
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffdec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews int bucket;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews isc_boolean_t destroy_entry;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews bucket = entry->lock_bucket;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (lock)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews LOCK(&adb->entrylocks[bucket]);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(entry->refcnt > 0);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont entry->refcnt--;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont destroy_entry = ISC_FALSE;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (entry->refcnt == 0 &&
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont (adb->entry_sd[bucket] || entry->expires == 0)) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont destroy_entry = ISC_TRUE;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont unlink_entry(adb, entry);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (lock)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont UNLOCK(&adb->entrylocks[bucket]);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (!destroy_entry)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont free_adbentry(adb, &entry);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline dns_adbname_t *
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontnew_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbname_t *name;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name = isc_mempool_get(adb->nmp);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (name == NULL)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_name_init(&name->name, NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_put(adb->nmp, name);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_name_init(&name->target, NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->magic = DNS_ADBNAME_MAGIC;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->adb = adb;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->partial_result = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->flags = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->expire_v4 = INT_MAX;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->expire_v6 = INT_MAX;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->expire_target = INT_MAX;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->chains = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->lock_bucket = DNS_ADB_INVALIDBUCKET;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LIST_INIT(name->v4);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LIST_INIT(name->v6);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->fetch_a = NULL;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont name->fetch_aaaa = NULL;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LIST_INIT(name->fetches_a6);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LIST_INIT(name->finds);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LINK_INIT(name, plink);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (name);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline void
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontfree_adbname(dns_adb_t *adb, dns_adbname_t **name) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbname_t *n;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont n = *name;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont *name = NULL;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(!NAME_HAS_V4(n));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(!NAME_HAS_V6(n));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(!NAME_FETCH(n));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(ISC_LIST_EMPTY(n->finds));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(!ISC_LINK_LINKED(n, plink));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(n->adb == adb);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont n->magic = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_name_free(&n->name, adb->mctx);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_put(adb->nmp, n);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline dns_adbnamehook_t *
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbnamehook_t *nh;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont nh = isc_mempool_get(adb->nhmp);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (nh == NULL)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont nh->magic = DNS_ADBNAMEHOOK_MAGIC;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont nh->entry = entry;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LINK_INIT(nh, plink);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (nh);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline void
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbnamehook_t *nh;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont nh = *namehook;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont *namehook = NULL;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(nh->entry == NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(!ISC_LINK_LINKED(nh, plink));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont nh->magic = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_put(adb->nhmp, nh);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline dns_adbzoneinfo_t *
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontnew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbzoneinfo_t *zi;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zi = isc_mempool_get(adb->zimp);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (zi == NULL)
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_name_init(&zi->zone, NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_put(adb->zimp, zi);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (NULL);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont }
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zi->magic = DNS_ADBZONEINFO_MAGIC;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zi->lame_timer = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont ISC_LINK_INIT(zi, plink);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont return (zi);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont}
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline void
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbzoneinfo_t *zi;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zi = *zoneinfo;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont *zoneinfo = NULL;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont INSIST(!ISC_LINK_LINKED(zi, plink));
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_name_free(&zi->zone, adb->mctx);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont zi->magic = 0;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_mempool_put(adb->zimp, zi);
7e9d637131516486630290d36c4c0db544cb700eMark Andrews}
7e9d637131516486630290d36c4c0db544cb700eMark Andrews
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontstatic inline dns_adbentry_t *
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupontnew_adbentry(dns_adb_t *adb) {
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont dns_adbentry_t *e;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont isc_uint32_t r;
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont e = isc_mempool_get(adb->emp);
9fe8cca06537c45375c1e1d36b82501caf0ae090Francis Dupont if (e == NULL)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->magic = DNS_ADBENTRY_MAGIC;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->lock_bucket = DNS_ADB_INVALIDBUCKET;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->refcnt = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->flags = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->edns_level = -1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->goodness = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_random_get(&adb->rand, &r);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->srtt = (r & 0x1f) + 1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->expires = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->avoid_bitstring = 0;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LIST_INIT(e->zoneinfo);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LINK_INIT(e, plink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (e);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline void
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrewsfree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews dns_adbentry_t *e;
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews dns_adbzoneinfo_t *zi;
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews
38b84a1fcfdbda4d1d0fdca409004ae83be2ace8Mark Andrews INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e = *entry;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *entry = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(e->refcnt == 0);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(!ISC_LINK_LINKED(e, plink));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews e->magic = 0;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence zi = ISC_LIST_HEAD(e->zoneinfo);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews while (zi != NULL) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews free_adbzoneinfo(adb, &zi);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence zi = ISC_LIST_HEAD(e->zoneinfo);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mempool_put(adb->emp, e);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline dns_adbfind_t *
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsnew_adbfind(dns_adb_t *adb) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_adbfind_t *h;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington h = isc_mempool_get(adb->ahmp);
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington if (h == NULL)
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington return (NULL);
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington /*
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington * Public members.
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington */
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington h->magic = 0;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington h->adb = adb;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington h->partial_result = 0;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington h->options = 0;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington h->flags = 0;
330705066b03f6ce0bc08a4bbfc5d2418038c68dBrian Wellington ISC_LINK_INIT(h, publink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LINK_INIT(h, plink);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews ISC_LIST_INIT(h->list);
26b0f58b6c4d65bc8b131debf40b8c376c2978bfBob Halley h->adbname = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews h->name_bucket = DNS_ADB_INVALIDBUCKET;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * private members
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = isc_mutex_init(&h->lock);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews if (result != ISC_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews "isc_mutex_init failed in new_adbfind()");
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_mempool_put(adb->ahmp, h);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews return (NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews NULL, NULL, h);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews h->magic = DNS_ADBFIND_MAGIC;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (h);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline dns_adbfetch_t *
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrewsnew_adbfetch(dns_adb_t *adb) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_adbfetch_t *f;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
1c3191528684f3dd93ebb122298c2f8ebfc6d397Mark Andrews f = isc_mempool_get(adb->afmp);
34b394b43e2207e8f8f3703f0402422121455638David Lawrence if (f == NULL)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff f->magic = 0;
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence f->namehook = NULL;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff f->entry = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews f->fetch = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff f->namehook = new_adbnamehook(adb, NULL);
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson if (f->namehook == NULL)
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson goto err;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson f->entry = new_adbentry(adb);
34b394b43e2207e8f8f3703f0402422121455638David Lawrence if (f->entry == NULL)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence goto err;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_rdataset_init(&f->rdataset);
7c0378745269fe49a05904935afc42b85528f53aDavid Lawrence
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff f->magic = DNS_ADBFETCH_MAGIC;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson return (f);
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson err:
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson if (f->namehook != NULL)
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson free_adbnamehook(adb, &f->namehook);
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson if (f->entry != NULL)
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson free_adbentry(adb, &f->entry);
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson isc_mempool_put(adb->afmp, f);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews return (NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
2da53322f30495e9bcf30f3776def2da329d3343Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrencestatic inline void
035504dbd8ca5949e8380b860873b3385a4e61e5Mark Andrewsfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_adbfetch_t *f;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews f = *fetch;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *fetch = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews f->magic = 0;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews if (f->namehook != NULL)
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews free_adbnamehook(adb, &f->namehook);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews if (f->entry != NULL)
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews free_adbentry(adb, &f->entry);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews if (dns_rdataset_isassociated(&f->rdataset))
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews dns_rdataset_disassociate(&f->rdataset);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_mempool_put(adb->afmp, f);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
035504dbd8ca5949e8380b860873b3385a4e61e5Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Caller must be holding the name lock.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic isc_result_t
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafssona6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type, isc_stdtime_t now,
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence{
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson dns_adbname_t *name;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson dns_adb_t *adb;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson name = arg;
5a219d878f0bd786e86da2c9b92999260dda3f8dAndreas Gustafsson INSIST(DNS_ADBNAME_VALID(name));
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews adb = name->adb;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews INSIST(DNS_ADB_VALID(adb));
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews return (dns_view_simplefind(adb->view, a6name, type, now,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews DNS_DBFIND_GLUEOK, ISC_FALSE,
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews rdataset, sigrdataset));
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews}
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff/*
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews * Caller must be holding the name lock.
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews */
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrewsstatic void
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencea6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington dns_adbname_t *name;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_adb_t *adb;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews dns_adbfetch6_t *fetch;
7e9d637131516486630290d36c4c0db544cb700eMark Andrews isc_result_t result;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff name = a6ctx->arg;
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews INSIST(DNS_ADBNAME_VALID(name));
44a966dff66061ac3f266c6b451a70733eb78e82Mark Andrews adb = name->adb;
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews INSIST(DNS_ADB_VALID(adb));
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews fetch = new_adbfetch6(adb, name, a6ctx);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence if (fetch == NULL) {
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence name->partial_result |= DNS_ADBFIND_INET6;
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence return;
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence }
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews result = dns_resolver_createfetch(adb->view->resolver, a6name,
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence dns_rdatatype_a6,
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence NULL, NULL, NULL, 0,
cbef026164ceabccb2e85403434b722d77f7b5eeMark Andrews adb->task, fetch_callback_a6,
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews name, &fetch->rdataset, NULL,
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews &fetch->fetch);
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews if (result != ISC_R_SUCCESS)
ffe74cc719aa0f10c38fbc1f2f3ea7db0960cb8fMark Andrews goto cleanup;
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews name->chains = a6ctx->chains;
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence cleanup:
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence if (result != ISC_R_SUCCESS) {
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence free_adbfetch6(adb, &fetch);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence name->partial_result |= DNS_ADBFIND_INET6;
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence }
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews}
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrewsstatic inline dns_adbfetch6_t *
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrewsnew_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx) {
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews dns_adbfetch6_t *f;
8a17d1e7cdba9fdcf71fb2f821a954a251204105Mark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f = isc_mempool_get(adb->af6mp);
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence if (f == NULL)
47b26abe77184f9bedc68e36bdad03332cf67570David Lawrence return (NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->magic = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->namehook = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->entry = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->fetch = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->flags = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->namehook = new_adbnamehook(adb, NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (f->namehook == NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews goto err;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->entry = new_adbentry(adb);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (f->entry == NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews goto err;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_rdataset_init(&f->rdataset);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews a6missing, name);
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews if (a6ctx != NULL)
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews dns_a6_copy(a6ctx, &f->a6ctx);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->magic = DNS_ADBFETCH6_MAGIC;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (f);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews err:
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (f->namehook != NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews free_adbnamehook(adb, &f->namehook);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (f->entry != NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews free_adbentry(adb, &f->entry);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_mempool_put(adb->af6mp, f);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (NULL);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff}
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic inline void
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencefree_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_adbfetch6_t *f;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff f = *fetch;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *fetch = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews f->magic = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (f->namehook != NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews free_adbnamehook(adb, &f->namehook);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (f->entry != NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews free_adbentry(adb, &f->entry);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (dns_rdataset_isassociated(&f->rdataset))
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_rdataset_disassociate(&f->rdataset);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_mempool_put(adb->af6mp, f);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic inline void
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsfree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbfind_t *find;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews find = *findp;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *findp = NULL;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(!FIND_HAS_ADDRS(find));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(!ISC_LINK_LINKED(find, publink));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(!ISC_LINK_LINKED(find, plink));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews INSIST(find->adbname == NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews find->magic = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mutex_destroy(&find->lock);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_mempool_put(adb->ahmp, find);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence/*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * Copy bits from the entry into the newly allocated addrinfo. The entry
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff * must be locked, and the reference count must be bumped up by one
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * if this function returns a valid pointer.
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic inline dns_adbaddrinfo_t *
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsnew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbaddrinfo_t *ai;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews ai = isc_mempool_get(adb->aimp);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (ai == NULL)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews ai->magic = DNS_ADBADDRINFO_MAGIC;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews ai->sockaddr = entry->sockaddr;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_sockaddr_setport(&ai->sockaddr, port);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ai->goodness = entry->goodness;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews ai->srtt = entry->srtt;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ai->flags = entry->flags;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews ai->avoid_bitstring = entry->avoid_bitstring;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews ai->entry = entry;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ISC_LINK_INIT(ai, publink);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (ai);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence}
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencestatic inline void
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencefree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence dns_adbaddrinfo_t *ai;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ai = *ainfo;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *ainfo = NULL;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence INSIST(ai->entry == NULL);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence INSIST(!ISC_LINK_LINKED(ai, publink));
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff ai->magic = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
df8c9ee4819c97089664ccc035eb2aa7569034fdDavid Lawrence isc_mempool_put(adb->aimp, ai);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews/*
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * Search for the name. NOTE: The bucket is kept locked on both
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * success and failure, so it must always be unlocked by the caller!
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * On the first call to this function, *bucketp must be set to
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington * DNS_ADB_INVALIDBUCKET.
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic inline dns_adbname_t *
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsfind_name_and_lock(dns_adb_t *adb, dns_name_t *name,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews unsigned int options, int *bucketp)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews{
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbname_t *adbname;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews int bucket;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (*bucketp == DNS_ADB_INVALIDBUCKET) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence LOCK(&adb->namelocks[bucket]);
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington *bucketp = bucket;
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington } else if (*bucketp != bucket) {
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews UNLOCK(&adb->namelocks[*bucketp]);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK(&adb->namelocks[bucket]);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *bucketp = bucket;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence }
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence adbname = ISC_LIST_HEAD(adb->names[bucket]);
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington while (adbname != NULL) {
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington if (!NAME_DEAD(adbname)) {
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews if (dns_name_equal(name, &adbname->name)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews && GLUEHINT_OK(adbname, options))
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (adbname);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence adbname = ISC_LIST_NEXT(adbname, plink);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence }
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington return (NULL);
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington}
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews/*
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * Search for the address. NOTE: The bucket is kept locked on both
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * success and failure, so it must always be unlocked by the caller.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * On the first call to this function, *bucketp must be set to
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington * later calls (within the same "lock path") it can be left alone, so
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington * if this function is called multiple times locking is only done if
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews * the bucket changes.
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic inline dns_adbentry_t *
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbentry_t *entry;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews int bucket;
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (*bucketp == DNS_ADB_INVALIDBUCKET) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence LOCK(&adb->entrylocks[bucket]);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence *bucketp = bucket;
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence } else if (*bucketp != bucket) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff UNLOCK(&adb->entrylocks[*bucketp]);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK(&adb->entrylocks[bucket]);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews *bucketp = bucket;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews entry = ISC_LIST_HEAD(adb->entries[bucket]);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews while (entry != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (isc_sockaddr_equal(addr, &entry->sockaddr))
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (entry);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews entry = ISC_LIST_NEXT(entry, plink);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (NULL);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews/*
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * Entry bucket MUST be locked!
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic isc_boolean_t
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_stdtime_t now)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff{
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbzoneinfo_t *zi, *next_zi;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews isc_boolean_t is_bad;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews is_bad = ISC_FALSE;
15330e4fa27c82ac04cc2ce234ec930e4b6b42d3Mark Andrews
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zi = ISC_LIST_HEAD(entry->zoneinfo);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (zi == NULL)
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence return (ISC_FALSE);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence while (zi != NULL) {
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence next_zi = ISC_LIST_NEXT(zi, plink);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater /*
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * Has the entry expired?
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater if (zi->lame_timer < now) {
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews free_adbzoneinfo(adb, &zi);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater /*
83ac7ce833930a5c6cb92ad9c04a58e775579e73Bob Halley * Order tests from least to most expensive.
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews */
83ac7ce833930a5c6cb92ad9c04a58e775579e73Bob Halley if (zi != NULL && !is_bad) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (dns_name_equal(zone, &zi->zone))
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews is_bad = ISC_TRUE;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews zi = next_zi;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews return (is_bad);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews}
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewsstatic void
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrewscopy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbname_t *name, isc_stdtime_t now)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff{
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbnamehook_t *namehook;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbaddrinfo_t *addrinfo;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews dns_adbentry_t *entry;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews int bucket;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews bucket = DNS_ADB_INVALIDBUCKET;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (find->options & DNS_ADBFIND_INET) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews namehook = ISC_LIST_HEAD(name->v4);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews while (namehook != NULL) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews entry = namehook->entry;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson bucket = entry->lock_bucket;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews LOCK(&adb->entrylocks[bucket]);
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews /*
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews * Check for avoid bitstring timeout.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (entry->avoid_bitstring > 0
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews && entry->avoid_bitstring < now)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence entry->avoid_bitstring = 0;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews if (!FIND_RETURNLAME(find)
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews && entry_is_bad_for_zone(adb, entry, zone, now)) {
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews find->options |= DNS_ADBFIND_LAMEPRUNED;
806c235ecf533b98d068b3f8df9d7abbe1e30cf9Mark Andrews goto nextv4;
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews }
3a4ec3da9fa14511cbc3660f75817cfacb3f4d1eMark Andrews addrinfo = new_adbaddrinfo(adb, entry, find->port);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (addrinfo == NULL) {
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews find->partial_result |= DNS_ADBFIND_INET;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews goto out;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence }
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence /*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Found a valid entry. Add it to the find's list.
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews inc_entry_refcnt(adb, entry, ISC_FALSE);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence ISC_LIST_APPEND(find->list, addrinfo, publink);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews addrinfo = NULL;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews nextv4:
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews UNLOCK(&adb->entrylocks[bucket]);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews bucket = DNS_ADB_INVALIDBUCKET;
861cef13977886aca57ba95c2c93039b87bbbc43Brian Wellington namehook = ISC_LIST_NEXT(namehook, plink);
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews }
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews }
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews
fd4810861c0c0ccb9aebde94e9d289442b2630dbMark Andrews if (find->options & DNS_ADBFIND_INET6) {
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews namehook = ISC_LIST_HEAD(name->v6);
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews while (namehook != NULL) {
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews entry = namehook->entry;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews bucket = entry->lock_bucket;
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews LOCK(&adb->entrylocks[bucket]);
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews
23cb957a81a51a9656917ea98d0ae56b7abdcaccMark Andrews /*
b6a0341bcb113e93bd0bc41a9f9a1fc117444da6Mark Andrews * Check for avoid bitstring timeout.
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (entry->avoid_bitstring > 0
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews && entry->avoid_bitstring < now)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews entry->avoid_bitstring = 0;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (entry_is_bad_for_zone(adb, entry, zone, now))
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews goto nextv6;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews addrinfo = new_adbaddrinfo(adb, entry, find->port);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (addrinfo == NULL) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews find->partial_result |= DNS_ADBFIND_INET6;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews goto out;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews }
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater /*
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews * Found a valid entry. Add it to the find's list.
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews */
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews inc_entry_refcnt(adb, entry, ISC_FALSE);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews ISC_LIST_APPEND(find->list, addrinfo, publink);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews addrinfo = NULL;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews nextv6:
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNLOCK(&adb->entrylocks[bucket]);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews bucket = DNS_ADB_INVALIDBUCKET;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews namehook = ISC_LIST_NEXT(namehook, plink);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater out:
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (bucket != DNS_ADB_INVALIDBUCKET)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNLOCK(&adb->entrylocks[bucket]);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updaterstatic void
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsshutdown_task(isc_task_t *task, isc_event_t *ev) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_adb_t *adb;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNUSED(task);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews adb = ev->ev_arg;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence INSIST(DNS_ADB_VALID(adb));
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence /*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * Kill the timer, and then the ADB itself. Note that this implies
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * that this task was the one scheduled to get timer events. If
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * this is not true (and it is unfortunate there is no way to INSIST()
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * this) badness will occur.
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews LOCK(&adb->lock);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews isc_timer_detach(&adb->timer);
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence UNLOCK(&adb->lock);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews isc_event_free(&ev);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews destroy(adb);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews}
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence/*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * name bucket must be locked; adb may be locked; no other locks held.
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewsstatic void
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrewscheck_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence dns_adbname_t *name;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews name = *namep;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return;
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence if (NAME_FETCH(name))
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return;
c46f10e4a1702191b003cf8f8fc5059c15d29c48Mark Andrews if (!EXPIRE_OK(name->expire_v4, now))
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews if (!EXPIRE_OK(name->expire_v6, now))
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return;
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence if (!EXPIRE_OK(name->expire_target, now))
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews return;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews /*
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews * The name is empty. Delete it.
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews */
20b20b23948b90cb2f7d7f402da99d09f837efd0David Lawrence kill_name(&name, DNS_EVENT_ADBEXPIRED);
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews *namep = NULL;
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews
82d05588933a3c765aa8518fe455d6477d640b99Mark Andrews /*
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson * Our caller, or one of its callers, will be calling check_exit() at
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley * some point, so we don't need to do it here.
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence */
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews}
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley/*
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley * entry bucket must be locked; adb may be locked; no other locks held.
0513f89e68f82f9ec54e7af9c979a7c43babbe31Bob Halley */
ebd68da027cfa8da0fb536c3db11bb88292f41c7Andreas Gustafssonstatic void
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrencecheck_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff{
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff dns_adbentry_t *entry;
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
ebd68da027cfa8da0fb536c3db11bb88292f41c7Andreas Gustafsson entry = *entryp;
ebd68da027cfa8da0fb536c3db11bb88292f41c7Andreas Gustafsson
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence if (entry->refcnt != 0)
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff return;
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff if (entry->expires == 0 || entry->expires > now)
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff return;
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff /*
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff * The entry is not in use. Delete it.
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff */
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence DP(DEF_LEVEL, "killing entry %p", entry);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff INSIST(ISC_LINK_LINKED(entry, plink));
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff unlink_entry(adb, entry);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff free_adbentry(adb, &entry);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff *entryp = NULL;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff}
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff/*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * ADB must be locked, and no other locks held.
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff */
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graffstatic void
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graffcleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff dns_adbname_t *name;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff dns_adbname_t *next_name;
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews LOCK(&adb->namelocks[bucket]);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews if (adb->name_sd[bucket]) {
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews UNLOCK(&adb->namelocks[bucket]);
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews return;
0b09763c354ec91fb352b6b4cea383bd0195b2d8Mark Andrews }
3bb3b7ac462a90c2b8b1fb783324d800e2ba748cMichael Graff
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence name = ISC_LIST_HEAD(adb->names[bucket]);
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff while (name != NULL) {
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff next_name = ISC_LIST_NEXT(name, plink);
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff check_expire_namehooks(name, now);
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff check_expire_name(&name, now);
fa460c223a69449eaac67ddb6abafe74f5e1ff02Michael Graff name = next_name;
7ec579cd5d07228c0d6cece58b80694ad8d59de9Michael Graff }
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence UNLOCK(&adb->namelocks[bucket]);
f8aae502686e2448c48f56697c212a50e2a1cbaeAndreas Gustafsson}
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson/*
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence * ADB must be locked, and no other locks held.
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff */
6324997211a5e2d82528dcde98e8981190a35faeMichael Graffstatic void
6324997211a5e2d82528dcde98e8981190a35faeMichael Graffcleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson dns_adbentry_t *entry, *next_entry;
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson int freq;
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff freq = NBUCKETS / CLEAN_SECONDS;
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff REQUIRE(freq > 0);
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff LOCK(&adb->entrylocks[bucket]);
b469f0321d2bcea3914c57d26fd43319e506c313Andreas Gustafsson entry = ISC_LIST_HEAD(adb->entries[bucket]);
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson while (entry != NULL) {
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson next_entry = ISC_LIST_NEXT(entry, plink);
8abddcd3f24476b945419659e7cb73bcb970886bDavid Lawrence check_expire_entry(adb, &entry, now);
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson if (entry != NULL && entry->goodness != 0) {
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson if (entry->goodness > 0
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson && entry->goodness < freq)
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson entry->goodness = 0;
014892d86d30b7eceb0003d51788f9b5cadfc1bfAndreas Gustafsson else if (entry->goodness < 0
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 && entry->goodness > -freq)
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 entry->goodness = 0;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 else if (entry->goodness > 0)
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 entry->goodness -= freq;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 else if (entry->goodness < 0)
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 entry->goodness += freq;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 }
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 entry = next_entry;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 }
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 UNLOCK(&adb->entrylocks[bucket]);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉static void
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉timer_cleanup(isc_task_t *task, isc_event_t *ev) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 dns_adb_t *adb;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_stdtime_t now;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 unsigned int i;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 UNUSED(task);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 adb = ev->ev_arg;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 INSIST(DNS_ADB_VALID(adb));
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 LOCK(&adb->lock);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_stdtime_get(&now);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 for (i = 0 ; i < CLEAN_BUCKETS ; i++) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Call our cleanup routines.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 cleanup_names(adb, adb->next_cleanbucket, now);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 cleanup_entries(adb, adb->next_cleanbucket, now);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Set the next bucket to be cleaned.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 adb->next_cleanbucket++;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 if (adb->next_cleanbucket >= NBUCKETS) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 adb->next_cleanbucket = 0;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#ifdef DUMP_ADB_AFTER_CLEANING
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 dump_adb(adb, stdout);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉#endif
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 }
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 }
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * Reset the timer.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * ISC_R_NOMEMORY, but it isn't clear what could be done here
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * if either one of those things happened.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 &adb->tick_interval, ISC_FALSE);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 UNLOCK(&adb->lock);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_event_free(&ev);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉static void
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉destroy(dns_adb_t *adb) {
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 adb->magic = 0;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 /*
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 * The timer is already dead, from the task's shutdown callback.
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 */
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_task_detach(&adb->task);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->nmp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->nhmp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->zimp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->emp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->ahmp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->aimp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->afmp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mempool_destroy(&adb->af6mp);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mutex_destroy(&adb->ilock);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mutex_destroy(&adb->lock);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mutex_destroy(&adb->mplock);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_random_invalidate(&adb->rand);
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉}
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉
/*
* Public functions.
*/
isc_result_t
dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
{
dns_adb_t *adb;
isc_result_t result;
int i;
REQUIRE(mem != NULL);
REQUIRE(view != NULL);
REQUIRE(timermgr != NULL);
REQUIRE(taskmgr != NULL);
REQUIRE(newadb != NULL && *newadb == NULL);
adb = isc_mem_get(mem, sizeof (dns_adb_t));
if (adb == NULL)
return (ISC_R_NOMEMORY);
/*
* Initialize things here that cannot fail, and especially things
* that must be NULL for the error return to work properly.
*/
adb->magic = 0;
adb->erefcnt = 1;
adb->irefcnt = 0;
adb->nmp = NULL;
adb->nhmp = NULL;
adb->zimp = NULL;
adb->emp = NULL;
adb->ahmp = NULL;
adb->aimp = NULL;
adb->afmp = NULL;
adb->af6mp = NULL;
adb->task = NULL;
adb->timer = NULL;
adb->mctx = mem;
adb->view = view;
adb->timermgr = timermgr;
adb->taskmgr = taskmgr;
adb->next_cleanbucket = 0;
ISC_EVENT_INIT(&adb->cevent, sizeof adb->cevent, 0, NULL,
DNS_EVENT_ADBCONTROL, shutdown_task, adb,
adb, NULL, NULL);
adb->cevent_sent = ISC_FALSE;
adb->shutting_down = ISC_FALSE;
ISC_LIST_INIT(adb->whenshutdown);
result = isc_random_init(&adb->rand);
if (result != ISC_R_SUCCESS)
goto fail0a;
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail0b;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail0c;
result = isc_mutex_init(&adb->ilock);
if (result != ISC_R_SUCCESS)
goto fail0d;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0 ; i < NBUCKETS ; i++) {
ISC_LIST_INIT(adb->names[i]);
adb->name_sd[i] = ISC_FALSE;
adb->name_refcnt[i] = 0;
adb->irefcnt++;
}
for (i = 0 ; i < NBUCKETS ; i++) {
ISC_LIST_INIT(adb->entries[i]);
adb->entry_sd[i] = ISC_FALSE;
adb->entry_refcnt[i] = 0;
adb->irefcnt++;
}
result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, n) do { \
result = isc_mempool_create(mem, sizeof (t), &(p)); \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
isc_mempool_setname((p), n); \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT(dns_adbname_t, adb->nmp, "adbname");
MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
MPINIT(dns_adbentry_t, adb->emp, "adbentry");
MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
MPINIT(dns_adbfetch6_t, adb->af6mp, "adbfetch6");
#undef MPINIT
/*
* Allocate a timer and a task for our periodic cleanup.
*/
result = isc_task_create(adb->taskmgr, 0, &adb->task);
if (result != ISC_R_SUCCESS)
goto fail3;
isc_task_setname(adb->task, "ADB", adb);
/*
* XXXMLG When this is changed to be a config file option,
*/
isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
result = isc_timer_create(adb->timermgr, isc_timertype_once,
NULL, &adb->tick_interval, adb->task,
timer_cleanup, adb, &adb->timer);
if (result != ISC_R_SUCCESS)
goto fail3;
DP(5,
"Cleaning interval for adb: "
"%u buckets every %u seconds, %u buckets in system, %u cl.interval",
CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
/*
* Normal return.
*/
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3:
if (adb->task != NULL)
isc_task_detach(&adb->task);
if (adb->timer != NULL)
isc_timer_detach(&adb->timer);
/* clean up entrylocks */
isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
fail2: /* clean up namelocks */
isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->zimp != NULL)
isc_mempool_destroy(&adb->zimp);
if (adb->emp != NULL)
isc_mempool_destroy(&adb->emp);
if (adb->ahmp != NULL)
isc_mempool_destroy(&adb->ahmp);
if (adb->aimp != NULL)
isc_mempool_destroy(&adb->aimp);
if (adb->afmp != NULL)
isc_mempool_destroy(&adb->afmp);
if (adb->af6mp != NULL)
isc_mempool_destroy(&adb->af6mp);
isc_mutex_destroy(&adb->ilock);
fail0d:
isc_mutex_destroy(&adb->mplock);
fail0c:
isc_mutex_destroy(&adb->lock);
fail0b:
isc_random_invalidate(&adb->rand);
fail0a:
isc_mem_put(mem, adb, sizeof (dns_adb_t));
return (result);
}
void
dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(adbx != NULL && *adbx == NULL);
inc_adb_erefcnt(adb, ISC_TRUE);
*adbx = adb;
}
void
dns_adb_detach(dns_adb_t **adbx) {
dns_adb_t *adb;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
LOCK(&adb->lock);
dec_adb_erefcnt(adb, ISC_FALSE);
if (adb->erefcnt == 0)
INSIST(adb->shutting_down);
UNLOCK(&adb->lock);
}
void
dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
isc_task_t *clone;
isc_event_t *event;
isc_boolean_t zeroirefcnt = ISC_FALSE;
/*
* Send '*eventp' to 'task' when 'adb' has shutdown.
*/
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(eventp != NULL);
event = *eventp;
*eventp = NULL;
LOCK(&adb->lock);
LOCK(&adb->ilock);
if (adb->irefcnt == 0)
zeroirefcnt = ISC_TRUE;
else
zeroirefcnt = ISC_FALSE;
UNLOCK(&adb->ilock);
if (adb->shutting_down && zeroirefcnt &&
isc_mempool_getallocated(adb->ahmp) == 0) {
/*
* We're already shutdown. Send the event.
*/
event->ev_sender = adb;
isc_task_send(task, &event);
} else {
clone = NULL;
isc_task_attach(task, &clone);
event->ev_sender = clone;
ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
}
UNLOCK(&adb->lock);
}
void
dns_adb_shutdown(dns_adb_t *adb) {
/*
* Shutdown 'adb'.
*/
LOCK(&adb->lock);
if (!adb->shutting_down) {
adb->shutting_down = ISC_TRUE;
shutdown_names(adb);
shutdown_entries(adb);
check_exit(adb);
}
UNLOCK(&adb->lock);
}
isc_result_t
dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
void *arg, dns_name_t *name, dns_name_t *zone,
unsigned int options, isc_stdtime_t now, dns_name_t *target,
in_port_t port, dns_adbfind_t **findp)
{
dns_adbfind_t *find;
dns_adbname_t *adbname;
int bucket;
isc_boolean_t want_event, start_at_root, alias;
isc_result_t result;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(name != NULL);
REQUIRE(zone != NULL);
REQUIRE(findp != NULL && *findp == NULL);
REQUIRE(target == NULL || dns_name_hasbuffer(target));
REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
result = ISC_R_UNEXPECTED;
wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
wanted_fetches = 0;
query_pending = 0;
want_event = ISC_FALSE;
start_at_root = ISC_FALSE;
alias = ISC_FALSE;
if (now == 0)
isc_stdtime_get(&now);
/*
* XXXMLG Move this comment somewhere else!
*
* Look up the name in our internal database.
*
* Possibilities: Note that these are not always exclusive.
*
* No name found. In this case, allocate a new name header and
* an initial namehook or two. If any of these allocations
* fail, clean up and return ISC_R_NOMEMORY.
*
* Name found, valid addresses present. Allocate one addrinfo
* structure for each found and append it to the linked list
* of addresses for this header.
*
* Name found, queries pending. In this case, if a task was
* passed in, allocate a job id, attach it to the name's job
* list and remember to tell the caller that there will be
* more info coming later.
*/
find = new_adbfind(adb);
if (find == NULL)
return (ISC_R_NOMEMORY);
find->port = port;
/*
* Remember what types of addresses we are interested in.
*/
find->options = options;
find->flags |= wanted_addresses;
if (FIND_WANTEVENT(find)) {
REQUIRE(task != NULL);
}
/*
* Try to see if we know anything about this name at all.
*/
bucket = DNS_ADB_INVALIDBUCKET;
adbname = find_name_and_lock(adb, name, find->options, &bucket);
if (adb->name_sd[bucket]) {
DP(DEF_LEVEL,
"dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
free_adbfind(adb, &find);
result = ISC_R_SHUTTINGDOWN;
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name.
*/
if (adbname == NULL) {
adbname = new_adbname(adb, name);
if (adbname == NULL) {
free_adbfind(adb, &find);
result = ISC_R_NOMEMORY;
goto out;
}
link_name(adb, bucket, adbname);
if (FIND_HINTOK(find))
adbname->flags |= NAME_HINT_OK;
if (FIND_GLUEOK(find))
adbname->flags |= NAME_GLUE_OK;
}
/*
* Expire old entries, etc.
*/
check_expire_namehooks(adbname, now);
/*
* Do we know that the name is an alias?
*/
if (!EXPIRE_OK(adbname->expire_target, now)) {
/*
* Yes, it is.
*/
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias (cached)",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Try to populate the name from the database and/or
* start fetches.
*/
if (!NAME_HAS_V4(adbname) && !NAME_FETCH_V4(adbname)
&& EXPIRE_OK(adbname->expire_v4, now)
&& WANT_INET(wanted_addresses)) {
result = dbfind_name(adbname, now, dns_rdatatype_a);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: Found A for name %p in db",
adbname);
goto v6;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Listen to negative cache hints, and don't start
* another query.
*/
if (NCACHE_RESULT(result) || AUTH_NX(result))
goto v6;
wanted_fetches |= DNS_ADBFIND_INET;
}
v6:
if (!NAME_HAS_V6(adbname) && !NAME_FETCH_V6(adbname)
&& EXPIRE_OK(adbname->expire_v6, now)
&& WANT_INET6(wanted_addresses)) {
result = dbfind_a6(adbname, now);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: Found A6 for name %p",
adbname);
goto fetch;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Listen to negative cache hints, and don't start
* another query.
*/
if (NCACHE_RESULT(result) || AUTH_NX(result))
goto fetch;
wanted_fetches |= DNS_ADBFIND_INET6;
}
fetch:
if (wanted_fetches != 0 &&
(!FIND_AVOIDFETCHES(find) || wanted_addresses == wanted_fetches)) {
/*
* We're missing at least one address family. Either the
* caller hasn't instructed us to avoid fetches, or we don't
* know anything about any of the address families that would
* be acceptable so we have to launch fetches.
*/
if (FIND_STARTATROOT(find))
start_at_root = ISC_TRUE;
/*
* Start V4.
*/
if (WANT_INET(wanted_fetches) &&
fetch_name_v4(adbname, start_at_root) == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: Started A fetch for name %p",
adbname);
}
/*
* Start V6.
*/
if (WANT_INET6(wanted_fetches) &&
fetch_name_a6(adbname, start_at_root) == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: Started A6 fetch for name %p",
adbname);
}
}
/*
* Run through the name and copy out the bits we are
* interested in.
*/
copy_namehook_lists(adb, find, zone, adbname, now);
post_copy:
if (NAME_FETCH_V4(adbname))
query_pending |= DNS_ADBFIND_INET;
if (NAME_FETCH_V6(adbname))
query_pending |= DNS_ADBFIND_INET6;
/*
* Attach to the name's query list if there are queries
* already running, and we have been asked to.
*/
want_event = ISC_TRUE;
if (!FIND_WANTEVENT(find))
want_event = ISC_FALSE;
if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
want_event = ISC_FALSE;
if ((wanted_addresses & query_pending) == 0)
want_event = ISC_FALSE;
if (alias)
want_event = ISC_FALSE;
if (want_event) {
find->adbname = adbname;
find->name_bucket = bucket;
ISC_LIST_APPEND(adbname->finds, find, plink);
find->query_pending = (query_pending & wanted_addresses);
find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
find, adbname);
} else {
/*
* Remove the flag so the caller knows there will never
* be an event, and set internal flags to fake that
* the event was sent and freed, so dns_adb_destroyfind() will
* do the right thing.
*/
find->query_pending = (query_pending & wanted_addresses);
find->options &= ~DNS_ADBFIND_WANTEVENT;
find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
}
find->partial_result |= (adbname->partial_result & wanted_addresses);
if (alias) {
if (target != NULL) {
result = dns_name_concatenate(&adbname->target, NULL,
target, NULL);
if (result != ISC_R_SUCCESS)
goto out;
}
result = DNS_R_ALIAS;
} else
result = ISC_R_SUCCESS;
out:
if (find != NULL) {
*findp = find;
if (want_event) {
isc_task_t *taskp;
INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
taskp = NULL;
isc_task_attach(task, &taskp);
find->event.ev_sender = taskp;
find->event.ev_action = action;
find->event.ev_arg = arg;
}
}
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
void
dns_adb_destroyfind(dns_adbfind_t **findp) {
dns_adbfind_t *find;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *ai;
int bucket;
dns_adb_t *adb;
REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
find = *findp;
*findp = NULL;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(FIND_EVENTFREED(find));
bucket = find->name_bucket;
INSIST(bucket == DNS_ADB_INVALIDBUCKET);
UNLOCK(&find->lock);
/*
* The find doesn't exist on any list, and nothing is locked.
* Return the find to the memory pool, and decrement the adb's
* reference count.
*/
ai = ISC_LIST_HEAD(find->list);
while (ai != NULL) {
ISC_LIST_UNLINK(find->list, ai, publink);
entry = ai->entry;
ai->entry = NULL;
INSIST(DNS_ADBENTRY_VALID(entry));
dec_entry_refcnt(adb, entry, ISC_TRUE);
free_adbaddrinfo(adb, &ai);
ai = ISC_LIST_HEAD(find->list);
}
/*
* WARNING: The find is freed with the adb locked. This is done
* to avoid a race condition where we free the find, some other
* thread tests to see if it should be destroyed, detects it should
* be, destroys it, and then we try to lock it for our check, but the
* lock is destroyed.
*/
LOCK(&adb->lock);
free_adbfind(adb, &find);
check_exit(adb);
UNLOCK(&adb->lock);
}
void
dns_adb_cancelfind(dns_adbfind_t *find) {
isc_event_t *ev;
isc_task_t *task;
dns_adb_t *adb;
int bucket;
int unlock_bucket;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(!FIND_EVENTFREED(find));
REQUIRE(FIND_WANTEVENT(find));
bucket = find->name_bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the find.
*/
unlock_bucket = bucket;
violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
bucket = find->name_bucket;
if (bucket != DNS_ADB_INVALIDBUCKET) {
ISC_LIST_UNLINK(find->adbname->finds, find, plink);
find->adbname = NULL;
find->name_bucket = DNS_ADB_INVALIDBUCKET;
}
UNLOCK(&adb->namelocks[unlock_bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
cleanup:
if (!FIND_EVENTSENT(find)) {
ev = &find->event;
task = ev->ev_sender;
ev->ev_sender = find;
ev->ev_type = DNS_EVENT_ADBCANCELED;
ev->ev_destroy = event_free;
ev->ev_destroy_arg = find;
DP(DEF_LEVEL, "Sending event %p to task %p for find %p",
ev, task, find);
isc_task_sendanddetach(&task, &ev);
}
UNLOCK(&find->lock);
}
void
dns_adb_dump(dns_adb_t *adb, FILE *f) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(f != NULL);
/*
* Lock the adb itself, lock all the name buckets, then lock all
* the entry buckets. This should put the adb into a state where
* nothing can change, so we can iterate through everything and
* print at our leasure.
*/
LOCK(&adb->lock);
dump_adb(adb, f);
UNLOCK(&adb->lock);
}
static void
dump_adb(dns_adb_t *adb, FILE *f) {
int i;
isc_sockaddr_t *sa;
dns_adbname_t *name;
dns_adbentry_t *entry;
char tmp[512];
const char *tmpp;
isc_stdtime_t now;
isc_stdtime_get(&now);
fprintf(f, "ADB %p DUMP:\n", adb);
fprintf(f, "erefcnt %u, irefcnt %u, finds out %u\n",
adb->erefcnt, adb->irefcnt,
isc_mempool_getallocated(adb->nhmp));
for (i = 0 ; i < NBUCKETS ; i++)
LOCK(&adb->namelocks[i]);
for (i = 0 ; i < NBUCKETS ; i++)
LOCK(&adb->entrylocks[i]);
/*
* Dump the names
*/
fprintf(f, "Names:\n");
for (i = 0 ; i < NBUCKETS ; i++) {
name = ISC_LIST_HEAD(adb->names[i]);
if (name == NULL)
continue;
fprintf(f, "Name bucket %d:\n", i);
while (name != NULL) {
fprintf(f, "name %p (flags %08x)\n",
name, name->flags);
if (!DNS_ADBNAME_VALID(name))
fprintf(f, "\tMAGIC %08x\n", name->magic);
fprintf(f, "\texpiry [");
if (name->expire_v4 == INT_MAX)
fprintf(f, "inf ");
else
fprintf(f, "%d ", name->expire_v4 - now);
if (name->expire_v6 == INT_MAX)
fprintf(f, "inf ");
else
fprintf(f, "%d ", name->expire_v6 - now);
if (name->expire_target == INT_MAX)
fprintf(f, "inf] ");
else
fprintf(f, "%d] ", name->expire_target - now);
print_dns_name(f, &name->name);
if (dns_name_countlabels(&name->target) > 0) {
fprintf(f, "\t\t alias for ");
print_dns_name(f, &name->target);
}
fprintf(f, "\n");
print_namehook_list(f, name);
print_fetch_list(f, name);
print_find_list(f, name);
fprintf(f, "\n");
name = ISC_LIST_NEXT(name, plink);
}
}
/*
* Dump the entries
*/
fprintf(f, "Entries:\n");
for (i = 0 ; i < NBUCKETS ; i++) {
entry = ISC_LIST_HEAD(adb->entries[i]);
if (entry == NULL)
continue;
fprintf(f, "Entry bucket %d:\n", i);
while (entry != NULL) {
if (!DNS_ADBENTRY_VALID(entry))
fprintf(f, "\tMAGIC %08x\n", entry->magic);
if (entry->lock_bucket != i)
fprintf(f, "\tWRONG BUCKET! lock_bucket %d\n",
entry->lock_bucket);
sa = &entry->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET,
&sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6,
&sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "BadAddress";
fprintf(f, "\t%p: refcnt %u flags %08x goodness %d"
" srtt %u addr %s, avoid_bitstring %u\n",
entry, entry->refcnt, entry->flags,
entry->goodness, entry->srtt, tmpp,
entry->avoid_bitstring);
entry = ISC_LIST_NEXT(entry, plink);
}
}
/*
* Unlock everything
*/
for (i = 0 ; i < NBUCKETS ; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0 ; i < NBUCKETS ; i++)
UNLOCK(&adb->namelocks[i]);
}
void
dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
char tmp[512];
const char *tmpp;
dns_adbaddrinfo_t *ai;
isc_sockaddr_t *sa;
/*
* Not used currently, in the API Just In Case we
* want to dump out the name and/or entries too.
*/
LOCK(&find->lock);
fprintf(f, "Find %p\n", find);
fprintf(f, "\tqpending %08x partial %08x options %08x flags %08x\n",
find->query_pending, find->partial_result,
find->options, find->flags);
fprintf(f, "\tname_bucket %d, name %p, event sender %p\n",
find->name_bucket, find->adbname, find->event.ev_sender);
ai = ISC_LIST_HEAD(find->list);
if (ai != NULL)
fprintf(f, "\tAddresses:\n");
while (ai != NULL) {
sa = &ai->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "BadAddress";
fprintf(f, "\t\tentry %p, flags %08x goodness %d"
" srtt %u addr %s avoid_bitstring %u\n",
ai->entry, ai->flags, ai->goodness, ai->srtt, tmpp,
ai->avoid_bitstring);
ai = ISC_LIST_NEXT(ai, publink);
}
UNLOCK(&find->lock);
}
static void
print_dns_name(FILE *f, dns_name_t *name) {
char buf[1024];
INSIST(f != NULL);
dns_name_format(name, buf, sizeof(buf));
fprintf(f, "%s", buf);
}
static void
print_namehook_list(FILE *f, dns_adbname_t *n) {
dns_adbnamehook_t *nh;
nh = ISC_LIST_HEAD(n->v4);
while (nh != NULL) {
fprintf(f, "\t\tHook(V4) %p -> entry %p\n", nh, nh->entry);
nh = ISC_LIST_NEXT(nh, plink);
}
nh = ISC_LIST_HEAD(n->v6);
while (nh != NULL) {
fprintf(f, "\t\tHook(V6) %p -> entry %p\n", nh, nh->entry);
nh = ISC_LIST_NEXT(nh, plink);
}
}
static inline void
print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
type, ft, ft->namehook, ft->entry, ft->fetch);
}
static inline void
print_fetch6(FILE *f, dns_adbfetch6_t *ft) {
fprintf(f, "\t\tFetch(A6): %p -> { nh %p, entry %p, fetch %p }\n",
ft, ft->namehook, ft->entry, ft->fetch);
}
static void
print_fetch_list(FILE *f, dns_adbname_t *n) {
dns_adbfetch6_t *fetch6;
if (NAME_FETCH_A(n))
print_fetch(f, n->fetch_a, "A");
if (NAME_FETCH_AAAA(n))
print_fetch(f, n->fetch_aaaa, "AAAA");
fetch6 = ISC_LIST_HEAD(n->fetches_a6);
while (fetch6 != NULL) {
print_fetch6(f, fetch6);
fetch6 = ISC_LIST_NEXT(fetch6, plink);
}
}
static void
print_find_list(FILE *f, dns_adbname_t *name) {
dns_adbfind_t *find;
find = ISC_LIST_HEAD(name->finds);
while (find != NULL) {
dns_adb_dumpfind(find, f);
find = ISC_LIST_NEXT(find, plink);
}
}
static isc_result_t
dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
{
isc_result_t result;
dns_rdataset_t rdataset;
dns_adb_t *adb;
dns_fixedname_t foundname;
dns_name_t *fname;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
dns_rdataset_init(&rdataset);
result = dns_view_find(adb->view, &adbname->name, rdtype, now,
NAME_GLUEOK(adbname),
ISC_TF(NAME_HINTOK(adbname)),
fname, &rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
/*
* Found in the database. Even if we can't copy out
* any information, return success, or else a fetch
* will be made, which will only make things worse.
*/
result = import_rdataset(adbname, &rdataset, now);
break;
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
/*
* We're authoritative and the data doesn't exist.
* Make up a negative cache entry so we don't ask again
* for a while.
*
* XXXRTH What time should we use? I'm putting in 30 seconds
* for now.
*/
if (rdtype == dns_rdatatype_a) {
adbname->expire_v4 = now + 30;
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for A",
adbname);
} else {
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for AAAA",
adbname);
adbname->expire_v6 = now + 30;
}
break;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
/*
* We found a negative cache entry. Pull the TTL from it
* so we won't ask again for a while.
*/
rdataset.ttl = ttlclamp(rdataset.ttl);
if (rdtype == dns_rdatatype_a) {
adbname->expire_v4 = rdataset.ttl + now;
DP(NCACHE_LEVEL,
"adb name %p: Caching negative entry for A (ttl %u)",
adbname, rdataset.ttl);
} else {
DP(NCACHE_LEVEL,
"adb name %p: Caching negative entry for AAAA (ttl %u)",
adbname, rdataset.ttl);
adbname->expire_v6 = rdataset.ttl + now;
}
break;
case DNS_R_CNAME:
case DNS_R_DNAME:
/*
* Clear the hint and glue flags, so this will match
* more often.
*/
adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
rdataset.ttl = ttlclamp(rdataset.ttl);
clean_target(adb, &adbname->target);
adbname->expire_target = INT_MAX;
result = set_target(adb, &adbname->name, fname, &rdataset,
&adbname->target);
if (result == ISC_R_SUCCESS) {
result = DNS_R_ALIAS;
DP(NCACHE_LEVEL,
"adb name %p: caching alias target",
adbname);
adbname->expire_target = rdataset.ttl + now;
}
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static isc_result_t
dbfind_a6(dns_adbname_t *adbname, isc_stdtime_t now) {
isc_result_t result;
dns_rdataset_t rdataset;
dns_adb_t *adb;
dns_a6context_t a6ctx;
dns_fixedname_t foundname;
dns_name_t *fname;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V6(adbname));
result = ISC_R_UNEXPECTED;
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
dns_rdataset_init(&rdataset);
result = dns_view_find(adb->view, &adbname->name, dns_rdatatype_a6,
now, NAME_GLUEOK(adbname),
ISC_TF(NAME_HINTOK(adbname)),
fname, &rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
/*
* Start a6 chain follower. There is no need to poke people
* who might be waiting, since this is call requires there
* are none.
*/
dns_a6_init(&a6ctx, a6find, NULL, import_a6,
a6missing, adbname);
(void)dns_a6_foreach(&a6ctx, &rdataset, now);
result = ISC_R_SUCCESS;
break;
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
/*
* We're authoritative and the data doesn't exist.
* Make up a negative cache entry so we don't ask again
* for a while.
*
* XXXRTH What time should we use? I'm putting in 30 seconds
* for now.
*/
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for AAAA",
adbname);
adbname->expire_v6 = now + 30;
break;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
/*
* We found a negative cache entry. Pull the TTL from it
* so we won't ask again for a while.
*/
DP(NCACHE_LEVEL,
"adb name %p: Caching negative entry for A6 (ttl %u)",
adbname, rdataset.ttl);
adbname->expire_v6 = ISC_MIN(rdataset.ttl + now,
adbname->expire_v6);
break;
case DNS_R_CNAME:
case DNS_R_DNAME:
rdataset.ttl = ttlclamp(rdataset.ttl);
clean_target(adb, &adbname->target);
adbname->expire_target = INT_MAX;
result = set_target(adb, &adbname->name, fname, &rdataset,
&adbname->target);
if (result == ISC_R_SUCCESS) {
result = DNS_R_ALIAS;
DP(NCACHE_LEVEL,
"adb name %p: caching alias target",
adbname);
adbname->expire_target = rdataset.ttl + now;
}
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static void
fetch_callback(isc_task_t *task, isc_event_t *ev) {
dns_fetchevent_t *dev;
dns_adbname_t *name;
dns_adb_t *adb;
dns_adbfetch_t *fetch;
int bucket;
isc_eventtype_t ev_status;
isc_stdtime_t now;
isc_result_t result;
unsigned int address_type;
isc_boolean_t want_check_exit = ISC_FALSE;
(void)task;
INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->ev_arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
address_type = 0;
if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
address_type = DNS_ADBFIND_INET;
fetch = name->fetch_a;
name->fetch_a = NULL;
} else if (NAME_FETCH_AAAA(name)
&& (name->fetch_aaaa->fetch == dev->fetch)) {
address_type = DNS_ADBFIND_INET6;
fetch = name->fetch_aaaa;
name->fetch_aaaa = NULL;
}
INSIST(address_type != 0);
dns_resolver_destroyfetch(&fetch->fetch);
dev->fetch = NULL;
ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
/*
* Cleanup things we don't care about.
*/
if (dev->node != NULL)
dns_db_detachnode(dev->db, &dev->node);
if (dev->db != NULL)
dns_db_detach(&dev->db);
/*
* If this name is marked as dead, clean up, throwing away
* potentially good data.
*/
if (NAME_DEAD(name)) {
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
/*
* If we're shutting down and this bucket is empty, we need
* to call check_exit() to see if we're done.
*/
if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
want_check_exit = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If we got a negative cache response, remember it.
*/
if (NCACHE_RESULT(dev->result)) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
if (address_type == DNS_ADBFIND_INET) {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"Caching negative entry for A (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v4 = ISC_MIN(name->expire_v4,
dev->rdataset->ttl + now);
} else {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"Caching negative entry for AAAA (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v6 = ISC_MIN(name->expire_v6,
dev->rdataset->ttl + now);
}
goto out;
}
/*
* Handle CNAME/DNAME.
*/
if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
clean_target(adb, &name->target);
name->expire_target = INT_MAX;
result = set_target(adb, &name->name,
dns_fixedname_name(&dev->foundname),
dev->rdataset,
&name->target);
if (result == ISC_R_SUCCESS) {
DP(NCACHE_LEVEL,
"adb fetch name %p: caching alias target",
name);
name->expire_target = dev->rdataset->ttl + now;
}
goto check_result;
}
/*
* Did we get back junk? If so, and there are no more fetches
* sitting out there, tell all the finds about it.
*/
if (dev->result != ISC_R_SUCCESS) {
/* XXXMLG Don't pound on bad servers. */
if (address_type == DNS_ADBFIND_INET)
name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
else
name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
DP(1, "got junk in fetch for name %p (%s)",
name, isc_result_totext(dev->result));
goto out;
}
/*
* We got something potentially useful.
*/
result = import_rdataset(name, &fetch->rdataset, now);
check_result:
if (result == ISC_R_SUCCESS)
ev_status = DNS_EVENT_ADBMOREADDRESSES;
out:
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
clean_finds_at_name(name, ev_status, address_type);
UNLOCK(&adb->namelocks[bucket]);
}
static void
fetch_callback_a6(isc_task_t *task, isc_event_t *ev) {
dns_fetchevent_t *dev;
dns_adbname_t *name;
dns_adb_t *adb;
dns_adbfetch6_t *fetch;
int bucket;
isc_stdtime_t now;
isc_result_t result;
isc_boolean_t want_check_exit = ISC_FALSE;
(void)task;
INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->ev_arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
INSIST(!NAME_NEEDSPOKE(name));
for (fetch = ISC_LIST_HEAD(name->fetches_a6);
fetch != NULL;
fetch = ISC_LIST_NEXT(fetch, plink))
if (fetch->fetch == dev->fetch)
break;
INSIST(fetch != NULL);
ISC_LIST_UNLINK(name->fetches_a6, fetch, plink);
DP(ENTER_LEVEL, "ENTER: fetch_callback_a6() name %p", name);
dns_resolver_destroyfetch(&fetch->fetch);
dev->fetch = NULL;
/*
* Cleanup things we don't care about.
*/
if (dev->node != NULL)
dns_db_detachnode(dev->db, &dev->node);
if (dev->db != NULL)
dns_db_detach(&dev->db);
/*
* If this name is marked as dead, clean up, throwing away
* potentially good data.
*/
if (NAME_DEAD(name)) {
free_adbfetch6(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
/*
* If we're shutting down and this bucket is empty, we need
* to call check_exit() to see if we're done.
*/
if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
want_check_exit = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If the A6 query didn't succeed, and this is the first query
* in the A6 chain, try AAAA records instead. For later failures,
* don't do this.
*/
if (dev->result != ISC_R_SUCCESS) {
DP(DEF_LEVEL, "name %p: A6 failed, result %u",
name, dev->result);
/*
* If we got a negative cache response, remember it.
*/
if (NCACHE_RESULT(dev->result)) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
DP(NCACHE_LEVEL, "adb fetch name %p: "
"Caching negative entry for A6 (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v6 = ISC_MIN(name->expire_v6,
dev->rdataset->ttl + now);
}
/*
* Handle CNAME/DNAME.
*/
if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
clean_target(adb, &name->target);
name->expire_target = INT_MAX;
result = set_target(adb, &name->name,
dns_fixedname_name(&dev->foundname),
dev->rdataset,
&name->target);
if (result == ISC_R_SUCCESS) {
DP(NCACHE_LEVEL,
"adb A6 fetch name %p: caching alias target",
name);
name->expire_target = dev->rdataset->ttl + now;
if (FETCH_FIRSTA6(fetch)) {
/*
* Make this name 'pokeable', since
* we've learned that this name is an
* alias.
*/
name->flags |= NAME_NEEDS_POKE;
}
}
goto out;
}
if (FETCH_FIRSTA6(fetch) && !NAME_HAS_V6(name)) {
DP(DEF_LEVEL,
"name %p: A6 query failed, starting AAAA", name);
/*
* Since this is the very first fetch, and it
* failed, we know there are no more running.
*/
result = dbfind_name(name, now, dns_rdatatype_aaaa);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"name %p: callback_a6: Found AAAA for",
name);
name->flags |= NAME_NEEDS_POKE;
goto out;
}
/*
* Listen to negative cache hints, and don't start
* another query.
*/
if (NCACHE_RESULT(result) || AUTH_NX(result))
goto out;
/*
* Try to start fetches for AAAA.
*/
result = fetch_name_aaaa(name);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"name %p: callback_a6: Started AAAA fetch",
name);
goto out;
}
}
goto out;
}
/*
* We got something potentially useful. Run the A6 chain
* follower on this A6 rdataset.
*/
fetch->a6ctx.chains = name->chains;
(void)dns_a6_foreach(&fetch->a6ctx, dev->rdataset, now);
out:
free_adbfetch6(adb, &fetch);
isc_event_free(&ev);
if (NAME_NEEDSPOKE(name))
clean_finds_at_name(name, DNS_EVENT_ADBMOREADDRESSES,
DNS_ADBFIND_INET6);
else if (!NAME_FETCH_V6(name))
clean_finds_at_name(name, DNS_EVENT_ADBNOMOREADDRESSES,
DNS_ADBFIND_INET6);
name->flags &= ~NAME_NEEDS_POKE;
UNLOCK(&adb->namelocks[bucket]);
return;
}
static isc_result_t
fetch_name_v4(dns_adbname_t *adbname, isc_boolean_t start_at_root) {
isc_result_t result;
dns_adbfetch_t *fetch;
dns_adb_t *adb;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V4(adbname));
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
if (start_at_root) {
DP(50, "fetch_name_v4: starting at DNS root for name %p",
adbname);
name = dns_rootname;
result = dns_view_simplefind(adb->view, name, dns_rdatatype_ns,
0, 0, ISC_TRUE, &rdataset, NULL);
if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
goto cleanup;
nameservers = &rdataset;
}
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a,
name, nameservers, NULL, 0,
adb->task, fetch_callback,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
adbname->fetch_a = fetch;
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static isc_result_t
fetch_name_aaaa(dns_adbname_t *adbname) {
isc_result_t result;
dns_adbfetch_t *fetch;
dns_adb_t *adb;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_AAAA(adbname));
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_aaaa,
NULL, NULL, NULL, 0,
adb->task, fetch_callback,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
adbname->fetch_aaaa = fetch;
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
return (result);
}
static isc_result_t
fetch_name_a6(dns_adbname_t *adbname, isc_boolean_t start_at_root) {
isc_result_t result;
dns_adbfetch6_t *fetch;
dns_adb_t *adb;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V6(adbname));
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
if (start_at_root) {
DP(50, "fetch_name_a6: starting at DNS root for name %p",
adbname);
name = dns_rootname;
result = dns_view_simplefind(adb->view, name, dns_rdatatype_ns,
0, 0, ISC_TRUE, &rdataset, NULL);
if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
goto cleanup;
nameservers = &rdataset;
}
fetch = new_adbfetch6(adb, adbname, NULL);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
fetch->flags |= FETCH_FIRST_A6;
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a6,
name, nameservers, NULL, 0,
adb->task, fetch_callback_a6,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
ISC_LIST_APPEND(adbname->fetches_a6, fetch, plink);
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch6(adb, &fetch);
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
/* XXXMLG
* Needs to take a find argument and an address info, no zone or adb, since
* these can be extracted from the find itself.
*/
isc_result_t
dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
isc_stdtime_t expire_time)
{
dns_adbzoneinfo_t *zi;
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(zone != NULL);
zi = new_adbzoneinfo(adb, zone);
if (zi == NULL)
return (ISC_R_NOMEMORY);
zi->lame_timer = expire_time;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, plink);
UNLOCK(&adb->entrylocks[bucket]);
return (ISC_R_SUCCESS);
}
void
dns_adb_adjustgoodness(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
int goodness_adjustment)
{
int bucket;
int old_goodness, new_goodness;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
if (goodness_adjustment == 0)
return;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
old_goodness = addr->entry->goodness;
if (goodness_adjustment > 0) {
if (old_goodness > INT_MAX - goodness_adjustment)
new_goodness = INT_MAX;
else
new_goodness = old_goodness + goodness_adjustment;
} else {
if (old_goodness < INT_MIN - goodness_adjustment)
new_goodness = INT_MIN;
else
new_goodness = old_goodness + goodness_adjustment;
}
addr->entry->goodness = new_goodness;
addr->goodness = new_goodness;
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int rtt, unsigned int factor)
{
int bucket;
unsigned int new_srtt;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(factor <= 10);
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
new_srtt = (addr->entry->srtt / 10 * factor)
+ (rtt / 10 * (10 - factor));
addr->entry->srtt = new_srtt;
addr->srtt = new_srtt;
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int bits, unsigned int mask)
{
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
/*
* Note that we do not update the other bits in addr->flags with
* the most recent values from addr->entry->flags.
*/
addr->flags = (addr->flags & ~mask) | (bits & mask);
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_setavoidbitstring(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
isc_stdtime_t when)
{
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
addr->entry->avoid_bitstring = when;
addr->avoid_bitstring = when;
UNLOCK(&adb->entrylocks[bucket]);
}
isc_result_t
dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
{
int bucket;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *addr;
isc_result_t result;
in_port_t port;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL && *addrp == NULL);
if (now == 0)
isc_stdtime_get(&now);
result = ISC_R_SUCCESS;
bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, sa, &bucket);
if (adb->entry_sd[bucket]) {
result = ISC_R_SHUTTINGDOWN;
goto unlock;
}
if (entry == NULL) {
/*
* We don't know anything about this address.
*/
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
entry->sockaddr = *sa;
link_entry(adb, bucket, entry);
DP(50, "findaddrinfo: new entry %p", entry);
} else
DP(50, "findaddrinfo: found entry %p", entry);
/*
* Check for avoid bitstring timeout.
*/
if (entry->avoid_bitstring > 0 && entry->avoid_bitstring < now)
entry->avoid_bitstring = 0;
port = isc_sockaddr_getport(sa);
addr = new_adbaddrinfo(adb, entry, port);
if (addr != NULL) {
inc_entry_refcnt(adb, entry, ISC_FALSE);
*addrp = addr;
}
unlock:
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
void
dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
dns_adbaddrinfo_t *addr;
dns_adbentry_t *entry;
int bucket;
isc_stdtime_t now;
isc_boolean_t want_check_exit = ISC_FALSE;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL);
addr = *addrp;
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
entry = addr->entry;
REQUIRE(DNS_ADBENTRY_VALID(entry));
isc_stdtime_get(&now);
*addrp = NULL;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
entry->expires = now + 1800; /* XXXRTH */
dec_entry_refcnt(adb, entry, ISC_FALSE);
if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
want_check_exit = ISC_TRUE;
UNLOCK(&adb->entrylocks[bucket]);
addr->entry = NULL;
free_adbaddrinfo(adb, &addr);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
}