2 * Copyright (C) 2004, 2005, 2007-2009, 2011-2014 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 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.
22 /* Principal Authors: DCL */
27 #include <isc/platform.h>
28 #include <isc/print.h>
29 #include <isc/refcount.h>
30 #include <isc/string.h>
34 * This define is so dns/name.h (included by dns/fixedname.h) uses more
35 * efficient macro calls instead of functions for a few operations.
37 #define DNS_NAME_USEINLINE 1
39 #include <dns/fixedname.h>
42 #include <dns/result.h>
44 #define RBT_MAGIC ISC_MAGIC('R', 'B', 'T', '+')
45 #define VALID_RBT(rbt) ISC_MAGIC_VALID(rbt, RBT_MAGIC)
48 * XXXDCL Since parent pointers were added in again, I could remove all of the
49 * chain junk, and replace with dns_rbt_firstnode, _previousnode, _nextnode,
50 * _lastnode. This would involve pretty major change to the API.
52 #define CHAIN_MAGIC ISC_MAGIC('0', '-', '0', '-')
53 #define VALID_CHAIN(chain) ISC_MAGIC_VALID(chain, CHAIN_MAGIC)
55 #define RBT_HASH_SIZE 64
59 #define RBT_HASH_SIZE 2 /*%< To give the reallocation code a workout. */
66 void (*data_deleter)(void *, void *);
68 unsigned int nodecount;
69 unsigned int hashsize;
70 dns_rbtnode_t ** hashtable;
77 * Elements of the rbtnode structure.
79 #define PARENT(node) ((node)->parent)
80 #define LEFT(node) ((node)->left)
81 #define RIGHT(node) ((node)->right)
82 #define DOWN(node) ((node)->down)
83 #define DATA(node) ((node)->data)
84 #define HASHNEXT(node) ((node)->hashnext)
85 #define HASHVAL(node) ((node)->hashval)
86 #define COLOR(node) ((node)->color)
87 #define NAMELEN(node) ((node)->namelen)
88 #define OLDNAMELEN(node) ((node)->oldnamelen)
89 #define OFFSETLEN(node) ((node)->offsetlen)
90 #define ATTRS(node) ((node)->attributes)
91 #define IS_ROOT(node) ISC_TF((node)->is_root == 1)
92 #define FINDCALLBACK(node) ISC_TF((node)->find_callback == 1)
95 * Structure elements from the rbtdb.c, not
96 * used as part of the rbt.c algorithms.
98 #define DIRTY(node) ((node)->dirty)
99 #define WILD(node) ((node)->wild)
100 #define LOCKNUM(node) ((node)->locknum)
103 * The variable length stuff stored after the node has the following
106 * <name_data>{1..255}<oldoffsetlen>{1}<offsets>{1..128}
108 * <name_data> contains the name of the node when it was created.
109 * <oldoffsetlen> contains the length of <offsets> when the node was created.
110 * <offsets> contains the offets into name for each label when the node was
114 #define NAME(node) ((unsigned char *)((node) + 1))
115 #define OFFSETS(node) (NAME(node) + OLDNAMELEN(node) + 1)
116 #define OLDOFFSETLEN(node) (OFFSETS(node)[-1])
118 #define NODE_SIZE(node) (sizeof(*node) + \
119 OLDNAMELEN(node) + OLDOFFSETLEN(node) + 1)
124 #define IS_RED(node) ((node) != NULL && (node)->color == RED)
125 #define IS_BLACK(node) ((node) == NULL || (node)->color == BLACK)
126 #define MAKE_RED(node) ((node)->color = RED)
127 #define MAKE_BLACK(node) ((node)->color = BLACK)
132 * The "ancestors" member of chains were removed, with their job now
133 * being wholly handled by parent pointers (which didn't exist, because
134 * of memory concerns, when chains were first implemented).
136 #define ADD_LEVEL(chain, node) \
137 (chain)->levels[(chain)->level_count++] = (node)
140 * The following macros directly access normally private name variables.
141 * These macros are used to avoid a lot of function calls in the critical
142 * path of the tree traversal code.
145 #define NODENAME(node, name) \
147 (name)->length = NAMELEN(node); \
148 (name)->labels = OFFSETLEN(node); \
149 (name)->ndata = NAME(node); \
150 (name)->offsets = OFFSETS(node); \
151 (name)->attributes = ATTRS(node); \
152 (name)->attributes |= DNS_NAMEATTR_READONLY; \
155 #ifdef DNS_RBT_USEHASH
157 inithash(dns_rbt_t *rbt);
163 * A little something to help out in GDB.
165 dns_name_t Name(dns_rbtnode_t *node);
167 Name(dns_rbtnode_t *node) {
170 dns_name_init(&name, NULL);
172 NODENAME(node, &name);
177 static void dns_rbt_printnodename(dns_rbtnode_t *node);
180 static inline dns_rbtnode_t *
181 find_up(dns_rbtnode_t *node) {
185 * Return the node in the level above the argument node that points
186 * to the level the argument node is in. If the argument node is in
187 * the top level, the return value is NULL.
189 for (root = node; ! IS_ROOT(root); root = PARENT(root))
192 return (PARENT(root));
196 * Forward declarations.
199 create_node(isc_mem_t *mctx, dns_name_t *name, dns_rbtnode_t **nodep);
201 #ifdef DNS_RBT_USEHASH
203 hash_node(dns_rbt_t *rbt, dns_rbtnode_t *node, dns_name_t *name);
205 unhash_node(dns_rbt_t *rbt, dns_rbtnode_t *node);
207 #define hash_node(rbt, node, name) (ISC_R_SUCCESS)
208 #define unhash_node(rbt, node)
212 rotate_left(dns_rbtnode_t *node, dns_rbtnode_t **rootp);
214 rotate_right(dns_rbtnode_t *node, dns_rbtnode_t **rootp);
217 dns_rbt_addonlevel(dns_rbtnode_t *node, dns_rbtnode_t *current, int order,
218 dns_rbtnode_t **rootp);
221 dns_rbt_deletefromlevel(dns_rbtnode_t *delete, dns_rbtnode_t **rootp);
224 dns_rbt_deletetree(dns_rbt_t *rbt, dns_rbtnode_t *node);
227 dns_rbt_deletetreeflat(dns_rbt_t *rbt, unsigned int quantum,
228 dns_rbtnode_t **nodep);
231 * Initialize a red/black tree of trees.
234 dns_rbt_create(isc_mem_t *mctx, void (*deleter)(void *, void *),
235 void *deleter_arg, dns_rbt_t **rbtp)
237 #ifdef DNS_RBT_USEHASH
243 REQUIRE(mctx != NULL);
244 REQUIRE(rbtp != NULL && *rbtp == NULL);
245 REQUIRE(deleter == NULL ? deleter_arg == NULL : 1);
247 rbt = (dns_rbt_t *)isc_mem_get(mctx, sizeof(*rbt));
249 return (ISC_R_NOMEMORY);
252 isc_mem_attach(mctx, &rbt->mctx);
253 rbt->data_deleter = deleter;
254 rbt->deleter_arg = deleter_arg;
257 rbt->hashtable = NULL;
260 #ifdef DNS_RBT_USEHASH
261 result = inithash(rbt);
262 if (result != ISC_R_SUCCESS) {
263 isc_mem_putanddetach(&rbt->mctx, rbt, sizeof(*rbt));
268 rbt->magic = RBT_MAGIC;
272 return (ISC_R_SUCCESS);
276 * Deallocate a red/black tree of trees.
279 dns_rbt_destroy(dns_rbt_t **rbtp) {
280 RUNTIME_CHECK(dns_rbt_destroy2(rbtp, 0) == ISC_R_SUCCESS);
284 dns_rbt_destroy2(dns_rbt_t **rbtp, unsigned int quantum) {
287 REQUIRE(rbtp != NULL && VALID_RBT(*rbtp));
291 dns_rbt_deletetreeflat(rbt, quantum, &rbt->root);
292 if (rbt->root != NULL)
293 return (ISC_R_QUOTA);
295 INSIST(rbt->nodecount == 0);
297 if (rbt->hashtable != NULL)
298 isc_mem_put(rbt->mctx, rbt->hashtable,
299 rbt->hashsize * sizeof(dns_rbtnode_t *));
303 isc_mem_putanddetach(&rbt->mctx, rbt, sizeof(*rbt));
305 return (ISC_R_SUCCESS);
309 dns_rbt_nodecount(dns_rbt_t *rbt) {
310 REQUIRE(VALID_RBT(rbt));
311 return (rbt->nodecount);
314 static inline isc_result_t
315 chain_name(dns_rbtnodechain_t *chain, dns_name_t *name,
316 isc_boolean_t include_chain_end)
319 isc_result_t result = ISC_R_SUCCESS;
322 dns_name_init(&nodename, NULL);
324 if (include_chain_end && chain->end != NULL) {
325 NODENAME(chain->end, &nodename);
326 result = dns_name_copy(&nodename, name, NULL);
327 if (result != ISC_R_SUCCESS)
330 dns_name_reset(name);
332 for (i = (int)chain->level_count - 1; i >= 0; i--) {
333 NODENAME(chain->levels[i], &nodename);
334 result = dns_name_concatenate(name, &nodename, name, NULL);
336 if (result != ISC_R_SUCCESS)
342 static inline isc_result_t
343 move_chain_to_last(dns_rbtnodechain_t *chain, dns_rbtnode_t *node) {
346 * Go as far right and then down as much as possible,
347 * as long as the rightmost node has a down pointer.
349 while (RIGHT(node) != NULL)
352 if (DOWN(node) == NULL)
355 ADD_LEVEL(chain, node);
361 return (ISC_R_SUCCESS);
365 * Add 'name' to tree, initializing its data pointer with 'data'.
369 dns_rbt_addnode(dns_rbt_t *rbt, dns_name_t *name, dns_rbtnode_t **nodep) {
371 * Does this thing have too many variables or what?
373 dns_rbtnode_t **root, *parent, *child, *current, *new_current;
374 dns_name_t *add_name, *new_name, current_name, *prefix, *suffix;
375 dns_fixedname_t fixedcopy, fixedprefix, fixedsuffix, fnewname;
376 dns_offsets_t current_offsets;
377 dns_namereln_t compared;
378 isc_result_t result = ISC_R_SUCCESS;
379 dns_rbtnodechain_t chain;
380 unsigned int common_labels;
381 unsigned int nlabels, hlabels;
384 REQUIRE(VALID_RBT(rbt));
385 REQUIRE(dns_name_isabsolute(name));
386 REQUIRE(nodep != NULL && *nodep == NULL);
389 * Create a copy of the name so the original name structure is
392 dns_fixedname_init(&fixedcopy);
393 add_name = dns_fixedname_name(&fixedcopy);
394 dns_name_clone(name, add_name);
396 if (rbt->root == NULL) {
397 result = create_node(rbt->mctx, add_name, &new_current);
398 if (result == ISC_R_SUCCESS) {
400 new_current->is_root = 1;
401 rbt->root = new_current;
402 *nodep = new_current;
403 hash_node(rbt, new_current, name);
408 dns_rbtnodechain_init(&chain, rbt->mctx);
410 dns_fixedname_init(&fixedprefix);
411 dns_fixedname_init(&fixedsuffix);
412 prefix = dns_fixedname_name(&fixedprefix);
413 suffix = dns_fixedname_name(&fixedsuffix);
416 INSIST(IS_ROOT(*root));
420 dns_name_init(¤t_name, current_offsets);
421 dns_fixedname_init(&fnewname);
422 new_name = dns_fixedname_name(&fnewname);
423 nlabels = dns_name_countlabels(name);
429 NODENAME(current, ¤t_name);
430 compared = dns_name_fullcompare(add_name, ¤t_name,
431 &order, &common_labels);
433 if (compared == dns_namereln_equal) {
435 result = ISC_R_EXISTS;
440 if (compared == dns_namereln_none) {
444 child = LEFT(current);
446 } else if (order > 0) {
448 child = RIGHT(current);
454 * This name has some suffix in common with the
455 * name at the current node. If the name at
456 * the current node is shorter, that means the
457 * new name should be in a subtree. If the
458 * name at the current node is longer, that means
459 * the down pointer to this tree should point
460 * to a new tree that has the common suffix, and
461 * the non-common parts of these two names should
464 hlabels += common_labels;
465 if (compared == dns_namereln_subdomain) {
467 * All of the existing labels are in common,
468 * so the new name is in a subtree.
469 * Whack off the common labels for the
470 * not-in-common part to be searched for
473 dns_name_split(add_name, common_labels,
477 * Follow the down pointer (possibly NULL).
479 root = &DOWN(current);
481 INSIST(*root == NULL ||
483 PARENT(*root) == current));
486 child = DOWN(current);
487 ADD_LEVEL(&chain, current);
491 * The number of labels in common is fewer
492 * than the number of labels at the current
493 * node, so the current node must be adjusted
494 * to have just the common suffix, and a down
495 * pointer made to a new tree.
498 INSIST(compared == dns_namereln_commonancestor
499 || compared == dns_namereln_contains);
502 * Ensure the number of levels in the tree
503 * does not exceed the number of logical
504 * levels allowed by DNSSEC.
506 * XXXDCL need a better error result?
508 * XXXDCL Since chain ancestors were removed,
509 * no longer used by dns_rbt_addonlevel(),
510 * this is the only real use of chains in the
511 * function. It could be done instead with
512 * a simple integer variable, but I am pressed
515 if (chain.level_count ==
516 (sizeof(chain.levels) /
517 sizeof(*chain.levels))) {
518 result = ISC_R_NOSPACE;
523 * Split the name into two parts, a prefix
524 * which is the not-in-common parts of the
525 * two names and a suffix that is the common
528 dns_name_split(¤t_name, common_labels,
530 result = create_node(rbt->mctx, suffix,
533 if (result != ISC_R_SUCCESS)
537 * Reproduce the tree attributes of the
540 new_current->is_root = current->is_root;
541 if (current->nsec == DNS_RBT_NSEC_HAS_NSEC)
542 new_current->nsec = DNS_RBT_NSEC_NORMAL;
544 new_current->nsec = current->nsec;
545 PARENT(new_current) = PARENT(current);
546 LEFT(new_current) = LEFT(current);
547 RIGHT(new_current) = RIGHT(current);
548 COLOR(new_current) = COLOR(current);
551 * Fix pointers that were to the current node.
553 if (parent != NULL) {
554 if (LEFT(parent) == current)
555 LEFT(parent) = new_current;
557 RIGHT(parent) = new_current;
559 if (LEFT(new_current) != NULL)
560 PARENT(LEFT(new_current)) =
562 if (RIGHT(new_current) != NULL)
563 PARENT(RIGHT(new_current)) =
565 if (*root == current)
568 NAMELEN(current) = prefix->length;
569 OFFSETLEN(current) = prefix->labels;
572 * Set up the new root of the next level.
573 * By definition it will not be the top
574 * level tree, so clear DNS_NAMEATTR_ABSOLUTE.
576 current->is_root = 1;
577 PARENT(current) = new_current;
578 DOWN(new_current) = current;
579 root = &DOWN(new_current);
581 ADD_LEVEL(&chain, new_current);
583 LEFT(current) = NULL;
584 RIGHT(current) = NULL;
587 ATTRS(current) &= ~DNS_NAMEATTR_ABSOLUTE;
590 dns_name_getlabelsequence(name,
593 hash_node(rbt, new_current, new_name);
596 dns_name_countlabels(add_name)) {
598 * The name has been added by pushing
599 * the not-in-common parts down to
602 *nodep = new_current;
603 return (ISC_R_SUCCESS);
607 * The current node has no data,
608 * because it is just a placeholder.
609 * Its data pointer is already NULL
610 * from create_node()), so there's
611 * nothing more to do to it.
615 * The not-in-common parts of the new
616 * name will be inserted into the new
617 * level following this loop (unless
618 * result != ISC_R_SUCCESS, which
619 * is tested after the loop ends).
621 dns_name_split(add_name, common_labels,
631 } while (child != NULL);
633 if (result == ISC_R_SUCCESS)
634 result = create_node(rbt->mctx, add_name, &new_current);
636 if (result == ISC_R_SUCCESS) {
637 dns_rbt_addonlevel(new_current, current, order, root);
639 *nodep = new_current;
640 hash_node(rbt, new_current, name);
647 * Add a name to the tree of trees, associating it with some data.
650 dns_rbt_addname(dns_rbt_t *rbt, dns_name_t *name, void *data) {
654 REQUIRE(VALID_RBT(rbt));
655 REQUIRE(dns_name_isabsolute(name));
659 result = dns_rbt_addnode(rbt, name, &node);
662 * dns_rbt_addnode will report the node exists even when
663 * it does not have data associated with it, but the
664 * dns_rbt_*name functions all behave depending on whether
665 * there is data associated with a node.
667 if (result == ISC_R_SUCCESS ||
668 (result == ISC_R_EXISTS && DATA(node) == NULL)) {
670 result = ISC_R_SUCCESS;
677 * Find the node for "name" in the tree of trees.
680 dns_rbt_findnode(dns_rbt_t *rbt, dns_name_t *name, dns_name_t *foundname,
681 dns_rbtnode_t **node, dns_rbtnodechain_t *chain,
682 unsigned int options, dns_rbtfindcallback_t callback,
685 dns_rbtnode_t *current, *last_compared, *current_root;
686 dns_rbtnodechain_t localchain;
687 dns_name_t *search_name, current_name, *callback_name;
688 dns_fixedname_t fixedcallbackname, fixedsearchname;
689 dns_namereln_t compared;
690 isc_result_t result, saved_result;
691 unsigned int common_labels;
692 unsigned int hlabels = 0;
695 REQUIRE(VALID_RBT(rbt));
696 REQUIRE(dns_name_isabsolute(name));
697 REQUIRE(node != NULL && *node == NULL);
698 REQUIRE((options & (DNS_RBTFIND_NOEXACT | DNS_RBTFIND_NOPREDECESSOR))
699 != (DNS_RBTFIND_NOEXACT | DNS_RBTFIND_NOPREDECESSOR));
702 * If there is a chain it needs to appear to be in a sane state,
703 * otherwise a chain is still needed to generate foundname and
707 options |= DNS_RBTFIND_NOPREDECESSOR;
709 dns_rbtnodechain_init(chain, rbt->mctx);
711 dns_rbtnodechain_reset(chain);
713 if (rbt->root == NULL)
714 return (ISC_R_NOTFOUND);
717 * Appease GCC about variables it incorrectly thinks are
718 * possibly used uninitialized.
720 compared = dns_namereln_none;
721 last_compared = NULL;
725 dns_fixedname_init(&fixedcallbackname);
726 callback_name = dns_fixedname_name(&fixedcallbackname);
729 * search_name is the name segment being sought in each tree level.
730 * By using a fixedname, the search_name will definitely have offsets
731 * for use by any splitting.
732 * By using dns_name_clone, no name data should be copied thanks to
733 * the lack of bitstring labels.
735 dns_fixedname_init(&fixedsearchname);
736 search_name = dns_fixedname_name(&fixedsearchname);
737 dns_name_clone(name, search_name);
739 dns_name_init(¤t_name, NULL);
741 saved_result = ISC_R_SUCCESS;
743 current_root = rbt->root;
745 while (current != NULL) {
746 NODENAME(current, ¤t_name);
747 compared = dns_name_fullcompare(search_name, ¤t_name,
748 &order, &common_labels);
749 last_compared = current;
751 if (compared == dns_namereln_equal)
754 if (compared == dns_namereln_none) {
755 #ifdef DNS_RBT_USEHASH
756 dns_name_t hash_name;
757 dns_rbtnode_t *hnode;
758 dns_rbtnode_t *up_current;
759 unsigned int nlabels;
760 unsigned int tlabels = 1;
764 * If there is no hash table, hashing can't be done.
766 if (rbt->hashtable == NULL)
770 * The case of current != current_root, that
771 * means a left or right pointer was followed,
772 * only happens when the algorithm fell through to
773 * the traditional binary search because of a
774 * bitstring label. Since we dropped the bitstring
775 * support, this should not happen.
777 INSIST(current == current_root);
779 nlabels = dns_name_countlabels(search_name);
782 * current_root is the root of the current level, so
783 * it's parent is the same as it's "up" pointer.
785 up_current = PARENT(current_root);
786 dns_name_init(&hash_name, NULL);
790 * Hash includes tail.
792 dns_name_getlabelsequence(name,
796 hash = dns_name_fullhash(&hash_name, ISC_FALSE);
797 dns_name_getlabelsequence(search_name,
799 tlabels, &hash_name);
801 for (hnode = rbt->hashtable[hash % rbt->hashsize];
803 hnode = hnode->hashnext)
805 dns_name_t hnode_name;
807 if (hash != HASHVAL(hnode))
809 if (find_up(hnode) != up_current)
811 dns_name_init(&hnode_name, NULL);
812 NODENAME(hnode, &hnode_name);
813 if (dns_name_equal(&hnode_name, &hash_name))
820 * This is an optimization. If hashing found
821 * the right node, the next call to
822 * dns_name_fullcompare() would obviously
823 * return _equal or _subdomain. Determine
824 * which of those would be the case by
825 * checking if the full name was hashed. Then
826 * make it look like dns_name_fullcompare
827 * was called and jump to the right place.
829 if (tlabels == nlabels) {
830 compared = dns_namereln_equal;
833 common_labels = tlabels;
834 compared = dns_namereln_subdomain;
839 if (tlabels++ < nlabels)
843 * All of the labels have been tried against the hash
844 * table. Since we dropped the support of bitstring
845 * labels, the name isn't in the table.
851 #endif /* DNS_RBT_USEHASH */
853 * Standard binary search tree movement.
856 current = LEFT(current);
858 current = RIGHT(current);
862 * The names have some common suffix labels.
864 * If the number in common are equal in length to
865 * the current node's name length, then follow the
866 * down pointer and search in the new tree.
868 if (compared == dns_namereln_subdomain) {
871 * Whack off the current node's common parts
872 * for the name to search in the next level.
874 dns_name_split(search_name, common_labels,
876 hlabels += common_labels;
878 * This might be the closest enclosing name.
880 if (DATA(current) != NULL ||
881 (options & DNS_RBTFIND_EMPTYDATA) != 0)
885 * Point the chain to the next level. This
886 * needs to be done before 'current' is pointed
887 * there because the callback in the next
888 * block of code needs the current 'current',
889 * but in the event the callback requests that
890 * the search be stopped then the
891 * DNS_R_PARTIALMATCH code at the end of this
892 * function needs the chain pointed to the
895 ADD_LEVEL(chain, current);
898 * The caller may want to interrupt the
899 * downward search when certain special nodes
900 * are traversed. If this is a special node,
901 * the callback is used to learn what the
902 * caller wants to do.
904 if (callback != NULL &&
905 FINDCALLBACK(current)) {
906 result = chain_name(chain,
909 if (result != ISC_R_SUCCESS) {
910 dns_rbtnodechain_reset(chain);
914 result = (callback)(current,
917 if (result != DNS_R_CONTINUE) {
918 saved_result = result;
920 * Treat this node as if it
921 * had no down pointer.
929 * Finally, head to the next tree level.
931 current = DOWN(current);
932 current_root = current;
936 * Though there are labels in common, the
937 * entire name at this node is not common
938 * with the search name so the search
939 * name does not exist in the tree.
941 INSIST(compared == dns_namereln_commonancestor
942 || compared == dns_namereln_contains);
950 * If current is not NULL, NOEXACT is not disallowing exact matches,
951 * and either the node has data or an empty node is ok, return
952 * ISC_R_SUCCESS to indicate an exact match.
954 if (current != NULL && (options & DNS_RBTFIND_NOEXACT) == 0 &&
955 (DATA(current) != NULL ||
956 (options & DNS_RBTFIND_EMPTYDATA) != 0)) {
958 * Found an exact match.
960 chain->end = current;
961 chain->level_matches = chain->level_count;
963 if (foundname != NULL)
964 result = chain_name(chain, foundname, ISC_TRUE);
966 result = ISC_R_SUCCESS;
968 if (result == ISC_R_SUCCESS) {
970 result = saved_result;
975 * Did not find an exact match (or did not want one).
979 * ... but found a partially matching superdomain.
980 * Unwind the chain to the partial match node
981 * to set level_matches to the level above the node,
982 * and then to derive the name.
984 * chain->level_count is guaranteed to be at least 1
985 * here because by definition of finding a superdomain,
986 * the chain is pointed to at least the first subtree.
988 chain->level_matches = chain->level_count - 1;
990 while (chain->levels[chain->level_matches] != *node) {
991 INSIST(chain->level_matches > 0);
992 chain->level_matches--;
995 if (foundname != NULL) {
996 unsigned int saved_count = chain->level_count;
998 chain->level_count = chain->level_matches + 1;
1000 result = chain_name(chain, foundname,
1003 chain->level_count = saved_count;
1005 result = ISC_R_SUCCESS;
1007 if (result == ISC_R_SUCCESS)
1008 result = DNS_R_PARTIALMATCH;
1011 result = ISC_R_NOTFOUND;
1013 if (current != NULL) {
1015 * There was an exact match but either
1016 * DNS_RBTFIND_NOEXACT was set, or
1017 * DNS_RBTFIND_EMPTYDATA was set and the node had no
1018 * data. A policy decision was made to set the
1019 * chain to the exact match, but this is subject
1020 * to change if it becomes apparent that something
1021 * else would be more useful. It is important that
1022 * this case is handled here, because the predecessor
1023 * setting code below assumes the match was not exact.
1025 INSIST(((options & DNS_RBTFIND_NOEXACT) != 0) ||
1026 ((options & DNS_RBTFIND_EMPTYDATA) == 0 &&
1027 DATA(current) == NULL));
1028 chain->end = current;
1030 } else if ((options & DNS_RBTFIND_NOPREDECESSOR) != 0) {
1032 * Ensure the chain points nowhere.
1038 * Since there was no exact match, the chain argument
1039 * needs to be pointed at the DNSSEC predecessor of
1042 if (compared == dns_namereln_subdomain) {
1044 * Attempted to follow a down pointer that was
1045 * NULL, which means the searched for name was
1046 * a subdomain of a terminal name in the tree.
1047 * Since there are no existing subdomains to
1048 * order against, the terminal name is the
1051 INSIST(chain->level_count > 0);
1052 INSIST(chain->level_matches <
1053 chain->level_count);
1055 chain->levels[--chain->level_count];
1058 isc_result_t result2;
1061 * Point current to the node that stopped
1064 * With the hashing modification that has been
1065 * added to the algorithm, the stop node of a
1066 * standard binary search is not known. So it
1067 * has to be found. There is probably a more
1068 * clever way of doing this.
1070 * The assignment of current to NULL when
1071 * the relationship is *not* dns_namereln_none,
1072 * even though it later gets set to the same
1073 * last_compared anyway, is simply to not push
1074 * the while loop in one more level of
1077 if (compared == dns_namereln_none)
1078 current = last_compared;
1082 while (current != NULL) {
1083 NODENAME(current, ¤t_name);
1084 compared = dns_name_fullcompare(
1091 last_compared = current;
1094 * Standard binary search movement.
1097 current = LEFT(current);
1099 current = RIGHT(current);
1103 current = last_compared;
1106 * Reached a point within a level tree that
1107 * positively indicates the name is not
1108 * present, but the stop node could be either
1109 * less than the desired name (order > 0) or
1110 * greater than the desired name (order < 0).
1112 * If the stop node is less, it is not
1113 * necessarily the predecessor. If the stop
1114 * node has a down pointer, then the real
1115 * predecessor is at the end of a level below
1116 * (not necessarily the next level).
1117 * Move down levels until the rightmost node
1118 * does not have a down pointer.
1120 * When the stop node is greater, it is
1121 * the successor. All the logic for finding
1122 * the predecessor is handily encapsulated
1123 * in dns_rbtnodechain_prev. In the event
1124 * that the search name is less than anything
1125 * else in the tree, the chain is reset.
1126 * XXX DCL What is the best way for the caller
1127 * to know that the search name has
1133 if (DOWN(current) != NULL) {
1134 ADD_LEVEL(chain, current);
1137 move_chain_to_last(chain,
1140 if (result2 != ISC_R_SUCCESS)
1144 * Ah, the pure and simple
1145 * case. The stop node is the
1148 chain->end = current;
1153 chain->end = current;
1155 result2 = dns_rbtnodechain_prev(chain,
1158 if (result2 == ISC_R_SUCCESS ||
1159 result2 == DNS_R_NEWORIGIN)
1161 else if (result2 == ISC_R_NOMORE)
1163 * There is no predecessor.
1165 dns_rbtnodechain_reset(chain);
1174 ENSURE(*node == NULL || DNS_RBTNODE_VALID(*node));
1180 * Get the data pointer associated with 'name'.
1183 dns_rbt_findname(dns_rbt_t *rbt, dns_name_t *name, unsigned int options,
1184 dns_name_t *foundname, void **data) {
1185 dns_rbtnode_t *node = NULL;
1186 isc_result_t result;
1188 REQUIRE(data != NULL && *data == NULL);
1190 result = dns_rbt_findnode(rbt, name, foundname, &node, NULL,
1191 options, NULL, NULL);
1194 (DATA(node) != NULL || (options & DNS_RBTFIND_EMPTYDATA) != 0))
1197 result = ISC_R_NOTFOUND;
1203 * Delete a name from the tree of trees.
1206 dns_rbt_deletename(dns_rbt_t *rbt, dns_name_t *name, isc_boolean_t recurse) {
1207 dns_rbtnode_t *node = NULL;
1208 isc_result_t result;
1210 REQUIRE(VALID_RBT(rbt));
1211 REQUIRE(dns_name_isabsolute(name));
1214 * First, find the node.
1216 * When searching, the name might not have an exact match:
1217 * consider a.b.a.com, b.b.a.com and c.b.a.com as the only
1218 * elements of a tree, which would make layer 1 a single
1219 * node tree of "b.a.com" and layer 2 a three node tree of
1220 * a, b, and c. Deleting a.com would find only a partial depth
1221 * match in the first layer. Should it be a requirement that
1222 * that the name to be deleted have data? For now, it is.
1224 * ->dirty, ->locknum and ->references are ignored; they are
1225 * solely the province of rbtdb.c.
1227 result = dns_rbt_findnode(rbt, name, NULL, &node, NULL,
1228 DNS_RBTFIND_NOOPTIONS, NULL, NULL);
1230 if (result == ISC_R_SUCCESS) {
1231 if (DATA(node) != NULL)
1232 result = dns_rbt_deletenode(rbt, node, recurse);
1234 result = ISC_R_NOTFOUND;
1236 } else if (result == DNS_R_PARTIALMATCH)
1237 result = ISC_R_NOTFOUND;
1243 * Remove a node from the tree of trees.
1245 * NOTE WELL: deletion is *not* symmetric with addition; that is, reversing
1246 * a sequence of additions to be deletions will not generally get the
1247 * tree back to the state it started in. For example, if the addition
1248 * of "b.c" caused the node "a.b.c" to be split, pushing "a" to its own level,
1249 * then the subsequent deletion of "b.c" will not cause "a" to be pulled up,
1250 * restoring "a.b.c". The RBT *used* to do this kind of rejoining, but it
1251 * turned out to be a bad idea because it could corrupt an active nodechain
1252 * that had "b.c" as one of its levels -- and the RBT has no idea what
1253 * nodechains are in use by callers, so it can't even *try* to helpfully
1254 * fix them up (which would probably be doomed to failure anyway).
1256 * Similarly, it is possible to leave the tree in a state where a supposedly
1257 * deleted node still exists. The first case of this is obvious; take
1258 * the tree which has "b.c" on one level, pointing to "a". Now deleted "b.c".
1259 * It was just established in the previous paragraph why we can't pull "a"
1260 * back up to its parent level. But what happens when "a" then gets deleted?
1261 * "b.c" is left hanging around without data or children. This condition
1262 * is actually pretty easy to detect, but ... should it really be removed?
1263 * Is a chain pointing to it? An iterator? Who knows! (Note that the
1264 * references structure member cannot be looked at because it is private to
1265 * rbtdb.) This is ugly and makes me unhappy, but after hours of trying to
1266 * make it more aesthetically proper and getting nowhere, this is the way it
1267 * is going to stay until such time as it proves to be a *real* problem.
1269 * Finally, for reference, note that the original routine that did node
1270 * joining was called join_nodes(). It has been excised, living now only
1271 * in the CVS history, but comments have been left behind that point to it just
1272 * in case someone wants to muck with this some more.
1274 * The one positive aspect of all of this is that joining used to have a
1275 * case where it might fail. Without trying to join, now this function always
1276 * succeeds. It still returns isc_result_t, though, so the API wouldn't change.
1279 dns_rbt_deletenode(dns_rbt_t *rbt, dns_rbtnode_t *node, isc_boolean_t recurse)
1281 dns_rbtnode_t *parent;
1283 REQUIRE(VALID_RBT(rbt));
1284 REQUIRE(DNS_RBTNODE_VALID(node));
1286 if (DOWN(node) != NULL) {
1288 RUNTIME_CHECK(dns_rbt_deletetree(rbt, DOWN(node))
1291 if (DATA(node) != NULL && rbt->data_deleter != NULL)
1292 rbt->data_deleter(DATA(node), rbt->deleter_arg);
1296 * Since there is at least one node below this one and
1297 * no recursion was requested, the deletion is
1298 * complete. The down node from this node might be all
1299 * by itself on a single level, so join_nodes() could
1300 * be used to collapse the tree (with all the caveats
1301 * of the comment at the start of this function).
1303 return (ISC_R_SUCCESS);
1308 * Note the node that points to the level of the node that is being
1309 * deleted. If the deleted node is the top level, parent will be set
1312 parent = find_up(node);
1315 * This node now has no down pointer (either because it didn't
1316 * have one to start, or because it was recursively removed).
1317 * So now the node needs to be removed from this level.
1319 dns_rbt_deletefromlevel(node, parent == NULL ? &rbt->root :
1322 if (DATA(node) != NULL && rbt->data_deleter != NULL)
1323 rbt->data_deleter(DATA(node), rbt->deleter_arg);
1325 unhash_node(rbt, node);
1326 #if DNS_RBT_USEMAGIC
1329 dns_rbtnode_refdestroy(node);
1330 isc_mem_put(rbt->mctx, node, NODE_SIZE(node));
1334 * There are now two special cases that can exist that would
1335 * not have existed if the tree had been created using only
1336 * the names that now exist in it. (This is all related to
1337 * join_nodes() as described in this function's introductory comment.)
1338 * Both cases exist when the deleted node's parent (the node
1339 * that pointed to the deleted node's level) is not null but
1340 * it has no data: parent != NULL && DATA(parent) == NULL.
1342 * The first case is that the deleted node was the last on its level:
1343 * DOWN(parent) == NULL. This case can only exist if the parent was
1344 * previously deleted -- and so now, apparently, the parent should go
1345 * away. That can't be done though because there might be external
1346 * references to it, such as through a nodechain.
1348 * The other case also involves a parent with no data, but with the
1349 * deleted node being the next-to-last node instead of the last:
1350 * LEFT(DOWN(parent)) == NULL && RIGHT(DOWN(parent)) == NULL.
1351 * Presumably now the remaining node on the level should be joined
1352 * with the parent, but it's already been described why that can't be
1357 * This function never fails.
1359 return (ISC_R_SUCCESS);
1363 dns_rbt_namefromnode(dns_rbtnode_t *node, dns_name_t *name) {
1365 REQUIRE(DNS_RBTNODE_VALID(node));
1366 REQUIRE(name != NULL);
1367 REQUIRE(name->offsets == NULL);
1369 NODENAME(node, name);
1373 dns_rbt_fullnamefromnode(dns_rbtnode_t *node, dns_name_t *name) {
1375 isc_result_t result;
1377 REQUIRE(DNS_RBTNODE_VALID(node));
1378 REQUIRE(name != NULL);
1379 REQUIRE(name->buffer != NULL);
1381 dns_name_init(¤t, NULL);
1382 dns_name_reset(name);
1385 INSIST(node != NULL);
1387 NODENAME(node, ¤t);
1389 result = dns_name_concatenate(name, ¤t, name, NULL);
1390 if (result != ISC_R_SUCCESS)
1393 node = find_up(node);
1394 } while (! dns_name_isabsolute(name));
1400 dns_rbt_formatnodename(dns_rbtnode_t *node, char *printname, unsigned int size)
1402 dns_fixedname_t fixedname;
1404 isc_result_t result;
1406 REQUIRE(DNS_RBTNODE_VALID(node));
1407 REQUIRE(printname != NULL);
1409 dns_fixedname_init(&fixedname);
1410 name = dns_fixedname_name(&fixedname);
1411 result = dns_rbt_fullnamefromnode(node, name);
1412 if (result == ISC_R_SUCCESS)
1413 dns_name_format(name, printname, size);
1415 snprintf(printname, size, "<error building name: %s>",
1416 dns_result_totext(result));
1422 create_node(isc_mem_t *mctx, dns_name_t *name, dns_rbtnode_t **nodep) {
1423 dns_rbtnode_t *node;
1424 isc_region_t region;
1425 unsigned int labels;
1427 REQUIRE(name->offsets != NULL);
1429 dns_name_toregion(name, ®ion);
1430 labels = dns_name_countlabels(name);
1434 * Allocate space for the node structure, the name, and the offsets.
1436 node = (dns_rbtnode_t *)isc_mem_get(mctx, sizeof(*node) +
1437 region.length + labels + 1);
1440 return (ISC_R_NOMEMORY);
1443 PARENT(node) = NULL;
1448 #ifdef DNS_RBT_USEHASH
1449 HASHNEXT(node) = NULL;
1453 ISC_LINK_INIT(node, deadlink);
1458 dns_rbtnode_refinit(node, 0);
1459 node->find_callback = 0;
1460 node->nsec = DNS_RBT_NSEC_NORMAL;
1465 * The following is stored to make reconstructing a name from the
1466 * stored value in the node easy: the length of the name, the number
1467 * of labels, whether the name is absolute or not, the name itself,
1468 * and the name's offsets table.
1471 * The offsets table could be made smaller by eliminating the
1472 * first offset, which is always 0. This requires changes to
1475 * Note: OLDOFFSETLEN *must* be assigned *after* OLDNAMELEN is assigned
1476 * as it uses OLDNAMELEN.
1478 OLDNAMELEN(node) = NAMELEN(node) = region.length;
1479 OLDOFFSETLEN(node) = OFFSETLEN(node) = labels;
1480 ATTRS(node) = name->attributes;
1482 memmove(NAME(node), region.base, region.length);
1483 memmove(OFFSETS(node), name->offsets, labels);
1485 #if DNS_RBT_USEMAGIC
1486 node->magic = DNS_RBTNODE_MAGIC;
1490 return (ISC_R_SUCCESS);
1493 #ifdef DNS_RBT_USEHASH
1495 hash_add_node(dns_rbt_t *rbt, dns_rbtnode_t *node, dns_name_t *name) {
1498 HASHVAL(node) = dns_name_fullhash(name, ISC_FALSE);
1500 hash = HASHVAL(node) % rbt->hashsize;
1501 HASHNEXT(node) = rbt->hashtable[hash];
1503 rbt->hashtable[hash] = node;
1507 inithash(dns_rbt_t *rbt) {
1510 rbt->hashsize = RBT_HASH_SIZE;
1511 bytes = rbt->hashsize * sizeof(dns_rbtnode_t *);
1512 rbt->hashtable = isc_mem_get(rbt->mctx, bytes);
1514 if (rbt->hashtable == NULL)
1515 return (ISC_R_NOMEMORY);
1517 memset(rbt->hashtable, 0, bytes);
1519 return (ISC_R_SUCCESS);
1523 rehash(dns_rbt_t *rbt) {
1524 unsigned int oldsize;
1525 dns_rbtnode_t **oldtable;
1526 dns_rbtnode_t *node;
1530 oldsize = rbt->hashsize;
1531 oldtable = rbt->hashtable;
1532 rbt->hashsize = rbt->hashsize * 2 + 1;
1533 rbt->hashtable = isc_mem_get(rbt->mctx,
1534 rbt->hashsize * sizeof(dns_rbtnode_t *));
1535 if (rbt->hashtable == NULL) {
1536 rbt->hashtable = oldtable;
1537 rbt->hashsize = oldsize;
1541 INSIST(rbt->hashsize > 0);
1543 for (i = 0; i < rbt->hashsize; i++)
1544 rbt->hashtable[i] = NULL;
1546 for (i = 0; i < oldsize; i++) {
1548 while (node != NULL) {
1549 hash = HASHVAL(node) % rbt->hashsize;
1550 oldtable[i] = HASHNEXT(node);
1551 HASHNEXT(node) = rbt->hashtable[hash];
1552 rbt->hashtable[hash] = node;
1557 isc_mem_put(rbt->mctx, oldtable, oldsize * sizeof(dns_rbtnode_t *));
1561 hash_node(dns_rbt_t *rbt, dns_rbtnode_t *node, dns_name_t *name) {
1563 REQUIRE(DNS_RBTNODE_VALID(node));
1565 if (rbt->nodecount >= (rbt->hashsize *3))
1568 hash_add_node(rbt, node, name);
1572 unhash_node(dns_rbt_t *rbt, dns_rbtnode_t *node) {
1573 unsigned int bucket;
1574 dns_rbtnode_t *bucket_node;
1576 REQUIRE(DNS_RBTNODE_VALID(node));
1578 if (rbt->hashtable != NULL) {
1579 bucket = HASHVAL(node) % rbt->hashsize;
1580 bucket_node = rbt->hashtable[bucket];
1582 if (bucket_node == node)
1583 rbt->hashtable[bucket] = HASHNEXT(node);
1585 while (HASHNEXT(bucket_node) != node) {
1586 INSIST(HASHNEXT(bucket_node) != NULL);
1587 bucket_node = HASHNEXT(bucket_node);
1589 HASHNEXT(bucket_node) = HASHNEXT(node);
1593 #endif /* DNS_RBT_USEHASH */
1596 rotate_left(dns_rbtnode_t *node, dns_rbtnode_t **rootp) {
1597 dns_rbtnode_t *child;
1599 REQUIRE(DNS_RBTNODE_VALID(node));
1600 REQUIRE(rootp != NULL);
1602 child = RIGHT(node);
1603 INSIST(child != NULL);
1605 RIGHT(node) = LEFT(child);
1606 if (LEFT(child) != NULL)
1607 PARENT(LEFT(child)) = node;
1611 PARENT(child) = PARENT(node);
1613 if (IS_ROOT(node)) {
1619 if (LEFT(PARENT(node)) == node)
1620 LEFT(PARENT(node)) = child;
1622 RIGHT(PARENT(node)) = child;
1625 PARENT(node) = child;
1629 rotate_right(dns_rbtnode_t *node, dns_rbtnode_t **rootp) {
1630 dns_rbtnode_t *child;
1632 REQUIRE(DNS_RBTNODE_VALID(node));
1633 REQUIRE(rootp != NULL);
1636 INSIST(child != NULL);
1638 LEFT(node) = RIGHT(child);
1639 if (RIGHT(child) != NULL)
1640 PARENT(RIGHT(child)) = node;
1641 RIGHT(child) = node;
1644 PARENT(child) = PARENT(node);
1646 if (IS_ROOT(node)) {
1652 if (LEFT(PARENT(node)) == node)
1653 LEFT(PARENT(node)) = child;
1655 RIGHT(PARENT(node)) = child;
1658 PARENT(node) = child;
1662 * This is the real workhorse of the insertion code, because it does the
1663 * true red/black tree on a single level.
1666 dns_rbt_addonlevel(dns_rbtnode_t *node, dns_rbtnode_t *current, int order,
1667 dns_rbtnode_t **rootp)
1669 dns_rbtnode_t *child, *root, *parent, *grandparent;
1670 dns_name_t add_name, current_name;
1671 dns_offsets_t add_offsets, current_offsets;
1673 REQUIRE(rootp != NULL);
1674 REQUIRE(DNS_RBTNODE_VALID(node) && LEFT(node) == NULL &&
1675 RIGHT(node) == NULL);
1676 REQUIRE(current != NULL);
1681 * First node of a level.
1685 PARENT(node) = current;
1693 dns_name_init(&add_name, add_offsets);
1694 NODENAME(node, &add_name);
1696 dns_name_init(¤t_name, current_offsets);
1697 NODENAME(current, ¤t_name);
1700 INSIST(LEFT(current) == NULL);
1701 LEFT(current) = node;
1703 INSIST(RIGHT(current) == NULL);
1704 RIGHT(current) = node;
1707 INSIST(PARENT(node) == NULL);
1708 PARENT(node) = current;
1712 while (node != root && IS_RED(PARENT(node))) {
1714 * XXXDCL could do away with separate parent and grandparent
1715 * variables. They are vestiges of the days before parent
1716 * pointers. However, they make the code a little clearer.
1719 parent = PARENT(node);
1720 grandparent = PARENT(parent);
1722 if (parent == LEFT(grandparent)) {
1723 child = RIGHT(grandparent);
1724 if (child != NULL && IS_RED(child)) {
1727 MAKE_RED(grandparent);
1730 if (node == RIGHT(parent)) {
1731 rotate_left(parent, &root);
1733 parent = PARENT(node);
1734 grandparent = PARENT(parent);
1737 MAKE_RED(grandparent);
1738 rotate_right(grandparent, &root);
1741 child = LEFT(grandparent);
1742 if (child != NULL && IS_RED(child)) {
1745 MAKE_RED(grandparent);
1748 if (node == LEFT(parent)) {
1749 rotate_right(parent, &root);
1751 parent = PARENT(node);
1752 grandparent = PARENT(parent);
1755 MAKE_RED(grandparent);
1756 rotate_left(grandparent, &root);
1762 ENSURE(IS_ROOT(root));
1769 * This is the real workhorse of the deletion code, because it does the
1770 * true red/black tree on a single level.
1773 dns_rbt_deletefromlevel(dns_rbtnode_t *delete, dns_rbtnode_t **rootp) {
1774 dns_rbtnode_t *child, *sibling, *parent;
1775 dns_rbtnode_t *successor;
1777 REQUIRE(delete != NULL);
1780 * Verify that the parent history is (apparently) correct.
1782 INSIST((IS_ROOT(delete) && *rootp == delete) ||
1783 (! IS_ROOT(delete) &&
1784 (LEFT(PARENT(delete)) == delete ||
1785 RIGHT(PARENT(delete)) == delete)));
1789 if (LEFT(delete) == NULL) {
1790 if (RIGHT(delete) == NULL) {
1791 if (IS_ROOT(delete)) {
1793 * This is the only item in the tree.
1800 * This node has one child, on the right.
1802 child = RIGHT(delete);
1804 } else if (RIGHT(delete) == NULL)
1806 * This node has one child, on the left.
1808 child = LEFT(delete);
1810 dns_rbtnode_t holder, *tmp = &holder;
1813 * This node has two children, so it cannot be directly
1814 * deleted. Find its immediate in-order successor and
1815 * move it to this location, then do the deletion at the
1816 * old site of the successor.
1818 successor = RIGHT(delete);
1819 while (LEFT(successor) != NULL)
1820 successor = LEFT(successor);
1823 * The successor cannot possibly have a left child;
1824 * if there is any child, it is on the right.
1826 if (RIGHT(successor) != NULL)
1827 child = RIGHT(successor);
1830 * Swap the two nodes; it would be simpler to just replace
1831 * the value being deleted with that of the successor,
1832 * but this rigamarole is done so the caller has complete
1833 * control over the pointers (and memory allocation) of
1834 * all of nodes. If just the key value were removed from
1835 * the tree, the pointer to the node would be unchanged.
1839 * First, put the successor in the tree location of the
1840 * node to be deleted. Save its existing tree pointer
1841 * information, which will be needed when linking up
1842 * delete to the successor's old location.
1844 memmove(tmp, successor, sizeof(dns_rbtnode_t));
1846 if (IS_ROOT(delete)) {
1848 successor->is_root = ISC_TRUE;
1849 delete->is_root = ISC_FALSE;
1852 if (LEFT(PARENT(delete)) == delete)
1853 LEFT(PARENT(delete)) = successor;
1855 RIGHT(PARENT(delete)) = successor;
1857 PARENT(successor) = PARENT(delete);
1858 LEFT(successor) = LEFT(delete);
1859 RIGHT(successor) = RIGHT(delete);
1860 COLOR(successor) = COLOR(delete);
1862 if (LEFT(successor) != NULL)
1863 PARENT(LEFT(successor)) = successor;
1864 if (RIGHT(successor) != successor)
1865 PARENT(RIGHT(successor)) = successor;
1868 * Now relink the node to be deleted into the
1869 * successor's previous tree location. PARENT(tmp)
1870 * is the successor's original parent.
1872 INSIST(! IS_ROOT(delete));
1874 if (PARENT(tmp) == delete) {
1876 * Node being deleted was successor's parent.
1878 RIGHT(successor) = delete;
1879 PARENT(delete) = successor;
1882 LEFT(PARENT(tmp)) = delete;
1883 PARENT(delete) = PARENT(tmp);
1887 * Original location of successor node has no left.
1889 LEFT(delete) = NULL;
1890 RIGHT(delete) = RIGHT(tmp);
1891 COLOR(delete) = COLOR(tmp);
1895 * Remove the node by removing the links from its parent.
1897 if (! IS_ROOT(delete)) {
1898 if (LEFT(PARENT(delete)) == delete)
1899 LEFT(PARENT(delete)) = child;
1901 RIGHT(PARENT(delete)) = child;
1904 PARENT(child) = PARENT(delete);
1908 * This is the root being deleted, and at this point
1909 * it is known to have just one child.
1913 PARENT(child) = PARENT(delete);
1917 * Fix color violations.
1919 if (IS_BLACK(delete)) {
1920 parent = PARENT(delete);
1922 while (child != *rootp && IS_BLACK(child)) {
1923 INSIST(child == NULL || ! IS_ROOT(child));
1925 if (LEFT(parent) == child) {
1926 sibling = RIGHT(parent);
1928 if (IS_RED(sibling)) {
1929 MAKE_BLACK(sibling);
1931 rotate_left(parent, rootp);
1932 sibling = RIGHT(parent);
1935 INSIST(sibling != NULL);
1937 if (IS_BLACK(LEFT(sibling)) &&
1938 IS_BLACK(RIGHT(sibling))) {
1944 if (IS_BLACK(RIGHT(sibling))) {
1945 MAKE_BLACK(LEFT(sibling));
1947 rotate_right(sibling, rootp);
1948 sibling = RIGHT(parent);
1951 COLOR(sibling) = COLOR(parent);
1953 INSIST(RIGHT(sibling) != NULL);
1954 MAKE_BLACK(RIGHT(sibling));
1955 rotate_left(parent, rootp);
1961 * Child is parent's right child.
1962 * Everything is done the same as above,
1965 sibling = LEFT(parent);
1967 if (IS_RED(sibling)) {
1968 MAKE_BLACK(sibling);
1970 rotate_right(parent, rootp);
1971 sibling = LEFT(parent);
1974 INSIST(sibling != NULL);
1976 if (IS_BLACK(LEFT(sibling)) &&
1977 IS_BLACK(RIGHT(sibling))) {
1982 if (IS_BLACK(LEFT(sibling))) {
1983 MAKE_BLACK(RIGHT(sibling));
1985 rotate_left(sibling, rootp);
1986 sibling = LEFT(parent);
1989 COLOR(sibling) = COLOR(parent);
1991 INSIST(LEFT(sibling) != NULL);
1992 MAKE_BLACK(LEFT(sibling));
1993 rotate_right(parent, rootp);
1998 parent = PARENT(child);
2007 * This should only be used on the root of a tree, because no color fixup
2010 * NOTE: No root pointer maintenance is done, because the function is only
2011 * used for two cases:
2012 * + deleting everything DOWN from a node that is itself being deleted, and
2013 * + deleting the entire tree of trees from dns_rbt_destroy.
2014 * In each case, the root pointer is no longer relevant, so there
2015 * is no need for a root parameter to this function.
2017 * If the function is ever intended to be used to delete something where
2018 * a pointer needs to be told that this tree no longer exists,
2019 * this function would need to adjusted accordingly.
2022 dns_rbt_deletetree(dns_rbt_t *rbt, dns_rbtnode_t *node) {
2023 isc_result_t result = ISC_R_SUCCESS;
2024 REQUIRE(VALID_RBT(rbt));
2029 if (LEFT(node) != NULL) {
2030 result = dns_rbt_deletetree(rbt, LEFT(node));
2031 if (result != ISC_R_SUCCESS)
2035 if (RIGHT(node) != NULL) {
2036 result = dns_rbt_deletetree(rbt, RIGHT(node));
2037 if (result != ISC_R_SUCCESS)
2041 if (DOWN(node) != NULL) {
2042 result = dns_rbt_deletetree(rbt, DOWN(node));
2043 if (result != ISC_R_SUCCESS)
2048 if (result != ISC_R_SUCCESS)
2051 if (DATA(node) != NULL && rbt->data_deleter != NULL)
2052 rbt->data_deleter(DATA(node), rbt->deleter_arg);
2054 unhash_node(rbt, node);
2055 #if DNS_RBT_USEMAGIC
2059 isc_mem_put(rbt->mctx, node, NODE_SIZE(node));
2065 dns_rbt_deletetreeflat(dns_rbt_t *rbt, unsigned int quantum,
2066 dns_rbtnode_t **nodep)
2068 dns_rbtnode_t *parent;
2069 dns_rbtnode_t *node = *nodep;
2070 REQUIRE(VALID_RBT(rbt));
2079 if (LEFT(node) != NULL) {
2083 if (DOWN(node) != NULL) {
2088 if (DATA(node) != NULL && rbt->data_deleter != NULL)
2089 rbt->data_deleter(DATA(node), rbt->deleter_arg);
2092 * Note: we don't call unhash_node() here as we are destroying
2093 * the complete rbt tree.
2095 #if DNS_RBT_USEMAGIC
2098 parent = PARENT(node);
2099 if (RIGHT(node) != NULL)
2100 PARENT(RIGHT(node)) = parent;
2101 if (parent != NULL) {
2102 if (LEFT(parent) == node)
2103 LEFT(parent) = RIGHT(node);
2104 else if (DOWN(parent) == node)
2105 DOWN(parent) = RIGHT(node);
2107 parent = RIGHT(node);
2109 isc_mem_put(rbt->mctx, node, NODE_SIZE(node));
2112 if (quantum != 0 && --quantum == 0) {
2120 dns_rbt_indent(int depth) {
2123 for (i = 0; i < depth; i++)
2128 dns_rbt_printnodename(dns_rbtnode_t *node) {
2131 char buffer[DNS_NAME_FORMATSIZE];
2132 dns_offsets_t offsets;
2134 r.length = NAMELEN(node);
2135 r.base = NAME(node);
2137 dns_name_init(&name, offsets);
2138 dns_name_fromregion(&name, &r);
2140 dns_name_format(&name, buffer, sizeof(buffer));
2142 printf("%s", buffer);
2146 dns_rbt_printtree(dns_rbtnode_t *root, dns_rbtnode_t *parent, int depth) {
2147 dns_rbt_indent(depth);
2150 dns_rbt_printnodename(root);
2151 printf(" (%s", IS_RED(root) ? "RED" : "black");
2154 dns_rbt_printnodename(parent);
2157 if ((! IS_ROOT(root) && PARENT(root) != parent) ||
2158 ( IS_ROOT(root) && depth > 0 &&
2159 DOWN(PARENT(root)) != root)) {
2161 printf(" (BAD parent pointer! -> ");
2162 if (PARENT(root) != NULL)
2163 dns_rbt_printnodename(PARENT(root));
2175 dns_rbt_indent(depth);
2176 printf("++ BEG down from ");
2177 dns_rbt_printnodename(root);
2179 dns_rbt_printtree(DOWN(root), NULL, depth);
2180 dns_rbt_indent(depth);
2181 printf("-- END down from ");
2182 dns_rbt_printnodename(root);
2186 if (IS_RED(root) && IS_RED(LEFT(root)))
2187 printf("** Red/Red color violation on left\n");
2188 dns_rbt_printtree(LEFT(root), root, depth);
2190 if (IS_RED(root) && IS_RED(RIGHT(root)))
2191 printf("** Red/Red color violation on right\n");
2192 dns_rbt_printtree(RIGHT(root), root, depth);
2199 dns_rbt_printall(dns_rbt_t *rbt) {
2200 REQUIRE(VALID_RBT(rbt));
2202 dns_rbt_printtree(rbt->root, NULL, 0);
2210 dns_rbtnodechain_init(dns_rbtnodechain_t *chain, isc_mem_t *mctx) {
2212 * Initialize 'chain'.
2215 REQUIRE(chain != NULL);
2219 chain->level_count = 0;
2220 chain->level_matches = 0;
2221 memset(chain->levels, 0, sizeof(chain->levels));
2223 chain->magic = CHAIN_MAGIC;
2227 dns_rbtnodechain_current(dns_rbtnodechain_t *chain, dns_name_t *name,
2228 dns_name_t *origin, dns_rbtnode_t **node)
2230 isc_result_t result = ISC_R_SUCCESS;
2232 REQUIRE(VALID_CHAIN(chain));
2237 if (chain->end == NULL)
2238 return (ISC_R_NOTFOUND);
2241 NODENAME(chain->end, name);
2243 if (chain->level_count == 0) {
2245 * Names in the top level tree are all absolute.
2246 * Always make 'name' relative.
2248 INSIST(dns_name_isabsolute(name));
2251 * This is cheaper than dns_name_getlabelsequence().
2255 name->attributes &= ~DNS_NAMEATTR_ABSOLUTE;
2259 if (origin != NULL) {
2260 if (chain->level_count > 0)
2261 result = chain_name(chain, origin, ISC_FALSE);
2263 result = dns_name_copy(dns_rootname, origin, NULL);
2270 dns_rbtnodechain_prev(dns_rbtnodechain_t *chain, dns_name_t *name,
2273 dns_rbtnode_t *current, *previous, *predecessor;
2274 isc_result_t result = ISC_R_SUCCESS;
2275 isc_boolean_t new_origin = ISC_FALSE;
2277 REQUIRE(VALID_CHAIN(chain) && chain->end != NULL);
2281 current = chain->end;
2283 if (LEFT(current) != NULL) {
2285 * Moving left one then right as far as possible is the
2286 * previous node, at least for this level.
2288 current = LEFT(current);
2290 while (RIGHT(current) != NULL)
2291 current = RIGHT(current);
2293 predecessor = current;
2297 * No left links, so move toward the root. If at any point on
2298 * the way there the link from parent to child is a right
2299 * link, then the parent is the previous node, at least
2302 while (! IS_ROOT(current)) {
2304 current = PARENT(current);
2306 if (RIGHT(current) == previous) {
2307 predecessor = current;
2313 if (predecessor != NULL) {
2315 * Found a predecessor node in this level. It might not
2316 * really be the predecessor, however.
2318 if (DOWN(predecessor) != NULL) {
2320 * The predecessor is really down at least one level.
2321 * Go down and as far right as possible, and repeat
2322 * as long as the rightmost node has a down pointer.
2326 * XXX DCL Need to do something about origins
2327 * here. See whether to go down, and if so
2328 * whether it is truly what Bob calls a
2331 ADD_LEVEL(chain, predecessor);
2332 predecessor = DOWN(predecessor);
2334 /* XXX DCL duplicated from above; clever
2335 * way to unduplicate? */
2337 while (RIGHT(predecessor) != NULL)
2338 predecessor = RIGHT(predecessor);
2339 } while (DOWN(predecessor) != NULL);
2341 /* XXX DCL probably needs work on the concept */
2343 new_origin = ISC_TRUE;
2346 } else if (chain->level_count > 0) {
2348 * Dang, didn't find a predecessor in this level.
2349 * Got to the root of this level without having traversed
2350 * any right links. Ascend the tree one level; the
2351 * node that points to this tree is the predecessor.
2353 INSIST(chain->level_count > 0 && IS_ROOT(current));
2354 predecessor = chain->levels[--chain->level_count];
2356 /* XXX DCL probably needs work on the concept */
2358 * Don't declare an origin change when the new origin is "."
2359 * at the top level tree, because "." is declared as the origin
2360 * for the second level tree.
2362 if (origin != NULL &&
2363 (chain->level_count > 0 || OFFSETLEN(predecessor) > 1))
2364 new_origin = ISC_TRUE;
2367 if (predecessor != NULL) {
2368 chain->end = predecessor;
2371 result = dns_rbtnodechain_current(chain, name, origin,
2373 if (result == ISC_R_SUCCESS)
2374 result = DNS_R_NEWORIGIN;
2377 result = dns_rbtnodechain_current(chain, name, NULL,
2381 result = ISC_R_NOMORE;
2387 dns_rbtnodechain_down(dns_rbtnodechain_t *chain, dns_name_t *name,
2390 dns_rbtnode_t *current, *successor;
2391 isc_result_t result = ISC_R_SUCCESS;
2392 isc_boolean_t new_origin = ISC_FALSE;
2394 REQUIRE(VALID_CHAIN(chain) && chain->end != NULL);
2398 current = chain->end;
2400 if (DOWN(current) != NULL) {
2402 * Don't declare an origin change when the new origin is "."
2403 * at the second level tree, because "." is already declared
2404 * as the origin for the top level tree.
2406 if (chain->level_count > 0 ||
2407 OFFSETLEN(current) > 1)
2408 new_origin = ISC_TRUE;
2410 ADD_LEVEL(chain, current);
2411 current = DOWN(current);
2413 while (LEFT(current) != NULL)
2414 current = LEFT(current);
2416 successor = current;
2419 if (successor != NULL) {
2420 chain->end = successor;
2423 * It is not necessary to use dns_rbtnodechain_current like
2424 * the other functions because this function will never
2425 * find a node in the topmost level. This is because the
2426 * root level will never be more than one name, and everything
2427 * in the megatree is a successor to that node, down at
2428 * the second level or below.
2432 NODENAME(chain->end, name);
2436 result = chain_name(chain, origin, ISC_FALSE);
2438 if (result == ISC_R_SUCCESS)
2439 result = DNS_R_NEWORIGIN;
2442 result = ISC_R_SUCCESS;
2445 result = ISC_R_NOMORE;
2451 dns_rbtnodechain_nextflat(dns_rbtnodechain_t *chain, dns_name_t *name) {
2452 dns_rbtnode_t *current, *previous, *successor;
2453 isc_result_t result = ISC_R_SUCCESS;
2455 REQUIRE(VALID_CHAIN(chain) && chain->end != NULL);
2459 current = chain->end;
2461 if (RIGHT(current) == NULL) {
2462 while (! IS_ROOT(current)) {
2464 current = PARENT(current);
2466 if (LEFT(current) == previous) {
2467 successor = current;
2472 current = RIGHT(current);
2474 while (LEFT(current) != NULL)
2475 current = LEFT(current);
2477 successor = current;
2480 if (successor != NULL) {
2481 chain->end = successor;
2484 NODENAME(chain->end, name);
2486 result = ISC_R_SUCCESS;
2488 result = ISC_R_NOMORE;
2494 dns_rbtnodechain_next(dns_rbtnodechain_t *chain, dns_name_t *name,
2497 dns_rbtnode_t *current, *previous, *successor;
2498 isc_result_t result = ISC_R_SUCCESS;
2499 isc_boolean_t new_origin = ISC_FALSE;
2501 REQUIRE(VALID_CHAIN(chain) && chain->end != NULL);
2505 current = chain->end;
2508 * If there is a level below this node, the next node is the leftmost
2509 * node of the next level.
2511 if (DOWN(current) != NULL) {
2513 * Don't declare an origin change when the new origin is "."
2514 * at the second level tree, because "." is already declared
2515 * as the origin for the top level tree.
2517 if (chain->level_count > 0 ||
2518 OFFSETLEN(current) > 1)
2519 new_origin = ISC_TRUE;
2521 ADD_LEVEL(chain, current);
2522 current = DOWN(current);
2524 while (LEFT(current) != NULL)
2525 current = LEFT(current);
2527 successor = current;
2529 } else if (RIGHT(current) == NULL) {
2531 * The successor is up, either in this level or a previous one.
2532 * Head back toward the root of the tree, looking for any path
2533 * that was via a left link; the successor is the node that has
2534 * that left link. In the event the root of the level is
2535 * reached without having traversed any left links, ascend one
2536 * level and look for either a right link off the point of
2537 * ascent, or search for a left link upward again, repeating
2538 * ascends until either case is true.
2541 while (! IS_ROOT(current)) {
2543 current = PARENT(current);
2545 if (LEFT(current) == previous) {
2546 successor = current;
2551 if (successor == NULL) {
2553 * Reached the root without having traversed
2554 * any left pointers, so this level is done.
2556 if (chain->level_count == 0)
2559 current = chain->levels[--chain->level_count];
2560 new_origin = ISC_TRUE;
2562 if (RIGHT(current) != NULL)
2565 } while (successor == NULL);
2568 if (successor == NULL && RIGHT(current) != NULL) {
2569 current = RIGHT(current);
2571 while (LEFT(current) != NULL)
2572 current = LEFT(current);
2574 successor = current;
2577 if (successor != NULL) {
2578 chain->end = successor;
2581 * It is not necessary to use dns_rbtnodechain_current like
2582 * the other functions because this function will never
2583 * find a node in the topmost level. This is because the
2584 * root level will never be more than one name, and everything
2585 * in the megatree is a successor to that node, down at
2586 * the second level or below.
2590 NODENAME(chain->end, name);
2594 result = chain_name(chain, origin, ISC_FALSE);
2596 if (result == ISC_R_SUCCESS)
2597 result = DNS_R_NEWORIGIN;
2600 result = ISC_R_SUCCESS;
2603 result = ISC_R_NOMORE;
2609 dns_rbtnodechain_first(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
2610 dns_name_t *name, dns_name_t *origin)
2613 isc_result_t result;
2615 REQUIRE(VALID_RBT(rbt));
2616 REQUIRE(VALID_CHAIN(chain));
2618 dns_rbtnodechain_reset(chain);
2620 chain->end = rbt->root;
2622 result = dns_rbtnodechain_current(chain, name, origin, NULL);
2624 if (result == ISC_R_SUCCESS)
2625 result = DNS_R_NEWORIGIN;
2631 dns_rbtnodechain_last(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
2632 dns_name_t *name, dns_name_t *origin)
2635 isc_result_t result;
2637 REQUIRE(VALID_RBT(rbt));
2638 REQUIRE(VALID_CHAIN(chain));
2640 dns_rbtnodechain_reset(chain);
2642 result = move_chain_to_last(chain, rbt->root);
2643 if (result != ISC_R_SUCCESS)
2646 result = dns_rbtnodechain_current(chain, name, origin, NULL);
2648 if (result == ISC_R_SUCCESS)
2649 result = DNS_R_NEWORIGIN;
2656 dns_rbtnodechain_reset(dns_rbtnodechain_t *chain) {
2658 * Free any dynamic storage associated with 'chain', and then
2659 * reinitialize 'chain'.
2662 REQUIRE(VALID_CHAIN(chain));
2665 chain->level_count = 0;
2666 chain->level_matches = 0;
2670 dns_rbtnodechain_invalidate(dns_rbtnodechain_t *chain) {
2672 * Free any dynamic storage associated with 'chain', and then
2673 * invalidate 'chain'.
2676 dns_rbtnodechain_reset(chain);