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