adb.c revision d92770e851ebbc1005b3bf121e3c9f13b67a3f42
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
1da14e066c23245c255dedb311d5a9cf0e5bb667Automatic Updater * Copyright (C) 1999-2001 Internet Software Consortium.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Permission to use, copy, modify, and distribute this software for any
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * purpose with or without fee is hereby granted, provided that the above
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews/* $Id: adb.c,v 1.200 2002/11/16 17:20:03 marka Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * Implementation notes
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * --------------------
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson *
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * In finds, if task == NULL, no events will be generated, and no events
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington * have been sent. If task != NULL but taskaction == NULL, an event has been
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson * posted but not yet freed. If neither are NULL, no event was posted.
cc0a5f714231709409b9e1b85f0f97ae50854451Mark Andrews *
6028d1ce0380d0ba7f6c6ecd1ad20b31ddd1becbDavid Lawrence */
364a82f7c25b62967678027043425201a5e5171aBob Halley
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * After we have cleaned all buckets, dump the database contents.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson#if 0
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DUMP_ADB_AFTER_CLEANING
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#endif
5afa531442369eed0e93a8af14422b30f400bd89Mark Andrews
476386968b1f287a695f73c48862e961011af99bMark Andrews#include <config.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
9259fed3d8ac5d1efa9b5a647969e40c9c934484Andreas Gustafsson#include <limits.h>
3aca8e5bf3740bbcc3bb13dde242d7cc369abb27Mark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#include <isc/mutexblock.h>
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson#include <isc/netaddr.h>
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson#include <isc/random.h>
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrews#include <isc/string.h> /* Required for HP/UX (and others?) */
9259fed3d8ac5d1efa9b5a647969e40c9c934484Andreas Gustafsson#include <isc/task.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <isc/timer.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/util.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/a6.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/adb.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/db.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/events.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/log.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/rdata.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/rdataset.h>
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#include <dns/rdatastruct.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <dns/resolver.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#include <dns/result.h>
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBZONEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBZONEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBZONEINFO_MAGIC)
7b68fa6229f1edadac44c7ec459c9ed77a8368c8Mark Andrews#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * The number of buckets needs to be a prime (for good hashing).
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington *
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * XXXRTH How many buckets do we need?
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NBUCKETS 1009 /* how many buckets for names/addrs */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
3eef7eaba00e9bd468d8036c709a296a0e5b76f1Andreas Gustafsson/*
7b68fa6229f1edadac44c7ec459c9ed77a8368c8Mark Andrews * For type 3 negative cache entries, we will remember that the address is
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * broken for this long. XXXMLG This is also used for actual addresses, too.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * The intent is to keep us from constantly asking about A/A6/AAAA records
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * if the zone has extremely low TTLs.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define ADB_CACHE_MINIMUM 10 /* seconds */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define ADB_CACHE_MAXIMUM 86400 /* seconds (86400 = 24 hours) */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
ad5bc22a819190839bdcc4d102d023782dc23660Mark Andrews * Wake up every CLEAN_SECONDS and clean CLEAN_BUCKETS buckets, so that all
c7e266b7e5675e12d1ca3cc929f24b3e86d41f8eEvan Hunt * buckets are cleaned in CLEAN_PERIOD seconds.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define CLEAN_PERIOD 3600
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define CLEAN_SECONDS 30
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define CLEAN_BUCKETS ((NBUCKETS * CLEAN_SECONDS) / CLEAN_PERIOD)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FREE_ITEMS 64 /* free count for memory pools */
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FILL_COUNT 16 /* fill count for memory pools */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADB_INVALIDBUCKET (-1) /* invalid bucket address */
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define DNS_ADB_MINADBSIZE (1024*1024) /* 1 Megabyte */
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewstypedef ISC_LIST(dns_adbname_t) dns_adbnamelist_t;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewstypedef struct dns_adbnamehook dns_adbnamehook_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef ISC_LIST(dns_adbnamehook_t) dns_adbnamehooklist_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef struct dns_adbzoneinfo dns_adbzoneinfo_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef ISC_LIST(dns_adbentry_t) dns_adbentrylist_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef struct dns_adbfetch dns_adbfetch_t;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtontypedef struct dns_adbfetch6 dns_adbfetch6_t;
a687a0592bbe3a582860eb5f03725bf80d7ac1d8Mark Andrews
a687a0592bbe3a582860eb5f03725bf80d7ac1d8Mark Andrewsstruct dns_adb {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int magic;
a687a0592bbe3a582860eb5f03725bf80d7ac1d8Mark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mutex_t lock;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mutex_t reflock; /* Covers irefcnt, erefcnt */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mem_t *mctx;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_view_t *view;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_timermgr_t *timermgr;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_timer_t *timer;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_taskmgr_t *taskmgr;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_task_t *task;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_boolean_t overmem;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews isc_interval_t tick_interval;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews int next_cleanbucket;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews unsigned int irefcnt;
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews unsigned int erefcnt;
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews
0015ab097438e041197b19b9de2ba48f6bfd1c6cDavid Lawrence isc_mutex_t mplock;
0015ab097438e041197b19b9de2ba48f6bfd1c6cDavid Lawrence isc_mempool_t *nmp; /* dns_adbname_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *nhmp; /* dns_adbnamehook_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *zimp; /* dns_adbzoneinfo_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *emp; /* dns_adbentry_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *ahmp; /* dns_adbfind_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *aimp; /* dns_adbaddrinfo_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *afmp; /* dns_adbfetch_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mempool_t *af6mp; /* dns_adbfetch6_t */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Bucketized locks and lists for names.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington *
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbnamelist_t names[NBUCKETS];
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_mutex_t namelocks[NBUCKETS];
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_boolean_t name_sd[NBUCKETS];
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int name_refcnt[NBUCKETS];
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Bucketized locks for entries.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington *
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * XXXRTH Have a per-bucket structure that contains all of these?
6e373c502584f9292e964378411d296c8259026bMark Andrews */
6e373c502584f9292e964378411d296c8259026bMark Andrews dns_adbentrylist_t entries[NBUCKETS];
6e373c502584f9292e964378411d296c8259026bMark Andrews isc_mutex_t entrylocks[NBUCKETS];
6e373c502584f9292e964378411d296c8259026bMark Andrews isc_boolean_t entry_sd[NBUCKETS]; /* shutting down */
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews unsigned int entry_refcnt[NBUCKETS];
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews isc_event_t cevent;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews isc_boolean_t cevent_sent;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews isc_boolean_t shutting_down;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews isc_eventlist_t whenshutdown;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews};
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
92f60809e854ccf5f115883c6347e370da048848Mark Andrews/*
92f60809e854ccf5f115883c6347e370da048848Mark Andrews * XXXMLG Document these structures.
92f60809e854ccf5f115883c6347e370da048848Mark Andrews */
92f60809e854ccf5f115883c6347e370da048848Mark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstruct dns_adbname {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_name_t name;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adb_t *adb;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int partial_result;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int flags;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington int lock_bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_name_t target;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_stdtime_t expire_target;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_stdtime_t expire_v4;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_stdtime_t expire_v6;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int chains;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbnamehooklist_t v4;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbnamehooklist_t v6;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbfetch_t *fetch_a;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbfetch_t *fetch_aaaa;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LIST(dns_adbfetch6_t) fetches_a6;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int fetch_err;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int fetch6_err;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbfindlist_t finds;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LINK(dns_adbname_t) plink;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington};
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstruct dns_adbfetch {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbnamehook_t *namehook;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *entry;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_fetch_t *fetch;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_rdataset_t rdataset;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington};
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstruct dns_adbfetch6 {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int flags;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbnamehook_t *namehook;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *entry;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_fetch_t *fetch;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_rdataset_t rdataset;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_a6context_t a6ctx;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LINK(dns_adbfetch6_t) plink;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington};
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * dns_adbnamehook_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington *
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * This is a small widget that dangles off a dns_adbname_t. It contains a
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * pointer to the address information about this host, and a link to the next
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * namehook that will contain the next address this host has.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstruct dns_adbnamehook {
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *entry;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LINK(dns_adbnamehook_t) plink;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington};
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * dns_adbzoneinfo_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington *
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * This is a small widget that holds zone-specific information about an
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * address. Currently limited to lameness, but could just as easily be
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * extended to other types of information about zones.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstruct dns_adbzoneinfo {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_name_t zone;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_stdtime_t lame_timer;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LINK(dns_adbzoneinfo_t) plink;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington};
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * An address entry. It holds quite a bit of information about addresses,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * including edns state (in "flags"), rtt, and of course the address of
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * the host.
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstruct dns_adbentry {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int magic;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington int lock_bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int refcnt;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int flags;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int srtt;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_sockaddr_t sockaddr;
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_stdtime_t expires;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington /*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * A nonzero 'expires' field indicates that the entry should
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * persist until that time. This allows entries found
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews * using dns_adb_findaddrinfo() to persist for a limited time
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * even though they are not necessarily associated with a
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * name.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LIST(dns_adbzoneinfo_t) zoneinfo;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson ISC_LINK(dns_adbentry_t) plink;
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson};
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson/*
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson * Internal functions (and prototypes).
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson */
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic inline dns_adbname_t *new_adbname(dns_adb_t *, dns_name_t *);
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic inline void free_adbname(dns_adb_t *, dns_adbname_t **);
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic inline dns_adbnamehook_t *new_adbnamehook(dns_adb_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson dns_adbentry_t *);
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updaterstatic inline void free_adbnamehook(dns_adb_t *, dns_adbnamehook_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbzoneinfo_t *new_adbzoneinfo(dns_adb_t *, dns_name_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void free_adbzoneinfo(dns_adb_t *, dns_adbzoneinfo_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbentry_t *new_adbentry(dns_adb_t *);
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updaterstatic inline void free_adbentry(dns_adb_t *, dns_adbentry_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbfind_t *new_adbfind(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline isc_boolean_t free_adbfind(dns_adb_t *, dns_adbfind_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbaddrinfo_t *new_adbaddrinfo(dns_adb_t *, dns_adbentry_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson in_port_t);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbfetch_t *new_adbfetch(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void free_adbfetch(dns_adb_t *, dns_adbfetch_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbfetch6_t *new_adbfetch6(dns_adb_t *, dns_adbname_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson dns_a6context_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void free_adbfetch6(dns_adb_t *, dns_adbfetch6_t **);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbname_t *find_name_and_lock(dns_adb_t *, dns_name_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson unsigned int, int *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline dns_adbentry_t *find_entry_and_lock(dns_adb_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson isc_sockaddr_t *, int *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void dump_adb(dns_adb_t *, FILE *, isc_boolean_t debug);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_dns_name(FILE *, dns_name_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_namehook_list(FILE *, const char *legend,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson dns_adbnamehooklist_t *list,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson isc_boolean_t debug,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson isc_stdtime_t now);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_find_list(FILE *, dns_adbname_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void print_fetch_list(FILE *, dns_adbname_t *);
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austeinstatic inline isc_boolean_t dec_adb_irefcnt(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void inc_adb_irefcnt(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void inc_adb_erefcnt(dns_adb_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void inc_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson isc_boolean_t);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline isc_boolean_t dec_entry_refcnt(dns_adb_t *, dns_adbentry_t *,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson isc_boolean_t);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic inline void violate_locking_hierarchy(isc_mutex_t *, isc_mutex_t *);
d5e72d5dba7b77ae0036c53578bcabcf3af1f4b7Andreas Gustafssonstatic isc_boolean_t clean_namehooks(dns_adb_t *, dns_adbnamehooklist_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void clean_target(dns_adb_t *, dns_name_t *);
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafssonstatic void clean_finds_at_name(dns_adbname_t *, isc_eventtype_t,
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson unsigned int);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t check_expire_namehooks(dns_adbname_t *, isc_stdtime_t,
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews isc_boolean_t);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic void cancel_fetches_at_name(dns_adbname_t *);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic isc_result_t dbfind_name(dns_adbname_t *, isc_stdtime_t,
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_rdatatype_t);
1cf54d1966b3de8f6593e9e80eae9a80a1c011adMark Andrewsstatic isc_result_t fetch_name_v4(dns_adbname_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_aaaa(dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_result_t fetch_name_a6(dns_adbname_t *, isc_boolean_t);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void check_exit(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void timer_cleanup(isc_task_t *, isc_event_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void destroy(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_names(dns_adb_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic isc_boolean_t shutdown_entries(dns_adb_t *);
1cf54d1966b3de8f6593e9e80eae9a80a1c011adMark Andrewsstatic inline void link_name(dns_adb_t *, int, dns_adbname_t *);
1cf54d1966b3de8f6593e9e80eae9a80a1c011adMark Andrewsstatic inline isc_boolean_t unlink_name(dns_adb_t *, dns_adbname_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline void link_entry(dns_adb_t *, int, dns_adbentry_t *);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t unlink_entry(dns_adb_t *, dns_adbentry_t *);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic isc_boolean_t kill_name(dns_adbname_t **, isc_eventtype_t);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic void fetch_callback_a6(isc_task_t *, isc_event_t *);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic isc_result_t dbfind_a6(dns_adbname_t *, isc_stdtime_t);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrewsstatic void water(void *arg, int mark);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson/*
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews * MUST NOT overlap DNS_ADBFIND_* flags!
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_EVENT_SENT 0x40000000
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define FIND_EVENT_FREED 0x80000000
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define FIND_EVENTSENT(h) (((h)->flags & FIND_EVENT_SENT) != 0)
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews#define FIND_EVENTFREED(h) (((h)->flags & FIND_EVENT_FREED) != 0)
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define NAME_NEEDS_POKE 0x80000000
0015ab097438e041197b19b9de2ba48f6bfd1c6cDavid Lawrence#define NAME_IS_DEAD 0x40000000
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_HINT_OK DNS_ADBFIND_HINTOK
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_GLUE_OK DNS_ADBFIND_GLUEOK
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_STARTATROOT DNS_ADBFIND_STARTATROOT
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_DEAD(n) (((n)->flags & NAME_IS_DEAD) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_NEEDSPOKE(n) (((n)->flags & NAME_NEEDS_POKE) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_GLUEOK(n) (((n)->flags & NAME_GLUE_OK) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson#define NAME_HINTOK(n) (((n)->flags & NAME_HINT_OK) != 0)
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson
8dd4cf7fc6c025c547a473de5df7a72939d0cb2aDavid Lawrence/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * To the name, address classes are all that really exist. If it has a
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * V6 address it doesn't care if it came from an A6 chain or an AAAA query.
cc0a5f714231709409b9e1b85f0f97ae50854451Mark Andrews */
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews#define NAME_HAS_V4(n) (!ISC_LIST_EMPTY((n)->v4))
476386968b1f287a695f73c48862e961011af99bMark Andrews#define NAME_HAS_V6(n) (!ISC_LIST_EMPTY((n)->v6))
476386968b1f287a695f73c48862e961011af99bMark Andrews#define NAME_HAS_ADDRS(n) (NAME_HAS_V4(n) || NAME_HAS_V6(n))
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews/*
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews * Fetches are broken out into A, AAAA, and A6 types. In some cases,
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * however, it makes more sense to test for a particular class of fetches,
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * like V4 or V6 above.
b4dfb4747498ab22831a30c44607e81082fef962Michael Graff */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_A(n) ((n)->fetch_a != NULL)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_AAAA(n) ((n)->fetch_aaaa != NULL)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_A6(n) (!ISC_LIST_EMPTY((n)->fetches_a6))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_V4(n) (NAME_FETCH_A(n))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH_V6(n) (NAME_FETCH_AAAA(n) || NAME_FETCH_A6(n))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NAME_FETCH(n) (NAME_FETCH_V4(n) || NAME_FETCH_V6(n))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Was this fetch started using the hints database?
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Was this the initial fetch for the A6 record? If so, we might want to
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * start AAAA queries if it fails.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define FETCH_FIRST_A6 0x80000000
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define FETCH_FIRSTA6(f) (((f)->flags & FETCH_FIRST_A6) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews/*
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews * Find options and tests to see if there are addresses on the list.
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews */
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_WANTEVENT(fn) (((fn)->options & DNS_ADBFIND_WANTEVENT) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_WANTEMPTYEVENT(fn) (((fn)->options & DNS_ADBFIND_EMPTYEVENT) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_AVOIDFETCHES(fn) (((fn)->options & DNS_ADBFIND_AVOIDFETCHES) \
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson != 0)
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson#define FIND_STARTATROOT(fn) (((fn)->options & DNS_ADBFIND_STARTATROOT) \
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson != 0)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FIND_HINTOK(fn) (((fn)->options & DNS_ADBFIND_HINTOK) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define FIND_GLUEOK(fn) (((fn)->options & DNS_ADBFIND_GLUEOK) != 0)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define FIND_HAS_ADDRS(fn) (!ISC_LIST_EMPTY((fn)->list))
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews#define FIND_RETURNLAME(fn) (((fn)->options & DNS_ADBFIND_RETURNLAME) != 0)
de9833be77ef92c17b35c02d138a0ad8df34dd91Mark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson * These are currently used on simple unsigned ints, so they are
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * not really associated with any particular type.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define WANT_INET(x) (((x) & DNS_ADBFIND_INET) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define WANT_INET6(x) (((x) & DNS_ADBFIND_INET6) != 0)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define EXPIRE_OK(exp, now) ((exp == INT_MAX) || (exp < now))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson * Find out if the flags on a name (nf) indicate if it is a hint or
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews * glue, and compare this to the appropriate bits set in o, to see if
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews * this is ok.
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews */
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define GLUE_OK(nf, o) (!NAME_GLUEOK(nf) || (((o) & DNS_ADBFIND_GLUEOK) != 0))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define HINT_OK(nf, o) (!NAME_HINTOK(nf) || (((o) & DNS_ADBFIND_HINTOK) != 0))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define GLUEHINT_OK(nf, o) (GLUE_OK(nf, o) || HINT_OK(nf, o))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define STARTATROOT_MATCHES(nf, o) (((nf)->flags & NAME_STARTATROOT) == \
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews ((o) & DNS_ADBFIND_STARTATROOT))
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define ENTER_LEVEL ISC_LOG_DEBUG(50)
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrews#define EXIT_LEVEL ENTER_LEVEL
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrews#define CLEAN_LEVEL ISC_LOG_DEBUG(100)
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrews#define DEF_LEVEL ISC_LOG_DEBUG(5)
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrews#define NCACHE_LEVEL ISC_LOG_DEBUG(20)
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrews
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define NCACHE_RESULT(r) ((r) == DNS_R_NCACHENXDOMAIN || \
886b96ebfd555cfeaf37ae46fc08421a41c61392Andreas Gustafsson (r) == DNS_R_NCACHENXRRSET)
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define AUTH_NX(r) ((r) == DNS_R_NXDOMAIN || \
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews (r) == DNS_R_NXRRSET)
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define NXDOMAIN_RESULT(r) ((r) == DNS_R_NXDOMAIN || \
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews (r) == DNS_R_NCACHENXDOMAIN)
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson#define NXRRSET_RESULT(r) ((r) == DNS_R_NCACHENXRRSET || \
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington (r) == DNS_R_NXRRSET || \
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington (r) == DNS_R_HINTNXRRSET)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews * Error state rankings.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_SUCCESS 0 /* highest rank */
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_CANCELED 1
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews#define FIND_ERR_FAILURE 2
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_NXDOMAIN 3
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_NXRRSET 4
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_UNEXPECTED 5
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_NOTFOUND 6
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews#define FIND_ERR_MAX 7
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrewsstatic const char *errnames[] = {
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews "success",
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews "canceled",
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson "failure",
207f0a15bb486d8dc27cf5ff963fac6068ee2972Mark Andrews "nxdomain",
207f0a15bb486d8dc27cf5ff963fac6068ee2972Mark Andrews "nxrrset",
207f0a15bb486d8dc27cf5ff963fac6068ee2972Mark Andrews "unexpected",
207f0a15bb486d8dc27cf5ff963fac6068ee2972Mark Andrews "not_found"
207f0a15bb486d8dc27cf5ff963fac6068ee2972Mark Andrews};
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington#define NEWERR(old, new) (ISC_MIN((old), (new)))
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrewsstatic isc_result_t find_err_map[FIND_ERR_MAX] = {
8249eee42adc8c9c06c6ff9aaecc7437e259c687Mark Andrews ISC_R_SUCCESS,
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson ISC_R_CANCELED,
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson ISC_R_FAILURE,
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson DNS_R_NXDOMAIN,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington DNS_R_NXRRSET,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_R_UNEXPECTED,
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson ISC_R_NOTFOUND /* not YET found */
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson};
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic void
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian WellingtonDP(int level, const char *format, ...) ISC_FORMAT_PRINTF(2, 3);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic void
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian WellingtonDP(int level, const char *format, ...) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington va_list args;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington va_start(args, format);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_log_vwrite(dns_lctx,
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ADB,
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington level, format, args);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington va_end(args);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington}
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic inline dns_ttl_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonttlclamp(dns_ttl_t ttl) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (ttl < ADB_CACHE_MINIMUM)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ttl = ADB_CACHE_MINIMUM;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (ttl > ADB_CACHE_MAXIMUM)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ttl = ADB_CACHE_MAXIMUM;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
dcd12febbd47f5846d6c75f6a4dcba8a873bc153Andreas Gustafsson return (ttl);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson}
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Requires the adbname bucket be locked and that no entry buckets be locked.
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson *
a3a11c4f3fc9ba972802b811c4d95a9884d6ff4aMichael Sawyer * This code handles A and AAAA rdatasets only.
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson */
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafssonstatic isc_result_t
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafssonimport_rdataset(dns_adbname_t *adbname, dns_rdataset_t *rdataset,
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson isc_stdtime_t now)
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson{
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_result_t result;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adb_t *adb;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbnamehook_t *nh;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_rdata_t rdata = DNS_RDATA_INIT;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington struct in_addr ina;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington struct in6_addr in6a;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_sockaddr_t sockaddr;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *foundentry; /* NO CLEAN UP! */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington int addr_bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_boolean_t new_addresses_added;
4e1d3e67cdc76609bad5f0310ac48de10b442b9fMark Andrews dns_rdatatype_t rdtype;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington unsigned int findoptions;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
3b1fce680f1dbe9467cd3b0ab3138ea52d5a976fMark Andrews INSIST(DNS_ADBNAME_VALID(adbname));
3b1fce680f1dbe9467cd3b0ab3138ea52d5a976fMark Andrews adb = adbname->adb;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington INSIST(DNS_ADB_VALID(adb));
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington rdtype = rdataset->type;
ef653fbdb122e9e251bdfbdd4609d03a208bb79dMichael Graff INSIST((rdtype == dns_rdatatype_a) || (rdtype == dns_rdatatype_aaaa));
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (rdtype == dns_rdatatype_a)
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington findoptions = DNS_ADBFIND_INET;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington else
7e0d7323d73f31ceb678879ad3aca873dc382483Brian Wellington findoptions = DNS_ADBFIND_INET6;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
023dd2cfe0029002f5e5066ce0cc586b7b703376Mark Andrews addr_bucket = DNS_ADB_INVALIDBUCKET;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington new_addresses_added = ISC_FALSE;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington nh = NULL;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington result = dns_rdataset_first(rdataset);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington while (result == ISC_R_SUCCESS) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_rdata_reset(&rdata);
023dd2cfe0029002f5e5066ce0cc586b7b703376Mark Andrews dns_rdataset_current(rdataset, &rdata);
7e0d7323d73f31ceb678879ad3aca873dc382483Brian Wellington if (rdtype == dns_rdatatype_a) {
7e0d7323d73f31ceb678879ad3aca873dc382483Brian Wellington INSIST(rdata.length == 4);
7e0d7323d73f31ceb678879ad3aca873dc382483Brian Wellington memcpy(&ina.s_addr, rdata.data, 4);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson isc_sockaddr_fromin(&sockaddr, &ina, 0);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence } else {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington INSIST(rdata.length == 16);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington memcpy(in6a.s6_addr, rdata.data, 16);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_sockaddr_fromin6(&sockaddr, &in6a, 0);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrews
dfd7798d8b870abf03795d8095297a4b982ab6e9Mark Andrews INSIST(nh == NULL);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington nh = new_adbnamehook(adb, NULL);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (nh == NULL) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington adbname->partial_result |= findoptions;
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson result = ISC_R_NOMEMORY;
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrews goto fail;
dfd7798d8b870abf03795d8095297a4b982ab6e9Mark Andrews }
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews if (foundentry == NULL) {
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews dns_adbentry_t *entry;
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews
ca84283333d22c64abfbcb87872dd5e6d9172c5aMark Andrews entry = new_adbentry(adb);
3aca8e5bf3740bbcc3bb13dde242d7cc369abb27Mark Andrews if (entry == NULL) {
3aca8e5bf3740bbcc3bb13dde242d7cc369abb27Mark Andrews adbname->partial_result |= findoptions;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington result = ISC_R_NOMEMORY;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington goto fail;
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson }
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson entry->sockaddr = sockaddr;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington entry->refcnt = 1;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington nh->entry = entry;
17dba29ba5db791976e505114baee53a1dde88aaBrian Wellington
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson link_entry(adb, addr_bucket, entry);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington } else {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington foundentry->refcnt++;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington nh->entry = foundentry;
17dba29ba5db791976e505114baee53a1dde88aaBrian Wellington }
cc0a5f714231709409b9e1b85f0f97ae50854451Mark Andrews
cc0a5f714231709409b9e1b85f0f97ae50854451Mark Andrews new_addresses_added = ISC_TRUE;
cb8fd52bbeaf40c9166a0144541c4ff2bafc2dd6Andreas Gustafsson if (rdtype == dns_rdatatype_a)
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson ISC_LIST_APPEND(adbname->v4, nh, plink);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson else
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson ISC_LIST_APPEND(adbname->v6, nh, plink);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson nh = NULL;
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson result = dns_rdataset_next(rdataset);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson }
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson fail:
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson if (nh != NULL)
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson free_adbnamehook(adb, &nh);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson if (addr_bucket != DNS_ADB_INVALIDBUCKET)
76d9120dd696209fa1186ea289ea01cd4677782fAndreas Gustafsson UNLOCK(&adb->entrylocks[addr_bucket]);
76d9120dd696209fa1186ea289ea01cd4677782fAndreas Gustafsson
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson rdataset->ttl = ttlclamp(rdataset->ttl);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson
cc0a5f714231709409b9e1b85f0f97ae50854451Mark Andrews if (rdtype == dns_rdatatype_a) {
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson DP(NCACHE_LEVEL, "expire_v4 set to MIN(%u,%u) import_rdataset",
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson adbname->expire_v4, now + rdataset->ttl);
687b7ef9989c9ab9040f4ccb5f1816b96fa4e43fAndreas Gustafsson adbname->expire_v4 = ISC_MIN(adbname->expire_v4,
8cccaeaee13993c49009d3915806c1d0bd03743bAndreas Gustafsson now + rdataset->ttl);
8cccaeaee13993c49009d3915806c1d0bd03743bAndreas Gustafsson } else {
8cccaeaee13993c49009d3915806c1d0bd03743bAndreas Gustafsson DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) import_rdataset",
8cccaeaee13993c49009d3915806c1d0bd03743bAndreas Gustafsson adbname->expire_v6, now + rdataset->ttl);
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews adbname->expire_v6 = ISC_MIN(adbname->expire_v6,
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews now + rdataset->ttl);
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews }
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews if (new_addresses_added) {
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews /*
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews * Lie a little here. This is more or less so code that cares
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews * can find out if any new information was added or not.
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews */
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews return (ISC_R_SUCCESS);
1672cff96d0b02badab6f94524e10285dde851fcMark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (result);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic void
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsimport_a6(dns_a6context_t *a6ctx) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_adbname_t *name;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_adb_t *adb;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_adbnamehook_t *nh;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_adbentry_t *foundentry; /* NO CLEAN UP! */
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews int addr_bucket;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_sockaddr_t sockaddr;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews name = a6ctx->arg;
48f929d315bafeeffe0a37082ab4c9661a928c39Mark Andrews INSIST(DNS_ADBNAME_VALID(name));
48f929d315bafeeffe0a37082ab4c9661a928c39Mark Andrews adb = name->adb;
48f929d315bafeeffe0a37082ab4c9661a928c39Mark Andrews INSIST(DNS_ADB_VALID(adb));
48f929d315bafeeffe0a37082ab4c9661a928c39Mark Andrews
48f929d315bafeeffe0a37082ab4c9661a928c39Mark Andrews addr_bucket = DNS_ADB_INVALIDBUCKET;
48f929d315bafeeffe0a37082ab4c9661a928c39Mark Andrews
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews DP(ENTER_LEVEL, "ENTER: import_a6() name %p", name);
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews nh = new_adbnamehook(adb, NULL);
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater if (nh == NULL) {
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews name->partial_result |= DNS_ADBFIND_INET6; /* clear for AAAA */
a1bc94109313bf4ebb6e6ff655d71d45582d2e43Mark Andrews goto fail;
a1bc94109313bf4ebb6e6ff655d71d45582d2e43Mark Andrews }
a1bc94109313bf4ebb6e6ff655d71d45582d2e43Mark Andrews
a1bc94109313bf4ebb6e6ff655d71d45582d2e43Mark Andrews isc_sockaddr_fromin6(&sockaddr, &a6ctx->in6addr, 0);
a1bc94109313bf4ebb6e6ff655d71d45582d2e43Mark Andrews
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson foundentry = find_entry_and_lock(adb, &sockaddr, &addr_bucket);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson if (foundentry == NULL) {
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson dns_adbentry_t *entry;
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson entry = new_adbentry(adb);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson if (entry == NULL) {
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson name->partial_result |= DNS_ADBFIND_INET6;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington goto fail;
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson }
80dd46d7aab16c42a8c1acf6156c95406a9f20a4Mark Andrews
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington entry->sockaddr = sockaddr;
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson entry->refcnt = 1;
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson nh->entry = entry;
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater link_entry(adb, addr_bucket, entry);
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson } else {
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson foundentry->refcnt++;
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson nh->entry = foundentry;
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson }
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson
9c566a852f31c3a5d0b9d6eaf11463114339c01dAndreas Gustafsson ISC_LIST_APPEND(name->v6, nh, plink);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington nh = NULL;
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington fail:
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington DP(NCACHE_LEVEL, "expire_v6 set to MIN(%u,%u) in import_v6",
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington name->expire_v6, a6ctx->expiration);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington name->expire_v6 = ISC_MIN(name->expire_v6, a6ctx->expiration);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews name->flags |= NAME_NEEDS_POKE;
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews if (nh != NULL)
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews free_adbnamehook(adb, &nh);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews if (addr_bucket != DNS_ADB_INVALIDBUCKET)
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews UNLOCK(&adb->entrylocks[addr_bucket]);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews}
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews/*
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * Requires the name's bucket be locked.
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews */
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrewsstatic isc_boolean_t
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrewskill_name(dns_adbname_t **n, isc_eventtype_t ev) {
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews dns_adbname_t *name;
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews isc_boolean_t result = ISC_FALSE;
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews isc_boolean_t result4, result6;
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews dns_adb_t *adb;
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews INSIST(n != NULL);
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington name = *n;
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington *n = NULL;
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington INSIST(DNS_ADBNAME_VALID(name));
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington adb = name->adb;
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington INSIST(DNS_ADB_VALID(adb));
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington DP(DEF_LEVEL, "killing name %p", name);
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington /*
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington * If we're dead already, just check to see if we should go
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington * away now or not.
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington */
a5c077e40c784cf9e25c95a1ab94db2faab04ae9Brian Wellington if (NAME_DEAD(name) && !NAME_FETCH(name)) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews result = unlink_name(adb, name);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews free_adbname(adb, &name);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews if (result)
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews result = dec_adb_irefcnt(adb);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews return (result);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews }
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews /*
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * Clean up the name's various lists. These two are destructive
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * in that they will always empty the list.
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews */
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews clean_finds_at_name(name, ev, DNS_ADBFIND_ADDRESSMASK);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews result4 = clean_namehooks(adb, &name->v4);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews result6 = clean_namehooks(adb, &name->v6);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews clean_target(adb, &name->target);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews result = ISC_TF(result4 || result6);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews /*
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * If fetches are running, cancel them. If none are running, we can
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews * just kill the name here.
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews */
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews if (!NAME_FETCH(name)) {
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews INSIST(result == ISC_FALSE);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews result = unlink_name(adb, name);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews free_adbname(adb, &name);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews if (result)
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews result = dec_adb_irefcnt(adb);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews } else {
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews name->flags |= NAME_IS_DEAD;
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews cancel_fetches_at_name(name);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews }
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews return (result);
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews}
3f42cf2f3e4dc7e740b4609ba7d7430292348f2bMark Andrews
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews/*
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews * Requires the name's bucket be locked and no entry buckets be locked.
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews */
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewsstatic isc_boolean_t
508f61f8d699c46f962b682f388e54b446a7194dMark Andrewscheck_expire_namehooks(dns_adbname_t *name, isc_stdtime_t now,
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews isc_boolean_t overmem)
508f61f8d699c46f962b682f388e54b446a7194dMark Andrews{
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews dns_adb_t *adb;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_boolean_t expire;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_boolean_t result4 = ISC_FALSE;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_boolean_t result6 = ISC_FALSE;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews INSIST(DNS_ADBNAME_VALID(name));
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews adb = name->adb;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews INSIST(DNS_ADB_VALID(adb));
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (overmem) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_uint32_t val;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews isc_random_get(&val);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews expire = ISC_TF((val % 4) == 0);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews } else
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews expire = ISC_FALSE;
2c15fcdeac4c2402258867fbac24d7475ef98259Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews /*
2f012d936b5ccdf6520c96a4de23721dc58a2221Automatic Updater * Check to see if we need to remove the v4 addresses
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews */
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews if (!NAME_FETCH_V4(name) &&
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews (expire || EXPIRE_OK(name->expire_v4, now))) {
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews if (NAME_HAS_V4(name)) {
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews DP(DEF_LEVEL, "expiring v4 for name %p", name);
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews result4 = clean_namehooks(adb, &name->v4);
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews name->partial_result &= ~DNS_ADBFIND_INET;
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews }
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews name->expire_v4 = INT_MAX;
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews name->fetch_err = FIND_ERR_UNEXPECTED;
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews }
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews /*
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews * Check to see if we need to remove the v6 addresses
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews */
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews if (!NAME_FETCH_V6(name) &&
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews (expire || EXPIRE_OK(name->expire_v6, now))) {
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews if (NAME_HAS_V6(name)) {
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews DP(DEF_LEVEL, "expiring v6 for name %p", name);
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews result6 = clean_namehooks(adb, &name->v6);
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews name->partial_result &= ~DNS_ADBFIND_INET6;
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews }
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews name->expire_v6 = INT_MAX;
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews name->fetch6_err = FIND_ERR_UNEXPECTED;
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews }
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews /*
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews * Check to see if we need to remove the alias target.
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews */
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews if (expire || EXPIRE_OK(name->expire_target, now)) {
dc6da18ccbb808d21f123cc6bda399b44ad11445Mark Andrews clean_target(adb, &name->target);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson name->expire_target = INT_MAX;
6fcfd0c35d3fd6aea3d36ad002b68e59ac62fdc7Brian Wellington }
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson return (ISC_TF(result4 || result6));
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson}
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Requires the name's bucket be locked.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
2674e1a455d4f71de09b2b60e7a8304b9a305588Mark Andrewsstatic inline void
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonlink_name(dns_adb_t *adb, int bucket, dns_adbname_t *name) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington INSIST(name->lock_bucket == DNS_ADB_INVALIDBUCKET);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington ISC_LIST_PREPEND(adb->names[bucket], name, plink);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington name->lock_bucket = bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington adb->name_refcnt[bucket]++;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington}
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * Requires the name's bucket be locked.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic inline isc_boolean_t
a12d9cfa59b5981c52e1aaafedf652d5128f3448Brian Wellingtonunlink_name(dns_adb_t *adb, dns_adbname_t *name) {
6c7e680943ccdb75f23b050a7bc5ac0825e5244aMark Andrews int bucket;
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson isc_boolean_t result = ISC_FALSE;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews bucket = name->lock_bucket;
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews INSIST(bucket != DNS_ADB_INVALIDBUCKET);
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews ISC_LIST_UNLINK(adb->names[bucket], name, plink);
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews name->lock_bucket = DNS_ADB_INVALIDBUCKET;
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews INSIST(adb->name_refcnt[bucket] > 0);
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews adb->name_refcnt[bucket]--;
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews if (adb->name_sd[bucket] && adb->name_refcnt[bucket] == 0)
87f4715d6c0a22f3449eb3291c91aa45ba86c955Mark Andrews result = ISC_TRUE;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington return (result);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington}
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
17dba29ba5db791976e505114baee53a1dde88aaBrian Wellington/*
bb56f556f520621bcab33688c31d655953774adcAndreas Gustafsson * Requires the entry's bucket be locked.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic inline void
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonlink_entry(dns_adb_t *adb, int bucket, dns_adbentry_t *entry) {
17dba29ba5db791976e505114baee53a1dde88aaBrian Wellington ISC_LIST_PREPEND(adb->entries[bucket], entry, plink);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson entry->lock_bucket = bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington adb->entry_refcnt[bucket]++;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington}
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington/*
cb9aa603ab2d019032e1b7b2d274e69adf0980f8Michael Graff * Requires the entry's bucket be locked.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonstatic inline isc_boolean_t
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellingtonunlink_entry(dns_adb_t *adb, dns_adbentry_t *entry) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington int bucket;
cb9aa603ab2d019032e1b7b2d274e69adf0980f8Michael Graff isc_boolean_t result = ISC_FALSE;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington bucket = entry->lock_bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington INSIST(bucket != DNS_ADB_INVALIDBUCKET);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
cb9aa603ab2d019032e1b7b2d274e69adf0980f8Michael Graff ISC_LIST_UNLINK(adb->entries[bucket], entry, plink);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington INSIST(adb->entry_refcnt[bucket] > 0);
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington adb->entry_refcnt[bucket]--;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington if (adb->entry_sd[bucket] && adb->entry_refcnt[bucket] == 0)
cb9aa603ab2d019032e1b7b2d274e69adf0980f8Michael Graff result = ISC_TRUE;
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson return (result);
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson}
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafssonstatic inline void
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrewsviolate_locking_hierarchy(isc_mutex_t *have, isc_mutex_t *want) {
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson if (isc_mutex_trylock(want) != ISC_R_SUCCESS) {
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson UNLOCK(have);
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson LOCK(want);
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson LOCK(have);
1f1d36a87b65186d9f89aac7f456ab1fd2a39ef6Andreas Gustafsson }
62700b67eb8abb7d13f9c3c1bc4b60a1477d35d8Mark Andrews}
bb60abb44549428414cd55a022f2b8cc4488f7adAndreas Gustafsson
476386968b1f287a695f73c48862e961011af99bMark Andrews/*
b500de3be9ba5318da157364bf9fbbda5f88f203Mark Andrews * The ADB _MUST_ be locked before calling. Also, exit conditions must be
476386968b1f287a695f73c48862e961011af99bMark Andrews * checked after calling this function.
476386968b1f287a695f73c48862e961011af99bMark Andrews */
476386968b1f287a695f73c48862e961011af99bMark Andrewsstatic isc_boolean_t
476386968b1f287a695f73c48862e961011af99bMark Andrewsshutdown_names(dns_adb_t *adb) {
476386968b1f287a695f73c48862e961011af99bMark Andrews int bucket;
476386968b1f287a695f73c48862e961011af99bMark Andrews isc_boolean_t result = ISC_FALSE;
476386968b1f287a695f73c48862e961011af99bMark Andrews dns_adbname_t *name;
476386968b1f287a695f73c48862e961011af99bMark Andrews dns_adbname_t *next_name;
476386968b1f287a695f73c48862e961011af99bMark Andrews
476386968b1f287a695f73c48862e961011af99bMark Andrews for (bucket = 0; bucket < NBUCKETS; bucket++) {
476386968b1f287a695f73c48862e961011af99bMark Andrews LOCK(&adb->namelocks[bucket]);
476386968b1f287a695f73c48862e961011af99bMark Andrews adb->name_sd[bucket] = ISC_TRUE;
476386968b1f287a695f73c48862e961011af99bMark Andrews
ce28ea0f2f8a6fbbafb8944dbe86cd0b98689b84Tatuya JINMEI 神明達哉 name = ISC_LIST_HEAD(adb->names[bucket]);
476386968b1f287a695f73c48862e961011af99bMark Andrews if (name == NULL) {
476386968b1f287a695f73c48862e961011af99bMark Andrews /*
476386968b1f287a695f73c48862e961011af99bMark Andrews * This bucket has no names. We must decrement the
476386968b1f287a695f73c48862e961011af99bMark Andrews * irefcnt ourselves, since it will not be
476386968b1f287a695f73c48862e961011af99bMark Andrews * automatically triggered by a name being unlinked.
476386968b1f287a695f73c48862e961011af99bMark Andrews */
476386968b1f287a695f73c48862e961011af99bMark Andrews INSIST(result == ISC_FALSE);
476386968b1f287a695f73c48862e961011af99bMark Andrews result = dec_adb_irefcnt(adb);
476386968b1f287a695f73c48862e961011af99bMark Andrews } else {
476386968b1f287a695f73c48862e961011af99bMark Andrews /*
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * Run through the list. For each name, clean up finds
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * found there, and cancel any fetches running. When
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * all the fetches are canceled, the name will destroy
a45a6ea2b03448751d7c44931e8ac7666e7cc2ceMark Andrews * itself.
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson */
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence while (name != NULL) {
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson next_name = ISC_LIST_NEXT(name, plink);
1adb2e87a20a480e640385609c9652dac04c7dffAndreas Gustafsson INSIST(result == ISC_FALSE);
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson result = kill_name(&name,
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson DNS_EVENT_ADBSHUTDOWN);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff name = next_name;
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson }
6017f424ee3c02d7f22132c77576ea38542fa949Andreas Gustafsson }
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews UNLOCK(&adb->namelocks[bucket]);
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews }
45e1bd63587102c3bb361eaca42ee7b714fb3542Mark Andrews return (result);
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson}
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson/*
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * The ADB _MUST_ be locked before calling. Also, exit conditions must be
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington * checked after calling this function.
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington */
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafssonstatic isc_boolean_t
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafssonshutdown_entries(dns_adb_t *adb) {
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington int bucket;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington isc_boolean_t result = ISC_FALSE;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *entry;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington dns_adbentry_t *next_entry;
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington
90c099e88e9f16bfee9edee3ac1a51fc98843772Brian Wellington for (bucket = 0; bucket < NBUCKETS; bucket++) {
8bba70c48d74a389a075b9c80837161b4ed91e88Andreas Gustafsson LOCK(&adb->entrylocks[bucket]);
ff4322d44f8404683b6fb6c86a38a2bc14f6c083Andreas Gustafsson adb->entry_sd[bucket] = ISC_TRUE;
0cf9ce19cc05a60f85ec610106a983fe806ebb77Andreas Gustafsson
ff4322d44f8404683b6fb6c86a38a2bc14f6c083Andreas Gustafsson entry = ISC_LIST_HEAD(adb->entries[bucket]);
ff4322d44f8404683b6fb6c86a38a2bc14f6c083Andreas Gustafsson if (entry == NULL) {
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson /*
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson * This bucket has no entries. We must decrement the
6889d57aabc5f48eaee78894e20054215319b461Andreas Gustafsson * irefcnt ourselves, since it will not be
* automatically triggered by an entry being unlinked.
*/
result = dec_adb_irefcnt(adb);
} else {
/*
* Run through the list. Cleanup any entries not
* associated with names, and which are not in use.
*/
while (entry != NULL) {
next_entry = ISC_LIST_NEXT(entry, plink);
if (entry->refcnt == 0 &&
entry->expires != 0) {
result = unlink_entry(adb, entry);
free_adbentry(adb, &entry);
if (result)
result = dec_adb_irefcnt(adb);
}
entry = next_entry;
}
}
UNLOCK(&adb->entrylocks[bucket]);
}
return (result);
}
/*
* Name bucket must be locked
*/
static void
cancel_fetches_at_name(dns_adbname_t *name) {
dns_adbfetch6_t *fetch6;
if (NAME_FETCH_A(name))
dns_resolver_cancelfetch(name->fetch_a->fetch);
if (NAME_FETCH_AAAA(name))
dns_resolver_cancelfetch(name->fetch_aaaa->fetch);
fetch6 = ISC_LIST_HEAD(name->fetches_a6);
while (fetch6 != NULL) {
dns_resolver_cancelfetch(fetch6->fetch);
fetch6 = ISC_LIST_NEXT(fetch6, plink);
}
}
/*
* Assumes the name bucket is locked.
*/
static isc_boolean_t
clean_namehooks(dns_adb_t *adb, dns_adbnamehooklist_t *namehooks) {
dns_adbentry_t *entry;
dns_adbnamehook_t *namehook;
int addr_bucket;
isc_boolean_t result = ISC_FALSE;
addr_bucket = DNS_ADB_INVALIDBUCKET;
namehook = ISC_LIST_HEAD(*namehooks);
while (namehook != NULL) {
INSIST(DNS_ADBNAMEHOOK_VALID(namehook));
/*
* Clean up the entry if needed.
*/
entry = namehook->entry;
if (entry != NULL) {
INSIST(DNS_ADBENTRY_VALID(entry));
if (addr_bucket != entry->lock_bucket) {
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[addr_bucket]);
addr_bucket = entry->lock_bucket;
LOCK(&adb->entrylocks[addr_bucket]);
}
result = dec_entry_refcnt(adb, entry, ISC_FALSE);
}
/*
* Free the namehook
*/
namehook->entry = NULL;
ISC_LIST_UNLINK(*namehooks, namehook, plink);
free_adbnamehook(adb, &namehook);
namehook = ISC_LIST_HEAD(*namehooks);
}
if (addr_bucket != DNS_ADB_INVALIDBUCKET)
UNLOCK(&adb->entrylocks[addr_bucket]);
return (result);
}
static void
clean_target(dns_adb_t *adb, dns_name_t *target) {
if (dns_name_countlabels(target) > 0) {
dns_name_free(target, adb->mctx);
dns_name_init(target, NULL);
}
}
static isc_result_t
set_target(dns_adb_t *adb, dns_name_t *name, dns_name_t *fname,
dns_rdataset_t *rdataset, dns_name_t *target)
{
isc_result_t result;
dns_namereln_t namereln;
unsigned int nlabels, nbits;
int order;
dns_rdata_t rdata = DNS_RDATA_INIT;
dns_fixedname_t fixed1, fixed2;
dns_name_t *prefix, *new_target;
REQUIRE(dns_name_countlabels(target) == 0);
if (rdataset->type == dns_rdatatype_cname) {
dns_rdata_cname_t cname;
/*
* Copy the CNAME's target into the target name.
*/
result = dns_rdataset_first(rdataset);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &cname, NULL);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_name_dup(&cname.cname, adb->mctx, target);
dns_rdata_freestruct(&cname);
if (result != ISC_R_SUCCESS)
return (result);
} else {
dns_rdata_dname_t dname;
INSIST(rdataset->type == dns_rdatatype_dname);
namereln = dns_name_fullcompare(name, fname, &order,
&nlabels, &nbits);
INSIST(namereln == dns_namereln_subdomain);
/*
* Get the target name of the DNAME.
*/
result = dns_rdataset_first(rdataset);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdataset_current(rdataset, &rdata);
result = dns_rdata_tostruct(&rdata, &dname, NULL);
if (result != ISC_R_SUCCESS)
return (result);
/*
* Construct the new target name.
*/
dns_fixedname_init(&fixed1);
prefix = dns_fixedname_name(&fixed1);
dns_fixedname_init(&fixed2);
new_target = dns_fixedname_name(&fixed2);
result = dns_name_split(name, nlabels, nbits, prefix, NULL);
if (result != ISC_R_SUCCESS) {
dns_rdata_freestruct(&dname);
return (result);
}
result = dns_name_concatenate(prefix, &dname.dname, new_target,
NULL);
dns_rdata_freestruct(&dname);
if (result != ISC_R_SUCCESS)
return (result);
result = dns_name_dup(new_target, adb->mctx, target);
if (result != ISC_R_SUCCESS)
return (result);
}
return (ISC_R_SUCCESS);
}
/*
* Assumes nothing is locked, since this is called by the client.
*/
static void
event_free(isc_event_t *event) {
dns_adbfind_t *find;
INSIST(event != NULL);
find = event->ev_destroy_arg;
INSIST(DNS_ADBFIND_VALID(find));
LOCK(&find->lock);
find->flags |= FIND_EVENT_FREED;
event->ev_destroy_arg = NULL;
UNLOCK(&find->lock);
}
/*
* Assumes the name bucket is locked.
*/
static void
clean_finds_at_name(dns_adbname_t *name, isc_eventtype_t evtype,
unsigned int addrs)
{
isc_event_t *ev;
isc_task_t *task;
dns_adbfind_t *find;
dns_adbfind_t *next_find;
isc_boolean_t process;
unsigned int wanted, notify;
DP(ENTER_LEVEL,
"ENTER clean_finds_at_name, name %p, evtype %08x, addrs %08x",
name, evtype, addrs);
find = ISC_LIST_HEAD(name->finds);
while (find != NULL) {
LOCK(&find->lock);
next_find = ISC_LIST_NEXT(find, plink);
process = ISC_FALSE;
wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
notify = wanted & addrs;
switch (evtype) {
case DNS_EVENT_ADBMOREADDRESSES:
DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBMOREADDRESSES");
if ((notify) != 0) {
find->flags &= ~addrs;
process = ISC_TRUE;
}
break;
case DNS_EVENT_ADBNOMOREADDRESSES:
DP(ISC_LOG_DEBUG(3), "DNS_EVENT_ADBNOMOREADDRESSES");
find->flags &= ~addrs;
wanted = find->flags & DNS_ADBFIND_ADDRESSMASK;
if (wanted == 0)
process = ISC_TRUE;
break;
default:
find->flags &= ~addrs;
process = ISC_TRUE;
}
if (process) {
DP(DEF_LEVEL, "cfan: processing find %p", find);
/*
* Unlink the find from the name, letting the caller
* call dns_adb_destroyfind() on it to clean it up
* later.
*/
ISC_LIST_UNLINK(name->finds, find, plink);
find->adbname = NULL;
find->name_bucket = DNS_ADB_INVALIDBUCKET;
INSIST(!FIND_EVENTSENT(find));
ev = &find->event;
task = ev->ev_sender;
ev->ev_sender = find;
find->result_v4 = find_err_map[name->fetch_err];
find->result_v6 = find_err_map[name->fetch6_err];
ev->ev_type = evtype;
ev->ev_destroy = event_free;
ev->ev_destroy_arg = find;
DP(DEF_LEVEL,
"sending event %p to task %p for find %p",
ev, task, find);
isc_task_sendanddetach(&task, (isc_event_t **)&ev);
} else {
DP(DEF_LEVEL, "cfan: skipping find %p", find);
}
UNLOCK(&find->lock);
find = next_find;
}
DP(ENTER_LEVEL, "EXIT clean_finds_at_name, name %p", name);
}
static inline void
check_exit(dns_adb_t *adb) {
isc_event_t *event;
/*
* The caller must be holding the adb lock.
*/
if (adb->shutting_down) {
/*
* If there aren't any external references either, we're
* done. Send the control event to initiate shutdown.
*/
INSIST(!adb->cevent_sent); /* Sanity check. */
event = &adb->cevent;
isc_task_send(adb->task, &event);
adb->cevent_sent = ISC_TRUE;
}
}
static inline isc_boolean_t
dec_adb_irefcnt(dns_adb_t *adb) {
isc_event_t *event;
isc_task_t *etask;
isc_boolean_t result = ISC_FALSE;
LOCK(&adb->reflock);
INSIST(adb->irefcnt > 0);
adb->irefcnt--;
if (adb->irefcnt == 0) {
event = ISC_LIST_HEAD(adb->whenshutdown);
while (event != NULL) {
ISC_LIST_UNLINK(adb->whenshutdown, event, ev_link);
etask = event->ev_sender;
event->ev_sender = adb;
isc_task_sendanddetach(&etask, &event);
event = ISC_LIST_HEAD(adb->whenshutdown);
}
}
if (adb->irefcnt == 0 && adb->erefcnt == 0)
result = ISC_TRUE;
UNLOCK(&adb->reflock);
return (result);
}
static inline void
inc_adb_irefcnt(dns_adb_t *adb) {
LOCK(&adb->reflock);
adb->irefcnt++;
UNLOCK(&adb->reflock);
}
static inline void
inc_adb_erefcnt(dns_adb_t *adb) {
LOCK(&adb->reflock);
adb->erefcnt++;
UNLOCK(&adb->reflock);
}
static inline void
inc_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
int bucket;
bucket = entry->lock_bucket;
if (lock)
LOCK(&adb->entrylocks[bucket]);
entry->refcnt++;
if (lock)
UNLOCK(&adb->entrylocks[bucket]);
}
static inline isc_boolean_t
dec_entry_refcnt(dns_adb_t *adb, dns_adbentry_t *entry, isc_boolean_t lock) {
int bucket;
isc_boolean_t destroy_entry;
isc_boolean_t result = ISC_FALSE;
bucket = entry->lock_bucket;
if (lock)
LOCK(&adb->entrylocks[bucket]);
INSIST(entry->refcnt > 0);
entry->refcnt--;
destroy_entry = ISC_FALSE;
if (entry->refcnt == 0 &&
(adb->entry_sd[bucket] || entry->expires == 0)) {
destroy_entry = ISC_TRUE;
result = unlink_entry(adb, entry);
}
if (lock)
UNLOCK(&adb->entrylocks[bucket]);
if (!destroy_entry)
return (result);
entry->lock_bucket = DNS_ADB_INVALIDBUCKET;
free_adbentry(adb, &entry);
if (result)
result =dec_adb_irefcnt(adb);
return (result);
}
static inline dns_adbname_t *
new_adbname(dns_adb_t *adb, dns_name_t *dnsname) {
dns_adbname_t *name;
name = isc_mempool_get(adb->nmp);
if (name == NULL)
return (NULL);
dns_name_init(&name->name, NULL);
if (dns_name_dup(dnsname, adb->mctx, &name->name) != ISC_R_SUCCESS) {
isc_mempool_put(adb->nmp, name);
return (NULL);
}
dns_name_init(&name->target, NULL);
name->magic = DNS_ADBNAME_MAGIC;
name->adb = adb;
name->partial_result = 0;
name->flags = 0;
name->expire_v4 = INT_MAX;
name->expire_v6 = INT_MAX;
name->expire_target = INT_MAX;
name->chains = 0;
name->lock_bucket = DNS_ADB_INVALIDBUCKET;
ISC_LIST_INIT(name->v4);
ISC_LIST_INIT(name->v6);
name->fetch_a = NULL;
name->fetch_aaaa = NULL;
ISC_LIST_INIT(name->fetches_a6);
name->fetch_err = FIND_ERR_UNEXPECTED;
name->fetch6_err = FIND_ERR_UNEXPECTED;
ISC_LIST_INIT(name->finds);
ISC_LINK_INIT(name, plink);
return (name);
}
static inline void
free_adbname(dns_adb_t *adb, dns_adbname_t **name) {
dns_adbname_t *n;
INSIST(name != NULL && DNS_ADBNAME_VALID(*name));
n = *name;
*name = NULL;
INSIST(!NAME_HAS_V4(n));
INSIST(!NAME_HAS_V6(n));
INSIST(!NAME_FETCH(n));
INSIST(ISC_LIST_EMPTY(n->finds));
INSIST(!ISC_LINK_LINKED(n, plink));
INSIST(n->lock_bucket == DNS_ADB_INVALIDBUCKET);
INSIST(n->adb == adb);
n->magic = 0;
dns_name_free(&n->name, adb->mctx);
isc_mempool_put(adb->nmp, n);
}
static inline dns_adbnamehook_t *
new_adbnamehook(dns_adb_t *adb, dns_adbentry_t *entry) {
dns_adbnamehook_t *nh;
nh = isc_mempool_get(adb->nhmp);
if (nh == NULL)
return (NULL);
nh->magic = DNS_ADBNAMEHOOK_MAGIC;
nh->entry = entry;
ISC_LINK_INIT(nh, plink);
return (nh);
}
static inline void
free_adbnamehook(dns_adb_t *adb, dns_adbnamehook_t **namehook) {
dns_adbnamehook_t *nh;
INSIST(namehook != NULL && DNS_ADBNAMEHOOK_VALID(*namehook));
nh = *namehook;
*namehook = NULL;
INSIST(nh->entry == NULL);
INSIST(!ISC_LINK_LINKED(nh, plink));
nh->magic = 0;
isc_mempool_put(adb->nhmp, nh);
}
static inline dns_adbzoneinfo_t *
new_adbzoneinfo(dns_adb_t *adb, dns_name_t *zone) {
dns_adbzoneinfo_t *zi;
zi = isc_mempool_get(adb->zimp);
if (zi == NULL)
return (NULL);
dns_name_init(&zi->zone, NULL);
if (dns_name_dup(zone, adb->mctx, &zi->zone) != ISC_R_SUCCESS) {
isc_mempool_put(adb->zimp, zi);
return (NULL);
}
zi->magic = DNS_ADBZONEINFO_MAGIC;
zi->lame_timer = 0;
ISC_LINK_INIT(zi, plink);
return (zi);
}
static inline void
free_adbzoneinfo(dns_adb_t *adb, dns_adbzoneinfo_t **zoneinfo) {
dns_adbzoneinfo_t *zi;
INSIST(zoneinfo != NULL && DNS_ADBZONEINFO_VALID(*zoneinfo));
zi = *zoneinfo;
*zoneinfo = NULL;
INSIST(!ISC_LINK_LINKED(zi, plink));
dns_name_free(&zi->zone, adb->mctx);
zi->magic = 0;
isc_mempool_put(adb->zimp, zi);
}
static inline dns_adbentry_t *
new_adbentry(dns_adb_t *adb) {
dns_adbentry_t *e;
isc_uint32_t r;
e = isc_mempool_get(adb->emp);
if (e == NULL)
return (NULL);
e->magic = DNS_ADBENTRY_MAGIC;
e->lock_bucket = DNS_ADB_INVALIDBUCKET;
e->refcnt = 0;
e->flags = 0;
isc_random_get(&r);
e->srtt = (r & 0x1f) + 1;
e->expires = 0;
ISC_LIST_INIT(e->zoneinfo);
ISC_LINK_INIT(e, plink);
return (e);
}
static inline void
free_adbentry(dns_adb_t *adb, dns_adbentry_t **entry) {
dns_adbentry_t *e;
dns_adbzoneinfo_t *zi;
INSIST(entry != NULL && DNS_ADBENTRY_VALID(*entry));
e = *entry;
*entry = NULL;
INSIST(e->lock_bucket == DNS_ADB_INVALIDBUCKET);
INSIST(e->refcnt == 0);
INSIST(!ISC_LINK_LINKED(e, plink));
e->magic = 0;
zi = ISC_LIST_HEAD(e->zoneinfo);
while (zi != NULL) {
ISC_LIST_UNLINK(e->zoneinfo, zi, plink);
free_adbzoneinfo(adb, &zi);
zi = ISC_LIST_HEAD(e->zoneinfo);
}
isc_mempool_put(adb->emp, e);
}
static inline dns_adbfind_t *
new_adbfind(dns_adb_t *adb) {
dns_adbfind_t *h;
isc_result_t result;
h = isc_mempool_get(adb->ahmp);
if (h == NULL)
return (NULL);
/*
* Public members.
*/
h->magic = 0;
h->adb = adb;
h->partial_result = 0;
h->options = 0;
h->flags = 0;
h->result_v4 = ISC_R_UNEXPECTED;
h->result_v6 = ISC_R_UNEXPECTED;
ISC_LINK_INIT(h, publink);
ISC_LINK_INIT(h, plink);
ISC_LIST_INIT(h->list);
h->adbname = NULL;
h->name_bucket = DNS_ADB_INVALIDBUCKET;
/*
* private members
*/
result = isc_mutex_init(&h->lock);
if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_mutex_init failed in new_adbfind()");
isc_mempool_put(adb->ahmp, h);
return (NULL);
}
ISC_EVENT_INIT(&h->event, sizeof(isc_event_t), 0, 0, 0, NULL, NULL,
NULL, NULL, h);
inc_adb_irefcnt(adb);
h->magic = DNS_ADBFIND_MAGIC;
return (h);
}
static inline dns_adbfetch_t *
new_adbfetch(dns_adb_t *adb) {
dns_adbfetch_t *f;
f = isc_mempool_get(adb->afmp);
if (f == NULL)
return (NULL);
f->magic = 0;
f->namehook = NULL;
f->entry = NULL;
f->fetch = NULL;
f->namehook = new_adbnamehook(adb, NULL);
if (f->namehook == NULL)
goto err;
f->entry = new_adbentry(adb);
if (f->entry == NULL)
goto err;
dns_rdataset_init(&f->rdataset);
f->magic = DNS_ADBFETCH_MAGIC;
return (f);
err:
if (f->namehook != NULL)
free_adbnamehook(adb, &f->namehook);
if (f->entry != NULL)
free_adbentry(adb, &f->entry);
isc_mempool_put(adb->afmp, f);
return (NULL);
}
static inline void
free_adbfetch(dns_adb_t *adb, dns_adbfetch_t **fetch) {
dns_adbfetch_t *f;
INSIST(fetch != NULL && DNS_ADBFETCH_VALID(*fetch));
f = *fetch;
*fetch = NULL;
f->magic = 0;
if (f->namehook != NULL)
free_adbnamehook(adb, &f->namehook);
if (f->entry != NULL)
free_adbentry(adb, &f->entry);
if (dns_rdataset_isassociated(&f->rdataset))
dns_rdataset_disassociate(&f->rdataset);
isc_mempool_put(adb->afmp, f);
}
/*
* Caller must be holding the name lock.
*/
static isc_result_t
a6find(void *arg, dns_name_t *a6name, dns_rdatatype_t type, isc_stdtime_t now,
dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
{
dns_adbname_t *name;
dns_adb_t *adb;
isc_result_t result;
name = arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
result = dns_view_simplefind(adb->view, a6name, type, now,
DNS_DBFIND_GLUEOK, ISC_FALSE,
rdataset, sigrdataset);
if (result == DNS_R_GLUE)
result = ISC_R_SUCCESS;
return (result);
}
/*
* Caller must be holding the name lock.
*/
static void
a6missing(dns_a6context_t *a6ctx, dns_name_t *a6name) {
dns_adbname_t *name;
dns_adb_t *adb;
dns_adbfetch6_t *fetch;
isc_result_t result;
name = a6ctx->arg;
INSIST(DNS_ADBNAME_VALID(name));
adb = name->adb;
INSIST(DNS_ADB_VALID(adb));
fetch = new_adbfetch6(adb, name, a6ctx);
if (fetch == NULL) {
name->partial_result |= DNS_ADBFIND_INET6;
return;
}
result = dns_resolver_createfetch(adb->view->resolver, a6name,
dns_rdatatype_a6,
NULL, NULL, NULL, 0,
adb->task, fetch_callback_a6,
name, &fetch->rdataset, NULL,
&fetch->fetch);
if (result != ISC_R_SUCCESS)
goto cleanup;
name->chains = a6ctx->chains;
ISC_LIST_APPEND(name->fetches_a6, fetch, plink);
cleanup:
if (result != ISC_R_SUCCESS) {
free_adbfetch6(adb, &fetch);
name->partial_result |= DNS_ADBFIND_INET6;
}
}
static inline dns_adbfetch6_t *
new_adbfetch6(dns_adb_t *adb, dns_adbname_t *name, dns_a6context_t *a6ctx) {
dns_adbfetch6_t *f;
f = isc_mempool_get(adb->af6mp);
if (f == NULL)
return (NULL);
f->magic = 0;
f->namehook = NULL;
f->entry = NULL;
f->fetch = NULL;
f->flags = 0;
f->namehook = new_adbnamehook(adb, NULL);
if (f->namehook == NULL)
goto err;
f->entry = new_adbentry(adb);
if (f->entry == NULL)
goto err;
dns_rdataset_init(&f->rdataset);
dns_a6_init(&f->a6ctx, a6find, NULL, import_a6,
a6missing, name);
if (a6ctx != NULL)
dns_a6_copy(a6ctx, &f->a6ctx);
ISC_LINK_INIT(f, plink);
f->magic = DNS_ADBFETCH6_MAGIC;
return (f);
err:
if (f->namehook != NULL)
free_adbnamehook(adb, &f->namehook);
if (f->entry != NULL)
free_adbentry(adb, &f->entry);
isc_mempool_put(adb->af6mp, f);
return (NULL);
}
static inline void
free_adbfetch6(dns_adb_t *adb, dns_adbfetch6_t **fetch) {
dns_adbfetch6_t *f;
INSIST(fetch != NULL && DNS_ADBFETCH6_VALID(*fetch));
f = *fetch;
*fetch = NULL;
f->magic = 0;
if (f->namehook != NULL)
free_adbnamehook(adb, &f->namehook);
if (f->entry != NULL)
free_adbentry(adb, &f->entry);
if (dns_rdataset_isassociated(&f->rdataset))
dns_rdataset_disassociate(&f->rdataset);
isc_mempool_put(adb->af6mp, f);
}
static inline isc_boolean_t
free_adbfind(dns_adb_t *adb, dns_adbfind_t **findp) {
dns_adbfind_t *find;
INSIST(findp != NULL && DNS_ADBFIND_VALID(*findp));
find = *findp;
*findp = NULL;
INSIST(!FIND_HAS_ADDRS(find));
INSIST(!ISC_LINK_LINKED(find, publink));
INSIST(!ISC_LINK_LINKED(find, plink));
INSIST(find->name_bucket == DNS_ADB_INVALIDBUCKET);
INSIST(find->adbname == NULL);
find->magic = 0;
DESTROYLOCK(&find->lock);
isc_mempool_put(adb->ahmp, find);
return (dec_adb_irefcnt(adb));
}
/*
* Copy bits from the entry into the newly allocated addrinfo. The entry
* must be locked, and the reference count must be bumped up by one
* if this function returns a valid pointer.
*/
static inline dns_adbaddrinfo_t *
new_adbaddrinfo(dns_adb_t *adb, dns_adbentry_t *entry, in_port_t port) {
dns_adbaddrinfo_t *ai;
ai = isc_mempool_get(adb->aimp);
if (ai == NULL)
return (NULL);
ai->magic = DNS_ADBADDRINFO_MAGIC;
ai->sockaddr = entry->sockaddr;
isc_sockaddr_setport(&ai->sockaddr, port);
ai->srtt = entry->srtt;
ai->flags = entry->flags;
ai->entry = entry;
ISC_LINK_INIT(ai, publink);
return (ai);
}
static inline void
free_adbaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **ainfo) {
dns_adbaddrinfo_t *ai;
INSIST(ainfo != NULL && DNS_ADBADDRINFO_VALID(*ainfo));
ai = *ainfo;
*ainfo = NULL;
INSIST(ai->entry == NULL);
INSIST(!ISC_LINK_LINKED(ai, publink));
ai->magic = 0;
isc_mempool_put(adb->aimp, ai);
}
/*
* Search for the name. 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.
*/
static inline dns_adbname_t *
find_name_and_lock(dns_adb_t *adb, dns_name_t *name,
unsigned int options, int *bucketp)
{
dns_adbname_t *adbname;
int bucket;
bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
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)
&& STARTATROOT_MATCHES(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 isc_boolean_t
check_expire_name(dns_adbname_t **namep, isc_stdtime_t now) {
dns_adbname_t *name;
isc_result_t result = ISC_FALSE;
INSIST(namep != NULL && DNS_ADBNAME_VALID(*namep));
name = *namep;
if (NAME_HAS_V4(name) || NAME_HAS_V6(name))
return (result);
if (NAME_FETCH(name))
return (result);
if (!EXPIRE_OK(name->expire_v4, now))
return (result);
if (!EXPIRE_OK(name->expire_v6, now))
return (result);
if (!EXPIRE_OK(name->expire_target, now))
return (result);
/*
* The name is empty. Delete it.
*/
result = kill_name(&name, DNS_EVENT_ADBEXPIRED);
*namep = NULL;
/*
* Our caller, or one of its callers, will be calling check_exit() at
* some point, so we don't need to do it here.
*/
return (result);
}
/*
* Entry bucket must be locked; adb may be locked; no other locks held.
*/
static isc_boolean_t
check_expire_entry(dns_adb_t *adb, dns_adbentry_t **entryp, isc_stdtime_t now)
{
dns_adbentry_t *entry;
isc_boolean_t expire;
isc_boolean_t result = ISC_FALSE;
INSIST(entryp != NULL && DNS_ADBENTRY_VALID(*entryp));
entry = *entryp;
if (entry->refcnt != 0)
return (result);
if (adb->overmem) {
isc_uint32_t val;
isc_random_get(&val);
expire = ISC_TF((val % 4) == 0);
} else
expire = ISC_FALSE;
if (entry->expires == 0 || (! expire && entry->expires > now))
return (result);
/*
* The entry is not in use. Delete it.
*/
DP(DEF_LEVEL, "killing entry %p", entry);
INSIST(ISC_LINK_LINKED(entry, plink));
result = unlink_entry(adb, entry);
free_adbentry(adb, &entry);
if (result)
dec_adb_irefcnt(adb);
*entryp = NULL;
return (result);
}
/*
* ADB must be locked, and no other locks held.
*/
static isc_boolean_t
cleanup_names(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
dns_adbname_t *name;
dns_adbname_t *next_name;
isc_result_t result = ISC_FALSE;
DP(CLEAN_LEVEL, "cleaning name bucket %d", bucket);
LOCK(&adb->namelocks[bucket]);
if (adb->name_sd[bucket]) {
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
name = ISC_LIST_HEAD(adb->names[bucket]);
while (name != NULL) {
next_name = ISC_LIST_NEXT(name, plink);
INSIST(result == ISC_FALSE);
result = check_expire_namehooks(name, now, adb->overmem);
if (!result)
result = check_expire_name(&name, now);
name = next_name;
}
UNLOCK(&adb->namelocks[bucket]);
return (result);
}
/*
* ADB must be locked, and no other locks held.
*/
static isc_boolean_t
cleanup_entries(dns_adb_t *adb, int bucket, isc_stdtime_t now) {
dns_adbentry_t *entry, *next_entry;
isc_boolean_t result = ISC_FALSE;
DP(CLEAN_LEVEL, "cleaning entry bucket %d", bucket);
LOCK(&adb->entrylocks[bucket]);
entry = ISC_LIST_HEAD(adb->entries[bucket]);
while (entry != NULL) {
next_entry = ISC_LIST_NEXT(entry, plink);
INSIST(result == ISC_FALSE);
result = check_expire_entry(adb, &entry, now);
entry = next_entry;
}
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
static void
timer_cleanup(isc_task_t *task, isc_event_t *ev) {
dns_adb_t *adb;
isc_stdtime_t now;
unsigned int i;
isc_interval_t interval;
UNUSED(task);
adb = ev->ev_arg;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
isc_stdtime_get(&now);
for (i = 0; i < CLEAN_BUCKETS; i++) {
/*
* Call our cleanup routines.
*/
INSIST(cleanup_names(adb, adb->next_cleanbucket, now) ==
ISC_FALSE);
INSIST(cleanup_entries(adb, adb->next_cleanbucket, now) ==
ISC_FALSE);
/*
* Set the next bucket to be cleaned.
*/
adb->next_cleanbucket++;
if (adb->next_cleanbucket >= NBUCKETS) {
adb->next_cleanbucket = 0;
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE);
#endif
}
}
/*
* Reset the timer.
* XXXDCL isc_timer_reset might return ISC_R_UNEXPECTED or
* ISC_R_NOMEMORY, but it isn't clear what could be done here
* if either one of those things happened.
*/
interval = adb->tick_interval;
if (adb->overmem)
isc_interval_set(&interval, 0, 1);
(void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
&interval, ISC_FALSE);
UNLOCK(&adb->lock);
isc_event_free(&ev);
}
static void
destroy(dns_adb_t *adb) {
adb->magic = 0;
/*
* The timer is already dead, from the task's shutdown callback.
*/
isc_task_detach(&adb->task);
isc_mempool_destroy(&adb->nmp);
isc_mempool_destroy(&adb->nhmp);
isc_mempool_destroy(&adb->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);
DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
DESTROYLOCK(&adb->reflock);
DESTROYLOCK(&adb->lock);
DESTROYLOCK(&adb->mplock);
isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
}
/*
* Public functions.
*/
isc_result_t
dns_adb_create(isc_mem_t *mem, dns_view_t *view, isc_timermgr_t *timermgr,
isc_taskmgr_t *taskmgr, dns_adb_t **newadb)
{
dns_adb_t *adb;
isc_result_t result;
int i;
REQUIRE(mem != NULL);
REQUIRE(view != NULL);
REQUIRE(timermgr != NULL);
REQUIRE(taskmgr != NULL);
REQUIRE(newadb != NULL && *newadb == NULL);
adb = isc_mem_get(mem, sizeof(dns_adb_t));
if (adb == NULL)
return (ISC_R_NOMEMORY);
/*
* Initialize things here that cannot fail, and especially things
* that must be NULL for the error return to work properly.
*/
adb->magic = 0;
adb->erefcnt = 1;
adb->irefcnt = 0;
adb->nmp = NULL;
adb->nhmp = NULL;
adb->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 = NULL;
adb->view = view;
adb->timermgr = timermgr;
adb->taskmgr = taskmgr;
adb->next_cleanbucket = 0;
ISC_EVENT_INIT(&adb->cevent, sizeof(adb->cevent), 0, NULL,
DNS_EVENT_ADBCONTROL, shutdown_task, adb,
adb, NULL, NULL);
adb->cevent_sent = ISC_FALSE;
adb->shutting_down = ISC_FALSE;
adb->overmem = ISC_FALSE;
ISC_LIST_INIT(adb->whenshutdown);
isc_mem_attach(mem, &adb->mctx);
result = isc_mutex_init(&adb->lock);
if (result != ISC_R_SUCCESS)
goto fail0b;
result = isc_mutex_init(&adb->mplock);
if (result != ISC_R_SUCCESS)
goto fail0c;
result = isc_mutex_init(&adb->reflock);
if (result != ISC_R_SUCCESS)
goto fail0d;
/*
* Initialize the bucket locks for names and elements.
* May as well initialize the list heads, too.
*/
result = isc_mutexblock_init(adb->namelocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail1;
for (i = 0; i < NBUCKETS; i++) {
ISC_LIST_INIT(adb->names[i]);
adb->name_sd[i] = ISC_FALSE;
adb->name_refcnt[i] = 0;
adb->irefcnt++;
}
for (i = 0; i < NBUCKETS; i++) {
ISC_LIST_INIT(adb->entries[i]);
adb->entry_sd[i] = ISC_FALSE;
adb->entry_refcnt[i] = 0;
adb->irefcnt++;
}
result = isc_mutexblock_init(adb->entrylocks, NBUCKETS);
if (result != ISC_R_SUCCESS)
goto fail2;
/*
* Memory pools
*/
#define MPINIT(t, p, n) do { \
result = isc_mempool_create(mem, sizeof(t), &(p)); \
if (result != ISC_R_SUCCESS) \
goto fail3; \
isc_mempool_setfreemax((p), FREE_ITEMS); \
isc_mempool_setfillcount((p), FILL_COUNT); \
isc_mempool_setname((p), n); \
isc_mempool_associatelock((p), &adb->mplock); \
} while (0)
MPINIT(dns_adbname_t, adb->nmp, "adbname");
MPINIT(dns_adbnamehook_t, adb->nhmp, "adbnamehook");
MPINIT(dns_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(ISC_LOG_DEBUG(5), "cleaning interval for adb: "
"%u buckets every %u seconds, %u buckets in system, %u cl.interval",
CLEAN_BUCKETS, CLEAN_SECONDS, NBUCKETS, CLEAN_PERIOD);
/*
* Normal return.
*/
adb->magic = DNS_ADB_MAGIC;
*newadb = adb;
return (ISC_R_SUCCESS);
fail3:
if (adb->task != NULL)
isc_task_detach(&adb->task);
if (adb->timer != NULL)
isc_timer_detach(&adb->timer);
/* clean up entrylocks */
DESTROYMUTEXBLOCK(adb->entrylocks, NBUCKETS);
fail2: /* clean up namelocks */
DESTROYMUTEXBLOCK(adb->namelocks, NBUCKETS);
fail1: /* clean up only allocated memory */
if (adb->nmp != NULL)
isc_mempool_destroy(&adb->nmp);
if (adb->nhmp != NULL)
isc_mempool_destroy(&adb->nhmp);
if (adb->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->reflock);
fail0d:
DESTROYLOCK(&adb->mplock);
fail0c:
DESTROYLOCK(&adb->lock);
fail0b:
isc_mem_putanddetach(&adb->mctx, adb, sizeof(dns_adb_t));
return (result);
}
void
dns_adb_attach(dns_adb_t *adb, dns_adb_t **adbx) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(adbx != NULL && *adbx == NULL);
inc_adb_erefcnt(adb);
*adbx = adb;
}
void
dns_adb_detach(dns_adb_t **adbx) {
dns_adb_t *adb;
isc_boolean_t need_exit_check;
REQUIRE(adbx != NULL && DNS_ADB_VALID(*adbx));
adb = *adbx;
*adbx = NULL;
INSIST(adb->erefcnt > 0);
LOCK(&adb->reflock);
adb->erefcnt--;
need_exit_check = ISC_TF(adb->erefcnt == 0 && adb->irefcnt == 0);
UNLOCK(&adb->reflock);
if (need_exit_check) {
LOCK(&adb->lock);
INSIST(adb->shutting_down);
check_exit(adb);
UNLOCK(&adb->lock);
}
}
void
dns_adb_whenshutdown(dns_adb_t *adb, isc_task_t *task, isc_event_t **eventp) {
isc_task_t *clone;
isc_event_t *event;
isc_boolean_t zeroirefcnt = ISC_FALSE;
/*
* Send '*eventp' to 'task' when 'adb' has shutdown.
*/
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(eventp != NULL);
event = *eventp;
*eventp = NULL;
LOCK(&adb->lock);
LOCK(&adb->reflock);
zeroirefcnt = ISC_TF(adb->irefcnt == 0);
if (adb->shutting_down && zeroirefcnt &&
isc_mempool_getallocated(adb->ahmp) == 0) {
/*
* We're already shutdown. Send the event.
*/
event->ev_sender = adb;
isc_task_send(task, &event);
} else {
clone = NULL;
isc_task_attach(task, &clone);
event->ev_sender = clone;
ISC_LIST_APPEND(adb->whenshutdown, event, ev_link);
}
UNLOCK(&adb->reflock);
UNLOCK(&adb->lock);
}
void
dns_adb_shutdown(dns_adb_t *adb) {
isc_boolean_t need_check_exit;
/*
* Shutdown 'adb'.
*/
LOCK(&adb->lock);
if (!adb->shutting_down) {
adb->shutting_down = ISC_TRUE;
isc_mem_setwater(adb->mctx, water, adb, 0, 0);
need_check_exit = shutdown_names(adb);
if (!need_check_exit)
need_check_exit = shutdown_entries(adb);
if (need_check_exit)
check_exit(adb);
}
UNLOCK(&adb->lock);
}
isc_result_t
dns_adb_createfind(dns_adb_t *adb, isc_task_t *task, isc_taskaction_t action,
void *arg, dns_name_t *name, dns_name_t *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");
INSIST(free_adbfind(adb, &find) == ISC_FALSE);
result = ISC_R_SHUTTINGDOWN;
goto out;
}
/*
* Nothing found. Allocate a new adbname structure for this name.
*/
if (adbname == NULL) {
adbname = new_adbname(adb, name);
if (adbname == NULL) {
INSIST(free_adbfind(adb, &find) == ISC_FALSE);
result = ISC_R_NOMEMORY;
goto out;
}
link_name(adb, bucket, adbname);
if (FIND_HINTOK(find))
adbname->flags |= NAME_HINT_OK;
if (FIND_GLUEOK(find))
adbname->flags |= NAME_GLUE_OK;
if (FIND_STARTATROOT(find))
adbname->flags |= NAME_STARTATROOT;
}
/*
* Expire old entries, etc.
*/
INSIST(check_expire_namehooks(adbname, now, adb->overmem) == ISC_FALSE);
/*
* Do we know that the name is an alias?
*/
if (!EXPIRE_OK(adbname->expire_target, now)) {
/*
* Yes, it is.
*/
DP(DEF_LEVEL,
"dns_adb_createfind: name %p is an alias (cached)",
adbname);
alias = ISC_TRUE;
goto post_copy;
}
/*
* Try to populate the name from the database and/or
* start fetches. First try looking for an A record
* in the database.
*/
if (!NAME_HAS_V4(adbname) && !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));
INSIST(dec_entry_refcnt(adb, entry, ISC_TRUE) == ISC_FALSE);
free_adbaddrinfo(adb, &ai);
ai = ISC_LIST_HEAD(find->list);
}
/*
* WARNING: The find is freed with the adb locked. This is done
* to avoid a race condition where we free the find, some other
* thread tests to see if it should be destroyed, detects it should
* be, destroys it, and then we try to lock it for our check, but the
* lock is destroyed.
*/
LOCK(&adb->lock);
if (free_adbfind(adb, &find))
check_exit(adb);
UNLOCK(&adb->lock);
}
void
dns_adb_cancelfind(dns_adbfind_t *find) {
isc_event_t *ev;
isc_task_t *task;
dns_adb_t *adb;
int bucket;
int unlock_bucket;
LOCK(&find->lock);
DP(DEF_LEVEL, "dns_adb_cancelfind on find %p", find);
adb = find->adb;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(!FIND_EVENTFREED(find));
REQUIRE(FIND_WANTEVENT(find));
bucket = find->name_bucket;
if (bucket == DNS_ADB_INVALIDBUCKET)
goto cleanup;
/*
* We need to get the adbname's lock to unlink the find.
*/
unlock_bucket = bucket;
violate_locking_hierarchy(&find->lock, &adb->namelocks[unlock_bucket]);
bucket = find->name_bucket;
if (bucket != DNS_ADB_INVALIDBUCKET) {
ISC_LIST_UNLINK(find->adbname->finds, find, plink);
find->adbname = NULL;
find->name_bucket = DNS_ADB_INVALIDBUCKET;
}
UNLOCK(&adb->namelocks[unlock_bucket]);
bucket = DNS_ADB_INVALIDBUCKET;
cleanup:
if (!FIND_EVENTSENT(find)) {
ev = &find->event;
task = ev->ev_sender;
ev->ev_sender = find;
ev->ev_type = DNS_EVENT_ADBCANCELED;
ev->ev_destroy = event_free;
ev->ev_destroy_arg = find;
find->result_v4 = ISC_R_CANCELED;
find->result_v6 = ISC_R_CANCELED;
DP(DEF_LEVEL, "sending event %p to task %p for find %p",
ev, task, find);
isc_task_sendanddetach(&task, (isc_event_t **)&ev);
}
UNLOCK(&find->lock);
}
void
dns_adb_dump(dns_adb_t *adb, FILE *f) {
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(f != NULL);
/*
* Lock the adb itself, lock all the name buckets, then lock all
* the entry buckets. This should put the adb into a state where
* nothing can change, so we can iterate through everything and
* print at our leisure.
*/
LOCK(&adb->lock);
dump_adb(adb, f, ISC_FALSE);
UNLOCK(&adb->lock);
}
static void
dump_ttl(FILE *f, const char *legend, isc_stdtime_t value, isc_stdtime_t now) {
if (value == INT_MAX)
return;
fprintf(f, " [%s TTL %d]", legend, value - now);
}
static void
dump_adb(dns_adb_t *adb, FILE *f, isc_boolean_t debug) {
int i;
dns_adbname_t *name;
isc_stdtime_t now;
isc_stdtime_get(&now);
fprintf(f, ";\n; Address database dump\n;\n");
if (debug)
fprintf(f, "; addr %p, erefcnt %u, irefcnt %u, finds out %u\n",
adb, adb->erefcnt, adb->irefcnt,
isc_mempool_getallocated(adb->nhmp));
for (i = 0; i < NBUCKETS; i++)
LOCK(&adb->namelocks[i]);
for (i = 0; i < NBUCKETS; i++)
LOCK(&adb->entrylocks[i]);
/*
* Dump the names
*/
for (i = 0; i < NBUCKETS; i++) {
name = ISC_LIST_HEAD(adb->names[i]);
if (name == NULL)
continue;
if (debug)
fprintf(f, "; bucket %d\n", i);
for (;
name != NULL;
name = ISC_LIST_NEXT(name, plink))
{
if (debug)
fprintf(f, "; name %p (flags %08x)\n",
name, name->flags);
fprintf(f, "; ");
print_dns_name(f, &name->name);
if (dns_name_countlabels(&name->target) > 0) {
fprintf(f, " alias ");
print_dns_name(f, &name->target);
}
dump_ttl(f, "v4", name->expire_v4, now);
dump_ttl(f, "v6", name->expire_v6, now);
dump_ttl(f, "target", name->expire_target, now);
fprintf(f, " [v4 %s] [v6 %s]",
errnames[name->fetch_err],
errnames[name->fetch6_err]);
fprintf(f, "\n");
print_namehook_list(f, "v4", &name->v4, debug, now);
print_namehook_list(f, "v6", &name->v6, debug, now);
if (debug)
print_fetch_list(f, name);
if (debug)
print_find_list(f, name);
}
}
/*
* Unlock everything
*/
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->entrylocks[i]);
for (i = 0; i < NBUCKETS; i++)
UNLOCK(&adb->namelocks[i]);
}
static void
dump_entry(FILE *f, dns_adbentry_t *entry, isc_boolean_t debug,
isc_stdtime_t now)
{
char addrbuf[ISC_NETADDR_FORMATSIZE];
isc_netaddr_t netaddr;
dns_adbzoneinfo_t *zi;
isc_netaddr_fromsockaddr(&netaddr, &entry->sockaddr);
isc_netaddr_format(&netaddr, addrbuf, sizeof(addrbuf));
if (debug)
fprintf(f, ";\t%p: refcnt %u\n", entry, entry->refcnt);
fprintf(f, ";\t%s [srtt %u] [flags %08x]",
addrbuf, entry->srtt, entry->flags);
fprintf(f, "\n");
for (zi = ISC_LIST_HEAD(entry->zoneinfo);
zi != NULL;
zi = ISC_LIST_NEXT(zi, plink)) {
fprintf(f, ";\t\t");
print_dns_name(f, &zi->zone);
fprintf(f, " [lame TTL %d]\n", zi->lame_timer - now);
}
}
void
dns_adb_dumpfind(dns_adbfind_t *find, FILE *f) {
char tmp[512];
const char *tmpp;
dns_adbaddrinfo_t *ai;
isc_sockaddr_t *sa;
/*
* Not used currently, in the API Just In Case we
* want to dump out the name and/or entries too.
*/
LOCK(&find->lock);
fprintf(f, ";Find %p\n", find);
fprintf(f, ";\tqpending %08x partial %08x options %08x flags %08x\n",
find->query_pending, find->partial_result,
find->options, find->flags);
fprintf(f, ";\tname_bucket %d, name %p, event sender %p\n",
find->name_bucket, find->adbname, find->event.ev_sender);
ai = ISC_LIST_HEAD(find->list);
if (ai != NULL)
fprintf(f, "\tAddresses:\n");
while (ai != NULL) {
sa = &ai->sockaddr;
switch (sa->type.sa.sa_family) {
case AF_INET:
tmpp = inet_ntop(AF_INET, &sa->type.sin.sin_addr,
tmp, sizeof(tmp));
break;
case AF_INET6:
tmpp = inet_ntop(AF_INET6, &sa->type.sin6.sin6_addr,
tmp, sizeof(tmp));
break;
default:
tmpp = "UnkFamily";
}
if (tmpp == NULL)
tmpp = "BadAddress";
fprintf(f, "\t\tentry %p, flags %08x"
" srtt %u addr %s\n",
ai->entry, ai->flags, ai->srtt, tmpp);
ai = ISC_LIST_NEXT(ai, publink);
}
UNLOCK(&find->lock);
}
static void
print_dns_name(FILE *f, dns_name_t *name) {
char buf[DNS_NAME_FORMATSIZE];
INSIST(f != NULL);
dns_name_format(name, buf, sizeof(buf));
fprintf(f, "%s", buf);
}
static void
print_namehook_list(FILE *f, const char *legend, dns_adbnamehooklist_t *list,
isc_boolean_t debug, isc_stdtime_t now)
{
dns_adbnamehook_t *nh;
for (nh = ISC_LIST_HEAD(*list);
nh != NULL;
nh = ISC_LIST_NEXT(nh, plink))
{
if (debug)
fprintf(f, ";\tHook(%s) %p\n", legend, nh);
dump_entry(f, nh->entry, debug, now);
}
}
static inline void
print_fetch(FILE *f, dns_adbfetch_t *ft, const char *type) {
fprintf(f, "\t\tFetch(%s): %p -> { nh %p, entry %p, fetch %p }\n",
type, ft, ft->namehook, ft->entry, ft->fetch);
}
static 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);
want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If we got a negative cache response, remember it.
*/
if (NCACHE_RESULT(dev->result)) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
if (address_type == DNS_ADBFIND_INET) {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"caching negative entry for A (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v4 = ISC_MIN(name->expire_v4,
dev->rdataset->ttl + now);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch_err = FIND_ERR_NXDOMAIN;
else
name->fetch_err = FIND_ERR_NXRRSET;
} else {
DP(NCACHE_LEVEL, "adb fetch name %p: "
"caching negative entry for AAAA (ttl %u)",
name, dev->rdataset->ttl);
name->expire_v6 = ISC_MIN(name->expire_v6,
dev->rdataset->ttl + now);
if (dev->result == DNS_R_NCACHENXDOMAIN)
name->fetch6_err = FIND_ERR_NXDOMAIN;
else
name->fetch6_err = FIND_ERR_NXRRSET;
}
goto out;
}
/*
* Handle CNAME/DNAME.
*/
if (dev->result == DNS_R_CNAME || dev->result == DNS_R_DNAME) {
dev->rdataset->ttl = ttlclamp(dev->rdataset->ttl);
clean_target(adb, &name->target);
name->expire_target = INT_MAX;
result = set_target(adb, &name->name,
dns_fixedname_name(&dev->foundname),
dev->rdataset,
&name->target);
if (result == ISC_R_SUCCESS) {
DP(NCACHE_LEVEL,
"adb fetch name %p: caching alias target",
name);
name->expire_target = dev->rdataset->ttl + now;
}
goto check_result;
}
/*
* Did we get back junk? If so, and there are no more fetches
* sitting out there, tell all the finds about it.
*/
if (dev->result != ISC_R_SUCCESS) {
char buf[DNS_NAME_FORMATSIZE];
dns_name_format(&name->name, buf, sizeof(buf));
DP(DEF_LEVEL, "adb: fetch of '%s' %s failed: %s",
buf, address_type == DNS_ADBFIND_INET ? "A" : "AAAA",
dns_result_totext(dev->result));
/* XXXMLG Don't pound on bad servers. */
if (address_type == DNS_ADBFIND_INET) {
name->expire_v4 = ISC_MIN(name->expire_v4, now + 300);
name->fetch_err = FIND_ERR_FAILURE;
} else {
name->expire_v6 = ISC_MIN(name->expire_v6, now + 300);
name->fetch6_err = FIND_ERR_FAILURE;
}
goto out;
}
/*
* We got something potentially useful.
*/
result = import_rdataset(name, &fetch->rdataset, now);
check_result:
if (result == ISC_R_SUCCESS) {
ev_status = DNS_EVENT_ADBMOREADDRESSES;
if (address_type == DNS_ADBFIND_INET)
name->fetch_err = FIND_ERR_SUCCESS;
else
name->fetch6_err = FIND_ERR_SUCCESS;
}
out:
free_adbfetch(adb, &fetch);
isc_event_free(&ev);
clean_finds_at_name(name, ev_status, address_type);
UNLOCK(&adb->namelocks[bucket]);
}
static 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);
want_check_exit = kill_name(&name, DNS_EVENT_ADBCANCELED);
UNLOCK(&adb->namelocks[bucket]);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
return;
}
isc_stdtime_get(&now);
/*
* If 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 = NULL;
dns_adb_t *adb;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
unsigned int options;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V4(adbname));
adbname->fetch_err = FIND_ERR_NOTFOUND;
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
options = 0;
if (start_at_root) {
DP(ENTER_LEVEL,
"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;
options |= DNS_FETCHOPT_UNSHARED;
}
fetch = new_adbfetch(adb);
if (fetch == NULL) {
result = ISC_R_NOMEMORY;
goto cleanup;
}
result = dns_resolver_createfetch(adb->view->resolver, &adbname->name,
dns_rdatatype_a,
name, nameservers, NULL, options,
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 = NULL;
dns_adb_t *adb;
dns_name_t *name;
dns_rdataset_t rdataset;
dns_rdataset_t *nameservers;
unsigned int options;
INSIST(DNS_ADBNAME_VALID(adbname));
adb = adbname->adb;
INSIST(DNS_ADB_VALID(adb));
INSIST(!NAME_FETCH_V6(adbname));
adbname->fetch6_err = FIND_ERR_NOTFOUND;
name = NULL;
nameservers = NULL;
dns_rdataset_init(&rdataset);
options = 0;
if (start_at_root) {
DP(ENTER_LEVEL,
"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;
options |= DNS_FETCHOPT_UNSHARED;
}
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, options,
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]);
if (factor == DNS_ADB_RTTADJAGE)
new_srtt = addr->entry->srtt * 98 / 100;
else
new_srtt = (addr->entry->srtt / 10 * factor)
+ (rtt / 10 * (10 - factor));
addr->entry->srtt = new_srtt;
addr->srtt = new_srtt;
UNLOCK(&adb->entrylocks[bucket]);
}
void
dns_adb_changeflags(dns_adb_t *adb, dns_adbaddrinfo_t *addr,
unsigned int bits, unsigned int mask)
{
int bucket;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
addr->entry->flags = (addr->entry->flags & ~mask) | (bits & mask);
/*
* Note that we do not update the other bits in addr->flags with
* the most recent values from addr->entry->flags.
*/
addr->flags = (addr->flags & ~mask) | (bits & mask);
UNLOCK(&adb->entrylocks[bucket]);
}
isc_result_t
dns_adb_findaddrinfo(dns_adb_t *adb, isc_sockaddr_t *sa,
dns_adbaddrinfo_t **addrp, isc_stdtime_t now)
{
int bucket;
dns_adbentry_t *entry;
dns_adbaddrinfo_t *addr;
isc_result_t result;
in_port_t port;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL && *addrp == NULL);
UNUSED(now);
result = ISC_R_SUCCESS;
bucket = DNS_ADB_INVALIDBUCKET;
entry = find_entry_and_lock(adb, sa, &bucket);
if (adb->entry_sd[bucket]) {
result = ISC_R_SHUTTINGDOWN;
goto unlock;
}
if (entry == NULL) {
/*
* We don't know anything about this address.
*/
entry = new_adbentry(adb);
if (entry == NULL) {
result = ISC_R_NOMEMORY;
goto unlock;
}
entry->sockaddr = *sa;
link_entry(adb, bucket, entry);
DP(ENTER_LEVEL, "findaddrinfo: new entry %p", entry);
} else
DP(ENTER_LEVEL, "findaddrinfo: found entry %p", entry);
port = isc_sockaddr_getport(sa);
addr = new_adbaddrinfo(adb, entry, port);
if (addr != NULL) {
inc_entry_refcnt(adb, entry, ISC_FALSE);
*addrp = addr;
}
unlock:
UNLOCK(&adb->entrylocks[bucket]);
return (result);
}
void
dns_adb_freeaddrinfo(dns_adb_t *adb, dns_adbaddrinfo_t **addrp) {
dns_adbaddrinfo_t *addr;
dns_adbentry_t *entry;
int bucket;
isc_stdtime_t now;
isc_boolean_t want_check_exit = ISC_FALSE;
REQUIRE(DNS_ADB_VALID(adb));
REQUIRE(addrp != NULL);
addr = *addrp;
REQUIRE(DNS_ADBADDRINFO_VALID(addr));
entry = addr->entry;
REQUIRE(DNS_ADBENTRY_VALID(entry));
isc_stdtime_get(&now);
*addrp = NULL;
bucket = addr->entry->lock_bucket;
LOCK(&adb->entrylocks[bucket]);
entry->expires = now + 1800; /* XXXRTH */
want_check_exit = dec_entry_refcnt(adb, entry, ISC_FALSE);
UNLOCK(&adb->entrylocks[bucket]);
addr->entry = NULL;
free_adbaddrinfo(adb, &addr);
if (want_check_exit) {
LOCK(&adb->lock);
check_exit(adb);
UNLOCK(&adb->lock);
}
}
void
dns_adb_flush(dns_adb_t *adb) {
unsigned int i;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
for (i = 0; i < NBUCKETS; i++) {
/*
* Call our cleanup routines.
*/
INSIST(cleanup_names(adb, i, INT_MAX) == ISC_FALSE);
INSIST(cleanup_entries(adb, i, INT_MAX) == ISC_FALSE);
}
#ifdef DUMP_ADB_AFTER_CLEANING
dump_adb(adb, stdout, ISC_TRUE);
#endif
UNLOCK(&adb->lock);
}
void
dns_adb_flushname(dns_adb_t *adb, dns_name_t *name) {
dns_adbname_t *adbname;
dns_adbname_t *nextname;
int bucket;
INSIST(DNS_ADB_VALID(adb));
LOCK(&adb->lock);
bucket = dns_name_hash(name, ISC_FALSE) % NBUCKETS;
LOCK(&adb->namelocks[bucket]);
adbname = ISC_LIST_HEAD(adb->names[bucket]);
while (adbname != NULL) {
nextname = ISC_LIST_NEXT(adbname, plink);
if (!NAME_DEAD(adbname) &&
dns_name_equal(name, &adbname->name)) {
INSIST(kill_name(&adbname, DNS_EVENT_ADBCANCELED)
== ISC_FALSE);
}
adbname = nextname;
}
UNLOCK(&adb->namelocks[bucket]);
UNLOCK(&adb->lock);
}
static void
water(void *arg, int mark) {
dns_adb_t *adb = arg;
isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
isc_interval_t interval;
REQUIRE(DNS_ADB_VALID(adb));
DP(ISC_LOG_DEBUG(1),
"adb reached %s water mark", overmem ? "high" : "low");
adb->overmem = overmem;
if (overmem) {
isc_interval_set(&interval, 0, 1);
(void)isc_timer_reset(adb->timer, isc_timertype_once, NULL,
&interval, ISC_TRUE);
}
}
void
dns_adb_setadbsize(dns_adb_t *adb, isc_uint32_t size) {
isc_uint32_t hiwater;
isc_uint32_t lowater;
INSIST(DNS_ADB_VALID(adb));
if (size != 0 && size < DNS_ADB_MINADBSIZE)
size = DNS_ADB_MINADBSIZE;
hiwater = size - (size >> 3); /* Approximately 7/8ths. */
lowater = size - (size >> 2); /* Approximately 3/4ths. */
if (size == 0 || hiwater == 0 || lowater == 0)
isc_mem_setwater(adb->mctx, water, adb, 0, 0);
else
isc_mem_setwater(adb->mctx, water, adb, hiwater, lowater);
}