adb.c revision e883738546c7c73b761bce8a15e08661863d3ae1
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
5fa46bc91672ef5737aee6f99763161511566c24Tinderbox User * Copyright (C) 1999 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * copyright notice and this permission notice appear in all copies.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * SOFTWARE.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
28a8f5b0de57d269cf2845c69cb6abe18cbd3b3aMark Andrews/*
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * Implementation notes
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * --------------------
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * In finds, if task == NULL, no events will be generated, and no events
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * have been sent. If task != NULL but taskaction == NULL, an event has been
f4b4e7c16211137332e50bcad3fef0d15639a4f1Brian Wellington * posted but not yet freed. If neigher are NULL, no event was posted.
f4b4e7c16211137332e50bcad3fef0d15639a4f1Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <config.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <stdio.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <string.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/assertions.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/condition.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/event.h>
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington#include <isc/log.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/magic.h>
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington#include <isc/mutex.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/mutexblock.h>
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <isc/random.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <isc/timer.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/a6.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/adb.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/db.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/events.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/fixedname.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/log.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/name.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdata.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/rdataset.h>
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include <dns/resolver.h>
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington#include <dns/types.h>
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#include <dns/view.h>
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#include "../isc/util.h"
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBFETCH_MAGIC 0x61644634 /* adF4. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBFETCH6_MAGIC 0x61644636 /* adF6. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Lengths of lists needs to be small primes.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBNAMELIST_LENGTH 31 /* how many buckets for names */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADBENTRYLIST_LENGTH 31 /* how many buckets for addresses */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/* clean this many seconds initially */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define CLEAN_SECONDS (300 / DNS_ADBNAMELIST_LENGTH)
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FREE_ITEMS 16 /* free count for memory pools */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FILL_COUNT 8 /* fill count for memory pools */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef struct dns_adbnamehook dns_adbnamehook_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef struct dns_adbfetch dns_adbfetch_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtontypedef struct dns_adbfetch6 dns_adbfetch6_t;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstruct dns_adb {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int magic;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mutex_t lock;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mem_t *mctx;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_view_t *view;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_timermgr_t *timermgr;
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence isc_timer_t *timer;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington isc_taskmgr_t *taskmgr;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_task_t *task;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington
571688b02f955f6304649866e768b1f81739cbedBrian Wellington isc_interval_t tick_interval;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington int next_namebucket;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington int next_entrybucket;
571688b02f955f6304649866e768b1f81739cbedBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int irefcnt;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int erefcnt;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mutex_t mplock;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *nmp; /* dns_adbname_t */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein isc_mempool_t *nhmp; /* dns_adbnamehook_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *emp; /* dns_adbentry_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *ahmp; /* dns_adbfind_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *afmp; /* dns_adbfetch_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_t *af6mp; /* dns_adbfetch6_t */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_random_t rand;
557bcc2092642b2d4668c9b08872c9f2bb88bddbMark Andrews
557bcc2092642b2d4668c9b08872c9f2bb88bddbMark Andrews /*
557bcc2092642b2d4668c9b08872c9f2bb88bddbMark Andrews * Bucketized locks and lists for names.
557bcc2092642b2d4668c9b08872c9f2bb88bddbMark Andrews */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington dns_adbnamelist_t names[DNS_ADBNAMELIST_LENGTH];
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington isc_mutex_t namelocks[DNS_ADBNAMELIST_LENGTH];
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington isc_boolean_t name_sd[DNS_ADBNAMELIST_LENGTH];
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington unsigned int name_refcnt[DNS_ADBNAMELIST_LENGTH];
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington /*
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington * Bucketized locks for entries.
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington */
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington dns_adbentrylist_t entries[DNS_ADBENTRYLIST_LENGTH];
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington isc_mutex_t entrylocks[DNS_ADBENTRYLIST_LENGTH];
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington};
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington/*
557bcc2092642b2d4668c9b08872c9f2bb88bddbMark Andrews * XXX: This has a pointer to the adb it came from. It shouldn't need
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington * this, but I can't think of how to get rid of it. In particular, since
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * events have but one "arg" value, and that is currently used for the name
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * pointer in fetches, we need a way to get to the fetch contexts as well
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * as the adb itself.
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstruct dns_adbname {
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int magic;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_t name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adb_t *adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int partial_result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int query_pending;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int flags;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int lock_bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_stdtime_t expire_v4;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_stdtime_t expire_v6;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int chains;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbnamehooklist_t v4;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbnamehooklist_t v6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfetch_t *fetch_a;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfetch_t *fetch_aaaa;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST(dns_adbfetch6_t) fetches_a6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST(dns_adbfind_t) finds;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK(dns_adbname_t) plink;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington};
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstruct dns_adbfetch {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int magic;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbnamehook_t *namehook;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbentry_t *entry;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_fetch_t *fetch;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_rdataset_t rdataset;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington};
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstruct dns_adbfetch6 {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int magic;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbnamehook_t *namehook;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbentry_t *entry;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_fetch_t *fetch;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_rdataset_t rdataset;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_a6context_t a6ctx;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LINK(dns_adbfetch6_t) plink;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington};
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * dns_adbnamehook_t
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington *
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * This is a small widget that dangles off a dns_adbname_t. It contains a
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * pointer to the address information about this host, and a link to the next
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * namehook that will contain the next address this host has.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct dns_adbnamehook {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington unsigned int magic;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbentry_t *entry;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LINK(dns_adbnamehook_t) plink;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington};
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * dns_adbzoneinfo_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * This is a small widget that holds zone-specific information about an
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * address. Currently limited to lameness, but could just as easily be
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * extended to other types of information about zones.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstruct dns_adbzoneinfo {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned int magic;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_t zone;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_stdtime_t lame_timer;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ISC_LINK(dns_adbzoneinfo_t) plink;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington};
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington/*
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington * An address entry. It holds quite a bit of information about addresses,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * including edns state, rtt, and of course the address of the host.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellingtonstruct dns_adbentry {
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews unsigned int magic;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington int lock_bucket;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington unsigned int refcnt;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews unsigned int flags;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews int edns_level; /* must be int! */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int goodness; /* bad < 0 <= good */
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington unsigned int srtt;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington isc_sockaddr_t sockaddr;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington ISC_LINK(dns_adbentry_t) plink;
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington};
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington/*
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington * Internal functions (and prototypes).
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews */
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington dns_adbentry_t *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline void free_adbfind(dns_adb_t *, dns_adbfind_t **);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *,
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington dns_adbentry_t *);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_a6context_t *);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington int *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_sockaddr_t *, int *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic void dump_adb(dns_adb_t *, FILE *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic void print_dns_name(FILE *, dns_name_t *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic void print_namehook_list(FILE *, dns_adbname_t *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic void print_find_list(FILE *, dns_adbname_t *);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellingtonstatic void print_fetch_list(FILE *, dns_adbname_t *);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void inc_adb_irefcnt(dns_adb_t *, isc_boolean_t);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void dec_adb_irefcnt(dns_adb_t *, isc_boolean_t);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellingtonstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington isc_boolean_t);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic void clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
de32cbd34e78bdd276e69cff239846760d4ee16eBrian Wellingtonstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews unsigned int);
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrewsstatic void check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonstatic void cancel_fetches_at_name(dns_adb_t *, dns_adbname_t *);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonstatic isc_result_t dbfind_name(dns_adbfind_t *, dns_name_t *,
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews dns_adbname_t *, int, isc_stdtime_t,
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellington dns_rdatatype_t);
d2aebe24c477c70e79dc33ea0507e8886eb7d626Brian Wellingtonstatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_stdtime_t);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic isc_result_t fetch_name_aaaa(dns_adbname_t *, isc_stdtime_t);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsstatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_stdtime_t);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsstatic inline void check_exit(dns_adb_t *);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void destroy(dns_adb_t *);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void shutdown_names(dns_adb_t *);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic inline void unlink_name(dns_adb_t *, dns_adbname_t *);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void kill_name(dns_adbname_t **, isc_eventtype_t);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic isc_result_t dbfind_a6(dns_adbfind_t *, dns_name_t *,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *, int, isc_stdtime_t);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * MUST NOT overlap DNS_ADBFIND_* flags!
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FIND_EVENT_SENT 0x40000000
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define FIND_EVENT_FREED 0x80000000
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define EVENT_SENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define EVENT_FREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define NAME_NEEDS_POKE 0x80000000
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define NAME_IS_DEAD 0x40000000
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define WANTEVENT(x) (((x) & DNS_ADBFIND_WANTEVENT) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define WANTEMPTYEVENT(x) (((x) & DNS_ADBFIND_EMPTYEVENT) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define HAVE_INET(n) (!ISC_LIST_EMPTY((n)->v4))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define HAVE_INET6(n) (!ISC_LIST_EMPTY((n)->v6))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define HAVE_ADDRS(h) (!ISC_LIST_EMPTY((h)->list))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define QUERY_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater#define QUERY_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define QUERYPENDING(x, y) (((x) & (y)) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define WANTEDADDR(x, y) (((x) & (y)) != 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington#define NO_FETCHES_A(n) ((n)->fetch_a == NULL)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson#define NO_FETCHES_AAAA(n) ((n)->fetch_aaaa == NULL)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson#define NO_FETCHES_A6(n) (ISC_LIST_EMPTY((n)->fetches_a6))
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson#define NO_FETCHES(n) (NO_FETCHES_A(n) \
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson && NO_FETCHES_AAAA(n) \
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson && NO_FETCHES_A6(n))
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic void
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas GustafssonDP(int level, char *format, ...)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson va_list args;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson va_start(args, format);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_log_vwrite(dns_lctx,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ISC_LOG_DEBUG(level), format, args);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson va_end(args);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson}
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson/*
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * Requires the adbname bucket be locked and that no entry buckets be locked.
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater *
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson * This code handles A and AAAA rdatasets only.
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonstatic isc_result_t
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafssonimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_stdtime_t now)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson{
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews isc_result_t result;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_adb_t *adb;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_adbnamehook_t *nh;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_rdata_t rdata;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson struct in_addr ina;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson struct in6_addr in6a;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_sockaddr_t sockaddr;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_adbentry_t *foundentry; /* NO CLEAN UP! */
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson int addr_bucket;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_boolean_t new_addresses_added;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_rdatatype_t rdtype;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson unsigned int findoptions;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST(DNS_ADBNAME_VALID(adbname));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson adb = adbname->adb;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST(DNS_ADB_VALID(adb));
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews rdtype = rdataset->type;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews if (rdtype == dns_rdatatype_a)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson findoptions = DNS_ADBFIND_INET;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington findoptions = DNS_ADBFIND_INET6;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington addr_bucket = DNS_ADB_INVALIDBUCKET;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington new_addresses_added = ISC_FALSE;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington result = dns_rdataset_first(rdataset);
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington while (result == ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh = new_adbnamehook(adb, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (nh == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adbname->partial_result |= findoptions;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = ISC_R_NOMEMORY;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson goto fail;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
6c7f722d31d12cf83964c8132f0a59ef70e34cb5Mark Andrews
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_rdataset_current(rdataset, &rdata);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (rdtype == dns_rdatatype_a) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(rdata.length == 4);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington memcpy(&ina.s_addr, rdata.data, 4);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_sockaddr_fromin(&sockaddr, &ina, 53);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews INSIST(rdata.length == 16);
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews memcpy(&in6a.s6_addr, rdata.data, 16);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson isc_sockaddr_fromin6(&sockaddr, &in6a, 53);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (foundentry == NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_adbentry_t *entry;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry = new_adbentry(adb);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (entry == NULL) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews adbname->partial_result |= findoptions;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson result = ISC_R_NOMEMORY;
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington goto fail;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry->sockaddr = sockaddr;
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews entry->refcnt = 1;
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews entry->lock_bucket = addr_bucket;
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews nh->entry = entry;
6b79e960e6ba2991aeb02a6c39af255ab7f06d99Mark Andrews
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ISC_LIST_APPEND(adb->entries[addr_bucket],
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington foundentry->refcnt++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh->entry = foundentry;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington new_addresses_added = ISC_TRUE;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (rdtype == dns_rdatatype_a)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ISC_LIST_APPEND(adbname->v4, nh, plink);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(adbname->v6, nh, plink);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson nh = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_rdataset_next(rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington fail:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (nh != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbnamehook(adb, &nh);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(&adb->entrylocks[addr_bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington adbname->expire_v4 = ISC_MIN(adbname->expire_v4, now + rdataset->ttl);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Lie a little here. This is more or less so code that cares
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * can find out if any new information was added or not.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (new_addresses_added)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (ISC_R_SUCCESS);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (result);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonimport_a6(void *arg, struct in6_addr *address)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbname_t *name;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adb_t *adb;
307d2084502eddc7ce921e5ce439aec3531d90e0Tatuya JINMEI 神明達哉 dns_adbnamehook_t *nh;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_stdtime_t now;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_stdtime_t expire_time;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_result_t result;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_boolean_t address_added;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington int addr_bucket;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_sockaddr_t sockaddr;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington name = arg;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(DNS_ADBNAME_VALID(name));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington adb = name->adb;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(DNS_ADB_VALID(adb));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington address_added = ISC_FALSE;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * XXX Once the time is passed in to us, this can go away.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington result = isc_stdtime_get(&now);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (result != ISC_R_SUCCESS)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington expire_time = now + 30; /* XXX 30 seconds */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington nh = new_adbnamehook(adb, NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (nh == NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto fail;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_sockaddr_fromin6(&sockaddr, address, 53);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (foundentry == NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson dns_adbentry_t *entry;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry = new_adbentry(adb);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (entry == NULL) {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson name->partial_result |= DNS_ADBFIND_INET6;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson goto fail;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry->sockaddr = sockaddr;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry->refcnt = 1;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson entry->lock_bucket = addr_bucket;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson nh->entry = entry;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ISC_LIST_APPEND(adb->entries[addr_bucket], entry, plink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington } else {
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson foundentry->refcnt++;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington nh->entry = foundentry;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson }
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson address_added = ISC_TRUE;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson ISC_LIST_APPEND(name->v6, nh, plink);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson nh = NULL;
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson fail:
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (nh != NULL)
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson free_adbnamehook(adb, &nh);
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson
307ba34fa07db768c3a899844f248a2c1d7dcc7fAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(&adb->entrylocks[addr_bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->expire_v6 = ISC_MIN(name->expire_v6, expire_time);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (address_added)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->flags |= NAME_NEEDS_POKE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Requires the name's bucket be locked.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
f3ca27e9fe307b55e35ea8d7b37351650630e5a3Andreas Gustafssonkill_name(dns_adbname_t **n, isc_eventtype_t ev)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adb_t *adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(n != NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = *n;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *n = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADBNAME_VALID(name));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb = name->adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADB_VALID(adb));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DP(1, "killing name %p", name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If we're dead already, just check to see if we should go
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * away now or not.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (NAME_DEAD(name) && NO_FETCHES(name)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unlink_name(adb, name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbname(adb, &name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Clean up the name's various lists. These two are destructive
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * in that they will always empty the list.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington clean_namehooks(adb, &name->v4);
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington clean_namehooks(adb, &name->v6);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If fetches are running, cancel them. If none are running, we can
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington * just kill the name here.
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington */
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington if (NO_FETCHES(name)) {
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington unlink_name(adb, name);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington free_adbname(adb, &name);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington } else {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->flags |= NAME_IS_DEAD;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cancel_fetches_at_name(adb, name);
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Requires the name's bucket be locked and no entry buckets be locked.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adb_t *adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADBNAME_VALID(name));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb = name->adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADB_VALID(adb));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Check to see if we need to remove the v4 addresses
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (QUERY_INET(name->query_pending) && (name->expire_v4 < now)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DP(1, "expiring v4 for name %p", name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington clean_namehooks(adb, &name->v4);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->partial_result &= ~DNS_ADBFIND_INET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Check to see if we need to remove the v6 addresses
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (QUERY_INET6(name->query_pending) && (name->expire_v6 < now)) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DP(1, "expiring v6 for name %p", name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington clean_namehooks(adb, &name->v6);
7829fad4093f2c1985b1efb7cea00287ff015d2bckb name->partial_result &= ~DNS_ADBFIND_INET6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
7829fad4093f2c1985b1efb7cea00287ff015d2bckb}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Requires the name's bucket be locked.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
7829fad4093f2c1985b1efb7cea00287ff015d2bckbstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonlink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_PREPEND(adb->names[bucket], name, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->lock_bucket = bucket;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews adb->name_refcnt[bucket]++;
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Requires the name's bucket be locked.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonunlink_name(dns_adb_t *adb, dns_adbname_t *name)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington bucket = name->lock_bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(bucket != DNS_ADB_INVALIDBUCKET);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_UNLINK(adb->names[bucket], name, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->lock_bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(adb->name_refcnt[bucket] > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb->name_refcnt[bucket]--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(have);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(want);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(have);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * The ADB _MUST_ be locked before calling. Also, exit conditions must be
c0d2891f6e08fcf5379dfb9a1bf8fbbb63f1952aMark Andrews * checked after calling this function.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
c0d2891f6e08fcf5379dfb9a1bf8fbbb63f1952aMark Andrewsshutdown_names(dns_adb_t *adb)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *next_name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington for (bucket = 0 ; bucket < DNS_ADBNAMELIST_LENGTH ; bucket++) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->namelocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb->name_sd[bucket] = ISC_TRUE;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Run through the list. For each name, clean up finds
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * found there, and cancel any fetches running. When
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * all the fetches are canceled, the name will destroy
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * itself.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = ISC_LIST_HEAD(adb->names[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (name != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington next_name = ISC_LIST_NEXT(name, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington kill_name(&name, DNS_EVENT_ADBSHUTDOWN);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = next_name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* kill_name() will decrement the refcnt. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (adb->name_refcnt[bucket] == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dec_adb_irefcnt(adb, ISC_FALSE);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(&adb->namelocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dump_adb(adb, stderr);
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
229ce407c359b0b641759ba1fc4a5fa2054a44daBrian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Name bucket must be locked
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoncancel_fetches_at_name(dns_adb_t *adb, dns_adbname_t *name)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (name->fetch_a != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_resolver_cancelfetch(adb->view->resolver,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->fetch_a->fetch);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (name->fetch_aaaa != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_resolver_cancelfetch(adb->view->resolver,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->fetch_aaaa->fetch);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Assumes the name bucket is locked.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbentry_t *entry;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbnamehook_t *namehook;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int addr_bucket;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington addr_bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namehook = ISC_LIST_HEAD(*namehooks);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (namehook != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Clean up the entry if needed.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington entry = namehook->entry;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (entry != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADBENTRY_VALID(entry));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (addr_bucket != entry->lock_bucket) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (addr_bucket != DNS_ADB_INVALIDBUCKET)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(&adb->entrylocks[addr_bucket]);
5e387b9ce6bafdfadedb5b34e4c33a4404e5d589Brian Wellington addr_bucket = entry->lock_bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->entrylocks[addr_bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dec_entry_refcnt(adb, entry, ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt /*
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt * Free the namehook
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namehook->entry = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_UNLINK(*namehooks, namehook, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbnamehook(adb, &namehook);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namehook = ISC_LIST_HEAD(*namehooks);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (addr_bucket != DNS_ADB_INVALIDBUCKET)
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews UNLOCK(&adb->entrylocks[addr_bucket]);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Assumes nothing is locked, since this is called by the client.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonevent_free(isc_event_t *event)
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfind_t *find;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington INSIST(event != NULL);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews find = event->destroy_arg;
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington INSIST(DNS_ADBFIND_VALID(find));
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews LOCK(&find->lock);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews find->flags |= FIND_EVENT_FREED;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews event->destroy_arg = NULL;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews UNLOCK(&find->lock);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews}
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews/*
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington * Assumes the name bucket is locked.
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews */
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsstatic void
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews unsigned int addrs)
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews{
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews isc_event_t *ev;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews isc_task_t *task;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews dns_adbfind_t *find;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews dns_adbfind_t *next_find;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews find = ISC_LIST_HEAD(name->finds);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews while (find != NULL) {
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews LOCK(&find->lock);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews next_find = ISC_LIST_NEXT(find, plink);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews /*
a4c351fcef77fb332e3cb20253fb96556a414a17Brian Wellington * If this is a successful poke, we want to match only
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * finds that are interested in the address family.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * If it is an unsuccessful poke (kill all names, etc)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * match everything that matches the "addrs" mask.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington find->options &= ~addrs;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington if (evtype != DNS_EVENT_ADBMOREADDRESSES) {
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews if ((find->options & DNS_ADBFIND_ADDRESSMASK) != 0)
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews goto next;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews }
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
3fb95bfcb2e478d721d378f0be5ff0a32269bc2aTinderbox User /*
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews * Unlink the find from the name, letting the caller
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews * call dns_adb_destroyfind() on it to clean it up later.
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington */
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson ISC_LIST_UNLINK(name->finds, find, plink);
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson find->adbname = NULL;
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson find->name_bucket = DNS_ADB_INVALIDBUCKET;
6e93e6ea4557291e847aced6a88adcdf39f06843Andreas Gustafsson
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(!EVENT_SENT(find));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ev = &find->event;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington task = ev->sender;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington ev->sender = find;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington ev->type = evtype;
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington ev->destroy = event_free;
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington ev->destroy_arg = find;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington DP(1, "Sending event %p to task %p for find %p",
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ev, task, find);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_task_sendanddetach(&task, &ev);
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington next:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&find->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington find = next_find;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntstatic inline void
793814f80703afdd69b59ade91e63efa81ae4178Evan Huntcheck_exit(dns_adb_t *adb)
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt{
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt if ((adb->irefcnt == 0) && (adb->erefcnt == 0)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington && (isc_mempool_getallocated(adb->ahmp) == 0))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_task_shutdown(adb->task);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtoninc_adb_irefcnt(dns_adb_t *adb, isc_boolean_t lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->lock);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb->irefcnt++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondec_adb_irefcnt(dns_adb_t *adb, isc_boolean_t lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(adb->irefcnt > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb->irefcnt--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_exit(adb);
fefe1106d96d3a89b21315f665b36a67cdea3840Mark Andrews
fefe1106d96d3a89b21315f665b36a67cdea3840Mark Andrews if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->lock);
fefe1106d96d3a89b21315f665b36a67cdea3840Mark Andrews}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsinc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews{
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb->erefcnt++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt if (lock)
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt UNLOCK(&adb->lock);
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrews}
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrews
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrewsstatic inline void
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrewsdec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock)
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrews{
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrews if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(adb->erefcnt > 0);
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrews adb->erefcnt--;
85fcd0b9b2d80be4646187d7094e5644a52e3263Mark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington check_exit(adb);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews if (lock)
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews UNLOCK(&adb->lock);
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews}
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsstatic inline void
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrewsinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews{
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews int bucket;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews bucket = entry->lock_bucket;
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington entry->refcnt++;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtondec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington int bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_boolean_t destroy_entry;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington bucket = entry->lock_bucket;
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (lock)
0a45f098e446a7f41c2a71d9dd0fd8f459b49c34Brian Wellington LOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(entry->refcnt > 0);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington entry->refcnt--;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington destroy_entry = ISC_FALSE;
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington if (entry->refcnt == 0) {
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington destroy_entry = ISC_TRUE;
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
b1b0dca1464a11b8a63623e8567e744dccfbcb41Tatuya JINMEI 神明達哉 }
b1b0dca1464a11b8a63623e8567e744dccfbcb41Tatuya JINMEI 神明達哉
ab381c1e22e0ed732170428937d20d13146d863aAutomatic Updater if (lock)
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington
b1b0dca1464a11b8a63623e8567e744dccfbcb41Tatuya JINMEI 神明達哉 if (!destroy_entry)
c302b021cc65cc9a358a9a1cbe48de12364f4cb6Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbentry(adb, &entry);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbname_t *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonnew_adbname(dns_adb_t *adb, dns_name_t *dnsname)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = isc_mempool_get(adb->nmp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (name == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75582adac73202213d936dc0850f1c9cf47d6cbeMark Andrews dns_name_init(&name->name, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->nmp, name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->magic = DNS_ADBNAME_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->adb = adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->partial_result = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->query_pending = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->flags = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->expire_v4 = INT_MAX;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->expire_v6 = INT_MAX;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->chains = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->lock_bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(name->v4);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(name->v6);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->fetch_a = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->fetch_aaaa = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(name->fetches_a6);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(name->finds);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(name, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfree_adbname(dns_adb_t *adb, dns_adbname_t **name)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *n;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington n = *name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *name = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(!HAVE_INET(n));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(!HAVE_INET6(n));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(NO_FETCHES(n));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ISC_LIST_EMPTY(n->finds));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(!ISC_LINK_LINKED(n, plink));
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(n->adb == adb);
cfcb0881d12db2b7cb33475b7d20ac6c9015203bMark Andrews
cfcb0881d12db2b7cb33475b7d20ac6c9015203bMark Andrews n->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_free(&n->name, adb->mctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->nmp, n);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbnamehook_t *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonnew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbnamehook_t *nh;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh = isc_mempool_get(adb->nhmp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (nh == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh->magic = DNS_ADBNAMEHOOK_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh->entry = entry;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(nh, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (nh);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbnamehook_t *nh;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh = *namehook;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *namehook = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(nh->entry == NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(!ISC_LINK_LINKED(nh, plink));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nh->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->nhmp, nh);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafssonstatic inline dns_adbzoneinfo_t *
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafssonnew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone)
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbzoneinfo_t *zi;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington zi = isc_mempool_get(adb->zimp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (zi == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_init(&zi->zone, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->zimp, zi);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington zi->magic = DNS_ADBZONEINFO_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington zi->lame_timer = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(zi, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (zi);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafsson
7a6f285bc933d08353d2f18290c85def575b6e57Andreas Gustafssonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbzoneinfo_t *zi;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington zi = *zoneinfo;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *zoneinfo = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(!ISC_LINK_LINKED(zi, plink));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_free(&zi->zone, adb->mctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington zi->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->zimp, zi);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbentry_t *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonnew_adbentry(dns_adb_t *adb)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbentry_t *e;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_uint32_t r;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington e = isc_mempool_get(adb->emp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (e == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington e->magic = DNS_ADBENTRY_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington e->lock_bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington e->refcnt = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington e->flags = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington e->goodness = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_random_get(&adb->rand, &r);
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews e->srtt = (r & 0x1f) + 1;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(e->zoneinfo);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LINK_INIT(e, plink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return (e);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic inline void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonfree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbentry_t *e;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbzoneinfo_t *zi;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
6e2871232f7ede047799480370aff444be1f5a13Automatic Updater e = *entry;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews *entry = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(e->refcnt == 0);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington INSIST(!ISC_LINK_LINKED(e, plink));
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington e->magic = 0;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews zi = ISC_LIST_HEAD(e->zoneinfo);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (zi != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington free_adbzoneinfo(adb, &zi);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington zi = ISC_LIST_HEAD(e->zoneinfo);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_mempool_put(adb->emp, e);
23f64ea0dcd7f5b7094ae6ade2a002fb7dde1466Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
daa73eae708d568d453e6082e0890d35886a9e0fMark Andrewsstatic inline dns_adbfind_t *
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonnew_adbfind(dns_adb_t *adb)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbfind_t *h;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_result_t result;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington h = isc_mempool_get(adb->ahmp);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (h == NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * public members
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->adb = adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->query_pending = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->partial_result = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->options = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(h, publink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(h, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_INIT(h->list);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->adbname = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->name_bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->flags = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * private members
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = isc_mutex_init(&h->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews "isc_mutex_init failed in new_adbfind()");
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->ahmp, h);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington NULL, NULL, h);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington h->magic = DNS_ADBFIND_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (h);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbfetch_t *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonnew_adbfetch(dns_adb_t *adb)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfetch_t *f;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f = isc_mempool_get(adb->afmp);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->namehook = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->entry = NULL;
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater f->fetch = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->namehook = new_adbnamehook(adb, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->namehook == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto err;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->entry = new_adbentry(adb);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->entry == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto err;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_init(&f->rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->magic = DNS_ADBFETCH_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (f);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington err:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->namehook != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbnamehook(adb, &f->namehook);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->entry != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbentry(adb, &f->entry);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->afmp, f);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfetch_t *f;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f = *fetch;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *fetch = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->namehook != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbnamehook(adb, &f->namehook);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->entry != NULL)
f8abaa0fae7f75d9601c10b6a4af8dd907494d45Mark Andrews free_adbentry(adb, &f->entry);
b55c30f2de6e1baaa3a9ba69b92f428f2c255ac3Mark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (dns_rdataset_isassociated(&f->rdataset))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_disassociate(&f->rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->afmp, f);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
f8abaa0fae7f75d9601c10b6a4af8dd907494d45Mark Andrews
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Caller must be holding the name lock.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic isc_result_t
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtona6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adb_t *adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = arg;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADBNAME_VALID(name));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb = name->adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(DNS_ADB_VALID(adb));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * XXXRTH Cache 'now' in the name so we don't have to call
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * isc_stdtime_get() on every dns_view_find().
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (dns_view_find(adb->view, a6name, type, 0, 0, ISC_FALSE,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington rdataset, sigrdataset));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Caller must be holding the name lock.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtona6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbname_t *name;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adb_t *adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfetch6_t *fetch;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_result_t result;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_t nameservers;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_name_t fname;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_buffer_t buffer;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington unsigned char ndata[256];
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name = a6ctx->arg;
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews INSIST(DNS_ADBNAME_VALID(name));
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews adb = name->adb;
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews INSIST(DNS_ADB_VALID(adb));
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews isc_buffer_init(&buffer, ndata, sizeof(ndata), ISC_BUFFERTYPE_BINARY);
4a30ede93d59137009db734661cde17612e8ffbeMark Andrews dns_name_init(&fname, NULL);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews dns_name_setbuffer(&fname, &buffer);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews dns_rdataset_init(&nameservers);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews fetch = new_adbfetch6(adb, name, a6ctx);
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews if (fetch == NULL) {
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews name->partial_result |= DNS_ADBFIND_INET6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /* XXXRTH Use cached 'now' from name. */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_view_findzonecut(adb->view, a6name, &fname, 0,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington 0, ISC_TRUE, &nameservers, NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS)
7829fad4093f2c1985b1efb7cea00287ff015d2bckb goto cleanup;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington result = dns_resolver_createfetch(adb->view->resolver, a6name,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdatatype_a6, &fname,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington &nameservers, NULL, 0,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adb->task, fetch_callback_a6,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name, &fetch->rdataset, NULL,
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt &fetch->fetch);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt if (result != ISC_R_SUCCESS)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto cleanup;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->chains = a6ctx->chains;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington cleanup:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (result != ISC_R_SUCCESS) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbfetch6(adb, &fetch);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington name->partial_result |= DNS_ADBFIND_INET6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbfetch6_t *
4a30ede93d59137009db734661cde17612e8ffbeMark Andrewsnew_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx)
a03848252fa85734ca75beae3d0b01bb503c0a8bMark Andrews{
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_adbfetch6_t *f;
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt f = isc_mempool_get(adb->af6mp);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt if (f == NULL)
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt return (NULL);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt f->magic = 0;
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt f->namehook = NULL;
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt f->entry = NULL;
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt f->fetch = NULL;
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt f->namehook = new_adbnamehook(adb, NULL);
793814f80703afdd69b59ade91e63efa81ae4178Evan Hunt if (f->namehook == NULL)
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt goto err;
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt
dd2a0a6d2dec1c23787351e51b434a838dec5603Evan Hunt f->entry = new_adbentry(adb);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->entry == NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto err;
6fd4ab217ef8e37703b029eac5ab7ae4f7a663c9Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_rdataset_init(&f->rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington if (a6ctx != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_a6_copy(a6ctx, &f->a6ctx);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington else
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington a6missing, name);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington f->magic = DNS_ADBFETCH6_MAGIC;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington return (f);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
77ac297199fc44809d9628558223627c10ae3f31Brian Wellington err:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->namehook != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbnamehook(adb, &f->namehook);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->entry != NULL)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington free_adbentry(adb, &f->entry);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->af6mp, f);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonfree_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfetch6_t *f;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f = *fetch;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *fetch = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington f->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
18d0b5e54be891a1aa938c165b6d439859121ec8Mark Andrews if (f->namehook != NULL)
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington free_adbnamehook(adb, &f->namehook);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (f->entry != NULL)
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington free_adbentry(adb, &f->entry);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington if (dns_rdataset_isassociated(&f->rdataset))
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington dns_rdataset_disassociate(&f->rdataset);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->af6mp, f);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington}
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellingtonfree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbfind_t *find;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington find = *findp;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington *findp = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington INSIST(!HAVE_ADDRS(find));
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington INSIST(!ISC_LINK_LINKED(find, publink));
34f991028382079af7c2b53bac6768803ff28f8cBrian Wellington INSIST(!ISC_LINK_LINKED(find, plink));
75e1e12f48012505699f504cfa364260cb2bc1afBrian Wellington INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
e2fd12f3a020ca8c5de168a44fb72e339cdaa3e9Brian Wellington INSIST(find->adbname == NULL);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington find->magic = 0;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mutex_destroy(&find->lock);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington isc_mempool_put(adb->ahmp, find);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Copy bits from the entry into the newly allocated addrinfo. The entry
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * must be locked, and the reference count must be bumped up by one
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * if this function returns a valid pointer.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellingtonstatic inline dns_adbaddrinfo_t *
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellingtonnew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry)
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington{
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington dns_adbaddrinfo_t *ai;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington ai = isc_mempool_get(adb->aimp);
0a25550b464ad40e4d90ebff8ecf23595b05ae3bFrancis Dupont if (ai == NULL)
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington return (NULL);
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington ai->magic = DNS_ADBADDRINFO_MAGIC;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington ai->sockaddr = &entry->sockaddr;
f24c135e09214c3843a49fd32ebef2f6a436ba8eBrian Wellington ai->goodness = entry->goodness;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ai->srtt = entry->srtt;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ai->flags = entry->flags;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ai->entry = entry;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LINK_INIT(ai, publink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (ai);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline void
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellingtonfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adbaddrinfo_t *ai;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ai = *ainfo;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *ainfo = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington INSIST(ai->sockaddr == NULL);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington INSIST(ai->entry == NULL);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington INSIST(!ISC_LINK_LINKED(ai, publink));
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington ai->magic = 0;
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington isc_mempool_put(adb->aimp, ai);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington}
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington/*
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington * Search for the name. NOTE: The bucket is kept locked on both
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington * success and failure, so it must always be unlocked by the caller!
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * On the first call to this function, *bucketp must be set to
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * DNS_ADB_INVALIDBUCKET.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic inline dns_adbname_t *
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellingtonfind_name_and_lock(dns_adb_t *adb, dns_name_t *name, int *bucketp)
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews{
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews dns_adbname_t *adbname;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 int bucket;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 bucket = dns_name_hash(name, ISC_FALSE) % DNS_ADBNAMELIST_LENGTH;
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews
6098d364b690cb9dabf96e9664c4689c8559bd2eMark Andrews if (*bucketp == DNS_ADB_INVALIDBUCKET) {
0cae66577c69c89086cd065bb297690072b471b4Mark Andrews LOCK(&adb->namelocks[bucket]);
0cae66577c69c89086cd065bb297690072b471b4Mark Andrews *bucketp = bucket;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 } else if (*bucketp != bucket) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->namelocks[*bucketp]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->namelocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *bucketp = bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington adbname = ISC_LIST_HEAD(adb->names[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (adbname != NULL) {
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington if (!NAME_DEAD(adbname)) {
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington if (dns_name_equal(name, &adbname->name))
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington return (adbname);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington }
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington adbname = ISC_LIST_NEXT(adbname, plink);
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington }
a012d6dbfb100390efa7d0d4be64ada0210b09ddBrian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington return (NULL);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington/*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Search for the address. NOTE: The bucket is kept locked on both
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * success and failure, so it must always be unlocked by the caller.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington *
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * On the first call to this function, *bucketp must be set to
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * later calls (within the same "lock path") it can be left alone, so
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * if this function is called multiple times locking is only done if
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * the bucket changes.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic inline dns_adbentry_t *
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonfind_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbentry_t *entry;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington int bucket;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington bucket = isc_sockaddr_hash(addr, ISC_TRUE) % DNS_ADBENTRYLIST_LENGTH;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (*bucketp == DNS_ADB_INVALIDBUCKET) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington LOCK(&adb->entrylocks[bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington *bucketp = bucket;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington } else if (*bucketp != bucket) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington UNLOCK(&adb->entrylocks[*bucketp]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington LOCK(&adb->entrylocks[bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington *bucketp = bucket;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington entry = ISC_LIST_HEAD(adb->entries[bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (entry != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (isc_sockaddr_equal(addr, &entry->sockaddr))
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (entry);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington entry = ISC_LIST_NEXT(entry, plink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (NULL);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington/*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Entry bucket MUST be locked!
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic isc_boolean_t
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonentry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_stdtime_t now)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbzoneinfo_t *zi, *next_zi;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington isc_boolean_t is_bad;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington is_bad = ISC_FALSE;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington zi = ISC_LIST_HEAD(entry->zoneinfo);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (zi == NULL)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (ISC_FALSE);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (zi != NULL) {
01dbc4fc00adc933af96e88bfce95c07ef1f3c39Mark Andrews next_zi = ISC_LIST_NEXT(zi, plink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
01dbc4fc00adc933af96e88bfce95c07ef1f3c39Mark Andrews * Has the entry expired?
01dbc4fc00adc933af96e88bfce95c07ef1f3c39Mark Andrews */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (zi->lame_timer < now) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington free_adbzoneinfo(adb, &zi);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington * Order tests from least to most expensive.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (zi != NULL && !is_bad) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (dns_name_equal(zone, &zi->zone))
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington is_bad = ISC_TRUE;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington zi = next_zi;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington return (is_bad);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington}
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtonstatic void
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellingtoncopy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbname_t *name, dns_name_t *zone, isc_stdtime_t now)
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington{
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbnamehook_t *namehook;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington dns_adbaddrinfo_t *addrinfo;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington int bucket;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington bucket = DNS_ADB_INVALIDBUCKET;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (find->options & DNS_ADBFIND_INET) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington namehook = ISC_LIST_HEAD(name->v4);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington while (namehook != NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington bucket = namehook->entry->lock_bucket;
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington LOCK(&adb->entrylocks[bucket]);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (entry_is_bad_for_zone(adb, namehook->entry,
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington zone, now))
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto nextv4;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington addrinfo = new_adbaddrinfo(adb, namehook->entry);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington if (addrinfo == NULL) {
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington find->partial_result |= DNS_ADBFIND_INET;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington goto out;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington }
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington /*
5d85bf183f957f9ea0902be1a6e3405b90bc7c0fBrian Wellington * Found a valid entry. Add it to the find's list.
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington */
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
b7cd261f2fca2c7138cdc6ae8ee434e9c0031303Brian Wellington ISC_LIST_APPEND(find->list, addrinfo, publink);
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington addrinfo = NULL;
60b90a37f41ab7607762d0e9791e79bd19eae4f4Brian Wellington nextv4:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namehook = ISC_LIST_NEXT(namehook, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (find->options & DNS_ADBFIND_INET6) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namehook = ISC_LIST_HEAD(name->v6);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington while (namehook != NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington bucket = namehook->entry->lock_bucket;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington LOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (entry_is_bad_for_zone(adb, namehook->entry,
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington zone, now))
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto nextv6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington addrinfo = new_adbaddrinfo(adb, namehook->entry);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (addrinfo == NULL) {
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington find->partial_result |= DNS_ADBFIND_INET6;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington goto out;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington /*
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington * Found a valid entry. Add it to the find's list.
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington */
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington inc_entry_refcnt(adb, namehook->entry, ISC_FALSE);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington ISC_LIST_APPEND(find->list, addrinfo, publink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington addrinfo = NULL;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington nextv6:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington bucket = DNS_ADB_INVALIDBUCKET;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington namehook = ISC_LIST_NEXT(namehook, plink);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington }
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington out:
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington if (bucket != DNS_ADB_INVALIDBUCKET)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington UNLOCK(&adb->entrylocks[bucket]);
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington}
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonstatic void
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellingtonshutdown_task(isc_task_t *task, isc_event_t *ev)
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington{
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington dns_adb_t *adb;
033ba09d6df0ac92a736a480b9c3b164b61dccb2Brian Wellington
(void)task; /* not used */
adb = 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) baddness will occur.
*/
LOCK(&adb->lock);
isc_timer_detach(&adb->timer);
UNLOCK(&adb->lock);
destroy(adb);
isc_event_free(&ev);
}
/*
* name bucket must be locked; adb may be locked; no other locks held.
*/
static void
check_expire_name(dns_adbname_t **namep)
{
dns_adbname_t *name;
INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
name = *namep;
*namep = NULL;
if (HAVE_INET(name) || HAVE_INET6(name))
return;
if (!NO_FETCHES(name))
return;
INSIST(!QUERYPENDING(name->query_pending, DNS_ADBFIND_ADDRESSMASK));
/*
* The name is empty. Delete it.
*/
kill_name(&name, DNS_EVENT_ADBEXPIRED);
}
/*
* ADB must be locked, and no other locks held.
*/
static void
cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now)
{
dns_adbname_t *name;
dns_adbname_t *next_name;
DP(1, "cleaning bucket %d", bucket);
LOCK(&adb->namelocks[bucket]);
if (adb->name_sd[bucket]) {
UNLOCK(&adb->namelocks[bucket]);
return;
}
name = ISC_LIST_HEAD(adb->names[bucket]);
while (name != NULL) {
next_name = ISC_LIST_NEXT(name, plink);
check_expire_namehooks(name, now);
check_expire_name(&name);
name = next_name;
}
UNLOCK(&adb->namelocks[bucket]);
}
static void
timer_cleanup(isc_task_t *task, isc_event_t *ev)
{
dns_adb_t *adb;
isc_result_t result;
isc_stdtime_t now;
(void)task; /* not used */
adb = ev->arg;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
result = isc_stdtime_get(&now);
if (result != ISC_R_SUCCESS) {
DP(1, "isc_stdtime_get() failed! Resetting clean timer.");
goto reset;
}
/*
* Call our cleanup routine.
*/
cleanup_names(adb, adb->next_namebucket, now);
/*
* Set the next bucket to be cleaned.
*/
adb->next_namebucket++;
if (adb->next_namebucket >= DNS_ADBNAMELIST_LENGTH)
adb->next_namebucket = 0;
#if 0
/*
* Call the entry cleanup routine
*/
cleanup_entries(adb, adb->next_entrybucket, now);
adb->next_entrybucket++;
if (adb->next_entrybucket >= DNS_ADBENTRYLIST_LENGTH)
adb->next_entrybucket = 0;
#endif
/*
* Reset the timer.
*/
reset:
result = isc_timer_reset(adb->timer, isc_timertype_once, NULL,
&adb->tick_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->zimp);
isc_mempool_destroy(&adb->emp);
isc_mempool_destroy(&adb->ahmp);
isc_mempool_destroy(&adb->aimp);
isc_mempool_destroy(&adb->afmp);
isc_mempool_destroy(&adb->af6mp);
isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
isc_mutex_destroy(&adb->lock);
isc_mutex_destroy(&adb->mplock);
isc_random_invalidate(&adb->rand);
isc_mem_put(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->zimp = NULL;
adb->emp = NULL;
adb->ahmp = NULL;
adb->aimp = NULL;
adb->afmp = NULL;
adb->af6mp = NULL;
adb->task = NULL;
adb->timer = NULL;
adb->mctx = mem;
adb->view = view;
adb->timermgr = timermgr;
adb->taskmgr = taskmgr;
adb->next_namebucket = 0;
adb->next_entrybucket = 0;
result = isc_random_init(&adb->rand);
if (result != ISC_R_SUCCESS)
goto fail0a;
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail0b;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail0c;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
ISC_LIST_INIT(adb->names[i]);
adb->name_sd[i] = ISC_FALSE;
adb->name_refcnt[i] = 0;
adb->irefcnt++;
}
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
ISC_LIST_INIT(adb->entries[i]);
result = isc_mutexblock_init(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, l, 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); \
if (l) \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT(dns_adbname_t, adb->nmp, ISC_TRUE, "adbname");
MPINIT(dns_adbnamehook_t, adb->nhmp, ISC_TRUE, "adbnamehook");
MPINIT(dns_adbzoneinfo_t, adb->zimp, ISC_TRUE, "adbzoneinfo");
MPINIT(dns_adbentry_t, adb->emp, ISC_TRUE, "adbentry");
MPINIT(dns_adbfind_t, adb->ahmp, ISC_TRUE, "adbfind");
MPINIT(dns_adbaddrinfo_t, adb->aimp, ISC_TRUE, "adbaddrinfo");
MPINIT(dns_adbfetch_t, adb->afmp, ISC_TRUE, "adbfetch");
MPINIT(dns_adbfetch6_t, adb->af6mp, ISC_TRUE, "adbfetch6");
#undef MPINIT
/*
* Allocate a timer and a task for our periodic cleanup.
*/
result = isc_task_create(adb->taskmgr, adb->mctx, 0, &adb->task);
if (result != ISC_R_SUCCESS)
goto fail3;
result = isc_task_onshutdown(adb->task, shutdown_task, adb);
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;
/*
* Normal return.
*/
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3:
if (adb->task != NULL)
isc_task_detach(&adb->task);
if (adb->timer != NULL)
isc_timer_detach(&adb->timer);
/* clean up entrylocks */
isc_mutexblock_destroy(adb->entrylocks, DNS_ADBENTRYLIST_LENGTH);
fail2: /* clean up namelocks */
isc_mutexblock_destroy(adb->namelocks, DNS_ADBNAMELIST_LENGTH);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->zimp != NULL)
isc_mempool_destroy(&adb->zimp);
if (adb->emp != NULL)
isc_mempool_destroy(&adb->emp);
if (adb->ahmp != NULL)
isc_mempool_destroy(&adb->ahmp);
if (adb->aimp != NULL)
isc_mempool_destroy(&adb->aimp);
if (adb->afmp != NULL)
isc_mempool_destroy(&adb->afmp);
if (adb->af6mp != NULL)
isc_mempool_destroy(&adb->af6mp);
isc_mutex_destroy(&adb->mplock);
fail0c:
isc_mutex_destroy(&adb->lock);
fail0b:
isc_random_invalidate(&adb->rand);
fail0a:
isc_mem_put(mem, adb, sizeof (dns_adb_t));
return (result);
}
void
dns_adb_detach(dns_adb_t **adbx)
{
dns_adb_t *adb;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
LOCK(&adb->lock);
dec_adb_erefcnt(adb, ISC_FALSE);
if (adb->erefcnt == 0)
shutdown_names(adb);
check_exit(adb);
UNLOCK(&adb->lock);
}
isc_result_t
dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
void *arg, dns_name_t *name, dns_name_t *zone,
unsigned int options, isc_stdtime_t now,
dns_adbfind_t **findp)
{
dns_adbfind_t *find;
dns_adbname_t *adbname;
int bucket;
isc_result_t result;
isc_boolean_t attach_to_task;
unsigned int wanted_addresses;
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(name != NULL);
REQUIRE(zone != NULL);
REQUIRE(findp != NULL && *findp == NULL);
if (WANTEVENT(options)) {
REQUIRE(task != NULL);
}
attach_to_task = ISC_FALSE;
result = ISC_R_UNEXPECTED;
wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
if (now == 0) {
result = isc_stdtime_get(&now);
if (result != ISC_R_SUCCESS)
return (result);
}
/*
* 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);
/*
* Remember what types of addresses we are interested in.
*/
find->options = options;
find->flags |= wanted_addresses;
/*
* Try to see if we know anything about this name at all.
*/
bucket = DNS_ADB_INVALIDBUCKET;
adbname = find_name_and_lock(adb, name, &bucket);
if (adb->name_sd[bucket]) {
DP(1, "lookup: returning ISC_R_SHUTTINGDOWN");
free_adbfind(adb, &find);
result = ISC_R_SHUTTINGDOWN;
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name.
*/
if (adbname == NULL) {
adbname = new_adbname(adb, name);
if (adbname == NULL) {
free_adbfind(adb, &find);
result = ISC_R_NOMEMORY;
goto out;
}
link_name(adb, bucket, adbname);
}
/*
* Expire old entries, etc.
*/
check_expire_namehooks(adbname, now);
/*
* Try to populate the name from the database and/or
* start fetches.
*/
if (!HAVE_INET(adbname)
&& !QUERY_INET(adbname->query_pending)
&& WANT_INET(wanted_addresses)) {
result = dbfind_name(find, zone, adbname, bucket, now,
dns_rdatatype_a);
if (result == ISC_R_SUCCESS) {
DP(1, "lookup: Found A for name %p in db",
adbname);
goto v6;
}
/*
* Try to start fetches for v4.
*/
result = fetch_name_v4(adbname, now);
if (result == ISC_R_SUCCESS) {
DP(1, "lookup: Started A fetch for name %p",
adbname);
adbname->query_pending |= DNS_ADBFIND_INET;
goto v6;
}
}
v6:
if (!HAVE_INET6(adbname)
&& !QUERY_INET6(adbname->query_pending)
&& WANT_INET6(wanted_addresses)) {
result = dbfind_a6(find, zone, adbname, bucket, now);
if (result == ISC_R_SUCCESS) {
DP(1, "lookup: Found A6 for name %p", adbname);
goto copy;
}
/*
* Try to start fetches for a6.
*/
result = fetch_name_a6(adbname, now);
if (result == ISC_R_SUCCESS) {
DP(1, "lookup: Started A6 fetch for name %p",
adbname);
adbname->query_pending |= DNS_ADBFIND_INET6;
goto copy;
}
}
/*
* Run through the name and copy out the bits we are
* interested in.
*/
copy:
copy_namehook_lists(adb, find, adbname, zone, now);
/*
* Attach to the name's query list if there are queries
* already running, and we have been asked to.
*/
if (WANTEVENT(find->options)
&& QUERYPENDING(wanted_addresses, adbname->query_pending)
&& (WANTEMPTYEVENT(find->options) ? !HAVE_ADDRS(find) : 1)) {
find->adbname = adbname;
find->name_bucket = bucket;
ISC_LIST_APPEND(adbname->finds, find, plink);
attach_to_task = ISC_TRUE;
find->query_pending = (adbname->query_pending
& wanted_addresses);
} else {
/*
* Remove the flag so the caller knows there will never
* be an event, and set internal flags to fake that
* the event was sent and freed, so dns_adb_destroyfind() will
* do the right thing.
*/
find->options &= ~DNS_ADBFIND_WANTEVENT;
find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
}
find->partial_result |= (adbname->partial_result & wanted_addresses);
result = ISC_R_SUCCESS;
out:
if (find != NULL) {
*findp = find;
if (attach_to_task) {
isc_task_t *taskp;
taskp = NULL;
isc_task_attach(task, &taskp);
find->event.sender = taskp;
find->event.action = action;
find->event.arg = arg;
}
}
dns_adb_dumpfind(find, stderr);
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
isc_result_t
dns_adb_deletename(dns_adb_t *adb, dns_name_t *host)
{
int name_bucket;
dns_adbname_t *name;
isc_boolean_t decr_adbrefcnt;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
name = NULL;
/*
* Find the name.
*/
name_bucket = DNS_ADB_INVALIDBUCKET;
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
UNLOCK(&adb->namelocks[name_bucket]);
return (ISC_R_NOTFOUND);
}
kill_name(&name, DNS_EVENT_ADBNAMEDELETED);
decr_adbrefcnt = ISC_FALSE;
if (adb->name_sd[name_bucket] && (adb->name_refcnt[name_bucket] == 0))
decr_adbrefcnt = ISC_TRUE;
if (name_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[name_bucket]);
if (decr_adbrefcnt)
dec_adb_irefcnt(adb, ISC_TRUE);
return (DNS_R_SUCCESS);
}
/* XXXMLG needs v6 support */
isc_result_t
dns_adb_insert(dns_adb_t *adb, dns_name_t *host, isc_sockaddr_t *addr,
dns_ttl_t ttl, isc_stdtime_t now)
{
dns_adbname_t *name;
isc_boolean_t free_name;
dns_adbentry_t *entry;
isc_boolean_t free_entry;
dns_adbnamehook_t *namehook;
isc_boolean_t free_namehook;
int name_bucket, addr_bucket; /* unlock if != DNS_ADB_INVALIDBUCKET */
isc_result_t result;
isc_stdtime_t expire_time;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(host != NULL);
REQUIRE(addr != NULL);
if (now == 0) {
result = isc_stdtime_get(&now);
if (result != ISC_R_SUCCESS)
return (result);
}
expire_time = now + ttl;
name = NULL;
free_name = ISC_FALSE;
entry = NULL;
free_entry = ISC_FALSE;
namehook = NULL;
free_namehook = ISC_FALSE;
result = ISC_R_UNEXPECTED;
/*
* First, see if the host is already in the database. If it is,
* don't make a new host entry. If not, copy the name and name's
* contents into our structure and allocate what we'll need
* to attach things together.
*/
name_bucket = DNS_ADB_INVALIDBUCKET;
name = find_name_and_lock(adb, host, &name_bucket);
if (name == NULL) {
name = new_adbname(adb, host);
if (name == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_name = ISC_TRUE;
name->expire_v4 = ISC_MIN(expire_time, name->expire_v4);
}
/*
* Now, while keeping the name locked, search for the address.
* Three possibilities: One, the address doesn't exist.
* Two, the address exists, but we aren't linked to it.
* Three, the address exists and we are linked to it.
* (1) causes a new entry and namehook to be created.
* (2) causes only a new namehook.
* (3) is an error.
*/
addr_bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, addr, &addr_bucket);
/*
* Case (1): new entry and namehook.
*/
if (entry == NULL) {
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_entry = ISC_TRUE;
}
/*
* Case (3): entry exists, we're linked.
*/
namehook = ISC_LIST_HEAD(name->v4);
while (namehook != NULL) {
if (namehook->entry == entry) {
result = ISC_R_EXISTS;
goto out;
}
namehook = ISC_LIST_NEXT(namehook, plink);
}
/*
* Case (2): New namehook, link to entry from above.
*/
namehook = new_adbnamehook(adb, entry);
if (namehook == NULL) {
result = ISC_R_NOMEMORY;
goto out;
}
free_namehook = ISC_TRUE;
ISC_LIST_APPEND(name->v4, namehook, plink);
entry->lock_bucket = addr_bucket;
inc_entry_refcnt(adb, entry, ISC_FALSE);
entry->sockaddr = *addr;
/*
* If needed, string up the name and entry.
*/
if (!ISC_LINK_LINKED(name, plink))
link_name(adb, name_bucket, name);
if (!ISC_LINK_LINKED(entry, plink))
ISC_LIST_PREPEND(adb->entries[addr_bucket], entry, plink);
name->expire_v4 = ISC_MIN(name->expire_v4, expire_time);
UNLOCK(&adb->namelocks[name_bucket]);
name_bucket = DNS_ADB_INVALIDBUCKET;
UNLOCK(&adb->entrylocks[addr_bucket]);
addr_bucket = DNS_ADB_INVALIDBUCKET;
return (ISC_R_SUCCESS);
out:
if (free_name)
free_adbname(adb, &name);
if (free_entry)
isc_mempool_put(adb->emp, entry);
if (free_namehook)
isc_mempool_put(adb->nhmp, namehook);
if (name_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[name_bucket]);
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[addr_bucket]);
return (result);
}
void
dns_adb_destroyfind(dns_adbfind_t **findp)
{
dns_adbfind_t *find;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *ai;
int bucket;
dns_adb_t *adb;
REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
find = *findp;
*findp = NULL;
LOCK(&find->lock);
DP(1, "dns_adb_done on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(EVENT_FREED(find));
bucket = find->name_bucket;
INSIST(bucket == DNS_ADB_INVALIDBUCKET);
UNLOCK(&find->lock);
/*
* The find doesn't exist on any list, and nothing is locked.
* Return the find to the memory pool, and decrement the adb's
* reference count.
*/
ai = ISC_LIST_HEAD(find->list);
while (ai != NULL) {
ISC_LIST_UNLINK(find->list, ai, publink);
entry = ai->entry;
ai->entry = NULL;
ai->sockaddr = NULL;
INSIST(DNS_ADBENTRY_VALID(entry));
dec_entry_refcnt(adb, entry, ISC_TRUE);
free_adbaddrinfo(adb, &ai);
ai = ISC_LIST_HEAD(find->list);
}
/*
* WARNING: The find is freed with the adb locked. This is done
* to avoid a race condition where we free the find, some other
* thread tests to see if it should be destroyed, detects it should
* be, destroys it, and then we try to lock it for our check, but the
* lock is destroyed.
*/
LOCK(&adb->lock);
free_adbfind(adb, &find);
check_exit(adb);
UNLOCK(&adb->lock);
}
void
dns_adb_cancelfind(dns_adbfind_t *find)
{
isc_event_t *ev;
isc_task_t *task;
dns_adb_t *adb;
int bucket;
LOCK(&find->lock);
DP(1, "dns_adb_cancelfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(!EVENT_FREED(find));
REQUIRE(WANTEVENT(find->options));
bucket = find->name_bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the find.
*/
violate_locking_hierarchy(&find->lock, &adb->namelocks[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[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
cleanup:
if (!EVENT_SENT(find)) {
ev = &find->event;
task = ev->sender;
ev->sender = find;
ev->type = DNS_EVENT_ADBCANCELED;
ev->destroy = event_free;
ev->destroy_arg = find;
DP(1, "Sending event %p to task %p for find %p",
ev, task, find);
isc_task_sendanddetach(&task, &ev);
}
UNLOCK(&find->lock);
}
void
dns_adb_dump(dns_adb_t *adb, FILE *f)
{
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(f != NULL);
/*
* Lock the adb itself, lock all the name buckets, then lock all
* the entry buckets. This should put the adb into a state where
* nothing can change, so we can iterate through everything and
* print at our leasure.
*/
LOCK(&adb->lock);
dump_adb(adb, f);
UNLOCK(&adb->lock);
}
static void
dump_adb(dns_adb_t *adb, FILE *f)
{
int i;
isc_sockaddr_t *sa;
dns_adbname_t *name;
dns_adbentry_t *entry;
char tmp[512];
const char *tmpp;
fprintf(f, "ADB %p DUMP:\n", adb);
fprintf(f, "erefcnt %u, irefcnt %u, finds out %u\n",
adb->erefcnt, adb->irefcnt,
isc_mempool_getallocated(adb->nhmp));
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
LOCK(&adb->namelocks[i]);
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
LOCK(&adb->entrylocks[i]);
/*
* Dump the names
*/
fprintf(f, "Names:\n");
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++) {
name = ISC_LIST_HEAD(adb->names[i]);
if (name == NULL)
continue;
fprintf(f, "Name bucket %d:\n", i);
while (name != NULL) {
fprintf(f, "name %p\n", name);
if (!DNS_ADBNAME_VALID(name))
fprintf(f, "\tMAGIC %08x\n", name->magic);
fprintf(f, "\texpiry [%u %u] ",
name->expire_v4, name->expire_v6);
print_dns_name(f, &name->name);
fprintf(f, "\n");
print_namehook_list(f, name);
print_fetch_list(f, name);
print_find_list(f, name);
fprintf(f, "\n");
name = ISC_LIST_NEXT(name, plink);
}
}
/*
* Dump the entries
*/
fprintf(f, "Entries:\n");
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++) {
entry = ISC_LIST_HEAD(adb->entries[i]);
if (entry == NULL)
continue;
fprintf(f, "Entry bucket %d:\n", i);
while (entry != NULL) {
if (!DNS_ADBENTRY_VALID(entry))
fprintf(f, "\tMAGIC %08x\n", entry->magic);
if (entry->lock_bucket != i)
fprintf(f, "\tWRONG BUCKET! lock_bucket %d\n",
entry->lock_bucket);
sa = &entry->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET,
&sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6,
&sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\t%p: refcnt %u flags %08x goodness %d"
" srtt %u addr %s\n",
entry, entry->refcnt, entry->flags,
entry->goodness, entry->srtt, tmpp);
entry = ISC_LIST_NEXT(entry, plink);
}
}
/*
* Unlock everything
*/
for (i = 0 ; i < DNS_ADBENTRYLIST_LENGTH ; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0 ; i < DNS_ADBNAMELIST_LENGTH ; i++)
UNLOCK(&adb->namelocks[i]);
}
void
dns_adb_dumpfind(dns_adbfind_t *find, FILE *f)
{
char tmp[512];
const char *tmpp;
dns_adbaddrinfo_t *ai;
isc_sockaddr_t *sa;
/*
* Not used currently, in the API Just In Case we
* want to dump out the name and/or entries too.
*/
LOCK(&find->lock);
fprintf(f, "Find %p\n", find);
fprintf(f, "\tqpending %08x partial %08x options %08x\n",
find->query_pending, find->partial_result,
find->options);
fprintf(f, "\tname_bucket %d, name %p, event sender %p\n",
find->name_bucket, find->adbname, find->event.sender);
ai = ISC_LIST_HEAD(find->list);
if (ai != NULL)
fprintf(f, "\tAddresses:\n");
while (ai != NULL) {
sa = ai->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "CANNOT TRANSLATE ADDRESS!";
fprintf(f, "\t\tentry %p, flags %08x goodness %d"
" srtt %u addr %s\n",
ai->entry, ai->flags, ai->goodness, ai->srtt, tmpp);
ai = ISC_LIST_NEXT(ai, publink);
}
UNLOCK(&find->lock);
}
static void
print_dns_name(FILE *f, dns_name_t *name)
{
char buf[257];
isc_buffer_t b;
INSIST(f != NULL);
memset(buf, 0, sizeof (buf));
isc_buffer_init(&b, buf, sizeof (buf) - 1, ISC_BUFFERTYPE_TEXT);
dns_name_totext(name, ISC_FALSE, &b);
fprintf(f, buf); /* safe, since names < 256 chars, and we memset */
}
static void
print_namehook_list(FILE *f, dns_adbname_t *n)
{
dns_adbnamehook_t *nh;
nh = ISC_LIST_HEAD(n->v4);
while (nh != NULL) {
fprintf(f, "\t\tHook(V4) %p -> entry %p\n", nh, nh->entry);
nh = ISC_LIST_NEXT(nh, plink);
}
nh = ISC_LIST_HEAD(n->v6);
while (nh != NULL) {
fprintf(f, "\t\tHook(V6) %p -> entry %p\n", nh, nh->entry);
nh = ISC_LIST_NEXT(nh, plink);
}
}
static void
print_fetch(FILE *f, dns_adbfetch_t *ft)
{
fprintf(f, "\t\tFetch: %p -> { nh %p, entry %p, fetch %p }\n",
ft, ft->namehook, ft->entry, ft->fetch);
}
static void
print_fetch_list(FILE *f, dns_adbname_t *n)
{
if (n->fetch_a != NULL)
print_fetch(f, n->fetch_a);
if (n->fetch_a != NULL)
print_fetch(f, n->fetch_a);
}
static void
print_find_list(FILE *f, dns_adbname_t *name)
{
dns_adbfind_t *find;
find = ISC_LIST_HEAD(name->finds);
while (find != NULL) {
fprintf(f, "\t\tFind %p\n", find);
find = ISC_LIST_NEXT(find, plink);
}
}
/*
* On entry, "bucket" refers to a locked name bucket, "find" is not NULL,
* and "name" is the name we are looking for. We will allocate an adbname
* and return a pointer to it in *adbnamep.
*
* If we return ISC_R_SUCCESS, the new name will have been allocated, and
* perhaps some namehooks will have been filled in with valid entries, and
* perhaps some fetches have been started.
*/
static isc_result_t
dbfind_name(dns_adbfind_t *find, dns_name_t *zone,
dns_adbname_t *adbname, int bucket, isc_stdtime_t now,
dns_rdatatype_t rdtype)
{
isc_result_t result;
isc_boolean_t use_hints;
dns_rdataset_t rdataset;
dns_adb_t *adb;
INSIST(DNS_ADBFIND_VALID(find));
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(bucket != DNS_ADB_INVALIDBUCKET);
INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
if (adb->view == NULL)
return (ISC_R_NOTIMPLEMENTED);
result = ISC_R_UNEXPECTED;
use_hints = dns_name_equal(zone, dns_rootname);
dns_rdataset_init(&rdataset);
result = dns_view_find(adb->view, &adbname->name, rdtype,
now, DNS_DBFIND_GLUEOK, use_hints,
&rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case DNS_R_SUCCESS:
/*
* Found in the database. Even if we can't copy out
* any information, return success, or else a fetch
* will be made, which will only make things worse.
*/
(void)import_rdataset(adbname, &rdataset, now);
result = ISC_R_SUCCESS;
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static isc_result_t
dbfind_a6(dns_adbfind_t *find, dns_name_t *zone,
dns_adbname_t *adbname, int bucket, isc_stdtime_t now)
{
isc_result_t result;
isc_boolean_t use_hints;
dns_rdataset_t rdataset;
dns_adb_t *adb;
dns_a6context_t a6ctx;
INSIST(DNS_ADBFIND_VALID(find));
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(bucket != DNS_ADB_INVALIDBUCKET);
INSIST(NO_FETCHES_A6(adbname));
if (adb->view == NULL)
return (ISC_R_NOTIMPLEMENTED);
result = ISC_R_UNEXPECTED;
use_hints = dns_name_equal(zone, dns_rootname);
dns_rdataset_init(&rdataset);
result = dns_view_find(adb->view, &adbname->name, dns_rdatatype_a6,
now, DNS_DBFIND_GLUEOK, use_hints,
&rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case DNS_R_SUCCESS:
/*
* Start a6 chain follower. There is no need to poke people
* who might be waiting, since this is call requires there
* are none.
*/
dns_a6_init(&a6ctx, a6find, NULL, import_a6,
a6missing, adbname);
(void)dns_a6_foreach(&a6ctx, &rdataset);
result = ISC_R_SUCCESS;
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static void
fetch_callback_v4(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;
(void)task;
INSIST(ev->type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
fetch = name->fetch_a;
name->fetch_a = NULL;
INSIST(fetch != NULL);
INSIST(fetch->fetch == dev->fetch);
dns_resolver_destroyfetch(adb->view->resolver, &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)) {
isc_boolean_t decr_adbrefcnt;
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
decr_adbrefcnt = ISC_FALSE;
if (adb->name_sd[bucket] && (adb->name_refcnt[bucket] == 0))
decr_adbrefcnt = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (decr_adbrefcnt)
dec_adb_irefcnt(adb, ISC_TRUE);
return;
}
/*
* 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)
goto out;
/*
* We got something potentially useful.
*/
result = isc_stdtime_get(&now);
if (result == ISC_R_SUCCESS)
result = import_rdataset(name, &fetch->rdataset, now);
if (result == ISC_R_SUCCESS)
ev_status = DNS_EVENT_ADBMOREADDRESSES;
out:
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
clean_finds_at_name(name, ev_status, DNS_ADBFIND_INET);
name->query_pending &= ~DNS_ADBFIND_INET;
UNLOCK(&adb->namelocks[bucket]);
return;
}
static void
fetch_callback_aaaa(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;
(void)task;
INSIST(ev->type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
fetch = name->fetch_aaaa;
name->fetch_aaaa = NULL;
INSIST(fetch != NULL);
INSIST(fetch->fetch == dev->fetch);
dns_resolver_destroyfetch(adb->view->resolver, &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)) {
isc_boolean_t decr_adbrefcnt;
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
decr_adbrefcnt = ISC_FALSE;
if (adb->name_sd[bucket] && (adb->name_refcnt[bucket] == 0))
decr_adbrefcnt = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (decr_adbrefcnt)
dec_adb_irefcnt(adb, ISC_TRUE);
return;
}
/*
* 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)
goto out;
/*
* We got something potentially useful.
*/
result = isc_stdtime_get(&now);
if (result == ISC_R_SUCCESS)
result = import_rdataset(name, &fetch->rdataset, now);
if (result == ISC_R_SUCCESS)
ev_status = DNS_EVENT_ADBMOREADDRESSES;
out:
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
clean_finds_at_name(name, ev_status, DNS_ADBFIND_INET6);
name->query_pending &= ~DNS_ADBFIND_INET6;
UNLOCK(&adb->namelocks[bucket]);
return;
}
static void
fetch_callback_a6(isc_task_t *task, isc_event_t *ev)
{
dns_fetchevent_t *dev;
dns_adbname_t *name;
dns_adb_t *adb;
dns_adbfetch6_t *fetch;
int bucket;
(void)task;
INSIST(ev->type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
INSIST(!NAME_NEEDSPOKE(name));
for (fetch = ISC_LIST_HEAD(name->fetches_a6);
fetch != NULL;
fetch = ISC_LIST_NEXT(fetch, plink))
if (fetch->fetch == dev->fetch)
break;
INSIST(fetch != NULL);
ISC_LIST_UNLINK(name->fetches_a6, fetch, plink);
dns_resolver_destroyfetch(adb->view->resolver, &fetch->fetch);
dev->fetch = NULL;
/*
* Cleanup things we don't care about.
*/
if (dev->node != NULL)
dns_db_detachnode(dev->db, &dev->node);
if (dev->db != NULL)
dns_db_detach(&dev->db);
/*
* If this name is marked as dead, clean up, throwing away
* potentially good data.
*/
if (NAME_DEAD(name)) {
isc_boolean_t decr_adbrefcnt;
free_adbfetch6(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
decr_adbrefcnt = ISC_FALSE;
if (adb->name_sd[bucket] && (adb->name_refcnt[bucket] == 0))
decr_adbrefcnt = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (decr_adbrefcnt)
dec_adb_irefcnt(adb, ISC_TRUE);
return;
}
/*
* 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)
goto out;
/*
* We got something potentially useful. Run the A6 chain
* follower on this A6 rdataset.
*/
fetch->a6ctx.chains = name->chains;
(void)dns_a6_foreach(&fetch->a6ctx, dev->rdataset);
out:
free_adbfetch6(adb, &fetch);
isc_event_free(&ev);
if (NAME_NEEDSPOKE(name))
clean_finds_at_name(name, DNS_EVENT_ADBMOREADDRESSES,
DNS_ADBFIND_INET6);
else if (ISC_LIST_EMPTY(name->fetches_a6)) {
clean_finds_at_name(name, DNS_EVENT_ADBNOMOREADDRESSES,
DNS_ADBFIND_INET6);
name->query_pending &= ~DNS_ADBFIND_INET6;
}
name->flags &= ~NAME_NEEDS_POKE;
UNLOCK(&adb->namelocks[bucket]);
return;
}
static isc_result_t
fetch_name_v4(dns_adbname_t *adbname, isc_stdtime_t now)
{
isc_result_t result;
dns_adbfetch_t *fetch;
dns_rdataset_t nameservers;
dns_name_t fname;
isc_buffer_t buffer;
unsigned char ndata[256];
dns_adb_t *adb;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(adbname->fetch_a == NULL);
isc_buffer_init(&buffer, ndata, sizeof(ndata), ISC_BUFFERTYPE_BINARY);
fetch = NULL;
dns_name_init(&fname, NULL);
dns_name_setbuffer(&fname, &buffer);
dns_rdataset_init(&nameservers);
result = dns_view_findzonecut(adb->view, &adbname->name, &fname, now,
0, ISC_TRUE, &nameservers, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a, &fname,
&nameservers, NULL, 0,
adb->task, fetch_callback_v4,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
adbname->fetch_a = fetch;
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
if (dns_rdataset_isassociated(&nameservers))
dns_rdataset_disassociate(&nameservers);
return (result);
}
static isc_result_t
fetch_name_aaaa(dns_adbname_t *adbname, isc_stdtime_t now)
{
isc_result_t result;
dns_adbfetch_t *fetch;
dns_rdataset_t nameservers;
dns_name_t fname;
isc_buffer_t buffer;
unsigned char ndata[256];
dns_adb_t *adb;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(adbname->fetch_aaaa == NULL);
isc_buffer_init(&buffer, ndata, sizeof(ndata), ISC_BUFFERTYPE_BINARY);
fetch = NULL;
dns_name_init(&fname, NULL);
dns_name_setbuffer(&fname, &buffer);
dns_rdataset_init(&nameservers);
result = dns_view_findzonecut(adb->view, &adbname->name, &fname, now,
0, ISC_TRUE, &nameservers, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_aaaa, &fname,
&nameservers, NULL, 0,
adb->task, fetch_callback_aaaa,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
adbname->fetch_aaaa = fetch;
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
if (dns_rdataset_isassociated(&nameservers))
dns_rdataset_disassociate(&nameservers);
return (result);
}
static isc_result_t
fetch_name_a6(dns_adbname_t *adbname, isc_stdtime_t now)
{
isc_result_t result;
dns_adbfetch6_t *fetch;
dns_rdataset_t nameservers;
dns_name_t fname;
isc_buffer_t buffer;
unsigned char ndata[256];
dns_adb_t *adb;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(adbname->fetch_aaaa == NULL);
isc_buffer_init(&buffer, ndata, sizeof(ndata), ISC_BUFFERTYPE_BINARY);
fetch = NULL;
dns_name_init(&fname, NULL);
dns_name_setbuffer(&fname, &buffer);
dns_rdataset_init(&nameservers);
result = dns_view_findzonecut(adb->view, &adbname->name, &fname, now,
0, ISC_TRUE, &nameservers, NULL);
if (result != ISC_R_SUCCESS)
goto cleanup;
fetch = new_adbfetch6(adb, adbname, NULL);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a6, &fname,
&nameservers, NULL, 0,
adb->task, fetch_callback_a6,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
ISC_LIST_APPEND(adbname->fetches_a6, fetch, plink);
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch6(adb, &fetch);
if (dns_rdataset_isassociated(&nameservers))
dns_rdataset_disassociate(&nameservers);
return (result);
}
isc_result_t
dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
isc_stdtime_t expire_time)
{
dns_adbzoneinfo_t *zi;
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(zone != NULL);
zi = new_adbzoneinfo(adb, zone);
if (zi == NULL)
return (ISC_R_NOMEMORY);
zi->lame_timer = expire_time;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, plink);
UNLOCK(&adb->entrylocks[bucket]);
return (ISC_R_SUCCESS);
}
void
dns_adb_adjustgoodness(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
int goodness_adjustment)
{
int bucket;
int old_goodness, new_goodness;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
if (goodness_adjustment == 0)
return;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
old_goodness = addr->entry->goodness;
if (goodness_adjustment > 0) {
if (old_goodness > INT_MAX - goodness_adjustment)
new_goodness = INT_MAX;
else
new_goodness = old_goodness + goodness_adjustment;
} else {
if (old_goodness < INT_MIN - goodness_adjustment)
new_goodness = INT_MAX;
else
new_goodness = old_goodness + goodness_adjustment;
}
addr->entry->goodness = new_goodness;
addr->goodness = new_goodness;
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int rtt, unsigned int factor)
{
int bucket;
unsigned int new_srtt;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
if (factor == 0)
factor = 4;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
new_srtt = (addr->entry->srtt * (factor - 1) + rtt) / factor;
addr->entry->srtt = new_srtt;
addr->srtt = new_srtt;
UNLOCK(&adb->entrylocks[bucket]);
}