2009-06-30 02:53:46 +00:00
|
|
|
/*
|
2011-11-01 23:47:00 +00:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2009-06-30 02:53:46 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2009-06-30 02:53:46 +00: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 https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2009-06-30 02:53:46 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Imports
|
|
|
|
***/
|
|
|
|
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2009-06-30 02:53:46 +00:00
|
|
|
#include <isc/string.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/dbiterator.h>
|
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/rdatasetiter.h>
|
|
|
|
#include <dns/rriterator.h>
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** RRiterator methods
|
|
|
|
***/
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_rriterator_init(dns_rriterator_t *it, dns_db_t *db, dns_dbversion_t *ver,
|
|
|
|
isc_stdtime_t now) {
|
|
|
|
isc_result_t result;
|
|
|
|
it->magic = RRITERATOR_MAGIC;
|
|
|
|
it->db = db;
|
|
|
|
it->dbit = NULL;
|
|
|
|
it->ver = ver;
|
|
|
|
it->now = now;
|
|
|
|
it->node = NULL;
|
|
|
|
result = dns_db_createiterator(it->db, 0, &it->dbit);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
return result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
it->rdatasetit = NULL;
|
|
|
|
dns_rdata_init(&it->rdata);
|
|
|
|
dns_rdataset_init(&it->rdataset);
|
|
|
|
dns_fixedname_init(&it->fixedname);
|
|
|
|
INSIST(!dns_rdataset_isassociated(&it->rdataset));
|
|
|
|
it->result = ISC_R_SUCCESS;
|
|
|
|
return it->result;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_rriterator_first(dns_rriterator_t *it) {
|
|
|
|
REQUIRE(VALID_RRITERATOR(it));
|
|
|
|
/* Reset state */
|
|
|
|
if (dns_rdataset_isassociated(&it->rdataset)) {
|
|
|
|
dns_rdataset_disassociate(&it->rdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
if (it->rdatasetit != NULL) {
|
|
|
|
dns_rdatasetiter_destroy(&it->rdatasetit);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
if (it->node != NULL) {
|
Decouple database and node lifetimes by adding node-specific vtables
All databases in the codebase follow the same structure: a database is
an associative container from DNS names to nodes, and each node is an
associative container from RR types to RR data.
Each database implementation (qpzone, qpcache, sdlz, builtin, dyndb) has
its own corresponding node type (qpznode, qpcnode, etc). However, some
code needs to work with nodes generically regardless of their specific
type - for example, to acquire locks, manage references, or
register/unregister slabs from the heap.
Currently, these generic node operations are implemented as methods in
the database vtable, which creates problematic coupling between database
and node lifetimes. If a node outlives its parent database, the node
destructor will destroy all RR data, and each RR data destructor will
try to unregister from heaps by calling a virtual function from the
database vtable. Since the database was already freed, this causes a
crash.
This commit breaks the coupling by standardizing the layout of all
database nodes, adding a dedicated vtable for node operations, and
moving node-specific methods from the database vtable to the node
vtable.
2025-06-05 11:51:29 +02:00
|
|
|
dns_db_detachnode(&it->node);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_dbiterator_first(it->dbit);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The top node may be empty when out of zone glue exists.
|
|
|
|
* Walk the tree to find the first node with data.
|
|
|
|
*/
|
|
|
|
while (it->result == ISC_R_SUCCESS) {
|
|
|
|
it->result = dns_dbiterator_current(
|
|
|
|
it->dbit, &it->node,
|
|
|
|
dns_fixedname_name(&it->fixedname));
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
|
2022-11-16 10:47:40 +11:00
|
|
|
it->result = dns_db_allrdatasets(it->db, it->node, it->ver, 0,
|
2009-06-30 02:53:46 +00:00
|
|
|
it->now, &it->rdatasetit);
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
it->result = dns_rdatasetiter_first(it->rdatasetit);
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* This node is empty. Try next node.
|
|
|
|
*/
|
|
|
|
dns_rdatasetiter_destroy(&it->rdatasetit);
|
Decouple database and node lifetimes by adding node-specific vtables
All databases in the codebase follow the same structure: a database is
an associative container from DNS names to nodes, and each node is an
associative container from RR types to RR data.
Each database implementation (qpzone, qpcache, sdlz, builtin, dyndb) has
its own corresponding node type (qpznode, qpcnode, etc). However, some
code needs to work with nodes generically regardless of their specific
type - for example, to acquire locks, manage references, or
register/unregister slabs from the heap.
Currently, these generic node operations are implemented as methods in
the database vtable, which creates problematic coupling between database
and node lifetimes. If a node outlives its parent database, the node
destructor will destroy all RR data, and each RR data destructor will
try to unregister from heaps by calling a virtual function from the
database vtable. Since the database was already freed, this causes a
crash.
This commit breaks the coupling by standardizing the layout of all
database nodes, adding a dedicated vtable for node operations, and
moving node-specific methods from the database vtable to the node
vtable.
2025-06-05 11:51:29 +02:00
|
|
|
dns_db_detachnode(&it->node);
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_dbiterator_next(it->dbit);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dns_rdatasetiter_current(it->rdatasetit, &it->rdataset);
|
2015-02-27 15:08:38 +11:00
|
|
|
dns_rdataset_getownercase(&it->rdataset,
|
|
|
|
dns_fixedname_name(&it->fixedname));
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_rdataset_first(&it->rdataset);
|
|
|
|
return it->result;
|
|
|
|
}
|
|
|
|
return it->result;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_rriterator_nextrrset(dns_rriterator_t *it) {
|
|
|
|
REQUIRE(VALID_RRITERATOR(it));
|
|
|
|
if (dns_rdataset_isassociated(&it->rdataset)) {
|
|
|
|
dns_rdataset_disassociate(&it->rdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_rdatasetiter_next(it->rdatasetit);
|
|
|
|
/*
|
|
|
|
* The while loop body is executed more than once
|
|
|
|
* only when an empty dbnode needs to be skipped.
|
|
|
|
*/
|
|
|
|
while (it->result == ISC_R_NOMORE) {
|
|
|
|
dns_rdatasetiter_destroy(&it->rdatasetit);
|
Decouple database and node lifetimes by adding node-specific vtables
All databases in the codebase follow the same structure: a database is
an associative container from DNS names to nodes, and each node is an
associative container from RR types to RR data.
Each database implementation (qpzone, qpcache, sdlz, builtin, dyndb) has
its own corresponding node type (qpznode, qpcnode, etc). However, some
code needs to work with nodes generically regardless of their specific
type - for example, to acquire locks, manage references, or
register/unregister slabs from the heap.
Currently, these generic node operations are implemented as methods in
the database vtable, which creates problematic coupling between database
and node lifetimes. If a node outlives its parent database, the node
destructor will destroy all RR data, and each RR data destructor will
try to unregister from heaps by calling a virtual function from the
database vtable. Since the database was already freed, this causes a
crash.
This commit breaks the coupling by standardizing the layout of all
database nodes, adding a dedicated vtable for node operations, and
moving node-specific methods from the database vtable to the node
vtable.
2025-06-05 11:51:29 +02:00
|
|
|
dns_db_detachnode(&it->node);
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_dbiterator_next(it->dbit);
|
|
|
|
if (it->result == ISC_R_NOMORE) {
|
|
|
|
/* We are at the end of the entire database. */
|
|
|
|
return it->result;
|
|
|
|
}
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_dbiterator_current(
|
|
|
|
it->dbit, &it->node,
|
|
|
|
dns_fixedname_name(&it->fixedname));
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2022-11-16 10:47:40 +11:00
|
|
|
it->result = dns_db_allrdatasets(it->db, it->node, it->ver, 0,
|
2009-06-30 02:53:46 +00:00
|
|
|
it->now, &it->rdatasetit);
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_rdatasetiter_first(it->rdatasetit);
|
|
|
|
}
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_rdatasetiter_current(it->rdatasetit, &it->rdataset);
|
2015-02-27 15:08:38 +11:00
|
|
|
dns_rdataset_getownercase(&it->rdataset,
|
|
|
|
dns_fixedname_name(&it->fixedname));
|
2009-06-30 02:53:46 +00:00
|
|
|
it->result = dns_rdataset_first(&it->rdataset);
|
|
|
|
return it->result;
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_rriterator_next(dns_rriterator_t *it) {
|
|
|
|
REQUIRE(VALID_RRITERATOR(it));
|
|
|
|
if (it->result != ISC_R_SUCCESS) {
|
|
|
|
return it->result;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
INSIST(it->dbit != NULL);
|
|
|
|
INSIST(it->node != NULL);
|
|
|
|
INSIST(it->rdatasetit != NULL);
|
|
|
|
|
|
|
|
it->result = dns_rdataset_next(&it->rdataset);
|
|
|
|
if (it->result == ISC_R_NOMORE) {
|
|
|
|
return dns_rriterator_nextrrset(it);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
return it->result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_rriterator_pause(dns_rriterator_t *it) {
|
|
|
|
REQUIRE(VALID_RRITERATOR(it));
|
|
|
|
RUNTIME_CHECK(dns_dbiterator_pause(it->dbit) == ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_rriterator_destroy(dns_rriterator_t *it) {
|
|
|
|
REQUIRE(VALID_RRITERATOR(it));
|
|
|
|
if (dns_rdataset_isassociated(&it->rdataset)) {
|
|
|
|
dns_rdataset_disassociate(&it->rdataset);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
if (it->rdatasetit != NULL) {
|
|
|
|
dns_rdatasetiter_destroy(&it->rdatasetit);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
if (it->node != NULL) {
|
Decouple database and node lifetimes by adding node-specific vtables
All databases in the codebase follow the same structure: a database is
an associative container from DNS names to nodes, and each node is an
associative container from RR types to RR data.
Each database implementation (qpzone, qpcache, sdlz, builtin, dyndb) has
its own corresponding node type (qpznode, qpcnode, etc). However, some
code needs to work with nodes generically regardless of their specific
type - for example, to acquire locks, manage references, or
register/unregister slabs from the heap.
Currently, these generic node operations are implemented as methods in
the database vtable, which creates problematic coupling between database
and node lifetimes. If a node outlives its parent database, the node
destructor will destroy all RR data, and each RR data destructor will
try to unregister from heaps by calling a virtual function from the
database vtable. Since the database was already freed, this causes a
crash.
This commit breaks the coupling by standardizing the layout of all
database nodes, adding a dedicated vtable for node operations, and
moving node-specific methods from the database vtable to the node
vtable.
2025-06-05 11:51:29 +02:00
|
|
|
dns_db_detachnode(&it->node);
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2009-06-30 02:53:46 +00:00
|
|
|
dns_dbiterator_destroy(&it->dbit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_rriterator_current(dns_rriterator_t *it, dns_name_t **name, uint32_t *ttl,
|
2018-03-28 14:19:37 +02:00
|
|
|
dns_rdataset_t **rdataset, dns_rdata_t **rdata) {
|
2009-06-30 02:53:46 +00:00
|
|
|
REQUIRE(name != NULL && *name == NULL);
|
|
|
|
REQUIRE(VALID_RRITERATOR(it));
|
|
|
|
REQUIRE(it->result == ISC_R_SUCCESS);
|
2011-11-01 04:00:45 +00:00
|
|
|
REQUIRE(rdataset == NULL || *rdataset == NULL);
|
|
|
|
REQUIRE(rdata == NULL || *rdata == NULL);
|
2009-06-30 02:53:46 +00:00
|
|
|
|
|
|
|
*name = dns_fixedname_name(&it->fixedname);
|
|
|
|
*ttl = it->rdataset.ttl;
|
|
|
|
|
|
|
|
dns_rdata_reset(&it->rdata);
|
|
|
|
dns_rdataset_current(&it->rdataset, &it->rdata);
|
|
|
|
|
2023-04-06 16:32:16 +01:00
|
|
|
SET_IF_NOT_NULL(rdataset, &it->rdataset);
|
2009-06-30 02:53:46 +00:00
|
|
|
|
2023-04-06 16:32:16 +01:00
|
|
|
SET_IF_NOT_NULL(rdata, &it->rdata);
|
2009-06-30 02:53:46 +00:00
|
|
|
}
|