2 * Copyright (C) 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: radix.h,v 1.13 2008/12/01 23:47:45 tbox Exp $ */
20 * This source was adapted from MRT's RCS Ids:
21 * Id: radix.h,v 1.6 1999/08/03 03:32:53 masaki Exp
22 * Id: mrt.h,v 1.57.2.6 1999/12/28 23:41:27 labovit Exp
23 * Id: defs.h,v 1.5.2.2 2000/01/15 14:19:16 masaki Exp
26 #include <isc/magic.h>
27 #include <isc/types.h>
28 #include <isc/mutex.h>
30 #include <isc/refcount.h>
37 #define NETADDR_TO_PREFIX_T(na,pt,bits) \
39 memset(&(pt), 0, sizeof(pt)); \
41 (pt).family = (na)->family; \
42 (pt).bitlen = (bits); \
43 if ((pt).family == AF_INET6) { \
44 memcpy(&(pt).add.sin6, &(na)->type.in6, \
47 memcpy(&(pt).add.sin, &(na)->type.in, \
50 (pt).family = AF_UNSPEC; \
53 isc_refcount_init(&(pt).refcount, 0); \
56 typedef struct isc_prefix {
57 unsigned int family; /* AF_INET | AF_INET6, or AF_UNSPEC for "any" */
58 unsigned int bitlen; /* 0 for "any" */
59 isc_refcount_t refcount;
66 typedef void (*isc_radix_destroyfunc_t)(void *);
67 typedef void (*isc_radix_processfunc_t)(isc_prefix_t *, void **);
69 #define isc_prefix_tochar(prefix) ((char *)&(prefix)->add.sin)
70 #define isc_prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
72 #define BIT_TEST(f, b) ((f) & (b))
75 * We need "first match" when we search the radix tree to preserve
76 * compatibility with the existing ACL implementation. Radix trees
77 * naturally lend themselves to "best match". In order to get "first match"
78 * behavior, we keep track of the order in which entries are added to the
79 * tree--and when a search is made, we find all matching entries, and
80 * return the one that was added first.
82 * An IPv4 prefix and an IPv6 prefix may share a radix tree node if they
83 * have the same length and bit pattern (e.g., 127/8 and 7f::/8). To
84 * disambiguate between them, node_num and data are two-element arrays;
85 * node_num[0] and data[0] are used for IPv4 addresses, node_num[1]
86 * and data[1] for IPv6 addresses. The only exception is a prefix of
87 * 0/0 (aka "any" or "none"), which is always stored as IPv4 but matches
91 #define ISC_IS6(family) ((family) == AF_INET6 ? 1 : 0)
92 typedef struct isc_radix_node {
93 isc_uint32_t bit; /* bit length of the prefix */
94 isc_prefix_t *prefix; /* who we are in radix tree */
95 struct isc_radix_node *l, *r; /* left and right children */
96 struct isc_radix_node *parent; /* may be used */
97 void *data[2]; /* pointers to IPv4 and IPV6 data */
98 int node_num[2]; /* which node this was in the tree,
99 or -1 for glue nodes */
102 #define RADIX_TREE_MAGIC ISC_MAGIC('R','d','x','T');
103 #define RADIX_TREE_VALID(a) ISC_MAGIC_VALID(a, RADIX_TREE_MAGIC);
105 typedef struct isc_radix_tree {
108 isc_radix_node_t *head;
109 isc_uint32_t maxbits; /* for IP, 32 bit addresses */
110 int num_active_node; /* for debugging purposes */
111 int num_added_node; /* total number of nodes */
115 isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
116 isc_prefix_t *prefix);
118 * Search 'radix' for the best match to 'prefix'.
119 * Return the node found in '*target'.
122 * \li 'radix' to be valid.
123 * \li 'target' is not NULL and "*target" is NULL.
124 * \li 'prefix' to be valid.
132 isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
133 isc_radix_node_t *source, isc_prefix_t *prefix);
135 * Insert 'source' or 'prefix' into the radix tree 'radix'.
136 * Return the node added in 'target'.
139 * \li 'radix' to be valid.
140 * \li 'target' is not NULL and "*target" is NULL.
141 * \li 'prefix' to be valid or 'source' to be non NULL and contain
150 isc_radix_remove(isc_radix_tree_t *radix, isc_radix_node_t *node);
152 * Remove the node 'node' from the radix tree 'radix'.
155 * \li 'radix' to be valid.
156 * \li 'node' to be valid.
160 isc_radix_create(isc_mem_t *mctx, isc_radix_tree_t **target, int maxbits);
162 * Create a radix tree with a maximum depth of 'maxbits';
165 * \li 'mctx' to be valid.
166 * \li 'target' to be non NULL and '*target' to be NULL.
167 * \li 'maxbits' to be less than or equal to RADIX_MAXBITS.
175 isc_radix_destroy(isc_radix_tree_t *radix, isc_radix_destroyfunc_t func);
177 * Destroy a radix tree optionally calling 'func' to clean up node data.
180 * \li 'radix' to be valid.
184 isc_radix_process(isc_radix_tree_t *radix, isc_radix_processfunc_t func);
186 * Walk a radix tree calling 'func' to process node data.
189 * \li 'radix' to be valid.
190 * \li 'func' to point to a function.
193 #define RADIX_MAXBITS 128
194 #define RADIX_NBIT(x) (0x80 >> ((x) & 0x7f))
195 #define RADIX_NBYTE(x) ((x) >> 3)
197 #define RADIX_DATA_GET(node, type) (type *)((node)->data)
198 #define RADIX_DATA_SET(node, value) ((node)->data = (void *)(value))
200 #define RADIX_WALK(Xhead, Xnode) \
202 isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
203 isc_radix_node_t **Xsp = Xstack; \
204 isc_radix_node_t *Xrn = (Xhead); \
205 while ((Xnode = Xrn)) { \
208 #define RADIX_WALK_ALL(Xhead, Xnode) \
210 isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
211 isc_radix_node_t **Xsp = Xstack; \
212 isc_radix_node_t *Xrn = (Xhead); \
213 while ((Xnode = Xrn)) { \
216 #define RADIX_WALK_BREAK { \
217 if (Xsp != Xstack) { \
220 Xrn = (radix_node_t *) 0; \
224 #define RADIX_WALK_END \
230 } else if (Xrn->r) { \
232 } else if (Xsp != Xstack) { \
235 Xrn = (isc_radix_node_t *) 0; \
240 #endif /* _RADIX_H */