Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / bind-9.3 / lib / dns / include / dns / rbt.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and 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.
8  *
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.
16  */
17
18 /* $Id: rbt.h,v 1.55.12.6 2004/10/11 05:55:51 marka Exp $ */
19
20 #ifndef DNS_RBT_H
21 #define DNS_RBT_H 1
22
23 #include <isc/lang.h>
24 #include <isc/magic.h>
25
26 #include <dns/types.h>
27
28 ISC_LANG_BEGINDECLS
29
30 #define DNS_RBT_USEHASH 1
31
32 /*
33  * Option values for dns_rbt_findnode() and dns_rbt_findname().
34  * These are used to form a bitmask.
35  */
36 #define DNS_RBTFIND_NOOPTIONS                   0x00
37 #define DNS_RBTFIND_EMPTYDATA                   0x01
38 #define DNS_RBTFIND_NOEXACT                     0x02
39 #define DNS_RBTFIND_NOPREDECESSOR               0x04
40
41 /*
42  * These should add up to 30.
43  */
44 #define DNS_RBT_LOCKLENGTH                      10
45 #define DNS_RBT_REFLENGTH                       20
46
47 #define DNS_RBTNODE_MAGIC               ISC_MAGIC('R','B','N','O')
48 #if DNS_RBT_USEMAGIC
49 #define DNS_RBTNODE_VALID(n)            ISC_MAGIC_VALID(n, DNS_RBTNODE_MAGIC)
50 #else
51 #define DNS_RBTNODE_VALID(n)            ISC_TRUE
52 #endif
53
54 /*
55  * This is the structure that is used for each node in the red/black
56  * tree of trees.  NOTE WELL:  the implementation manages this as a variable
57  * length structure, with the actual wire-format name and other data
58  * appended to this structure.  Allocating a contiguous block of memory for
59  * multiple dns_rbtnode structures will not work.
60  */
61 typedef struct dns_rbtnode {
62 #if DNS_RBT_USEMAGIC
63         unsigned int magic;
64 #endif
65         struct dns_rbtnode *parent;
66         struct dns_rbtnode *left;
67         struct dns_rbtnode *right;
68         struct dns_rbtnode *down;
69 #ifdef DNS_RBT_USEHASH
70         struct dns_rbtnode *hashnext;
71 #endif
72         /*
73          * The following bitfields add up to a total bitwidth of 32.
74          * The range of values necessary for each item is indicated,
75          * but in the case of "attributes" the field is wider to accomodate
76          * possible future expansion.  "offsetlen" could be one bit
77          * narrower by always adjusting its value by 1 to find the real
78          * offsetlen, but doing so does not gain anything (except perhaps
79          * another bit for "attributes", which doesn't yet need any more).
80          *
81          * In each case below the "range" indicated is what's _necessary_ for
82          * the bitfield to hold, not what it actually _can_ hold.
83          */
84         unsigned int is_root : 1;       /* range is 0..1 */
85         unsigned int color : 1;         /* range is 0..1 */
86         unsigned int find_callback : 1; /* range is 0..1 */
87         unsigned int attributes : 4;    /* range is 0..2 */
88         unsigned int namelen : 8;       /* range is 1..255 */
89         unsigned int offsetlen : 8;     /* range is 1..128 */
90         unsigned int padbytes : 9;      /* range is 0..380 */
91
92 #ifdef DNS_RBT_USEHASH
93         unsigned int hashval;
94 #endif
95
96         /*
97          * These values are used in the RBT DB implementation.  The appropriate
98          * node lock must be held before accessing them.
99          */
100         void *data;
101         unsigned int dirty:1;
102         unsigned int wild:1;
103         unsigned int locknum:DNS_RBT_LOCKLENGTH;
104         unsigned int references:DNS_RBT_REFLENGTH;
105 } dns_rbtnode_t;
106
107 typedef isc_result_t (*dns_rbtfindcallback_t)(dns_rbtnode_t *node,
108                                               dns_name_t *name,
109                                               void *callback_arg);
110
111 /*****
112  *****  Chain Info
113  *****/
114
115 /*
116  * A chain is used to keep track of the sequence of nodes to reach any given
117  * node from the root of the tree.  Originally nodes did not have parent
118  * pointers in them (for memory usage reasons) so there was no way to find
119  * the path back to the root from any given node.  Now that nodes have parent
120  * pointers, chains might be going away in a future release, though the
121  * movement functionality would remain.
122  *
123  * In any event, parent information, whether via parent pointers or chains, is
124  * necessary information for iterating through the tree or for basic internal
125  * tree maintenance issues (ie, the rotations that are done to rebalance the
126  * tree when a node is added).  The obvious implication of this is that for a
127  * chain to remain valid, the tree has to be locked down against writes for the
128  * duration of the useful life of the chain, because additions or removals can
129  * change the path from the root to the node the chain has targetted.
130  *
131  * The dns_rbtnodechain_ functions _first, _last, _prev and _next all take
132  * dns_name_t parameters for the name and the origin, which can be NULL.  If
133  * non-NULL, 'name' will end up pointing to the name data and offsets that are
134  * stored at the node (and thus it will be read-only), so it should be a
135  * regular dns_name_t that has been initialized with dns_name_init.  When
136  * 'origin' is non-NULL, it will get the name of the origin stored in it, so it
137  * needs to have its own buffer space and offsets, which is most easily
138  * accomplished with a dns_fixedname_t.  It is _not_ necessary to reinitialize
139  * either 'name' or 'origin' between calls to the chain functions.
140  *
141  * NOTE WELL: even though the name data at the root of the tree of trees will
142  * be absolute (typically just "."), it will will be made into a relative name
143  * with an origin of "." -- an empty name when the node is ".".  This is
144  * because a common on operation on 'name' and 'origin' is to use
145  * dns_name_concatenate() on them to generate the complete name.  An empty name
146  * can be detected when dns_name_countlabels == 0, and is printed by
147  * dns_name_totext()/dns_name_format() as "@", consistent with RFC1035's
148  * definition of "@" as the current origin.
149  *
150  * dns_rbtnodechain_current is similar to the _first, _last, _prev and _next
151  * functions but additionally can provide the node to which the chain points.
152  */
153
154 /*
155  * The number of level blocks to allocate at a time.  Currently the maximum
156  * number of levels is allocated directly in the structure, but future
157  * revisions of this code might have a static initial block with dynamic
158  * growth.  Allocating space for 256 levels when the tree is almost never that
159  * deep is wasteful, but it's not clear that it matters, since the waste is
160  * only 2MB for 1000 concurrently active chains on a system with 64-bit
161  * pointers.
162  */
163 #define DNS_RBT_LEVELBLOCK 254
164
165 typedef struct dns_rbtnodechain {
166         unsigned int            magic;
167         isc_mem_t *             mctx;
168         /*
169          * The terminal node of the chain.  It is not in levels[].
170          * This is ostensibly private ... but in a pinch it could be
171          * used tell that the chain points nowhere without needing to
172          * call dns_rbtnodechain_current().
173          */
174         dns_rbtnode_t *         end;
175         /*
176          * The maximum number of labels in a name is 128; bitstrings mean
177          * a conceptually very large number (which I have not bothered to
178          * compute) of logical levels because splitting can potentially occur
179          * at each bit.  However, DNSSEC restricts the number of "logical"
180          * labels in a name to 255, meaning only 254 pointers are needed
181          * in the worst case.
182          */
183         dns_rbtnode_t *         levels[DNS_RBT_LEVELBLOCK];
184         /*
185          * level_count indicates how deep the chain points into the
186          * tree of trees, and is the index into the levels[] array.
187          * Thus, levels[level_count - 1] is the last level node stored.
188          * A chain that points to the top level of the tree of trees has
189          * a level_count of 0, the first level has a level_count of 1, and
190          * so on.
191          */
192         unsigned int            level_count;
193         /*
194          * level_matches tells how many levels matched above the node
195          * returned by dns_rbt_findnode().  A match (partial or exact) found
196          * in the first level thus results in level_matches being set to 1.
197          * This is used by the rbtdb to set the start point for a recursive
198          * search of superdomains until the RR it is looking for is found.
199          */
200         unsigned int            level_matches;
201 } dns_rbtnodechain_t;
202
203 /*****
204  ***** Public interfaces.
205  *****/
206
207 isc_result_t
208 dns_rbt_create(isc_mem_t *mctx, void (*deleter)(void *, void *),
209                void *deleter_arg, dns_rbt_t **rbtp);
210 /*
211  * Initialize a red-black tree of trees.
212  *
213  * Notes:
214  *      The deleter argument, if non-null, points to a function that is
215  *      responsible for cleaning up any memory associated with the data
216  *      pointer of a node when the node is deleted.  It is passed the
217  *      deleted node's data pointer as its first argument and deleter_arg
218  *      as its second argument.
219  *
220  * Requires:
221  *      mctx is a pointer to a valid memory context.
222  *      rbtp != NULL && *rbtp == NULL
223  *      arg == NULL iff deleter == NULL
224  *
225  * Ensures:
226  *      If result is ISC_R_SUCCESS:
227  *              *rbtp points to a valid red-black tree manager
228  *
229  *      If result is failure:
230  *              *rbtp does not point to a valid red-black tree manager.
231  *
232  * Returns:
233  *      ISC_R_SUCCESS   Success
234  *      ISC_R_NOMEMORY  Resource limit: Out of Memory
235  */
236
237 isc_result_t
238 dns_rbt_addname(dns_rbt_t *rbt, dns_name_t *name, void *data);
239 /*
240  * Add 'name' to the tree of trees, associated with 'data'.
241  *
242  * Notes:
243  *      'data' is never required to be non-NULL, but specifying it
244  *      when the name is added is faster than searching for 'name'
245  *      again and then setting the data pointer.  The lack of a data pointer
246  *      for a node also has other ramifications regarding whether
247  *      dns_rbt_findname considers a node to exist, or dns_rbt_deletename
248  *      joins nodes.
249  *
250  * Requires:
251  *      rbt is a valid rbt manager.
252  *      dns_name_isabsolute(name) == TRUE
253  *
254  * Ensures:
255  *      'name' is not altered in any way.
256  *
257  *      Any external references to nodes in the tree are unaffected by
258  *      node splits that are necessary to insert the new name.
259  *
260  *      If result is ISC_R_SUCCESS:
261  *              'name' is findable in the red/black tree of trees in O(log N).
262  *
263  *              The data pointer of the node for 'name' is set to 'data'.
264  *
265  *      If result is ISC_R_EXISTS or ISC_R_NOSPACE:
266  *              The tree of trees is unaltered.
267  *
268  *      If result is ISC_R_NOMEMORY:
269  *              No guarantees.
270  *
271  * Returns:
272  *      ISC_R_SUCCESS   Success
273  *      ISC_R_EXISTS    The name already exists with associated data.
274  *      ISC_R_NOSPACE   The name had more logical labels than are allowed.
275  *      ISC_R_NOMEMORY  Resource Limit: Out of Memory
276  */
277
278 isc_result_t
279 dns_rbt_addnode(dns_rbt_t *rbt, dns_name_t *name, dns_rbtnode_t **nodep);
280
281 /*
282  * Just like dns_rbt_addname, but returns the address of the node.
283  *
284  * Requires:
285  *      rbt is a valid rbt structure.
286  *      dns_name_isabsolute(name) == TRUE
287  *      nodep != NULL && *nodep == NULL
288  *
289  * Ensures:
290  *      'name' is not altered in any way.
291  *
292  *      Any external references to nodes in the tree are unaffected by
293  *      node splits that are necessary to insert the new name.
294  *
295  *      If result is ISC_R_SUCCESS:
296  *              'name' is findable in the red/black tree of trees in O(log N).
297  *
298  *              *nodep is the node that was added for 'name'.
299  *
300  *      If result is ISC_R_EXISTS:
301  *              The tree of trees is unaltered.
302  *
303  *              *nodep is the existing node for 'name'.
304  *
305  *      If result is ISC_R_NOMEMORY:
306  *              No guarantees.
307  *
308  * Returns:
309  *      ISC_R_SUCCESS   Success
310  *      ISC_R_EXISTS    The name already exists, possibly without data.
311  *      ISC_R_NOMEMORY  Resource Limit: Out of Memory
312  */
313
314 isc_result_t
315 dns_rbt_findname(dns_rbt_t *rbt, dns_name_t *name, unsigned int options,
316                  dns_name_t *foundname, void **data);
317 /*
318  * Get the data pointer associated with 'name'.
319  *
320  * Notes:
321  *      When DNS_RBTFIND_NOEXACT is set, the closest matching superdomain is
322  *      returned (also subject to DNS_RBTFIND_EMPTYDATA), even when there is
323  *      an exact match in the tree.
324  *
325  *      A node that has no data is considered not to exist for this function,
326  *      unless the DNS_RBTFIND_EMPTYDATA option is set.
327  *
328  * Requires:
329  *      rbt is a valid rbt manager.
330  *      dns_name_isabsolute(name) == TRUE
331  *      data != NULL && *data == NULL
332  *
333  * Ensures:
334  *      'name' and the tree are not altered in any way.
335  *
336  *      If result is ISC_R_SUCCESS:
337  *              *data is the data associated with 'name'.
338  *
339  *      If result is DNS_R_PARTIALMATCH:
340  *              *data is the data associated with the deepest superdomain
341  *              of 'name' which has data.
342  *
343  *      If result is ISC_R_NOTFOUND:
344  *              Neither the name nor a superdomain was found with data.
345  *
346  * Returns:
347  *      ISC_R_SUCCESS           Success
348  *      DNS_R_PARTIALMATCH      Superdomain found with data
349  *      ISC_R_NOTFOUND          No match
350  *      ISC_R_NOSPACE           Concatenating nodes to form foundname failed
351  */
352
353 isc_result_t
354 dns_rbt_findnode(dns_rbt_t *rbt, dns_name_t *name, dns_name_t *foundname,
355                  dns_rbtnode_t **node, dns_rbtnodechain_t *chain,
356                  unsigned int options, dns_rbtfindcallback_t callback,
357                  void *callback_arg);
358 /*
359  * Find the node for 'name'.
360  *
361  * Notes:
362  *      A node that has no data is considered not to exist for this function,
363  *      unless the DNS_RBTFIND_EMPTYDATA option is set.  This applies to both
364  *      exact matches and partial matches.
365  *
366  *      If the chain parameter is non-NULL, then the path through the tree
367  *      to the DNSSEC predecessor of the searched for name is maintained,
368  *      unless the DNS_RBTFIND_NOPREDECESSOR or DNS_RBTFIND_NOEXACT option
369  *      is used. (For more details on those options, see below.)
370  *
371  *      If there is no predecessor, then the chain will point to nowhere, as
372  *      indicated by chain->end being NULL or dns_rbtnodechain_current
373  *      returning ISC_R_NOTFOUND.  Note that in a normal Internet DNS RBT
374  *      there will always be a predecessor for all names except the root
375  *      name, because '.' will exist and '.' is the predecessor of
376  *      everything.  But you can certainly construct a trivial tree and a
377  *      search for it that has no predecessor.
378  *
379  *      Within the chain structure, the 'levels' member of the structure holds
380  *      the root node of each level except the first.
381  *
382  *      The 'level_count' of the chain indicates how deep the chain to the
383  *      predecessor name is, as an index into the 'levels[]' array.  It does
384  *      not count name elements, per se, but only levels of the tree of trees,
385  *      the distinction arrising because multiple labels from a name can be
386  *      stored on only one level.  It is also does not include the level
387  *      that has the node, since that level is not stored in levels[].
388  *
389  *      The chain's 'level_matches' is not directly related to the predecessor.
390  *      It is the number of levels above the level of the found 'node',
391  *      regardless of whether it was a partial match or exact match.  When
392  *      the node is found in the top level tree, or no node is found at all,
393  *      level_matches is 0.
394  *
395  *      When DNS_RBTFIND_NOEXACT is set, the closest matching superdomain is
396  *      returned (also subject to DNS_RBTFIND_EMPTYDATA), even when
397  *      there is an exact match in the tree.  In this case, the chain
398  *      will not point to the DNSSEC predecessor, but will instead point
399  *      to the exact match, if there was any.  Thus the preceding paragraphs
400  *      should have "exact match" substituted for "predecessor" to describe
401  *      how the various elements of the chain are set.  This was done to
402  *      ensure that the chain's state was sane, and to prevent problems that
403  *      occurred when running the predecessor location code under conditions
404  *      it was not designed for.  It is not clear *where* the chain should
405  *      point when DNS_RBTFIND_NOEXACT is set, so if you end up using a chain
406  *      with this option because you want a particular node, let us know
407  *      where you want the chain pointed, so this can be made more firm.
408  *
409  * Requires:
410  *      rbt is a valid rbt manager.
411  *      dns_name_isabsolute(name) == TRUE.
412  *      node != NULL && *node == NULL.
413  *      DNS_RBTFIND_NOEXACT and DNS_RBTFIND_NOPREDECESSOR are mutally
414  *              exclusive.
415  *
416  * Ensures:
417  *      'name' and the tree are not altered in any way.
418  *
419  *      If result is ISC_R_SUCCESS:
420  *              *node is the terminal node for 'name'.
421  *
422  *              'foundname' and 'name' represent the same name (though not
423  *              the same memory).
424  *
425  *              'chain' points to the DNSSEC predecessor, if any, of 'name'.
426  *
427  *              chain->level_matches and chain->level_count are equal.
428  *
429  *      If result is DNS_R_PARTIALMATCH:
430  *              *node is the data associated with the deepest superdomain
431  *              of 'name' which has data.
432  *
433  *              'foundname' is the name of deepest superdomain (which has
434  *              data, unless the DNS_RBTFIND_EMPTYDATA option is set).
435  *
436  *              'chain' points to the DNSSEC predecessor, if any, of 'name'.
437  *
438  *      If result is ISC_R_NOTFOUND:
439  *              Neither the name nor a superdomain was found.  *node is NULL.
440  *
441  *              'chain' points to the DNSSEC predecessor, if any, of 'name'.
442  *
443  *              chain->level_matches is 0.
444  *
445  * Returns:
446  *      ISC_R_SUCCESS           Success
447  *      DNS_R_PARTIALMATCH      Superdomain found with data
448  *      ISC_R_NOTFOUND          No match, or superdomain with no data
449  *      ISC_R_NOSPACE Concatenating nodes to form foundname failed
450  */
451
452 isc_result_t
453 dns_rbt_deletename(dns_rbt_t *rbt, dns_name_t *name, isc_boolean_t recurse);
454 /*
455  * Delete 'name' from the tree of trees.
456  *
457  * Notes:
458  *      When 'name' is removed, if recurse is ISC_TRUE then all of its
459  *      subnames are removed too.
460  *
461  * Requires:
462  *      rbt is a valid rbt manager.
463  *      dns_name_isabsolute(name) == TRUE
464  *
465  * Ensures:
466  *      'name' is not altered in any way.
467  *
468  *      Does NOT ensure that any external references to nodes in the tree
469  *      are unaffected by node joins.
470  *
471  *      If result is ISC_R_SUCCESS:
472  *              'name' does not appear in the tree with data; however,
473  *              the node for the name might still exist which can be
474  *              found with dns_rbt_findnode (but not dns_rbt_findname).
475  *
476  *      If result is ISC_R_NOTFOUND:
477  *              'name' does not appear in the tree with data, because
478  *              it did not appear in the tree before the function was called.
479  *
480  *      If result is something else:
481  *              See result codes for dns_rbt_findnode (if it fails, the
482  *              node is not deleted) or dns_rbt_deletenode (if it fails,
483  *              the node is deleted, but the tree is not optimized when
484  *              it could have been).
485  *
486  * Returns:
487  *      ISC_R_SUCCESS   Success
488  *      ISC_R_NOTFOUND  No match
489  *      something_else  Any return code from dns_rbt_findnode except
490  *                      DNS_R_PARTIALMATCH (which causes ISC_R_NOTFOUND
491  *                      to be returned instead), and any code from
492  *                      dns_rbt_deletenode.
493  */
494
495 isc_result_t
496 dns_rbt_deletenode(dns_rbt_t *rbt, dns_rbtnode_t *node, isc_boolean_t recurse);
497 /*
498  * Delete 'node' from the tree of trees.
499  *
500  * Notes:
501  *      When 'node' is removed, if recurse is ISC_TRUE then all nodes
502  *      in levels down from it are removed too.
503  *
504  * Requires:
505  *      rbt is a valid rbt manager.
506  *      node != NULL.
507  *
508  * Ensures:
509  *      Does NOT ensure that any external references to nodes in the tree
510  *      are unaffected by node joins.
511  *
512  *      If result is ISC_R_SUCCESS:
513  *              'node' does not appear in the tree with data; however,
514  *              the node might still exist if it serves as a pointer to
515  *              a lower tree level as long as 'recurse' was false, hence
516  *              the node could can be found with dns_rbt_findnode whem
517  *              that function's empty_data_ok parameter is true.
518  *
519  *      If result is ISC_R_NOMEMORY or ISC_R_NOSPACE:
520  *              The node was deleted, but the tree structure was not
521  *              optimized.
522  *
523  * Returns:
524  *      ISC_R_SUCCESS   Success
525  *      ISC_R_NOMEMORY  Resource Limit: Out of Memory when joining nodes.
526  *      ISC_R_NOSPACE   dns_name_concatenate failed when joining nodes.
527  */
528
529 void
530 dns_rbt_namefromnode(dns_rbtnode_t *node, dns_name_t *name);
531 /*
532  * Convert the sequence of labels stored at 'node' into a 'name'.
533  *
534  * Notes:
535  *      This function does not return the full name, from the root, but
536  *      just the labels at the indicated node.
537  *
538  *      The name data pointed to by 'name' is the information stored
539  *      in the node, not a copy.  Altering the data at this pointer
540  *      will likely cause grief.
541  *
542  * Requires:
543  *      name->offsets == NULL
544  *
545  * Ensures:
546  *      'name' is DNS_NAMEATTR_READONLY.
547  *
548  *      'name' will point directly to the labels stored after the
549  *      dns_rbtnode_t struct.
550  *
551  *      'name' will have offsets that also point to the information stored
552  *      as part of the node.
553  */
554
555 isc_result_t
556 dns_rbt_fullnamefromnode(dns_rbtnode_t *node, dns_name_t *name);
557 /*
558  * Like dns_rbt_namefromnode, but returns the full name from the root.
559  *
560  * Notes:
561  *      Unlike dns_rbt_namefromnode, the name will not point directly
562  *      to node data.  Rather, dns_name_concatenate will be used to copy
563  *      the name data from each node into the 'name' argument.
564  *
565  * Requires:
566  *      name != NULL
567  *      name has a dedicated buffer.
568  *
569  * Returns:
570  *      ISC_R_SUCCESS
571  *      ISC_R_NOSPACE           (possible via dns_name_concatenate)
572  *      DNS_R_NAMETOOLONG       (possible via dns_name_concatenate)
573  */
574
575 char *
576 dns_rbt_formatnodename(dns_rbtnode_t *node, char *printname,
577                        unsigned int size);
578 /*
579  * Format the full name of a node for printing, using dns_name_format().
580  *
581  * Notes:
582  *      'size' is the length of the printname buffer.  This should be
583  *      DNS_NAME_FORMATSIZE or larger.
584  *
585  * Requires:
586  *      node and printname are not NULL.
587  *
588  * Returns:
589  *      The 'printname' pointer.
590  */
591
592 unsigned int
593 dns_rbt_nodecount(dns_rbt_t *rbt);
594 /*
595  * Obtain the number of nodes in the tree of trees.
596  *
597  * Requires:
598  *      rbt is a valid rbt manager.
599  */
600
601 void
602 dns_rbt_destroy(dns_rbt_t **rbtp);
603 isc_result_t
604 dns_rbt_destroy2(dns_rbt_t **rbtp, unsigned int quantum);
605 /*
606  * Stop working with a red-black tree of trees. 
607  * If 'quantum' is zero then the entire tree will be destroyed.
608  * If 'quantum' is non zero then up to 'quantum' nodes will be destroyed
609  * allowing the rbt to be incrementally destroyed by repeated calls to
610  * dns_rbt_destroy2().  Once dns_rbt_destroy2() has been called no other
611  * operations than dns_rbt_destroy()/dns_rbt_destroy2() should be
612  * performed on the tree of trees.
613  * 
614  * Requires:
615  *      *rbt is a valid rbt manager.
616  *
617  * Ensures on ISC_R_SUCCESS:
618  *      All space allocated by the RBT library has been returned.
619  *
620  *      *rbt is invalidated as an rbt manager.
621  *
622  * Returns:
623  *      ISC_R_SUCCESS
624  *      ISC_R_QUOTA if 'quantum' nodes have been destroyed.
625  */
626
627 void
628 dns_rbt_printall(dns_rbt_t *rbt);
629 /*
630  * Print an ASCII representation of the internal structure of the red-black
631  * tree of trees.
632  *
633  * Notes:
634  *      The name stored at each node, along with the node's color, is printed.
635  *      Then the down pointer, left and right pointers are displayed
636  *      recursively in turn.  NULL down pointers are silently omitted;
637  *      NULL left and right pointers are printed.
638  */
639
640 /*****
641  ***** Chain Functions
642  *****/
643
644 void
645 dns_rbtnodechain_init(dns_rbtnodechain_t *chain, isc_mem_t *mctx);
646 /*
647  * Initialize 'chain'.
648  *
649  * Requires:
650  *      'chain' is a valid pointer.
651  *
652  *      'mctx' is a valid memory context.
653  *
654  * Ensures:
655  *      'chain' is suitable for use.
656  */
657
658 void
659 dns_rbtnodechain_reset(dns_rbtnodechain_t *chain);
660 /*
661  * Free any dynamic storage associated with 'chain', and then reinitialize
662  * 'chain'.
663  *
664  * Requires:
665  *      'chain' is a valid pointer.
666  *
667  * Ensures:
668  *      'chain' is suitable for use, and uses no dynamic storage.
669  */
670
671 void
672 dns_rbtnodechain_invalidate(dns_rbtnodechain_t *chain);
673 /*
674  * Free any dynamic storage associated with 'chain', and then invalidates it.
675  *
676  * Notes:
677  *      Future calls to any dns_rbtnodechain_ function will need to call
678  *      dns_rbtnodechain_init on the chain first (except, of course,
679  *      dns_rbtnodechain_init itself).
680  *
681  * Requires:
682  *      'chain' is a valid chain.
683  *
684  * Ensures:
685  *      'chain' is no longer suitable for use, and uses no dynamic storage.
686  */
687
688 isc_result_t
689 dns_rbtnodechain_current(dns_rbtnodechain_t *chain, dns_name_t *name,
690                          dns_name_t *origin, dns_rbtnode_t **node);
691 /*
692  * Provide the name, origin and node to which the chain is currently pointed.
693  *
694  * Notes:
695  *      The tree need not have be locked against additions for the chain
696  *      to remain valid, however there are no guarantees if any deletion
697  *      has been made since the chain was established.
698  *
699  * Requires:
700  *      'chain' is a valid chain.
701  *
702  * Ensures:
703  *      'node', if non-NULL, is the node to which the chain was pointed
704  *      by dns_rbt_findnode, dns_rbtnodechain_first or dns_rbtnodechain_last.
705  *      If none were called for the chain since it was initialized or reset,
706  *      or if the was no predecessor to the name searched for with
707  *      dns_rbt_findnode, then '*node' is NULL and ISC_R_NOTFOUND is returned.
708  *
709  *      'name', if non-NULL, is the name stored at the terminal level of
710  *      the chain.  This is typically a single label, like the "www" of
711  *      "www.isc.org", but need not be so.  At the root of the tree of trees,
712  *      if the node is "." then 'name' is ".", otherwise it is relative to ".".
713  *      (Minimalist and atypical case:  if the tree has just the name
714  *      "isc.org." then the root node's stored name is "isc.org." but 'name'
715  *      will be "isc.org".)
716  *
717  *      'origin', if non-NULL, is the sequence of labels in the levels
718  *      above the terminal level, such as "isc.org." in the above example.
719  *      'origin' is always "." for the root node.
720  *
721  *
722  * Returns:
723  *      ISC_R_SUCCESS           name, origin & node were successfully set.
724  *      ISC_R_NOTFOUND          The chain does not point to any node.
725  *      <something_else>        Any error return from dns_name_concatenate.
726  */
727
728 isc_result_t
729 dns_rbtnodechain_first(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
730                        dns_name_t *name, dns_name_t *origin);
731 /*
732  * Set the chain to the lexically first node in the tree of trees.
733  *
734  * Notes:
735  *      By the definition of ordering for DNS names, the root of the tree of
736  *      trees is the very first node, since everything else in the megatree
737  *      uses it as a common suffix.
738  *
739  * Requires:
740  *      'chain' is a valid chain.
741  *      'rbt' is a valid rbt manager.
742  *
743  * Ensures:
744  *      The chain points to the very first node of the tree.
745  *
746  *      'name' and 'origin', if non-NULL, are set as described for
747  *      dns_rbtnodechain_current.  Thus 'origin' will always be ".".
748  *
749  * Returns:
750  *      DNS_R_NEWORIGIN         The name & origin were successfully set.
751  *      <something_else>        Any error result from dns_rbtnodechain_current.
752  */
753
754 isc_result_t
755 dns_rbtnodechain_last(dns_rbtnodechain_t *chain, dns_rbt_t *rbt,
756                        dns_name_t *name, dns_name_t *origin);
757 /*
758  * Set the chain to the lexically last node in the tree of trees.
759  *
760  * Requires:
761  *      'chain' is a valid chain.
762  *      'rbt' is a valid rbt manager.
763  *
764  * Ensures:
765  *      The chain points to the very last node of the tree.
766  *
767  *      'name' and 'origin', if non-NULL, are set as described for
768  *      dns_rbtnodechain_current.
769  *
770  * Returns:
771  *      DNS_R_NEWORIGIN         The name & origin were successfully set.
772  *      ISC_R_NOMEMORY          Resource Limit: Out of Memory building chain.
773  *      <something_else>        Any error result from dns_name_concatenate.
774  */
775
776 isc_result_t
777 dns_rbtnodechain_prev(dns_rbtnodechain_t *chain, dns_name_t *name,
778                       dns_name_t *origin);
779 /*
780  * Adjusts chain to point the DNSSEC predecessor of the name to which it
781  * is currently pointed.
782  *
783  * Requires:
784  *      'chain' is a valid chain.
785  *      'chain' has been pointed somewhere in the tree with dns_rbt_findnode,
786  *      dns_rbtnodechain_first or dns_rbtnodechain_last -- and remember that
787  *      dns_rbt_findnode is not guaranteed to point the chain somewhere,
788  *      since there may have been no predecessor to the searched for name.
789  *
790  * Ensures:
791  *      The chain is pointed to the predecessor of its current target.
792  *
793  *      'name' and 'origin', if non-NULL, are set as described for
794  *      dns_rbtnodechain_current.
795  *
796  *      'origin' is only if a new origin was found.
797  *
798  * Returns:
799  *      ISC_R_SUCCESS           The predecessor was found and 'name' was set.
800  *      DNS_R_NEWORIGIN         The predecessor was found with a different
801  *                              origin and 'name' and 'origin' were set.
802  *      ISC_R_NOMORE            There was no predecessor.
803  *      <something_else>        Any error result from dns_rbtnodechain_current.
804  */
805
806 isc_result_t
807 dns_rbtnodechain_next(dns_rbtnodechain_t *chain, dns_name_t *name,
808                       dns_name_t *origin);
809 /*
810  * Adjusts chain to point the DNSSEC successor of the name to which it
811  * is currently pointed.
812  *
813  * Requires:
814  *      'chain' is a valid chain.
815  *      'chain' has been pointed somewhere in the tree with dns_rbt_findnode,
816  *      dns_rbtnodechain_first or dns_rbtnodechain_last -- and remember that
817  *      dns_rbt_findnode is not guaranteed to point the chain somewhere,
818  *      since there may have been no predecessor to the searched for name.
819  *
820  * Ensures:
821  *      The chain is pointed to the successor of its current target.
822  *
823  *      'name' and 'origin', if non-NULL, are set as described for
824  *      dns_rbtnodechain_current.
825  *
826  *      'origin' is only if a new origin was found.
827  *
828  * Returns:
829  *      ISC_R_SUCCESS           The successor was found and 'name' was set.
830  *      DNS_R_NEWORIGIN         The successor was found with a different
831  *                              origin and 'name' and 'origin' were set.
832  *      ISC_R_NOMORE            There was no successor.
833  *      <something_else>        Any error result from dns_name_concatenate.
834  */
835
836 ISC_LANG_ENDDECLS
837
838 #endif /* DNS_RBT_H */