adb.c revision fe197676cc48d9b81940a0031a340bb5d23e1dcb
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 1999 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark 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
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
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.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Implementation notes
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * --------------------
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence *
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * In finds, if task == NULL, no events will be generated, and no events
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * have been sent. If task != NULL but taskaction == NULL, an event has been
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * posted but not yet freed. If neither are NULL, no event was posted.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
ddfe394c061049bbd71125ad41c3dd3092b2bbfdAndreas Gustafsson
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews/*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * After we have cleaned all buckets, dump the database contents.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews */
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley#define DUMP_ADB_AFTER_CLEANING
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews#include <config.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <limits.h>
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley#include <stdio.h>
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt#include <string.h>
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington#include <isc/assertions.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <isc/condition.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/event.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <isc/log.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <isc/magic.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/mutex.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/mutexblock.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/random.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/timer.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/util.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews#include <dns/a6.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/adb.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/db.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/events.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/fixedname.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/log.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/name.h>
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence#include <dns/rdata.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/rdataset.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <dns/resolver.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <dns/types.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <dns/view.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_MAGIC 0x61644634 /* adF4. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_MAGIC 0x61644636 /* adF6. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The number of buckets needs to be a prime (for good hashing).
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * XXXRTH How many buckets do we need?
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This value must be coordinated with CLEAN_SECONDS (below).
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NBUCKETS 1009 /* how many buckets for names/addrs */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * For type 3 negative cache entries, we will remember that the address is
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * broken for this long. XXXMLG This is also used for actual addresses, too.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The intent is to keep us from constantly asking about A/A6/AAAA records
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * if the zone has extremely low TTLs.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_CACHE_MINIMUM 10 /* seconds */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Clean one bucket every CLEAN_SECONDS.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_SECONDS (300 / NBUCKETS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#if CLEAN_SECONDS < 1
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#undef CLEAN_SECONDS
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_SECONDS 1
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FREE_ITEMS 16 /* free count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FILL_COUNT 8 /* fill count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbnamehook dns_adbnamehook_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleytypedef struct dns_adbfetch dns_adbfetch_t;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleytypedef struct dns_adbfetch6 dns_adbfetch6_t;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystruct dns_adb {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int magic;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mutex_t lock;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mutex_t ilock;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mem_t *mctx;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_view_t *view;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_timermgr_t *timermgr;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_timer_t *timer;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_taskmgr_t *taskmgr;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_task_t *task;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_interval_t tick_interval;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley int next_cleanbucket;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley unsigned int irefcnt;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley unsigned int erefcnt;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mutex_t mplock;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *nmp; /* dns_adbname_t */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *nhmp; /* dns_adbnamehook_t */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *emp; /* dns_adbentry_t */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *ahmp; /* dns_adbfind_t */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_t *afmp; /* dns_adbfetch_t */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mempool_t *af6mp; /* dns_adbfetch6_t */
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley isc_random_t rand;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley /*
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * Bucketized locks and lists for names.
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley *
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * XXXRTH Have a per-bucket structure that contains all of these?
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson dns_adbnamelist_t names[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_mutex_t namelocks[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_boolean_t name_sd[NBUCKETS];
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence unsigned int name_refcnt[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson /*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Bucketized locks for entries.
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater *
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * XXXRTH Have a per-bucket structure that contains all of these?
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson dns_adbentrylist_t entries[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_mutex_t entrylocks[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_boolean_t entry_sd[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson unsigned int entry_refcnt[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_event_t cevent;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t cevent_sent;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_boolean_t shutting_down;
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer isc_eventlist_t whenshutdown;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson};
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson/*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * XXXMLG Document these structures.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafssonstruct dns_adbname {
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson unsigned int magic;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson dns_name_t name;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson dns_adb_t *adb;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson unsigned int partial_result;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson unsigned int flags;
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley int lock_bucket;
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley dns_name_t target;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_stdtime_t expire_target;
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer isc_stdtime_t expire_v4;
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley isc_stdtime_t expire_v6;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews unsigned int chains;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews dns_adbnamehooklist_t v4;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews dns_adbnamehooklist_t v6;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_adbfetch_t *fetch_a;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_adbfetch_t *fetch_aaaa;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews ISC_LIST(dns_adbfetch6_t) fetches_a6;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_adbfindlist_t finds;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LINK(dns_adbname_t) plink;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews};
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrewsstruct dns_adbfetch {
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews unsigned int magic;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_adbnamehook_t *namehook;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_adbentry_t *entry;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_fetch_t *fetch;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_rdataset_t rdataset;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence};
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestruct dns_adbfetch6 {
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int flags;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adbnamehook_t *namehook;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley dns_fetch_t *fetch;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_t rdataset;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington dns_a6context_t a6ctx;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LINK(dns_adbfetch6_t) plink;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley};
da527e4ff6a013364826637963e7ac372e024f33David Lawrence
da527e4ff6a013364826637963e7ac372e024f33David Lawrence/*
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * dns_adbnamehook_t
da527e4ff6a013364826637963e7ac372e024f33David Lawrence *
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * This is a small widget that dangles off a dns_adbname_t. It contains a
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * pointer to the address information about this host, and a link to the next
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * namehook that will contain the next address this host has.
da527e4ff6a013364826637963e7ac372e024f33David Lawrence */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystruct dns_adbnamehook {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int magic;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbentry_t *entry;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LINK(dns_adbnamehook_t) plink;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley};
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * dns_adbzoneinfo_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This is a small widget that holds zone-specific information about an
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * address. Currently limited to lameness, but could just as easily be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * extended to other types of information about zones.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystruct dns_adbzoneinfo {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int magic;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_t zone;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_stdtime_t lame_timer;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley ISC_LINK(dns_adbzoneinfo_t) plink;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley};
ee03a00244edc6c823ea11bed16437865b395d5dAndreas Gustafsson
ce3761f64d3d734cc94605026985898900ecc474Bob Halley/*
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * An address entry. It holds quite a bit of information about addresses,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * including edns state, rtt, and of course the address of the host.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halleystruct dns_adbentry {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int magic;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley int lock_bucket;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int refcnt;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence unsigned int flags;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley int edns_level; /* must be int! */
ce3761f64d3d734cc94605026985898900ecc474Bob Halley int goodness; /* bad < 0 <= good */
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int srtt;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_sockaddr_t sockaddr;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_stdtime_t expires;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley ISC_LINK(dns_adbentry_t) plink;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley};
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley/*
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * Internal functions (and prototypes).
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline void free_adbfind(dns_adb_t *, dns_adbfind_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_a6context_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews int *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_sockaddr_t *, int *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void dump_adb(dns_adb_t *, FILE *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_dns_name(FILE *, dns_name_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_namehook_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_find_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_fetch_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void dec_adb_irefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_boolean_t);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updaterstatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void clean_target(dns_adb_t *, dns_name_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews unsigned int);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrewsstatic void cancel_fetches_at_name(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_boolean_t, dns_rdatatype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_aaaa(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t,
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void check_exit(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void destroy(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void shutdown_names(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void shutdown_entries(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void unlink_name(dns_adb_t *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrewsstatic inline void unlink_entry(dns_adb_t *, dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void kill_name(dns_adbname_t **, isc_eventtype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_EVENT_SENT 0x40000000
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_EVENT_FREED 0x80000000
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_NEEDS_POKE 0x80000000
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_IS_DEAD 0x40000000
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * To the name, address classes are all that really exist. If it has a
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater/*
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater * Fetches are broken out into A, AAAA, and A6 types. In some cases,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * however, it makes more sense to test for a particular class of fetches,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * like V4 or V6 above.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Was this fetch started using the hints database?
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Was this the initial fetch for the A6 record? If so, we might want to
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * start AAAA queries if it fails.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FETCH_USE_HINTS 0x80000000
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FETCH_FIRST_A6 0x40000000
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrews#define FETCH_USEHINTS(f) (((f)->flags & FETCH_USE_HINTS) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Find options and tests to see if there are addresses on the list.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_STARTATROOT(fn) (((fn)->options & DNS_ADBFIND_STARTATROOT) \
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * These are currently used on simple unsigned ints, so they are
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley * not really associated with any particular type.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence */
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define ENTER_LEVEL 50
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define EXIT_LEVEL ENTER_LEVEL
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define CLEAN_LEVEL 100
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define DEF_LEVEL 5
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NCACHE_LEVEL 20
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley (r) == DNS_R_NCACHENXRRSET)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley (r) == DNS_R_NXRRSET)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halleystatic void
508f61f8d699c46f962b682f388e54b446a7194dMark AndrewsDP(int level, char *format, ...)
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews{
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews va_list args;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews va_start(args, format);
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews isc_log_vwrite(dns_lctx,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews ISC_LOG_DEBUG(level), format, args);
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews va_end(args);
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews}
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews/*
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * Requires the adbname bucket be locked and that no entry buckets be locked.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews *
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * This code handles A and AAAA rdatasets only.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews */
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsstatic isc_result_t
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews isc_stdtime_t now)
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews{
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews isc_result_t result;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews dns_adb_t *adb;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews dns_adbnamehook_t *nh;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews dns_rdata_t rdata;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews struct in_addr ina;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews struct in6_addr in6a;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews isc_sockaddr_t sockaddr;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews dns_adbentry_t *foundentry; /* NO CLEAN UP! */
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews int addr_bucket;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews isc_boolean_t new_addresses_added;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews dns_rdatatype_t rdtype;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews unsigned int findoptions;
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews INSIST(DNS_ADBNAME_VALID(adbname));
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews adb = adbname->adb;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington INSIST(DNS_ADB_VALID(adb));
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley rdtype = rdataset->type;
f0bbac2c0f1afa74b88cab902daf11202ebe7cbdBob Halley INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley if (rdtype == dns_rdatatype_a)
1366b7833c86343de278480b9abd71754e418bfaBob Halley findoptions = DNS_ADBFIND_INET;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley else
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley findoptions = DNS_ADBFIND_INET6;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley addr_bucket = DNS_ADB_INVALIDBUCKET;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley new_addresses_added = ISC_FALSE;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
f969863d54ae3f0ddccce84c6dd054fe1e792cf7Andreas Gustafsson nh = NULL;
f969863d54ae3f0ddccce84c6dd054fe1e792cf7Andreas Gustafsson result = dns_rdataset_first(rdataset);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley while (result == ISC_R_SUCCESS) {
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley dns_rdataset_current(rdataset, &rdata);
1366b7833c86343de278480b9abd71754e418bfaBob Halley if (rdtype == dns_rdatatype_a) {
1366b7833c86343de278480b9abd71754e418bfaBob Halley INSIST(rdata.length == 4);
b7ebc6a127becc7c3ad93d3b1f143132cf1d3609Andreas Gustafsson memcpy(&ina.s_addr, rdata.data, 4);
1366b7833c86343de278480b9abd71754e418bfaBob Halley isc_sockaddr_fromin(&sockaddr, &ina, 53);
1366b7833c86343de278480b9abd71754e418bfaBob Halley } else {
1366b7833c86343de278480b9abd71754e418bfaBob Halley INSIST(rdata.length == 16);
1366b7833c86343de278480b9abd71754e418bfaBob Halley memcpy(in6a.s6_addr, rdata.data, 16);
1366b7833c86343de278480b9abd71754e418bfaBob Halley isc_sockaddr_fromin6(&sockaddr, &in6a, 53);
1366b7833c86343de278480b9abd71754e418bfaBob Halley }
b7ebc6a127becc7c3ad93d3b1f143132cf1d3609Andreas Gustafsson
1366b7833c86343de278480b9abd71754e418bfaBob Halley if (IN6_IS_ADDR_V4MAPPED(&sockaddr.type.sin6.sin6_addr)
1366b7833c86343de278480b9abd71754e418bfaBob Halley || IN6_IS_ADDR_V4COMPAT(&sockaddr.type.sin6.sin6_addr)) {
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley DP(1, "Ignoring IPv6 mapped IPv4 address");
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley goto next;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley }
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley INSIST(nh == NULL);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley nh = new_adbnamehook(adb, NULL);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (nh == NULL) {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->partial_result |= findoptions;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington result = ISC_R_NOMEMORY;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington goto fail;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington }
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (foundentry == NULL) {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_adbentry_t *entry;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington entry = new_adbentry(adb);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (entry == NULL) {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->partial_result |= findoptions;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews result = ISC_R_NOMEMORY;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews goto fail;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews }
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews entry->sockaddr = sockaddr;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews entry->refcnt = 1;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews nh->entry = entry;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews link_entry(adb, addr_bucket, entry);
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews } else {
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews foundentry->refcnt++;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews nh->entry = foundentry;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews }
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews new_addresses_added = ISC_TRUE;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews if (rdtype == dns_rdatatype_a)
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews ISC_LIST_APPEND(adbname->v4, nh, plink);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews else
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews ISC_LIST_APPEND(adbname->v6, nh, plink);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews nh = NULL;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews next:
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews result = dns_rdataset_next(rdataset);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews }
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington fail:
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (nh != NULL)
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington free_adbnamehook(adb, &nh);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (addr_bucket != DNS_ADB_INVALIDBUCKET)
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington UNLOCK(&adb->entrylocks[addr_bucket]);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington rdataset->ttl = ISC_MAX(rdataset->ttl, ADB_CACHE_MINIMUM);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (rdtype == dns_rdatatype_a) {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->expire_v4, now + rdataset->ttl);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington now + rdataset->ttl);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington } else {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->expire_v6, now + rdataset->ttl);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington now + rdataset->ttl);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington }
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington if (new_addresses_added) {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington /*
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington * Lie a little here. This is more or less so code that cares
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington * can find out if any new information was added or not.
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington */
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington return (ISC_R_SUCCESS);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington }
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington return (result);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington}
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellingtonstatic void
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleyimport_a6(dns_a6context_t *a6ctx)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence{
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 dns_adbname_t *name;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dns_adb_t *adb;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_adbnamehook_t *nh;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley dns_adbentry_t *foundentry; /* NO CLEAN UP! */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int addr_bucket;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_sockaddr_t sockaddr;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley name = a6ctx->arg;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBNAME_VALID(name));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb = name->adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADB_VALID(adb));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley addr_bucket = DNS_ADB_INVALIDBUCKET;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley nh = new_adbnamehook(adb, NULL);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (nh == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto fail;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 53);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (IN6_IS_ADDR_V4MAPPED(&sockaddr.type.sin6.sin6_addr)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley || IN6_IS_ADDR_V4COMPAT(&sockaddr.type.sin6.sin6_addr)) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley DP(1, "Ignoring IPv6 mapped IPv4 address");
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley goto fail;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (foundentry == NULL) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adbentry_t *entry;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley entry = new_adbentry(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result |= DNS_ADBFIND_INET6;
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews goto fail;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews entry->sockaddr = sockaddr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt = 1;
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews nh->entry = entry;
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews link_entry(adb, addr_bucket, entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley foundentry->refcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->entry = foundentry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley ISC_LIST_APPEND(name->v6, nh, plink);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley nh = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fail:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->expire_v6, a6ctx->expiration);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->flags |= NAME_NEEDS_POKE;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (nh != NULL)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews free_adbnamehook(adb, &nh);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (addr_bucket != DNS_ADB_INVALIDBUCKET)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews UNLOCK(&adb->entrylocks[addr_bucket]);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews/*
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Requires the name's bucket be locked.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley{
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley dns_adbname_t *name;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_adb_t *adb;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews INSIST(n != NULL);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name = *n;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews *n = NULL;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews INSIST(DNS_ADBNAME_VALID(name));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley adb = name->adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADB_VALID(adb));
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "killing name %p", name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * If we're dead already, just check to see if we should go
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * away now or not.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (NAME_DEAD(name) && !NAME_FETCH(name)) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley unlink_name(adb, name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley free_adbname(adb, &name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley return;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Clean up the name's various lists. These two are destructive
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * in that they will always empty the list.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley clean_namehooks(adb, &name->v4);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley clean_namehooks(adb, &name->v6);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley clean_target(adb, &name->target);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * If fetches are running, cancel them. If none are running, we can
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * just kill the name here.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (!NAME_FETCH(name)) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley unlink_name(adb, name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley free_adbname(adb, &name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley } else {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley name->flags |= NAME_IS_DEAD;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley cancel_fetches_at_name(name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley}
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉/*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Requires the name's bucket be locked and no entry buckets be locked.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley{
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_adb_t *adb;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley INSIST(DNS_ADBNAME_VALID(name));
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley adb = name->adb;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley INSIST(DNS_ADB_VALID(adb));
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley /*
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the v4 addresses
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (NAME_HAS_V4(name)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley DP(DEF_LEVEL, "expiring v4 for name %p", name);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley clean_namehooks(adb, &name->v4);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->partial_result &= ~DNS_ADBFIND_INET;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->expire_v4 = INT_MAX;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley /*
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the v6 addresses
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (NAME_HAS_V6(name)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley DP(DEF_LEVEL, "expiring v6 for name %p", name);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley clean_namehooks(adb, &name->v6);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->partial_result &= ~DNS_ADBFIND_INET6;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->expire_v6 = INT_MAX;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley /*
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the alias target.
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (EXPIRE_OK(name->expire_target, now)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley clean_target(adb, &name->target);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->expire_target = INT_MAX;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews/*
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Requires the name's bucket be locked.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewslink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley{
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley ISC_LIST_PREPEND(adb->names[bucket], name, plink);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->lock_bucket = bucket;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley adb->name_refcnt[bucket]++;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley}
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews/*
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the name's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews */
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline void
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsunlink_name(dns_adb_t *adb, dns_adbname_t *name)
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews{
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews int bucket;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews bucket = name->lock_bucket;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews INSIST(bucket != DNS_ADB_INVALIDBUCKET);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews name->lock_bucket = DNS_ADB_INVALIDBUCKET;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews INSIST(adb->name_refcnt[bucket] > 0);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews adb->name_refcnt[bucket]--;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews dec_adb_irefcnt(adb);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews}
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews/*
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the entry's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews */
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline void
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewslink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry)
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews{
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews entry->lock_bucket = bucket;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews adb->entry_refcnt[bucket]++;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews}
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews/*
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Requires the entry's bucket be locked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews{
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews int bucket;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews bucket = entry->lock_bucket;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews INSIST(bucket != DNS_ADB_INVALIDBUCKET);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews INSIST(adb->entry_refcnt[bucket] > 0);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews adb->entry_refcnt[bucket]--;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews dec_adb_irefcnt(adb);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews}
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews{
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews UNLOCK(have);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews LOCK(want);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews LOCK(have);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews/*
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * The ADB _MUST_ be locked before calling. Also, exit conditions must be
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * checked after calling this function.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsshutdown_names(dns_adb_t *adb)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews{
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews int bucket;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_adbname_t *name;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_adbname_t *next_name;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews LOCK(&adb->namelocks[bucket]);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews adb->name_sd[bucket] = ISC_TRUE;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson name = ISC_LIST_HEAD(adb->names[bucket]);
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson if (name == NULL) {
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson /*
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * This bucket has no names. We must decrement the
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * irefcnt ourselves, since it will not be
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * automatically triggered by a name being unlinked.
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews dec_adb_irefcnt(adb);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews } else {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews /*
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Run through the list. For each name, clean up finds
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * found there, and cancel any fetches running. When
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * all the fetches are canceled, the name will destroy
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * itself.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 while (name != NULL) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley next_name = ISC_LIST_NEXT(name, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley kill_name(&name, DNS_EVENT_ADBSHUTDOWN);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name = next_name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->namelocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * The ADB _MUST_ be locked before calling. Also, exit conditions must be
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * checked after calling this function.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyshutdown_entries(dns_adb_t *adb)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington dns_adbentry_t *next_entry;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 LOCK(&adb->entrylocks[bucket]);
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington adb->entry_sd[bucket] = ISC_TRUE;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington entry = ISC_LIST_HEAD(adb->entries[bucket]);
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley if (entry == NULL) {
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington /*
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley * This bucket has no entries. We must decrement the
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley * irefcnt ourselves, since it will not be
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington * automatically triggered by an entry being unlinked.
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington dec_adb_irefcnt(adb);
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington } else {
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 /*
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley * Run through the list. Cleanup any entries not
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley * associated with names, and which are not in use.
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley */
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington while (entry != NULL) {
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington next_entry = ISC_LIST_NEXT(entry, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry->refcnt == 0 &&
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence entry->expires != 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unlink_entry(adb, entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbentry(adb, &entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry = next_entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Name bucket must be locked
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic void
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleycancel_fetches_at_name(dns_adbname_t *name)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch6_t *fetch6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (NAME_FETCH_A(name))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_resolver_cancelfetch(name->fetch_a->fetch);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_FETCH_AAAA(name))
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley fetch6 = ISC_LIST_HEAD(name->fetches_a6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (fetch6 != NULL) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_resolver_cancelfetch(fetch6->fetch);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fetch6 = ISC_LIST_NEXT(fetch6, plink);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Assumes the name bucket is locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
3740b569ae76295b941d57a724a43beb75b533baBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley{
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adbentry_t *entry;
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson dns_adbnamehook_t *namehook;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley int addr_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addr_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namehook = ISC_LIST_HEAD(*namehooks);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (namehook != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
0370190a682a3933119784a1023b1b9a51c66fa3Bob Halley /*
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * Clean up the entry if needed.
f083a44415365f6464f8bd35f439dc13ee0b684fMark Andrews */
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley entry = namehook->entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry != NULL) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley INSIST(DNS_ADBENTRY_VALID(entry));
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson if (addr_bucket != entry->lock_bucket) {
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson UNLOCK(&adb->entrylocks[addr_bucket]);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson addr_bucket = entry->lock_bucket;
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson LOCK(&adb->entrylocks[addr_bucket]);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson }
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson dec_entry_refcnt(adb, entry, ISC_FALSE);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson }
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson /*
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * Free the namehook
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater */
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson namehook->entry = NULL;
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson ISC_LIST_UNLINK(*namehooks, namehook, plink);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson free_adbnamehook(adb, &namehook);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley namehook = ISC_LIST_HEAD(*namehooks);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley}
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halleystatic void
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halleyclean_target(dns_adb_t *adb, dns_name_t *target) {
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley if (dns_name_countlabels(target) > 0) {
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley dns_name_free(target, adb->mctx);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_name_init(target, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic isc_result_t
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halleyset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_rdataset_t *rdataset, dns_name_t *target)
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley{
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley isc_result_t result;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_namereln_t namereln;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley unsigned int nlabels, nbits;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley int order;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley dns_rdata_t rdata;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley isc_region_t r;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_name_t tname;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_fixedname_t fixed1, fixed2;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_name_t *prefix, *new_target;
fff9a37a0355e4181b3132370a173d1dbb5a0f3cBob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence REQUIRE(dns_name_countlabels(target) == 0);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley if (rdataset->type == dns_rdatatype_cname) {
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley /*
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley * Copy the CNAME's target into the target name.
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley */
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley result = dns_rdataset_first(rdataset);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (result != ISC_R_SUCCESS)
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley return (result);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_rdataset_current(rdataset, &rdata);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley r.base = rdata.data;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley r.length = rdata.length;
6585d8782b2e20caf7b71f264afa4beb8a3fdf98Brian Wellington dns_name_init(&tname, NULL);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_name_fromregion(&tname, &r);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = dns_name_dup(&tname, adb->mctx, target);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews if (result != ISC_R_SUCCESS)
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews return (result);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(rdataset->type == dns_rdatatype_dname);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namereln = dns_name_fullcompare(name, fname, &order,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley &nlabels, &nbits);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(namereln == dns_namereln_subdomain);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Get the target name of the DNAME.
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_rdataset_first(rdataset);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (result != ISC_R_SUCCESS)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (result);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_rdataset_current(rdataset, &rdata);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews r.base = rdata.data;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence r.length = rdata.length;
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_name_init(&tname, NULL);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer dns_name_fromregion(&tname, &r);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer /*
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * Construct the new target name.
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews */
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_fixedname_init(&fixed1);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews prefix = dns_fixedname_name(&fixed1);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_fixedname_init(&fixed2);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater new_target = dns_fixedname_name(&fixed2);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer result = dns_name_split(name, nlabels, nbits, prefix, NULL);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_concatenate(prefix, &tname, new_target,
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence NULL);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews result = dns_name_dup(new_target, adb->mctx, target);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews if (result != ISC_R_SUCCESS)
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews return (result);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews }
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes nothing is locked, since this is called by the client.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyevent_free(isc_event_t *event)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
3e6753a75350a990d2723a936657e5dabc1c5e30Brian Wellington INSIST(event != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find = event->destroy_arg;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBFIND_VALID(find));
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&find->lock);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews find->flags |= FIND_EVENT_FREED;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley event->destroy_arg = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&find->lock);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
216030f2849b0812910fbc1817ca17208a112663Mark Andrews * Assumes the name bucket is locked.
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews */
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrewsstatic void
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrewsclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews unsigned int addrs)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley{
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_event_t *ev;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_task_t *task;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *next_find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t process;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int wanted;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ENTER_LEVEL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name, evtype, addrs);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find = ISC_LIST_HEAD(name->finds);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (find != NULL) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley LOCK(&find->lock);
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence next_find = ISC_LIST_NEXT(find, plink);
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews process = ISC_FALSE;
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence switch (evtype) {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley case DNS_EVENT_ADBMOREADDRESSES:
ce3761f64d3d734cc94605026985898900ecc474Bob Halley DP(3, "DNS_EVENT_ADBMOREADDRESSES");
ce3761f64d3d734cc94605026985898900ecc474Bob Halley if ((wanted & addrs) != 0) {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley DP(3, "processing");
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence find->flags &= ~addrs;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley process = ISC_TRUE;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley break;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley case DNS_EVENT_ADBNOMOREADDRESSES:
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley DP(3, "DNS_EVENT_ADBNOMOREADDRESSES");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->flags &= ~addrs;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (wanted == 0) {
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley process = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(3, "processing");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley default:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find->flags &= ~addrs;
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence process = ISC_TRUE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence if (process) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "cfan: processing find %p", find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * Unlink the find from the name, letting the caller
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * call dns_adb_destroyfind() on it to clean it up
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * later.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(name->finds, find, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->adbname = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->name_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(!FIND_EVENTSENT(find));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev = &find->event;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley task = ev->sender;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->sender = find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->type = evtype;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->destroy = event_free;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->destroy_arg = find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "Sending event %p to task %p for find %p",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev, task, find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley isc_task_sendanddetach(&task, &ev);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "cfan: skipping find %p", find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley UNLOCK(&find->lock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find = next_find;
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycheck_exit(dns_adb_t *adb)
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_event_t *event, *next_event;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *etask;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_boolean_t zeroirefcnt;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * The caller must be holding the adb lock.
0ad5cb4782cd419b089bcab28d2fd9e140dbcc59Mark Andrews */
0ad5cb4782cd419b089bcab28d2fd9e140dbcc59Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->ilock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (adb->irefcnt == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zeroirefcnt = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zeroirefcnt = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->ilock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (adb->shutting_down && zeroirefcnt &&
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_mempool_getallocated(adb->ahmp) == 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * We're now shutdown. Send any whenshutdown events.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley for (event = ISC_LIST_HEAD(adb->whenshutdown);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley event != NULL;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley event = next_event) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley next_event = ISC_LIST_NEXT(event, link);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_UNLINK(adb->whenshutdown, event, link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley etask = event->sender;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley event->sender = adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_sendanddetach(&etask, &event);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If there aren't any external references either, we're
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * done. Send the control event to initiate shutdown.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (adb->erefcnt == 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!adb->cevent_sent); /* Sanity check. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event = &adb->cevent;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley isc_task_send(adb->task, &event);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->cevent_sent = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsdec_adb_irefcnt(dns_adb_t *adb)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews{
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews LOCK(&adb->ilock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(adb->irefcnt > 0);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews adb->irefcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->ilock);
af3459443207253f197cd684567bfea0311ab76dMark Andrews}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->erefcnt++;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (lock)
af3459443207253f197cd684567bfea0311ab76dMark Andrews LOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(adb->erefcnt > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->erefcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (adb->erefcnt == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley check_exit(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley int bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->entrylocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley entry->refcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffstatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews{
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff int bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_boolean_t destroy_entry;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (lock)
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(entry->refcnt > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt--;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley destroy_entry = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry->refcnt == 0 &&
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (adb->entry_sd[bucket] || entry->expires == 0)) {
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews destroy_entry = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unlink_entry(adb, entry);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (lock)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews UNLOCK(&adb->entrylocks[bucket]);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (!destroy_entry)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews return;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews free_adbentry(adb, &entry);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley}
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline dns_adbname_t *
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleynew_adbname(dns_adb_t *adb, dns_name_t *dnsname)
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley{
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_adbname_t *name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name = isc_mempool_get(adb->nmp);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (name == NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (NULL);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_name_init(&name->name, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mempool_put(adb->nmp, name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_init(&name->target, NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->magic = DNS_ADBNAME_MAGIC;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->adb = adb;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->partial_result = 0;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff name->flags = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v4 = INT_MAX;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v6 = INT_MAX;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews name->expire_target = INT_MAX;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews name->chains = 0;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews name->lock_bucket = DNS_ADB_INVALIDBUCKET;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LIST_INIT(name->v4);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LIST_INIT(name->v6);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews name->fetch_a = NULL;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews name->fetch_aaaa = NULL;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LIST_INIT(name->fetches_a6);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LIST_INIT(name->finds);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LINK_INIT(name, plink);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews return (name);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline void
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrewsfree_adbname(dns_adb_t *adb, dns_adbname_t **name)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews{
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_adbname_t *n;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews n = *name;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews *name = NULL;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(!NAME_HAS_V4(n));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(!NAME_HAS_V6(n));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(!NAME_FETCH(n));
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews INSIST(ISC_LIST_EMPTY(n->finds));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(!ISC_LINK_LINKED(n, plink));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(n->adb == adb);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews n->magic = 0;
cffe96e26744abcf33494837b234219046a631d8Mark Andrews dns_name_free(&n->name, adb->mctx);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater isc_mempool_put(adb->nmp, n);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrewsstatic inline dns_adbnamehook_t *
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews{
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_adbnamehook_t *nh;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews nh = isc_mempool_get(adb->nhmp);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (nh == NULL)
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return (NULL);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews nh->magic = DNS_ADBNAMEHOOK_MAGIC;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews nh->entry = entry;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews ISC_LINK_INIT(nh, plink);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley return (nh);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntstatic inline void
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook)
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt{
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt dns_adbnamehook_t *nh;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt nh = *namehook;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt *namehook = NULL;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt INSIST(nh->entry == NULL);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt INSIST(!ISC_LINK_LINKED(nh, plink));
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt nh->magic = 0;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt isc_mempool_put(adb->nhmp, nh);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbzoneinfo_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbzoneinfo_t *zi;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley zi = isc_mempool_get(adb->zimp);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews if (zi == NULL)
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews return (NULL);
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews dns_name_init(&zi->zone, NULL);
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews isc_mempool_put(adb->zimp, zi);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt return (NULL);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi->magic = DNS_ADBZONEINFO_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi->lame_timer = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(zi, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (zi);
39f029558da291314b32291bd26b66891b5ade91Andreas Gustafsson}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrewsstatic inline void
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo)
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews{
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews dns_adbzoneinfo_t *zi;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = *zoneinfo;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *zoneinfo = NULL;
39f029558da291314b32291bd26b66891b5ade91Andreas Gustafsson
39f029558da291314b32291bd26b66891b5ade91Andreas Gustafsson INSIST(!ISC_LINK_LINKED(zi, plink));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_free(&zi->zone, adb->mctx);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence zi->magic = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence isc_mempool_put(adb->zimp, zi);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence}
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencestatic inline dns_adbentry_t *
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencenew_adbentry(dns_adb_t *adb)
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence{
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence dns_adbentry_t *e;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence isc_uint32_t r;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e = isc_mempool_get(adb->emp);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence if (e == NULL)
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence return (NULL);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->magic = DNS_ADBENTRY_MAGIC;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->lock_bucket = DNS_ADB_INVALIDBUCKET;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->refcnt = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->flags = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->goodness = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence isc_random_get(&adb->rand, &r);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->srtt = (r & 0x1f) + 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->expires = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence ISC_LIST_INIT(e->zoneinfo);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence ISC_LINK_INIT(e, plink);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence return (e);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencestatic inline void
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencefree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry)
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *e;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbzoneinfo_t *zi;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e = *entry;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence *entry = NULL;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence INSIST(e->refcnt == 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(e, plink));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = ISC_LIST_HEAD(e->zoneinfo);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley while (zi != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbzoneinfo(adb, &zi);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = ISC_LIST_HEAD(e->zoneinfo);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->emp, e);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntstatic inline dns_adbfind_t *
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntnew_adbfind(dns_adb_t *adb)
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt{
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt dns_adbfind_t *h;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_result_t result;
cedd0ab1e812ec7cf05d57c3e602db41b79f0a2aAndreas Gustafsson
cedd0ab1e812ec7cf05d57c3e602db41b79f0a2aAndreas Gustafsson h = isc_mempool_get(adb->ahmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (h == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * public members
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halley h->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->adb = adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->partial_result = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->options = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->flags = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(h, publink);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt ISC_LINK_INIT(h, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(h->list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->adbname = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->name_bucket = DNS_ADB_INVALIDBUCKET;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * private members
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = isc_mutex_init(&h->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "isc_mutex_init failed in new_adbfind()");
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mempool_put(adb->ahmp, h);
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews return (NULL);
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews }
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews NULL, NULL, h);
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->magic = DNS_ADBFIND_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (h);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbfetch(dns_adb_t *adb)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews dns_adbfetch_t *f;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f = isc_mempool_get(adb->afmp);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence if (f == NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->namehook = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->entry = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->fetch = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->namehook = new_adbnamehook(adb, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->namehook == NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->entry = new_adbentry(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->entry == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_rdataset_init(&f->rdataset);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->magic = DNS_ADBFETCH_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (f);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley err:
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews if (f->namehook != NULL)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews free_adbnamehook(adb, &f->namehook);
4cf50d58d65bbe602b68e843744799534e2cf2c0Mark Andrews if (f->entry != NULL)
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews free_adbentry(adb, &f->entry);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews isc_mempool_put(adb->afmp, f);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews return (NULL);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews}
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffstatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbfetch_t *f;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f = *fetch;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington *fetch = NULL;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->magic = 0;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (f->namehook != NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington free_adbnamehook(adb, &f->namehook);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (f->entry != NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington free_adbentry(adb, &f->entry);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (dns_rdataset_isassociated(&f->rdataset))
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_rdataset_disassociate(&f->rdataset);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington isc_mempool_put(adb->afmp, f);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington/*
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Caller must be holding the name lock.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington */
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic isc_result_t
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtona6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type, isc_stdtime_t now,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington{
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbname_t *name;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adb_t *adb;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington name = arg;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(DNS_ADBNAME_VALID(name));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington adb = name->adb;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(DNS_ADB_VALID(adb));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (dns_view_simplefind(adb->view, a6name, type, now,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington DNS_DBFIND_GLUEOK, ISC_FALSE,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington rdataset, sigrdataset));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington/*
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Caller must be holding the name lock.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington */
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic void
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtona6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbname_t *name;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adb_t *adb;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbfetch6_t *fetch;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington isc_result_t result;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington name = a6ctx->arg;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(DNS_ADBNAME_VALID(name));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington adb = name->adb;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(DNS_ADB_VALID(adb));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington fetch = new_adbfetch6(adb, name, a6ctx);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (fetch == NULL) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington name->partial_result |= DNS_ADBFIND_INET6;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington }
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington result = dns_resolver_createfetch(adb->view->resolver, a6name,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_rdatatype_a6,
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington NULL, NULL, NULL, 0,
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington adb->task, fetch_callback_a6,
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington name, &fetch->rdataset, NULL,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington &fetch->fetch);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (result != ISC_R_SUCCESS)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington goto cleanup;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington name->chains = a6ctx->chains;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington cleanup:
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (result != ISC_R_SUCCESS) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington free_adbfetch6(adb, &fetch);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington name->partial_result |= DNS_ADBFIND_INET6;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington }
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic inline dns_adbfetch6_t *
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonnew_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington{
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbfetch6_t *f;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f = isc_mempool_get(adb->af6mp);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (f == NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (NULL);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->magic = 0;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->namehook = NULL;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->entry = NULL;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->fetch = NULL;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->flags = 0;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->namehook = new_adbnamehook(adb, NULL);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (f->namehook == NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington goto err;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->entry = new_adbentry(adb);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (f->entry == NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington goto err;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_rdataset_init(&f->rdataset);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington a6missing, name);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (a6ctx != NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_a6_copy(a6ctx, &f->a6ctx);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington f->magic = DNS_ADBFETCH6_MAGIC;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (f);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews err:
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (f->namehook != NULL)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence free_adbnamehook(adb, &f->namehook);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (f->entry != NULL)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley free_adbentry(adb, &f->entry);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_mempool_put(adb->af6mp, f);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley return (NULL);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleystatic inline void
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleyfree_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence{
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley dns_adbfetch6_t *f;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley f = *fetch;
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews *fetch = NULL;
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews
d8d489cd8efc45b06a232ac07a636b3d36cc7e8fMark Andrews f->magic = 0;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (f->namehook != NULL)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley free_adbnamehook(adb, &f->namehook);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (f->entry != NULL)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley free_adbentry(adb, &f->entry);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (dns_rdataset_isassociated(&f->rdataset))
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley dns_rdataset_disassociate(&f->rdataset);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_mempool_put(adb->af6mp, f);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleystatic inline void
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Grafffree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley{
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington dns_adbfind_t *find;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley find = *findp;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley *findp = NULL;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington INSIST(!FIND_HAS_ADDRS(find));
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington INSIST(!ISC_LINK_LINKED(find, publink));
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington INSIST(!ISC_LINK_LINKED(find, plink));
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington INSIST(find->adbname == NULL);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington find->magic = 0;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington isc_mutex_destroy(&find->lock);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington isc_mempool_put(adb->ahmp, find);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington}
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington/*
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * Copy bits from the entry into the newly allocated addrinfo. The entry
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * must be locked, and the reference count must be bumped up by one
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * if this function returns a valid pointer.
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley */
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleystatic inline dns_adbaddrinfo_t *
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleynew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley{
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley dns_adbaddrinfo_t *ai;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence ai = isc_mempool_get(adb->aimp);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (ai == NULL)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley return (NULL);
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley ai->magic = DNS_ADBADDRINFO_MAGIC;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley ai->sockaddr = &entry->sockaddr;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington ai->goodness = entry->goodness;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington ai->srtt = entry->srtt;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ai->flags = entry->flags;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence ai->entry = entry;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence ISC_LINK_INIT(ai, publink);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews return (ai);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo)
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbaddrinfo_t *ai;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley ai = *ainfo;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *ainfo = NULL;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ai->sockaddr == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ai->entry == NULL);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews INSIST(!ISC_LINK_LINKED(ai, publink));
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews ai->magic = 0;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews isc_mempool_put(adb->aimp, ai);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Search for the name. NOTE: The bucket is kept locked on both
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * success and failure, so it must always be unlocked by the caller!
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * On the first call to this function, *bucketp must be set to
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * DNS_ADB_INVALIDBUCKET.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline dns_adbname_t *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencefind_name_and_lock(dns_adb_t *adb, dns_name_t *name, int *bucketp)
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley{
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_adbname_t *adbname;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (*bucketp == DNS_ADB_INVALIDBUCKET) {
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff LOCK(&adb->namelocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *bucketp = bucket;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews } else if (*bucketp != bucket) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->namelocks[*bucketp]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->namelocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *bucketp = bucket;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson }
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence adbname = ISC_LIST_HEAD(adb->names[bucket]);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews while (adbname != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (!NAME_DEAD(adbname)) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (dns_name_equal(name, &adbname->name))
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (adbname);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews adbname = ISC_LIST_NEXT(adbname, plink);
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Search for the address. NOTE: The bucket is kept locked on both
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * success and failure, so it must always be unlocked by the caller.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley *
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews * On the first call to this function, *bucketp must be set to
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * later calls (within the same "lock path") it can be left alone, so
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * if this function is called multiple times locking is only done if
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * the bucket changes.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbentry_t *
ce3761f64d3d734cc94605026985898900ecc474Bob Halleyfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp)
ce3761f64d3d734cc94605026985898900ecc474Bob Halley{
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_adbentry_t *entry;
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley int bucket;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (*bucketp == DNS_ADB_INVALIDBUCKET) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->entrylocks[bucket]);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley *bucketp = bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley } else if (*bucketp != bucket) {
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley UNLOCK(&adb->entrylocks[*bucketp]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->entrylocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *bucketp = bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley entry = ISC_LIST_HEAD(adb->entries[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley while (entry != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (isc_sockaddr_equal(addr, &entry->sockaddr))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley entry = ISC_LIST_NEXT(entry, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley/*
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews * Entry bucket MUST be locked!
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssonstatic isc_boolean_t
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssonentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson isc_stdtime_t now)
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson{
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson dns_adbzoneinfo_t *zi, *next_zi;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson isc_boolean_t is_bad;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer is_bad = ISC_FALSE;
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence zi = ISC_LIST_HEAD(entry->zoneinfo);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson if (zi == NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (ISC_FALSE);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence while (zi != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley next_zi = ISC_LIST_NEXT(zi, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Has the entry expired?
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (zi->lame_timer < now) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_adbzoneinfo(adb, &zi);
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews * Order tests from least to most expensive.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (zi != NULL && !is_bad) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (dns_name_equal(zone, &zi->zone))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley is_bad = ISC_TRUE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
31efcf64783b32a97a7dd60169375edb7d580376Brian Wellington
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley zi = next_zi;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson }
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley return (is_bad);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycopy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *name, isc_stdtime_t now)
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews{
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews dns_adbnamehook_t *namehook;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews dns_adbaddrinfo_t *addrinfo;
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence int bucket;
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews bucket = DNS_ADB_INVALIDBUCKET;
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (find->options & DNS_ADBFIND_INET) {
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley namehook = ISC_LIST_HEAD(name->v4);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley while (namehook != NULL) {
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley bucket = namehook->entry->lock_bucket;
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley LOCK(&adb->entrylocks[bucket]);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley if (entry_is_bad_for_zone(adb, namehook->entry,
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley zone, now))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto nextv4;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = new_adbaddrinfo(adb, namehook->entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (addrinfo == NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find->partial_result |= DNS_ADBFIND_INET;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto out;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Found a valid entry. Add it to the find's list.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_APPEND(find->list, addrinfo, publink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley nextv4:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->entrylocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley bucket = DNS_ADB_INVALIDBUCKET;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley namehook = ISC_LIST_NEXT(namehook, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (find->options & DNS_ADBFIND_INET6) {
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews namehook = ISC_LIST_HEAD(name->v6);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews while (namehook != NULL) {
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews bucket = namehook->entry->lock_bucket;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews LOCK(&adb->entrylocks[bucket]);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews if (entry_is_bad_for_zone(adb, namehook->entry,
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews zone, now))
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews goto nextv6;
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews addrinfo = new_adbaddrinfo(adb, namehook->entry);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews if (addrinfo == NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find->partial_result |= DNS_ADBFIND_INET6;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto out;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Found a valid entry. Add it to the find's list.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_APPEND(find->list, addrinfo, publink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = NULL;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff nextv6:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->entrylocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley bucket = DNS_ADB_INVALIDBUCKET;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley namehook = ISC_LIST_NEXT(namehook, plink);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley out:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (bucket != DNS_ADB_INVALIDBUCKET)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence UNLOCK(&adb->entrylocks[bucket]);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssonstatic void
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyershutdown_task(isc_task_t *task, isc_event_t *ev)
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson{
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson dns_adb_t *adb;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson (void)task; /* not used */
425028583845247a2e09532a88e58165e82924f4Brian Wellington
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson adb = ev->arg;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson INSIST(DNS_ADB_VALID(adb));
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson /*
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson * Kill the timer, and then the ADB itself. Note that this implies
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson * that this task was the one scheduled to get timer events. If
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * this is not true (and it is unfortunate there is no way to INSIST()
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * this) badness will occur.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff */
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews LOCK(&adb->lock);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews isc_timer_detach(&adb->timer);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews UNLOCK(&adb->lock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_event_free(&ev);
571469b0a58b011966bbea91ca992928554d6ca1David Lawrence destroy(adb);
ec0613a09776abfb6409506495a7ccbe72294938Andreas Gustafsson}
ec0613a09776abfb6409506495a7ccbe72294938Andreas Gustafsson
ec0613a09776abfb6409506495a7ccbe72294938Andreas Gustafsson/*
0c264c909424f855e7e3b7ce7bb21f650609e768Brian Wellington * name bucket must be locked; adb may be locked; no other locks held.
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewsstatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycheck_expire_name(dns_adbname_t **namep, isc_stdtime_t now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley{
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name = *namep;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *namep = NULL;
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley return;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley if (NAME_FETCH(name))
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley return;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley if (!EXPIRE_OK(name->expire_v4, now))
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley return;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (!EXPIRE_OK(name->expire_v6, now))
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley return;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley if (!EXPIRE_OK(name->expire_target, now))
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley return;
0c264c909424f855e7e3b7ce7bb21f650609e768Brian Wellington
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews /*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The name is empty. Delete it.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews kill_name(&name, DNS_EVENT_ADBEXPIRED);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews /*
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * Our caller, or one of its callers, will be calling check_exit() at
29daf5bc7738f1cdab7914562269e1129c81acdcBrian Wellington * some point, so we don't need to do it here.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews}
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington/*
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington * entry bucket must be locked; adb may be locked; no other locks held.
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington */
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellingtonstatic void
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellingtoncheck_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington{
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington dns_adbentry_t *entry;
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews entry = *entryp;
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington *entryp = NULL;
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews if (entry->refcnt != 0)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews return;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (entry->expires == 0 || entry->expires > now)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews /*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * The entry is not in use. Delete it.
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington */
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington DP(DEF_LEVEL, "killing entry %p", entry);
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington INSIST(ISC_LINK_LINKED(entry, plink));
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington unlink_entry(adb, entry);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews free_adbentry(adb, &entry);
1c33761bcff783815a952e6e1fb9d5e07a3e1363Brian Wellington}
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews/*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * ADB must be locked, and no other locks held.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewsstatic void
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewscleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now)
1c33761bcff783815a952e6e1fb9d5e07a3e1363Brian Wellington{
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews dns_adbname_t *name;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_adbname_t *next_name;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews LOCK(&adb->namelocks[bucket]);
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews if (adb->name_sd[bucket]) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews UNLOCK(&adb->namelocks[bucket]);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley return;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley name = ISC_LIST_HEAD(adb->names[bucket]);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley while (name != NULL) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley next_name = ISC_LIST_NEXT(name, plink);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews check_expire_namehooks(name, now);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley check_expire_name(&name, now);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley name = next_name;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
ce3761f64d3d734cc94605026985898900ecc474Bob Halley UNLOCK(&adb->namelocks[bucket]);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews}
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley/*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * ADB must be locked, and no other locks held.
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews */
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrewsstatic void
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halleycleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley{
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley dns_adbentry_t *entry, *next_entry;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley LOCK(&adb->entrylocks[bucket]);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley entry = ISC_LIST_HEAD(adb->entries[bucket]);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley while (entry != NULL) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley next_entry = ISC_LIST_NEXT(entry, plink);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington check_expire_entry(adb, &entry, now);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley entry = next_entry;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews UNLOCK(&adb->entrylocks[bucket]);
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews}
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halleystatic void
ce3761f64d3d734cc94605026985898900ecc474Bob Halleytimer_cleanup(isc_task_t *task, isc_event_t *ev)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence{
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley dns_adb_t *adb;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_result_t result;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_stdtime_t now;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley (void)task; /* not used */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews adb = ev->arg;
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer INSIST(DNS_ADB_VALID(adb));
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley LOCK(&adb->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_stdtime_get(&now);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Call our cleanup routines.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley cleanup_names(adb, adb->next_cleanbucket, now);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley cleanup_entries(adb, adb->next_cleanbucket, now);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Set the next bucket to be cleaned.
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer */
425028583845247a2e09532a88e58165e82924f4Brian Wellington adb->next_cleanbucket++;
425028583845247a2e09532a88e58165e82924f4Brian Wellington if (adb->next_cleanbucket >= NBUCKETS) {
425028583845247a2e09532a88e58165e82924f4Brian Wellington adb->next_cleanbucket = 0;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley#ifdef DUMP_ADB_AFTER_CLEANING
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley dump_adb(adb, stdout);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff#endif
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Reset the timer.
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_timer_reset(adb->timer, isc_timertype_once, NULL,
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley &adb->tick_interval, ISC_FALSE);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley UNLOCK(&adb->lock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_event_free(&ev);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley}
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic void
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halleydestroy(dns_adb_t *adb)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley{
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->magic = 0;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * The timer is already dead, from the task's shutdown callback.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_task_detach(&adb->task);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_destroy(&adb->nmp);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_destroy(&adb->nhmp);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_destroy(&adb->zimp);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_destroy(&adb->emp);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_destroy(&adb->ahmp);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mempool_destroy(&adb->aimp);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews isc_mempool_destroy(&adb->afmp);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_destroy(&adb->af6mp);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_mutex_destroy(&adb->ilock);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mutex_destroy(&adb->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mutex_destroy(&adb->mplock);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 isc_random_invalidate(&adb->rand);
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_mem_put(adb->mctx, adb, sizeof (dns_adb_t));
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence}
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence/*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Public functions.
72ce8db9183a39f393dbc6ff15259dbf7ea55275David Lawrence */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrenceisc_result_t
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrencedns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence{
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley dns_adb_t *adb;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_result_t result;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence int i;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley REQUIRE(mem != NULL);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence REQUIRE(view != NULL);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence REQUIRE(timermgr != NULL);
41c53e1d11ed6685dfa929bd8bef524ed17b4064Andreas Gustafsson REQUIRE(taskmgr != NULL);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews REQUIRE(newadb != NULL && *newadb == NULL);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence adb = isc_mem_get(mem, sizeof (dns_adb_t));
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (adb == NULL)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews return (ISC_R_NOMEMORY);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * Initialize things here that cannot fail, and especially things
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley * that must be NULL for the error return to work properly.
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff adb->magic = 0;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews adb->erefcnt = 1;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews adb->irefcnt = 0;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews adb->nmp = NULL;
8326257468615966b10820260beb3ee96eee94b5Bob Halley adb->nhmp = NULL;
8326257468615966b10820260beb3ee96eee94b5Bob Halley adb->zimp = NULL;
8326257468615966b10820260beb3ee96eee94b5Bob Halley adb->emp = NULL;
8326257468615966b10820260beb3ee96eee94b5Bob Halley adb->ahmp = NULL;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley adb->aimp = NULL;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley adb->afmp = NULL;
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley adb->af6mp = NULL;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley adb->task = NULL;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley adb->timer = NULL;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley adb->mctx = mem;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley adb->view = view;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley adb->timermgr = timermgr;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley adb->taskmgr = taskmgr;
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley adb->next_cleanbucket = 0;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley ISC_EVENT_INIT(&adb->cevent, sizeof adb->cevent, 0, NULL,
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley DNS_EVENT_ADBCONTROL, shutdown_task, adb,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff adb, NULL, NULL);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley adb->cevent_sent = ISC_FALSE;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley adb->shutting_down = ISC_FALSE;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley ISC_LIST_INIT(adb->whenshutdown);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley result = isc_random_init(&adb->rand);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley if (result != ISC_R_SUCCESS)
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley goto fail0a;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mutex_init(&adb->lock);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (result != ISC_R_SUCCESS)
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley goto fail0b;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mutex_init(&adb->mplock);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (result != ISC_R_SUCCESS)
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley goto fail0c;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = isc_mutex_init(&adb->ilock);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (result != ISC_R_SUCCESS)
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley goto fail0d;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley /*
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * Initialize the bucket locks for names and elements.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * May as well initialize the list heads, too.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley */
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (result != ISC_R_SUCCESS)
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley goto fail1;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley for (i = 0 ; i < NBUCKETS ; i++) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley ISC_LIST_INIT(adb->names[i]);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->name_sd[i] = ISC_FALSE;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->name_refcnt[i] = 0;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->irefcnt++;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley }
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley for (i = 0 ; i < NBUCKETS ; i++) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley ISC_LIST_INIT(adb->entries[i]);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->entry_sd[i] = ISC_FALSE;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->entry_refcnt[i] = 0;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->irefcnt++;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley }
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (result != ISC_R_SUCCESS)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff goto fail2;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley /*
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * Memory pools
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley */
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley#define MPINIT(t, p, l, n) do { \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_mempool_create(mem, sizeof (t), &(p)); \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (result != ISC_R_SUCCESS) \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley goto fail3; \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_mempool_setfreemax((p), FREE_ITEMS); \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_mempool_setfillcount((p), FILL_COUNT); \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_mempool_setname((p), n); \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (l) \
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_mempool_associatelock((p), &adb->mplock); \
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence} while (0)
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff MPINIT(dns_adbname_t, adb->nmp, ISC_TRUE, "adbname");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbnamehook_t, adb->nhmp, ISC_TRUE, "adbnamehook");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbzoneinfo_t, adb->zimp, ISC_TRUE, "adbzoneinfo");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbentry_t, adb->emp, ISC_TRUE, "adbentry");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbfind_t, adb->ahmp, ISC_TRUE, "adbfind");
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley MPINIT(dns_adbaddrinfo_t, adb->aimp, ISC_TRUE, "adbaddrinfo");
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley MPINIT(dns_adbfetch_t, adb->afmp, ISC_TRUE, "adbfetch");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley MPINIT(dns_adbfetch6_t, adb->af6mp, ISC_TRUE, "adbfetch6");
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley#undef MPINIT
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley /*
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Allocate a timer and a task for our periodic cleanup.
8326257468615966b10820260beb3ee96eee94b5Bob Halley */
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley result = isc_task_create(adb->taskmgr, adb->mctx, 0, &adb->task);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (result != ISC_R_SUCCESS)
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley goto fail3;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley /*
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley * XXXMLG When this is changed to be a config file option,
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley */
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff result = isc_timer_create(adb->timermgr, isc_timertype_once,
8326257468615966b10820260beb3ee96eee94b5Bob Halley NULL, &adb->tick_interval, adb->task,
8326257468615966b10820260beb3ee96eee94b5Bob Halley timer_cleanup, adb, &adb->timer);
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (result != ISC_R_SUCCESS)
8326257468615966b10820260beb3ee96eee94b5Bob Halley goto fail3;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
8326257468615966b10820260beb3ee96eee94b5Bob Halley /*
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Normal return.
8326257468615966b10820260beb3ee96eee94b5Bob Halley */
8326257468615966b10820260beb3ee96eee94b5Bob Halley adb->magic = DNS_ADB_MAGIC;
8326257468615966b10820260beb3ee96eee94b5Bob Halley *newadb = adb;
8326257468615966b10820260beb3ee96eee94b5Bob Halley return (ISC_R_SUCCESS);
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley fail3:
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (adb->task != NULL)
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington isc_task_detach(&adb->task);
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews if (adb->timer != NULL)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_timer_detach(&adb->timer);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
8326257468615966b10820260beb3ee96eee94b5Bob Halley /* clean up entrylocks */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
8326257468615966b10820260beb3ee96eee94b5Bob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence fail2: /* clean up namelocks */
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
8326257468615966b10820260beb3ee96eee94b5Bob Halley fail1: /* clean up only allocated memory */
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (adb->nmp != NULL)
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_mempool_destroy(&adb->nmp);
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews if (adb->nhmp != NULL)
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_mempool_destroy(&adb->nhmp);
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews if (adb->zimp != NULL)
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_mempool_destroy(&adb->zimp);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (adb->emp != NULL)
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mempool_destroy(&adb->emp);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (adb->ahmp != NULL)
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mempool_destroy(&adb->ahmp);
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews if (adb->aimp != NULL)
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_mempool_destroy(&adb->aimp);
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews if (adb->afmp != NULL)
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mempool_destroy(&adb->afmp);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (adb->af6mp != NULL)
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mempool_destroy(&adb->af6mp);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mutex_destroy(&adb->ilock);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley fail0d:
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mutex_destroy(&adb->mplock);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley fail0c:
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mutex_destroy(&adb->lock);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley fail0b:
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_random_invalidate(&adb->rand);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley fail0a:
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley isc_mem_put(mem, adb, sizeof (dns_adb_t));
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence return (result);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley}
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halleyvoid
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencedns_adb_detach(dns_adb_t **adbx)
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley{
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley dns_adb_t *adb;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews adb = *adbx;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews *adbx = NULL;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews LOCK(&adb->lock);
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews dec_adb_erefcnt(adb, ISC_FALSE);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (adb->erefcnt == 0)
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews INSIST(adb->shutting_down);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews UNLOCK(&adb->lock);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews}
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrewsvoid
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrewsdns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp)
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews{
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews isc_task_t *clone;
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews isc_event_t *event;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews isc_boolean_t zeroirefcnt = ISC_FALSE;
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews /*
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Send '*eventp' to 'task' when 'adb' has shutdown.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews */
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews REQUIRE(DNS_ADB_VALID(adb));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews REQUIRE(eventp != NULL);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews event = *eventp;
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews *eventp = NULL;
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater LOCK(&adb->lock);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews LOCK(&adb->ilock);
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews if (adb->irefcnt == 0)
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews zeroirefcnt = ISC_TRUE;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews else
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews zeroirefcnt = ISC_FALSE;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews UNLOCK(&adb->ilock);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews if (adb->shutting_down && zeroirefcnt &&
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews isc_mempool_getallocated(adb->ahmp) == 0) {
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews /*
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * We're already shutdown. Send the event.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews */
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews event->sender = adb;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews isc_task_send(task, &event);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson } else {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence clone = NULL;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson isc_task_attach(task, &clone);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson event->sender = clone;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson ISC_LIST_APPEND(adb->whenshutdown, event, link);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence UNLOCK(&adb->lock);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence}
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencevoid
a3c0a79b61edfd6a021c080d4b368c9c962fcad6Andreas Gustafssondns_adb_shutdown(dns_adb_t *adb) {
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * Shutdown 'adb'.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence LOCK(&adb->lock);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (!adb->shutting_down) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence adb->shutting_down = ISC_TRUE;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson shutdown_names(adb);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson shutdown_entries(adb);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington check_exit(adb);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt }
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt UNLOCK(&adb->lock);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt}
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Huntisc_result_t
3f8be559f0871022c78a229bad0eb09560b90909Evan Huntdns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt void *arg, dns_name_t *name, dns_name_t *zone,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt unsigned int options, isc_stdtime_t now, dns_name_t *target,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt dns_adbfind_t **findp)
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt{
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt dns_adbfind_t *find;
74f4bfde4abb36524e62bed2bbc27d775e67c0a9Automatic Updater dns_adbname_t *adbname;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt int bucket;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt isc_boolean_t use_hints, want_event, start_at_root, alias;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt isc_result_t result;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt unsigned int wanted_addresses;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt unsigned int wanted_fetches;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt unsigned int query_pending;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(DNS_ADB_VALID(adb));
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (task != NULL) {
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(action != NULL);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt }
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(name != NULL);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(zone != NULL);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(findp != NULL && *findp == NULL);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE(target == NULL || dns_name_hasbuffer(target));
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt result = ISC_R_UNEXPECTED;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 wanted_fetches = 0;
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 query_pending = 0;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews want_event = ISC_FALSE;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews start_at_root = ISC_FALSE;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews alias = ISC_FALSE;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (now == 0)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews isc_stdtime_get(&now);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
9cee5bb02863bf191e12cd4297adabf1971020deAutomatic Updater /*
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * XXXMLG Move this comment somewhere else!
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt *
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Look up the name in our internal database.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt *
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Possibilities: Note that these are not always exclusive.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt *
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * No name found. In this case, allocate a new name header and
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * an initial namehook or two. If any of these allocations
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * fail, clean up and return ISC_R_NOMEMORY.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Name found, valid addresses present. Allocate one addrinfo
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * structure for each found and append it to the linked list
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * of addresses for this header.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Name found, queries pending. In this case, if a task was
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * passed in, allocate a job id, attach it to the name's job
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * list and remember to tell the caller that there will be
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * more info coming later.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt */
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt find = new_adbfind(adb);
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (find == NULL)
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews return (ISC_R_NOMEMORY);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt /*
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Remember what types of addresses we are interested in.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington find->options = options;
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington find->flags |= wanted_addresses;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (FIND_WANTEVENT(find)) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington REQUIRE(task != NULL);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Try to see if we know anything about this name at all.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington bucket = DNS_ADB_INVALIDBUCKET;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname = find_name_and_lock(adb, name, &bucket);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (adb->name_sd[bucket]) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington DP(DEF_LEVEL,
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington free_adbfind(adb, &find);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington result = ISC_R_SHUTTINGDOWN;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington goto out;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Nothing found. Allocate a new adbname structure for this name.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (adbname == NULL) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname = new_adbname(adb, name);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (adbname == NULL) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington free_adbfind(adb, &find);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington result = ISC_R_NOMEMORY;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington goto out;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington link_name(adb, bucket, adbname);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Expire old entries, etc.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington check_expire_namehooks(adbname, now);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington use_hints = dns_name_equal(zone, dns_rootname);
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington /*
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * Do we know that the name is an alias?
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (!EXPIRE_OK(adbname->expire_target, now)) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Yes, it is.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington DP(DEF_LEVEL,
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington "dns_adb_createfind: name %p is an alias (cached)",
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews alias = ISC_TRUE;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews goto post_copy;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews }
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews /*
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * Try to populate the name from the database and/or
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * start fetches.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews */
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (!NAME_HAS_V4(adbname) && !NAME_FETCH_V4(adbname)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews && EXPIRE_OK(adbname->expire_v4, now)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews && WANT_INET(wanted_addresses)) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews result = dbfind_name(adbname, now, use_hints, dns_rdatatype_a);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (result == ISC_R_SUCCESS) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews DP(DEF_LEVEL,
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews "dns_adb_createfind: Found A for name %p in db",
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews adbname);
goto v6;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_CNAME || result == DNS_R_DNAME) {
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, use_hints);
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_CNAME || result == DNS_R_DNAME) {
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, use_hints, 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);
if (NAME_FETCH_V4(adbname))
query_pending |= DNS_ADBFIND_INET;
if (NAME_FETCH_V6(adbname))
query_pending |= DNS_ADBFIND_INET6;
post_copy:
/*
* 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->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.sender = taskp;
find->event.action = action;
find->event.arg = arg;
}
}
/* dns_adb_dumpfind(find, stderr); */
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
isc_result_t
dns_adb_deletename(dns_adb_t *adb, dns_name_t *host)
{
int name_bucket;
dns_adbname_t *name;
isc_boolean_t want_check_exit = ISC_FALSE;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
name = NULL;
/*
* Find the name.
*/
name_bucket = DNS_ADB_INVALIDBUCKET;
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
UNLOCK(&adb->namelocks[name_bucket]);
return (ISC_R_NOTFOUND);
}
kill_name(&name, DNS_EVENT_ADBNAMEDELETED);
/*
* 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[name_bucket] && adb->name_refcnt[name_bucket] == 0)
want_check_exit = ISC_TRUE;
if (name_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[name_bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return (DNS_R_SUCCESS);
}
/* XXXMLG needs v6 support */
isc_result_t
_dns_adb_insert(dns_adb_t *adb, dns_name_t *host, isc_sockaddr_t *addr,
dns_ttl_t ttl, isc_stdtime_t now)
{
dns_adbname_t *name;
isc_boolean_t free_name;
dns_adbentry_t *entry;
isc_boolean_t free_entry;
dns_adbnamehook_t *namehook;
isc_boolean_t free_namehook;
int name_bucket, addr_bucket; /* unlock if != DNS_ADB_INVALIDBUCKET */
isc_result_t result;
isc_stdtime_t expire_time;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
REQUIRE(addr != NULL);
if (now == 0)
isc_stdtime_get(&now);
expire_time = now + ttl;
name = NULL;
free_name = ISC_FALSE;
entry = NULL;
free_entry = ISC_FALSE;
namehook = NULL;
free_namehook = ISC_FALSE;
result = ISC_R_UNEXPECTED;
/*
* First, see if the host is already in the database. If it is,
* don't make a new host entry. If not, copy the name and name's
* contents into our structure and allocate what we'll need
* to attach things together.
*/
name_bucket = DNS_ADB_INVALIDBUCKET;
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
name = new_adbname(adb, host);
if (name == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_name = ISC_TRUE;
name->expire_v4 = ISC_MIN(expire_time, name->expire_v4);
}
/*
* Now, while keeping the name locked, search for the address.
* Three possibilities: One, the address doesn't exist.
* Two, the address exists, but we aren't linked to it.
* Three, the address exists and we are linked to it.
* (1) causes a new entry and namehook to be created.
* (2) causes only a new namehook.
* (3) is an error.
*/
addr_bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, addr, &addr_bucket);
/*
* Case (1): new entry and namehook.
*/
if (entry == NULL) {
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_entry = ISC_TRUE;
}
/*
* Case (3): entry exists, we're linked.
*/
namehook = ISC_LIST_HEAD(name->v4);
while (namehook != NULL) {
if (namehook->entry == entry) {
result = ISC_R_EXISTS;
goto out;
}
namehook = ISC_LIST_NEXT(namehook, plink);
}
/*
* Case (2): New namehook, link to entry from above.
*/
namehook = new_adbnamehook(adb, entry);
if (namehook == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_namehook = ISC_TRUE;
ISC_LIST_APPEND(name->v4, namehook, plink);
entry->lock_bucket = addr_bucket;
inc_entry_refcnt(adb, entry, ISC_FALSE);
entry->sockaddr = *addr;
/*
* If needed, string up the name and entry.
*/
if (!ISC_LINK_LINKED(name, plink))
link_name(adb, name_bucket, name);
if (!ISC_LINK_LINKED(entry, plink))
link_entry(adb, addr_bucket, entry);
name->expire_v4 = ISC_MIN(name->expire_v4, expire_time);
UNLOCK(&adb->namelocks[name_bucket]);
name_bucket = DNS_ADB_INVALIDBUCKET;
UNLOCK(&adb->entrylocks[addr_bucket]);
addr_bucket = DNS_ADB_INVALIDBUCKET;
return (ISC_R_SUCCESS);
out:
if (free_name)
free_adbname(adb, &name);
if (free_entry)
isc_mempool_put(adb->emp, entry);
if (free_namehook)
isc_mempool_put(adb->nhmp, namehook);
if (name_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[name_bucket]);
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[addr_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;
ai->sockaddr = 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->sender;
ev->sender = find;
ev->type = DNS_EVENT_ADBCANCELED;
ev->destroy = event_free;
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\n", name);
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 = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\t%p: refcnt %u flags %08x goodness %d"
" srtt %u addr %s\n",
entry, entry->refcnt, entry->flags,
entry->goodness, entry->srtt, tmpp);
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.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 = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\t\tentry %p, flags %08x goodness %d"
" srtt %u addr %s\n",
ai->entry, ai->flags, ai->goodness, ai->srtt, tmpp);
ai = ISC_LIST_NEXT(ai, publink);
}
UNLOCK(&find->lock);
}
static void
print_dns_name(FILE *f, dns_name_t *name)
{
char buf[1024];
isc_buffer_t b;
isc_region_t r;
INSIST(f != NULL);
isc_buffer_init(&b, buf, sizeof buf, ISC_BUFFERTYPE_TEXT);
if (dns_name_totext(name, ISC_FALSE, &b) == ISC_R_SUCCESS) {
isc_buffer_used(&b, &r);
fprintf(f, "%.*s", (int)r.length, r.base);
}
}
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, 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,
isc_boolean_t use_hints, 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,
DNS_DBFIND_GLUEOK, use_hints, fname,
&rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case DNS_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 = ISC_MAX(rdataset.ttl, ADB_CACHE_MINIMUM);
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:
rdataset.ttl = ISC_MAX(rdataset.ttl, ADB_CACHE_MINIMUM);
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) {
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_boolean_t use_hints)
{
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, DNS_DBFIND_GLUEOK, use_hints, fname,
&rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case DNS_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 = ISC_MAX(rdataset.ttl, ADB_CACHE_MINIMUM);
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) {
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->type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = 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 = ISC_MAX(dev->rdataset->ttl,
ADB_CACHE_MINIMUM);
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 = ISC_MAX(dev->rdataset->ttl,
ADB_CACHE_MINIMUM);
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->type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = 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) {
isc_boolean_t use_hints;
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 = ISC_MAX(dev->rdataset->ttl,
ADB_CACHE_MINIMUM);
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 = ISC_MAX(dev->rdataset->ttl,
ADB_CACHE_MINIMUM);
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_USEHINTS(fetch))
use_hints = ISC_TRUE;
else
use_hints = ISC_FALSE;
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, use_hints,
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;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V4(adbname));
if (start_at_root) {
DP(50, "fetch_name_v4: starting at DNS root for name %p",
adbname);
name = dns_rootname;
} else
name = &adbname->name;
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,
NULL, NULL, 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);
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 use_hints,
isc_boolean_t start_at_root)
{
isc_result_t result;
dns_adbfetch6_t *fetch;
dns_adb_t *adb;
dns_name_t *name;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V6(adbname));
if (start_at_root) {
DP(50, "fetch_name_a6: starting at DNS root for name %p",
adbname);
name = dns_rootname;
} else
name = &adbname->name;
fetch = new_adbfetch6(adb, adbname, NULL);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
if (use_hints)
fetch->flags |= FETCH_USE_HINTS;
fetch->flags |= FETCH_FIRST_A6;
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a6,
NULL, NULL, 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);
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_MAX;
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.
*
* XXXRTH I think this is what we want, because otherwise flags
* that the caller didn't ask to change could be updated.
*/
addr->flags = (addr->flags & ~mask) | (bits & mask);
UNLOCK(&adb->entrylocks[bucket]);
}
isc_result_t
dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
dns_adbaddrinfo_t **addrp)
{
int bucket;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *addr;
isc_result_t result;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL && *addrp == NULL);
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);
addr = new_adbaddrinfo(adb, entry);
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;
addr->sockaddr = NULL;
free_adbaddrinfo(adb, &addr);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
}