| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
#ifndef _PATRICIA_H |
| 65 |
#define _PATRICIA_H |
| 66 |
|
| 67 |
#define HAVE_IPV6 |
| 68 |
|
| 69 |
|
| 70 |
typedef void (*void_fn_t)(); |
| 71 |
|
| 72 |
#define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin) |
| 73 |
#define MAXLINE 1024 |
| 74 |
#define BIT_TEST(f, b) ((f) & (b)) |
| 75 |
|
| 76 |
|
| 77 |
#define addroute make_and_lookup |
| 78 |
|
| 79 |
#include <sys/types.h> |
| 80 |
|
| 81 |
#include <errno.h> |
| 82 |
#ifndef EAFNOSUPPORT |
| 83 |
#defined EAFNOSUPPORT WSAEAFNOSUPPORT |
| 84 |
#include <winsock.h> |
| 85 |
#else |
| 86 |
#include <netinet/in.h> |
| 87 |
#endif |
| 88 |
|
| 89 |
#include <sys/socket.h> |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
typedef struct _prefix4_t { |
| 94 |
u_short family; |
| 95 |
u_short bitlen; |
| 96 |
int ref_count; |
| 97 |
struct in_addr sin; |
| 98 |
} prefix4_t; |
| 99 |
|
| 100 |
typedef struct _prefix_t { |
| 101 |
u_short family; |
| 102 |
u_short bitlen; |
| 103 |
int ref_count; |
| 104 |
union { |
| 105 |
struct in_addr sin; |
| 106 |
#ifdef HAVE_IPV6 |
| 107 |
struct in6_addr sin6; |
| 108 |
#endif |
| 109 |
} add; |
| 110 |
} prefix_t; |
| 111 |
|
| 112 |
typedef void (*data_fn_t)(void *); |
| 113 |
typedef void (*prefix_data_fn_t)(prefix_t *, void *); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
typedef struct _patricia_node_t { |
| 118 |
u_int bit; |
| 119 |
prefix_t *prefix; |
| 120 |
struct _patricia_node_t *l, *r; |
| 121 |
struct _patricia_node_t *parent; |
| 122 |
void *data; |
| 123 |
void *user1; |
| 124 |
} patricia_node_t; |
| 125 |
|
| 126 |
typedef struct _patricia_tree_t { |
| 127 |
patricia_node_t *head; |
| 128 |
u_int maxbits; |
| 129 |
int num_active_node; |
| 130 |
} patricia_tree_t; |
| 131 |
|
| 132 |
patricia_node_t *patricia_search_exact(patricia_tree_t *patricia, |
| 133 |
prefix_t *prefix); |
| 134 |
int patricia_search_all(patricia_tree_t *patricia, prefix_t *prefix, |
| 135 |
patricia_node_t ***list, int *n); |
| 136 |
patricia_node_t *patricia_search_best(patricia_tree_t *patricia, |
| 137 |
prefix_t *prefix); |
| 138 |
patricia_node_t *patricia_search_best2(patricia_tree_t *patricia, |
| 139 |
prefix_t *prefix, int inclusive); |
| 140 |
patricia_node_t *patricia_lookup(patricia_tree_t *patricia, prefix_t *prefix); |
| 141 |
void patricia_remove(patricia_tree_t *patricia, patricia_node_t *node); |
| 142 |
patricia_tree_t *New_Patricia(int maxbits); |
| 143 |
void Clear_Patricia(patricia_tree_t *patricia, data_fn_t func); |
| 144 |
void Destroy_Patricia(patricia_tree_t *patricia, data_fn_t func); |
| 145 |
|
| 146 |
void patricia_process(patricia_tree_t *patricia, prefix_data_fn_t func); |
| 147 |
|
| 148 |
void Deref_Prefix(prefix_t *prefix); |
| 149 |
char *prefix_toa(prefix_t *prefix); |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
prefix_t *ascii2prefix(int family, char *string); |
| 154 |
|
| 155 |
patricia_node_t *make_and_lookup(patricia_tree_t *tree, char *string); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#define PATRICIA_MAXBITS (sizeof(struct in6_addr) * 8) |
| 160 |
#define PATRICIA_NBIT(x) (0x80 >> ((x)&0x7f)) |
| 161 |
#define PATRICIA_NBYTE(x) ((x) >> 3) |
| 162 |
|
| 163 |
#define PATRICIA_DATA_GET(node, type) (type *)((node)->data) |
| 164 |
#define PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value)) |
| 165 |
|
| 166 |
#define PATRICIA_WALK(Xhead, Xnode) \ |
| 167 |
do { \ |
| 168 |
patricia_node_t *Xstack[PATRICIA_MAXBITS + 1]; \ |
| 169 |
patricia_node_t **Xsp = Xstack; \ |
| 170 |
patricia_node_t *Xrn = (Xhead); \ |
| 171 |
while ((Xnode = Xrn)) { \ |
| 172 |
if (Xnode->prefix) |
| 173 |
|
| 174 |
#define PATRICIA_WALK_ALL(Xhead, Xnode) \ |
| 175 |
do { \ |
| 176 |
patricia_node_t *Xstack[PATRICIA_MAXBITS + 1]; \ |
| 177 |
patricia_node_t **Xsp = Xstack; \ |
| 178 |
patricia_node_t *Xrn = (Xhead); \ |
| 179 |
while ((Xnode = Xrn)) { \ |
| 180 |
if (1) |
| 181 |
|
| 182 |
#define PATRICIA_WALK_BREAK \ |
| 183 |
{ \ |
| 184 |
if (Xsp != Xstack) { \ |
| 185 |
Xrn = *(--Xsp); \ |
| 186 |
} else { \ |
| 187 |
Xrn = (patricia_node_t *)0; \ |
| 188 |
} \ |
| 189 |
continue; \ |
| 190 |
} |
| 191 |
|
| 192 |
#define PATRICIA_WALK_END \ |
| 193 |
if (Xrn->l) { \ |
| 194 |
if (Xrn->r) { \ |
| 195 |
*Xsp++ = Xrn->r; \ |
| 196 |
} \ |
| 197 |
Xrn = Xrn->l; \ |
| 198 |
} else if (Xrn->r) { \ |
| 199 |
Xrn = Xrn->r; \ |
| 200 |
} else if (Xsp != Xstack) { \ |
| 201 |
Xrn = *(--Xsp); \ |
| 202 |
} else { \ |
| 203 |
Xrn = (patricia_node_t *)0; \ |
| 204 |
} \ |
| 205 |
} \ |
| 206 |
} \ |
| 207 |
while (0) |
| 208 |
|
| 209 |
#endif |