|
| 1 | +/* |
| 2 | + * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MPL-2.0 |
| 5 | + * |
| 6 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | + * |
| 10 | + * See the COPYRIGHT file distributed with this work for additional |
| 11 | + * information regarding copyright ownership. |
| 12 | + */ |
| 13 | + |
| 14 | +#pragma once |
| 15 | + |
| 16 | +/***** |
| 17 | +***** Module Info |
| 18 | +*****/ |
| 19 | + |
| 20 | +/*! \file |
| 21 | + * \brief |
| 22 | + * A nametree module is a tree of DNS names containing boolean values |
| 23 | + * or bitfields, allowing a quick lookup to see whether a name is included |
| 24 | + * in or excluded from some policy. |
| 25 | + */ |
| 26 | + |
| 27 | +#include <stdbool.h> |
| 28 | + |
| 29 | +#include <isc/lang.h> |
| 30 | +#include <isc/magic.h> |
| 31 | +#include <isc/refcount.h> |
| 32 | +#include <isc/rwlock.h> |
| 33 | +#include <isc/stdtime.h> |
| 34 | + |
| 35 | +#include <dns/rdatastruct.h> |
| 36 | +#include <dns/types.h> |
| 37 | + |
| 38 | +#include <dst/dst.h> |
| 39 | + |
| 40 | +/* Define to 1 for detailed reference tracing */ |
| 41 | +#undef DNS_NAMETREE_TRACE |
| 42 | + |
| 43 | +typedef enum { |
| 44 | + DNS_NAMETREE_BOOL, |
| 45 | + DNS_NAMETREE_BITS, |
| 46 | + DNS_NAMETREE_COUNT |
| 47 | +} dns_nametree_type_t; |
| 48 | + |
| 49 | +ISC_LANG_BEGINDECLS |
| 50 | + |
| 51 | +void |
| 52 | +dns_nametree_create(isc_mem_t *mctx, dns_nametree_type_t type, const char *name, |
| 53 | + dns_nametree_t **ntp); |
| 54 | +/*%< |
| 55 | + * Create a nametree. |
| 56 | + * |
| 57 | + * If 'name' is not NULL, it will be saved as the name of the QP trie |
| 58 | + * for debugging purposes. |
| 59 | + * |
| 60 | + * 'type' indicates whether the tree will be used for storing boolean |
| 61 | + * values (DNS_NAMETREE_BOOL), bitfields (DNS_NAMETREE_BITS), or counters |
| 62 | + * (DNS_NAMETREE_COUNT). |
| 63 | + * |
| 64 | + * Requires: |
| 65 | + * |
| 66 | + *\li 'mctx' is a valid memory context. |
| 67 | + *\li ntp != NULL && *ntp == NULL |
| 68 | + */ |
| 69 | + |
| 70 | +isc_result_t |
| 71 | +dns_nametree_add(dns_nametree_t *nametree, const dns_name_t *name, |
| 72 | + uint32_t value); |
| 73 | +/*%< |
| 74 | + * Add a node to 'nametree'. |
| 75 | + * |
| 76 | + * If the nametree type was set to DNS_NAMETREE_BOOL, then 'value' |
| 77 | + * represents a single boolean value, true or false. If the name already |
| 78 | + * exists within the tree, then return ISC_R_EXISTS. |
| 79 | + * |
| 80 | + * If the nametree type was set to DNS_NAMETREE_COUNT, then 'value' |
| 81 | + * can only be true. Each time the same name is added to the tree, |
| 82 | + * ISC_R_SUCCESS is returned and a counter is incremented. |
| 83 | + * dns_nametree_delete() must be deleted the same number of times |
| 84 | + * as dns_nametree_add() before the name is removed from the tree. |
| 85 | + * |
| 86 | + * If the nametree type was set to DNS_NAMETREE_BITS, then 'value' is |
| 87 | + * a bit number within a bit field, which is sized to accomodate at least |
| 88 | + * 'value' bits. If the name already exists, then that bit will be set |
| 89 | + * in the bitfield, other bits will be retained, and ISC_R_SUCCESS will be |
| 90 | + * returned. If 'value' excees the number of bits in the existing bit |
| 91 | + * field, the field will be expanded. |
| 92 | + * |
| 93 | + * Requires: |
| 94 | + * |
| 95 | + *\li 'nametree' points to a valid nametree. |
| 96 | + * |
| 97 | + * Returns: |
| 98 | + * |
| 99 | + *\li ISC_R_SUCCESS |
| 100 | + *\li ISC_R_EXISTS |
| 101 | + * |
| 102 | + *\li Any other result indicates failure. |
| 103 | + */ |
| 104 | + |
| 105 | +isc_result_t |
| 106 | +dns_nametree_delete(dns_nametree_t *nametree, const dns_name_t *name); |
| 107 | +/*%< |
| 108 | + * Delete 'name' from 'nametree'. |
| 109 | + * |
| 110 | + * If the nametree type was set to DNS_NAMETREE_COUNT, then this must |
| 111 | + * be called for each name the same number of times as dns_nametree_add() |
| 112 | + * was called before the name is removed. |
| 113 | + * |
| 114 | + * Requires: |
| 115 | + * |
| 116 | + *\li 'nametree' points to a valid nametree. |
| 117 | + *\li 'name' is not NULL |
| 118 | + * |
| 119 | + * Returns: |
| 120 | + * |
| 121 | + *\li ISC_R_SUCCESS |
| 122 | + * |
| 123 | + *\li Any other result indicates failure. |
| 124 | + */ |
| 125 | + |
| 126 | +isc_result_t |
| 127 | +dns_nametree_find(dns_nametree_t *nametree, const dns_name_t *name, |
| 128 | + dns_ntnode_t **ntp); |
| 129 | +/*%< |
| 130 | + * Retrieve the node that exactly matches 'name' from 'nametree'. |
| 131 | + * |
| 132 | + * Requires: |
| 133 | + * |
| 134 | + *\li 'nametree' is a valid nametree. |
| 135 | + * |
| 136 | + *\li 'name' is a valid name. |
| 137 | + * |
| 138 | + *\li ntp != NULL && *ntp == NULL |
| 139 | + * |
| 140 | + * Returns: |
| 141 | + * |
| 142 | + *\li ISC_R_SUCCESS |
| 143 | + *\li ISC_R_NOTFOUND |
| 144 | + * |
| 145 | + *\li Any other result indicates an error. |
| 146 | + */ |
| 147 | + |
| 148 | +bool |
| 149 | +dns_nametree_covered(dns_nametree_t *nametree, const dns_name_t *name, |
| 150 | + dns_name_t *found, uint32_t bit); |
| 151 | +/*%< |
| 152 | + * Indicates whether a 'name' (with optional 'bit' value) is covered by |
| 153 | + * 'nametree'. |
| 154 | + * |
| 155 | + * In DNS_NAMETREE_BOOL nametrees, this returns true if 'name' has a match |
| 156 | + * or a closest ancestor in 'nametree' with its value set to 'true'. |
| 157 | + * 'bit' is ignored. |
| 158 | + * |
| 159 | + * In DNS_NAMETREE_BITS trees, this returns true if 'name' has a match or |
| 160 | + * a closest ancestor in 'nametree' with the 'bit' set in its bitfield. |
| 161 | + * |
| 162 | + * If a name is not found, the default return value is false. |
| 163 | + * |
| 164 | + * If 'found' is not NULL, the name or ancestor name that was found in |
| 165 | + * the tree is copied into it. |
| 166 | + * |
| 167 | + * Requires: |
| 168 | + * |
| 169 | + *\li 'nametree' is a valid nametree, or is NULL. |
| 170 | + */ |
| 171 | + |
| 172 | +#if DNS_NAMETREE_TRACE |
| 173 | +#define dns_nametree_ref(ptr) \ |
| 174 | + dns_nametree__ref(ptr, __func__, __FILE__, __LINE__) |
| 175 | +#define dns_nametree_unref(ptr) \ |
| 176 | + dns_nametree__unref(ptr, __func__, __FILE__, __LINE__) |
| 177 | +#define dns_nametree_attach(ptr, ptrp) \ |
| 178 | + dns_nametree__attach(ptr, ptrp, __func__, __FILE__, __LINE__) |
| 179 | +#define dns_nametree_detach(ptrp) \ |
| 180 | + dns_nametree__detach(ptrp, __func__, __FILE__, __LINE__) |
| 181 | +#define dns_ntnode_ref(ptr) dns_ntnode__ref(ptr, __func__, __FILE__, __LINE__) |
| 182 | +#define dns_ntnode_unref(ptr) \ |
| 183 | + dns_ntnode__unref(ptr, __func__, __FILE__, __LINE__) |
| 184 | +#define dns_ntnode_attach(ptr, ptrp) \ |
| 185 | + dns_ntnode__attach(ptr, ptrp, __func__, __FILE__, __LINE__) |
| 186 | +#define dns_ntnode_detach(ptrp) \ |
| 187 | + dns_ntnode__detach(ptrp, __func__, __FILE__, __LINE__) |
| 188 | +ISC_REFCOUNT_TRACE_DECL(dns_nametree); |
| 189 | +ISC_REFCOUNT_TRACE_DECL(dns_ntnode); |
| 190 | +#else |
| 191 | +ISC_REFCOUNT_DECL(dns_nametree); |
| 192 | +ISC_REFCOUNT_DECL(dns_ntnode); |
| 193 | +#endif |
| 194 | +ISC_LANG_ENDDECLS |
0 commit comments