adb.c revision 2113dfd6e20a9ca16000ed226517b4660087c1f2
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1999-2003 Internet Software Consortium.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley *
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Permission to use, copy, modify, and distribute this software for any
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley */
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson/* $Id: adb.c,v 1.227 2006/08/30 23:09:18 marka Exp $ */
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
e419f613d8591885df608cb73065921be07dd12eBob Halley/*! \file
e419f613d8591885df608cb73065921be07dd12eBob Halley *
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * \note
fca5f81ad69098ea8abba130c7f841c951ef91c2Bob Halley * In finds, if task == NULL, no events will be generated, and no events
e419f613d8591885df608cb73065921be07dd12eBob Halley * have been sent. If task != NULL but taskaction == NULL, an event has been
e419f613d8591885df608cb73065921be07dd12eBob Halley * posted but not yet freed. If neither are NULL, no event was posted.
9695ae1c24b168996e3a267855dc754971ccb32cBob Halley *
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
1c776a2909632bc755f3fddd3b53addd792ab4d0Brian Wellington
e419f613d8591885df608cb73065921be07dd12eBob Halley/*%
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * After we have cleaned all buckets, dump the database contents.
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson */
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington#if 0
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington#define DUMP_ADB_AFTER_CLEANING
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#endif
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <config.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington#include <limits.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <isc/mutexblock.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <isc/netaddr.h>
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <isc/random.h>
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence#include <isc/string.h> /* Required for HP/UX (and others?) */
e419f613d8591885df608cb73065921be07dd12eBob Halley#include <isc/task.h>
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley#include <isc/timer.h>
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <isc/util.h>
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington#include <dns/adb.h>
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <dns/db.h>
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley#include <dns/events.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/log.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/rdata.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/rdataset.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/rdatastruct.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/rdatatype.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/resolver.h>
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#include <dns/result.h>
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
ed019cabc1cc75d4412010c331876e4ae5080a4dDavid Lawrence#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*!
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * The number of buckets needs to be a prime (for good hashing).
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley *
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * XXXRTH How many buckets do we need?
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington#define NBUCKETS 1009 /*%< how many buckets for names/addrs */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*!
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * For type 3 negative cache entries, we will remember that the address is
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * broken for this long. XXXMLG This is also used for actual addresses, too.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * The intent is to keep us from constantly asking about A/AAAA records
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington * if the zone has extremely low TTLs.
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff */
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff#define ADB_CACHE_MINIMUM 10 /*%< seconds */
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff#define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff#define ADB_ENTRY_WINDOW 1800 /*%< seconds */
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*%
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * buckets are cleaned in CLEAN_PERIOD seconds.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define CLEAN_PERIOD 3600
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington/*% See #CLEAN_PERIOD */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define CLEAN_SECONDS 30
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington/*% See #CLEAN_PERIOD */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define FREE_ITEMS 64 /*%< free count for memory pools */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define FILL_COUNT 16 /*%< fill count for memory pools */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define DNS_ADB_INVALIDBUCKET (-1) /*%< invalid bucket address */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington#define DNS_ADB_MINADBSIZE (1024*1024) /*%< 1 Megabyte */
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef struct dns_adbnamehook dns_adbnamehook_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef struct dns_adblameinfo dns_adblameinfo_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef struct dns_adbfetch dns_adbfetch_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellingtontypedef struct dns_adbfetch6 dns_adbfetch6_t;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington/*% dns adb structure */
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellingtonstruct dns_adb {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington unsigned int magic;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_mutex_t lock;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_mutex_t reflock; /*%< Covers irefcnt, erefcnt */
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_mem_t *mctx;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_view_t *view;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_timermgr_t *timermgr;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_timer_t *timer;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_taskmgr_t *taskmgr;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_task_t *task;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff isc_boolean_t overmem;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_interval_t tick_interval;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington int next_cleanbucket;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int irefcnt;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington unsigned int erefcnt;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mutex_t mplock;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mempool_t *nmp; /*%< dns_adbname_t */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mempool_t *nhmp; /*%< dns_adbnamehook_t */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mempool_t *limp; /*%< dns_adblameinfo_t */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mempool_t *emp; /*%< dns_adbentry_t */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson isc_mempool_t *ahmp; /*%< dns_adbfind_t */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mempool_t *aimp; /*%< dns_adbaddrinfo_t */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_mempool_t *afmp; /*%< dns_adbfetch_t */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington /*!
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * Bucketized locks and lists for names.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington dns_adbnamelist_t names[NBUCKETS];
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*% See dns_adbnamelist_t */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_mutex_t namelocks[NBUCKETS];
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*% See dns_adbnamelist_t */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_boolean_t name_sd[NBUCKETS];
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington /*% See dns_adbnamelist_t */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int name_refcnt[NBUCKETS];
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington /*!
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * Bucketized locks for entries.
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington *
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington dns_adbentrylist_t entries[NBUCKETS];
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington isc_mutex_t entrylocks[NBUCKETS];
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson isc_boolean_t entry_sd[NBUCKETS]; /*%< shutting down */
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson unsigned int entry_refcnt[NBUCKETS];
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
01446841be2b73f9a2ead74056df2d5342414041Andreas Gustafsson isc_event_t cevent;
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington isc_boolean_t cevent_sent;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington isc_boolean_t shutting_down;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_eventlist_t whenshutdown;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington};
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * XXXMLG Document these structures.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington/*% dns_adbname structure */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstruct dns_adbname {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int magic;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington dns_name_t name;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington dns_adb_t *adb;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington unsigned int partial_result;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington unsigned int flags;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington int lock_bucket;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_name_t target;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_stdtime_t expire_target;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_stdtime_t expire_v4;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_stdtime_t expire_v6;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington unsigned int chains;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adbnamehooklist_t v4;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adbnamehooklist_t v6;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adbfetch_t *fetch_a;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adbfetch_t *fetch_aaaa;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int fetch_err;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int fetch6_err;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbfindlist_t finds;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_LINK(dns_adbname_t) plink;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington};
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington/*% The adbfetch structure */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstruct dns_adbfetch {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington unsigned int magic;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington dns_adbnamehook_t *namehook;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington dns_adbentry_t *entry;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington dns_fetch_t *fetch;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington dns_rdataset_t rdataset;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson};
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington/*%
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * This is a small widget that dangles off a dns_adbname_t. It contains a
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * pointer to the address information about this host, and a link to the next
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * namehook that will contain the next address this host has.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstruct dns_adbnamehook {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int magic;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbentry_t *entry;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_LINK(dns_adbnamehook_t) plink;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence};
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence/*%
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * This is a small widget that holds qname-specific information about an
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * address. Currently limited to lameness, but could just as easily be
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * extended to other types of information about zones.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington */
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellingtonstruct dns_adblameinfo {
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington unsigned int magic;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington dns_name_t qname;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington dns_rdatatype_t qtype;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington isc_stdtime_t lame_timer;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington ISC_LINK(dns_adblameinfo_t) plink;
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington};
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington/*%
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * An address entry. It holds quite a bit of information about addresses,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * including edns state (in "flags"), rtt, and of course the address of
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington * the host.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellingtonstruct dns_adbentry {
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington unsigned int magic;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington int lock_bucket;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned int refcnt;
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington unsigned int flags;
aa863b2d1e3739ac958c510aa7d61137860c3b7eBrian Wellington unsigned int srtt;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_sockaddr_t sockaddr;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_stdtime_t expires;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*%<
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * A nonzero 'expires' field indicates that the entry should
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * persist until that time. This allows entries found
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * using dns_adb_findaddrinfo() to persist for a limited time
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * even though they are not necessarily associated with a
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * name.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_LIST(dns_adblameinfo_t) lameinfo;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington ISC_LINK(dns_adbentry_t) plink;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington};
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Internal functions (and prototypes).
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbentry_t *);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline dns_adblameinfo_t *new_adblameinfo(dns_adb_t *, dns_name_t *,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_rdatatype_t);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void free_adblameinfo(dns_adb_t *, dns_adblameinfo_t **);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonstatic inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington in_port_t);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellingtonstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned int, int *);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_sockaddr_t *, int *);
01446841be2b73f9a2ead74056df2d5342414041Andreas Gustafssonstatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug, isc_stdtime_t);
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellingtonstatic void print_dns_name(FILE *, dns_name_t *);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic void print_namehook_list(FILE *, const char *legend,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adbnamehooklist_t *list,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_boolean_t debug,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_stdtime_t now);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void print_find_list(FILE *, dns_adbname_t *);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic void print_fetch_list(FILE *, dns_adbname_t *);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void inc_adb_irefcnt(dns_adb_t *);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void inc_adb_erefcnt(dns_adb_t *);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_boolean_t);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington isc_boolean_t);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic void clean_target(dns_adb_t *, dns_name_t *);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington unsigned int);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington isc_boolean_t);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graffstatic void cancel_fetches_at_name(dns_adbname_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington dns_rdatatype_t);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic isc_result_t fetch_name(dns_adbname_t *, isc_boolean_t,
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington dns_rdatatype_t);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic inline void check_exit(dns_adb_t *);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic void timer_cleanup(isc_task_t *, isc_event_t *);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic void destroy(dns_adb_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic isc_boolean_t shutdown_names(dns_adb_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic isc_boolean_t shutdown_entries(dns_adb_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic void water(void *, int);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonstatic void dump_entry(FILE *, dns_adbentry_t *, isc_boolean_t, isc_stdtime_t);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington/*
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson * MUST NOT overlap DNS_ADBFIND_* flags!
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_EVENT_SENT 0x40000000
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_EVENT_FREED 0x80000000
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington#define NAME_NEEDS_POKE 0x80000000
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington#define NAME_IS_DEAD 0x40000000
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington#define NAME_HINT_OK DNS_ADBFIND_HINTOK
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington#define NAME_STARTATZONE DNS_ADBFIND_STARTATZONE
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * To the name, address classes are all that really exist. If it has a
17a3fcecd069130a5f318685493b0db5639a77c9Brian Wellington * V6 address it doesn't care if it came from a AAAA query.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Fetches are broken out into A and AAAA types. In some cases,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * however, it makes more sense to test for a particular class of fetches,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * like V4 or V6 above.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Note: since we have removed the support of A6 in adb, FETCH_A and FETCH_AAAA
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * are now equal to FETCH_V4 and FETCH_V6, respectively.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n))
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Find options and tests to see if there are addresses on the list.
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_STARTATZONE(fn) (((fn)->options & DNS_ADBFIND_STARTATZONE) \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington != 0)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * These are currently used on simple unsigned ints, so they are
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * not really associated with any particular type.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson/*
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * Find out if the flags on a name (nf) indicate if it is a hint or
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * glue, and compare this to the appropriate bits set in o, to see if
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * this is ok.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define STARTATZONE_MATCHES(nf, o) (((nf)->flags & NAME_STARTATZONE) == \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ((o) & DNS_ADBFIND_STARTATZONE))
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#define ENTER_LEVEL ISC_LOG_DEBUG(50)
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define EXIT_LEVEL ENTER_LEVEL
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define CLEAN_LEVEL ISC_LOG_DEBUG(100)
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews#define DEF_LEVEL ISC_LOG_DEBUG(5)
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#define NCACHE_LEVEL ISC_LOG_DEBUG(20)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (r) == DNS_R_NCACHENXRRSET)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington (r) == DNS_R_NXRRSET)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington (r) == DNS_R_NCACHENXDOMAIN)
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington (r) == DNS_R_NXRRSET || \
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson (r) == DNS_R_HINTNXRRSET)
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington/*
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington * Error state rankings.
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington */
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington#define FIND_ERR_SUCCESS 0 /* highest rank */
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_ERR_CANCELED 1
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson#define FIND_ERR_FAILURE 2
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_ERR_NXDOMAIN 3
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_ERR_NXRRSET 4
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_ERR_UNEXPECTED 5
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington#define FIND_ERR_NOTFOUND 6
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#define FIND_ERR_MAX 7
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic const char *errnames[] = {
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington "success",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "canceled",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "failure",
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson "nxdomain",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "nxrrset",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington "unexpected",
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington "not_found"
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson};
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington#define NEWERR(old, new) (ISC_MIN((old), (new)))
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic isc_result_t find_err_map[FIND_ERR_MAX] = {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_R_SUCCESS,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_R_CANCELED,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_R_FAILURE,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington DNS_R_NXDOMAIN,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington DNS_R_NXRRSET,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_R_UNEXPECTED,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_R_NOTFOUND /* not YET found */
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington};
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic void
94766449d6125cd5870891b70d46573e5deaceb4Brian WellingtonDP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic void
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian WellingtonDP(int level, const char *format, ...) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington va_list args;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington va_start(args, format);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington isc_log_vwrite(dns_lctx,
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington level, format, args);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington va_end(args);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington}
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonstatic inline dns_ttl_t
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellingtonttlclamp(dns_ttl_t ttl) {
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington if (ttl < ADB_CACHE_MINIMUM)
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington ttl = ADB_CACHE_MINIMUM;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (ttl > ADB_CACHE_MAXIMUM)
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ttl = ADB_CACHE_MAXIMUM;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington return (ttl);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington}
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington/*
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * Requires the adbname bucket be locked and that no entry buckets be locked.
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * This code handles A and AAAA rdatasets only.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafssonstatic isc_result_t
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_stdtime_t now)
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington{
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington isc_result_t result;
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington dns_adb_t *adb;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbnamehook_t *nh;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbnamehook_t *anh;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington dns_rdata_t rdata = DNS_RDATA_INIT;
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington struct in_addr ina;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington struct in6_addr in6a;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington isc_sockaddr_t sockaddr;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington int addr_bucket;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_boolean_t new_addresses_added;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dns_rdatatype_t rdtype;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington unsigned int findoptions;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington INSIST(DNS_ADBNAME_VALID(adbname));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington adb = adbname->adb;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington INSIST(DNS_ADB_VALID(adb));
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington rdtype = rdataset->type;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (rdtype == dns_rdatatype_a)
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington findoptions = DNS_ADBFIND_INET;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington else
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington findoptions = DNS_ADBFIND_INET6;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington addr_bucket = DNS_ADB_INVALIDBUCKET;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington new_addresses_added = ISC_FALSE;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington nh = NULL;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington result = dns_rdataset_first(rdataset);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington while (result == ISC_R_SUCCESS) {
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington dns_rdata_reset(&rdata);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington dns_rdataset_current(rdataset, &rdata);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington if (rdtype == dns_rdatatype_a) {
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington INSIST(rdata.length == 4);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington memcpy(&ina.s_addr, rdata.data, 4);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_sockaddr_fromin(&sockaddr, &ina, 0);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington } else {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington INSIST(rdata.length == 16);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington memcpy(in6a.s6_addr, rdata.data, 16);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington }
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson INSIST(nh == NULL);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington nh = new_adbnamehook(adb, NULL);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (nh == NULL) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington adbname->partial_result |= findoptions;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson result = ISC_R_NOMEMORY;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington goto fail;
98d010a24a9f1b4b45ce9791845941ef90426d0cBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (foundentry == NULL) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington dns_adbentry_t *entry;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington entry = new_adbentry(adb);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (entry == NULL) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington adbname->partial_result |= findoptions;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington result = ISC_R_NOMEMORY;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington goto fail;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington entry->sockaddr = sockaddr;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington entry->refcnt = 1;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington nh->entry = entry;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington link_entry(adb, addr_bucket, entry);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington } else {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington for (anh = ISC_LIST_HEAD(adbname->v4);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington anh != NULL;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington anh = ISC_LIST_NEXT(anh, plink))
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (anh->entry == foundentry)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington break;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (anh == NULL) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington foundentry->refcnt++;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington nh->entry = foundentry;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington } else
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington free_adbnamehook(adb, &nh);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington new_addresses_added = ISC_TRUE;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington if (nh != NULL) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (rdtype == dns_rdatatype_a)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ISC_LIST_APPEND(adbname->v4, nh, plink);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington else
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington ISC_LIST_APPEND(adbname->v6, nh, plink);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington }
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington nh = NULL;
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington result = dns_rdataset_next(rdataset);
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington }
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington
6f071989da905bb5ab2c6dfd01a71ee5ecea5918Brian Wellington fail:
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (nh != NULL)
f439363eeb4052fddc0e3ec648658548daa10506Brian Wellington free_adbnamehook(adb, &nh);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington UNLOCK(&adb->entrylocks[addr_bucket]);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
c38cf70db10594c4d23f092a65e17b123b381a60Brian Wellington if (rdataset->trust == dns_trust_glue ||
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington rdataset->trust == dns_trust_additional)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington rdataset->ttl = ADB_CACHE_MINIMUM;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington else
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington rdataset->ttl = ttlclamp(rdataset->ttl);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (rdtype == dns_rdatatype_a) {
f439363eeb4052fddc0e3ec648658548daa10506Brian Wellington DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington adbname->expire_v4, now + rdataset->ttl);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington now + rdataset->ttl);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence } else {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington adbname->expire_v6, now + rdataset->ttl);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington now + rdataset->ttl);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (new_addresses_added) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington /*
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Lie a little here. This is more or less so code that cares
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * can find out if any new information was added or not.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington return (ISC_R_SUCCESS);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington }
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington return (result);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews}
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington/*
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * Requires the name's bucket be locked.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonstatic isc_boolean_t
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonkill_name(dns_adbname_t **n, isc_eventtype_t ev) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adbname_t *name;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_boolean_t result = ISC_FALSE;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington isc_boolean_t result4, result6;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington dns_adb_t *adb;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington INSIST(n != NULL);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington name = *n;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington *n = NULL;
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington INSIST(DNS_ADBNAME_VALID(name));
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington adb = name->adb;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington INSIST(DNS_ADB_VALID(adb));
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington DP(DEF_LEVEL, "killing name %p", name);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington /*
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews * If we're dead already, just check to see if we should go
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington * away now or not.
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington */
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (NAME_DEAD(name) && !NAME_FETCH(name)) {
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington result = unlink_name(adb, name);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington free_adbname(adb, &name);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson if (result)
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson result = dec_adb_irefcnt(adb);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson return (result);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson }
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington /*
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson * Clean up the name's various lists. These two are destructive
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * in that they will always empty the list.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result4 = clean_namehooks(adb, &name->v4);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result6 = clean_namehooks(adb, &name->v6);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley clean_target(adb, &name->target);
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews result = ISC_TF(result4 || result6);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington /*
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington * If fetches are running, cancel them. If none are running, we can
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington * just kill the name here.
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington */
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington if (!NAME_FETCH(name)) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington INSIST(result == ISC_FALSE);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington result = unlink_name(adb, name);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington free_adbname(adb, &name);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (result)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = dec_adb_irefcnt(adb);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } else {
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington name->flags |= NAME_IS_DEAD;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley cancel_fetches_at_name(name);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (result);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington * Requires the name's bucket be locked and no entry buckets be locked.
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystatic isc_boolean_t
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellingtoncheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_boolean_t overmem)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley{
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adb_t *adb;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_boolean_t expire;
63bf060be4ff2a7ade02fd86abb98694a5afc250Brian Wellington isc_boolean_t result4 = ISC_FALSE;
63bf060be4ff2a7ade02fd86abb98694a5afc250Brian Wellington isc_boolean_t result6 = ISC_FALSE;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington INSIST(DNS_ADBNAME_VALID(name));
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington adb = name->adb;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington INSIST(DNS_ADB_VALID(adb));
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington if (overmem) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington isc_uint32_t val;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington isc_random_get(&val);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley expire = ISC_TF((val % 4) == 0);
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington } else
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews expire = ISC_FALSE;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Check to see if we need to remove the v4 addresses
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (!NAME_FETCH_V4(name) &&
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington (expire || EXPIRE_OK(name->expire_v4, now))) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington if (NAME_HAS_V4(name)) {
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington DP(DEF_LEVEL, "expiring v4 for name %p", name);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington result4 = clean_namehooks(adb, &name->v4);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley name->partial_result &= ~DNS_ADBFIND_INET;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley name->expire_v4 = INT_MAX;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley name->fetch_err = FIND_ERR_UNEXPECTED;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Check to see if we need to remove the v6 addresses
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (!NAME_FETCH_V6(name) &&
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley (expire || EXPIRE_OK(name->expire_v6, now))) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (NAME_HAS_V6(name)) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley DP(DEF_LEVEL, "expiring v6 for name %p", name);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result6 = clean_namehooks(adb, &name->v6);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley name->partial_result &= ~DNS_ADBFIND_INET6;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington }
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington name->expire_v6 = INT_MAX;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley name->fetch6_err = FIND_ERR_UNEXPECTED;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington /*
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington * Check to see if we need to remove the alias target.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (expire || EXPIRE_OK(name->expire_target, now)) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley clean_target(adb, &name->target);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley name->expire_target = INT_MAX;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (ISC_TF(result4 || result6));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson/*
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson * Requires the name's bucket be locked.
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson */
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafssonstatic inline void
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafssonlink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_LIST_PREPEND(adb->names[bucket], name, plink);
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson name->lock_bucket = bucket;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson adb->name_refcnt[bucket]++;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson}
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson/*
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson * Requires the name's bucket be locked.
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson */
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafssonstatic inline isc_boolean_t
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafssonunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington int bucket;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington isc_boolean_t result = ISC_FALSE;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington bucket = name->lock_bucket;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington INSIST(bucket != DNS_ADB_INVALIDBUCKET);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington ISC_LIST_UNLINK(adb->names[bucket], name, plink);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington name->lock_bucket = DNS_ADB_INVALIDBUCKET;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington INSIST(adb->name_refcnt[bucket] > 0);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson adb->name_refcnt[bucket]--;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington result = ISC_TRUE;
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington return (result);
a9ba7e65644c50e1549b38150ba8d4787e1fe643Brian Wellington}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Requires the entry's bucket be locked.
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington */
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonlink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley entry->lock_bucket = bucket;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley adb->entry_refcnt[bucket]++;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley}
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Requires the entry's bucket be locked.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystatic inline isc_boolean_t
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellingtonunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington int bucket;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_boolean_t result = ISC_FALSE;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley bucket = entry->lock_bucket;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(bucket != DNS_ADB_INVALIDBUCKET);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington INSIST(adb->entry_refcnt[bucket] > 0);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington adb->entry_refcnt[bucket]--;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington result = ISC_TRUE;
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington return (result);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington}
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonstatic inline void
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellingtonviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington UNLOCK(have);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington LOCK(want);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington LOCK(have);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington}
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington * The ADB _MUST_ be locked before calling. Also, exit conditions must be
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington * checked after calling this function.
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington */
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonstatic isc_boolean_t
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonshutdown_names(dns_adb_t *adb) {
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington int bucket;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington isc_boolean_t result = ISC_FALSE;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington dns_adbname_t *name;
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington dns_adbname_t *next_name;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley for (bucket = 0; bucket < NBUCKETS; bucket++) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley LOCK(&adb->namelocks[bucket]);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson adb->name_sd[bucket] = ISC_TRUE;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington name = ISC_LIST_HEAD(adb->names[bucket]);
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington if (name == NULL) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * This bucket has no names. We must decrement the
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * irefcnt ourselves, since it will not be
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * automatically triggered by a name being unlinked.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(result == ISC_FALSE);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = dec_adb_irefcnt(adb);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } else {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Run through the list. For each name, clean up finds
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * found there, and cancel any fetches running. When
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * all the fetches are canceled, the name will destroy
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * itself.
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington */
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington while (name != NULL) {
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington next_name = ISC_LIST_NEXT(name, plink);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington INSIST(result == ISC_FALSE);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington result = kill_name(&name,
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff DNS_EVENT_ADBSHUTDOWN);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington name = next_name;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington }
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington UNLOCK(&adb->namelocks[bucket]);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington }
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (result);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington}
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley/*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * The ADB _MUST_ be locked before calling. Also, exit conditions must be
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington * checked after calling this function.
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleystatic isc_boolean_t
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halleyshutdown_entries(dns_adb_t *adb) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley int bucket;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley isc_boolean_t result = ISC_FALSE;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_adbentry_t *entry;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_adbentry_t *next_entry;
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington
77c67dfb2607618f5e7940486daebafd42a502abBrian Wellington for (bucket = 0; bucket < NBUCKETS; bucket++) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington LOCK(&adb->entrylocks[bucket]);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington adb->entry_sd[bucket] = ISC_TRUE;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley entry = ISC_LIST_HEAD(adb->entries[bucket]);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (entry == NULL) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * This bucket has no entries. We must decrement the
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * irefcnt ourselves, since it will not be
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * automatically triggered by an entry being unlinked.
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington */
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington result = dec_adb_irefcnt(adb);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington } else {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington /*
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * Run through the list. Cleanup any entries not
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * associated with names, and which are not in use.
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington */
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews while (entry != NULL) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington next_entry = ISC_LIST_NEXT(entry, plink);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (entry->refcnt == 0 &&
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington entry->expires != 0) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington result = unlink_entry(adb, entry);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington free_adbentry(adb, &entry);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (result)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington result = dec_adb_irefcnt(adb);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington entry = next_entry;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington UNLOCK(&adb->entrylocks[bucket]);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington return (result);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington}
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews/*
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * Name bucket must be locked
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington */
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonstatic void
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellingtoncancel_fetches_at_name(dns_adbname_t *name) {
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington if (NAME_FETCH_A(name))
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington dns_resolver_cancelfetch(name->fetch_a->fetch);
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington
26e5029fd55434db52702313a49e3dc7d47bd328Brian Wellington if (NAME_FETCH_AAAA(name))
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington}
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington/*
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * Assumes the name bucket is locked.
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington */
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonstatic isc_boolean_t
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_adbentry_t *entry;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_adbnamehook_t *namehook;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington int addr_bucket;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_boolean_t result = ISC_FALSE;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington addr_bucket = DNS_ADB_INVALIDBUCKET;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington namehook = ISC_LIST_HEAD(*namehooks);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington while (namehook != NULL) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington /*
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington * Clean up the entry if needed.
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington */
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington entry = namehook->entry;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (entry != NULL) {
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson INSIST(DNS_ADBENTRY_VALID(entry));
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson if (addr_bucket != entry->lock_bucket) {
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson UNLOCK(&adb->entrylocks[addr_bucket]);
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson addr_bucket = entry->lock_bucket;
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson LOCK(&adb->entrylocks[addr_bucket]);
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson }
3ce4b8b03ebd017c1d1b320429219ba91e705ea4Andreas Gustafsson
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = dec_entry_refcnt(adb, entry, ISC_FALSE);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews * Free the namehook
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley namehook->entry = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ISC_LIST_UNLINK(*namehooks, namehook, plink);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley free_adbnamehook(adb, &namehook);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley namehook = ISC_LIST_HEAD(*namehooks);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (addr_bucket != DNS_ADB_INVALIDBUCKET)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington UNLOCK(&adb->entrylocks[addr_bucket]);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington return (result);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington}
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonstatic void
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonclean_target(dns_adb_t *adb, dns_name_t *target) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (dns_name_countlabels(target) > 0) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_name_free(target, adb->mctx);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_name_init(target, NULL);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington}
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonstatic isc_result_t
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellingtonset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_rdataset_t *rdataset, dns_name_t *target)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington{
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_result_t result;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_namereln_t namereln;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington unsigned int nlabels;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington int order;
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson dns_rdata_t rdata = DNS_RDATA_INIT;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence dns_fixedname_t fixed1, fixed2;
78951552dccf0d0004d61072bbc71fa4b1aab30fAndreas Gustafsson dns_name_t *prefix, *new_target;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson REQUIRE(dns_name_countlabels(target) == 0);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson if (rdataset->type == dns_rdatatype_cname) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_rdata_cname_t cname;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson /*
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson * Copy the CNAME's target into the target name.
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson */
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson result = dns_rdataset_first(rdataset);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson if (result != ISC_R_SUCCESS)
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews return (result);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_rdataset_current(rdataset, &rdata);
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington result = dns_rdata_tostruct(&rdata, &cname, NULL);
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington if (result != ISC_R_SUCCESS)
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson return (result);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington result = dns_name_dup(&cname.cname, adb->mctx, target);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson dns_rdata_freestruct(&cname);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if (result != ISC_R_SUCCESS)
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (result);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson } else {
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson dns_rdata_dname_t dname;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence INSIST(rdataset->type == dns_rdatatype_dname);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley namereln = dns_name_fullcompare(name, fname, &order, &nlabels);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(namereln == dns_namereln_subdomain);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Get the target name of the DNAME.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence result = dns_rdataset_first(rdataset);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (result != ISC_R_SUCCESS)
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (result);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson dns_rdataset_current(rdataset, &rdata);
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson result = dns_rdata_tostruct(&rdata, &dname, NULL);
0a3e2e1d590dac7fb011e72bd3a4982c179d8e68Brian Wellington if (result != ISC_R_SUCCESS)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (result);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley /*
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley * Construct the new target name.
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington */
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington dns_fixedname_init(&fixed1);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington prefix = dns_fixedname_name(&fixed1);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson dns_fixedname_init(&fixed2);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington new_target = dns_fixedname_name(&fixed2);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington dns_name_split(name, nlabels, prefix, NULL);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington result = dns_name_concatenate(prefix, &dname.dname, new_target,
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington NULL);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington dns_rdata_freestruct(&dname);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington if (result != ISC_R_SUCCESS)
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington return (result);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington result = dns_name_dup(new_target, adb->mctx, target);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington if (result != ISC_R_SUCCESS)
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington return (result);
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington }
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington return (ISC_R_SUCCESS);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington}
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington/*
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington * Assumes nothing is locked, since this is called by the client.
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington */
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellingtonstatic void
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellingtonevent_free(isc_event_t *event) {
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington dns_adbfind_t *find;
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington INSIST(event != NULL);
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington find = event->ev_destroy_arg;
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington INSIST(DNS_ADBFIND_VALID(find));
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington LOCK(&find->lock);
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington find->flags |= FIND_EVENT_FREED;
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington event->ev_destroy_arg = NULL;
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington UNLOCK(&find->lock);
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington}
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington/*
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellington * Assumes the name bucket is locked.
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington */
ba393f380e4cd93029f6a7291d6c2d14f9022b3cBrian Wellingtonstatic void
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellingtonclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson unsigned int addrs)
d6643ef587324e40d8bda63e9f80be8141e101edBrian Wellington{
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington isc_event_t *ev;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington isc_task_t *task;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington dns_adbfind_t *find;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington dns_adbfind_t *next_find;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington isc_boolean_t process;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington unsigned int wanted, notify;
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington DP(ENTER_LEVEL,
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington name, evtype, addrs);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington find = ISC_LIST_HEAD(name->finds);
75f6c57d9544aa77a3b1a04587b4702c07343c90Brian Wellington while (find != NULL) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington LOCK(&find->lock);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington next_find = ISC_LIST_NEXT(find, plink);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington process = ISC_FALSE;
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington notify = wanted & addrs;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington switch (evtype) {
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington case DNS_EVENT_ADBMOREADDRESSES:
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington if ((notify) != 0) {
187604c1adfe841d909d4a8453b6900e652f7f6dBrian Wellington find->flags &= ~addrs;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington process = ISC_TRUE;
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington }
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington break;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson case DNS_EVENT_ADBNOMOREADDRESSES:
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
538fea1c91c68c0a5569c7b8552c8fd0490055efBrian Wellington find->flags &= ~addrs;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington if (wanted == 0)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson process = ISC_TRUE;
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington break;
e49c834de8cdf92d4b85ef0fbf1d9dc59620a342Brian Wellington default:
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson find->flags &= ~addrs;
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington process = ISC_TRUE;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson }
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington if (process) {
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington DP(DEF_LEVEL, "cfan: processing find %p", find);
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington /*
e1f16346db02486f751c6db683fffe53c866c186Andreas Gustafsson * Unlink the find from the name, letting the caller
e1f16346db02486f751c6db683fffe53c866c186Andreas Gustafsson * call dns_adb_destroyfind() on it to clean it up
e1f16346db02486f751c6db683fffe53c866c186Andreas Gustafsson * later.
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ISC_LIST_UNLINK(name->finds, find, plink);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington find->adbname = NULL;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington find->name_bucket = DNS_ADB_INVALIDBUCKET;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington INSIST(!FIND_EVENTSENT(find));
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington ev = &find->event;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington task = ev->ev_sender;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington ev->ev_sender = find;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington find->result_v4 = find_err_map[name->fetch_err];
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington find->result_v6 = find_err_map[name->fetch6_err];
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington ev->ev_type = evtype;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ev->ev_destroy = event_free;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington ev->ev_destroy_arg = find;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington DP(DEF_LEVEL,
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington "sending event %p to task %p for find %p",
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington ev, task, find);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington isc_task_sendanddetach(&task, (isc_event_t **)&ev);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington } else {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington DP(DEF_LEVEL, "cfan: skipping find %p", find);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington }
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington UNLOCK(&find->lock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington find = next_find;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington}
f15af68028adc665d3bdddf955fc52bad83f0514Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline void
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtoncheck_exit(dns_adb_t *adb) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington isc_event_t *event;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington /*
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * The caller must be holding the adb lock.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (adb->shutting_down) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington /*
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * If there aren't any external references either, we're
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * done. Send the control event to initiate shutdown.
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington INSIST(!adb->cevent_sent); /* Sanity check. */
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington event = &adb->cevent;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington isc_task_send(adb->task, &event);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington adb->cevent_sent = ISC_TRUE;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington }
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington}
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline isc_boolean_t
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellingtondec_adb_irefcnt(dns_adb_t *adb) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington isc_event_t *event;
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington isc_task_t *etask;
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington isc_boolean_t result = ISC_FALSE;
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington LOCK(&adb->reflock);
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington INSIST(adb->irefcnt > 0);
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington adb->irefcnt--;
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington if (adb->irefcnt == 0) {
d6be55c63f83194d97a565d0fd7b632b31b52a68Brian Wellington event = ISC_LIST_HEAD(adb->whenshutdown);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington while (event != NULL) {
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington etask = event->ev_sender;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews event->ev_sender = adb;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington isc_task_sendanddetach(&etask, &event);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington event = ISC_LIST_HEAD(adb->whenshutdown);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington }
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington }
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington if (adb->irefcnt == 0 && adb->erefcnt == 0)
c70908209ee26c51a8e7242a56fdb73847249728Brian Wellington result = ISC_TRUE;
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington UNLOCK(&adb->reflock);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington return (result);
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington}
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellington
32b2cdf212de957e3f9b0efca59f098ed4fb42deBrian Wellingtonstatic inline void
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtoninc_adb_irefcnt(dns_adb_t *adb) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington LOCK(&adb->reflock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington adb->irefcnt++;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington UNLOCK(&adb->reflock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington}
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline void
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtoninc_adb_erefcnt(dns_adb_t *adb) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington LOCK(&adb->reflock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington adb->erefcnt++;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington UNLOCK(&adb->reflock);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington}
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellingtonstatic inline void
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington int bucket;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington bucket = entry->lock_bucket;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (lock)
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington LOCK(&adb->entrylocks[bucket]);
777ac454c0cdec27dc11d80b9b2a8d7239d833a8Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington entry->refcnt++;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington if (lock)
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington}
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellingtonstatic inline isc_boolean_t
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellingtondec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington int bucket;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington isc_boolean_t destroy_entry;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington isc_boolean_t result = ISC_FALSE;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington bucket = entry->lock_bucket;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (lock)
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington LOCK(&adb->entrylocks[bucket]);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington INSIST(entry->refcnt > 0);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson entry->refcnt--;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington destroy_entry = ISC_FALSE;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington if (entry->refcnt == 0 &&
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington (adb->entry_sd[bucket] || entry->expires == 0)) {
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington destroy_entry = ISC_TRUE;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington result = unlink_entry(adb, entry);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington if (lock)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (!destroy_entry)
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington return (result);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington free_adbentry(adb, &entry);
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington if (result)
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington result =dec_adb_irefcnt(adb);
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington return (result);
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington}
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonstatic inline dns_adbname_t *
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonnew_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_adbname_t *name;
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington name = isc_mempool_get(adb->nmp);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (name == NULL)
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington return (NULL);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington dns_name_init(&name->name, NULL);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington isc_mempool_put(adb->nmp, name);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington return (NULL);
25496cebadd170fd5fae2aabf0469eef551259aaBrian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington dns_name_init(&name->target, NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington name->magic = DNS_ADBNAME_MAGIC;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington name->adb = adb;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson name->partial_result = 0;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington name->flags = 0;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington name->expire_v4 = INT_MAX;
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington name->expire_v6 = INT_MAX;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington name->expire_target = INT_MAX;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington name->chains = 0;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington name->lock_bucket = DNS_ADB_INVALIDBUCKET;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ISC_LIST_INIT(name->v4);
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson ISC_LIST_INIT(name->v6);
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson name->fetch_a = NULL;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington name->fetch_aaaa = NULL;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington name->fetch_err = FIND_ERR_UNEXPECTED;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington name->fetch6_err = FIND_ERR_UNEXPECTED;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington ISC_LIST_INIT(name->finds);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington ISC_LINK_INIT(name, plink);
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington return (name);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonstatic inline void
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonfree_adbname(dns_adb_t *adb, dns_adbname_t **name) {
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington dns_adbname_t *n;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson n = *name;
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson *name = NULL;
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson
6036112f4874637240d461c3ccbcb8dbfb1f405bAndreas Gustafsson INSIST(!NAME_HAS_V4(n));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(!NAME_HAS_V6(n));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(!NAME_FETCH(n));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(ISC_LIST_EMPTY(n->finds));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(!ISC_LINK_LINKED(n, plink));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington INSIST(n->adb == adb);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington n->magic = 0;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_name_free(&n->name, adb->mctx);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_mempool_put(adb->nmp, n);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline dns_adbnamehook_t *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbnamehook_t *nh;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson nh = isc_mempool_get(adb->nhmp);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (nh == NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence nh->magic = DNS_ADBNAMEHOOK_MAGIC;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington nh->entry = entry;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ISC_LINK_INIT(nh, plink);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington return (nh);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington}
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellingtonstatic inline void
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellingtonfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson dns_adbnamehook_t *nh;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington nh = *namehook;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington *namehook = NULL;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington INSIST(nh->entry == NULL);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson INSIST(!ISC_LINK_LINKED(nh, plink));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington nh->magic = 0;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington isc_mempool_put(adb->nhmp, nh);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellingtonstatic inline dns_adblameinfo_t *
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellingtonnew_adblameinfo(dns_adb_t *adb, dns_name_t *qname, dns_rdatatype_t qtype) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adblameinfo_t *li;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington li = isc_mempool_get(adb->limp);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (li == NULL)
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington return (NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington dns_name_init(&li->qname, NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington if (dns_name_dup(qname, adb->mctx, &li->qname) != ISC_R_SUCCESS) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_mempool_put(adb->limp, li);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington }
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington li->magic = DNS_ADBLAMEINFO_MAGIC;
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington li->lame_timer = 0;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington li->qtype = qtype;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ISC_LINK_INIT(li, plink);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence return (li);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline void
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonfree_adblameinfo(dns_adb_t *adb, dns_adblameinfo_t **lameinfo) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adblameinfo_t *li;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington INSIST(lameinfo != NULL && DNS_ADBLAMEINFO_VALID(*lameinfo));
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington li = *lameinfo;
48ed268b3378a8b729a0037bc4ae2ed73647a96aBrian Wellington *lameinfo = NULL;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington INSIST(!ISC_LINK_LINKED(li, plink));
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington dns_name_free(&li->qname, adb->mctx);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington li->magic = 0;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_mempool_put(adb->limp, li);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington}
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellingtonstatic inline dns_adbentry_t *
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonnew_adbentry(dns_adb_t *adb) {
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington dns_adbentry_t *e;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington isc_uint32_t r;
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington e = isc_mempool_get(adb->emp);
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington if (e == NULL)
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellington return (NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington e->magic = DNS_ADBENTRY_MAGIC;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington e->lock_bucket = DNS_ADB_INVALIDBUCKET;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington e->refcnt = 0;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington e->flags = 0;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington isc_random_get(&r);
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington e->srtt = (r & 0x1f) + 1;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley e->expires = 0;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington ISC_LIST_INIT(e->lameinfo);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff ISC_LINK_INIT(e, plink);
115635379a2baf2100695018109ad39e0dac349dAndreas Gustafsson
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington return (e);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington}
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonstatic inline void
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellingtonfree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington dns_adbentry_t *e;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson dns_adblameinfo_t *li;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley e = *entry;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley *entry = NULL;
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(e->refcnt == 0);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(!ISC_LINK_LINKED(e, plink));
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley e->magic = 0;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson li = ISC_LIST_HEAD(e->lameinfo);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington while (li != NULL) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ISC_LIST_UNLINK(e->lameinfo, li, plink);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley free_adblameinfo(adb, &li);
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington li = ISC_LIST_HEAD(e->lameinfo);
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington }
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington isc_mempool_put(adb->emp, e);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson}
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellingtonstatic inline dns_adbfind_t *
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellingtonnew_adbfind(dns_adb_t *adb) {
60783293cc27f74a84ec93c95c5d46edd30bd8e0Brian Wellington dns_adbfind_t *h;
6bc1a645619a14707da68b130dafe41721fd2f25Brian Wellington isc_result_t result;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington h = isc_mempool_get(adb->ahmp);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson if (h == NULL)
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson return (NULL);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson /*
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington * Public members.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence */
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington h->magic = 0;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington h->adb = adb;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington h->partial_result = 0;
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington h->options = 0;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley h->flags = 0;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley h->result_v4 = ISC_R_UNEXPECTED;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley h->result_v6 = ISC_R_UNEXPECTED;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson ISC_LINK_INIT(h, publink);
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington ISC_LINK_INIT(h, plink);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ISC_LIST_INIT(h->list);
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington h->adbname = NULL;
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington h->name_bucket = DNS_ADB_INVALIDBUCKET;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * private members
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington */
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley result = isc_mutex_init(&h->lock);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (result != ISC_R_SUCCESS) {
93c786e0924aeca2c258e32355349e6ae60a0f72Andreas Gustafsson isc_mempool_put(adb->ahmp, h);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (NULL);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley NULL, NULL, h);
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington inc_adb_irefcnt(adb);
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley h->magic = DNS_ADBFIND_MAGIC;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley return (h);
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley}
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halleystatic inline dns_adbfetch_t *
e419f613d8591885df608cb73065921be07dd12eBob Halleynew_adbfetch(dns_adb_t *adb) {
e419f613d8591885df608cb73065921be07dd12eBob Halley dns_adbfetch_t *f;
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley f = isc_mempool_get(adb->afmp);
e419f613d8591885df608cb73065921be07dd12eBob Halley if (f == NULL)
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington return (NULL);
59e99793307eed0914f8467243d1c4ac761b1d9cAndreas Gustafsson
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington f->magic = 0;
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington f->namehook = NULL;
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson f->entry = NULL;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley f->fetch = NULL;
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley f->namehook = new_adbnamehook(adb, NULL);
e419f613d8591885df608cb73065921be07dd12eBob Halley if (f->namehook == NULL)
e419f613d8591885df608cb73065921be07dd12eBob Halley goto err;
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson
e419f613d8591885df608cb73065921be07dd12eBob Halley f->entry = new_adbentry(adb);
e419f613d8591885df608cb73065921be07dd12eBob Halley if (f->entry == NULL)
62a84c4a27033bb0e7316256964a6950b1e230bdAndreas Gustafsson goto err;
ef97e09e20da2133adc731cf7e29e72d04dfc93fAndreas Gustafsson
e419f613d8591885df608cb73065921be07dd12eBob Halley dns_rdataset_init(&f->rdataset);
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington f->magic = DNS_ADBFETCH_MAGIC;
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson return (f);
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley err:
e419f613d8591885df608cb73065921be07dd12eBob Halley if (f->namehook != NULL)
e419f613d8591885df608cb73065921be07dd12eBob Halley free_adbnamehook(adb, &f->namehook);
e419f613d8591885df608cb73065921be07dd12eBob Halley if (f->entry != NULL)
e419f613d8591885df608cb73065921be07dd12eBob Halley free_adbentry(adb, &f->entry);
e419f613d8591885df608cb73065921be07dd12eBob Halley isc_mempool_put(adb->afmp, f);
e419f613d8591885df608cb73065921be07dd12eBob Halley return (NULL);
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington}
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halleystatic inline void
e419f613d8591885df608cb73065921be07dd12eBob Halleyfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
e419f613d8591885df608cb73065921be07dd12eBob Halley dns_adbfetch_t *f;
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
e419f613d8591885df608cb73065921be07dd12eBob Halley f = *fetch;
e419f613d8591885df608cb73065921be07dd12eBob Halley *fetch = NULL;
e419f613d8591885df608cb73065921be07dd12eBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley f->magic = 0;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington if (f->namehook != NULL)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley free_adbnamehook(adb, &f->namehook);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington if (f->entry != NULL)
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington free_adbentry(adb, &f->entry);
3676eeb6ca95c66aae1256f37af8c990d9f25eb4Brian Wellington
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington if (dns_rdataset_isassociated(&f->rdataset))
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington dns_rdataset_disassociate(&f->rdataset);
613efcd8fbd0d1ce0d0afd1ac85d95cf85bffc27Brian Wellington
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington isc_mempool_put(adb->afmp, f);
feb40fc5f911d0b2050fb9fd34950a52930b981dBrian Wellington}
5c6117688525d0e8d247f50c63364f66bd8d4185Brian Wellington
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonstatic inline isc_boolean_t
ca9af3aaf798f98624fc1dc69d8c7d51bf01334dBrian Wellingtonfree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington dns_adbfind_t *find;
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
b5debbe212097d1c573a2ba3bd9a3d526d86b0aeBrian Wellington find = *findp;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley *findp = NULL;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(!FIND_HAS_ADDRS(find));
e419f613d8591885df608cb73065921be07dd12eBob Halley INSIST(!ISC_LINK_LINKED(find, publink));
e419f613d8591885df608cb73065921be07dd12eBob Halley INSIST(!ISC_LINK_LINKED(find, plink));
e419f613d8591885df608cb73065921be07dd12eBob Halley INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
e419f613d8591885df608cb73065921be07dd12eBob Halley INSIST(find->adbname == NULL);
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley find->magic = 0;
e419f613d8591885df608cb73065921be07dd12eBob Halley
ef97e09e20da2133adc731cf7e29e72d04dfc93fAndreas Gustafsson DESTROYLOCK(&find->lock);
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson isc_mempool_put(adb->ahmp, find);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence return (dec_adb_irefcnt(adb));
e419f613d8591885df608cb73065921be07dd12eBob Halley}
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley/*
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * Copy bits from the entry into the newly allocated addrinfo. The entry
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley * must be locked, and the reference count must be bumped up by one
e419f613d8591885df608cb73065921be07dd12eBob Halley * if this function returns a valid pointer.
e419f613d8591885df608cb73065921be07dd12eBob Halley */
e419f613d8591885df608cb73065921be07dd12eBob Halleystatic inline dns_adbaddrinfo_t *
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafssonnew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson dns_adbaddrinfo_t *ai;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington
e419f613d8591885df608cb73065921be07dd12eBob Halley ai = isc_mempool_get(adb->aimp);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington if (ai == NULL)
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson return (NULL);
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson ai->magic = DNS_ADBADDRINFO_MAGIC;
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson ai->sockaddr = entry->sockaddr;
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson isc_sockaddr_setport(&ai->sockaddr, port);
264fd373f3f6cc7f271bdff14a020385620015f1Andreas Gustafsson ai->srtt = entry->srtt;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ai->flags = entry->flags;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ai->entry = entry;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington ISC_LINK_INIT(ai, publink);
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halley return (ai);
e419f613d8591885df608cb73065921be07dd12eBob Halley}
e419f613d8591885df608cb73065921be07dd12eBob Halley
e419f613d8591885df608cb73065921be07dd12eBob Halleystatic inline void
e419f613d8591885df608cb73065921be07dd12eBob Halleyfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson dns_adbaddrinfo_t *ai;
e419f613d8591885df608cb73065921be07dd12eBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
e419f613d8591885df608cb73065921be07dd12eBob Halley ai = *ainfo;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley *ainfo = NULL;
e419f613d8591885df608cb73065921be07dd12eBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(ai->entry == NULL);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley INSIST(!ISC_LINK_LINKED(ai, publink));
e83cae7fa837e4757c687035d6f6c0900f152749Brian Wellington
c50936eb40263b65ebf6afe4e6556e2dc67c10e4Brian Wellington ai->magic = 0;
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington isc_mempool_put(adb->aimp, ai);
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington}
e27021ee1f37185ab839a7a3b6bc078c24255e62Brian Wellington
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson/*
fe5ba8ddb55b2b3ee139e13b7891817117ad4e63Brian Wellington * Search for the name. NOTE: The bucket is kept locked on both
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson * success and failure, so it must always be unlocked by the caller!
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington *
ef97e09e20da2133adc731cf7e29e72d04dfc93fAndreas Gustafsson * On the first call to this function, *bucketp must be set to
1872808932603066d401d3de97db11af8ffee78aAndreas Gustafsson * DNS_ADB_INVALIDBUCKET.
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafsson */
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halleystatic inline dns_adbname_t *
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halleyfind_name_and_lock(dns_adb_t *adb, dns_name_t *name,
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley unsigned int options, int *bucketp)
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley{
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley dns_adbname_t *adbname;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley int bucket;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley bucket = dns_name_fullhash(name, ISC_FALSE) % NBUCKETS;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (*bucketp == DNS_ADB_INVALIDBUCKET) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley LOCK(&adb->namelocks[bucket]);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley *bucketp = bucket;
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley } else if (*bucketp != bucket) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley UNLOCK(&adb->namelocks[*bucketp]);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley LOCK(&adb->namelocks[bucket]);
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson *bucketp = bucket;
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington adbname = ISC_LIST_HEAD(adb->names[bucket]);
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington while (adbname != NULL) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (!NAME_DEAD(adbname)) {
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley if (dns_name_equal(name, &adbname->name)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley && GLUEHINT_OK(adbname, options)
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley && STARTATZONE_MATCHES(adbname, options))
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley return (adbname);
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley }
0ec4b862c9abd11c82c88ed62438f0cf06fed25dBob Halley adbname = ISC_LIST_NEXT(adbname, plink);
e419f613d8591885df608cb73065921be07dd12eBob Halley }
bf43fdafa3bff9e84cb03f1a19aca74514d2516eBob Halley
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson return (NULL);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson}
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson/*
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson * Search for the address. NOTE: The bucket is kept locked on both
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson * success and failure, so it must always be unlocked by the caller.
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson *
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * On the first call to this function, *bucketp must be set to
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
9cd6710f91bdffef5aed68ab02533e398f6134d7Brian Wellington * later calls (within the same "lock path") it can be left alone, so
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * if this function is called multiple times locking is only done if
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * the bucket changes.
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson */
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonstatic inline dns_adbentry_t *
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
94766449d6125cd5870891b70d46573e5deaceb4Brian Wellington dns_adbentry_t *entry;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson int bucket;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
ecfe4a349073b60eeaf7f1362d7dd48695514205Andreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (*bucketp == DNS_ADB_INVALIDBUCKET) {
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson LOCK(&adb->entrylocks[bucket]);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson *bucketp = bucket;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence } else if (*bucketp != bucket) {
ec371edc34e2adb9e337b774d1a6e613f5863655Brian Wellington UNLOCK(&adb->entrylocks[*bucketp]);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson LOCK(&adb->entrylocks[bucket]);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson *bucketp = bucket;
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson }
8db70f36bee634ad850bc5ab60afab5c6123bc01Andreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson entry = ISC_LIST_HEAD(adb->entries[bucket]);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence while (entry != NULL) {
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (isc_sockaddr_equal(addr, &entry->sockaddr))
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson return (entry);
115635379a2baf2100695018109ad39e0dac349dAndreas Gustafsson entry = ISC_LIST_NEXT(entry, plink);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson }
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson return (NULL);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence}
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson/*
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson * Entry bucket MUST be locked!
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson */
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonstatic isc_boolean_t
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafssonentry_is_lame(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *qname,
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington dns_rdatatype_t qtype, isc_stdtime_t now)
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington{
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington dns_adblameinfo_t *li, *next_li;
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington isc_boolean_t is_bad;
5b0413f993b1c1ed837d23641e9f696cda1ee293Brian Wellington
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson is_bad = ISC_FALSE;
34aa7909371f13b4bc0ba6d155cfc38bfa1e3c5cAndreas Gustafsson
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson li = ISC_LIST_HEAD(entry->lameinfo);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson if (li == NULL)
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson return (ISC_FALSE);
1b1e1fda4638334b484aa38c15f53a131c0b0fdfAndreas Gustafsson while (li != NULL) {
next_li = ISC_LIST_NEXT(li, plink);
/*
* Has the entry expired?
*/
if (li->lame_timer < now) {
ISC_LIST_UNLINK(entry->lameinfo, li, plink);
free_adblameinfo(adb, &li);
}
/*
* Order tests from least to most expensive.
*
* We do not break out of the main loop here as
* we use the loop for house keeping.
*/
if (li != NULL && !is_bad && li->qtype == qtype &&
dns_name_equal(qname, &li->qname))
is_bad = ISC_TRUE;
li = next_li;
}
return (is_bad);
}
static void
copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *qname,
dns_rdatatype_t qtype, dns_adbname_t *name,
isc_stdtime_t now)
{
dns_adbnamehook_t *namehook;
dns_adbaddrinfo_t *addrinfo;
dns_adbentry_t *entry;
int bucket;
bucket = DNS_ADB_INVALIDBUCKET;
if (find->options & DNS_ADBFIND_INET) {
namehook = ISC_LIST_HEAD(name->v4);
while (namehook != NULL) {
entry = namehook->entry;
bucket = entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
if (!FIND_RETURNLAME(find)
&& entry_is_lame(adb, entry, qname, qtype, now)) {
find->options |= DNS_ADBFIND_LAMEPRUNED;
goto nextv4;
}
addrinfo = new_adbaddrinfo(adb, entry, find->port);
if (addrinfo == NULL) {
find->partial_result |= DNS_ADBFIND_INET;
goto out;
}
/*
* Found a valid entry. Add it to the find's list.
*/
inc_entry_refcnt(adb, entry, ISC_FALSE);
ISC_LIST_APPEND(find->list, addrinfo, publink);
addrinfo = NULL;
nextv4:
UNLOCK(&adb->entrylocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_NEXT(namehook, plink);
}
}
if (find->options & DNS_ADBFIND_INET6) {
namehook = ISC_LIST_HEAD(name->v6);
while (namehook != NULL) {
entry = namehook->entry;
bucket = entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
if (entry_is_lame(adb, entry, qname, qtype, now))
goto nextv6;
addrinfo = new_adbaddrinfo(adb, entry, find->port);
if (addrinfo == NULL) {
find->partial_result |= DNS_ADBFIND_INET6;
goto out;
}
/*
* Found a valid entry. Add it to the find's list.
*/
inc_entry_refcnt(adb, entry, ISC_FALSE);
ISC_LIST_APPEND(find->list, addrinfo, publink);
addrinfo = NULL;
nextv6:
UNLOCK(&adb->entrylocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_NEXT(namehook, plink);
}
}
out:
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[bucket]);
}
static void
shutdown_task(isc_task_t *task, isc_event_t *ev) {
dns_adb_t *adb;
UNUSED(task);
adb = ev->ev_arg;
INSIST(DNS_ADB_VALID(adb));
/*
* Kill the timer, and then the ADB itself. Note that this implies
* that this task was the one scheduled to get timer events. If
* this is not true (and it is unfortunate there is no way to INSIST()
* this) badness will occur.
*/
LOCK(&adb->lock);
isc_timer_detach(&adb->timer);
UNLOCK(&adb->lock);
isc_event_free(&ev);
destroy(adb);
}
/*
* Name bucket must be locked; adb may be locked; no other locks held.
*/
static isc_boolean_t
check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
dns_adbname_t *name;
isc_boolean_t result = ISC_FALSE;
INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
name = *namep;
if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
return (result);
if (NAME_FETCH(name))
return (result);
if (!EXPIRE_OK(name->expire_v4, now))
return (result);
if (!EXPIRE_OK(name->expire_v6, now))
return (result);
if (!EXPIRE_OK(name->expire_target, now))
return (result);
/*
* The name is empty. Delete it.
*/
result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
*namep = NULL;
/*
* Our caller, or one of its callers, will be calling check_exit() at
* some point, so we don't need to do it here.
*/
return (result);
}
/*
* Entry bucket must be locked; adb may be locked; no other locks held.
*/
static isc_boolean_t
check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
{
dns_adbentry_t *entry;
isc_boolean_t expire;
isc_boolean_t result = ISC_FALSE;
INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
entry = *entryp;
if (entry->refcnt != 0)
return (result);
if (adb->overmem) {
isc_uint32_t val;
isc_random_get(&val);
expire = ISC_TF((val % 4) == 0);
} else
expire = ISC_FALSE;
if (entry->expires == 0 || (! expire && entry->expires > now))
return (result);
/*
* The entry is not in use. Delete it.
*/
DP(DEF_LEVEL, "killing entry %p", entry);
INSIST(ISC_LINK_LINKED(entry, plink));
result = unlink_entry(adb, entry);
free_adbentry(adb, &entry);
if (result)
dec_adb_irefcnt(adb);
*entryp = NULL;
return (result);
}
/*
* ADB must be locked, and no other locks held.
*/
static isc_boolean_t
cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
dns_adbname_t *name;
dns_adbname_t *next_name;
isc_boolean_t result = ISC_FALSE;
DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
LOCK(&adb->namelocks[bucket]);
if (adb->name_sd[bucket]) {
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
name = ISC_LIST_HEAD(adb->names[bucket]);
while (name != NULL) {
next_name = ISC_LIST_NEXT(name, plink);
INSIST(result == ISC_FALSE);
result = check_expire_namehooks(name, now, adb->overmem);
if (!result)
result = check_expire_name(&name, now);
name = next_name;
}
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
/*
* ADB must be locked, and no other locks held.
*/
static isc_boolean_t
cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
dns_adbentry_t *entry, *next_entry;
isc_boolean_t result = ISC_FALSE;
DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
LOCK(&adb->entrylocks[bucket]);
entry = ISC_LIST_HEAD(adb->entries[bucket]);
while (entry != NULL) {
next_entry = ISC_LIST_NEXT(entry, plink);
INSIST(result == ISC_FALSE);
result = check_expire_entry(adb, &entry, now);
entry = next_entry;
}
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
static void
timer_cleanup(isc_task_t *task, isc_event_t *ev) {
dns_adb_t *adb;
isc_stdtime_t now;
unsigned int i;
isc_interval_t interval;
UNUSED(task);
adb = ev->ev_arg;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
isc_stdtime_get(&now);
for (i = 0; i < CLEAN_BUCKETS; i++) {
/*
* Call our cleanup routines.
*/
RUNTIME_CHECK(cleanup_names(adb, adb->next_cleanbucket, now) ==
ISC_FALSE);
RUNTIME_CHECK(cleanup_entries(adb, adb->next_cleanbucket, now)
== ISC_FALSE);
/*
* Set the next bucket to be cleaned.
*/
adb->next_cleanbucket++;
if (adb->next_cleanbucket >= NBUCKETS) {
adb->next_cleanbucket = 0;
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE, now);
#endif
}
}
/*
* Reset the timer.
* XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
* ISC_R_NOMEMORY, but it isn't clear what could be done here
* if either one of those things happened.
*/
interval = adb->tick_interval;
if (adb->overmem)
isc_interval_set(&interval, 0, 1);
(void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
&interval, ISC_FALSE);
UNLOCK(&adb->lock);
isc_event_free(&ev);
}
static void
destroy(dns_adb_t *adb) {
adb->magic = 0;
/*
* The timer is already dead, from the task's shutdown callback.
*/
isc_task_detach(&adb->task);
isc_mempool_destroy(&adb->nmp);
isc_mempool_destroy(&adb->nhmp);
isc_mempool_destroy(&adb->limp);
isc_mempool_destroy(&adb->emp);
isc_mempool_destroy(&adb->ahmp);
isc_mempool_destroy(&adb->aimp);
isc_mempool_destroy(&adb->afmp);
DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
DESTROYLOCK(&adb->reflock);
DESTROYLOCK(&adb->lock);
DESTROYLOCK(&adb->mplock);
isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
}
/*
* Public functions.
*/
isc_result_t
dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
{
dns_adb_t *adb;
isc_result_t result;
int i;
REQUIRE(mem != NULL);
REQUIRE(view != NULL);
REQUIRE(timermgr != NULL);
REQUIRE(taskmgr != NULL);
REQUIRE(newadb != NULL && *newadb == NULL);
adb = isc_mem_get(mem, sizeof(dns_adb_t));
if (adb == NULL)
return (ISC_R_NOMEMORY);
/*
* Initialize things here that cannot fail, and especially things
* that must be NULL for the error return to work properly.
*/
adb->magic = 0;
adb->erefcnt = 1;
adb->irefcnt = 0;
adb->nmp = NULL;
adb->nhmp = NULL;
adb->limp = NULL;
adb->emp = NULL;
adb->ahmp = NULL;
adb->aimp = NULL;
adb->afmp = NULL;
adb->task = NULL;
adb->timer = NULL;
adb->mctx = NULL;
adb->view = view;
adb->timermgr = timermgr;
adb->taskmgr = taskmgr;
adb->next_cleanbucket = 0;
ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
DNS_EVENT_ADBCONTROL, shutdown_task, adb,
adb, NULL, NULL);
adb->cevent_sent = ISC_FALSE;
adb->shutting_down = ISC_FALSE;
adb->overmem = ISC_FALSE;
ISC_LIST_INIT(adb->whenshutdown);
isc_mem_attach(mem, &adb->mctx);
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail0b;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail0c;
result = isc_mutex_init(&adb->reflock);
if (result != ISC_R_SUCCESS)
goto fail0d;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0; i < NBUCKETS; i++) {
ISC_LIST_INIT(adb->names[i]);
adb->name_sd[i] = ISC_FALSE;
adb->name_refcnt[i] = 0;
adb->irefcnt++;
}
for (i = 0; i < NBUCKETS; i++) {
ISC_LIST_INIT(adb->entries[i]);
adb->entry_sd[i] = ISC_FALSE;
adb->entry_refcnt[i] = 0;
adb->irefcnt++;
}
result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, n) do { \
result = isc_mempool_create(mem, sizeof(t), &(p)); \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
isc_mempool_setname((p), n); \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT(dns_adbname_t, adb->nmp, "adbname");
MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
MPINIT(dns_adblameinfo_t, adb->limp, "adblameinfo");
MPINIT(dns_adbentry_t, adb->emp, "adbentry");
MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
#undef MPINIT
/*
* Allocate a timer and a task for our periodic cleanup.
*/
result = isc_task_create(adb->taskmgr, 0, &adb->task);
if (result != ISC_R_SUCCESS)
goto fail3;
isc_task_setname(adb->task, "ADB", adb);
/*
* XXXMLG When this is changed to be a config file option,
*/
isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
result = isc_timer_create(adb->timermgr, isc_timertype_once,
NULL, &adb->tick_interval, adb->task,
timer_cleanup, adb, &adb->timer);
if (result != ISC_R_SUCCESS)
goto fail3;
DP(ISC_LOG_DEBUG(5), "cleaning interval for adb: "
"%u buckets every %u seconds, %u buckets in system, %u cl.interval",
CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
/*
* Normal return.
*/
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3:
if (adb->task != NULL)
isc_task_detach(&adb->task);
if (adb->timer != NULL)
isc_timer_detach(&adb->timer);
/* clean up entrylocks */
DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
fail2: /* clean up namelocks */
DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->limp != NULL)
isc_mempool_destroy(&adb->limp);
if (adb->emp != NULL)
isc_mempool_destroy(&adb->emp);
if (adb->ahmp != NULL)
isc_mempool_destroy(&adb->ahmp);
if (adb->aimp != NULL)
isc_mempool_destroy(&adb->aimp);
if (adb->afmp != NULL)
isc_mempool_destroy(&adb->afmp);
DESTROYLOCK(&adb->reflock);
fail0d:
DESTROYLOCK(&adb->mplock);
fail0c:
DESTROYLOCK(&adb->lock);
fail0b:
isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
return (result);
}
void
dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(adbx != NULL && *adbx == NULL);
inc_adb_erefcnt(adb);
*adbx = adb;
}
void
dns_adb_detach(dns_adb_t **adbx) {
dns_adb_t *adb;
isc_boolean_t need_exit_check;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
INSIST(adb->erefcnt > 0);
LOCK(&adb->reflock);
adb->erefcnt--;
need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
UNLOCK(&adb->reflock);
if (need_exit_check) {
LOCK(&adb->lock);
INSIST(adb->shutting_down);
check_exit(adb);
UNLOCK(&adb->lock);
}
}
void
dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
isc_task_t *clone;
isc_event_t *event;
isc_boolean_t zeroirefcnt = ISC_FALSE;
/*
* Send '*eventp' to 'task' when 'adb' has shutdown.
*/
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(eventp != NULL);
event = *eventp;
*eventp = NULL;
LOCK(&adb->lock);
LOCK(&adb->reflock);
zeroirefcnt = ISC_TF(adb->irefcnt == 0);
if (adb->shutting_down && zeroirefcnt &&
isc_mempool_getallocated(adb->ahmp) == 0) {
/*
* We're already shutdown. Send the event.
*/
event->ev_sender = adb;
isc_task_send(task, &event);
} else {
clone = NULL;
isc_task_attach(task, &clone);
event->ev_sender = clone;
ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
}
UNLOCK(&adb->reflock);
UNLOCK(&adb->lock);
}
void
dns_adb_shutdown(dns_adb_t *adb) {
isc_boolean_t need_check_exit;
/*
* Shutdown 'adb'.
*/
LOCK(&adb->lock);
if (!adb->shutting_down) {
adb->shutting_down = ISC_TRUE;
isc_mem_setwater(adb->mctx, water, adb, 0, 0);
need_check_exit = shutdown_names(adb);
if (!need_check_exit)
need_check_exit = shutdown_entries(adb);
if (need_check_exit)
check_exit(adb);
}
UNLOCK(&adb->lock);
}
isc_result_t
dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
void *arg, dns_name_t *name, dns_name_t *qname,
dns_rdatatype_t qtype, unsigned int options,
isc_stdtime_t now, dns_name_t *target,
in_port_t port, dns_adbfind_t **findp)
{
dns_adbfind_t *find;
dns_adbname_t *adbname;
int bucket;
isc_boolean_t want_event, start_at_zone, alias, have_address;
isc_result_t result;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(name != NULL);
REQUIRE(qname != NULL);
REQUIRE(findp != NULL && *findp == NULL);
REQUIRE(target == NULL || dns_name_hasbuffer(target));
REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
result = ISC_R_UNEXPECTED;
wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
wanted_fetches = 0;
query_pending = 0;
want_event = ISC_FALSE;
start_at_zone = ISC_FALSE;
alias = ISC_FALSE;
if (now == 0)
isc_stdtime_get(&now);
/*
* XXXMLG Move this comment somewhere else!
*
* Look up the name in our internal database.
*
* Possibilities: Note that these are not always exclusive.
*
* No name found. In this case, allocate a new name header and
* an initial namehook or two. If any of these allocations
* fail, clean up and return ISC_R_NOMEMORY.
*
* Name found, valid addresses present. Allocate one addrinfo
* structure for each found and append it to the linked list
* of addresses for this header.
*
* Name found, queries pending. In this case, if a task was
* passed in, allocate a job id, attach it to the name's job
* list and remember to tell the caller that there will be
* more info coming later.
*/
find = new_adbfind(adb);
if (find == NULL)
return (ISC_R_NOMEMORY);
find->port = port;
/*
* Remember what types of addresses we are interested in.
*/
find->options = options;
find->flags |= wanted_addresses;
if (FIND_WANTEVENT(find)) {
REQUIRE(task != NULL);
}
/*
* Try to see if we know anything about this name at all.
*/
bucket = DNS_ADB_INVALIDBUCKET;
adbname = find_name_and_lock(adb, name, find->options, &bucket);
if (adb->name_sd[bucket]) {
DP(DEF_LEVEL,
"dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
result = ISC_R_SHUTTINGDOWN;
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name.
*/
if (adbname == NULL) {
adbname = new_adbname(adb, name);
if (adbname == NULL) {
RUNTIME_CHECK(free_adbfind(adb, &find) == ISC_FALSE);
result = ISC_R_NOMEMORY;
goto out;
}
link_name(adb, bucket, adbname);
if (FIND_HINTOK(find))
adbname->flags |= NAME_HINT_OK;
if (FIND_GLUEOK(find))
adbname->flags |= NAME_GLUE_OK;
if (FIND_STARTATZONE(find))
adbname->flags |= NAME_STARTATZONE;
}
/*
* Expire old entries, etc.
*/
RUNTIME_CHECK(check_expire_namehooks(adbname, now, adb->overmem) ==
ISC_FALSE);
/*
* Do we know that the name is an alias?
*/
if (!EXPIRE_OK(adbname->expire_target, now)) {
/*
* Yes, it is.
*/
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias (cached)",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Try to populate the name from the database and/or
* start fetches. First try looking for an A record
* in the database.
*/
if (!NAME_HAS_V4(adbname) && EXPIRE_OK(adbname->expire_v4, now)
&& WANT_INET(wanted_addresses)) {
result = dbfind_name(adbname, now, dns_rdatatype_a);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: found A for name %p in db",
adbname);
goto v6;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* If the name doesn't exist at all, don't bother with
* v6 queries; they won't work.
*
* If the name does exist but we didn't get our data, go
* ahead and try AAAA.
*
* If the result is neither of these, try a fetch for A.
*/
if (NXDOMAIN_RESULT(result))
goto fetch;
else if (NXRRSET_RESULT(result))
goto v6;
if (!NAME_FETCH_V4(adbname))
wanted_fetches |= DNS_ADBFIND_INET;
}
v6:
if (!NAME_HAS_V6(adbname) && EXPIRE_OK(adbname->expire_v6, now)
&& WANT_INET6(wanted_addresses)) {
result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: found AAAA for name %p",
adbname);
goto fetch;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Listen to negative cache hints, and don't start
* another query.
*/
if (NCACHE_RESULT(result) || AUTH_NX(result))
goto fetch;
if (!NAME_FETCH_V6(adbname))
wanted_fetches |= DNS_ADBFIND_INET6;
}
fetch:
if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
(WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
have_address = ISC_TRUE;
else
have_address = ISC_FALSE;
if (wanted_fetches != 0 &&
! (FIND_AVOIDFETCHES(find) && have_address)) {
/*
* We're missing at least one address family. Either the
* caller hasn't instructed us to avoid fetches, or we don't
* know anything about any of the address families that would
* be acceptable so we have to launch fetches.
*/
if (FIND_STARTATZONE(find))
start_at_zone = ISC_TRUE;
/*
* Start V4.
*/
if (WANT_INET(wanted_fetches) &&
fetch_name(adbname, start_at_zone,
dns_rdatatype_a) == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: started A fetch for name %p",
adbname);
}
/*
* Start V6.
*/
if (WANT_INET6(wanted_fetches) &&
fetch_name(adbname, start_at_zone,
dns_rdatatype_aaaa) == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"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, qname, qtype, 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;
}
}
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));
RUNTIME_CHECK(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) {
int i;
isc_stdtime_t now;
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);
isc_stdtime_get(&now);
for (i = 0; i < NBUCKETS; i++)
RUNTIME_CHECK(cleanup_names(adb, i, now) == ISC_FALSE);
for (i = 0; i < NBUCKETS; i++)
RUNTIME_CHECK(cleanup_entries(adb, i, now) == ISC_FALSE);
dump_adb(adb, f, ISC_FALSE, now);
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, isc_stdtime_t now) {
int i;
dns_adbname_t *name;
dns_adbentry_t *entry;
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);
}
}
fprintf(f, ";\n; Unassociated entries\n;\n");
for (i = 0; i < NBUCKETS; i++) {
entry = ISC_LIST_HEAD(adb->entries[i]);
while (entry != NULL) {
if (entry->refcnt == 0)
dump_entry(f, entry, debug, now);
entry = ISC_LIST_NEXT(entry, plink);
}
}
/*
* Unlock everything
*/
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->namelocks[i]);
}
static void
dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
isc_stdtime_t now)
{
char addrbuf[ISC_NETADDR_FORMATSIZE];
char typebuf[DNS_RDATATYPE_FORMATSIZE];
isc_netaddr_t netaddr;
dns_adblameinfo_t *li;
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);
if (entry->expires != 0)
fprintf(f, " [ttl %d]", entry->expires - now);
fprintf(f, "\n");
for (li = ISC_LIST_HEAD(entry->lameinfo);
li != NULL;
li = ISC_LIST_NEXT(li, plink)) {
fprintf(f, ";\t\t");
print_dns_name(f, &li->qname);
dns_rdatatype_format(li->qtype, typebuf, sizeof(typebuf));
fprintf(f, " %s [lame TTL %d]\n", typebuf,
li->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);
/* XXXVIX this switch statement is too sparse to gen a jump table. */
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 = DNS_FETCHOPT_NOVALIDATE;
if (start_at_zone) {
DP(ENTER_LEVEL,
"fetch_name: starting at zone for name %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 *qname,
dns_rdatatype_t qtype, isc_stdtime_t expire_time)
{
dns_adblameinfo_t *li;
int bucket;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(qname != NULL);
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
li = ISC_LIST_HEAD(addr->entry->lameinfo);
while (li != NULL &&
(li->qtype != qtype || !dns_name_equal(qname, &li->qname)))
li = ISC_LIST_NEXT(li, plink);
if (li != NULL) {
if (expire_time > li->lame_timer)
li->lame_timer = expire_time;
goto unlock;
}
li = new_adblameinfo(adb, qname, qtype);
if (li == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
li->lame_timer = expire_time;
ISC_LIST_PREPEND(addr->entry->lameinfo, li, plink);
unlock:
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
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);
/*
* Call our cleanup routines.
*/
for (i = 0; i < NBUCKETS; i++)
RUNTIME_CHECK(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
for (i = 0; i < NBUCKETS; i++)
RUNTIME_CHECK(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE, INT_MAX);
#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)) {
RUNTIME_CHECK(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);
}