2 * Copyright (C) 2004-2009 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.71.48.3 2009/10/20 05:06:04 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;
77 dns_rbtnode_t *parent;
81 #ifdef DNS_RBT_USEHASH
82 dns_rbtnode_t *hashnext;
86 * Used for LRU cache. This linked list is used to mark nodes which
87 * have no data any longer, but we cannot unlink at that exact moment
88 * because we did not or could not obtain a write lock on the tree.
90 ISC_LINK(dns_rbtnode_t) deadlink;
94 * The following bitfields add up to a total bitwidth of 32.
95 * The range of values necessary for each item is indicated,
96 * but in the case of "attributes" the field is wider to accommodate
97 * possible future expansion. "offsetlen" could be one bit
98 * narrower by always adjusting its value by 1 to find the real
99 * offsetlen, but doing so does not gain anything (except perhaps
100 * another bit for "attributes", which doesn't yet need any more).
102 * In each case below the "range" indicated is what's _necessary_ for
103 * the bitfield to hold, not what it actually _can_ hold.
105 unsigned int is_root : 1; /*%< range is 0..1 */
106 unsigned int color : 1; /*%< range is 0..1 */
107 unsigned int find_callback : 1; /*%< range is 0..1 */
108 unsigned int attributes : 4; /*%< range is 0..2 */
109 unsigned int nsec3 : 1; /*%< range is 0..1 */
110 unsigned int namelen : 8; /*%< range is 1..255 */
111 unsigned int offsetlen : 8; /*%< range is 1..128 */
112 unsigned int oldnamelen : 8; /*%< range is 1..255 */
115 #ifdef DNS_RBT_USEHASH
116 unsigned int hashval;
121 * These values are used in the RBT DB implementation. The appropriate
122 * node lock must be held before accessing them.
125 unsigned int dirty:1;
127 unsigned int locknum:DNS_RBT_LOCKLENGTH;
128 #ifndef DNS_RBT_USEISCREFCOUNT
129 unsigned int references:DNS_RBT_REFLENGTH;
131 isc_refcount_t references; /* note that this is not in the bitfield */
136 typedef isc_result_t (*dns_rbtfindcallback_t)(dns_rbtnode_t *node,
145 * A chain is used to keep track of the sequence of nodes to reach any given
146 * node from the root of the tree. Originally nodes did not have parent
147 * pointers in them (for memory usage reasons) so there was no way to find
148 * the path back to the root from any given node. Now that nodes have parent
149 * pointers, chains might be going away in a future release, though the
150 * movement functionality would remain.
152 * In any event, parent information, whether via parent pointers or chains, is
153 * necessary information for iterating through the tree or for basic internal
154 * tree maintenance issues (ie, the rotations that are done to rebalance the
155 * tree when a node is added). The obvious implication of this is that for a
156 * chain to remain valid, the tree has to be locked down against writes for the
157 * duration of the useful life of the chain, because additions or removals can
158 * change the path from the root to the node the chain has targeted.
160 * The dns_rbtnodechain_ functions _first, _last, _prev and _next all take
161 * dns_name_t parameters for the name and the origin, which can be NULL. If
162 * non-NULL, 'name' will end up pointing to the name data and offsets that are
163 * stored at the node (and thus it will be read-only), so it should be a
164 * regular dns_name_t that has been initialized with dns_name_init. When
165 * 'origin' is non-NULL, it will get the name of the origin stored in it, so it
166 * needs to have its own buffer space and offsets, which is most easily
167 * accomplished with a dns_fixedname_t. It is _not_ necessary to reinitialize
168 * either 'name' or 'origin' between calls to the chain functions.
170 * NOTE WELL: even though the name data at the root of the tree of trees will
171 * be absolute (typically just "."), it will will be made into a relative name
172 * with an origin of "." -- an empty name when the node is ".". This is
173 * because a common on operation on 'name' and 'origin' is to use
174 * dns_name_concatenate() on them to generate the complete name. An empty name
175 * can be detected when dns_name_countlabels == 0, and is printed by
176 * dns_name_totext()/dns_name_format() as "@", consistent with RFC1035's
177 * definition of "@" as the current origin.
179 * dns_rbtnodechain_current is similar to the _first, _last, _prev and _next
180 * functions but additionally can provide the node to which the chain points.
184 * The number of level blocks to allocate at a time. Currently the maximum
185 * number of levels is allocated directly in the structure, but future
186 * revisions of this code might have a static initial block with dynamic
187 * growth. Allocating space for 256 levels when the tree is almost never that
188 * deep is wasteful, but it's not clear that it matters, since the waste is
189 * only 2MB for 1000 concurrently active chains on a system with 64-bit
192 #define DNS_RBT_LEVELBLOCK 254
194 typedef struct dns_rbtnodechain {
198 * The terminal node of the chain. It is not in levels[].
199 * This is ostensibly private ... but in a pinch it could be
200 * used tell that the chain points nowhere without needing to
201 * call dns_rbtnodechain_current().
205 * The maximum number of labels in a name is 128; bitstrings mean
206 * a conceptually very large number (which I have not bothered to
207 * compute) of logical levels because splitting can potentially occur
208 * at each bit. However, DNSSEC restricts the number of "logical"
209 * labels in a name to 255, meaning only 254 pointers are needed
212 dns_rbtnode_t * levels[DNS_RBT_LEVELBLOCK];
214 * level_count indicates how deep the chain points into the
215 * tree of trees, and is the index into the levels[] array.
216 * Thus, levels[level_count - 1] is the last level node stored.
217 * A chain that points to the top level of the tree of trees has
218 * a level_count of 0, the first level has a level_count of 1, and
221 unsigned int level_count;
223 * level_matches tells how many levels matched above the node
224 * returned by dns_rbt_findnode(). A match (partial or exact) found
225 * in the first level thus results in level_matches being set to 1.
226 * This is used by the rbtdb to set the start point for a recursive
227 * search of superdomains until the RR it is looking for is found.
229 unsigned int level_matches;
230 } dns_rbtnodechain_t;
233 ***** Public interfaces.
236 dns_rbt_create(isc_mem_t *mctx, void (*deleter)(void *, void *),
237 void *deleter_arg, dns_rbt_t **rbtp);
239 * Initialize a red-black tree of trees.
242 *\li The deleter argument, if non-null, points to a function that is
243 * responsible for cleaning up any memory associated with the data
244 * pointer of a node when the node is deleted. It is passed the
245 * deleted node's data pointer as its first argument and deleter_arg
246 * as its second argument.
249 * \li mctx is a pointer to a valid memory context.
250 *\li rbtp != NULL && *rbtp == NULL
251 *\li arg == NULL iff deleter == NULL
254 *\li If result is ISC_R_SUCCESS:
255 * *rbtp points to a valid red-black tree manager
257 *\li If result is failure:
258 * *rbtp does not point to a valid red-black tree manager.
261 *\li #ISC_R_SUCCESS Success
262 *\li #ISC_R_NOMEMORY Resource limit: Out of Memory
266 dns_rbt_addname(dns_rbt_t *rbt, dns_name_t *name, void *data);
268 * Add 'name' to the tree of trees, associated with 'data'.
271 *\li 'data' is never required to be non-NULL, but specifying it
272 * when the name is added is faster than searching for 'name'
273 * again and then setting the data pointer. The lack of a data pointer
274 * for a node also has other ramifications regarding whether
275 * dns_rbt_findname considers a node to exist, or dns_rbt_deletename
279 *\li rbt is a valid rbt manager.
280 *\li dns_name_isabsolute(name) == TRUE
283 *\li 'name' is not altered in any way.
285 *\li Any external references to nodes in the tree are unaffected by
286 * node splits that are necessary to insert the new name.
288 *\li If result is #ISC_R_SUCCESS:
289 * 'name' is findable in the red/black tree of trees in O(log N).
290 * The data pointer of the node for 'name' is set to 'data'.
292 *\li If result is #ISC_R_EXISTS or #ISC_R_NOSPACE:
293 * The tree of trees is unaltered.
295 *\li If result is #ISC_R_NOMEMORY:
299 *\li #ISC_R_SUCCESS Success
300 *\li #ISC_R_EXISTS The name already exists with associated data.
301 *\li #ISC_R_NOSPACE The name had more logical labels than are allowed.
302 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory
306 dns_rbt_addnode(dns_rbt_t *rbt, dns_name_t *name, dns_rbtnode_t **nodep);
309 * Just like dns_rbt_addname, but returns the address of the node.
312 *\li rbt is a valid rbt structure.
313 *\li dns_name_isabsolute(name) == TRUE
314 *\li nodep != NULL && *nodep == NULL
317 *\li 'name' is not altered in any way.
319 *\li Any external references to nodes in the tree are unaffected by
320 * node splits that are necessary to insert the new name.
322 *\li If result is ISC_R_SUCCESS:
323 * 'name' is findable in the red/black tree of trees in O(log N).
324 * *nodep is the node that was added for 'name'.
326 *\li If result is ISC_R_EXISTS:
327 * The tree of trees is unaltered.
328 * *nodep is the existing node for 'name'.
330 *\li If result is ISC_R_NOMEMORY:
334 *\li #ISC_R_SUCCESS Success
335 *\li #ISC_R_EXISTS The name already exists, possibly without data.
336 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory
340 dns_rbt_findname(dns_rbt_t *rbt, dns_name_t *name, unsigned int options,
341 dns_name_t *foundname, void **data);
343 * Get the data pointer associated with 'name'.
346 *\li When #DNS_RBTFIND_NOEXACT is set, the closest matching superdomain is
347 * returned (also subject to #DNS_RBTFIND_EMPTYDATA), even when there is
348 * an exact match in the tree.
350 *\li A node that has no data is considered not to exist for this function,
351 * unless the #DNS_RBTFIND_EMPTYDATA option is set.
354 *\li rbt is a valid rbt manager.
355 *\li dns_name_isabsolute(name) == TRUE
356 *\li data != NULL && *data == NULL
359 *\li 'name' and the tree are not altered in any way.
361 *\li If result is ISC_R_SUCCESS:
362 * *data is the data associated with 'name'.
364 *\li If result is DNS_R_PARTIALMATCH:
365 * *data is the data associated with the deepest superdomain
366 * of 'name' which has data.
368 *\li If result is ISC_R_NOTFOUND:
369 * Neither the name nor a superdomain was found with data.
372 *\li #ISC_R_SUCCESS Success
373 *\li #DNS_R_PARTIALMATCH Superdomain found with data
374 *\li #ISC_R_NOTFOUND No match
375 *\li #ISC_R_NOSPACE Concatenating nodes to form foundname failed
379 dns_rbt_findnode(dns_rbt_t *rbt, dns_name_t *name, dns_name_t *foundname,
380 dns_rbtnode_t **node, dns_rbtnodechain_t *chain,
381 unsigned int options, dns_rbtfindcallback_t callback,
384 * Find the node for 'name'.
387 *\li A node that has no data is considered not to exist for this function,
388 * unless the DNS_RBTFIND_EMPTYDATA option is set. This applies to both
389 * exact matches and partial matches.
391 *\li If the chain parameter is non-NULL, then the path through the tree
392 * to the DNSSEC predecessor of the searched for name is maintained,
393 * unless the DNS_RBTFIND_NOPREDECESSOR or DNS_RBTFIND_NOEXACT option
394 * is used. (For more details on those options, see below.)
396 *\li If there is no predecessor, then the chain will point to nowhere, as
397 * indicated by chain->end being NULL or dns_rbtnodechain_current
398 * returning ISC_R_NOTFOUND. Note that in a normal Internet DNS RBT
399 * there will always be a predecessor for all names except the root
400 * name, because '.' will exist and '.' is the predecessor of
401 * everything. But you can certainly construct a trivial tree and a
402 * search for it that has no predecessor.
404 *\li Within the chain structure, the 'levels' member of the structure holds
405 * the root node of each level except the first.
407 *\li The 'level_count' of the chain indicates how deep the chain to the
408 * predecessor name is, as an index into the 'levels[]' array. It does
409 * not count name elements, per se, but only levels of the tree of trees,
410 * the distinction arising because multiple labels from a name can be
411 * stored on only one level. It is also does not include the level
412 * that has the node, since that level is not stored in levels[].
414 *\li The chain's 'level_matches' is not directly related to the predecessor.
415 * It is the number of levels above the level of the found 'node',
416 * regardless of whether it was a partial match or exact match. When
417 * the node is found in the top level tree, or no node is found at all,
418 * level_matches is 0.
420 *\li When DNS_RBTFIND_NOEXACT is set, the closest matching superdomain is
421 * returned (also subject to DNS_RBTFIND_EMPTYDATA), even when
422 * there is an exact match in the tree. In this case, the chain
423 * will not point to the DNSSEC predecessor, but will instead point
424 * to the exact match, if there was any. Thus the preceding paragraphs
425 * should have "exact match" substituted for "predecessor" to describe
426 * how the various elements of the chain are set. This was done to
427 * ensure that the chain's state was sane, and to prevent problems that
428 * occurred when running the predecessor location code under conditions
429 * it was not designed for. It is not clear *where* the chain should
430 * point when DNS_RBTFIND_NOEXACT is set, so if you end up using a chain
431 * with this option because you want a particular node, let us know
432 * where you want the chain pointed, so this can be made more firm.
435 *\li rbt is a valid rbt manager.
436 *\li dns_name_isabsolute(name) == TRUE.
437 *\li node != NULL && *node == NULL.
438 *\li #DNS_RBTFIND_NOEXACT and DNS_RBTFIND_NOPREDECESSOR are mutually
442 *\li 'name' and the tree are not altered in any way.
444 *\li If result is ISC_R_SUCCESS:
446 * *node is the terminal node for 'name'.
448 * 'foundname' and 'name' represent the same name (though not
451 * 'chain' points to the DNSSEC predecessor, if any, of 'name'.
453 * chain->level_matches and chain->level_count are equal.
456 * If result is DNS_R_PARTIALMATCH:
458 * *node is the data associated with the deepest superdomain
459 * of 'name' which has data.
461 * 'foundname' is the name of deepest superdomain (which has
462 * data, unless the DNS_RBTFIND_EMPTYDATA option is set).
464 * 'chain' points to the DNSSEC predecessor, if any, of 'name'.
467 *\li If result is ISC_R_NOTFOUND:
469 * Neither the name nor a superdomain was found. *node is NULL.
471 * 'chain' points to the DNSSEC predecessor, if any, of 'name'.
473 * chain->level_matches is 0.
477 *\li #ISC_R_SUCCESS Success
478 *\li #DNS_R_PARTIALMATCH Superdomain found with data
479 *\li #ISC_R_NOTFOUND No match, or superdomain with no data
480 *\li #ISC_R_NOSPACE Concatenating nodes to form foundname failed
484 dns_rbt_deletename(dns_rbt_t *rbt, dns_name_t *name, isc_boolean_t recurse);
486 * Delete 'name' from the tree of trees.
489 *\li When 'name' is removed, if recurse is ISC_TRUE then all of its
490 * subnames are removed too.
493 *\li rbt is a valid rbt manager.
494 *\li dns_name_isabsolute(name) == TRUE
497 *\li 'name' is not altered in any way.
499 *\li Does NOT ensure that any external references to nodes in the tree
500 * are unaffected by node joins.
502 *\li If result is ISC_R_SUCCESS:
503 * 'name' does not appear in the tree with data; however,
504 * the node for the name might still exist which can be
505 * found with dns_rbt_findnode (but not dns_rbt_findname).
507 *\li If result is ISC_R_NOTFOUND:
508 * 'name' does not appear in the tree with data, because
509 * it did not appear in the tree before the function was called.
511 *\li If result is something else:
512 * See result codes for dns_rbt_findnode (if it fails, the
513 * node is not deleted) or dns_rbt_deletenode (if it fails,
514 * the node is deleted, but the tree is not optimized when
515 * it could have been).
518 *\li #ISC_R_SUCCESS Success
519 *\li #ISC_R_NOTFOUND No match
520 *\li something_else Any return code from dns_rbt_findnode except
521 * DNS_R_PARTIALMATCH (which causes ISC_R_NOTFOUND
522 * to be returned instead), and any code from
523 * dns_rbt_deletenode.
527 dns_rbt_deletenode(dns_rbt_t *rbt, dns_rbtnode_t *node, isc_boolean_t recurse);
529 * Delete 'node' from the tree of trees.
532 *\li When 'node' is removed, if recurse is ISC_TRUE then all nodes
533 * in levels down from it are removed too.
536 *\li rbt is a valid rbt manager.
540 *\li Does NOT ensure that any external references to nodes in the tree
541 * are unaffected by node joins.
543 *\li If result is ISC_R_SUCCESS:
544 * 'node' does not appear in the tree with data; however,
545 * the node might still exist if it serves as a pointer to
546 * a lower tree level as long as 'recurse' was false, hence
547 * the node could can be found with dns_rbt_findnode when
548 * that function's empty_data_ok parameter is true.
550 *\li If result is ISC_R_NOMEMORY or ISC_R_NOSPACE:
551 * The node was deleted, but the tree structure was not
555 *\li #ISC_R_SUCCESS Success
556 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory when joining nodes.
557 *\li #ISC_R_NOSPACE dns_name_concatenate failed when joining nodes.
561 dns_rbt_namefromnode(dns_rbtnode_t *node, dns_name_t *name);
563 * Convert the sequence of labels stored at 'node' into a 'name'.
566 *\li This function does not return the full name, from the root, but
567 * just the labels at the indicated node.
569 *\li The name data pointed to by 'name' is the information stored
570 * in the node, not a copy. Altering the data at this pointer
571 * will likely cause grief.
574 * \li name->offsets == NULL
577 * \li 'name' is DNS_NAMEATTR_READONLY.
579 * \li 'name' will point directly to the labels stored after the
580 * dns_rbtnode_t struct.
582 * \li 'name' will have offsets that also point to the information stored
583 * as part of the node.
587 dns_rbt_fullnamefromnode(dns_rbtnode_t *node, dns_name_t *name);
589 * Like dns_rbt_namefromnode, but returns the full name from the root.
592 * \li Unlike dns_rbt_namefromnode, the name will not point directly
593 * to node data. Rather, dns_name_concatenate will be used to copy
594 * the name data from each node into the 'name' argument.
598 * \li name has a dedicated buffer.
602 * \li ISC_R_NOSPACE (possible via dns_name_concatenate)
603 * \li DNS_R_NAMETOOLONG (possible via dns_name_concatenate)
607 dns_rbt_formatnodename(dns_rbtnode_t *node, char *printname,
610 * Format the full name of a node for printing, using dns_name_format().
613 * \li 'size' is the length of the printname buffer. This should be
614 * DNS_NAME_FORMATSIZE or larger.
617 * \li node and printname are not NULL.
620 * \li The 'printname' pointer.
624 dns_rbt_nodecount(dns_rbt_t *rbt);
626 * Obtain the number of nodes in the tree of trees.
629 * \li rbt is a valid rbt manager.
633 dns_rbt_destroy(dns_rbt_t **rbtp);
635 dns_rbt_destroy2(dns_rbt_t **rbtp, unsigned int quantum);
637 * Stop working with a red-black tree of trees.
638 * If 'quantum' is zero then the entire tree will be destroyed.
639 * If 'quantum' is non zero then up to 'quantum' nodes will be destroyed
640 * allowing the rbt to be incrementally destroyed by repeated calls to
641 * dns_rbt_destroy2(). Once dns_rbt_destroy2() has been called no other
642 * operations than dns_rbt_destroy()/dns_rbt_destroy2() should be
643 * performed on the tree of trees.
646 * \li *rbt is a valid rbt manager.
648 * Ensures on ISC_R_SUCCESS:
649 * \li All space allocated by the RBT library has been returned.
651 * \li *rbt is invalidated as an rbt manager.
655 * \li ISC_R_QUOTA if 'quantum' nodes have been destroyed.
659 dns_rbt_printall(dns_rbt_t *rbt);
661 * Print an ASCII representation of the internal structure of the red-black
665 * \li The name stored at each node, along with the node's color, is printed.
666 * Then the down pointer, left and right pointers are displayed
667 * recursively in turn. NULL down pointers are silently omitted;
668 * NULL left and right pointers are printed.
672 ***** Chain Functions
676 dns_rbtnodechain_init(dns_rbtnodechain_t *chain, isc_mem_t *mctx);
678 * Initialize 'chain'.
681 *\li 'chain' is a valid pointer.
683 *\li 'mctx' is a valid memory context.
686 *\li 'chain' is suitable for use.
690 dns_rbtnodechain_reset(dns_rbtnodechain_t *chain);
692 * Free any dynamic storage associated with 'chain', and then reinitialize
696 *\li 'chain' is a valid pointer.
699 *\li 'chain' is suitable for use, and uses no dynamic storage.
703 dns_rbtnodechain_invalidate(dns_rbtnodechain_t *chain);
705 * Free any dynamic storage associated with 'chain', and then invalidates it.
708 *\li Future calls to any dns_rbtnodechain_ function will need to call
709 * dns_rbtnodechain_init on the chain first (except, of course,
710 * dns_rbtnodechain_init itself).
713 *\li 'chain' is a valid chain.
716 *\li 'chain' is no longer suitable for use, and uses no dynamic storage.
720 dns_rbtnodechain_current(dns_rbtnodechain_t *chain, dns_name_t *name,
721 dns_name_t *origin, dns_rbtnode_t **node);
723 * Provide the name, origin and node to which the chain is currently pointed.
726 *\li The tree need not have be locked against additions for the chain
727 * to remain valid, however there are no guarantees if any deletion
728 * has been made since the chain was established.
731 *\li 'chain' is a valid chain.
734 *\li 'node', if non-NULL, is the node to which the chain was pointed
735 * by dns_rbt_findnode, dns_rbtnodechain_first or dns_rbtnodechain_last.
736 * If none were called for the chain since it was initialized or reset,
737 * or if the was no predecessor to the name searched for with
738 * dns_rbt_findnode, then '*node' is NULL and ISC_R_NOTFOUND is returned.
740 *\li 'name', if non-NULL, is the name stored at the terminal level of
741 * the chain. This is typically a single label, like the "www" of
742 * "www.isc.org", but need not be so. At the root of the tree of trees,
743 * if the node is "." then 'name' is ".", otherwise it is relative to ".".
744 * (Minimalist and atypical case: if the tree has just the name
745 * "isc.org." then the root node's stored name is "isc.org." but 'name'
746 * will be "isc.org".)
748 *\li 'origin', if non-NULL, is the sequence of labels in the levels
749 * above the terminal level, such as "isc.org." in the above example.
750 * 'origin' is always "." for the root node.
754 *\li #ISC_R_SUCCESS name, origin & node were successfully set.
755 *\li #ISC_R_NOTFOUND The chain does not point to any node.
756 *\li <something_else> Any error return from dns_name_concatenate.
760 dns_rbtnodechain_first(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
761 dns_name_t *name, dns_name_t *origin);
763 * Set the chain to the lexically first node in the tree of trees.
766 *\li By the definition of ordering for DNS names, the root of the tree of
767 * trees is the very first node, since everything else in the megatree
768 * uses it as a common suffix.
771 *\li 'chain' is a valid chain.
772 *\li 'rbt' is a valid rbt manager.
775 *\li The chain points to the very first node of the tree.
777 *\li 'name' and 'origin', if non-NULL, are set as described for
778 * dns_rbtnodechain_current. Thus 'origin' will always be ".".
781 *\li #DNS_R_NEWORIGIN The name & origin were successfully set.
782 *\li <something_else> Any error result from dns_rbtnodechain_current.
786 dns_rbtnodechain_last(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
787 dns_name_t *name, dns_name_t *origin);
789 * Set the chain to the lexically last node in the tree of trees.
792 *\li 'chain' is a valid chain.
793 *\li 'rbt' is a valid rbt manager.
796 *\li The chain points to the very last node of the tree.
798 *\li 'name' and 'origin', if non-NULL, are set as described for
799 * dns_rbtnodechain_current.
802 *\li #DNS_R_NEWORIGIN The name & origin were successfully set.
803 *\li #ISC_R_NOMEMORY Resource Limit: Out of Memory building chain.
804 *\li <something_else> Any error result from dns_name_concatenate.
808 dns_rbtnodechain_prev(dns_rbtnodechain_t *chain, dns_name_t *name,
811 * Adjusts chain to point the DNSSEC predecessor of the name to which it
812 * is currently pointed.
815 *\li 'chain' is a valid chain.
816 *\li 'chain' has been pointed somewhere in the tree with dns_rbt_findnode,
817 * dns_rbtnodechain_first or dns_rbtnodechain_last -- and remember that
818 * dns_rbt_findnode is not guaranteed to point the chain somewhere,
819 * since there may have been no predecessor to the searched for name.
822 *\li The chain is pointed to the predecessor of its current target.
824 *\li 'name' and 'origin', if non-NULL, are set as described for
825 * dns_rbtnodechain_current.
827 *\li 'origin' is only if a new origin was found.
830 *\li #ISC_R_SUCCESS The predecessor was found and 'name' was set.
831 *\li #DNS_R_NEWORIGIN The predecessor was found with a different
832 * origin and 'name' and 'origin' were set.
833 *\li #ISC_R_NOMORE There was no predecessor.
834 *\li <something_else> Any error result from dns_rbtnodechain_current.
838 dns_rbtnodechain_next(dns_rbtnodechain_t *chain, dns_name_t *name,
841 * Adjusts chain to point the DNSSEC successor of the name to which it
842 * is currently pointed.
845 *\li 'chain' is a valid chain.
846 *\li 'chain' has been pointed somewhere in the tree with dns_rbt_findnode,
847 * dns_rbtnodechain_first or dns_rbtnodechain_last -- and remember that
848 * dns_rbt_findnode is not guaranteed to point the chain somewhere,
849 * since there may have been no predecessor to the searched for name.
852 *\li The chain is pointed to the successor of its current target.
854 *\li 'name' and 'origin', if non-NULL, are set as described for
855 * dns_rbtnodechain_current.
857 *\li 'origin' is only if a new origin was found.
860 *\li #ISC_R_SUCCESS The successor was found and 'name' was set.
861 *\li #DNS_R_NEWORIGIN The successor was found with a different
862 * origin and 'name' and 'origin' were set.
863 *\li #ISC_R_NOMORE There was no successor.
864 *\li <something_else> Any error result from dns_name_concatenate.
868 dns_rbtnodechain_down(dns_rbtnodechain_t *chain, dns_name_t *name,
871 * Descend down if possible.
875 dns_rbtnodechain_nextflat(dns_rbtnodechain_t *chain, dns_name_t *name);
877 * Find the next node at the current depth in DNSSEC order.
881 * Wrapper macros for manipulating the rbtnode reference counter:
882 * Since we selectively use isc_refcount_t for the reference counter of
883 * a rbtnode, operations on the counter depend on the actual type of it.
884 * The following macros provide a common interface to these operations,
885 * hiding the back-end. The usage is the same as that of isc_refcount_xxx().
887 #ifdef DNS_RBT_USEISCREFCOUNT
888 #define dns_rbtnode_refinit(node, n) \
890 isc_refcount_init(&(node)->references, (n)); \
892 #define dns_rbtnode_refdestroy(node) \
894 isc_refcount_destroy(&(node)->references); \
896 #define dns_rbtnode_refcurrent(node) \
897 isc_refcount_current(&(node)->references)
898 #define dns_rbtnode_refincrement0(node, refs) \
900 isc_refcount_increment0(&(node)->references, (refs)); \
902 #define dns_rbtnode_refincrement(node, refs) \
904 isc_refcount_increment(&(node)->references, (refs)); \
906 #define dns_rbtnode_refdecrement(node, refs) \
908 isc_refcount_decrement(&(node)->references, (refs)); \
910 #else /* DNS_RBT_USEISCREFCOUNT */
911 #define dns_rbtnode_refinit(node, n) ((node)->references = (n))
912 #define dns_rbtnode_refdestroy(node) (REQUIRE((node)->references == 0))
913 #define dns_rbtnode_refcurrent(node) ((node)->references)
914 #define dns_rbtnode_refincrement0(node, refs) \
916 unsigned int *_tmp = (unsigned int *)(refs); \
917 (node)->references++; \
918 if ((_tmp) != NULL) \
919 (*_tmp) = (node)->references; \
921 #define dns_rbtnode_refincrement(node, refs) \
923 REQUIRE((node)->references > 0); \
924 (node)->references++; \
925 if ((refs) != NULL) \
926 (*refs) = (node)->references; \
928 #define dns_rbtnode_refdecrement(node, refs) \
930 REQUIRE((node)->references > 0); \
931 (node)->references--; \
932 if ((refs) != NULL) \
933 (*refs) = (node)->references; \
935 #endif /* DNS_RBT_USEISCREFCOUNT */
939 #endif /* DNS_RBT_H */