adb.c revision 784d78b831830bf7b53507e742254f9c49295752
1633838b8255282d10af15c5c84cee5a51466712Bob Halley/*
94d102893aeb8ecea49dcda64e742835ffe0c102Bob Halley * Copyright (C) 1999-2001 Internet Software Consortium.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * Permission to use, copy, modify, and distribute this software for any
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * purpose with or without fee is hereby granted, provided that the above
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * copyright notice and this permission notice appear in all copies.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley *
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
1633838b8255282d10af15c5c84cee5a51466712Bob Halley * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1633838b8255282d10af15c5c84cee5a51466712Bob Halley */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley/* $Id: adb.c,v 1.175 2001/04/30 18:27:10 gson Exp $ */
d25afd60ee2286cb171c4960a790f3d7041b6f85Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Implementation notes
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * --------------------
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * In finds, if task == NULL, no events will be generated, and no events
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * have been sent. If task != NULL but taskaction == NULL, an event has been
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * posted but not yet freed. If neither are NULL, no event was posted.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley * After we have cleaned all buckets, dump the database contents.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#if 0
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define DUMP_ADB_AFTER_CLEANING
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#endif
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#include <config.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <limits.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/mutexblock.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/netaddr.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/random.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/string.h> /* Required for HP/UX (and others?) */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/task.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/timer.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <isc/util.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/a6.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/adb.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/db.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/events.h>
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews#include <dns/log.h>
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews#include <dns/rdata.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/rdataset.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/rdatastruct.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/resolver.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#include <dns/result.h>
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define DNS_ADB_MAGIC 0x44616462 /* Dadb. */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define DNS_ADBNAME_MAGIC 0x6164624e /* adbN. */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define DNS_ADBNAMEHOOK_MAGIC 0x61644e48 /* adNH. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_MAGIC 0x6164625a /* adbZ. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_MAGIC 0x61646245 /* adbE. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_MAGIC 0x61644634 /* adF4. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_MAGIC 0x61644636 /* adF6. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The number of buckets needs to be a prime (for good hashing).
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * XXXRTH How many buckets do we need?
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NBUCKETS 1009 /* how many buckets for names/addrs */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * For type 3 negative cache entries, we will remember that the address is
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * broken for this long. XXXMLG This is also used for actual addresses, too.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The intent is to keep us from constantly asking about A/A6/AAAA records
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * if the zone has extremely low TTLs.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_CACHE_MINIMUM 10 /* seconds */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * buckets are cleaned in CLEAN_PERIOD seconds.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_PERIOD 3600
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_SECONDS 30
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FREE_ITEMS 64 /* free count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FILL_COUNT 16 /* fill count for memory pools */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbnamehook dns_adbnamehook_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbfetch dns_adbfetch_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleytypedef struct dns_adbfetch6 dns_adbfetch6_t;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystruct dns_adb {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mutex_t lock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mutex_t ilock;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mem_t *mctx;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_view_t *view;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_timermgr_t *timermgr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_timer_t *timer;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_taskmgr_t *taskmgr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *task;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_interval_t tick_interval;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley int next_cleanbucket;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int irefcnt;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unsigned int erefcnt;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mutex_t mplock;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *nmp; /* dns_adbname_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *nhmp; /* dns_adbnamehook_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *emp; /* dns_adbentry_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *ahmp; /* dns_adbfind_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *afmp; /* dns_adbfetch_t */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_t *af6mp; /* dns_adbfetch6_t */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley /*
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * Bucketized locks and lists for names.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley *
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * XXXRTH Have a per-bucket structure that contains all of these?
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley dns_adbnamelist_t names[NBUCKETS];
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley isc_mutex_t namelocks[NBUCKETS];
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_boolean_t name_sd[NBUCKETS];
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int name_refcnt[NBUCKETS];
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * Bucketized locks for entries.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley *
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * XXXRTH Have a per-bucket structure that contains all of these?
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentrylist_t entries[NBUCKETS];
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_mutex_t entrylocks[NBUCKETS];
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int entry_refcnt[NBUCKETS];
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_event_t cevent;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_boolean_t cevent_sent;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_boolean_t shutting_down;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_eventlist_t whenshutdown;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley};
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley/*
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley * XXXMLG Document these structures.
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley */
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleystruct dns_adbname {
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int magic;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley dns_name_t name;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley dns_adb_t *adb;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int partial_result;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int flags;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley int lock_bucket;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley dns_name_t target;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_stdtime_t expire_target;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_stdtime_t expire_v4;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley isc_stdtime_t expire_v6;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley unsigned int chains;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley dns_adbnamehooklist_t v4;
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley dns_adbnamehooklist_t v6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch_t *fetch_a;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch_t *fetch_aaaa;
3740b569ae76295b941d57a724a43beb75b533baBob Halley ISC_LIST(dns_adbfetch6_t) fetches_a6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int fetch_err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int fetch6_err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfindlist_t finds;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK(dns_adbname_t) plink;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystruct dns_adbfetch {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbnamehook_t *namehook;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_fetch_t *fetch;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_t rdataset;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystruct dns_adbfetch6 {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
3740b569ae76295b941d57a724a43beb75b533baBob Halley unsigned int flags;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbnamehook_t *namehook;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_fetch_t *fetch;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_t rdataset;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_a6context_t a6ctx;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK(dns_adbfetch6_t) plink;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * dns_adbnamehook_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This is a small widget that dangles off a dns_adbname_t. It contains a
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * pointer to the address information about this host, and a link to the next
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * namehook that will contain the next address this host has.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystruct dns_adbnamehook {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
3740b569ae76295b941d57a724a43beb75b533baBob Halley ISC_LINK(dns_adbnamehook_t) plink;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * dns_adbzoneinfo_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This is a small widget that holds zone-specific information about an
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * address. Currently limited to lameness, but could just as easily be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * extended to other types of information about zones.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystruct dns_adbzoneinfo {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_t zone;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_stdtime_t lame_timer;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK(dns_adbzoneinfo_t) plink;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * An address entry. It holds quite a bit of information about addresses,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * including edns state (in "flags"), rtt, and of course the address of
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * the host.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystruct dns_adbentry {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int magic;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley int lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int refcnt;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int flags;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int srtt;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_sockaddr_t sockaddr;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_stdtime_t expires;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * A nonzero 'expires' field indicates that the entry should
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * persist until that time. This allows entries found
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * using dns_adb_findaddrinfo() to persist for a limited time
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * even though they are not necessarily associated with a
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * name.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LINK(dns_adbentry_t) plink;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley};
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Internal functions (and prototypes).
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
3740b569ae76295b941d57a724a43beb75b533baBob Halleystatic inline void free_adbfind(dns_adb_t *, dns_adbfind_t **);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley in_port_t);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_a6context_t *);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley unsigned int, int *);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleystatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley isc_sockaddr_t *, int *);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void print_dns_name(FILE *, dns_name_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void print_namehook_list(FILE *, const char *legend,
1366b7833c86343de278480b9abd71754e418bfaBob Halley dns_adbnamehooklist_t *list,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley isc_boolean_t debug);
f0bbac2c0f1afa74b88cab902daf11202ebe7cbdBob Halleystatic void print_find_list(FILE *, dns_adbname_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void print_fetch_list(FILE *, dns_adbname_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic inline void dec_adb_irefcnt(dns_adb_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic inline void inc_adb_erefcnt(dns_adb_t *, isc_boolean_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic inline void dec_adb_erefcnt(dns_adb_t *, isc_boolean_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley isc_boolean_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic inline void dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley isc_boolean_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void clean_target(dns_adb_t *, dns_name_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley unsigned int);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void check_expire_namehooks(dns_adbname_t *, isc_stdtime_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void cancel_fetches_at_name(dns_adbname_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley dns_rdatatype_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic isc_result_t fetch_name_aaaa(dns_adbname_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic inline void check_exit(dns_adb_t *);
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halleystatic void timer_cleanup(isc_task_t *, isc_event_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic void destroy(dns_adb_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic void shutdown_names(dns_adb_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic void shutdown_entries(dns_adb_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic inline void unlink_name(dns_adb_t *, dns_adbname_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic inline void unlink_entry(dns_adb_t *, dns_adbentry_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic void kill_name(dns_adbname_t **, isc_eventtype_t);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
1366b7833c86343de278480b9abd71754e418bfaBob Halleystatic isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t);
1366b7833c86343de278480b9abd71754e418bfaBob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halley/*
1366b7833c86343de278480b9abd71754e418bfaBob Halley * MUST NOT overlap DNS_ADBFIND_* flags!
1366b7833c86343de278480b9abd71754e418bfaBob Halley */
1366b7833c86343de278480b9abd71754e418bfaBob Halley#define FIND_EVENT_SENT 0x40000000
1366b7833c86343de278480b9abd71754e418bfaBob Halley#define FIND_EVENT_FREED 0x80000000
1366b7833c86343de278480b9abd71754e418bfaBob Halley#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
1366b7833c86343de278480b9abd71754e418bfaBob Halley#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
1366b7833c86343de278480b9abd71754e418bfaBob Halley
1366b7833c86343de278480b9abd71754e418bfaBob Halley#define NAME_NEEDS_POKE 0x80000000
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#define NAME_IS_DEAD 0x40000000
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#define NAME_HINT_OK DNS_ADBFIND_HINTOK
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
bcfcece57e9411ee4bd352b45a8b1ac1dbcf01f4Bob Halley#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * To the name, address classes are all that really exist. If it has a
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Fetches are broken out into A, AAAA, and A6 types. In some cases,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * however, it makes more sense to test for a particular class of fetches,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * like V4 or V6 above.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley#define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Was this fetch started using the hints database?
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Was this the initial fetch for the A6 record? If so, we might want to
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * start AAAA queries if it fails.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FETCH_FIRST_A6 0x80000000
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Find options and tests to see if there are addresses on the list.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_STARTATROOT(fn) (((fn)->options & DNS_ADBFIND_STARTATROOT) \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * These are currently used on simple unsigned ints, so they are
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * not really associated with any particular type.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Find out if the flags on a name (nf) indicate if it is a hint or
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * glue, and compare this to the appropriate bits set in o, to see if
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * this is ok.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define ENTER_LEVEL 50
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define EXIT_LEVEL ENTER_LEVEL
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define CLEAN_LEVEL 100
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define DEF_LEVEL 5
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NCACHE_LEVEL 20
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (r) == DNS_R_NCACHENXRRSET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (r) == DNS_R_NXRRSET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (r) == DNS_R_NCACHENXDOMAIN)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (r) == DNS_R_NXRRSET || \
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (r) == DNS_R_HINTNXRRSET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Error state rankings.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_SUCCESS 0 /* highest rank */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_CANCELED 1
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_FAILURE 2
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_NXDOMAIN 3
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_NXRRSET 4
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_UNEXPECTED 5
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_NOTFOUND 6
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define FIND_ERR_MAX 7
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic const char *errnames[] = {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "success",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "canceled",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "failure",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "nxdomain",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "nxrrset",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "unexpected",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "not_found"
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley};
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley#define NEWERR(old, new) (ISC_MIN((old), (new)))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic isc_result_t find_err_map[FIND_ERR_MAX] = {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_R_SUCCESS,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_R_CANCELED,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_R_FAILURE,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DNS_R_NXDOMAIN,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DNS_R_NXRRSET,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_R_UNEXPECTED,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_R_NOTFOUND /* not YET found */
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley};
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob HalleyDP(int level, const char *format, ...) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley va_list args;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley va_start(args, format);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_log_vwrite(dns_lctx,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LOG_DEBUG(level), format, args);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley va_end(args);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_ttl_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyttlclamp(dns_ttl_t ttl) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (ttl < ADB_CACHE_MINIMUM)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ttl = ADB_CACHE_MINIMUM;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (ttl > ADB_CACHE_MAXIMUM)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ttl = ADB_CACHE_MAXIMUM;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ttl);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley/*
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley * Requires the adbname bucket be locked and that no entry buckets be locked.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley *
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley * This code handles A and AAAA rdatasets only.
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley */
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halleystatic isc_result_t
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halleyimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley isc_stdtime_t now)
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley{
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley isc_result_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adb_t *adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbnamehook_t *nh;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_t rdata = DNS_RDATA_INIT;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley struct in_addr ina;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley struct in6_addr in6a;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_sockaddr_t sockaddr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *foundentry; /* NO CLEAN UP! */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int addr_bucket;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_boolean_t new_addresses_added;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_rdatatype_t rdtype;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int findoptions;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBNAME_VALID(adbname));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb = adbname->adb;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley INSIST(DNS_ADB_VALID(adb));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley rdtype = rdataset->type;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (rdtype == dns_rdatatype_a)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley findoptions = DNS_ADBFIND_INET;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley findoptions = DNS_ADBFIND_INET6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addr_bucket = DNS_ADB_INVALIDBUCKET;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley new_addresses_added = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_rdataset_first(rdataset);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (result == ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_reset(&rdata);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_current(rdataset, &rdata);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (rdtype == dns_rdatatype_a) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(rdata.length == 4);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley memcpy(&ina.s_addr, rdata.data, 4);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley isc_sockaddr_fromin(&sockaddr, &ina, 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(rdata.length == 16);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley memcpy(in6a.s6_addr, rdata.data, 16);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(nh == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh = new_adbnamehook(adb, NULL);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley if (nh == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adbname->partial_result |= findoptions;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ISC_R_NOMEMORY;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto fail;
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (foundentry == NULL) {
3740b569ae76295b941d57a724a43beb75b533baBob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry = new_adbentry(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adbname->partial_result |= findoptions;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = ISC_R_NOMEMORY;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto fail;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->sockaddr = sockaddr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt = 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->entry = entry;
3740b569ae76295b941d57a724a43beb75b533baBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley link_entry(adb, addr_bucket, entry);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley } else {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley foundentry->refcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->entry = foundentry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley new_addresses_added = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (rdtype == dns_rdatatype_a)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_APPEND(adbname->v4, nh, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_APPEND(adbname->v6, nh, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh = NULL;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley result = dns_rdataset_next(rdataset);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley fail:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (nh != NULL)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley free_adbnamehook(adb, &nh);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley rdataset->ttl = ttlclamp(rdataset->ttl);
3740b569ae76295b941d57a724a43beb75b533baBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (rdtype == dns_rdatatype_a) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley adbname->expire_v4, now + rdataset->ttl);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley now + rdataset->ttl);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adbname->expire_v6, now + rdataset->ttl);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley now + rdataset->ttl);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (new_addresses_added) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * Lie a little here. This is more or less so code that cares
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * can find out if any new information was added or not.
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (ISC_R_SUCCESS);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return (result);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley}
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic void
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleyimport_a6(dns_a6context_t *a6ctx) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_adbname_t *name;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adb_t *adb;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_adbnamehook_t *nh;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_adbentry_t *foundentry; /* NO CLEAN UP! */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int addr_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_sockaddr_t sockaddr;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley name = a6ctx->arg;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley INSIST(DNS_ADBNAME_VALID(name));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb = name->adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADB_VALID(adb));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley addr_bucket = DNS_ADB_INVALIDBUCKET;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh = new_adbnamehook(adb, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (nh == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto fail;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 0);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (foundentry == NULL) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry = new_adbentry(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result |= DNS_ADBFIND_INET6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto fail;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->sockaddr = sockaddr;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt = 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->entry = entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley link_entry(adb, addr_bucket, entry);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley foundentry->refcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->entry = foundentry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_APPEND(name->v6, nh, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fail:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v6, a6ctx->expiration);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->flags |= NAME_NEEDS_POKE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley if (nh != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_adbnamehook(adb, &nh);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * Requires the name's bucket be locked.
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleykill_name(dns_adbname_t **n, isc_eventtype_t ev) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley dns_adbname_t *name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adb_t *adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(n != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name = *n;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *n = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBNAME_VALID(name));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb = name->adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADB_VALID(adb));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "killing name %p", name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * If we're dead already, just check to see if we should go
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * away now or not.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_DEAD(name) && !NAME_FETCH(name)) {
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley unlink_name(adb, name);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley free_adbname(adb, &name);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Clean up the name's various lists. These two are destructive
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * in that they will always empty the list.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley clean_namehooks(adb, &name->v4);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley clean_namehooks(adb, &name->v6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley clean_target(adb, &name->target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If fetches are running, cancel them. If none are running, we can
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * just kill the name here.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (!NAME_FETCH(name)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unlink_name(adb, name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbname(adb, &name);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->flags |= NAME_IS_DEAD;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley cancel_fetches_at_name(name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Requires the name's bucket be locked and no entry buckets be locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleycheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adb_t *adb;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(DNS_ADBNAME_VALID(name));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb = name->adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADB_VALID(adb));
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley * Check to see if we need to remove the v4 addresses
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (!NAME_FETCH_V4(name) && EXPIRE_OK(name->expire_v4, now)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_HAS_V4(name)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "expiring v4 for name %p", name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley clean_namehooks(adb, &name->v4);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result &= ~DNS_ADBFIND_INET;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v4 = INT_MAX;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Check to see if we need to remove the v6 addresses
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (!NAME_FETCH_V6(name) && EXPIRE_OK(name->expire_v6, now)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_HAS_V6(name)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "expiring v6 for name %p", name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley clean_namehooks(adb, &name->v6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result &= ~DNS_ADBFIND_INET6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v6 = INT_MAX;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley }
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Check to see if we need to remove the alias target.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley if (EXPIRE_OK(name->expire_target, now)) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley clean_target(adb, &name->target);
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews name->expire_target = INT_MAX;
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Requires the name's bucket be locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleylink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_PREPEND(adb->names[bucket], name, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->lock_bucket = bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley adb->name_refcnt[bucket]++;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Requires the name's bucket be locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int bucket;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley bucket = name->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(bucket != DNS_ADB_INVALIDBUCKET);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(adb->names[bucket], name, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->lock_bucket = DNS_ADB_INVALIDBUCKET;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley INSIST(adb->name_refcnt[bucket] > 0);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley adb->name_refcnt[bucket]--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Requires the entry's bucket be locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
577179503f2eb7695ec668d8eeb41889a150e28fBob Halleylink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->lock_bucket = bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->entry_refcnt[bucket]++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Requires the entry's bucket be locked.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleyunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley int bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(bucket != DNS_ADB_INVALIDBUCKET);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(adb->entry_refcnt[bucket] > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->entry_refcnt[bucket]--;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(have);
577179503f2eb7695ec668d8eeb41889a150e28fBob Halley LOCK(want);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(have);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * The ADB _MUST_ be locked before calling. Also, exit conditions must be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * checked after calling this function.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyshutdown_names(dns_adb_t *adb) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbname_t *name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *next_name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->namelocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->name_sd[bucket] = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name = ISC_LIST_HEAD(adb->names[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (name == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This bucket has no names. We must decrement the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * irefcnt ourselves, since it will not be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * automatically triggered by a name being unlinked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley * Run through the list. For each name, clean up finds
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * found there, and cancel any fetches running. When
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * all the fetches are canceled, the name will destroy
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * itself.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (name != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley next_name = ISC_LIST_NEXT(name, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley kill_name(&name, DNS_EVENT_ADBSHUTDOWN);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name = next_name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->namelocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * The ADB _MUST_ be locked before calling. Also, exit conditions must be
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * checked after calling this function.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyshutdown_entries(dns_adb_t *adb) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *next_entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley for (bucket = 0 ; bucket < NBUCKETS ; bucket++) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->entry_sd[bucket] = ISC_TRUE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley entry = ISC_LIST_HEAD(adb->entries[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry == NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * This bucket has no entries. We must decrement the
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * irefcnt ourselves, since it will not be
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * automatically triggered by an entry being unlinked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dec_adb_irefcnt(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Run through the list. Cleanup any entries not
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * associated with names, and which are not in use.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (entry != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley next_entry = ISC_LIST_NEXT(entry, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry->refcnt == 0 &&
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->expires != 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unlink_entry(adb, entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbentry(adb, &entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley entry = next_entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley UNLOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Name bucket must be locked
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycancel_fetches_at_name(dns_adbname_t *name) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch6_t *fetch6;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_FETCH_A(name))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_resolver_cancelfetch(name->fetch_a->fetch);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (NAME_FETCH_AAAA(name))
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley fetch6 = ISC_LIST_HEAD(name->fetches_a6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (fetch6 != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_resolver_cancelfetch(fetch6->fetch);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley fetch6 = ISC_LIST_NEXT(fetch6, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes the name bucket is locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyclean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *entry;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbnamehook_t *namehook;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int addr_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addr_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namehook = ISC_LIST_HEAD(*namehooks);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (namehook != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Clean up the entry if needed.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry = namehook->entry;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (entry != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBENTRY_VALID(entry));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addr_bucket != entry->lock_bucket) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley addr_bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->entrylocks[addr_bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dec_entry_refcnt(adb, entry, ISC_FALSE);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Free the namehook
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namehook->entry = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(*namehooks, namehook, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbnamehook(adb, &namehook);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namehook = ISC_LIST_HEAD(*namehooks);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (addr_bucket != DNS_ADB_INVALIDBUCKET)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[addr_bucket]);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyclean_target(dns_adb_t *adb, dns_name_t *target) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dns_name_countlabels(target) > 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_free(target, adb->mctx);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_init(target, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic isc_result_t
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyset_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_rdataset_t *rdataset, dns_name_t *target)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_result_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_namereln_t namereln;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int nlabels, nbits;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int order;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_t rdata = DNS_RDATA_INIT;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_fixedname_t fixed1, fixed2;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_t *prefix, *new_target;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley REQUIRE(dns_name_countlabels(target) == 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (rdataset->type == dns_rdatatype_cname) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_cname_t cname;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Copy the CNAME's target into the target name.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_rdataset_first(rdataset);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_current(rdataset, &rdata);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley result = dns_rdata_tostruct(&rdata, &cname, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_dup(&cname.cname, adb->mctx, target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_freestruct(&cname);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley } else {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_dname_t dname;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(rdataset->type == dns_rdatatype_dname);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley namereln = dns_name_fullcompare(name, fname, &order,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley &nlabels, &nbits);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(namereln == dns_namereln_subdomain);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Get the target name of the DNAME.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_rdataset_first(rdataset);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_current(rdataset, &rdata);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_rdata_tostruct(&rdata, &dname, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Construct the new target name.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_fixedname_init(&fixed1);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley prefix = dns_fixedname_name(&fixed1);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_fixedname_init(&fixed2);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley new_target = dns_fixedname_name(&fixed2);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_split(name, nlabels, nbits, prefix, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_freestruct(&dname);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_concatenate(prefix, &dname.dname, new_target,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdata_freestruct(&dname);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (result != ISC_R_SUCCESS)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = dns_name_dup(new_target, adb->mctx, target);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (result);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (ISC_R_SUCCESS);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes nothing is locked, since this is called by the client.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halleystatic void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyevent_free(isc_event_t *event) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(event != NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find = event->ev_destroy_arg;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(DNS_ADBFIND_VALID(find));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley LOCK(&find->lock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find->flags |= FIND_EVENT_FREED;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event->ev_destroy_arg = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&find->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Assumes the name bucket is locked.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyclean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unsigned int addrs)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley{
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_event_t *ev;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_t *task;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *next_find;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t process;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int wanted, notify;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(ENTER_LEVEL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name, evtype, addrs);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find = ISC_LIST_HEAD(name->finds);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley while (find != NULL) {
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley LOCK(&find->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley next_find = ISC_LIST_NEXT(find, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley notify = wanted & addrs;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley switch (evtype) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley case DNS_EVENT_ADBMOREADDRESSES:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(3, "DNS_EVENT_ADBMOREADDRESSES");
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if ((notify) != 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->flags &= ~addrs;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley break;
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews case DNS_EVENT_ADBNOMOREADDRESSES:
cee7525336d4710a64368875d92eb439d4d3efb1Mark Andrews DP(3, "DNS_EVENT_ADBNOMOREADDRESSES");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley find->flags &= ~addrs;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (wanted == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley break;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley default:
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find->flags &= ~addrs;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley process = ISC_TRUE;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (process) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley DP(DEF_LEVEL, "cfan: processing find %p", find);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Unlink the find from the name, letting the caller
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * call dns_adb_destroyfind() on it to clean it up
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * later.
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley */
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_LIST_UNLINK(name->finds, find, plink);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find->adbname = NULL;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find->name_bucket = DNS_ADB_INVALIDBUCKET;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley INSIST(!FIND_EVENTSENT(find));
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev = &find->event;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley task = ev->ev_sender;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev->ev_sender = find;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find->result_v4 = find_err_map[name->fetch_err];
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find->result_v6 = find_err_map[name->fetch6_err];
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev->ev_type = evtype;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev->ev_destroy = event_free;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev->ev_destroy_arg = find;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley DP(DEF_LEVEL,
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley "Sending event %p to task %p for find %p",
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ev, task, find);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_task_sendanddetach(&task, (isc_event_t **)&ev);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley } else {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley DP(DEF_LEVEL, "cfan: skipping find %p", find);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley UNLOCK(&find->lock);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley find = next_find;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley}
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleycheck_exit(dns_adb_t *adb) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_event_t *event;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley isc_task_t *etask;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t zeroirefcnt;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * The caller must be holding the adb lock.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->ilock);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley if (adb->irefcnt == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zeroirefcnt = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley else
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley zeroirefcnt = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->ilock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (adb->shutting_down && zeroirefcnt &&
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mempool_getallocated(adb->ahmp) == 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * We're now shutdown. Send any whenshutdown events.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event = ISC_LIST_HEAD(adb->whenshutdown);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley while (event != NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley etask = event->ev_sender;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event->ev_sender = adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_sendanddetach(&etask, &event);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event = ISC_LIST_HEAD(adb->whenshutdown);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * If there aren't any external references either, we're
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * done. Send the control event to initiate shutdown.
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (adb->erefcnt == 0) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!adb->cevent_sent); /* Sanity check. */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley event = &adb->cevent;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_task_send(adb->task, &event);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->cevent_sent = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydec_adb_irefcnt(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->ilock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(adb->irefcnt > 0);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley adb->irefcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->ilock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halleyinc_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->erefcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydec_adb_erefcnt(dns_adb_t *adb, isc_boolean_t lock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->lock);
38d2d0e9326a2f70b5893302b89a26978b539405Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(adb->erefcnt > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley adb->erefcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (adb->erefcnt == 0)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley check_exit(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyinc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt++;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleydec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley int bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_boolean_t destroy_entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley bucket = entry->lock_bucket;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley LOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(entry->refcnt > 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->refcnt--;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley destroy_entry = ISC_FALSE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (entry->refcnt == 0 &&
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley (adb->entry_sd[bucket] || entry->expires == 0)) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley destroy_entry = ISC_TRUE;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley unlink_entry(adb, entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (lock)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNLOCK(&adb->entrylocks[bucket]);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (!destroy_entry)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbentry(adb, &entry);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbname_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbname_t *name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name = isc_mempool_get(adb->nmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (name == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_init(&name->name, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->nmp, name);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_init(&name->target, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->magic = DNS_ADBNAME_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->adb = adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->partial_result = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->flags = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v4 = INT_MAX;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_v6 = INT_MAX;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->expire_target = INT_MAX;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->chains = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->lock_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(name->v4);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(name->v6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->fetch_a = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->fetch_aaaa = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(name->fetches_a6);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley name->fetch_err = FIND_ERR_UNEXPECTED;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->fetch6_err = FIND_ERR_UNEXPECTED;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_INIT(name->finds);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LINK_INIT(name, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (name);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbname(dns_adb_t *adb, dns_adbname_t **name) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbname_t *n;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley n = *name;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *name = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!NAME_HAS_V4(n));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!NAME_HAS_V6(n));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!NAME_FETCH(n));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(ISC_LIST_EMPTY(n->finds));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(n, plink));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(n->adb == adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley n->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_name_free(&n->name, adb->mctx);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->nmp, n);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbnamehook_t *
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleynew_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley dns_adbnamehook_t *nh;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh = isc_mempool_get(adb->nhmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (nh == NULL)
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->magic = DNS_ADBNAMEHOOK_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->entry = entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(nh, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (nh);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbnamehook_t *nh;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews nh = *namehook;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley *namehook = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(nh->entry == NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(nh, plink));
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley nh->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->nhmp, nh);
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley}
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halleystatic inline dns_adbzoneinfo_t *
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halleynew_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley dns_adbzoneinfo_t *zi;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = isc_mempool_get(adb->zimp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (zi == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_init(&zi->zone, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->zimp, zi);
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews return (NULL);
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews }
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley zi->magic = DNS_ADBZONEINFO_MAGIC;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley zi->lame_timer = 0;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_LINK_INIT(zi, plink);
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley return (zi);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
b5b97de45a561784bd88fb8fa7e1464a28ad9a44Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbzoneinfo_t *zi;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = *zoneinfo;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *zoneinfo = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(zi, plink));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_name_free(&zi->zone, adb->mctx);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->zimp, zi);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbentry_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbentry(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *e;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_uint32_t r;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley e = isc_mempool_get(adb->emp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (e == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley e->magic = DNS_ADBENTRY_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->lock_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->refcnt = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->flags = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_random_get(&r);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->srtt = (r & 0x1f) + 1;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->expires = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(e->zoneinfo);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(e, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (e);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline void
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleyfree_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbentry_t *e;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbzoneinfo_t *zi;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e = *entry;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley *entry = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(e->refcnt == 0);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley INSIST(!ISC_LINK_LINKED(e, plink));
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley e->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = ISC_LIST_HEAD(e->zoneinfo);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley while (zi != NULL) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbzoneinfo(adb, &zi);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley zi = ISC_LIST_HEAD(e->zoneinfo);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->emp, e);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfind_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbfind(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfind_t *h;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_result_t result;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h = isc_mempool_get(adb->ahmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (h == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * Public members.
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->adb = adb;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->partial_result = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->options = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->flags = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->result_v4 = ISC_R_UNEXPECTED;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->result_v6 = ISC_R_UNEXPECTED;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(h, publink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LINK_INIT(h, plink);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_LIST_INIT(h->list);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->adbname = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->name_bucket = DNS_ADB_INVALIDBUCKET;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley /*
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley * private members
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley */
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley result = isc_mutex_init(&h->lock);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (result != ISC_R_SUCCESS) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley UNEXPECTED_ERROR(__FILE__, __LINE__,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley "isc_mutex_init failed in new_adbfind()");
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley isc_mempool_put(adb->ahmp, h);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley }
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley ISC_EVENT_INIT(&h->event, sizeof (isc_event_t), 0, 0, 0, NULL, NULL,
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley NULL, NULL, h);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley h->magic = DNS_ADBFIND_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (h);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley}
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleystatic inline dns_adbfetch_t *
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halleynew_adbfetch(dns_adb_t *adb) {
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_adbfetch_t *f;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f = isc_mempool_get(adb->afmp);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->magic = 0;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->namehook = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->entry = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->fetch = NULL;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->namehook = new_adbnamehook(adb, NULL);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->namehook == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->entry = new_adbentry(adb);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->entry == NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley goto err;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley dns_rdataset_init(&f->rdataset);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley f->magic = DNS_ADBFETCH_MAGIC;
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (f);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley err:
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->namehook != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbnamehook(adb, &f->namehook);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley if (f->entry != NULL)
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley free_adbentry(adb, &f->entry);
566f721b3a0a4e63e7da775a0fbcc2c44645cae4Mark Andrews isc_mempool_put(adb->afmp, f);
c50fd34a4e0e6978f8ca5f6f3ad8545549c3cfeeBob Halley return (NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyfree_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbfetch_t *f;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley f = *fetch;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *fetch = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley if (f->namehook != NULL)
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley free_adbnamehook(adb, &f->namehook);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (f->entry != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_adbentry(adb, &f->entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (dns_rdataset_isassociated(&f->rdataset))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_rdataset_disassociate(&f->rdataset);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley isc_mempool_put(adb->afmp, f);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley/*
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley * Caller must be holding the name lock.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleystatic isc_result_t
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halleya6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type, isc_stdtime_t now,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley{
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adb_t *adb;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name = arg;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(DNS_ADBNAME_VALID(name));
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley adb = name->adb;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(DNS_ADB_VALID(adb));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (dns_view_simplefind(adb->view, a6name, type, now,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DNS_DBFIND_GLUEOK, ISC_FALSE,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley rdataset, sigrdataset));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Caller must be holding the name lock.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleya6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *name;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adb_t *adb;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbfetch6_t *fetch;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_result_t result;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name = a6ctx->arg;
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley INSIST(DNS_ADBNAME_VALID(name));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley adb = name->adb;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(DNS_ADB_VALID(adb));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley fetch = new_adbfetch6(adb, name, a6ctx);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (fetch == NULL) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->partial_result |= DNS_ADBFIND_INET6;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley result = dns_resolver_createfetch(adb->view->resolver, a6name,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_rdatatype_a6,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley NULL, NULL, NULL, 0,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley adb->task, fetch_callback_a6,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name, &fetch->rdataset, NULL,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley &fetch->fetch);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (result != ISC_R_SUCCESS)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto cleanup;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->chains = a6ctx->chains;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley cleanup:
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (result != ISC_R_SUCCESS) {
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley free_adbfetch6(adb, &fetch);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley name->partial_result |= DNS_ADBFIND_INET6;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley }
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline dns_adbfetch6_t *
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleynew_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx) {
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley dns_adbfetch6_t *f;
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley f = isc_mempool_get(adb->af6mp);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley if (f == NULL)
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley return (NULL);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->namehook = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->entry = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->fetch = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->flags = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->namehook = new_adbnamehook(adb, NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (f->namehook == NULL)
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley goto err;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->entry = new_adbentry(adb);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (f->entry == NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley goto err;
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley dns_rdataset_init(&f->rdataset);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley a6missing, name);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley if (a6ctx != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_a6_copy(a6ctx, &f->a6ctx);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ISC_LINK_INIT(f, plink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->magic = DNS_ADBFETCH6_MAGIC;
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley return (f);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley err:
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley if (f->namehook != NULL)
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley free_adbnamehook(adb, &f->namehook);
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley if (f->entry != NULL)
9192e92f7d0f4e78385a1d5f9b6607cc5bf0e42aBob Halley free_adbentry(adb, &f->entry);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mempool_put(adb->af6mp, f);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyfree_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbfetch6_t *f;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f = *fetch;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *fetch = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley f->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (f->namehook != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_adbnamehook(adb, &f->namehook);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (f->entry != NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley free_adbentry(adb, &f->entry);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (dns_rdataset_isassociated(&f->rdataset))
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_rdataset_disassociate(&f->rdataset);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mempool_put(adb->af6mp, f);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyfree_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbfind_t *find;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find = *findp;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *findp = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(!FIND_HAS_ADDRS(find));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(!ISC_LINK_LINKED(find, publink));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(!ISC_LINK_LINKED(find, plink));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(find->adbname == NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley find->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley DESTROYLOCK(&find->lock);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mempool_put(adb->ahmp, find);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Copy bits from the entry into the newly allocated addrinfo. The entry
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * must be locked, and the reference count must be bumped up by one
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * if this function returns a valid pointer.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline dns_adbaddrinfo_t *
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleynew_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbaddrinfo_t *ai;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ai = isc_mempool_get(adb->aimp);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (ai == NULL)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (NULL);
8db3b065b4659f593f7b8eaa7c9ca0c3daa4da02Bob Halley
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ai->magic = DNS_ADBADDRINFO_MAGIC;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ai->sockaddr = entry->sockaddr;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_sockaddr_setport(&ai->sockaddr, port);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ai->srtt = entry->srtt;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ai->flags = entry->flags;
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley ai->entry = entry;
c5839c39bd07c9dd3d4cd598035deb0537098475Bob Halley ISC_LINK_INIT(ai, publink);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley return (ai);
86131d8d7aaf1bb8b8bfc7819985d05ea369b708Bob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline void
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyfree_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbaddrinfo_t *ai;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ai = *ainfo;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *ainfo = NULL;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(ai->entry == NULL);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley INSIST(!ISC_LINK_LINKED(ai, publink));
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley ai->magic = 0;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley isc_mempool_put(adb->aimp, ai);
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley}
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley/*
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * Search for the name. NOTE: The bucket is kept locked on both
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * success and failure, so it must always be unlocked by the caller!
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley *
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * On the first call to this function, *bucketp must be set to
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley * DNS_ADB_INVALIDBUCKET.
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley */
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleystatic inline dns_adbname_t *
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halleyfind_name_and_lock(dns_adb_t *adb, dns_name_t *name,
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley unsigned int options, int *bucketp)
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley{
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley dns_adbname_t *adbname;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley int bucket;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley
95c86af1e92dae4ff837a39e7e2dcb7308dd9cceBob Halley if (*bucketp == DNS_ADB_INVALIDBUCKET) {
LOCK(&adb->namelocks[bucket]);
*bucketp = bucket;
} else if (*bucketp != bucket) {
UNLOCK(&adb->namelocks[*bucketp]);
LOCK(&adb->namelocks[bucket]);
*bucketp = bucket;
}
adbname = ISC_LIST_HEAD(adb->names[bucket]);
while (adbname != NULL) {
if (!NAME_DEAD(adbname)) {
if (dns_name_equal(name, &adbname->name)
&& GLUEHINT_OK(adbname, options))
return (adbname);
}
adbname = ISC_LIST_NEXT(adbname, plink);
}
return (NULL);
}
/*
* Search for the address. NOTE: The bucket is kept locked on both
* success and failure, so it must always be unlocked by the caller.
*
* On the first call to this function, *bucketp must be set to
* DNS_ADB_INVALIDBUCKET. This will cause a lock to occur. On
* later calls (within the same "lock path") it can be left alone, so
* if this function is called multiple times locking is only done if
* the bucket changes.
*/
static inline dns_adbentry_t *
find_entry_and_lock(dns_adb_t *adb, isc_sockaddr_t *addr, int *bucketp) {
dns_adbentry_t *entry;
int bucket;
bucket = isc_sockaddr_hash(addr, ISC_TRUE) % NBUCKETS;
if (*bucketp == DNS_ADB_INVALIDBUCKET) {
LOCK(&adb->entrylocks[bucket]);
*bucketp = bucket;
} else if (*bucketp != bucket) {
UNLOCK(&adb->entrylocks[*bucketp]);
LOCK(&adb->entrylocks[bucket]);
*bucketp = bucket;
}
entry = ISC_LIST_HEAD(adb->entries[bucket]);
while (entry != NULL) {
if (isc_sockaddr_equal(addr, &entry->sockaddr))
return (entry);
entry = ISC_LIST_NEXT(entry, plink);
}
return (NULL);
}
/*
* Entry bucket MUST be locked!
*/
static isc_boolean_t
entry_is_bad_for_zone(dns_adb_t *adb, dns_adbentry_t *entry, dns_name_t *zone,
isc_stdtime_t now)
{
dns_adbzoneinfo_t *zi, *next_zi;
isc_boolean_t is_bad;
is_bad = ISC_FALSE;
zi = ISC_LIST_HEAD(entry->zoneinfo);
if (zi == NULL)
return (ISC_FALSE);
while (zi != NULL) {
next_zi = ISC_LIST_NEXT(zi, plink);
/*
* Has the entry expired?
*/
if (zi->lame_timer < now) {
ISC_LIST_UNLINK(entry->zoneinfo, zi, plink);
free_adbzoneinfo(adb, &zi);
}
/*
* Order tests from least to most expensive.
*/
if (zi != NULL && !is_bad) {
if (dns_name_equal(zone, &zi->zone))
is_bad = ISC_TRUE;
}
zi = next_zi;
}
return (is_bad);
}
static void
copy_namehook_lists(dns_adb_t *adb, dns_adbfind_t *find, dns_name_t *zone,
dns_adbname_t *name, isc_stdtime_t now)
{
dns_adbnamehook_t *namehook;
dns_adbaddrinfo_t *addrinfo;
dns_adbentry_t *entry;
int bucket;
bucket = DNS_ADB_INVALIDBUCKET;
if (find->options & DNS_ADBFIND_INET) {
namehook = ISC_LIST_HEAD(name->v4);
while (namehook != NULL) {
entry = namehook->entry;
bucket = entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
if (!FIND_RETURNLAME(find)
&& entry_is_bad_for_zone(adb, entry, zone, now)) {
find->options |= DNS_ADBFIND_LAMEPRUNED;
goto nextv4;
}
addrinfo = new_adbaddrinfo(adb, entry, find->port);
if (addrinfo == NULL) {
find->partial_result |= DNS_ADBFIND_INET;
goto out;
}
/*
* Found a valid entry. Add it to the find's list.
*/
inc_entry_refcnt(adb, entry, ISC_FALSE);
ISC_LIST_APPEND(find->list, addrinfo, publink);
addrinfo = NULL;
nextv4:
UNLOCK(&adb->entrylocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_NEXT(namehook, plink);
}
}
if (find->options & DNS_ADBFIND_INET6) {
namehook = ISC_LIST_HEAD(name->v6);
while (namehook != NULL) {
entry = namehook->entry;
bucket = entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
if (entry_is_bad_for_zone(adb, entry, zone, now))
goto nextv6;
addrinfo = new_adbaddrinfo(adb, entry, find->port);
if (addrinfo == NULL) {
find->partial_result |= DNS_ADBFIND_INET6;
goto out;
}
/*
* Found a valid entry. Add it to the find's list.
*/
inc_entry_refcnt(adb, entry, ISC_FALSE);
ISC_LIST_APPEND(find->list, addrinfo, publink);
addrinfo = NULL;
nextv6:
UNLOCK(&adb->entrylocks[bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_NEXT(namehook, plink);
}
}
out:
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[bucket]);
}
static void
shutdown_task(isc_task_t *task, isc_event_t *ev) {
dns_adb_t *adb;
UNUSED(task);
adb = ev->ev_arg;
INSIST(DNS_ADB_VALID(adb));
/*
* Kill the timer, and then the ADB itself. Note that this implies
* that this task was the one scheduled to get timer events. If
* this is not true (and it is unfortunate there is no way to INSIST()
* this) badness will occur.
*/
LOCK(&adb->lock);
isc_timer_detach(&adb->timer);
UNLOCK(&adb->lock);
isc_event_free(&ev);
destroy(adb);
}
/*
* name bucket must be locked; adb may be locked; no other locks held.
*/
static void
check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
dns_adbname_t *name;
INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
name = *namep;
if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
return;
if (NAME_FETCH(name))
return;
if (!EXPIRE_OK(name->expire_v4, now))
return;
if (!EXPIRE_OK(name->expire_v6, now))
return;
if (!EXPIRE_OK(name->expire_target, now))
return;
/*
* The name is empty. Delete it.
*/
kill_name(&name, DNS_EVENT_ADBEXPIRED);
*namep = NULL;
/*
* Our caller, or one of its callers, will be calling check_exit() at
* some point, so we don't need to do it here.
*/
}
/*
* entry bucket must be locked; adb may be locked; no other locks held.
*/
static void
check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
{
dns_adbentry_t *entry;
INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
entry = *entryp;
if (entry->refcnt != 0)
return;
if (entry->expires == 0 || entry->expires > now)
return;
/*
* The entry is not in use. Delete it.
*/
DP(DEF_LEVEL, "killing entry %p", entry);
INSIST(ISC_LINK_LINKED(entry, plink));
unlink_entry(adb, entry);
free_adbentry(adb, &entry);
*entryp = NULL;
}
/*
* 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(CLEAN_LEVEL, "cleaning name 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, now);
name = next_name;
}
UNLOCK(&adb->namelocks[bucket]);
}
/*
* ADB must be locked, and no other locks held.
*/
static void
cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
dns_adbentry_t *entry, *next_entry;
DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
LOCK(&adb->entrylocks[bucket]);
entry = ISC_LIST_HEAD(adb->entries[bucket]);
while (entry != NULL) {
next_entry = ISC_LIST_NEXT(entry, plink);
check_expire_entry(adb, &entry, now);
entry = next_entry;
}
UNLOCK(&adb->entrylocks[bucket]);
}
static void
timer_cleanup(isc_task_t *task, isc_event_t *ev) {
dns_adb_t *adb;
isc_stdtime_t now;
unsigned int i;
UNUSED(task);
adb = ev->ev_arg;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
isc_stdtime_get(&now);
for (i = 0 ; i < CLEAN_BUCKETS ; i++) {
/*
* Call our cleanup routines.
*/
cleanup_names(adb, adb->next_cleanbucket, now);
cleanup_entries(adb, adb->next_cleanbucket, now);
/*
* Set the next bucket to be cleaned.
*/
adb->next_cleanbucket++;
if (adb->next_cleanbucket >= NBUCKETS) {
adb->next_cleanbucket = 0;
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE);
#endif
}
}
/*
* Reset the timer.
* XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
* ISC_R_NOMEMORY, but it isn't clear what could be done here
* if either one of those things happened.
*/
(void)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, NBUCKETS);
isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
DESTROYLOCK(&adb->ilock);
DESTROYLOCK(&adb->lock);
DESTROYLOCK(&adb->mplock);
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_cleanbucket = 0;
ISC_EVENT_INIT(&adb->cevent, sizeof adb->cevent, 0, NULL,
DNS_EVENT_ADBCONTROL, shutdown_task, adb,
adb, NULL, NULL);
adb->cevent_sent = ISC_FALSE;
adb->shutting_down = ISC_FALSE;
ISC_LIST_INIT(adb->whenshutdown);
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail0b;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail0c;
result = isc_mutex_init(&adb->ilock);
if (result != ISC_R_SUCCESS)
goto fail0d;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0 ; i < NBUCKETS ; i++) {
ISC_LIST_INIT(adb->names[i]);
adb->name_sd[i] = ISC_FALSE;
adb->name_refcnt[i] = 0;
adb->irefcnt++;
}
for (i = 0 ; i < NBUCKETS ; i++) {
ISC_LIST_INIT(adb->entries[i]);
adb->entry_sd[i] = ISC_FALSE;
adb->entry_refcnt[i] = 0;
adb->irefcnt++;
}
result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, n) do { \
result = isc_mempool_create(mem, sizeof (t), &(p)); \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
isc_mempool_setname((p), n); \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT(dns_adbname_t, adb->nmp, "adbname");
MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
MPINIT(dns_adbzoneinfo_t, adb->zimp, "adbzoneinfo");
MPINIT(dns_adbentry_t, adb->emp, "adbentry");
MPINIT(dns_adbfind_t, adb->ahmp, "adbfind");
MPINIT(dns_adbaddrinfo_t, adb->aimp, "adbaddrinfo");
MPINIT(dns_adbfetch_t, adb->afmp, "adbfetch");
MPINIT(dns_adbfetch6_t, adb->af6mp, "adbfetch6");
#undef MPINIT
/*
* Allocate a timer and a task for our periodic cleanup.
*/
result = isc_task_create(adb->taskmgr, 0, &adb->task);
if (result != ISC_R_SUCCESS)
goto fail3;
isc_task_setname(adb->task, "ADB", adb);
/*
* XXXMLG When this is changed to be a config file option,
*/
isc_interval_set(&adb->tick_interval, CLEAN_SECONDS, 0);
result = isc_timer_create(adb->timermgr, isc_timertype_once,
NULL, &adb->tick_interval, adb->task,
timer_cleanup, adb, &adb->timer);
if (result != ISC_R_SUCCESS)
goto fail3;
DP(5,
"Cleaning interval for adb: "
"%u buckets every %u seconds, %u buckets in system, %u cl.interval",
CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
/*
* Normal return.
*/
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3:
if (adb->task != NULL)
isc_task_detach(&adb->task);
if (adb->timer != NULL)
isc_timer_detach(&adb->timer);
/* clean up entrylocks */
isc_mutexblock_destroy(adb->entrylocks, NBUCKETS);
fail2: /* clean up namelocks */
isc_mutexblock_destroy(adb->namelocks, NBUCKETS);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->zimp != NULL)
isc_mempool_destroy(&adb->zimp);
if (adb->emp != NULL)
isc_mempool_destroy(&adb->emp);
if (adb->ahmp != NULL)
isc_mempool_destroy(&adb->ahmp);
if (adb->aimp != NULL)
isc_mempool_destroy(&adb->aimp);
if (adb->afmp != NULL)
isc_mempool_destroy(&adb->afmp);
if (adb->af6mp != NULL)
isc_mempool_destroy(&adb->af6mp);
DESTROYLOCK(&adb->ilock);
fail0d:
DESTROYLOCK(&adb->mplock);
fail0c:
DESTROYLOCK(&adb->lock);
fail0b:
isc_mem_put(mem, adb, sizeof (dns_adb_t));
return (result);
}
void
dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(adbx != NULL && *adbx == NULL);
inc_adb_erefcnt(adb, ISC_TRUE);
*adbx = adb;
}
void
dns_adb_detach(dns_adb_t **adbx) {
dns_adb_t *adb;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
LOCK(&adb->lock);
dec_adb_erefcnt(adb, ISC_FALSE);
if (adb->erefcnt == 0)
INSIST(adb->shutting_down);
UNLOCK(&adb->lock);
}
void
dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
isc_task_t *clone;
isc_event_t *event;
isc_boolean_t zeroirefcnt = ISC_FALSE;
/*
* Send '*eventp' to 'task' when 'adb' has shutdown.
*/
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(eventp != NULL);
event = *eventp;
*eventp = NULL;
LOCK(&adb->lock);
LOCK(&adb->ilock);
if (adb->irefcnt == 0)
zeroirefcnt = ISC_TRUE;
else
zeroirefcnt = ISC_FALSE;
UNLOCK(&adb->ilock);
if (adb->shutting_down && zeroirefcnt &&
isc_mempool_getallocated(adb->ahmp) == 0) {
/*
* We're already shutdown. Send the event.
*/
event->ev_sender = adb;
isc_task_send(task, &event);
} else {
clone = NULL;
isc_task_attach(task, &clone);
event->ev_sender = clone;
ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
}
UNLOCK(&adb->lock);
}
void
dns_adb_shutdown(dns_adb_t *adb) {
/*
* Shutdown 'adb'.
*/
LOCK(&adb->lock);
if (!adb->shutting_down) {
adb->shutting_down = ISC_TRUE;
shutdown_names(adb);
shutdown_entries(adb);
check_exit(adb);
}
UNLOCK(&adb->lock);
}
isc_result_t
dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
void *arg, dns_name_t *name, dns_name_t *zone,
unsigned int options, isc_stdtime_t now, dns_name_t *target,
in_port_t port, dns_adbfind_t **findp)
{
dns_adbfind_t *find;
dns_adbname_t *adbname;
int bucket;
isc_boolean_t want_event, start_at_root, alias, have_address;
isc_result_t result;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
REQUIRE(DNS_ADB_VALID(adb));
if (task != NULL) {
REQUIRE(action != NULL);
}
REQUIRE(name != NULL);
REQUIRE(zone != NULL);
REQUIRE(findp != NULL && *findp == NULL);
REQUIRE(target == NULL || dns_name_hasbuffer(target));
REQUIRE((options & DNS_ADBFIND_ADDRESSMASK) != 0);
result = ISC_R_UNEXPECTED;
wanted_addresses = (options & DNS_ADBFIND_ADDRESSMASK);
wanted_fetches = 0;
query_pending = 0;
want_event = ISC_FALSE;
start_at_root = ISC_FALSE;
alias = ISC_FALSE;
if (now == 0)
isc_stdtime_get(&now);
/*
* XXXMLG Move this comment somewhere else!
*
* Look up the name in our internal database.
*
* Possibilities: Note that these are not always exclusive.
*
* No name found. In this case, allocate a new name header and
* an initial namehook or two. If any of these allocations
* fail, clean up and return ISC_R_NOMEMORY.
*
* Name found, valid addresses present. Allocate one addrinfo
* structure for each found and append it to the linked list
* of addresses for this header.
*
* Name found, queries pending. In this case, if a task was
* passed in, allocate a job id, attach it to the name's job
* list and remember to tell the caller that there will be
* more info coming later.
*/
find = new_adbfind(adb);
if (find == NULL)
return (ISC_R_NOMEMORY);
find->port = port;
/*
* Remember what types of addresses we are interested in.
*/
find->options = options;
find->flags |= wanted_addresses;
if (FIND_WANTEVENT(find)) {
REQUIRE(task != NULL);
}
/*
* Try to see if we know anything about this name at all.
*/
bucket = DNS_ADB_INVALIDBUCKET;
adbname = find_name_and_lock(adb, name, find->options, &bucket);
if (adb->name_sd[bucket]) {
DP(DEF_LEVEL,
"dns_adb_createfind: returning ISC_R_SHUTTINGDOWN");
free_adbfind(adb, &find);
result = ISC_R_SHUTTINGDOWN;
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name.
*/
if (adbname == NULL) {
adbname = new_adbname(adb, name);
if (adbname == NULL) {
free_adbfind(adb, &find);
result = ISC_R_NOMEMORY;
goto out;
}
link_name(adb, bucket, adbname);
if (FIND_HINTOK(find))
adbname->flags |= NAME_HINT_OK;
if (FIND_GLUEOK(find))
adbname->flags |= NAME_GLUE_OK;
}
/*
* Expire old entries, etc.
*/
check_expire_namehooks(adbname, now);
/*
* Do we know that the name is an alias?
*/
if (!EXPIRE_OK(adbname->expire_target, now)) {
/*
* Yes, it is.
*/
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias (cached)",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Try to populate the name from the database and/or
* start fetches. First try looking for an A record
* in the database.
*/
if (!NAME_HAS_V4(adbname) && !NAME_FETCH_V4(adbname)
&& EXPIRE_OK(adbname->expire_v4, now)
&& WANT_INET(wanted_addresses)) {
result = dbfind_name(adbname, now, dns_rdatatype_a);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: found A for name %p in db",
adbname);
goto v6;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* If the name doesn't exist at all, don't bother with
* v6 queries; they won't work.
*
* If the name does exist but we didn't get our data, go
* ahead and try a6.
*
* If the result is neither of these, try a fetch for A.
*/
if (NXDOMAIN_RESULT(result))
goto fetch;
else if (NXRRSET_RESULT(result))
goto v6;
wanted_fetches |= DNS_ADBFIND_INET;
}
v6:
if (!NAME_HAS_V6(adbname) && !NAME_FETCH_V6(adbname)
&& EXPIRE_OK(adbname->expire_v6, now)
&& WANT_INET6(wanted_addresses)) {
result = dbfind_a6(adbname, now);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: found A6 for name %p",
adbname);
goto fetch;
}
/*
* Did we get a CNAME or DNAME?
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* If the name doesn't exist at all, jump to the fetch
* code. Otherwise, we'll try AAAA.
*/
if (NXDOMAIN_RESULT(result))
goto fetch;
result = dbfind_name(adbname, now, dns_rdatatype_aaaa);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: found AAAA for name %p",
adbname);
goto fetch;
}
/*
* Did we get a CNAME or DNAME? This should have hit
* during the A6 query, but we'll reproduce it here Just
* In Case.
*/
if (result == DNS_R_ALIAS) {
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Listen to negative cache hints, and don't start
* another query.
*/
if (NCACHE_RESULT(result) || AUTH_NX(result))
goto fetch;
wanted_fetches |= DNS_ADBFIND_INET6;
}
fetch:
if ((WANT_INET(wanted_addresses) && NAME_HAS_V4(adbname)) ||
(WANT_INET6(wanted_addresses) && NAME_HAS_V6(adbname)))
have_address = ISC_TRUE;
else
have_address = ISC_FALSE;
if (wanted_fetches != 0 &&
! (FIND_AVOIDFETCHES(find) && have_address)) {
/*
* We're missing at least one address family. Either the
* caller hasn't instructed us to avoid fetches, or we don't
* know anything about any of the address families that would
* be acceptable so we have to launch fetches.
*/
if (FIND_STARTATROOT(find))
start_at_root = ISC_TRUE;
/*
* Start V4.
*/
if (WANT_INET(wanted_fetches) &&
fetch_name_v4(adbname, start_at_root) == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: started A fetch for name %p",
adbname);
}
/*
* Start V6.
*/
if (WANT_INET6(wanted_fetches) &&
fetch_name_a6(adbname, start_at_root) == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"dns_adb_createfind: started A6 fetch for name %p",
adbname);
}
}
/*
* Run through the name and copy out the bits we are
* interested in.
*/
copy_namehook_lists(adb, find, zone, adbname, now);
post_copy:
if (NAME_FETCH_V4(adbname))
query_pending |= DNS_ADBFIND_INET;
if (NAME_FETCH_V6(adbname))
query_pending |= DNS_ADBFIND_INET6;
/*
* Attach to the name's query list if there are queries
* already running, and we have been asked to.
*/
want_event = ISC_TRUE;
if (!FIND_WANTEVENT(find))
want_event = ISC_FALSE;
if (FIND_WANTEMPTYEVENT(find) && FIND_HAS_ADDRS(find))
want_event = ISC_FALSE;
if ((wanted_addresses & query_pending) == 0)
want_event = ISC_FALSE;
if (alias)
want_event = ISC_FALSE;
if (want_event) {
find->adbname = adbname;
find->name_bucket = bucket;
ISC_LIST_APPEND(adbname->finds, find, plink);
find->query_pending = (query_pending & wanted_addresses);
find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
find->flags |= (find->query_pending & DNS_ADBFIND_ADDRESSMASK);
DP(DEF_LEVEL, "createfind: attaching find %p to adbname %p",
find, adbname);
} else {
/*
* Remove the flag so the caller knows there will never
* be an event, and set internal flags to fake that
* the event was sent and freed, so dns_adb_destroyfind() will
* do the right thing.
*/
find->query_pending = (query_pending & wanted_addresses);
find->options &= ~DNS_ADBFIND_WANTEVENT;
find->flags |= (FIND_EVENT_SENT | FIND_EVENT_FREED);
find->flags &= ~DNS_ADBFIND_ADDRESSMASK;
}
find->partial_result |= (adbname->partial_result & wanted_addresses);
if (alias) {
if (target != NULL) {
result = dns_name_copy(&adbname->target, target, NULL);
if (result != ISC_R_SUCCESS)
goto out;
}
result = DNS_R_ALIAS;
} else
result = ISC_R_SUCCESS;
/*
* Copy out error flags from the name structure into the find.
*/
find->result_v4 = find_err_map[adbname->fetch_err];
find->result_v6 = find_err_map[adbname->fetch6_err];
out:
if (find != NULL) {
*findp = find;
if (want_event) {
isc_task_t *taskp;
INSIST((find->flags & DNS_ADBFIND_ADDRESSMASK) != 0);
taskp = NULL;
isc_task_attach(task, &taskp);
find->event.ev_sender = taskp;
find->event.ev_action = action;
find->event.ev_arg = arg;
}
}
if (bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
void
dns_adb_destroyfind(dns_adbfind_t **findp) {
dns_adbfind_t *find;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *ai;
int bucket;
dns_adb_t *adb;
REQUIRE(findp != NULL && DNS_ADBFIND_VALID(*findp));
find = *findp;
*findp = NULL;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_destroyfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(FIND_EVENTFREED(find));
bucket = find->name_bucket;
INSIST(bucket == DNS_ADB_INVALIDBUCKET);
UNLOCK(&find->lock);
/*
* The find doesn't exist on any list, and nothing is locked.
* Return the find to the memory pool, and decrement the adb's
* reference count.
*/
ai = ISC_LIST_HEAD(find->list);
while (ai != NULL) {
ISC_LIST_UNLINK(find->list, ai, publink);
entry = ai->entry;
ai->entry = NULL;
INSIST(DNS_ADBENTRY_VALID(entry));
dec_entry_refcnt(adb, entry, ISC_TRUE);
free_adbaddrinfo(adb, &ai);
ai = ISC_LIST_HEAD(find->list);
}
/*
* WARNING: The find is freed with the adb locked. This is done
* to avoid a race condition where we free the find, some other
* thread tests to see if it should be destroyed, detects it should
* be, destroys it, and then we try to lock it for our check, but the
* lock is destroyed.
*/
LOCK(&adb->lock);
free_adbfind(adb, &find);
check_exit(adb);
UNLOCK(&adb->lock);
}
void
dns_adb_cancelfind(dns_adbfind_t *find) {
isc_event_t *ev;
isc_task_t *task;
dns_adb_t *adb;
int bucket;
int unlock_bucket;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(!FIND_EVENTFREED(find));
REQUIRE(FIND_WANTEVENT(find));
bucket = find->name_bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the find.
*/
unlock_bucket = bucket;
violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
bucket = find->name_bucket;
if (bucket != DNS_ADB_INVALIDBUCKET) {
ISC_LIST_UNLINK(find->adbname->finds, find, plink);
find->adbname = NULL;
find->name_bucket = DNS_ADB_INVALIDBUCKET;
}
UNLOCK(&adb->namelocks[unlock_bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
cleanup:
if (!FIND_EVENTSENT(find)) {
ev = &find->event;
task = ev->ev_sender;
ev->ev_sender = find;
ev->ev_type = DNS_EVENT_ADBCANCELED;
ev->ev_destroy = event_free;
ev->ev_destroy_arg = find;
find->result_v4 = ISC_R_CANCELED;
find->result_v6 = ISC_R_CANCELED;
DP(DEF_LEVEL, "Sending event %p to task %p for find %p",
ev, task, find);
isc_task_sendanddetach(&task, (isc_event_t **)&ev);
}
UNLOCK(&find->lock);
}
void
dns_adb_dump(dns_adb_t *adb, FILE *f) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(f != NULL);
/*
* Lock the adb itself, lock all the name buckets, then lock all
* the entry buckets. This should put the adb into a state where
* nothing can change, so we can iterate through everything and
* print at our leisure.
*/
LOCK(&adb->lock);
dump_adb(adb, f, ISC_FALSE);
UNLOCK(&adb->lock);
}
static void
dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
if (value == INT_MAX)
return;
fprintf(f, " [%s TTL %d]", legend, value - now);
}
static void
dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug) {
int i;
dns_adbname_t *name;
isc_stdtime_t now;
isc_stdtime_get(&now);
fprintf(f, ";\n; Address database dump\n;\n");
if (debug)
fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
adb, adb->erefcnt, adb->irefcnt,
isc_mempool_getallocated(adb->nhmp));
for (i = 0 ; i < NBUCKETS ; i++)
LOCK(&adb->namelocks[i]);
for (i = 0 ; i < NBUCKETS ; i++)
LOCK(&adb->entrylocks[i]);
/*
* Dump the names
*/
for (i = 0 ; i < NBUCKETS ; i++) {
name = ISC_LIST_HEAD(adb->names[i]);
if (name == NULL)
continue;
if (debug)
fprintf(f, "; bucket %d\n", i);
for (;
name != NULL;
name = ISC_LIST_NEXT(name, plink))
{
if (debug)
fprintf(f, "; name %p (flags %08x)\n",
name, name->flags);
fprintf(f, "; ");
print_dns_name(f, &name->name);
if (dns_name_countlabels(&name->target) > 0) {
fprintf(f, " alias ");
print_dns_name(f, &name->target);
}
dump_ttl(f, "v4", name->expire_v4, now);
dump_ttl(f, "v6", name->expire_v6, now);
dump_ttl(f, "target", name->expire_target, now);
fprintf(f, " [v4 %s] [v6 %s]",
errnames[name->fetch_err],
errnames[name->fetch6_err]);
fprintf(f, "\n");
print_namehook_list(f, "v4", &name->v4, debug);
print_namehook_list(f, "v6", &name->v6, debug);
if (debug)
print_fetch_list(f, name);
if (debug)
print_find_list(f, name);
}
}
/*
* Unlock everything
*/
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->namelocks[i]);
}
static void
dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug)
{
char addrbuf[ISC_NETADDR_FORMATSIZE];
isc_netaddr_t netaddr;
isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
isc_netaddr_format(&netaddr, addrbuf, sizeof addrbuf);
if (debug)
fprintf(f, ";\t%p: refcnt %u flags %08x \n",
entry, entry->refcnt, entry->flags);
fprintf(f, ";\t%s [srtt %u]", addrbuf, entry->srtt);
fprintf(f, "\n");
}
void
dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
char tmp[512];
const char *tmpp;
dns_adbaddrinfo_t *ai;
isc_sockaddr_t *sa;
/*
* Not used currently, in the API Just In Case we
* want to dump out the name and/or entries too.
*/
LOCK(&find->lock);
fprintf(f, ";Find %p\n", find);
fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
find->query_pending, find->partial_result,
find->options, find->flags);
fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
find->name_bucket, find->adbname, find->event.ev_sender);
ai = ISC_LIST_HEAD(find->list);
if (ai != NULL)
fprintf(f, "\tAddresses:\n");
while (ai != NULL) {
sa = &ai->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
tmp, sizeof tmp);
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
tmp, sizeof tmp);
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "BadAddress";
fprintf(f, "\t\tentry %p, flags %08x"
" srtt %u addr %s\n",
ai->entry, ai->flags, ai->srtt, tmpp);
ai = ISC_LIST_NEXT(ai, publink);
}
UNLOCK(&find->lock);
}
static void
print_dns_name(FILE *f, dns_name_t *name) {
char buf[DNS_NAME_FORMATSIZE];
INSIST(f != NULL);
dns_name_format(name, buf, sizeof(buf));
fprintf(f, "%s", buf);
}
static void
print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
isc_boolean_t debug)
{
dns_adbnamehook_t *nh;
for (nh = ISC_LIST_HEAD(*list);
nh != NULL;
nh = ISC_LIST_NEXT(nh, plink))
{
if (debug)
fprintf(f, ";\tHook(%s) %p\n", legend, nh);
dump_entry(f, nh->entry, debug);
}
}
static inline void
print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
type, ft, ft->namehook, ft->entry, ft->fetch);
}
static inline void
print_fetch6(FILE *f, dns_adbfetch6_t *ft) {
fprintf(f, "\t\tFetch(A6): %p -> { nh %p, entry %p, fetch %p }\n",
ft, ft->namehook, ft->entry, ft->fetch);
}
static void
print_fetch_list(FILE *f, dns_adbname_t *n) {
dns_adbfetch6_t *fetch6;
if (NAME_FETCH_A(n))
print_fetch(f, n->fetch_a, "A");
if (NAME_FETCH_AAAA(n))
print_fetch(f, n->fetch_aaaa, "AAAA");
fetch6 = ISC_LIST_HEAD(n->fetches_a6);
while (fetch6 != NULL) {
print_fetch6(f, fetch6);
fetch6 = ISC_LIST_NEXT(fetch6, plink);
}
}
static void
print_find_list(FILE *f, dns_adbname_t *name) {
dns_adbfind_t *find;
find = ISC_LIST_HEAD(name->finds);
while (find != NULL) {
dns_adb_dumpfind(find, f);
find = ISC_LIST_NEXT(find, plink);
}
}
static isc_result_t
dbfind_name(dns_adbname_t *adbname, isc_stdtime_t now, dns_rdatatype_t rdtype)
{
isc_result_t result;
dns_rdataset_t rdataset;
dns_adb_t *adb;
dns_fixedname_t foundname;
dns_name_t *fname;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(rdtype == dns_rdatatype_a || rdtype == dns_rdatatype_aaaa);
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
dns_rdataset_init(&rdataset);
if (rdtype == dns_rdatatype_a)
adbname->fetch_err = FIND_ERR_UNEXPECTED;
else
adbname->fetch6_err = FIND_ERR_UNEXPECTED;
result = dns_view_find(adb->view, &adbname->name, rdtype, now,
NAME_GLUEOK(adbname),
ISC_TF(NAME_HINTOK(adbname)),
NULL, NULL, fname, &rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
/*
* Found in the database. Even if we can't copy out
* any information, return success, or else a fetch
* will be made, which will only make things worse.
*/
if (rdtype == dns_rdatatype_a)
adbname->fetch_err = FIND_ERR_SUCCESS;
else
adbname->fetch6_err = FIND_ERR_SUCCESS;
result = import_rdataset(adbname, &rdataset, now);
break;
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
/*
* We're authoritative and the data doesn't exist.
* Make up a negative cache entry so we don't ask again
* for a while.
*
* XXXRTH What time should we use? I'm putting in 30 seconds
* for now.
*/
if (rdtype == dns_rdatatype_a) {
adbname->expire_v4 = now + 30;
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for A",
adbname);
if (result == DNS_R_NXDOMAIN)
adbname->fetch_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch_err = FIND_ERR_NXRRSET;
} else {
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for AAAA",
adbname);
adbname->expire_v6 = now + 30;
if (result == DNS_R_NXDOMAIN)
adbname->fetch6_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch6_err = FIND_ERR_NXRRSET;
}
break;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
/*
* We found a negative cache entry. Pull the TTL from it
* so we won't ask again for a while.
*/
rdataset.ttl = ttlclamp(rdataset.ttl);
if (rdtype == dns_rdatatype_a) {
adbname->expire_v4 = rdataset.ttl + now;
if (result == DNS_R_NCACHENXDOMAIN)
adbname->fetch_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch_err = FIND_ERR_NXRRSET;
DP(NCACHE_LEVEL,
"adb name %p: Caching negative entry for A (ttl %u)",
adbname, rdataset.ttl);
} else {
DP(NCACHE_LEVEL,
"adb name %p: Caching negative entry for AAAA (ttl %u)",
adbname, rdataset.ttl);
adbname->expire_v6 = rdataset.ttl + now;
if (result == DNS_R_NCACHENXDOMAIN)
adbname->fetch6_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch6_err = FIND_ERR_NXRRSET;
}
break;
case DNS_R_CNAME:
case DNS_R_DNAME:
/*
* Clear the hint and glue flags, so this will match
* more often.
*/
adbname->flags &= ~(DNS_ADBFIND_GLUEOK | DNS_ADBFIND_HINTOK);
rdataset.ttl = ttlclamp(rdataset.ttl);
clean_target(adb, &adbname->target);
adbname->expire_target = INT_MAX;
result = set_target(adb, &adbname->name, fname, &rdataset,
&adbname->target);
if (result == ISC_R_SUCCESS) {
result = DNS_R_ALIAS;
DP(NCACHE_LEVEL,
"adb name %p: caching alias target",
adbname);
adbname->expire_target = rdataset.ttl + now;
}
if (rdtype == dns_rdatatype_a)
adbname->fetch_err = FIND_ERR_SUCCESS;
else
adbname->fetch6_err = FIND_ERR_SUCCESS;
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static isc_result_t
dbfind_a6(dns_adbname_t *adbname, isc_stdtime_t now) {
isc_result_t result;
dns_rdataset_t rdataset;
dns_adb_t *adb;
dns_a6context_t a6ctx;
dns_fixedname_t foundname;
dns_name_t *fname;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V6(adbname));
result = ISC_R_UNEXPECTED;
dns_fixedname_init(&foundname);
fname = dns_fixedname_name(&foundname);
dns_rdataset_init(&rdataset);
adbname->fetch6_err = FIND_ERR_UNEXPECTED;
result = dns_view_find(adb->view, &adbname->name, dns_rdatatype_a6,
now, NAME_GLUEOK(adbname),
ISC_TF(NAME_HINTOK(adbname)),
NULL, NULL, fname, &rdataset, NULL);
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
/*
* Start a6 chain follower. There is no need to poke people
* who might be waiting, since this is call requires there
* are none.
*/
adbname->fetch6_err = FIND_ERR_SUCCESS;
dns_a6_init(&a6ctx, a6find, NULL, import_a6,
a6missing, adbname);
(void)dns_a6_foreach(&a6ctx, &rdataset, now);
adbname->flags &= ~NAME_NEEDS_POKE;
result = ISC_R_SUCCESS;
break;
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
/*
* We're authoritative and the data doesn't exist.
* Make up a negative cache entry so we don't ask again
* for a while.
*
* XXXRTH What time should we use? I'm putting in 30 seconds
* for now.
*/
DP(NCACHE_LEVEL,
"adb name %p: Caching auth negative entry for A6",
adbname);
adbname->expire_v6 = now + 30;
if (result == DNS_R_NXDOMAIN)
adbname->fetch6_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch6_err = FIND_ERR_NXRRSET;
break;
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
/*
* We found a negative cache entry. Pull the TTL from it
* so we won't ask again for a while.
*/
DP(NCACHE_LEVEL,
"adb name %p: Caching negative entry for A6 (ttl %u)",
adbname, rdataset.ttl);
adbname->expire_v6 = ISC_MIN(rdataset.ttl + now,
adbname->expire_v6);
if (result == DNS_R_NCACHENXDOMAIN)
adbname->fetch6_err = FIND_ERR_NXDOMAIN;
else
adbname->fetch6_err = FIND_ERR_NXRRSET;
break;
case DNS_R_CNAME:
case DNS_R_DNAME:
rdataset.ttl = ttlclamp(rdataset.ttl);
clean_target(adb, &adbname->target);
adbname->expire_target = INT_MAX;
result = set_target(adb, &adbname->name, fname, &rdataset,
&adbname->target);
if (result == ISC_R_SUCCESS) {
result = DNS_R_ALIAS;
DP(NCACHE_LEVEL,
"adb name %p: caching alias target",
adbname);
adbname->expire_target = rdataset.ttl + now;
}
break;
}
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
static void
fetch_callback(isc_task_t *task, isc_event_t *ev) {
dns_fetchevent_t *dev;
dns_adbname_t *name;
dns_adb_t *adb;
dns_adbfetch_t *fetch;
int bucket;
isc_eventtype_t ev_status;
isc_stdtime_t now;
isc_result_t result;
unsigned int address_type;
isc_boolean_t want_check_exit = ISC_FALSE;
UNUSED(task);
INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->ev_arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
INSIST(NAME_FETCH_A(name) || NAME_FETCH_AAAA(name));
address_type = 0;
if (NAME_FETCH_A(name) && (name->fetch_a->fetch == dev->fetch)) {
address_type = DNS_ADBFIND_INET;
fetch = name->fetch_a;
name->fetch_a = NULL;
} else if (NAME_FETCH_AAAA(name)
&& (name->fetch_aaaa->fetch == dev->fetch)) {
address_type = DNS_ADBFIND_INET6;
fetch = name->fetch_aaaa;
name->fetch_aaaa = NULL;
}
INSIST(address_type != 0);
dns_resolver_destroyfetch(&fetch->fetch);
dev->fetch = NULL;
ev_status = DNS_EVENT_ADBNOMOREADDRESSES;
/*
* Cleanup things we don't care about.
*/
if (dev->node != NULL)
dns_db_detachnode(dev->db, &dev->node);
if (dev->db != NULL)
dns_db_detach(&dev->db);
/*
* If this name is marked as dead, clean up, throwing away
* potentially good data.
*/
if (NAME_DEAD(name)) {
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
/*
* If we're shutting down and this bucket is empty, we need
* to call check_exit() to see if we're done.
*/
if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
want_check_exit = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If we got a negative cache response, remember it.
*/
if (NCACHE_RESULT(dev->result)) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
if (address_type == DNS_ADBFIND_INET) {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"Caching negative entry for A (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v4 = ISC_MIN(name->expire_v4,
dev->rdataset->ttl + now);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch_err = FIND_ERR_NXDOMAIN;
else
name->fetch_err = FIND_ERR_NXRRSET;
} else {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"Caching negative entry for AAAA (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v6 = ISC_MIN(name->expire_v6,
dev->rdataset->ttl + now);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch6_err = FIND_ERR_NXDOMAIN;
else
name->fetch6_err = FIND_ERR_NXRRSET;
}
goto out;
}
/*
* Handle CNAME/DNAME.
*/
if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
clean_target(adb, &name->target);
name->expire_target = INT_MAX;
result = set_target(adb, &name->name,
dns_fixedname_name(&dev->foundname),
dev->rdataset,
&name->target);
if (result == ISC_R_SUCCESS) {
DP(NCACHE_LEVEL,
"adb fetch name %p: caching alias target",
name);
name->expire_target = dev->rdataset->ttl + now;
}
goto check_result;
}
/*
* Did we get back junk? If so, and there are no more fetches
* sitting out there, tell all the finds about it.
*/
if (dev->result != ISC_R_SUCCESS) {
/* XXXMLG Don't pound on bad servers. */
if (address_type == DNS_ADBFIND_INET) {
name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
name->fetch_err = FIND_ERR_FAILURE;
} else {
name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
name->fetch6_err = FIND_ERR_FAILURE;
}
goto out;
}
/*
* We got something potentially useful.
*/
result = import_rdataset(name, &fetch->rdataset, now);
check_result:
if (result == ISC_R_SUCCESS) {
ev_status = DNS_EVENT_ADBMOREADDRESSES;
if (address_type == DNS_ADBFIND_INET)
name->fetch_err = FIND_ERR_SUCCESS;
else
name->fetch6_err = FIND_ERR_SUCCESS;
}
out:
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
clean_finds_at_name(name, ev_status, address_type);
UNLOCK(&adb->namelocks[bucket]);
}
static void
fetch_callback_a6(isc_task_t *task, isc_event_t *ev) {
dns_fetchevent_t *dev;
dns_adbname_t *name;
dns_adb_t *adb;
dns_adbfetch6_t *fetch;
int bucket;
isc_stdtime_t now;
isc_result_t result;
isc_boolean_t want_check_exit = ISC_FALSE;
UNUSED(task);
INSIST(ev->ev_type == DNS_EVENT_FETCHDONE);
dev = (dns_fetchevent_t *)ev;
name = ev->ev_arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
bucket = name->lock_bucket;
LOCK(&adb->namelocks[bucket]);
INSIST(!NAME_NEEDSPOKE(name));
for (fetch = ISC_LIST_HEAD(name->fetches_a6);
fetch != NULL;
fetch = ISC_LIST_NEXT(fetch, plink))
if (fetch->fetch == dev->fetch)
break;
INSIST(fetch != NULL);
ISC_LIST_UNLINK(name->fetches_a6, fetch, plink);
DP(ENTER_LEVEL, "ENTER: fetch_callback_a6() name %p", name);
dns_resolver_destroyfetch(&fetch->fetch);
dev->fetch = NULL;
/*
* Cleanup things we don't care about.
*/
if (dev->node != NULL)
dns_db_detachnode(dev->db, &dev->node);
if (dev->db != NULL)
dns_db_detach(&dev->db);
/*
* If this name is marked as dead, clean up, throwing away
* potentially good data.
*/
if (NAME_DEAD(name)) {
free_adbfetch6(adb, &fetch);
isc_event_free(&ev);
kill_name(&name, DNS_EVENT_ADBCANCELED);
/*
* If we're shutting down and this bucket is empty, we need
* to call check_exit() to see if we're done.
*/
if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
want_check_exit = ISC_TRUE;
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If the A6 query didn't succeed, and this is the first query
* in the A6 chain, try AAAA records instead. For later failures,
* don't do this.
*/
if (dev->result != ISC_R_SUCCESS) {
DP(DEF_LEVEL, "name %p: A6 failed: %s",
name, isc_result_totext(dev->result));
/*
* If we got a negative cache response, remember it.
*/
if (NCACHE_RESULT(dev->result)) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
DP(NCACHE_LEVEL, "adb fetch name %p: "
"Caching negative entry for A6 (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v6 = ISC_MIN(name->expire_v6,
dev->rdataset->ttl + now);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch6_err = FIND_ERR_NXDOMAIN;
else
name->fetch6_err = FIND_ERR_NXRRSET;
}
/*
* Handle CNAME/DNAME.
*/
if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
clean_target(adb, &name->target);
name->expire_target = INT_MAX;
result = set_target(adb, &name->name,
dns_fixedname_name(&dev->foundname),
dev->rdataset,
&name->target);
if (result == ISC_R_SUCCESS) {
DP(NCACHE_LEVEL,
"adb A6 fetch name %p: caching alias target",
name);
name->expire_target = dev->rdataset->ttl + now;
if (FETCH_FIRSTA6(fetch)) {
/*
* Make this name 'pokeable', since
* we've learned that this name is an
* alias.
*/
name->flags |= NAME_NEEDS_POKE;
}
}
goto out;
}
if (FETCH_FIRSTA6(fetch) && !NAME_HAS_V6(name)) {
DP(DEF_LEVEL,
"name %p: A6 query failed, starting AAAA", name);
/*
* Since this is the very first fetch, and it
* failed, we know there are no more running.
*/
result = dbfind_name(name, now, dns_rdatatype_aaaa);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"name %p: callback_a6: Found AAAA for",
name);
name->flags |= NAME_NEEDS_POKE;
goto out;
}
/*
* Listen to negative cache hints, and don't start
* another query.
*/
if (NCACHE_RESULT(result) || AUTH_NX(result)) {
if (NXDOMAIN_RESULT(result))
name->fetch6_err = NEWERR(name->fetch6_err, FIND_ERR_NXDOMAIN);
else
name->fetch6_err = NEWERR(name->fetch6_err, FIND_ERR_NXRRSET);
goto out;
}
/*
* Try to start fetches for AAAA.
*/
result = fetch_name_aaaa(name);
if (result == ISC_R_SUCCESS) {
DP(DEF_LEVEL,
"name %p: callback_a6: Started AAAA fetch",
name);
goto out;
}
}
goto out;
}
/*
* We got something potentially useful. Run the A6 chain
* follower on this A6 rdataset.
*/
fetch->a6ctx.chains = name->chains;
(void)dns_a6_foreach(&fetch->a6ctx, dev->rdataset, now);
out:
free_adbfetch6(adb, &fetch);
isc_event_free(&ev);
if (NAME_NEEDSPOKE(name)) {
clean_finds_at_name(name, DNS_EVENT_ADBMOREADDRESSES,
DNS_ADBFIND_INET6);
name->fetch6_err = FIND_ERR_SUCCESS;
} else if (!NAME_FETCH_V6(name))
clean_finds_at_name(name, DNS_EVENT_ADBNOMOREADDRESSES,
DNS_ADBFIND_INET6);
name->flags &= ~NAME_NEEDS_POKE;
UNLOCK(&adb->namelocks[bucket]);
return;
}
static isc_result_t
fetch_name_v4(dns_adbname_t *adbname, isc_boolean_t start_at_root) {
isc_result_t result;
dns_adbfetch_t *fetch;
dns_adb_t *adb;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V4(adbname));
adbname->fetch_err = FIND_ERR_NOTFOUND;
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
if (start_at_root) {
DP(50, "fetch_name_v4: starting at DNS root for name %p",
adbname);
name = dns_rootname;
result = dns_view_simplefind(adb->view, name, dns_rdatatype_ns,
0, 0, ISC_TRUE, &rdataset, NULL);
if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
goto cleanup;
nameservers = &rdataset;
}
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a,
name, nameservers, NULL, 0,
adb->task, fetch_callback,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
adbname->fetch_a = fetch;
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
/* XXXMLG Why doesn't this look a lot like fetch_name_a and fetch_name_a6? */
static isc_result_t
fetch_name_aaaa(dns_adbname_t *adbname) {
isc_result_t result;
dns_adbfetch_t *fetch;
dns_adb_t *adb;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_AAAA(adbname));
adbname->fetch6_err = FIND_ERR_NOTFOUND;
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_aaaa,
NULL, NULL, NULL, 0,
adb->task, fetch_callback,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
adbname->fetch_aaaa = fetch;
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch(adb, &fetch);
return (result);
}
static isc_result_t
fetch_name_a6(dns_adbname_t *adbname, isc_boolean_t start_at_root) {
isc_result_t result;
dns_adbfetch6_t *fetch;
dns_adb_t *adb;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V6(adbname));
adbname->fetch6_err = FIND_ERR_NOTFOUND;
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
if (start_at_root) {
DP(50, "fetch_name_a6: starting at DNS root for name %p",
adbname);
name = dns_rootname;
result = dns_view_simplefind(adb->view, name, dns_rdatatype_ns,
0, 0, ISC_TRUE, &rdataset, NULL);
if (result != ISC_R_SUCCESS && result != DNS_R_HINT)
goto cleanup;
nameservers = &rdataset;
}
fetch = new_adbfetch6(adb, adbname, NULL);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
fetch->flags |= FETCH_FIRST_A6;
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a6,
name, nameservers, NULL, 0,
adb->task, fetch_callback_a6,
adbname, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
ISC_LIST_APPEND(adbname->fetches_a6, fetch, plink);
fetch = NULL; /* keep us from cleaning this up below */
cleanup:
if (fetch != NULL)
free_adbfetch6(adb, &fetch);
if (dns_rdataset_isassociated(&rdataset))
dns_rdataset_disassociate(&rdataset);
return (result);
}
/*
* XXXMLG Needs to take a find argument and an address info, no zone or adb,
* since these can be extracted from the find itself.
*/
isc_result_t
dns_adb_marklame(dns_adb_t *adb, dns_adbaddrinfo_t *addr, dns_name_t *zone,
isc_stdtime_t expire_time)
{
dns_adbzoneinfo_t *zi;
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(zone != NULL);
zi = new_adbzoneinfo(adb, zone);
if (zi == NULL)
return (ISC_R_NOMEMORY);
zi->lame_timer = expire_time;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
ISC_LIST_PREPEND(addr->entry->zoneinfo, zi, plink);
UNLOCK(&adb->entrylocks[bucket]);
return (ISC_R_SUCCESS);
}
void
dns_adb_adjustsrtt(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int rtt, unsigned int factor)
{
int bucket;
unsigned int new_srtt;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
REQUIRE(factor <= 10);
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
new_srtt = (addr->entry->srtt / 10 * factor)
+ (rtt / 10 * (10 - factor));
addr->entry->srtt = new_srtt;
addr->srtt = new_srtt;
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int bits, unsigned int mask)
{
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
/*
* Note that we do not update the other bits in addr->flags with
* the most recent values from addr->entry->flags.
*/
addr->flags = (addr->flags & ~mask) | (bits & mask);
UNLOCK(&adb->entrylocks[bucket]);
}
isc_result_t
dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
{
int bucket;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *addr;
isc_result_t result;
in_port_t port;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL && *addrp == NULL);
UNUSED(now);
result = ISC_R_SUCCESS;
bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, sa, &bucket);
if (adb->entry_sd[bucket]) {
result = ISC_R_SHUTTINGDOWN;
goto unlock;
}
if (entry == NULL) {
/*
* We don't know anything about this address.
*/
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
entry->sockaddr = *sa;
link_entry(adb, bucket, entry);
DP(50, "findaddrinfo: new entry %p", entry);
} else
DP(50, "findaddrinfo: found entry %p", entry);
port = isc_sockaddr_getport(sa);
addr = new_adbaddrinfo(adb, entry, port);
if (addr != NULL) {
inc_entry_refcnt(adb, entry, ISC_FALSE);
*addrp = addr;
}
unlock:
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
void
dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
dns_adbaddrinfo_t *addr;
dns_adbentry_t *entry;
int bucket;
isc_stdtime_t now;
isc_boolean_t want_check_exit = ISC_FALSE;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL);
addr = *addrp;
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
entry = addr->entry;
REQUIRE(DNS_ADBENTRY_VALID(entry));
isc_stdtime_get(&now);
*addrp = NULL;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
entry->expires = now + 1800; /* XXXRTH */
dec_entry_refcnt(adb, entry, ISC_FALSE);
if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
want_check_exit = ISC_TRUE;
UNLOCK(&adb->entrylocks[bucket]);
addr->entry = NULL;
free_adbaddrinfo(adb, &addr);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
}
void
dns_adb_flush(dns_adb_t *adb) {
unsigned int i;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
for (i = 0 ; i < NBUCKETS ; i++) {
/*
* Call our cleanup routines.
*/
cleanup_names(adb, i, INT_MAX);
cleanup_entries(adb, i, INT_MAX);
}
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE);
#endif
UNLOCK(&adb->lock);
}