]>
Commit | Line | Data |
---|---|---|
db137867 | 1 | /* |
db137867 AC |
2 | * Dave Plonka <plonka@doit.wisc.edu> |
3 | * | |
4 | * This product includes software developed by the University of Michigan, | |
55abcbb2 | 5 | * Merit Network, Inc., and their contributors. |
db137867 AC |
6 | * |
7 | * This file had been called "radix.h" in the MRT sources. | |
8 | * | |
9 | * I renamed it to "patricia.h" since it's not an implementation of a general | |
10 | * radix trie. Also, pulled in various requirements from "mrt.h" and added | |
11 | * some other things it could be used as a standalone API. | |
12 | */ | |
13 | ||
14 | #ifndef _RB_PATRICIA_H | |
15 | #define _RB_PATRICIA_H | |
16 | ||
17 | #ifndef FALSE | |
18 | #define FALSE 0 | |
19 | #endif | |
20 | #ifndef TRUE | |
21 | #define TRUE !(FALSE) | |
22 | #endif | |
23 | #ifndef INET6_ADDRSTRLEN | |
24 | #define INET6_ADDRSTRLEN 46 | |
25 | #endif | |
26 | ||
db137867 AC |
27 | #define rb_prefix_touchar(prefix) ((unsigned char *)&(prefix)->add.sin) |
28 | #define MAXLINE 1024 | |
29 | #define BIT_TEST(f, b) ((f) & (b)) | |
30 | ||
31 | typedef struct _rb_prefix_t | |
32 | { | |
3202e249 VY |
33 | unsigned short family; /* AF_INET | AF_INET6 */ |
34 | unsigned short bitlen; /* same as mask? */ | |
db137867 AC |
35 | int ref_count; /* reference count */ |
36 | union | |
37 | { | |
38 | struct in_addr sin; | |
db137867 | 39 | struct in6_addr sin6; |
db137867 AC |
40 | } |
41 | add; | |
42 | } | |
43 | rb_prefix_t; | |
44 | ||
45 | ||
46 | typedef struct _rb_patricia_node_t | |
47 | { | |
3202e249 | 48 | unsigned int bit; /* flag if this node used */ |
db137867 AC |
49 | rb_prefix_t *prefix; /* who we are in patricia tree */ |
50 | struct _rb_patricia_node_t *l, *r; /* left and right children */ | |
51 | struct _rb_patricia_node_t *parent; /* may be used */ | |
52 | void *data; | |
53 | } | |
54 | rb_patricia_node_t; | |
55 | ||
56 | typedef struct _rb_patricia_tree_t | |
57 | { | |
58 | rb_patricia_node_t *head; | |
3202e249 | 59 | unsigned int maxbits; /* for IP, 32 bit addresses */ |
db137867 AC |
60 | int num_active_node; /* for debug purpose */ |
61 | } | |
62 | rb_patricia_tree_t; | |
63 | ||
64 | ||
3202e249 VY |
65 | rb_patricia_node_t *rb_match_ip(rb_patricia_tree_t *tree, struct sockaddr *ip); |
66 | rb_patricia_node_t *rb_match_ip_exact(rb_patricia_tree_t *tree, struct sockaddr *ip, | |
67 | unsigned int len); | |
68 | rb_patricia_node_t *rb_match_string(rb_patricia_tree_t *tree, const char *string); | |
69 | rb_patricia_node_t *rb_match_exact_string(rb_patricia_tree_t *tree, const char *string); | |
70 | rb_patricia_node_t *rb_patricia_search_exact(rb_patricia_tree_t *patricia, rb_prefix_t *prefix); | |
71 | rb_patricia_node_t *rb_patricia_search_best(rb_patricia_tree_t *patricia, rb_prefix_t *prefix); | |
72 | rb_patricia_node_t *rb_patricia_search_best2(rb_patricia_tree_t *patricia, | |
73 | rb_prefix_t *prefix, int inclusive); | |
74 | rb_patricia_node_t *rb_patricia_lookup(rb_patricia_tree_t *patricia, rb_prefix_t *prefix); | |
db137867 | 75 | |
3202e249 | 76 | void rb_patricia_remove(rb_patricia_tree_t *patricia, rb_patricia_node_t *node); |
db137867 | 77 | rb_patricia_tree_t *rb_new_patricia(int maxbits); |
3202e249 VY |
78 | void rb_clear_patricia(rb_patricia_tree_t *patricia, void (*func) (void *)); |
79 | void rb_destroy_patricia(rb_patricia_tree_t *patricia, void (*func) (void *)); | |
80 | void rb_patricia_process(rb_patricia_tree_t *patricia, void (*func) (rb_prefix_t *, void *)); | |
db137867 AC |
81 | void rb_init_patricia(void); |
82 | ||
83 | ||
84 | #if 0 | |
85 | rb_prefix_t *ascii2prefix(int family, char *string); | |
86 | #endif | |
3202e249 VY |
87 | rb_patricia_node_t *make_and_lookup(rb_patricia_tree_t *tree, const char *string); |
88 | rb_patricia_node_t *make_and_lookup_ip(rb_patricia_tree_t *tree, struct sockaddr *, int bitlen); | |
db137867 AC |
89 | |
90 | ||
91 | #define RB_PATRICIA_MAXBITS 128 | |
92 | #define RB_PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f)) | |
93 | #define RB_PATRICIA_NBYTE(x) ((x) >> 3) | |
94 | ||
95 | #define RB_PATRICIA_DATA_GET(node, type) (type *)((node)->data) | |
96 | #define RB_PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value)) | |
97 | ||
98 | #define RB_PATRICIA_WALK(Xhead, Xnode) \ | |
99 | do { \ | |
100 | rb_patricia_node_t *Xstack[RB_PATRICIA_MAXBITS+1]; \ | |
101 | rb_patricia_node_t **Xsp = Xstack; \ | |
102 | rb_patricia_node_t *Xrn = (Xhead); \ | |
103 | while ((Xnode = Xrn)) { \ | |
104 | if (Xnode->prefix) | |
105 | ||
106 | #define RB_PATRICIA_WALK_ALL(Xhead, Xnode) \ | |
107 | do { \ | |
108 | rb_patricia_node_t *Xstack[RB_PATRICIA_MAXBITS+1]; \ | |
109 | rb_patricia_node_t **Xsp = Xstack; \ | |
110 | rb_patricia_node_t *Xrn = (Xhead); \ | |
111 | while ((Xnode = Xrn)) { \ | |
112 | if (1) | |
113 | ||
114 | #define RB_PATRICIA_WALK_BREAK { \ | |
115 | if (Xsp != Xstack) { \ | |
116 | Xrn = *(--Xsp); \ | |
117 | } else { \ | |
118 | Xrn = (rb_patricia_node_t *) 0; \ | |
119 | } \ | |
120 | continue; } | |
121 | ||
122 | #define RB_PATRICIA_WALK_END \ | |
123 | if (Xrn->l) { \ | |
124 | if (Xrn->r) { \ | |
125 | *Xsp++ = Xrn->r; \ | |
126 | } \ | |
127 | Xrn = Xrn->l; \ | |
128 | } else if (Xrn->r) { \ | |
129 | Xrn = Xrn->r; \ | |
130 | } else if (Xsp != Xstack) { \ | |
131 | Xrn = *(--Xsp); \ | |
132 | } else { \ | |
133 | Xrn = (rb_patricia_node_t *) 0; \ | |
134 | } \ | |
135 | } \ | |
136 | } while (0) | |
137 | ||
138 | #endif /* _RB_PATRICIA_H */ |