1999-03-18 14:37:30 -05:00
|
|
|
/*
|
2018-02-23 03:53:12 -05:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-07-31 21:33:37 -04:00
|
|
|
*
|
2016-06-27 00:56:38 -04: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
|
2020-09-14 19:50:58 -04:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 03:53:12 -05:00
|
|
|
*
|
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
|
* information regarding copyright ownership.
|
1999-03-18 14:37:30 -05:00
|
|
|
*/
|
|
|
|
|
|
2018-04-17 11:29:14 -04:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
2000-05-08 10:38:29 -04:00
|
|
|
#include <isc/mem.h>
|
1999-03-18 14:37:30 -05:00
|
|
|
#include <isc/rwlock.h>
|
1999-12-16 17:24:22 -05:00
|
|
|
#include <isc/util.h>
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
#include <dns/db.h>
|
2020-02-12 09:33:32 -05:00
|
|
|
#include <dns/dbtable.h>
|
1999-03-18 14:37:30 -05:00
|
|
|
#include <dns/rbt.h>
|
2000-05-01 23:54:17 -04:00
|
|
|
#include <dns/result.h>
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
struct dns_dbtable {
|
1999-03-18 15:06:26 -05:00
|
|
|
/* Unlocked. */
|
2020-02-14 00:35:17 -05:00
|
|
|
unsigned int magic;
|
|
|
|
|
isc_mem_t *mctx;
|
2020-02-12 09:33:32 -05:00
|
|
|
dns_rdataclass_t rdclass;
|
2020-02-14 00:35:17 -05:00
|
|
|
isc_rwlock_t tree_lock;
|
2019-05-20 10:22:15 -04:00
|
|
|
/* Protected by atomics */
|
2020-02-12 09:33:32 -05:00
|
|
|
isc_refcount_t references;
|
1999-03-18 15:06:26 -05:00
|
|
|
/* Locked by tree_lock. */
|
2020-02-12 09:33:32 -05:00
|
|
|
dns_rbt_t *rbt;
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_db_t *default_db;
|
1999-03-18 14:37:30 -05:00
|
|
|
};
|
|
|
|
|
|
2020-02-14 00:35:17 -05:00
|
|
|
#define DBTABLE_MAGIC ISC_MAGIC('D', 'B', '-', '-')
|
2020-02-12 09:33:32 -05:00
|
|
|
#define VALID_DBTABLE(dbtable) ISC_MAGIC_VALID(dbtable, DBTABLE_MAGIC)
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-09-22 14:24:05 -04:00
|
|
|
static void
|
2020-02-14 00:35:17 -05:00
|
|
|
dbdetach(void *data, void *arg) {
|
1999-09-22 14:24:05 -04:00
|
|
|
dns_db_t *db = data;
|
|
|
|
|
|
2000-08-11 12:47:33 -04:00
|
|
|
UNUSED(arg);
|
1999-09-22 14:24:05 -04:00
|
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
|
}
|
|
|
|
|
|
1999-12-22 19:09:04 -05:00
|
|
|
isc_result_t
|
1999-04-20 18:26:50 -04:00
|
|
|
dns_dbtable_create(isc_mem_t *mctx, dns_rdataclass_t rdclass,
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_t **dbtablep) {
|
1999-03-18 14:37:30 -05:00
|
|
|
dns_dbtable_t *dbtable;
|
2020-02-14 00:35:17 -05:00
|
|
|
isc_result_t result;
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
REQUIRE(mctx != NULL);
|
|
|
|
|
REQUIRE(dbtablep != NULL && *dbtablep == NULL);
|
|
|
|
|
|
2019-07-16 09:52:14 -04:00
|
|
|
dbtable = isc_mem_get(mctx, sizeof(*dbtable));
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-05-25 14:41:52 -04:00
|
|
|
dbtable->rbt = NULL;
|
1999-09-22 14:24:05 -04:00
|
|
|
result = dns_rbt_create(mctx, dbdetach, NULL, &dbtable->rbt);
|
2020-02-13 16:28:07 -05:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1999-05-11 19:18:37 -04:00
|
|
|
goto clean1;
|
2020-02-13 16:28:07 -05:00
|
|
|
}
|
1999-05-11 19:18:37 -04:00
|
|
|
|
2021-01-31 23:59:41 -05:00
|
|
|
isc_rwlock_init(&dbtable->tree_lock, 0, 0);
|
1999-04-13 22:37:08 -04:00
|
|
|
dbtable->default_db = NULL;
|
2013-02-21 00:39:05 -05:00
|
|
|
dbtable->mctx = NULL;
|
|
|
|
|
isc_mem_attach(mctx, &dbtable->mctx);
|
1999-04-20 18:26:50 -04:00
|
|
|
dbtable->rdclass = rdclass;
|
1999-03-18 14:37:30 -05:00
|
|
|
dbtable->magic = DBTABLE_MAGIC;
|
2019-05-20 10:22:15 -04:00
|
|
|
isc_refcount_init(&dbtable->references, 1);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
*dbtablep = dbtable;
|
|
|
|
|
|
2000-04-06 18:03:35 -04:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
2020-02-12 09:33:32 -05:00
|
|
|
clean1:
|
2013-02-21 00:39:05 -05:00
|
|
|
isc_mem_putanddetach(&mctx, dbtable, sizeof(*dbtable));
|
1999-05-11 19:18:37 -04:00
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void
|
2020-02-14 00:35:17 -05:00
|
|
|
dbtable_free(dns_dbtable_t *dbtable) {
|
1999-05-11 19:18:37 -04:00
|
|
|
/*
|
2000-05-01 23:54:17 -04:00
|
|
|
* Caller must ensure that it is safe to call.
|
1999-05-11 19:18:37 -04:00
|
|
|
*/
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
2020-02-13 16:28:07 -05:00
|
|
|
if (dbtable->default_db != NULL) {
|
1999-04-13 22:37:08 -04:00
|
|
|
dns_db_detach(&dbtable->default_db);
|
2020-02-13 16:28:07 -05:00
|
|
|
}
|
1999-04-13 22:37:08 -04:00
|
|
|
|
1999-03-18 14:37:30 -05:00
|
|
|
dns_rbt_destroy(&dbtable->rbt);
|
1999-04-13 22:37:08 -04:00
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
isc_rwlock_destroy(&dbtable->tree_lock);
|
|
|
|
|
|
|
|
|
|
dbtable->magic = 0;
|
|
|
|
|
|
2013-02-21 00:39:05 -05:00
|
|
|
isc_mem_putanddetach(&dbtable->mctx, dbtable, sizeof(*dbtable));
|
1999-05-11 19:18:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_attach(dns_dbtable_t *source, dns_dbtable_t **targetp) {
|
1999-05-11 19:18:37 -04:00
|
|
|
REQUIRE(VALID_DBTABLE(source));
|
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
2000-07-31 21:33:37 -04:00
|
|
|
|
2019-05-20 10:22:15 -04:00
|
|
|
isc_refcount_increment(&source->references);
|
1999-05-11 19:18:37 -04:00
|
|
|
|
|
|
|
|
*targetp = source;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_detach(dns_dbtable_t **dbtablep) {
|
1999-05-11 19:18:37 -04:00
|
|
|
dns_dbtable_t *dbtable;
|
|
|
|
|
|
|
|
|
|
REQUIRE(dbtablep != NULL);
|
|
|
|
|
dbtable = *dbtablep;
|
2019-05-20 10:22:15 -04:00
|
|
|
*dbtablep = NULL;
|
2020-02-08 07:37:54 -05:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
2000-07-31 21:33:37 -04:00
|
|
|
|
2019-05-20 10:22:15 -04:00
|
|
|
if (isc_refcount_decrement(&dbtable->references) == 1) {
|
1999-05-11 19:18:37 -04:00
|
|
|
dbtable_free(dbtable);
|
2019-05-20 10:22:15 -04:00
|
|
|
}
|
1999-03-18 14:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
1999-12-22 19:09:04 -05:00
|
|
|
isc_result_t
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_add(dns_dbtable_t *dbtable, dns_db_t *db) {
|
1999-12-22 19:09:04 -05:00
|
|
|
isc_result_t result;
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_db_t *dbclone;
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-20 18:26:50 -04:00
|
|
|
REQUIRE(dns_db_class(db) == dbtable->rdclass);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
2016-06-28 21:25:30 -04:00
|
|
|
dbclone = NULL;
|
|
|
|
|
dns_db_attach(db, &dbclone);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04: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-13 22:37:08 -04:00
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
return (result);
|
1999-03-18 14:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_remove(dns_dbtable_t *dbtable, dns_db_t *db) {
|
|
|
|
|
dns_db_t *stored_data = NULL;
|
1999-03-18 14:37:30 -05:00
|
|
|
isc_result_t result;
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_name_t *name;
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
2000-07-31 21:33:37 -04:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
name = dns_db_origin(db);
|
|
|
|
|
|
1999-03-18 14:37:30 -05: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-01 23:54:17 -04:00
|
|
|
* XXXDCL ... this could be cheaper now with dns_rbt_deletenode.
|
1999-03-18 14:37:30 -05:00
|
|
|
*/
|
|
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
2000-04-19 14:27:24 -04:00
|
|
|
result = dns_rbt_findname(dbtable->rbt, name, 0, NULL,
|
2020-02-12 09:33:32 -05:00
|
|
|
(void **)(void *)&stored_data);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
2000-04-06 18:03:35 -04:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
1999-04-14 08:29:39 -04:00
|
|
|
INSIST(stored_data == db);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
2018-04-17 11:29:14 -04:00
|
|
|
(void)dns_rbt_deletename(dbtable->rbt, name, false);
|
1999-04-14 08:29:39 -04:00
|
|
|
}
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 14:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_adddefault(dns_dbtable_t *dbtable, dns_db_t *db) {
|
1999-03-18 14:37:30 -05:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-13 22:37:08 -04: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 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
dbtable->default_db = NULL;
|
|
|
|
|
dns_db_attach(db, &dbtable->default_db);
|
|
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 14:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_getdefault(dns_dbtable_t *dbtable, dns_db_t **dbp) {
|
1999-03-18 14:37:30 -05:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
1999-04-13 22:37:08 -04:00
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
1999-04-13 22:37:08 -04: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 14:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-02-14 00:35:17 -05:00
|
|
|
dns_dbtable_removedefault(dns_dbtable_t *dbtable) {
|
1999-03-18 14:37:30 -05:00
|
|
|
REQUIRE(VALID_DBTABLE(dbtable));
|
|
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
|
|
dns_db_detach(&dbtable->default_db);
|
|
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_write);
|
1999-03-18 14:37:30 -05:00
|
|
|
}
|
|
|
|
|
|
1999-12-22 19:09:04 -05:00
|
|
|
isc_result_t
|
2016-12-29 23:45:08 -05:00
|
|
|
dns_dbtable_find(dns_dbtable_t *dbtable, const dns_name_t *name,
|
2020-02-14 00:35:17 -05:00
|
|
|
unsigned int options, dns_db_t **dbp) {
|
|
|
|
|
dns_db_t *stored_data = NULL;
|
1999-12-22 19:09:04 -05:00
|
|
|
isc_result_t result;
|
2000-04-19 14:49:11 -04:00
|
|
|
unsigned int rbtoptions = 0;
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
|
|
|
|
|
2020-02-13 16:28:07 -05:00
|
|
|
if ((options & DNS_DBTABLEFIND_NOEXACT) != 0) {
|
2000-04-19 14:49:11 -04:00
|
|
|
rbtoptions |= DNS_RBTFIND_NOEXACT;
|
2020-02-13 16:28:07 -05:00
|
|
|
}
|
2000-04-19 14:49:11 -04:00
|
|
|
|
1999-04-13 22:37:08 -04:00
|
|
|
RWLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
|
|
|
|
|
2000-04-19 14:49:11 -04:00
|
|
|
result = dns_rbt_findname(dbtable->rbt, name, rbtoptions, NULL,
|
2020-02-12 09:33:32 -05:00
|
|
|
(void **)(void *)&stored_data);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
2020-02-13 16:28:07 -05:00
|
|
|
if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
|
1999-04-13 22:37:08 -04:00
|
|
|
dns_db_attach(stored_data, dbp);
|
2020-02-13 16:28:07 -05:00
|
|
|
} else if (dbtable->default_db != NULL) {
|
1999-04-13 22:37:08 -04:00
|
|
|
dns_db_attach(dbtable->default_db, dbp);
|
|
|
|
|
result = DNS_R_PARTIALMATCH;
|
2020-02-13 16:28:07 -05:00
|
|
|
} else {
|
2000-04-06 18:03:35 -04:00
|
|
|
result = ISC_R_NOTFOUND;
|
2020-02-13 16:28:07 -05:00
|
|
|
}
|
1999-04-13 22:37:08 -04:00
|
|
|
|
|
|
|
|
RWUNLOCK(&dbtable->tree_lock, isc_rwlocktype_read);
|
1999-03-18 14:37:30 -05:00
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
|
}
|