2 * Copyright (C) 2004-2009, 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: rbt.h,v 1.77 2009/11/04 01:18:19 marka Exp $ */
23 /*! \file dns/rbt.h */
26 #include <isc/magic.h>
27 #include <isc/refcount.h>
29 #include <dns/types.h>
33 #define DNS_RBT_USEHASH 1
37 * Option values for dns_rbt_findnode() and dns_rbt_findname().
38 * These are used to form a bitmask.
40 #define DNS_RBTFIND_NOOPTIONS 0x00
41 #define DNS_RBTFIND_EMPTYDATA 0x01
42 #define DNS_RBTFIND_NOEXACT 0x02
43 #define DNS_RBTFIND_NOPREDECESSOR 0x04
46 #ifndef DNS_RBT_USEISCREFCOUNT
47 #ifdef ISC_REFCOUNT_HAVEATOMIC
48 #define DNS_RBT_USEISCREFCOUNT 1
53 * These should add up to 30.
55 #define DNS_RBT_LOCKLENGTH 10
56 #define DNS_RBT_REFLENGTH 20
58 #define DNS_RBTNODE_MAGIC ISC_MAGIC('R','B','N','O')
60 #define DNS_RBTNODE_VALID(n) ISC_MAGIC_VALID(n, DNS_RBTNODE_MAGIC)
62 #define DNS_RBTNODE_VALID(n) ISC_TRUE
66 * This is the structure that is used for each node in the red/black
67 * tree of trees. NOTE WELL: the implementation manages this as a variable
68 * length structure, with the actual wire-format name and other data
69 * appended to this structure. Allocating a contiguous block of memory for
70 * multiple dns_rbtnode structures will not work.
72 typedef struct dns_rbtnode dns_rbtnode_t;
74 DNS_RBT_NSEC_NORMAL=0, /* in main tree */
75 DNS_RBT_NSEC_HAS_NSEC=1, /* also has node in nsec tree */
76 DNS_RBT_NSEC_NSEC=2, /* in nsec tree */
77 DNS_RBT_NSEC_NSEC3=3 /* in nsec3 tree */
83 dns_rbtnode_t *parent;
87 #ifdef DNS_RBT_USEHASH
88 dns_rbtnode_t *hashnext;
92 * Used for LRU cache. This linked list is used to mark nodes which
93 * have no data any longer, but we cannot unlink at that exact moment
94 * because we did not or could not obtain a write lock on the tree.
96 ISC_LINK(dns_rbtnode_t) deadlink;
100 * The following bitfields add up to a total bitwidth of 32.
101 * The range of values necessary for each item is indicated,
102 * but in the case of "attributes" the field is wider to accommodate
103 * possible future expansion.
105 * In each case below the "range" indicated is what's _necessary_ for
106 * the bitfield to hold, not what it actually _can_ hold.
108 unsigned int is_root : 1; /*%< range is 0..1 */
109 unsigned int color : 1; /*%< range is 0..1 */
110 unsigned int find_callback : 1; /*%< range is 0..1 */
111 unsigned int attributes : 3; /*%< range is 0..2 */
112 unsigned int nsec : 2; /*%< range is 0..3 */
113 unsigned int namelen : 8; /*%< range is 1..255 */
114 unsigned int offsetlen : 8; /*%< range is 1..128 */
115 unsigned int oldnamelen : 8; /*%< range is 1..255 */
118 /* node needs to be cleaned from rpz */
119 unsigned int rpz : 1;
121 #ifdef DNS_RBT_USEHASH
122 unsigned int hashval;
127 * These values are used in the RBT DB implementation. The appropriate
128 * node lock must be held before accessing them.
131 unsigned int dirty:1;
133 unsigned int locknum:DNS_RBT_LOCKLENGTH;
134 #ifndef DNS_RBT_USEISCREFCOUNT
135 unsigned int references:DNS_RBT_REFLENGTH;
137 isc_refcount_t references; /* note that this is not in the bitfield */
142 typedef isc_result_t (*dns_rbtfindcallback_t)(dns_rbtnode_t *node,
151 * A chain is used to keep track of the sequence of nodes to reach any given
152 * node from the root of the tree. Originally nodes did not have parent
153 * pointers in them (for memory usage reasons) so there was no way to find
154 * the path back to the root from any given node. Now that nodes have parent
155 * pointers, chains might be going away in a future release, though the
156 * movement functionality would remain.
158 * In any event, parent information, whether via parent pointers or chains, is
159 * necessary information for iterating through the tree or for basic internal
160 * tree maintenance issues (ie, the rotations that are done to rebalance the
161 * tree when a node is added). The obvious implication of this is that for a
162 * chain to remain valid, the tree has to be locked down against writes for the
163 * duration of the useful life of the chain, because additions or removals can
164 * change the path from the root to the node the chain has targeted.
166 * The dns_rbtnodechain_ functions _first, _last, _prev and _next all take
167 * dns_name_t parameters for the name and the origin, which can be NULL. If
168 * non-NULL, 'name' will end up pointing to the name data and offsets that are
169 * stored at the node (and thus it will be read-only), so it should be a
170 * regular dns_name_t that has been initialized with dns_name_init. When
171 * 'origin' is non-NULL, it will get the name of the origin stored in it, so it
172 * needs to have its own buffer space and offsets, which is most easily
173 * accomplished with a dns_fixedname_t. It is _not_ necessary to reinitialize
174 * either 'name' or 'origin' between calls to the chain functions.
176 * NOTE WELL: even though the name data at the root of the tree of trees will
177 * be absolute (typically just "."), it will will be made into a relative name
178 * with an origin of "." -- an empty name when the node is ".". This is
179 * because a common on operation on 'name' and 'origin' is to use
180 * dns_name_concatenate() on them to generate the complete name. An empty name
181 * can be detected when dns_name_countlabels == 0, and is printed by
182 * dns_name_totext()/dns_name_format() as "@", consistent with RFC1035's
183 * definition of "@" as the current origin.
185 * dns_rbtnodechain_current is similar to the _first, _last, _prev and _next
186 * functions but additionally can provide the node to which the chain points.
190 * The number of level blocks to allocate at a time. Currently the maximum
191 * number of levels is allocated directly in the structure, but future
192 * revisions of this code might have a static initial block with dynamic
193 * growth. Allocating space for 256 levels when the tree is almost never that
194 * deep is wasteful, but it's not clear that it matters, since the waste is
195 * only 2MB for 1000 concurrently active chains on a system with 64-bit
198 #define DNS_RBT_LEVELBLOCK 254
200 typedef struct dns_rbtnodechain {
204 * The terminal node of the chain. It is not in levels[].
205 * This is ostensibly private ... but in a pinch it could be
206 * used tell that the chain points nowhere without needing to
207 * call dns_rbtnodechain_current().
211 * The maximum number of labels in a name is 128; bitstrings mean
212 * a conceptually very large number (which I have not bothered to
213 * compute) of logical levels because splitting can potentially occur
214 * at each bit. However, DNSSEC restricts the number of "logical"
215 * labels in a name to 255, meaning only 254 pointers are needed
218 dns_rbtnode_t * levels[DNS_RBT_LEVELBLOCK];
220 * level_count indicates how deep the chain points into the
221 * tree of trees, and is the index into the levels[] array.
222 * Thus, levels[level_count - 1] is the last level node stored.
223 * A chain that points to the top level of the tree of trees has
224 * a level_count of 0, the first level has a level_count of 1, and
227 unsigned int level_count;
229 * level_matches tells how many levels matched above the node
230 * returned by dns_rbt_findnode(). A match (partial or exact) found
231 * in the first level thus results in level_matches being set to 1.
232 * This is used by the rbtdb to set the start point for a recursive
233 * search of superdomains until the RR it is looking for is found.
235 unsigned int level_matches;
236 } dns_rbtnodechain_t;
239 ***** Public interfaces.
242 dns_rbt_create(isc_mem_t *mctx, void (*deleter)(void *, void *),
243 void *deleter_arg, dns_rbt_t **rbtp);
245 * Initialize a red-black tree of trees.
248 *\li The deleter argument, if non-null, points to a function that is
249 * responsible for cleaning up any memory associated with the data
250 * pointer of a node when the node is deleted. It is passed the
251 * deleted node's data pointer as its first argument and deleter_arg
252 * as its second argument.
255 * \li mctx is a pointer to a valid memory context.
256 *\li rbtp != NULL && *rbtp == NULL
257 *\li arg == NULL iff deleter == NULL
260 *\li If result is ISC_R_SUCCESS:
261 * *rbtp points to a valid red-black tree manager
263 *\li If result is failure:
264 * *rbtp does not point to a valid red-black tree manager.
267 *\li #ISC_R_SUCCESS Success
268 *\li #ISC_R_NOMEMORY Resource limit: Out of Memory
272 dns_rbt_addname(dns_rbt_t *rbt, dns_name_t *name, void *data);
274 * Add 'name' to the tree of trees, associated with 'data'.
277 *\li 'data' is never required to be non-NULL, but specifying it
278 * when the name is added is faster than searching for 'name'
279 * again and then setting the data pointer. The lack of a data pointer
280 * for a node also has other ramifications regarding whether
281 * dns_rbt_findname considers a node to exist, or dns_rbt_deletename
285 *\li rbt is a valid rbt manager.
286 *\li dns_name_isabsolute(name) == TRUE
289 *\li 'name' is not altered in any way.
291 *\li Any external references to nodes in the tree are unaffected by
292 * node splits that are necessary to insert the new name.
294 *\li If result is #ISC_R_SUCCESS:
295 * 'name' is findable in the red/black tree of trees in O(log N).
296 * The data pointer of the node for 'name' is set to 'data'.
298 *\li If result is #ISC_R_EXISTS or #ISC_R_NOSPACE:
299 * The tree of trees is unaltered.
301 *\li If result is #ISC_R_NOMEMORY:
305 *\li #ISC_R_SUCCESS Success
306 *\li #ISC_R_EXISTS The name already exists with associated data.
307 *\li #ISC_R_NOSPACE The name had more logical labels than are allowed.
308 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory
312 dns_rbt_addnode(dns_rbt_t *rbt, dns_name_t *name, dns_rbtnode_t **nodep);
315 * Just like dns_rbt_addname, but returns the address of the node.
318 *\li rbt is a valid rbt structure.
319 *\li dns_name_isabsolute(name) == TRUE
320 *\li nodep != NULL && *nodep == NULL
323 *\li 'name' is not altered in any way.
325 *\li Any external references to nodes in the tree are unaffected by
326 * node splits that are necessary to insert the new name.
328 *\li If result is ISC_R_SUCCESS:
329 * 'name' is findable in the red/black tree of trees in O(log N).
330 * *nodep is the node that was added for 'name'.
332 *\li If result is ISC_R_EXISTS:
333 * The tree of trees is unaltered.
334 * *nodep is the existing node for 'name'.
336 *\li If result is ISC_R_NOMEMORY:
340 *\li #ISC_R_SUCCESS Success
341 *\li #ISC_R_EXISTS The name already exists, possibly without data.
342 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory
346 dns_rbt_findname(dns_rbt_t *rbt, dns_name_t *name, unsigned int options,
347 dns_name_t *foundname, void **data);
349 * Get the data pointer associated with 'name'.
352 *\li When #DNS_RBTFIND_NOEXACT is set, the closest matching superdomain is
353 * returned (also subject to #DNS_RBTFIND_EMPTYDATA), even when there is
354 * an exact match in the tree.
356 *\li A node that has no data is considered not to exist for this function,
357 * unless the #DNS_RBTFIND_EMPTYDATA option is set.
360 *\li rbt is a valid rbt manager.
361 *\li dns_name_isabsolute(name) == TRUE
362 *\li data != NULL && *data == NULL
365 *\li 'name' and the tree are not altered in any way.
367 *\li If result is ISC_R_SUCCESS:
368 * *data is the data associated with 'name'.
370 *\li If result is DNS_R_PARTIALMATCH:
371 * *data is the data associated with the deepest superdomain
372 * of 'name' which has data.
374 *\li If result is ISC_R_NOTFOUND:
375 * Neither the name nor a superdomain was found with data.
378 *\li #ISC_R_SUCCESS Success
379 *\li #DNS_R_PARTIALMATCH Superdomain found with data
380 *\li #ISC_R_NOTFOUND No match
381 *\li #ISC_R_NOSPACE Concatenating nodes to form foundname failed
385 dns_rbt_findnode(dns_rbt_t *rbt, dns_name_t *name, dns_name_t *foundname,
386 dns_rbtnode_t **node, dns_rbtnodechain_t *chain,
387 unsigned int options, dns_rbtfindcallback_t callback,
390 * Find the node for 'name'.
393 *\li A node that has no data is considered not to exist for this function,
394 * unless the DNS_RBTFIND_EMPTYDATA option is set. This applies to both
395 * exact matches and partial matches.
397 *\li If the chain parameter is non-NULL, then the path through the tree
398 * to the DNSSEC predecessor of the searched for name is maintained,
399 * unless the DNS_RBTFIND_NOPREDECESSOR or DNS_RBTFIND_NOEXACT option
400 * is used. (For more details on those options, see below.)
402 *\li If there is no predecessor, then the chain will point to nowhere, as
403 * indicated by chain->end being NULL or dns_rbtnodechain_current
404 * returning ISC_R_NOTFOUND. Note that in a normal Internet DNS RBT
405 * there will always be a predecessor for all names except the root
406 * name, because '.' will exist and '.' is the predecessor of
407 * everything. But you can certainly construct a trivial tree and a
408 * search for it that has no predecessor.
410 *\li Within the chain structure, the 'levels' member of the structure holds
411 * the root node of each level except the first.
413 *\li The 'level_count' of the chain indicates how deep the chain to the
414 * predecessor name is, as an index into the 'levels[]' array. It does
415 * not count name elements, per se, but only levels of the tree of trees,
416 * the distinction arising because multiple labels from a name can be
417 * stored on only one level. It is also does not include the level
418 * that has the node, since that level is not stored in levels[].
420 *\li The chain's 'level_matches' is not directly related to the predecessor.
421 * It is the number of levels above the level of the found 'node',
422 * regardless of whether it was a partial match or exact match. When
423 * the node is found in the top level tree, or no node is found at all,
424 * level_matches is 0.
426 *\li When DNS_RBTFIND_NOEXACT is set, the closest matching superdomain is
427 * returned (also subject to DNS_RBTFIND_EMPTYDATA), even when
428 * there is an exact match in the tree. In this case, the chain
429 * will not point to the DNSSEC predecessor, but will instead point
430 * to the exact match, if there was any. Thus the preceding paragraphs
431 * should have "exact match" substituted for "predecessor" to describe
432 * how the various elements of the chain are set. This was done to
433 * ensure that the chain's state was sane, and to prevent problems that
434 * occurred when running the predecessor location code under conditions
435 * it was not designed for. It is not clear *where* the chain should
436 * point when DNS_RBTFIND_NOEXACT is set, so if you end up using a chain
437 * with this option because you want a particular node, let us know
438 * where you want the chain pointed, so this can be made more firm.
441 *\li rbt is a valid rbt manager.
442 *\li dns_name_isabsolute(name) == TRUE.
443 *\li node != NULL && *node == NULL.
444 *\li #DNS_RBTFIND_NOEXACT and DNS_RBTFIND_NOPREDECESSOR are mutually
448 *\li 'name' and the tree are not altered in any way.
450 *\li If result is ISC_R_SUCCESS:
452 * *node is the terminal node for 'name'.
454 * 'foundname' and 'name' represent the same name (though not
457 * 'chain' points to the DNSSEC predecessor, if any, of 'name'.
459 * chain->level_matches and chain->level_count are equal.
462 * If result is DNS_R_PARTIALMATCH:
464 * *node is the data associated with the deepest superdomain
465 * of 'name' which has data.
467 * 'foundname' is the name of deepest superdomain (which has
468 * data, unless the DNS_RBTFIND_EMPTYDATA option is set).
470 * 'chain' points to the DNSSEC predecessor, if any, of 'name'.
473 *\li If result is ISC_R_NOTFOUND:
475 * Neither the name nor a superdomain was found. *node is NULL.
477 * 'chain' points to the DNSSEC predecessor, if any, of 'name'.
479 * chain->level_matches is 0.
483 *\li #ISC_R_SUCCESS Success
484 *\li #DNS_R_PARTIALMATCH Superdomain found with data
485 *\li #ISC_R_NOTFOUND No match, or superdomain with no data
486 *\li #ISC_R_NOSPACE Concatenating nodes to form foundname failed
490 dns_rbt_deletename(dns_rbt_t *rbt, dns_name_t *name, isc_boolean_t recurse);
492 * Delete 'name' from the tree of trees.
495 *\li When 'name' is removed, if recurse is ISC_TRUE then all of its
496 * subnames are removed too.
499 *\li rbt is a valid rbt manager.
500 *\li dns_name_isabsolute(name) == TRUE
503 *\li 'name' is not altered in any way.
505 *\li Does NOT ensure that any external references to nodes in the tree
506 * are unaffected by node joins.
508 *\li If result is ISC_R_SUCCESS:
509 * 'name' does not appear in the tree with data; however,
510 * the node for the name might still exist which can be
511 * found with dns_rbt_findnode (but not dns_rbt_findname).
513 *\li If result is ISC_R_NOTFOUND:
514 * 'name' does not appear in the tree with data, because
515 * it did not appear in the tree before the function was called.
517 *\li If result is something else:
518 * See result codes for dns_rbt_findnode (if it fails, the
519 * node is not deleted) or dns_rbt_deletenode (if it fails,
520 * the node is deleted, but the tree is not optimized when
521 * it could have been).
524 *\li #ISC_R_SUCCESS Success
525 *\li #ISC_R_NOTFOUND No match
526 *\li something_else Any return code from dns_rbt_findnode except
527 * DNS_R_PARTIALMATCH (which causes ISC_R_NOTFOUND
528 * to be returned instead), and any code from
529 * dns_rbt_deletenode.
533 dns_rbt_deletenode(dns_rbt_t *rbt, dns_rbtnode_t *node, isc_boolean_t recurse);
535 * Delete 'node' from the tree of trees.
538 *\li When 'node' is removed, if recurse is ISC_TRUE then all nodes
539 * in levels down from it are removed too.
542 *\li rbt is a valid rbt manager.
546 *\li Does NOT ensure that any external references to nodes in the tree
547 * are unaffected by node joins.
549 *\li If result is ISC_R_SUCCESS:
550 * 'node' does not appear in the tree with data; however,
551 * the node might still exist if it serves as a pointer to
552 * a lower tree level as long as 'recurse' was false, hence
553 * the node could can be found with dns_rbt_findnode when
554 * that function's empty_data_ok parameter is true.
556 *\li If result is ISC_R_NOMEMORY or ISC_R_NOSPACE:
557 * The node was deleted, but the tree structure was not
561 *\li #ISC_R_SUCCESS Success
562 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory when joining nodes.
563 *\li #ISC_R_NOSPACE dns_name_concatenate failed when joining nodes.
567 dns_rbt_namefromnode(dns_rbtnode_t *node, dns_name_t *name);
569 * Convert the sequence of labels stored at 'node' into a 'name'.
572 *\li This function does not return the full name, from the root, but
573 * just the labels at the indicated node.
575 *\li The name data pointed to by 'name' is the information stored
576 * in the node, not a copy. Altering the data at this pointer
577 * will likely cause grief.
580 * \li name->offsets == NULL
583 * \li 'name' is DNS_NAMEATTR_READONLY.
585 * \li 'name' will point directly to the labels stored after the
586 * dns_rbtnode_t struct.
588 * \li 'name' will have offsets that also point to the information stored
589 * as part of the node.
593 dns_rbt_fullnamefromnode(dns_rbtnode_t *node, dns_name_t *name);
595 * Like dns_rbt_namefromnode, but returns the full name from the root.
598 * \li Unlike dns_rbt_namefromnode, the name will not point directly
599 * to node data. Rather, dns_name_concatenate will be used to copy
600 * the name data from each node into the 'name' argument.
604 * \li name has a dedicated buffer.
608 * \li ISC_R_NOSPACE (possible via dns_name_concatenate)
609 * \li DNS_R_NAMETOOLONG (possible via dns_name_concatenate)
613 dns_rbt_formatnodename(dns_rbtnode_t *node, char *printname,
616 * Format the full name of a node for printing, using dns_name_format().
619 * \li 'size' is the length of the printname buffer. This should be
620 * DNS_NAME_FORMATSIZE or larger.
623 * \li node and printname are not NULL.
626 * \li The 'printname' pointer.
630 dns_rbt_nodecount(dns_rbt_t *rbt);
632 * Obtain the number of nodes in the tree of trees.
635 * \li rbt is a valid rbt manager.
639 dns_rbt_destroy(dns_rbt_t **rbtp);
641 dns_rbt_destroy2(dns_rbt_t **rbtp, unsigned int quantum);
643 * Stop working with a red-black tree of trees.
644 * If 'quantum' is zero then the entire tree will be destroyed.
645 * If 'quantum' is non zero then up to 'quantum' nodes will be destroyed
646 * allowing the rbt to be incrementally destroyed by repeated calls to
647 * dns_rbt_destroy2(). Once dns_rbt_destroy2() has been called no other
648 * operations than dns_rbt_destroy()/dns_rbt_destroy2() should be
649 * performed on the tree of trees.
652 * \li *rbt is a valid rbt manager.
654 * Ensures on ISC_R_SUCCESS:
655 * \li All space allocated by the RBT library has been returned.
657 * \li *rbt is invalidated as an rbt manager.
661 * \li ISC_R_QUOTA if 'quantum' nodes have been destroyed.
665 dns_rbt_printall(dns_rbt_t *rbt);
667 * Print an ASCII representation of the internal structure of the red-black
671 * \li The name stored at each node, along with the node's color, is printed.
672 * Then the down pointer, left and right pointers are displayed
673 * recursively in turn. NULL down pointers are silently omitted;
674 * NULL left and right pointers are printed.
678 ***** Chain Functions
682 dns_rbtnodechain_init(dns_rbtnodechain_t *chain, isc_mem_t *mctx);
684 * Initialize 'chain'.
687 *\li 'chain' is a valid pointer.
689 *\li 'mctx' is a valid memory context.
692 *\li 'chain' is suitable for use.
696 dns_rbtnodechain_reset(dns_rbtnodechain_t *chain);
698 * Free any dynamic storage associated with 'chain', and then reinitialize
702 *\li 'chain' is a valid pointer.
705 *\li 'chain' is suitable for use, and uses no dynamic storage.
709 dns_rbtnodechain_invalidate(dns_rbtnodechain_t *chain);
711 * Free any dynamic storage associated with 'chain', and then invalidates it.
714 *\li Future calls to any dns_rbtnodechain_ function will need to call
715 * dns_rbtnodechain_init on the chain first (except, of course,
716 * dns_rbtnodechain_init itself).
719 *\li 'chain' is a valid chain.
722 *\li 'chain' is no longer suitable for use, and uses no dynamic storage.
726 dns_rbtnodechain_current(dns_rbtnodechain_t *chain, dns_name_t *name,
727 dns_name_t *origin, dns_rbtnode_t **node);
729 * Provide the name, origin and node to which the chain is currently pointed.
732 *\li The tree need not have be locked against additions for the chain
733 * to remain valid, however there are no guarantees if any deletion
734 * has been made since the chain was established.
737 *\li 'chain' is a valid chain.
740 *\li 'node', if non-NULL, is the node to which the chain was pointed
741 * by dns_rbt_findnode, dns_rbtnodechain_first or dns_rbtnodechain_last.
742 * If none were called for the chain since it was initialized or reset,
743 * or if the was no predecessor to the name searched for with
744 * dns_rbt_findnode, then '*node' is NULL and ISC_R_NOTFOUND is returned.
746 *\li 'name', if non-NULL, is the name stored at the terminal level of
747 * the chain. This is typically a single label, like the "www" of
748 * "www.isc.org", but need not be so. At the root of the tree of trees,
749 * if the node is "." then 'name' is ".", otherwise it is relative to ".".
750 * (Minimalist and atypical case: if the tree has just the name
751 * "isc.org." then the root node's stored name is "isc.org." but 'name'
752 * will be "isc.org".)
754 *\li 'origin', if non-NULL, is the sequence of labels in the levels
755 * above the terminal level, such as "isc.org." in the above example.
756 * 'origin' is always "." for the root node.
760 *\li #ISC_R_SUCCESS name, origin & node were successfully set.
761 *\li #ISC_R_NOTFOUND The chain does not point to any node.
762 *\li <something_else> Any error return from dns_name_concatenate.
766 dns_rbtnodechain_first(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
767 dns_name_t *name, dns_name_t *origin);
769 * Set the chain to the lexically first node in the tree of trees.
772 *\li By the definition of ordering for DNS names, the root of the tree of
773 * trees is the very first node, since everything else in the megatree
774 * uses it as a common suffix.
777 *\li 'chain' is a valid chain.
778 *\li 'rbt' is a valid rbt manager.
781 *\li The chain points to the very first node of the tree.
783 *\li 'name' and 'origin', if non-NULL, are set as described for
784 * dns_rbtnodechain_current. Thus 'origin' will always be ".".
787 *\li #DNS_R_NEWORIGIN The name & origin were successfully set.
788 *\li <something_else> Any error result from dns_rbtnodechain_current.
792 dns_rbtnodechain_last(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
793 dns_name_t *name, dns_name_t *origin);
795 * Set the chain to the lexically last node in the tree of trees.
798 *\li 'chain' is a valid chain.
799 *\li 'rbt' is a valid rbt manager.
802 *\li The chain points to the very last node of the tree.
804 *\li 'name' and 'origin', if non-NULL, are set as described for
805 * dns_rbtnodechain_current.
808 *\li #DNS_R_NEWORIGIN The name & origin were successfully set.
809 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory building chain.
810 *\li <something_else> Any error result from dns_name_concatenate.
814 dns_rbtnodechain_prev(dns_rbtnodechain_t *chain, dns_name_t *name,
817 * Adjusts chain to point the DNSSEC predecessor of the name to which it
818 * is currently pointed.
821 *\li 'chain' is a valid chain.
822 *\li 'chain' has been pointed somewhere in the tree with dns_rbt_findnode,
823 * dns_rbtnodechain_first or dns_rbtnodechain_last -- and remember that
824 * dns_rbt_findnode is not guaranteed to point the chain somewhere,
825 * since there may have been no predecessor to the searched for name.
828 *\li The chain is pointed to the predecessor of its current target.
830 *\li 'name' and 'origin', if non-NULL, are set as described for
831 * dns_rbtnodechain_current.
833 *\li 'origin' is only if a new origin was found.
836 *\li #ISC_R_SUCCESS The predecessor was found and 'name' was set.
837 *\li #DNS_R_NEWORIGIN The predecessor was found with a different
838 * origin and 'name' and 'origin' were set.
839 *\li #ISC_R_NOMORE There was no predecessor.
840 *\li <something_else> Any error result from dns_rbtnodechain_current.
844 dns_rbtnodechain_next(dns_rbtnodechain_t *chain, dns_name_t *name,
847 * Adjusts chain to point the DNSSEC successor of the name to which it
848 * is currently pointed.
851 *\li 'chain' is a valid chain.
852 *\li 'chain' has been pointed somewhere in the tree with dns_rbt_findnode,
853 * dns_rbtnodechain_first or dns_rbtnodechain_last -- and remember that
854 * dns_rbt_findnode is not guaranteed to point the chain somewhere,
855 * since there may have been no predecessor to the searched for name.
858 *\li The chain is pointed to the successor of its current target.
860 *\li 'name' and 'origin', if non-NULL, are set as described for
861 * dns_rbtnodechain_current.
863 *\li 'origin' is only if a new origin was found.
866 *\li #ISC_R_SUCCESS The successor was found and 'name' was set.
867 *\li #DNS_R_NEWORIGIN The successor was found with a different
868 * origin and 'name' and 'origin' were set.
869 *\li #ISC_R_NOMORE There was no successor.
870 *\li <something_else> Any error result from dns_name_concatenate.
874 dns_rbtnodechain_down(dns_rbtnodechain_t *chain, dns_name_t *name,
877 * Descend down if possible.
881 dns_rbtnodechain_nextflat(dns_rbtnodechain_t *chain, dns_name_t *name);
883 * Find the next node at the current depth in DNSSEC order.
887 * Wrapper macros for manipulating the rbtnode reference counter:
888 * Since we selectively use isc_refcount_t for the reference counter of
889 * a rbtnode, operations on the counter depend on the actual type of it.
890 * The following macros provide a common interface to these operations,
891 * hiding the back-end. The usage is the same as that of isc_refcount_xxx().
893 #ifdef DNS_RBT_USEISCREFCOUNT
894 #define dns_rbtnode_refinit(node, n) \
896 isc_refcount_init(&(node)->references, (n)); \
898 #define dns_rbtnode_refdestroy(node) \
900 isc_refcount_destroy(&(node)->references); \
902 #define dns_rbtnode_refcurrent(node) \
903 isc_refcount_current(&(node)->references)
904 #define dns_rbtnode_refincrement0(node, refs) \
906 isc_refcount_increment0(&(node)->references, (refs)); \
908 #define dns_rbtnode_refincrement(node, refs) \
910 isc_refcount_increment(&(node)->references, (refs)); \
912 #define dns_rbtnode_refdecrement(node, refs) \
914 isc_refcount_decrement(&(node)->references, (refs)); \
916 #else /* DNS_RBT_USEISCREFCOUNT */
917 #define dns_rbtnode_refinit(node, n) ((node)->references = (n))
918 #define dns_rbtnode_refdestroy(node) REQUIRE((node)->references == 0)
919 #define dns_rbtnode_refcurrent(node) ((node)->references)
921 #if (__STDC_VERSION__ + 0) >= 199901L || defined __GNUC__
923 dns_rbtnode_refincrement0(dns_rbtnode_t *node, unsigned int *refs) {
926 *refs = node->references;
930 dns_rbtnode_refincrement(dns_rbtnode_t *node, unsigned int *refs) {
931 REQUIRE(node->references > 0);
934 *refs = node->references;
938 dns_rbtnode_refdecrement(dns_rbtnode_t *node, unsigned int *refs) {
939 REQUIRE(node->references > 0);
942 *refs = node->references;
945 #define dns_rbtnode_refincrement0(node, refs) \
947 unsigned int *_tmp = (unsigned int *)(refs); \
948 (node)->references++; \
949 if ((_tmp) != NULL) \
950 (*_tmp) = (node)->references; \
952 #define dns_rbtnode_refincrement(node, refs) \
954 REQUIRE((node)->references > 0); \
955 (node)->references++; \
956 if ((refs) != NULL) \
957 (*refs) = (node)->references; \
959 #define dns_rbtnode_refdecrement(node, refs) \
961 REQUIRE((node)->references > 0); \
962 (node)->references--; \
963 if ((refs) != NULL) \
964 (*refs) = (node)->references; \
967 #endif /* DNS_RBT_USEISCREFCOUNT */
971 #endif /* DNS_RBT_H */