adb.c revision da318480f3c527e48647f4531225b259dffc8cb8
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 1999-2001 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
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/* $Id: adb.c,v 1.206 2003/10/10 00:13:50 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Implementation notes
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley * --------------------
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * In finds, if task == NULL, no events will be generated, and no events
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * 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.
ddfe394c061049bbd71125ad41c3dd3092b2bbfdAndreas Gustafsson *
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews/*
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley * After we have cleaned all buckets, dump the database contents.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews#if 0
364a82f7c25b62967678027043425201a5e5171aBob Halley#define DUMP_ADB_AFTER_CLEANING
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#endif
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt#include <config.h>
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington#include <limits.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/mutexblock.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <isc/netaddr.h>
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <isc/random.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/string.h> /* Required for HP/UX (and others?) */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/task.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/timer.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/util.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/adb.h>
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews#include <dns/db.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/events.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/log.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/rdata.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/rdataset.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/rdatastruct.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/resolver.h>
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence#include <dns/result.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
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#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/AAAA records
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * if the zone has extremely low TTLs.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_CACHE_MINIMUM 10 /* seconds */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_ENTRY_WINDOW 1800 /* seconds */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * buckets are cleaned in CLEAN_PERIOD seconds.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_PERIOD 3600
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_SECONDS 30
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FREE_ITEMS 64 /* free count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FILL_COUNT 16 /* fill count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_MINADBSIZE (1024*1024) /* 1 Megabyte */
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;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbfetch dns_adbfetch_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbfetch6 dns_adbfetch6_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleystruct dns_adb {
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int magic;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mutex_t lock;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
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;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_boolean_t overmem;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_interval_t tick_interval;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence int next_cleanbucket;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int irefcnt;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein 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 */
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley /*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Bucketized locks and lists for names.
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley *
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * XXXRTH Have a per-bucket structure that contains all of these?
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley */
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley dns_adbnamelist_t names[NBUCKETS];
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley isc_mutex_t namelocks[NBUCKETS];
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley isc_boolean_t name_sd[NBUCKETS];
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley unsigned int name_refcnt[NBUCKETS];
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson /*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Bucketized locks for entries.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * 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];
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson unsigned int entry_refcnt[NBUCKETS];
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_event_t cevent;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_boolean_t cevent_sent;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_boolean_t shutting_down;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_eventlist_t whenshutdown;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson};
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * XXXMLG Document these structures.
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer */
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;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson int lock_bucket;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson dns_name_t target;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_stdtime_t expire_target;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_stdtime_t expire_v4;
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson isc_stdtime_t expire_v6;
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley unsigned int chains;
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley dns_adbnamehooklist_t v4;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_adbnamehooklist_t v6;
e61793f0865117ad87a19d6e245bea8f3b712d1bDanny Mayer dns_adbfetch_t *fetch_a;
5f120ce962b03e4dcf6f1974b9b896f0fa7cacb0Bob Halley dns_adbfetch_t *fetch_aaaa;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews unsigned int fetch_err;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews unsigned int fetch6_err;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews dns_adbfindlist_t finds;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews ISC_LINK(dns_adbname_t) plink;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews};
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrewsstruct dns_adbfetch {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews unsigned int magic;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_adbnamehook_t *namehook;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_adbentry_t *entry;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_fetch_t *fetch;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews dns_rdataset_t rdataset;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews};
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews/*
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * dns_adbnamehook_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * This is a small widget that dangles off a dns_adbname_t. It contains a
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * pointer to the address information about this host, and a link to the next
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * namehook that will contain the next address this host has.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystruct dns_adbnamehook {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK(dns_adbnamehook_t) plink;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington};
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * dns_adbzoneinfo_t
da527e4ff6a013364826637963e7ac372e024f33David Lawrence *
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * This is a small widget that holds zone-specific information about an
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * address. Currently limited to lameness, but could just as easily be
da527e4ff6a013364826637963e7ac372e024f33David Lawrence * extended to other types of information about zones.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
da527e4ff6a013364826637963e7ac372e024f33David Lawrencestruct dns_adbzoneinfo {
da527e4ff6a013364826637963e7ac372e024f33David Lawrence unsigned int magic;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_name_t zone;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_stdtime_t lame_timer;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LINK(dns_adbzoneinfo_t) plink;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley};
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * An address entry. It holds quite a bit of information about addresses,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * including edns state (in "flags"), rtt, and of course the address of
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * the host.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystruct dns_adbentry {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int magic;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley int lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int refcnt;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int flags;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley unsigned int srtt;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_sockaddr_t sockaddr;
ee03a00244edc6c823ea11bed16437865b395d5dAndreas Gustafsson
ce3761f64d3d734cc94605026985898900ecc474Bob Halley isc_stdtime_t expires;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley /*
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * A nonzero 'expires' field indicates that the entry should
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * persist until that time. This allows entries found
5fc1b54cc6134bd70f4e22df90a2e5631aaea77aBob Halley * using dns_adb_findaddrinfo() to persist for a limited time
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * even though they are not necessarily associated with a
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * name.
ce3761f64d3d734cc94605026985898900ecc474Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence ISC_LINK(dns_adbentry_t) plink;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley};
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley/*
ce3761f64d3d734cc94605026985898900ecc474Bob Halley * Internal functions (and prototypes).
ce3761f64d3d734cc94605026985898900ecc474Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley dns_adbentry_t *);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
ce3761f64d3d734cc94605026985898900ecc474Bob Halleystatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley in_port_t);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley unsigned int, int *);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley isc_sockaddr_t *, int *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void print_dns_name(FILE *, dns_name_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_namehook_list(FILE *, const char *legend,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_adbnamehooklist_t *list,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_boolean_t debug,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_stdtime_t now);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_find_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void print_fetch_list(FILE *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_adb_irefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_adb_erefcnt(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t 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 isc_boolean_t 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 isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void cancel_fetches_at_name(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_rdatatype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_rdatatype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void check_exit(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrewsstatic void destroy(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_names(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_entries(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updaterstatic inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void water(void *arg, int mark);
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
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark 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_HINT_OK DNS_ADBFIND_HINTOK
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
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#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 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 a 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
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Fetches are broken out into A and AAAA 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 * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater * are now equal to FETCH_V4 and FETCH_V6, respectively.
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater */
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_V4(n) (NAME_FETCH_A(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
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_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
86548554f6180bbe051c8cd8f03c93fc9b6a7825Mark Andrews#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * These are currently used on simple unsigned ints, so they are
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * not really associated with any particular type.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews/*
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * Find out if the flags on a name (nf) indicate if it is a hint or
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * glue, and compare this to the appropriate bits set in o, to see if
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews * this is ok.
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews */
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley ((o) & DNS_ADBFIND_STARTATZONE))
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define ENTER_LEVEL ISC_LOG_DEBUG(50)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define EXIT_LEVEL ENTER_LEVEL
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define CLEAN_LEVEL ISC_LOG_DEBUG(100)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define DEF_LEVEL ISC_LOG_DEBUG(5)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NCACHE_LEVEL ISC_LOG_DEBUG(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#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley (r) == DNS_R_NCACHENXDOMAIN)
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
00d81794884f1eee59ca058a292f2d1e50d9547cBob Halley (r) == DNS_R_NXRRSET || \
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews (r) == DNS_R_HINTNXRRSET)
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews/*
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * Error state rankings.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews */
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_SUCCESS 0 /* highest rank */
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_CANCELED 1
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_FAILURE 2
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_NXDOMAIN 3
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_NXRRSET 4
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_UNEXPECTED 5
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_NOTFOUND 6
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define FIND_ERR_MAX 7
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsstatic const char *errnames[] = {
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "success",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "canceled",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "failure",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "nxdomain",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "nxrrset",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "unexpected",
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews "not_found"
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews};
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews#define NEWERR(old, new) (ISC_MIN((old), (new)))
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsstatic isc_result_t find_err_map[FIND_ERR_MAX] = {
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews ISC_R_SUCCESS,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews ISC_R_CANCELED,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews ISC_R_FAILURE,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews DNS_R_NXDOMAIN,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews DNS_R_NXRRSET,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews ISC_R_UNEXPECTED,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews ISC_R_NOTFOUND /* not YET found */
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington};
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void
f0bbac2c0f1afa74b88cab902daf11202ebe7cbdBob HalleyDP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic void
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob HalleyDP(int level, const char *format, ...) {
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley va_list args;
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley va_start(args, format);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley isc_log_vwrite(dns_lctx,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
f969863d54ae3f0ddccce84c6dd054fe1e792cf7Andreas Gustafsson level, format, args);
f969863d54ae3f0ddccce84c6dd054fe1e792cf7Andreas Gustafsson va_end(args);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley}
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic inline dns_ttl_t
1366b7833c86343de278480b9abd71754e418bfaBob Halleyttlclamp(dns_ttl_t ttl) {
b7ebc6a127becc7c3ad93d3b1f143132cf1d3609Andreas Gustafsson if (ttl < ADB_CACHE_MINIMUM)
1366b7833c86343de278480b9abd71754e418bfaBob Halley ttl = ADB_CACHE_MINIMUM;
1366b7833c86343de278480b9abd71754e418bfaBob Halley if (ttl > ADB_CACHE_MAXIMUM)
1366b7833c86343de278480b9abd71754e418bfaBob Halley ttl = ADB_CACHE_MAXIMUM;
1366b7833c86343de278480b9abd71754e418bfaBob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halley return (ttl);
1366b7833c86343de278480b9abd71754e418bfaBob Halley}
b7ebc6a127becc7c3ad93d3b1f143132cf1d3609Andreas Gustafsson
1366b7833c86343de278480b9abd71754e418bfaBob Halley/*
1366b7833c86343de278480b9abd71754e418bfaBob Halley * Requires the adbname bucket be locked and that no entry buckets be locked.
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley *
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley * This code handles A and AAAA rdatasets only.
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley */
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic isc_result_t
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleyimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley isc_stdtime_t now)
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington{
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington isc_result_t result;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_adb_t *adb;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_adbnamehook_t *nh;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_adbnamehook_t *anh;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_rdata_t rdata = DNS_RDATA_INIT;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington struct in_addr ina;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington struct in6_addr in6a;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington isc_sockaddr_t sockaddr;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington int addr_bucket;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington isc_boolean_t new_addresses_added;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington dns_rdatatype_t rdtype;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews unsigned int findoptions;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews INSIST(DNS_ADBNAME_VALID(adbname));
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews adb = adbname->adb;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews INSIST(DNS_ADB_VALID(adb));
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews rdtype = rdataset->type;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews if (rdtype == dns_rdatatype_a)
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews findoptions = DNS_ADBFIND_INET;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews else
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews findoptions = DNS_ADBFIND_INET6;
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews
1e107b3d7b54de5022c3328423164e533afcc15eMark Andrews addr_bucket = DNS_ADB_INVALIDBUCKET;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews new_addresses_added = ISC_FALSE;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews nh = NULL;
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews result = dns_rdataset_first(rdataset);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews while (result == ISC_R_SUCCESS) {
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews dns_rdata_reset(&rdata);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews dns_rdataset_current(rdataset, &rdata);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews if (rdtype == dns_rdatatype_a) {
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews INSIST(rdata.length == 4);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews memcpy(&ina.s_addr, rdata.data, 4);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews isc_sockaddr_fromin(&sockaddr, &ina, 0);
d6fe7ba94969ee51a3f4298a735fbc6e11691ad8Mark Andrews } else {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington INSIST(rdata.length == 16);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington memcpy(in6a.s6_addr, rdata.data, 16);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington }
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington INSIST(nh == NULL);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington 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;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington result = ISC_R_NOMEMORY;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington goto fail;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington }
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington entry->sockaddr = sockaddr;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington entry->refcnt = 1;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington nh->entry = entry;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington link_entry(adb, addr_bucket, entry);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington } else {
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington for (anh = ISC_LIST_HEAD(adbname->v4);
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington anh != NULL;
41e50ece3804f8483a9ce5abbbe49415a6a64645Brian Wellington anh = ISC_LIST_NEXT(anh, plink))
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (anh->entry == foundentry)
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence break;
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 if (anh == NULL) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley foundentry->refcnt++;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews nh->entry = foundentry;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley } else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbnamehook(adb, &nh);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley new_addresses_added = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (nh != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (rdtype == dns_rdatatype_a)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_APPEND(adbname->v4, nh, plink);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley else
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley ISC_LIST_APPEND(adbname->v6, nh, plink);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley nh = NULL;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley result = dns_rdataset_next(rdataset);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fail:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (nh != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbnamehook(adb, &nh);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley if (rdataset->trust == dns_trust_glue ||
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley rdataset->trust == dns_trust_additional)
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley rdataset->ttl = ADB_CACHE_MINIMUM;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley rdataset->ttl = ttlclamp(rdataset->ttl);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (rdtype == dns_rdatatype_a) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley adbname->expire_v4, now + rdataset->ttl);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley now + rdataset->ttl);
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews adbname->expire_v6, now + rdataset->ttl);
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley now + rdataset->ttl);
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews }
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (new_addresses_added) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Lie a little here. This is more or less so code that cares
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * can find out if any new information was added or not.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (ISC_R_SUCCESS);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews/*
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Requires the name's bucket be locked.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic isc_boolean_t
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_adbname_t *name;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews isc_boolean_t result = ISC_FALSE;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews isc_boolean_t result4, result6;
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));
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews adb = name->adb;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews INSIST(DNS_ADB_VALID(adb));
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley DP(DEF_LEVEL, "killing name %p", name);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews /*
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * If we're dead already, just check to see if we should go
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * away now or not.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (NAME_DEAD(name) && !NAME_FETCH(name)) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley result = unlink_name(adb, name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbname(adb, &name);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (result)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
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 result4 = clean_namehooks(adb, &name->v4);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley result6 = clean_namehooks(adb, &name->v6);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley clean_target(adb, &name->target);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley result = ISC_TF(result4 || result6);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * 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 INSIST(result == ISC_FALSE);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence result = unlink_name(adb, name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley free_adbname(adb, &name);
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 if (result)
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley result = dec_adb_irefcnt(adb);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley } else {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley name->flags |= NAME_IS_DEAD;
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley cancel_fetches_at_name(name);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley }
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley return (result);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley}
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley/*
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * Requires the name's bucket be locked and no entry buckets be locked.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉static isc_boolean_t
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halleycheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley isc_boolean_t overmem)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adb_t *adb;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley isc_boolean_t expire;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence isc_boolean_t result4 = ISC_FALSE;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley isc_boolean_t result6 = ISC_FALSE;
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 if (overmem) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley isc_uint32_t val;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley isc_random_get(&val);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley expire = ISC_TF((val % 4) == 0);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley } else
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley expire = ISC_FALSE;
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) &&
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley (expire || 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 result4 = clean_namehooks(adb, &name->v4);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->partial_result &= ~DNS_ADBFIND_INET;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->expire_v4 = INT_MAX;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->fetch_err = FIND_ERR_UNEXPECTED;
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) &&
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley (expire || EXPIRE_OK(name->expire_v6, now))) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (NAME_HAS_V6(name)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley DP(DEF_LEVEL, "expiring v6 for name %p", name);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews result6 = clean_namehooks(adb, &name->v6);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->partial_result &= ~DNS_ADBFIND_INET6;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->expire_v6 = INT_MAX;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name->fetch6_err = FIND_ERR_UNEXPECTED;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley /*
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley * Check to see if we need to remove the alias target.
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley */
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley if (expire || EXPIRE_OK(name->expire_target, now)) {
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley clean_target(adb, &name->target);
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley name->expire_target = INT_MAX;
6957b87f931bb110ba4d0adf495932691ba550b1Bob Halley }
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews return (ISC_TF(result4 || result6));
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews}
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews/*
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the name's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews */
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline void
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewslink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews ISC_LIST_PREPEND(adb->names[bucket], name, plink);
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews name->lock_bucket = bucket;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews adb->name_refcnt[bucket]++;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews}
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews/*
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews * Requires the name's bucket be locked.
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews */
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsstatic inline isc_boolean_t
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrewsunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews int bucket;
39c7fc7e00af20144b94ef332943f62c1b3a622fMark Andrews isc_boolean_t result = ISC_FALSE;
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);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews adb->name_refcnt[bucket]--;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews result = ISC_TRUE;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews return (result);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews}
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews/*
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Requires the entry's bucket be locked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline void
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewslink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews entry->lock_bucket = bucket;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews adb->entry_refcnt[bucket]++;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews}
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews/*
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * Requires the entry's bucket be locked.
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews */
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsstatic inline isc_boolean_t
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrewsunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews int bucket;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews isc_boolean_t result = ISC_FALSE;
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);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews INSIST(adb->entry_refcnt[bucket] > 0);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews adb->entry_refcnt[bucket]--;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews result = ISC_TRUE;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews return (result);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline void
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews UNLOCK(have);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews LOCK(want);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews LOCK(have);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews}
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews/*
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * The ADB _MUST_ be locked before calling. Also, exit conditions must be
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson * checked after calling this function.
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson */
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafssonstatic isc_boolean_t
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafssonshutdown_names(dns_adb_t *adb) {
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson int bucket;
aa8e34546c1e51e69f5a4935d28cb0c543e7401aAndreas Gustafsson isc_boolean_t result = ISC_FALSE;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews dns_adbname_t *name;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews dns_adbname_t *next_name;
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews for (bucket = 0; bucket < NBUCKETS; bucket++) {
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews LOCK(&adb->namelocks[bucket]);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley adb->name_sd[bucket] = ISC_TRUE;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley name = ISC_LIST_HEAD(adb->names[bucket]);
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 if (name == NULL) {
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This bucket has no names. We must decrement the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * irefcnt ourselves, since it will not be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * automatically triggered by a name being unlinked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(result == ISC_FALSE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Run through the list. For each name, clean up finds
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * found there, and cancel any fetches running. When
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 * all the fetches are canceled, the name will destroy
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley * itself.
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley */
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley while (name != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley next_name = ISC_LIST_NEXT(name, plink);
29b487b0a458d655f0aad9257ca46021f4903d08Bob Halley INSIST(result == ISC_FALSE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = kill_name(&name,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DNS_EVENT_ADBSHUTDOWN);
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington name = next_name;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence }
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley }
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington UNLOCK(&adb->namelocks[bucket]);
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington }
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington return (result);
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley}
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley/*
a8e4c27d2c00e831d1eb7260e3f455d97907d799Bob Halley * The ADB _MUST_ be locked before calling. Also, exit conditions must be
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington * checked after calling this function.
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington */
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrewsstatic isc_boolean_t
6ebf3dbb33606411c17cbb75f7407e1457ca4b68Mark Andrewsshutdown_entries(dns_adb_t *adb) {
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews int bucket;
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews isc_boolean_t result = ISC_FALSE;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington dns_adbentry_t *entry;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington dns_adbentry_t *next_entry;
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley for (bucket = 0; bucket < NBUCKETS; bucket++) {
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley LOCK(&adb->entrylocks[bucket]);
5d661f0bde49c68d33eb1146d60058782aca50a7Bob Halley adb->entry_sd[bucket] = ISC_TRUE;
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington
d2b77d720f1dcdc85a761b1de1a94d32fbdef81aBrian Wellington entry = ISC_LIST_HEAD(adb->entries[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry == NULL) {
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This bucket has no entries. We must decrement the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * irefcnt ourselves, since it will not be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * automatically triggered by an entry being unlinked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Run through the list. Cleanup any entries not
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * associated with names, and which are not in use.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (entry != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley next_entry = ISC_LIST_NEXT(entry, plink);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (entry->refcnt == 0 &&
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley entry->expires != 0) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = unlink_entry(adb, entry);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley free_adbentry(adb, &entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence entry = next_entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[bucket]);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (result);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley}
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * Name bucket must be locked
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycancel_fetches_at_name(dns_adbname_t *name) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_FETCH_A(name))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_resolver_cancelfetch(name->fetch_a->fetch);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_FETCH_AAAA(name))
3740b569ae76295b941d57a724a43beb75b533baBob Halley dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley/*
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * Assumes the name bucket is locked.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic isc_boolean_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbnamehook_t *namehook;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int addr_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t result = ISC_FALSE;
0370190a682a3933119784a1023b1b9a51c66fa3Bob Halley
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson addr_bucket = DNS_ADB_INVALIDBUCKET;
f083a44415365f6464f8bd35f439dc13ee0b684fMark Andrews namehook = ISC_LIST_HEAD(*namehooks);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley while (namehook != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley /*
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson * Clean up the entry if needed.
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson */
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson entry = namehook->entry;
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson if (entry != NULL) {
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson INSIST(DNS_ADBENTRY_VALID(entry));
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson
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 }
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson result = dec_entry_refcnt(adb, entry, ISC_FALSE);
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson }
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson /*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * Free the namehook
0736cce338052ed219bcb5147046cb78d0361506Andreas Gustafsson */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namehook->entry = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(*namehooks, namehook, plink);
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley free_adbnamehook(adb, &namehook);
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley namehook = ISC_LIST_HEAD(*namehooks);
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley }
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halleystatic void
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsclean_target(dns_adb_t *adb, dns_name_t *target) {
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley if (dns_name_countlabels(target) > 0) {
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_name_free(target, adb->mctx);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_name_init(target, NULL);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley }
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley}
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halleystatic isc_result_t
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob 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;
fff9a37a0355e4181b3132370a173d1dbb5a0f3cBob Halley unsigned int nlabels, nbits;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence int order;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_rdata_t rdata = DNS_RDATA_INIT;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_fixedname_t fixed1, fixed2;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_name_t *prefix, *new_target;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley REQUIRE(dns_name_countlabels(target) == 0);
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (rdataset->type == dns_rdatatype_cname) {
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley dns_rdata_cname_t cname;
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley
bd53af8229e28cfec8bfd9572b4d31514ea97f48Bob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Copy the CNAME's target into the target name.
6585d8782b2e20caf7b71f264afa4beb8a3fdf98Brian Wellington */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = dns_rdataset_first(rdataset);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (result != ISC_R_SUCCESS)
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews return (result);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_rdataset_current(rdataset, &rdata);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = dns_rdata_tostruct(&rdata, &cname, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_dup(&cname.cname, adb->mctx, target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_freestruct(&cname);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_dname_t dname;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley INSIST(rdataset->type == dns_rdatatype_dname);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews namereln = dns_name_fullcompare(name, fname, &order,
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews &nlabels, &nbits);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence INSIST(namereln == dns_namereln_subdomain);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews /*
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer * Get the target name of the DNAME.
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer */
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews result = dns_rdataset_first(rdataset);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews if (result != ISC_R_SUCCESS)
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews return (result);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_rdataset_current(rdataset, &rdata);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews result = dns_rdata_tostruct(&rdata, &dname, NULL);
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater if (result != ISC_R_SUCCESS)
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer return (result);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Construct the new target name.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence dns_fixedname_init(&fixed1);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews prefix = dns_fixedname_name(&fixed1);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_fixedname_init(&fixed2);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews new_target = dns_fixedname_name(&fixed2);
f257e9369c27578eb87077923dc010a6614e2a7aMark Andrews result = dns_name_split(name, nlabels, nbits, prefix, NULL);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews if (result != ISC_R_SUCCESS) {
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews dns_rdata_freestruct(&dname);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_concatenate(prefix, &dname.dname, new_target,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley NULL);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley dns_rdata_freestruct(&dname);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_dup(new_target, adb->mctx, target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
3e6753a75350a990d2723a936657e5dabc1c5e30Brian Wellington return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Assumes nothing is locked, since this is called by the client.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyevent_free(isc_event_t *event) {
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews dns_adbfind_t *find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(event != NULL);
216030f2849b0812910fbc1817ca17208a112663Mark Andrews find = event->ev_destroy_arg;
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews INSIST(DNS_ADBFIND_VALID(find));
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews LOCK(&find->lock);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews find->flags |= FIND_EVENT_FREED;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley event->ev_destroy_arg = NULL;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley UNLOCK(&find->lock);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes the name bucket is locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int addrs)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_event_t *ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *task;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *find;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adbfind_t *next_find;
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence isc_boolean_t process;
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence unsigned int wanted, notify;
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater DP(ENTER_LEVEL,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence name, evtype, addrs);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley find = ISC_LIST_HEAD(name->finds);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley while (find != NULL) {
ce3761f64d3d734cc94605026985898900ecc474Bob Halley LOCK(&find->lock);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence next_find = ISC_LIST_NEXT(find, plink);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence process = ISC_FALSE;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley notify = wanted & addrs;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley switch (evtype) {
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley case DNS_EVENT_ADBMOREADDRESSES:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley if ((notify) != 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->flags &= ~addrs;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley case DNS_EVENT_ADBNOMOREADDRESSES:
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find->flags &= ~addrs;
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
82a30bf0a0d1a8120e8c021966a5275eacb9ed35David Lawrence if (wanted == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley default:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->flags &= ~addrs;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley process = ISC_TRUE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (process) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "cfan: processing find %p", find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Unlink the find from the name, letting the caller
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * call dns_adb_destroyfind() on it to clean it up
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob 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
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!FIND_EVENTSENT(find));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev = &find->event;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley task = ev->ev_sender;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->ev_sender = find;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley find->result_v4 = find_err_map[name->fetch_err];
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley find->result_v6 = find_err_map[name->fetch6_err];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->ev_type = evtype;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ev->ev_destroy = event_free;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev->ev_destroy_arg = find;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley DP(DEF_LEVEL,
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews "sending event %p to task %p for find %p",
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ev, task, find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_task_sendanddetach(&task, (isc_event_t **)&ev);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "cfan: skipping find %p", find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&find->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find = next_find;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
0ad5cb4782cd419b089bcab28d2fd9e140dbcc59Mark Andrews
0ad5cb4782cd419b089bcab28d2fd9e140dbcc59Mark Andrewsstatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycheck_exit(dns_adb_t *adb) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_event_t *event;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The caller must be holding the adb lock.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (adb->shutting_down) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * If there aren't any external references either, we're
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * done. Send the control event to initiate shutdown.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!adb->cevent_sent); /* Sanity check. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event = &adb->cevent;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_send(adb->task, &event);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley adb->cevent_sent = ISC_TRUE;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline isc_boolean_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydec_adb_irefcnt(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_event_t *event;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *etask;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t result = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley LOCK(&adb->reflock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(adb->irefcnt > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->irefcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (adb->irefcnt == 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event = ISC_LIST_HEAD(adb->whenshutdown);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (event != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley etask = event->ev_sender;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews event->ev_sender = adb;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews isc_task_sendanddetach(&etask, &event);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews event = ISC_LIST_HEAD(adb->whenshutdown);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (adb->irefcnt == 0 && adb->erefcnt == 0)
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews result = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->reflock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
af3459443207253f197cd684567bfea0311ab76dMark Andrews}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinc_adb_irefcnt(dns_adb_t *adb) {
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley LOCK(&adb->reflock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->irefcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->reflock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyinc_adb_erefcnt(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->reflock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->erefcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->reflock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
af3459443207253f197cd684567bfea0311ab76dMark Andrews int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline isc_boolean_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley int bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_boolean_t destroy_entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t result = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (lock)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(entry->refcnt > 0);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley entry->refcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley destroy_entry = ISC_FALSE;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (entry->refcnt == 0 &&
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley (adb->entry_sd[bucket] || entry->expires == 0)) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews destroy_entry = ISC_TRUE;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = unlink_entry(adb, entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[bucket]);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (!destroy_entry)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbentry(adb, &entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result =dec_adb_irefcnt(adb);
0874abad14e3e9ecfc3dc1a1a2b9969f2f027724Mark Andrews
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley}
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsstatic inline dns_adbname_t *
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrewsnew_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_adbname_t *name;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews name = isc_mempool_get(adb->nmp);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (name == NULL)
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews return (NULL);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews dns_name_init(&name->name, NULL);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_mempool_put(adb->nmp, name);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley return (NULL);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_name_init(&name->target, NULL);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name->magic = DNS_ADBNAME_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->adb = adb;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name->partial_result = 0;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name->flags = 0;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence name->expire_v4 = INT_MAX;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name->expire_v6 = INT_MAX;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name->expire_target = INT_MAX;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->chains = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->lock_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(name->v4);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley ISC_LIST_INIT(name->v6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->fetch_a = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->fetch_aaaa = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->fetch_err = FIND_ERR_UNEXPECTED;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->fetch6_err = FIND_ERR_UNEXPECTED;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ISC_LIST_INIT(name->finds);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(name, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews return (name);
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline void
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsfree_adbname(dns_adb_t *adb, dns_adbname_t **name) {
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));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews INSIST(ISC_LIST_EMPTY(n->finds));
030aafe4114875ff659fcf83db6d05846470fb3eMark 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;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_name_free(&n->name, adb->mctx);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews isc_mempool_put(adb->nmp, n);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline dns_adbnamehook_t *
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrewsnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_adbnamehook_t *nh;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews nh = isc_mempool_get(adb->nhmp);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (nh == NULL)
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews return (NULL);
cffe96e26744abcf33494837b234219046a631d8Mark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews nh->magic = DNS_ADBNAMEHOOK_MAGIC;
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater nh->entry = entry;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews ISC_LINK_INIT(nh, plink);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews return (nh);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsstatic inline void
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrewsfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_adbnamehook_t *nh;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews nh = *namehook;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews *namehook = NULL;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews INSIST(nh->entry == NULL);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff INSIST(!ISC_LINK_LINKED(nh, plink));
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley nh->magic = 0;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt isc_mempool_put(adb->nhmp, nh);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt}
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntstatic inline dns_adbzoneinfo_t *
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Huntnew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt dns_adbzoneinfo_t *zi;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt zi = isc_mempool_get(adb->zimp);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt if (zi == NULL)
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt return (NULL);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt dns_name_init(&zi->zone, NULL);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt isc_mempool_put(adb->zimp, zi);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi->magic = DNS_ADBZONEINFO_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi->lame_timer = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(zi, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (zi);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley}
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrewsstatic inline void
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrewsfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews dns_adbzoneinfo_t *zi;
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt zi = *zoneinfo;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt *zoneinfo = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(zi, plink));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_free(&zi->zone, adb->mctx);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley zi->magic = 0;
39f029558da291314b32291bd26b66891b5ade91Andreas Gustafsson
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->zimp, zi);
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews}
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrewsstatic inline dns_adbentry_t *
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrewsnew_adbentry(dns_adb_t *adb) {
030aafe4114875ff659fcf83db6d05846470fb3eMark Andrews dns_adbentry_t *e;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_uint32_t r;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e = isc_mempool_get(adb->emp);
39f029558da291314b32291bd26b66891b5ade91Andreas Gustafsson if (e == NULL)
39f029558da291314b32291bd26b66891b5ade91Andreas Gustafsson return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->magic = DNS_ADBENTRY_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->lock_bucket = DNS_ADB_INVALIDBUCKET;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->refcnt = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->flags = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence isc_random_get(&r);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->srtt = (r & 0x1f) + 1;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->expires = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence ISC_LIST_INIT(e->zoneinfo);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence ISC_LINK_INIT(e, plink);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence return (e);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence}
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencestatic inline void
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencefree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence dns_adbentry_t *e;
38d2d0e9326a2f70b5893302b89a26978b539405Bob 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
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence INSIST(e->refcnt == 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(e, plink));
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence e->magic = 0;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence zi = ISC_LIST_HEAD(e->zoneinfo);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (zi != NULL) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence free_adbzoneinfo(adb, &zi);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence zi = ISC_LIST_HEAD(e->zoneinfo);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->emp, e);
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence}
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencestatic inline dns_adbfind_t *
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrencenew_adbfind(dns_adb_t *adb) {
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence dns_adbfind_t *h;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_result_t result;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h = isc_mempool_get(adb->ahmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (h == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * Public members.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob 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 h->result_v4 = ISC_R_UNEXPECTED;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt h->result_v6 = ISC_R_UNEXPECTED;
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt ISC_LINK_INIT(h, publink);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt ISC_LINK_INIT(h, plink);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt ISC_LIST_INIT(h->list);
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt h->adbname = NULL;
47c5b8af920a93763c97d9a93ea1fd766961a5b3Evan Hunt h->name_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
cedd0ab1e812ec7cf05d57c3e602db41b79f0a2aAndreas Gustafsson /*
cedd0ab1e812ec7cf05d57c3e602db41b79f0a2aAndreas Gustafsson * 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__,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff "isc_mutex_init failed in new_adbfind()");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->ahmp, h);
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley NULL, NULL, h);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
bf9b852c3eaf2c9847f926751b57a06f1ae3d72aEvan Hunt inc_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->magic = DNS_ADBFIND_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (h);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
b9a2c5ddf99250e851f45a606cdbe3e3fc4beccaBob Halleystatic inline dns_adbfetch_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbfetch(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch_t *f;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f = isc_mempool_get(adb->afmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews f->magic = 0;
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews f->namehook = NULL;
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews f->entry = NULL;
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews f->fetch = NULL;
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews
7755f5932a3e59d0c6a2506cc94519de92b91ca6Mark Andrews f->namehook = new_adbnamehook(adb, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->namehook == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->entry = new_adbentry(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->entry == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
fcb54ce0a4f7377486df5bec83b3aa4711bf4131Mark Andrews dns_rdataset_init(&f->rdataset);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->magic = DNS_ADBFETCH_MAGIC;
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (f);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley err:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->namehook != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbnamehook(adb, &f->namehook);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->entry != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbentry(adb, &f->entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->afmp, f);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch_t *f;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
051d1879fe1f4eeb8908657a1dae0045ef41eb51David Lawrence f = *fetch;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *fetch = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->namehook != NULL)
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews free_adbnamehook(adb, &f->namehook);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (f->entry != NULL)
4cf50d58d65bbe602b68e843744799534e2cf2c0Mark Andrews free_adbentry(adb, &f->entry);
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews if (dns_rdataset_isassociated(&f->rdataset))
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews dns_rdataset_disassociate(&f->rdataset);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews isc_mempool_put(adb->afmp, f);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline isc_boolean_t
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonfree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbfind_t *find;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington find = *findp;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington *findp = NULL;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(!FIND_HAS_ADDRS(find));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(!ISC_LINK_LINKED(find, publink));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(!ISC_LINK_LINKED(find, plink));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(find->adbname == NULL);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington find->magic = 0;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington DESTROYLOCK(&find->lock);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington isc_mempool_put(adb->ahmp, find);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (dec_adb_irefcnt(adb));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington/*
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Copy bits from the entry into the newly allocated addrinfo. The entry
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * must be locked, and the reference count must be bumped up by one
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * if this function returns a valid pointer.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington */
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic inline dns_adbaddrinfo_t *
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonnew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbaddrinfo_t *ai;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai = isc_mempool_get(adb->aimp);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (ai == NULL)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (NULL);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai->magic = DNS_ADBADDRINFO_MAGIC;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai->sockaddr = entry->sockaddr;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington isc_sockaddr_setport(&ai->sockaddr, port);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai->srtt = entry->srtt;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai->flags = entry->flags;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai->entry = entry;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ISC_LINK_INIT(ai, publink);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (ai);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic inline void
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbaddrinfo_t *ai;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai = *ainfo;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington *ainfo = NULL;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(ai->entry == NULL);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington INSIST(!ISC_LINK_LINKED(ai, publink));
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington ai->magic = 0;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington isc_mempool_put(adb->aimp, ai);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington/*
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Search for the name. NOTE: The bucket is kept locked on both
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington * success and failure, so it must always be unlocked by the caller!
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington *
45e22378fc8fc87fb96391ffd23d4402fe003dc0Brian Wellington * On the first call to this function, *bucketp must be set to
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * DNS_ADB_INVALIDBUCKET.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington */
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic inline dns_adbname_t *
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonfind_name_and_lock(dns_adb_t *adb, dns_name_t *name,
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington unsigned int options, int *bucketp)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington{
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington dns_adbname_t *adbname;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington int bucket;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington bucket = dns_fullname_hash(name, ISC_FALSE) % NBUCKETS;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (*bucketp == DNS_ADB_INVALIDBUCKET) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington LOCK(&adb->namelocks[bucket]);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington *bucketp = bucket;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington } else if (*bucketp != bucket) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington UNLOCK(&adb->namelocks[*bucketp]);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington LOCK(&adb->namelocks[bucket]);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington *bucketp = bucket;
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington }
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington adbname = ISC_LIST_HEAD(adb->names[bucket]);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington while (adbname != NULL) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (!NAME_DEAD(adbname)) {
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington if (dns_name_equal(name, &adbname->name)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington && GLUEHINT_OK(adbname, options)
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington && STARTATZONE_MATCHES(adbname, options))
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (adbname);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington }
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington adbname = ISC_LIST_NEXT(adbname, plink);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington }
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington return (NULL);
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington}
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington/*
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * Search for the address. NOTE: The bucket is kept locked on both
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * success and failure, so it must always be unlocked by the caller.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington *
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * On the first call to this function, *bucketp must be set to
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * later calls (within the same "lock path") it can be left alone, so
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * if this function is called multiple times locking is only done if
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington * the bucket changes.
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellington */
7d7215baf845937786f3ceb64b582e3aeaa58a2cBrian Wellingtonstatic inline dns_adbentry_t *
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleyfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_adbentry_t *entry;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley int bucket;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (*bucketp == DNS_ADB_INVALIDBUCKET) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley LOCK(&adb->entrylocks[bucket]);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley *bucketp = bucket;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley } else if (*bucketp != bucket) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley UNLOCK(&adb->entrylocks[*bucketp]);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington LOCK(&adb->entrylocks[bucket]);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley *bucketp = bucket;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley entry = ISC_LIST_HEAD(adb->entries[bucket]);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley while (entry != NULL) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (isc_sockaddr_equal(addr, &entry->sockaddr))
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley return (entry);
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews entry = ISC_LIST_NEXT(entry, plink);
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews }
d8d489cd8efc45b06a232ac07a636b3d36cc7e8fMark Andrews
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley return (NULL);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley/*
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley * Entry bucket MUST be locked!
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellingtonstatic isc_boolean_t
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleyentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_stdtime_t now)
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley{
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley dns_adbzoneinfo_t *zi, *next_zi;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley isc_boolean_t is_bad;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff is_bad = ISC_FALSE;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington zi = ISC_LIST_HEAD(entry->zoneinfo);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (zi == NULL)
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington return (ISC_FALSE);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley while (zi != NULL) {
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley next_zi = ISC_LIST_NEXT(zi, plink);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington /*
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * Has the entry expired?
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington */
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley if (zi->lame_timer < now) {
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington free_adbzoneinfo(adb, &zi);
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington }
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington /*
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington * Order tests from least to most expensive.
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington */
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (zi != NULL && !is_bad) {
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington if (dns_name_equal(zone, &zi->zone))
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley is_bad = ISC_TRUE;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley }
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley zi = next_zi;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley }
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley return (is_bad);
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley}
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencestatic void
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halleycopy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley dns_adbname_t *name, isc_stdtime_t now)
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley{
a0abd77baacdd97272356faab5efc79dd1483077Bob Halley dns_adbnamehook_t *namehook;
6e952e42e56e01e4b49d4a41a40a4e8f4cb0e8bfBob Halley dns_adbaddrinfo_t *addrinfo;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington dns_adbentry_t *entry;
e22d03eb45fdc504bca3d6227725d45a3ff7d192Brian Wellington int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence bucket = DNS_ADB_INVALIDBUCKET;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (find->options & DNS_ADBFIND_INET) {
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews namehook = ISC_LIST_HEAD(name->v4);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (namehook != NULL) {
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley entry = namehook->entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->entrylocks[bucket]);
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (!FIND_RETURNLAME(find)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley && entry_is_bad_for_zone(adb, entry, zone, now)) {
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley find->options |= DNS_ADBFIND_LAMEPRUNED;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley goto nextv4;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley addrinfo = new_adbaddrinfo(adb, entry, find->port);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addrinfo == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->partial_result |= DNS_ADBFIND_INET;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews goto out;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews }
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews /*
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews * Found a valid entry. Add it to the find's list.
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews inc_entry_refcnt(adb, entry, ISC_FALSE);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews ISC_LIST_APPEND(find->list, addrinfo, publink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addrinfo = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nextv4:
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence UNLOCK(&adb->entrylocks[bucket]);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence bucket = DNS_ADB_INVALIDBUCKET;
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence namehook = ISC_LIST_NEXT(namehook, plink);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence }
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence if (find->options & DNS_ADBFIND_INET6) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley namehook = ISC_LIST_HEAD(name->v6);
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence while (namehook != NULL) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley entry = namehook->entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry_is_bad_for_zone(adb, entry, zone, now))
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff goto nextv6;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = new_adbaddrinfo(adb, entry, find->port);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (addrinfo == NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find->partial_result |= DNS_ADBFIND_INET6;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto out;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson /*
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews * Found a valid entry. Add it to the find's list.
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews inc_entry_refcnt(adb, entry, ISC_FALSE);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_APPEND(find->list, addrinfo, publink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley addrinfo = NULL;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley nextv6:
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley UNLOCK(&adb->entrylocks[bucket]);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews bucket = DNS_ADB_INVALIDBUCKET;
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews namehook = ISC_LIST_NEXT(namehook, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews out:
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews if (bucket != DNS_ADB_INVALIDBUCKET)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->entrylocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrewsstatic void
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrewsshutdown_task(isc_task_t *task, isc_event_t *ev) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence dns_adb_t *adb;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNUSED(task);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley adb = ev->ev_arg;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley INSIST(DNS_ADB_VALID(adb));
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /*
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley * Kill the timer, and then the ADB itself. Note that this implies
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley * 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()
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * this) badness will occur.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->lock);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_timer_detach(&adb->timer);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->lock);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_event_free(&ev);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley destroy(adb);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Name bucket must be locked; adb may be locked; no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic isc_boolean_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycheck_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_result_t result = ISC_FALSE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence name = *namep;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews return (result);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (NAME_FETCH(name))
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson return (result);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson if (!EXPIRE_OK(name->expire_v4, now))
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson return (result);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson if (!EXPIRE_OK(name->expire_v6, now))
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson return (result);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson if (!EXPIRE_OK(name->expire_target, now))
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson return (result);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer /*
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * The name is empty. Delete it.
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *namep = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Our caller, or one of its callers, will be calling check_exit() at
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * some point, so we don't need to do it here.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
b5819ca3eb4abf5b159a91ec2a413980b2cd0732Mark Andrews/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Entry bucket must be locked; adb may be locked; no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrewsstatic isc_boolean_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycheck_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley{
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbentry_t *entry;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_boolean_t expire;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_boolean_t result = ISC_FALSE;
31efcf64783b32a97a7dd60169375edb7d580376Brian Wellington
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson entry = *entryp;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (entry->refcnt != 0)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (adb->overmem) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_uint32_t val;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews isc_random_get(&val);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews expire = ISC_TF((val % 4) == 0);
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence } else
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence expire = ISC_FALSE;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
ecb6c5782ea248307e86c4bceac6c371d27576a6David Lawrence if (entry->expires == 0 || (! expire && entry->expires > now))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley /*
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley * The entry is not in use. Delete it.
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley */
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley DP(DEF_LEVEL, "killing entry %p", entry);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley INSIST(ISC_LINK_LINKED(entry, plink));
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley result = unlink_entry(adb, entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_adbentry(adb, &entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (result)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dec_adb_irefcnt(adb);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *entryp = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * ADB must be locked, and no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic isc_boolean_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *next_name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_result_t result = ISC_FALSE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley LOCK(&adb->namelocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (adb->name_sd[bucket]) {
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews UNLOCK(&adb->namelocks[bucket]);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews return (result);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews }
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews name = ISC_LIST_HEAD(adb->names[bucket]);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews while (name != NULL) {
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews next_name = ISC_LIST_NEXT(name, plink);
514aeac2acbbe2b77ff3c4e310617523cf5651c5Mark Andrews INSIST(result == ISC_FALSE);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews result = check_expire_namehooks(name, now, adb->overmem);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (!result)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley result = check_expire_name(&name, now);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name = next_name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->namelocks[bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff * ADB must be locked, and no other locks held.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic isc_boolean_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley dns_adbentry_t *entry, *next_entry;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_boolean_t result = ISC_FALSE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence LOCK(&adb->entrylocks[bucket]);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff entry = ISC_LIST_HEAD(adb->entries[bucket]);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson while (entry != NULL) {
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson next_entry = ISC_LIST_NEXT(entry, plink);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer INSIST(result == ISC_FALSE);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson result = check_expire_entry(adb, &entry, now);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson entry = next_entry;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson }
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson UNLOCK(&adb->entrylocks[bucket]);
425028583845247a2e09532a88e58165e82924f4Brian Wellington return (result);
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson}
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssonstatic void
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafssontimer_cleanup(isc_task_t *task, isc_event_t *ev) {
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson dns_adb_t *adb;
3fe45d9897459da9c78263ae709e5c611e622243Andreas Gustafsson isc_stdtime_t now;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int i;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_interval_t interval;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews UNUSED(task);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews adb = ev->ev_arg;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews INSIST(DNS_ADB_VALID(adb));
571469b0a58b011966bbea91ca992928554d6ca1David Lawrence
ec0613a09776abfb6409506495a7ccbe72294938Andreas Gustafsson LOCK(&adb->lock);
ec0613a09776abfb6409506495a7ccbe72294938Andreas Gustafsson
ec0613a09776abfb6409506495a7ccbe72294938Andreas Gustafsson isc_stdtime_get(&now);
0c264c909424f855e7e3b7ce7bb21f650609e768Brian Wellington
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley for (i = 0; i < CLEAN_BUCKETS; i++) {
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Call our cleanup routines.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley RUNTIME_CHECK(cleanup_names(adb, adb->next_cleanbucket, now) ==
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_FALSE);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley RUNTIME_CHECK(cleanup_entries(adb, adb->next_cleanbucket, now)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley == ISC_FALSE);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Set the next bucket to be cleaned.
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley */
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley adb->next_cleanbucket++;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley if (adb->next_cleanbucket >= NBUCKETS) {
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley adb->next_cleanbucket = 0;
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley#ifdef DUMP_ADB_AFTER_CLEANING
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews dump_adb(adb, stdout, ISC_TRUE);
6ebf3dbb33606411c17cbb75f7407e1457ca4b68Mark Andrews#endif
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews }
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley /*
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley * Reset the timer.
ace0c1b3f4bc3e6951b98428bde78149ad599d33Bob Halley * XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
0c264c909424f855e7e3b7ce7bb21f650609e768Brian Wellington * ISC_R_NOMEMORY, but it isn't clear what could be done here
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * if either one of those things happened.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews interval = adb->tick_interval;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews if (adb->overmem)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_interval_set(&interval, 0, 1);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews (void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews &interval, ISC_FALSE);
29daf5bc7738f1cdab7914562269e1129c81acdcBrian Wellington
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews UNLOCK(&adb->lock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_event_free(&ev);
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington}
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellingtonstatic void
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellingtondestroy(dns_adb_t *adb) {
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington adb->magic = 0;
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington /*
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews * The timer is already dead, from the task's shutdown callback.
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews */
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews isc_task_detach(&adb->task);
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews isc_mempool_destroy(&adb->nmp);
a41d348e14b0465c6444cdfd2d59f9370fd44fe8Mark Andrews isc_mempool_destroy(&adb->nhmp);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mempool_destroy(&adb->zimp);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mempool_destroy(&adb->emp);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mempool_destroy(&adb->ahmp);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mempool_destroy(&adb->aimp);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mempool_destroy(&adb->afmp);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington
d780c35e54f877df716e28db3e19d722cec44aa7Brian Wellington DESTROYLOCK(&adb->reflock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews DESTROYLOCK(&adb->lock);
1c33761bcff783815a952e6e1fb9d5e07a3e1363Brian Wellington DESTROYLOCK(&adb->mplock);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff}
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews/*
1c33761bcff783815a952e6e1fb9d5e07a3e1363Brian Wellington * Public functions.
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrewsisc_result_t
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleydns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews{
518be7faab2498c795e6dc9bb25ac10ca38b3a8dMark Andrews dns_adb_t *adb;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_result_t result;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley int i;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley REQUIRE(mem != NULL);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley REQUIRE(view != NULL);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley REQUIRE(timermgr != NULL);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley REQUIRE(taskmgr != NULL);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews REQUIRE(newadb != NULL && *newadb == NULL);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
ce3761f64d3d734cc94605026985898900ecc474Bob Halley adb = isc_mem_get(mem, sizeof(dns_adb_t));
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (adb == NULL)
ce3761f64d3d734cc94605026985898900ecc474Bob Halley return (ISC_R_NOMEMORY);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Initialize things here that cannot fail, and especially things
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * that must be NULL for the error return to work properly.
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews */
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews adb->magic = 0;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->erefcnt = 1;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->irefcnt = 0;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->nmp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->nhmp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->zimp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->emp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->ahmp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->aimp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->afmp = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->task = NULL;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adb->timer = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->mctx = NULL;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->view = view;
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews adb->timermgr = timermgr;
8f9664521724eefc39728c092d0bc6be527e1496Mark Andrews adb->taskmgr = taskmgr;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley adb->next_cleanbucket = 0;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
ce3761f64d3d734cc94605026985898900ecc474Bob Halley DNS_EVENT_ADBCONTROL, shutdown_task, adb,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence adb, NULL, NULL);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley adb->cevent_sent = ISC_FALSE;
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews adb->shutting_down = ISC_FALSE;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley adb->overmem = ISC_FALSE;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley ISC_LIST_INIT(adb->whenshutdown);
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mem_attach(mem, &adb->mctx);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer result = isc_mutex_init(&adb->lock);
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer if (result != ISC_R_SUCCESS)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley goto fail0b;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_mutex_init(&adb->mplock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (result != ISC_R_SUCCESS)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley goto fail0c;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_mutex_init(&adb->reflock);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (result != ISC_R_SUCCESS)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley goto fail0d;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Initialize the bucket locks for names and elements.
dc570b92f6cc60def4207733c7a194fbb69a4399Michael Sawyer * May as well initialize the list heads, too.
425028583845247a2e09532a88e58165e82924f4Brian Wellington */
425028583845247a2e09532a88e58165e82924f4Brian Wellington result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
425028583845247a2e09532a88e58165e82924f4Brian Wellington if (result != ISC_R_SUCCESS)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley goto fail1;
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley for (i = 0; i < NBUCKETS; i++) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ISC_LIST_INIT(adb->names[i]);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->name_sd[i] = ISC_FALSE;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->name_refcnt[i] = 0;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->irefcnt++;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley for (i = 0; i < NBUCKETS; i++) {
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley ISC_LIST_INIT(adb->entries[i]);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->entry_sd[i] = ISC_FALSE;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->entry_refcnt[i] = 0;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley adb->irefcnt++;
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley }
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (result != ISC_R_SUCCESS)
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews goto fail2;
ce3761f64d3d734cc94605026985898900ecc474Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley /*
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley * Memory pools
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley */
ce3761f64d3d734cc94605026985898900ecc474Bob Halley#define MPINIT(t, p, n) do { \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley result = isc_mempool_create(mem, sizeof(t), &(p)); \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley if (result != ISC_R_SUCCESS) \
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto fail3; \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_setfreemax((p), FREE_ITEMS); \
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mempool_setfillcount((p), FILL_COUNT); \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_setname((p), n); \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley isc_mempool_associatelock((p), &adb->mplock); \
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley} while (0)
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley MPINIT(dns_adbname_t, adb->nmp, "adbname");
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
84c3294183a1cca851ce3f7f33c86772cd57bee1Bob Halley MPINIT(dns_adbentry_t, adb->emp, "adbentry");
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
ce3761f64d3d734cc94605026985898900ecc474Bob Halley MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
ce3761f64d3d734cc94605026985898900ecc474Bob Halley#undef MPINIT
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Allocate a timer and a task for our periodic cleanup.
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 */
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 result = isc_task_create(adb->taskmgr, 0, &adb->task);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (result != ISC_R_SUCCESS)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence goto fail3;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_task_setname(adb->task, "ADB", adb);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * XXXMLG When this is changed to be a config file option,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence */
72ce8db9183a39f393dbc6ff15259dbf7ea55275David Lawrence isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews result = isc_timer_create(adb->timermgr, isc_timertype_once,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence NULL, &adb->tick_interval, adb->task,
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence timer_cleanup, adb, &adb->timer);
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence if (result != ISC_R_SUCCESS)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence goto fail3;
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence DP(ISC_LOG_DEBUG(5), "cleaning interval for adb: "
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence "%u buckets every %u seconds, %u buckets in system, %u cl.interval",
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence /*
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence * Normal return.
41c53e1d11ed6685dfa929bd8bef524ed17b4064Andreas Gustafsson */
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews adb->magic = DNS_ADB_MAGIC;
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews *newadb = adb;
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence return (ISC_R_SUCCESS);
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews fail3:
b6309ed962c4988a314d61742c4fbc4935467d68Mark Andrews if (adb->task != NULL)
605ae28c5a73ad6c86425dfc0ed1d49652141c67David Lawrence isc_task_detach(&adb->task);
e407562a75eb93073bb72089cced150d7ffe4d4fTatuya JINMEI 神明達哉 if (adb->timer != NULL)
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley isc_timer_detach(&adb->timer);
453603c018a9e7c3ee7726868f27c95798544ed7Bob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff /* clean up entrylocks */
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews fail2: /* clean up namelocks */
8326257468615966b10820260beb3ee96eee94b5Bob Halley DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley fail1: /* clean up only allocated memory */
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (adb->nmp != NULL)
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_mempool_destroy(&adb->nmp);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley if (adb->nhmp != NULL)
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley isc_mempool_destroy(&adb->nhmp);
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley if (adb->zimp != NULL)
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_mempool_destroy(&adb->zimp);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley if (adb->emp != NULL)
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_destroy(&adb->emp);
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley if (adb->ahmp != NULL)
70fdfcd1fa7ebd059deffa9a2cecc29df96dfe52Bob Halley isc_mempool_destroy(&adb->ahmp);
402b05ddea16c6179fd251e4e740606dd973d593Bob Halley if (adb->aimp != NULL)
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_mempool_destroy(&adb->aimp);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley if (adb->afmp != NULL)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_mempool_destroy(&adb->afmp);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley DESTROYLOCK(&adb->reflock);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley fail0d:
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley DESTROYLOCK(&adb->mplock);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley fail0c:
08c8a934ceb2dfc6a5ebfd3be4ba5a1b3243bc73Bob Halley DESTROYLOCK(&adb->lock);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley fail0b:
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley return (result);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley}
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halleyvoid
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencedns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley REQUIRE(DNS_ADB_VALID(adb));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(adbx != NULL && *adbx == NULL);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley inc_adb_erefcnt(adb);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley *adbx = adb;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley}
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halleyvoid
63c8c8f2a1c1e490305fde095321798f0342739dBob Halleydns_adb_detach(dns_adb_t **adbx) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley dns_adb_t *adb;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_boolean_t need_exit_check;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb = *adbx;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley *adbx = NULL;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley INSIST(adb->erefcnt > 0);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley LOCK(&adb->reflock);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley adb->erefcnt--;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley UNLOCK(&adb->reflock);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley if (need_exit_check) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley LOCK(&adb->lock);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff INSIST(adb->shutting_down);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley check_exit(adb);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley UNLOCK(&adb->lock);
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley }
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley}
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halleyvoid
63c8c8f2a1c1e490305fde095321798f0342739dBob Halleydns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_task_t *clone;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_event_t *event;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_boolean_t zeroirefcnt = ISC_FALSE;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley /*
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * Send '*eventp' to 'task' when 'adb' has shutdown.
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence */
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(DNS_ADB_VALID(adb));
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley REQUIRE(eventp != NULL);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley event = *eventp;
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley *eventp = NULL;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley LOCK(&adb->lock);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley LOCK(&adb->reflock);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley zeroirefcnt = ISC_TF(adb->irefcnt == 0);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley if (adb->shutting_down && zeroirefcnt &&
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_mempool_getallocated(adb->ahmp) == 0) {
8326257468615966b10820260beb3ee96eee94b5Bob Halley /*
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley * We're already shutdown. Send the event.
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley */
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley event->ev_sender = adb;
63c8c8f2a1c1e490305fde095321798f0342739dBob Halley isc_task_send(task, &event);
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley } else {
6f5c11ea91e890e78eaa31a73e309e07f09f0ec0Bob Halley clone = NULL;
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_task_attach(task, &clone);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff event->ev_sender = clone;
8326257468615966b10820260beb3ee96eee94b5Bob Halley ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
8326257468615966b10820260beb3ee96eee94b5Bob Halley }
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley UNLOCK(&adb->reflock);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff UNLOCK(&adb->lock);
8326257468615966b10820260beb3ee96eee94b5Bob Halley}
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halleyvoid
8326257468615966b10820260beb3ee96eee94b5Bob Halleydns_adb_shutdown(dns_adb_t *adb) {
8326257468615966b10820260beb3ee96eee94b5Bob Halley isc_boolean_t need_check_exit;
8326257468615966b10820260beb3ee96eee94b5Bob Halley
8326257468615966b10820260beb3ee96eee94b5Bob Halley /*
8326257468615966b10820260beb3ee96eee94b5Bob Halley * Shutdown 'adb'.
8326257468615966b10820260beb3ee96eee94b5Bob Halley */
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews
6ebf3dbb33606411c17cbb75f7407e1457ca4b68Mark Andrews LOCK(&adb->lock);
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews
c6af776a7e2087e9b41b6403633d1b591431dacfMark Andrews if (!adb->shutting_down) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adb->shutting_down = ISC_TRUE;
611dc8876869036ab5e981e53ae7a446145d9354Mark Andrews isc_mem_setwater(adb->mctx, water, adb, 0, 0);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence need_check_exit = shutdown_names(adb);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (!need_check_exit)
8326257468615966b10820260beb3ee96eee94b5Bob Halley need_check_exit = shutdown_entries(adb);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (need_check_exit)
8326257468615966b10820260beb3ee96eee94b5Bob Halley check_exit(adb);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence UNLOCK(&adb->lock);
8326257468615966b10820260beb3ee96eee94b5Bob Halley}
8326257468615966b10820260beb3ee96eee94b5Bob Halley
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrewsisc_result_t
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrewsdns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews void *arg, dns_name_t *name, dns_name_t *zone,
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews unsigned int options, isc_stdtime_t now, dns_name_t *target,
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews in_port_t port, dns_adbfind_t **findp)
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley{
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley dns_adbfind_t *find;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley dns_adbname_t *adbname;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley int bucket;
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_boolean_t want_event, start_at_zone, alias, have_address;
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews isc_result_t result;
000027219d9824bdfb0b2c1865ec4d4bc839b631Mark Andrews unsigned int wanted_addresses;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley unsigned int wanted_fetches;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley unsigned int query_pending;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(DNS_ADB_VALID(adb));
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (task != NULL) {
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(action != NULL);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley }
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(name != NULL);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(zone != NULL);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(findp != NULL && *findp == NULL);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE(target == NULL || dns_name_hasbuffer(target));
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence result = ISC_R_UNEXPECTED;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley wanted_fetches = 0;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley query_pending = 0;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence want_event = ISC_FALSE;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley start_at_zone = ISC_FALSE;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley alias = ISC_FALSE;
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley
345a84c9f1e87c179a6ec9053200a94d5888fa13Bob Halley if (now == 0)
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson isc_stdtime_get(&now);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews /*
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * XXXMLG Move this comment somewhere else!
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews *
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Look up the name in our internal database.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews *
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * Possibilities: Note that these are not always exclusive.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews *
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * No name found. In this case, allocate a new name header and
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * an initial namehook or two. If any of these allocations
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * fail, clean up and return ISC_R_NOMEMORY.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews *
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Name found, valid addresses present. Allocate one addrinfo
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * structure for each found and append it to the linked list
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * of addresses for this header.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews *
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Name found, queries pending. In this case, if a task was
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * passed in, allocate a job id, attach it to the name's job
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * list and remember to tell the caller that there will be
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * more info coming later.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews */
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews find = new_adbfind(adb);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (find == NULL)
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews return (ISC_R_NOMEMORY);
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews find->port = port;
e672951ed28b2e9cc7a19c3d7fa4a258382f981cAutomatic Updater
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews /*
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews * Remember what types of addresses we are interested in.
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews */
72bdbe3c70f415a717f59f72d04590d70acb380eMark Andrews find->options = options;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews find->flags |= wanted_addresses;
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (FIND_WANTEVENT(find)) {
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews REQUIRE(task != NULL);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews }
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews /*
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews * Try to see if we know anything about this name at all.
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews */
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews bucket = DNS_ADB_INVALIDBUCKET;
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews adbname = find_name_and_lock(adb, name, find->options, &bucket);
6cf369f528c4acd8182eada41ad83b8d97623db8Mark Andrews if (adb->name_sd[bucket]) {
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson DP(DEF_LEVEL,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence "dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson INSIST(free_adbfind(adb, &find) == ISC_FALSE);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson result = ISC_R_SHUTTINGDOWN;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson goto out;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence /*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * Nothing found. Allocate a new adbname structure for this name.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (adbname == NULL) {
a3c0a79b61edfd6a021c080d4b368c9c962fcad6Andreas Gustafsson adbname = new_adbname(adb, name);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson if (adbname == NULL) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence INSIST(free_adbfind(adb, &find) == ISC_FALSE);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence result = ISC_R_NOMEMORY;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence goto out;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence link_name(adb, bucket, adbname);
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson if (FIND_HINTOK(find))
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence adbname->flags |= NAME_HINT_OK;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (FIND_GLUEOK(find))
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson adbname->flags |= NAME_GLUE_OK;
b4b4adc097365bd3f980b30bc7cc30199f4b8456Andreas Gustafsson if (FIND_STARTATZONE(find))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname->flags |= NAME_STARTATZONE;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt }
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt /*
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Expire old entries, etc.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt */
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt INSIST(check_expire_namehooks(adbname, now, adb->overmem) == ISC_FALSE);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt /*
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Do we know that the name is an alias?
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt */
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (!EXPIRE_OK(adbname->expire_target, now)) {
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt /*
74f4bfde4abb36524e62bed2bbc27d775e67c0a9Automatic Updater * Yes, it is.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt */
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt DP(DEF_LEVEL,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt "dns_adb_createfind: name %p is an alias (cached)",
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt adbname);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt alias = ISC_TRUE;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt goto post_copy;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt }
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt /*
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * Try to populate the name from the database and/or
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * start fetches. First try looking for an A record
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * in the database.
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt */
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt && WANT_INET(wanted_addresses)) {
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt result = dbfind_name(adbname, now, dns_rdatatype_a);
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (result == ISC_R_SUCCESS) {
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt DP(DEF_LEVEL,
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt "dns_adb_createfind: found A for name %p in db",
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt adbname);
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 goto v6;
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 }
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews /*
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * Did we get a CNAME or DNAME?
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (result == DNS_R_ALIAS) {
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews DP(DEF_LEVEL,
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews "dns_adb_createfind: name %p is an alias",
9cee5bb02863bf191e12cd4297adabf1971020deAutomatic Updater adbname);
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 alias = ISC_TRUE;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt goto post_copy;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt }
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt /*
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * If the name doesn't exist at all, don't bother with
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 * v6 queries; they won't work.
44de0b1f7d9997aaf6092589c4c7da4a1df908dbTatuya JINMEI 神明達哉 *
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * If the name does exist but we didn't get our data, go
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt * ahead and try AAAA.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews *
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews * If the result is neither of these, try a fetch for A.
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews */
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews if (NXDOMAIN_RESULT(result))
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews goto fetch;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews else if (NXRRSET_RESULT(result))
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt goto v6;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (!NAME_FETCH_V4(adbname))
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt wanted_fetches |= DNS_ADBFIND_INET;
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt }
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews v6:
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt && WANT_INET6(wanted_addresses)) {
3f8be559f0871022c78a229bad0eb09560b90909Evan Hunt result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (result == ISC_R_SUCCESS) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington DP(DEF_LEVEL,
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington "dns_adb_createfind: found AAAA for name %p",
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington goto fetch;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Did we get a CNAME or DNAME?
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (result == DNS_R_ALIAS) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington DP(DEF_LEVEL,
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington "dns_adb_createfind: name %p is an alias",
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington adbname);
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington alias = ISC_TRUE;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington goto post_copy;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Listen to negative cache hints, and don't start
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * another query.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (NCACHE_RESULT(result) || AUTH_NX(result))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington goto fetch;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (!NAME_FETCH_V6(adbname))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington wanted_fetches |= DNS_ADBFIND_INET6;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington }
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington fetch:
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington (WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington have_address = ISC_TRUE;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington else
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington have_address = ISC_FALSE;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (wanted_fetches != 0 &&
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington ! (FIND_AVOIDFETCHES(find) && have_address)) {
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * We're missing at least one address family. Either the
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * caller hasn't instructed us to avoid fetches, or we don't
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * know anything about any of the address families that would
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington * be acceptable so we have to launch fetches.
b7dca533e041adcacafb9369892b19db9231937bBrian Wellington */
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington if (FIND_STARTATZONE(find))
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington start_at_zone = ISC_TRUE;
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington /*
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington * Start V4.
6d5032f9a23fe1197610114983c9938ac419b20cBrian Wellington */
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (WANT_INET(wanted_fetches) &&
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews fetch_name(adbname, start_at_zone,
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_rdatatype_a) == ISC_R_SUCCESS) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews DP(DEF_LEVEL,
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews "dns_adb_createfind: started A fetch for name %p",
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews adbname);
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews }
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews /*
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews * Start V6.
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews */
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews if (WANT_INET6(wanted_fetches) &&
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews fetch_name(adbname, start_at_zone,
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews DP(DEF_LEVEL,
ed6ca94ad75353d5344e2a456e7a8beb480a351fMark Andrews "dns_adb_createfind: "
"started AAAA fetch for name %p",
adbname);
}
}
/*
* Run through the name and copy out the bits we are
* interested in.
*/
copy_namehook_lists(adb, find, zone, adbname, now);
post_copy:
if (NAME_FETCH_V4(adbname))
query_pending |= DNS_ADBFIND_INET;
if (NAME_FETCH_V6(adbname))
query_pending |= DNS_ADBFIND_INET6;
/*
* Attach to the name's query list if there are queries
* already running, and we have been asked to.
*/
want_event = ISC_TRUE;
if (!FIND_WANTEVENT(find))
want_event = ISC_FALSE;
if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
want_event = ISC_FALSE;
if ((wanted_addresses & query_pending) == 0)
want_event = ISC_FALSE;
if (alias)
want_event = ISC_FALSE;
if (want_event) {
find->adbname = adbname;
find->name_bucket = bucket;
ISC_LIST_APPEND(adbname->finds, find, plink);
find->query_pending = (query_pending & wanted_addresses);
find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
find, adbname);
} else {
/*
* Remove the flag so the caller knows there will never
* be an event, and set internal flags to fake that
* the event was sent and freed, so dns_adb_destroyfind() will
* do the right thing.
*/
find->query_pending = (query_pending & wanted_addresses);
find->options &= ~DNS_ADBFIND_WANTEVENT;
find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
}
find->partial_result |= (adbname->partial_result & wanted_addresses);
if (alias) {
if (target != NULL) {
result = dns_name_copy(&adbname->target, target, NULL);
if (result != ISC_R_SUCCESS)
goto out;
}
result = DNS_R_ALIAS;
} else
result = ISC_R_SUCCESS;
/*
* Copy out error flags from the name structure into the find.
*/
find->result_v4 = find_err_map[adbname->fetch_err];
find->result_v6 = find_err_map[adbname->fetch6_err];
out:
if (find != NULL) {
*findp = find;
if (want_event) {
isc_task_t *taskp;
INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
taskp = NULL;
isc_task_attach(task, &taskp);
find->event.ev_sender = taskp;
find->event.ev_action = action;
find->event.ev_arg = arg;
}
}
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
void
dns_adb_destroyfind(dns_adbfind_t **findp) {
dns_adbfind_t *find;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *ai;
int bucket;
dns_adb_t *adb;
REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
find = *findp;
*findp = NULL;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(FIND_EVENTFREED(find));
bucket = find->name_bucket;
INSIST(bucket == DNS_ADB_INVALIDBUCKET);
UNLOCK(&find->lock);
/*
* The find doesn't exist on any list, and nothing is locked.
* Return the find to the memory pool, and decrement the adb's
* reference count.
*/
ai = ISC_LIST_HEAD(find->list);
while (ai != NULL) {
ISC_LIST_UNLINK(find->list, ai, publink);
entry = ai->entry;
ai->entry = NULL;
INSIST(DNS_ADBENTRY_VALID(entry));
INSIST(dec_entry_refcnt(adb, entry, ISC_TRUE) == ISC_FALSE);
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);
if (free_adbfind(adb, &find))
check_exit(adb);
UNLOCK(&adb->lock);
}
void
dns_adb_cancelfind(dns_adbfind_t *find) {
isc_event_t *ev;
isc_task_t *task;
dns_adb_t *adb;
int bucket;
int unlock_bucket;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(!FIND_EVENTFREED(find));
REQUIRE(FIND_WANTEVENT(find));
bucket = find->name_bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the find.
*/
unlock_bucket = bucket;
violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
bucket = find->name_bucket;
if (bucket != DNS_ADB_INVALIDBUCKET) {
ISC_LIST_UNLINK(find->adbname->finds, find, plink);
find->adbname = NULL;
find->name_bucket = DNS_ADB_INVALIDBUCKET;
}
UNLOCK(&adb->namelocks[unlock_bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
cleanup:
if (!FIND_EVENTSENT(find)) {
ev = &find->event;
task = ev->ev_sender;
ev->ev_sender = find;
ev->ev_type = DNS_EVENT_ADBCANCELED;
ev->ev_destroy = event_free;
ev->ev_destroy_arg = find;
find->result_v4 = ISC_R_CANCELED;
find->result_v6 = ISC_R_CANCELED;
DP(DEF_LEVEL, "sending event %p to task %p for find %p",
ev, task, find);
isc_task_sendanddetach(&task, (isc_event_t **)&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 leisure.
*/
LOCK(&adb->lock);
dump_adb(adb, f, ISC_FALSE);
UNLOCK(&adb->lock);
}
static void
dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
if (value == INT_MAX)
return;
fprintf(f, " [%s TTL %d]", legend, value - now);
}
static void
dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug) {
int i;
dns_adbname_t *name;
isc_stdtime_t now;
isc_stdtime_get(&now);
fprintf(f, ";\n; Address database dump\n;\n");
if (debug)
fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
adb, 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
*/
for (i = 0; i < NBUCKETS; i++) {
name = ISC_LIST_HEAD(adb->names[i]);
if (name == NULL)
continue;
if (debug)
fprintf(f, "; bucket %d\n", i);
for (;
name != NULL;
name = ISC_LIST_NEXT(name, plink))
{
if (debug)
fprintf(f, "; name %p (flags %08x)\n",
name, name->flags);
fprintf(f, "; ");
print_dns_name(f, &name->name);
if (dns_name_countlabels(&name->target) > 0) {
fprintf(f, " alias ");
print_dns_name(f, &name->target);
}
dump_ttl(f, "v4", name->expire_v4, now);
dump_ttl(f, "v6", name->expire_v6, now);
dump_ttl(f, "target", name->expire_target, now);
fprintf(f, " [v4 %s] [v6 %s]",
errnames[name->fetch_err],
errnames[name->fetch6_err]);
fprintf(f, "\n");
print_namehook_list(f, "v4", &name->v4, debug, now);
print_namehook_list(f, "v6", &name->v6, debug, now);
if (debug)
print_fetch_list(f, name);
if (debug)
print_find_list(f, name);
}
}
/*
* Unlock everything
*/
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->namelocks[i]);
}
static void
dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
isc_stdtime_t now)
{
char addrbuf[ISC_NETADDR_FORMATSIZE];
isc_netaddr_t netaddr;
dns_adbzoneinfo_t *zi;
isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
if (debug)
fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
fprintf(f, ";\t%s [srtt %u] [flags %08x]",
addrbuf, entry->srtt, entry->flags);
fprintf(f, "\n");
for (zi = ISC_LIST_HEAD(entry->zoneinfo);
zi != NULL;
zi = ISC_LIST_NEXT(zi, plink)) {
fprintf(f, ";\t\t");
print_dns_name(f, &zi->zone);
fprintf(f, " [lame TTL %d]\n", zi->lame_timer - now);
}
}
void
dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
char tmp[512];
const char *tmpp;
dns_adbaddrinfo_t *ai;
isc_sockaddr_t *sa;
/*
* Not used currently, in the API Just In Case we
* want to dump out the name and/or entries too.
*/
LOCK(&find->lock);
fprintf(f, ";Find %p\n", find);
fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
find->query_pending, find->partial_result,
find->options, find->flags);
fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
find->name_bucket, find->adbname, find->event.ev_sender);
ai = ISC_LIST_HEAD(find->list);
if (ai != NULL)
fprintf(f, "\tAddresses:\n");
while (ai != NULL) {
sa = &ai->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
tmp, sizeof(tmp));
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
tmp, sizeof(tmp));
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "BadAddress";
fprintf(f, "\t\tentry %p, flags %08x"
" srtt %u addr %s\n",
ai->entry, ai->flags, 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[DNS_NAME_FORMATSIZE];
INSIST(f != NULL);
dns_name_format(name, buf, sizeof(buf));
fprintf(f, "%s", buf);
}
static void
print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
isc_boolean_t debug, isc_stdtime_t now)
{
dns_adbnamehook_t *nh;
for (nh = ISC_LIST_HEAD(*list);
nh != NULL;
nh = ISC_LIST_NEXT(nh, plink))
{
if (debug)
fprintf(f, ";\tHook(%s) %p\n", legend, nh);
dump_entry(f, nh->entry, debug, now);
}
}
static inline void
print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
type, ft, ft->namehook, ft->entry, ft->fetch);
}
static void
print_fetch_list(FILE *f, dns_adbname_t *n) {
if (NAME_FETCH_A(n))
print_fetch(f, n->fetch_a, "A");
if (NAME_FETCH_AAAA(n))
print_fetch(f, n->fetch_aaaa, "AAAA");
}
static void
print_find_list(FILE *f, dns_adbname_t *name) {
dns_adbfind_t *find;
find = ISC_LIST_HEAD(name->finds);
while (find != NULL) {
dns_adb_dumpfind(find, f);
find = ISC_LIST_NEXT(find, plink);
}
}
static isc_result_t
dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
{
isc_result_t result;
dns_rdataset_t rdataset;
dns_adb_t *adb;
dns_fixedname_t foundname;
dns_name_t *fname;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
dns_rdataset_init(&rdataset);
if (rdtype == dns_rdatatype_a)
adbname->fetch_err = FIND_ERR_UNEXPECTED;
else
adbname->fetch6_err = FIND_ERR_UNEXPECTED;
result = dns_view_find(adb->view, &adbname->name, rdtype, now,
NAME_GLUEOK(adbname),
ISC_TF(NAME_HINTOK(adbname)),
NULL, NULL, fname, &rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
/*
* Found in the database. Even if we can't copy out
* any information, return success, or else a fetch
* will be made, which will only make things worse.
*/
if (rdtype == dns_rdatatype_a)
adbname->fetch_err = FIND_ERR_SUCCESS;
else
adbname->fetch6_err = FIND_ERR_SUCCESS;
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);
if (result == DNS_R_NXDOMAIN)
adbname->fetch_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch_err = FIND_ERR_NXRRSET;
} else {
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for AAAA",
adbname);
adbname->expire_v6 = now + 30;
if (result == DNS_R_NXDOMAIN)
adbname->fetch6_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch6_err = FIND_ERR_NXRRSET;
}
break;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
/*
* We found a negative cache entry. Pull the TTL from it
* so we won't ask again for a while.
*/
rdataset.ttl = ttlclamp(rdataset.ttl);
if (rdtype == dns_rdatatype_a) {
adbname->expire_v4 = rdataset.ttl + now;
if (result == DNS_R_NCACHENXDOMAIN)
adbname->fetch_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch_err = FIND_ERR_NXRRSET;
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;
if (result == DNS_R_NCACHENXDOMAIN)
adbname->fetch6_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch6_err = FIND_ERR_NXRRSET;
}
break;
case DNS_R_CNAME:
case DNS_R_DNAME:
/*
* Clear the hint and glue flags, so this will match
* more often.
*/
adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
rdataset.ttl = ttlclamp(rdataset.ttl);
clean_target(adb, &adbname->target);
adbname->expire_target = INT_MAX;
result = set_target(adb, &adbname->name, fname, &rdataset,
&adbname->target);
if (result == ISC_R_SUCCESS) {
result = DNS_R_ALIAS;
DP(NCACHE_LEVEL,
"adb name %p: caching alias target",
adbname);
adbname->expire_target = rdataset.ttl + now;
}
if (rdtype == dns_rdatatype_a)
adbname->fetch_err = FIND_ERR_SUCCESS;
else
adbname->fetch6_err = FIND_ERR_SUCCESS;
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;
UNUSED(task);
INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->ev_arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
address_type = 0;
if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
address_type = DNS_ADBFIND_INET;
fetch = name->fetch_a;
name->fetch_a = NULL;
} else if (NAME_FETCH_AAAA(name)
&& (name->fetch_aaaa->fetch == dev->fetch)) {
address_type = DNS_ADBFIND_INET6;
fetch = name->fetch_aaaa;
name->fetch_aaaa = NULL;
}
INSIST(address_type != 0);
dns_resolver_destroyfetch(&fetch->fetch);
dev->fetch = NULL;
ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
/*
* Cleanup things we don't care about.
*/
if (dev->node != NULL)
dns_db_detachnode(dev->db, &dev->node);
if (dev->db != NULL)
dns_db_detach(&dev->db);
/*
* If this name is marked as dead, clean up, throwing away
* potentially good data.
*/
if (NAME_DEAD(name)) {
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If we got a negative cache response, remember it.
*/
if (NCACHE_RESULT(dev->result)) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
if (address_type == DNS_ADBFIND_INET) {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"caching negative entry for A (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v4 = ISC_MIN(name->expire_v4,
dev->rdataset->ttl + now);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch_err = FIND_ERR_NXDOMAIN;
else
name->fetch_err = FIND_ERR_NXRRSET;
} 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);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch6_err = FIND_ERR_NXDOMAIN;
else
name->fetch6_err = FIND_ERR_NXRRSET;
}
goto out;
}
/*
* Handle CNAME/DNAME.
*/
if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
clean_target(adb, &name->target);
name->expire_target = INT_MAX;
result = set_target(adb, &name->name,
dns_fixedname_name(&dev->foundname),
dev->rdataset,
&name->target);
if (result == ISC_R_SUCCESS) {
DP(NCACHE_LEVEL,
"adb fetch name %p: caching alias target",
name);
name->expire_target = dev->rdataset->ttl + now;
}
goto check_result;
}
/*
* Did we get back junk? If so, and there are no more fetches
* sitting out there, tell all the finds about it.
*/
if (dev->result != ISC_R_SUCCESS) {
char buf[DNS_NAME_FORMATSIZE];
dns_name_format(&name->name, buf, sizeof(buf));
DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
dns_result_totext(dev->result));
/* XXXMLG Don't pound on bad servers. */
if (address_type == DNS_ADBFIND_INET) {
name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
name->fetch_err = FIND_ERR_FAILURE;
} else {
name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
name->fetch6_err = FIND_ERR_FAILURE;
}
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;
if (address_type == DNS_ADBFIND_INET)
name->fetch_err = FIND_ERR_SUCCESS;
else
name->fetch6_err = FIND_ERR_SUCCESS;
}
out:
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
clean_finds_at_name(name, ev_status, address_type);
UNLOCK(&adb->namelocks[bucket]);
}
static isc_result_t
fetch_name(dns_adbname_t *adbname,
isc_boolean_t start_at_zone,
dns_rdatatype_t type)
{
isc_result_t result;
dns_adbfetch_t *fetch = NULL;
dns_adb_t *adb;
dns_fixedname_t fixed;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
unsigned int options;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST((type == dns_rdatatype_a && !NAME_FETCH_V4(adbname)) ||
(type == dns_rdatatype_aaaa && !NAME_FETCH_V6(adbname)));
adbname->fetch_err = FIND_ERR_NOTFOUND;
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
options = 0;
if (start_at_zone) {
DP(50, "fetch_name: starting at zone for %p",
adbname);
dns_fixedname_init(&fixed);
name = dns_fixedname_name(&fixed);
result = dns_view_findzonecut2(adb->view, &adbname->name, name,
0, 0, ISC_TRUE, ISC_FALSE,
&rdataset, NULL);
if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
goto cleanup;
nameservers = &rdataset;
options |= DNS_FETCHOPT_UNSHARED;
}
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
type, name, nameservers, NULL,
options, adb->task, fetch_callback,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
if (type == dns_rdatatype_a)
adbname->fetch_a = fetch;
else
adbname->fetch_aaaa = fetch;
fetch = NULL; /* Keep us from cleaning this up below. */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
/*
* 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_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int rtt, unsigned int factor)
{
int bucket;
unsigned int new_srtt;
isc_stdtime_t now;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(factor <= 10);
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
if (factor == DNS_ADB_RTTADJAGE)
new_srtt = addr->entry->srtt * 98 / 100;
else
new_srtt = (addr->entry->srtt / 10 * factor)
+ (rtt / 10 * (10 - factor));
addr->entry->srtt = new_srtt;
addr->srtt = new_srtt;
isc_stdtime_get(&now);
addr->entry->expires = now + ADB_ENTRY_WINDOW;
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int bits, unsigned int mask)
{
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
/*
* Note that we do not update the other bits in addr->flags with
* the most recent values from addr->entry->flags.
*/
addr->flags = (addr->flags & ~mask) | (bits & mask);
UNLOCK(&adb->entrylocks[bucket]);
}
isc_result_t
dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
{
int bucket;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *addr;
isc_result_t result;
in_port_t port;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL && *addrp == NULL);
UNUSED(now);
result = ISC_R_SUCCESS;
bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, sa, &bucket);
if (adb->entry_sd[bucket]) {
result = ISC_R_SHUTTINGDOWN;
goto unlock;
}
if (entry == NULL) {
/*
* We don't know anything about this address.
*/
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
entry->sockaddr = *sa;
link_entry(adb, bucket, entry);
DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
} else
DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
port = isc_sockaddr_getport(sa);
addr = new_adbaddrinfo(adb, entry, port);
if (addr != NULL) {
inc_entry_refcnt(adb, entry, ISC_FALSE);
*addrp = addr;
}
unlock:
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
void
dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
dns_adbaddrinfo_t *addr;
dns_adbentry_t *entry;
int bucket;
isc_stdtime_t now;
isc_boolean_t want_check_exit = ISC_FALSE;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL);
addr = *addrp;
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
entry = addr->entry;
REQUIRE(DNS_ADBENTRY_VALID(entry));
isc_stdtime_get(&now);
*addrp = NULL;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
entry->expires = now + ADB_ENTRY_WINDOW;
want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
UNLOCK(&adb->entrylocks[bucket]);
addr->entry = NULL;
free_adbaddrinfo(adb, &addr);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
}
void
dns_adb_flush(dns_adb_t *adb) {
unsigned int i;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
for (i = 0; i < NBUCKETS; i++) {
/*
* Call our cleanup routines.
*/
INSIST(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
INSIST(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
}
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE);
#endif
UNLOCK(&adb->lock);
}
void
dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
dns_adbname_t *adbname;
dns_adbname_t *nextname;
int bucket;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
LOCK(&adb->namelocks[bucket]);
adbname = ISC_LIST_HEAD(adb->names[bucket]);
while (adbname != NULL) {
nextname = ISC_LIST_NEXT(adbname, plink);
if (!NAME_DEAD(adbname) &&
dns_name_equal(name, &adbname->name)) {
INSIST(kill_name(&adbname, DNS_EVENT_ADBCANCELED)
== ISC_FALSE);
}
adbname = nextname;
}
UNLOCK(&adb->namelocks[bucket]);
UNLOCK(&adb->lock);
}
static void
water(void *arg, int mark) {
dns_adb_t *adb = arg;
isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
isc_interval_t interval;
REQUIRE(DNS_ADB_VALID(adb));
DP(ISC_LOG_DEBUG(1),
"adb reached %s water mark", overmem ? "high" : "low");
adb->overmem = overmem;
if (overmem) {
isc_interval_set(&interval, 0, 1);
(void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
&interval, ISC_TRUE);
}
}
void
dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
isc_uint32_t hiwater;
isc_uint32_t lowater;
INSIST(DNS_ADB_VALID(adb));
if (size != 0 && size < DNS_ADB_MINADBSIZE)
size = DNS_ADB_MINADBSIZE;
hiwater = size - (size >> 3); /* Approximately 7/8ths. */
lowater = size - (size >> 2); /* Approximately 3/4ths. */
if (size == 0 || hiwater == 0 || lowater == 0)
isc_mem_setwater(adb->mctx, water, adb, 0, 0);
else
isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);
}