1999-03-18 19:37:30 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-03-18 19:37:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
1999-03-18 19:37:30 +00:00
|
|
|
#include <isc/rwlock.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
#include <dns/dbtable.h>
|
1999-04-14 02:37:08 +00:00
|
|
|
#include <dns/db.h>
|
1999-03-18 19:37:30 +00:00
|
|
|
#include <dns/rbt.h>
|
2000-05-02 03:54:17 +00:00
|
|
|
#include <dns/result.h>
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
struct dns_dbtable {
|
1999-03-18 20:06:26 +00:00
|
|
|
/* Unlocked. */
|
1999-03-18 19:37:30 +00:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t * mctx;
|
1999-04-20 22:26:50 +00:00
|
|
|
dns_rdataclass_t rdclass;
|
1999-05-11 23:18:37 +00:00
|
|
|
isc_mutex_t lock;
|
1999-03-18 19:37:30 +00:00
|
|
|
isc_rwlock_t tree_lock;
|
1999-05-11 23:18:37 +00:00
|
|
|
/* Locked by lock. */
|
|
|
|
unsigned int references;
|
1999-03-18 20:06:26 +00:00
|
|
|
/* Locked by tree_lock. */
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_rbt_t * rbt;
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_t * default_db;
|
1999-03-18 19:37:30 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define DBTABLE_MAGIC ISC_MAGIC('D', 'B', '-', '-')
|
|
|
|
#define VALID_DBTABLE(dbtable) ISC_MAGIC_VALID(dbtable, DBTABLE_MAGIC)
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-09-22 18:24:05 +00:00
|
|
|
static void
|
|
|
|
dbdetach(void *data, void *arg) {
|
|
|
|
dns_db_t *db = data;
|
|
|
|
|
2000-08-11 16:47:33 +00:00
|
|
|
UNUSED(arg);
|
1999-09-22 18:24:05 +00:00
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-04-20 22:26:50 +00:00
|
|
|
dns_dbtable_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
|
|
|
|
dns_dbtable_t **dbtablep)
|
|
|
|
{
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_dbtable_t *dbtable;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
REQUIRE(dbtablep != NULL && *dbtablep == NULL);
|
|
|
|
|
|
|
|
dbtable = (dns_dbtable_t *)isc_mem_get(mctx, sizeof(*dbtable));
|
|
|
|
if (dbtable == NULL)
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-25 18:41:52 +00:00
|
|
|
dbtable->rbt = NULL;
|
1999-09-22 18:24:05 +00:00
|
|
|
result = dns_rbt_create(mctx, dbdetach, NULL, &dbtable->rbt);
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
1999-05-11 23:18:37 +00:00
|
|
|
goto clean1;
|
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&dbtable->lock);
|
2001-01-12 00:31:50 +00:00
|
|
|
|
|
|
|
result = isc_rwlock_init(&dbtable->tree_lock, 0, 0);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
1999-05-11 23:18:37 +00:00
|
|
|
goto clean3;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
dbtable->default_db = NULL;
|
2013-02-20 21:39:05 -08:00
|
|
|
dbtable->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &dbtable->mctx);
|
1999-04-20 22:26:50 +00:00
|
|
|
dbtable->rdclass = rdclass;
|
1999-03-18 19:37:30 +00:00
|
|
|
dbtable->magic = DBTABLE_MAGIC;
|
1999-05-11 23:18:37 +00:00
|
|
|
dbtable->references = 1;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
*dbtablep = dbtable;
|
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
clean3:
|
2000-08-26 01:37:00 +00:00
|
|
|
DESTROYLOCK(&dbtable->lock);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
dns_rbt_destroy(&dbtable->rbt);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
clean1:
|
2013-02-20 21:39:05 -08:00
|
|
|
isc_mem_putanddetach(&mctx, dbtable, sizeof(*dbtable));
|
1999-05-11 23:18:37 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
dbtable_free(dns_dbtable_t *dbtable) {
|
|
|
|
/*
|
2000-05-02 03:54:17 +00:00
|
|
|
* Caller must ensure that it is safe to call.
|
1999-05-11 23:18:37 +00:00
|
|
|
*/
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
if (dbtable->default_db != NULL)
|
|
|
|
dns_db_detach(&dbtable->default_db);
|
|
|
|
|
1999-03-18 19:37:30 +00:00
|
|
|
dns_rbt_destroy(&dbtable->rbt);
|
1999-04-14 02:37:08 +00:00
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
isc_rwlock_destroy(&dbtable->tree_lock);
|
|
|
|
|
|
|
|
dbtable->magic = 0;
|
|
|
|
|
2013-02-20 21:39:05 -08:00
|
|
|
isc_mem_putanddetach(&dbtable->mctx, dbtable, sizeof(*dbtable));
|
1999-05-11 23:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_dbtable_attach(dns_dbtable_t *source, dns_dbtable_t **targetp) {
|
|
|
|
REQUIRE(VALID_DBTABLE(source));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
LOCK(&source->lock);
|
|
|
|
|
|
|
|
INSIST(source->references > 0);
|
|
|
|
source->references++;
|
|
|
|
INSIST(source->references != 0);
|
|
|
|
|
|
|
|
UNLOCK(&source->lock);
|
|
|
|
|
|
|
|
*targetp = source;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_dbtable_detach(dns_dbtable_t **dbtablep) {
|
|
|
|
dns_dbtable_t *dbtable;
|
2018-04-17 08:29:14 -07:00
|
|
|
bool free_dbtable = false;
|
1999-05-11 23:18:37 +00:00
|
|
|
|
|
|
|
REQUIRE(dbtablep != NULL);
|
|
|
|
dbtable = *dbtablep;
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-05-11 23:18:37 +00:00
|
|
|
LOCK(&dbtable->lock);
|
|
|
|
|
|
|
|
INSIST(dbtable->references > 0);
|
|
|
|
dbtable->references--;
|
|
|
|
if (dbtable->references == 0)
|
2018-04-17 08:29:14 -07:00
|
|
|
free_dbtable = true;
|
1999-05-11 23:18:37 +00:00
|
|
|
|
|
|
|
UNLOCK(&dbtable->lock);
|
|
|
|
|
|
|
|
if (free_dbtable)
|
|
|
|
dbtable_free(dbtable);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
*dbtablep = NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_add(dns_dbtable_t *dbtable, dns_db_t *db) {
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
2016-06-28 21:25:30 -04:00
|
|
|
dns_db_t *dbclone;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-20 22:26:50 +00:00
|
|
|
REQUIRE(dns_db_class(db) == dbtable->rdclass);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
2016-06-28 21:25:30 -04:00
|
|
|
dbclone = NULL;
|
|
|
|
dns_db_attach(db, &dbclone);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
2016-06-28 21:25:30 -04:00
|
|
|
result = dns_rbt_addname(dbtable->rbt, dns_db_origin(dbclone), dbclone);
|
1999-04-14 02:37:08 +00:00
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
return (result);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_remove(dns_dbtable_t *dbtable, dns_db_t *db) {
|
|
|
|
dns_db_t *stored_data = NULL;
|
1999-03-18 19:37:30 +00:00
|
|
|
isc_result_t result;
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_name_t *name;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
name = dns_db_origin(db);
|
|
|
|
|
1999-03-18 19:37:30 +00:00
|
|
|
/*
|
|
|
|
* There is a requirement that the association of name with db
|
|
|
|
* be verified. With the current rbt.c this is expensive to do,
|
|
|
|
* because effectively two find operations are being done, but
|
|
|
|
* deletion is relatively infrequent.
|
2000-05-02 03:54:17 +00:00
|
|
|
* XXXDCL ... this could be cheaper now with dns_rbt_deletenode.
|
1999-03-18 19:37:30 +00:00
|
|
|
*/
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
2000-04-19 18:27:24 +00:00
|
|
|
result = dns_rbt_findname(dbtable->rbt, name, 0, NULL,
|
2004-02-03 00:59:05 +00:00
|
|
|
(void **) (void *)&stored_data);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
1999-04-14 12:29:39 +00:00
|
|
|
INSIST(stored_data == db);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
(void)dns_rbt_deletename(dbtable->rbt, name, false);
|
1999-04-14 12:29:39 +00:00
|
|
|
}
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_dbtable_adddefault(dns_dbtable_t *dbtable, dns_db_t *db) {
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-14 02:37:08 +00:00
|
|
|
REQUIRE(dbtable->default_db == NULL);
|
|
|
|
REQUIRE(dns_name_compare(dns_db_origin(db), dns_rootname) == 0);
|
|
|
|
|
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
dbtable->default_db = NULL;
|
|
|
|
dns_db_attach(db, &dbtable->default_db);
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_getdefault(dns_dbtable_t *dbtable, dns_db_t **dbp) {
|
1999-03-18 19:37:30 +00:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-14 02:37:08 +00:00
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
dns_db_attach(dbtable->default_db, dbp);
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_dbtable_removedefault(dns_dbtable_t *dbtable) {
|
1999-03-18 19:37:30 +00:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
dns_db_detach(&dbtable->default_db);
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 19:37:30 +00:00
|
|
|
}
|
|
|
|
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_dbtable_find(dns_dbtable_t *dbtable, const dns_name_t *name,
|
2000-04-19 18:49:11 +00:00
|
|
|
unsigned int options, dns_db_t **dbp)
|
|
|
|
{
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_t *stored_data = NULL;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
2000-04-19 18:49:11 +00:00
|
|
|
unsigned int rbtoptions = 0;
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
|
|
|
|
2000-04-19 18:49:11 +00:00
|
|
|
if ((options & DNS_DBTABLEFIND_NOEXACT) != 0)
|
|
|
|
rbtoptions |= DNS_RBTFIND_NOEXACT;
|
|
|
|
|
1999-04-14 02:37:08 +00:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
|
|
|
|
2000-04-19 18:49:11 +00:00
|
|
|
result = dns_rbt_findname(dbtable->rbt, name, rbtoptions, NULL,
|
2004-02-03 00:59:05 +00:00
|
|
|
(void **) (void *)&stored_data);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
|
1999-04-14 02:37:08 +00:00
|
|
|
dns_db_attach(stored_data, dbp);
|
|
|
|
else if (dbtable->default_db != NULL) {
|
|
|
|
dns_db_attach(dbtable->default_db, dbp);
|
|
|
|
result = DNS_R_PARTIALMATCH;
|
|
|
|
} else
|
2000-04-06 22:03:35 +00:00
|
|
|
result = ISC_R_NOTFOUND;
|
1999-04-14 02:37:08 +00:00
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
1999-03-18 19:37:30 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|