00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <isc/magic.h>
00025 #include <isc/types.h>
00026 #include <isc/mutex.h>
00027 #include <isc/net.h>
00028 #include <isc/refcount.h>
00029
00030 #include <string.h>
00031
00032 #ifndef _RADIX_H
00033 #define _RADIX_H
00034
00035 #define NETADDR_TO_PREFIX_T(na,pt,bits,is_ecs) \
00036 do { \
00037 const void *p = na; \
00038 memset(&(pt), 0, sizeof(pt)); \
00039 if (p != NULL) { \
00040 (pt).family = (na)->family; \
00041 (pt).bitlen = (bits); \
00042 if ((pt).family == AF_INET6) { \
00043 memmove(&(pt).add.sin6, &(na)->type.in6, \
00044 ((bits)+7)/8); \
00045 } else \
00046 memmove(&(pt).add.sin, &(na)->type.in, \
00047 ((bits)+7)/8); \
00048 } else { \
00049 (pt).family = AF_UNSPEC; \
00050 (pt).bitlen = 0; \
00051 } \
00052 (pt).ecs = is_ecs; \
00053 isc_refcount_init(&(pt).refcount, 0); \
00054 } while(0)
00055
00056 typedef struct isc_prefix {
00057 isc_mem_t *mctx;
00058 unsigned int family;
00059 unsigned int bitlen;
00060 isc_boolean_t ecs;
00061 isc_refcount_t refcount;
00062 union {
00063 struct in_addr sin;
00064 struct in6_addr sin6;
00065 } add;
00066 } isc_prefix_t;
00067
00068 typedef void (*isc_radix_destroyfunc_t)(void *);
00069 typedef void (*isc_radix_processfunc_t)(isc_prefix_t *, void **);
00070
00071 #define isc_prefix_tochar(prefix) ((char *)&(prefix)->add.sin)
00072 #define isc_prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
00073
00074 #define BIT_TEST(f, b) ((f) & (b))
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 #define ISC_RADIX_OFF(p) \
00101 ((((p)->family == AF_INET6) ? 1 : 0) + ((p)->ecs ? 2 : 0))
00102
00103 typedef struct isc_radix_node {
00104 isc_mem_t *mctx;
00105 isc_uint32_t bit;
00106 isc_prefix_t *prefix;
00107 struct isc_radix_node *l, *r;
00108 struct isc_radix_node *parent;
00109 void *data[4];
00110 int node_num[4];
00111
00112 } isc_radix_node_t;
00113
00114 #define RADIX_TREE_MAGIC ISC_MAGIC('R','d','x','T');
00115 #define RADIX_TREE_VALID(a) ISC_MAGIC_VALID(a, RADIX_TREE_MAGIC);
00116
00117 typedef struct isc_radix_tree {
00118 unsigned int magic;
00119 isc_mem_t *mctx;
00120 isc_radix_node_t *head;
00121 isc_uint32_t maxbits;
00122 int num_active_node;
00123 int num_added_node;
00124 } isc_radix_tree_t;
00125
00126 isc_result_t
00127 isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
00128 isc_prefix_t *prefix);
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 isc_result_t
00144 isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
00145 isc_radix_node_t *source, isc_prefix_t *prefix);
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 void
00162 isc_radix_remove(isc_radix_tree_t *radix, isc_radix_node_t *node);
00163
00164
00165
00166
00167
00168
00169
00170
00171 isc_result_t
00172 isc_radix_create(isc_mem_t *mctx, isc_radix_tree_t **target, int maxbits);
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186 void
00187 isc_radix_destroy(isc_radix_tree_t *radix, isc_radix_destroyfunc_t func);
00188
00189
00190
00191
00192
00193
00194
00195 void
00196 isc_radix_process(isc_radix_tree_t *radix, isc_radix_processfunc_t func);
00197
00198
00199
00200
00201
00202
00203
00204
00205 #define RADIX_MAXBITS 128
00206 #define RADIX_NBIT(x) (0x80 >> ((x) & 0x7f))
00207 #define RADIX_NBYTE(x) ((x) >> 3)
00208
00209 #define RADIX_DATA_GET(node, type) (type *)((node)->data)
00210 #define RADIX_DATA_SET(node, value) ((node)->data = (void *)(value))
00211
00212 #define RADIX_WALK(Xhead, Xnode) \
00213 do { \
00214 isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
00215 isc_radix_node_t **Xsp = Xstack; \
00216 isc_radix_node_t *Xrn = (Xhead); \
00217 while ((Xnode = Xrn)) { \
00218 if (Xnode->prefix)
00219
00220 #define RADIX_WALK_ALL(Xhead, Xnode) \
00221 do { \
00222 isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
00223 isc_radix_node_t **Xsp = Xstack; \
00224 isc_radix_node_t *Xrn = (Xhead); \
00225 while ((Xnode = Xrn)) { \
00226 if (1)
00227
00228 #define RADIX_WALK_BREAK { \
00229 if (Xsp != Xstack) { \
00230 Xrn = *(--Xsp); \
00231 } else { \
00232 Xrn = (radix_node_t *) 0; \
00233 } \
00234 continue; }
00235
00236 #define RADIX_WALK_END \
00237 if (Xrn->l) { \
00238 if (Xrn->r) { \
00239 *Xsp++ = Xrn->r; \
00240 } \
00241 Xrn = Xrn->l; \
00242 } else if (Xrn->r) { \
00243 Xrn = Xrn->r; \
00244 } else if (Xsp != Xstack) { \
00245 Xrn = *(--Xsp); \
00246 } else { \
00247 Xrn = (isc_radix_node_t *) 0; \
00248 } \
00249 } \
00250 } while (0)
00251
00252 #endif