adb.c revision e01fbe2a45f9641968264a3bdf76d290e20521d7
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * Copyright (C) 1999-2003 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and/or distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * copyright notice and this permission notice appear in all copies.
d36adf470fe38579fd89cb8186a88a3b21a4be14Automatic Updater * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * PERFORMANCE OF THIS SOFTWARE.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington/* $Id: adb.c,v 1.264.16.1 2011/12/22 07:48:27 marka Exp $ */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * In finds, if task == NULL, no events will be generated, and no events
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * have been sent. If task != NULL but taskaction == NULL, an event has been
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein * posted but not yet freed. If neither are NULL, no event was posted.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#include <isc/string.h> /* Required for HP/UX (and others?) */
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#define DNS_ADB_MAGIC ISC_MAGIC('D', 'a', 'd', 'b')
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#define DNS_ADB_VALID(x) ISC_MAGIC_VALID(x, DNS_ADB_MAGIC)
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#define DNS_ADBNAME_MAGIC ISC_MAGIC('a', 'd', 'b', 'N')
78838d3e0cd62423c23de5503910e01884d2104bBrian Wellington#define DNS_ADBNAME_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAME_MAGIC)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBNAMEHOOK_MAGIC ISC_MAGIC('a', 'd', 'N', 'H')
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBNAMEHOOK_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBNAMEHOOK_MAGIC)
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#define DNS_ADBLAMEINFO_MAGIC ISC_MAGIC('a', 'd', 'b', 'Z')
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBLAMEINFO_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBLAMEINFO_MAGIC)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBENTRY_MAGIC ISC_MAGIC('a', 'd', 'b', 'E')
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBENTRY_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBENTRY_MAGIC)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBFETCH_MAGIC ISC_MAGIC('a', 'd', 'F', '4')
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBFETCH_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH_MAGIC)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBFETCH6_MAGIC ISC_MAGIC('a', 'd', 'F', '6')
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define DNS_ADBFETCH6_VALID(x) ISC_MAGIC_VALID(x, DNS_ADBFETCH6_MAGIC)
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * For type 3 negative cache entries, we will remember that the address is
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * broken for this long. XXXMLG This is also used for actual addresses, too.
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt * The intent is to keep us from constantly asking about A/AAAA records
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * if the zone has extremely low TTLs.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define ADB_CACHE_MINIMUM 10 /*%< seconds */
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt#define ADB_CACHE_MAXIMUM 86400 /*%< seconds (86400 = 24 hours) */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington#define ADB_ENTRY_WINDOW 1800 /*%< seconds */
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * The period in seconds after which an ADB name entry is regarded as stale
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * and forced to be cleaned up.
dd16d9d9e77c2d906ee5ffa3dd9f71cacfbcb081Brian Wellington * TODO: This should probably be configurable at run-time.
struct dns_adb {
unsigned int magic;
int next_cleanbucket;
unsigned int irefcnt;
unsigned int erefcnt;
unsigned int nnames;
unsigned int namescnt;
unsigned int *name_refcnt;
unsigned int nentries;
unsigned int entriescnt;
unsigned int *entry_refcnt;
struct dns_adbname {
unsigned int magic;
unsigned int partial_result;
unsigned int flags;
int lock_bucket;
unsigned int chains;
unsigned int fetch_err;
unsigned int fetch6_err;
struct dns_adbfetch {
unsigned int magic;
struct dns_adbnamehook {
unsigned int magic;
struct dns_adblameinfo {
unsigned int magic;
struct dns_adbentry {
unsigned int magic;
int lock_bucket;
unsigned int refcnt;
unsigned int flags;
unsigned int srtt;
unsigned char plain;
unsigned char plainto;
unsigned char edns;
unsigned char * sit;
dns_adbentry_t *);
isc_sockaddr_t *, int *,
static void water(void *, int);
((o) & DNS_ADBFIND_STARTATZONE))
(r) == DNS_R_NCACHENXRRSET)
(r) == DNS_R_NXRRSET)
(r) == DNS_R_NCACHENXDOMAIN)
(r) == DNS_R_NXRRSET || \
(r) == DNS_R_HINTNXRRSET)
static const char *errnames[] = {
static inline dns_ttl_t
return (ttl);
dns_adbentry_t *e;
unsigned int i, n, bucket;
goto check_exit;
if (nbuckets[i] != 0)
n = nbuckets[i];
goto done;
goto cleanup;
goto cleanup;
goto cleanup;
newentry_refcnt[i] = 0;
while (e != NULL) {
while (e != NULL) {
goto done;
sizeof(*newentries) * n);
sizeof(*newdeadentries) * n);
sizeof(*newentrylocks) * n);
sizeof(*newentry_sd) * n);
sizeof(*newentry_refcnt) * n);
done:
unsigned int i, n, bucket;
goto check_exit;
if (nbuckets[i] != 0)
n = nbuckets[i];
goto done;
goto cleanup;
goto cleanup;
goto cleanup;
newname_refcnt[i] = 0;
goto done;
sizeof(*newname_refcnt) * n);
done:
static isc_result_t
int addr_bucket;
unsigned int findoptions;
goto fail;
now);
goto fail;
fail:
if (new_addresses_added) {
return (ISC_R_SUCCESS);
return (result);
static isc_boolean_t
int bucket;
name = *n;
*n = NULL;
if (result)
return (result);
if (result)
return (result);
static isc_boolean_t
static inline isc_boolean_t
int bucket;
return (result);
dns_adbentry_t *e;
if (e == NULL)
if (e->refcnt == 0) {
static inline isc_boolean_t
int bucket;
return (result);
static isc_boolean_t
unsigned int bucket;
return (result);
static isc_boolean_t
unsigned int bucket;
if (result)
return (result);
static isc_boolean_t
int addr_bucket;
return (result);
static isc_result_t
unsigned int nlabels;
int order;
return (result);
return (result);
return (result);
return (result);
return (result);
NULL);
return (result);
return (result);
return (ISC_R_SUCCESS);
unsigned int addrs)
switch (evtype) {
if ((notify) != 0) {
if (wanted == 0)
if (process) {
static inline isc_boolean_t
return (result);
int bucket;
if (lock)
if (lock)
static inline isc_boolean_t
int bucket;
if (lock)
if (lock)
if (!destroy_entry)
return (result);
if (result)
return (result);
static inline dns_adbname_t *
return (NULL);
return (NULL);
return (name);
dns_adbname_t *n;
n = *name;
n->magic = 0;
static inline dns_adbnamehook_t *
return (NULL);
return (nh);
static inline dns_adblameinfo_t *
return (NULL);
return (NULL);
return (li);
static inline dns_adbentry_t *
dns_adbentry_t *e;
isc_uint32_t r;
if (e == NULL)
return (NULL);
e->refcnt = 0;
e->flags = 0;
e->udpsize = 0;
e->edns = 0;
e->plain = 0;
e->plainto = 0;
e->to4096 = 0;
e->to1432 = 0;
e->to1232 = 0;
e->to512 = 0;
e->sitlen = 0;
isc_random_get(&r);
e->expires = 0;
dns_adbentry_t *e;
e = *entry;
e->magic = 0;
static inline dns_adbfind_t *
dns_adbfind_t *h;
if (h == NULL)
return (NULL);
h->magic = 0;
h->partial_result = 0;
h->options = 0;
h->flags = 0;
return (NULL);
static inline dns_adbfetch_t *
dns_adbfetch_t *f;
if (f == NULL)
return (NULL);
f->magic = 0;
dns_adbfetch_t *f;
f = *fetch;
f->magic = 0;
static inline isc_boolean_t
static inline dns_adbaddrinfo_t *
return (NULL);
return (ai);
static inline dns_adbname_t *
int bucket;
return (adbname);
return (NULL);
static inline dns_adbentry_t *
int bucket;
return (entry);
return (NULL);
static isc_boolean_t
return (ISC_FALSE);
return (is_bad);
int bucket;
goto nextv4;
goto out;
goto nextv6;
goto out;
out:
static isc_boolean_t
return (result);
return (result);
return (result);
return (result);
return (result);
return (result);
int scans = 0;
for (victims = 0;
scans++;
victims++;
goto next;
victims++;
next:
if (!overmem)
static isc_boolean_t
return (result);
return (result);
if (result)
return (result);
static isc_boolean_t
return (result);
if (!result)
return (result);
static isc_boolean_t
return (result);
return (ISC_R_NOMEMORY);
goto fail0b;
goto fail0c;
goto fail0d;
goto fail0e;
goto fail0f;
goto fail0g;
goto fail1; \
goto fail1; \
goto fail1;
goto fail2;
#define MPINIT(t, p, n) do { \
goto fail3; \
isc_mempool_setname((p), n); \
goto fail3;
goto fail3;
return (ISC_R_SUCCESS);
return (result);
if (need_exit_check) {
if (!need_check_exit)
if (need_check_exit)
int bucket;
unsigned int wanted_addresses;
unsigned int wanted_fetches;
unsigned int query_pending;
wanted_fetches = 0;
query_pending = 0;
if (now == 0)
return (ISC_R_NOMEMORY);
namebuf[0] = 0;
goto out;
goto out;
goto post_copy;
goto v6;
goto post_copy;
goto fetch;
goto v6;
v6:
goto fetch;
goto post_copy;
goto fetch;
if (wanted_fetches != 0 &&
if (alias)
if (want_event) {
if (alias) {
goto out;
out:
if (want_event) {
return (result);
int bucket;
int bucket;
int unlock_bucket;
goto cleanup;
if (debug)
if (debug)
if (debug)
if (debug)
if (debug)
if (debug)
#ifdef ISC_PLATFORM_USESIT
const char *tmpp;
case AF_INET:
case AF_INET6:
if (debug)
if (NAME_FETCH_A(n))
if (NAME_FETCH_AAAA(n))
static isc_result_t
switch (result) {
case DNS_R_GLUE:
case DNS_R_HINT:
case ISC_R_SUCCESS:
case DNS_R_NXDOMAIN:
case DNS_R_NXRRSET:
adbname);
adbname);
case DNS_R_NCACHENXDOMAIN:
case DNS_R_NCACHENXRRSET:
case DNS_R_CNAME:
case DNS_R_DNAME:
adbname);
return (result);
int bucket;
unsigned int address_type;
address_type = 0;
if (want_check_exit) {
goto out;
name);
goto check_result;
goto out;
out:
static isc_result_t
unsigned int options;
if (start_at_zone) {
adbname);
goto cleanup;
goto cleanup;
goto cleanup;
return (result);
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
int bucket;
int bucket;
return (noedns);
int bucket;
int bucket;
int bucket;
int bucket;
int bucket;
unsigned int size;
return (size);
int bucket;
unsigned int size;
return (size);
int bucket;
int bucket;
len = 0;
return (len);
int bucket;
goto unlock;
goto unlock;
return (result);
int bucket;
if (want_check_exit) {
#ifdef DUMP_ADB_AFTER_CLEANING
int bucket;