2 * $Id: patricia.h 22407 2006-05-07 15:39:43Z androsyn $
3 * Dave Plonka <plonka@doit.wisc.edu>
5 * This product includes software developed by the University of Michigan,
6 * Merit Network, Inc., and their contributors.
8 * This file had been called "radix.h" in the MRT sources.
10 * I renamed it to "patricia.h" since it's not an implementation of a general
11 * radix trie. Also, pulled in various requirements from "mrt.h" and added
12 * some other things it could be used as a standalone API.
19 #include <sys/types.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
27 #include "irc_string.h"
42 #ifndef INET6_ADDRSTRLEN
43 #define INET6_ADDRSTRLEN 46
46 /* typedef unsigned int uint; */
47 typedef void (*void_fn_t
) ();
48 #define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
50 #define BIT_TEST(f, b) ((f) & (b))
52 #include <netinet/in.h>
53 #include <sys/socket.h>
55 typedef struct _prefix_t
57 u_short family
; /* AF_INET | AF_INET6 */
58 u_short bitlen
; /* same as mask? */
59 int ref_count
; /* reference count */
72 typedef struct _patricia_node_t
74 unsigned int bit
; /* flag if this node used */
75 prefix_t
*prefix
; /* who we are in patricia tree */
76 struct _patricia_node_t
*l
, *r
; /* left and right children */
77 struct _patricia_node_t
*parent
; /* may be used */
82 typedef struct _patricia_tree_t
84 patricia_node_t
*head
;
85 unsigned int maxbits
; /* for IP, 32 bit addresses */
86 int num_active_node
; /* for debug purpose */
91 patricia_node_t
*match_ip(patricia_tree_t
* tree
, struct sockaddr
*ip
);
92 patricia_node_t
*match_string(patricia_tree_t
* tree
, const char *string
);
93 patricia_node_t
*match_exact_string(patricia_tree_t
* tree
, const char *string
);
94 patricia_node_t
*patricia_search_exact(patricia_tree_t
* patricia
, prefix_t
* prefix
);
95 patricia_node_t
*patricia_search_best(patricia_tree_t
* patricia
, prefix_t
* prefix
);
96 patricia_node_t
*patricia_search_best2(patricia_tree_t
* patricia
,
97 prefix_t
* prefix
, int inclusive
);
98 patricia_node_t
*patricia_lookup(patricia_tree_t
* patricia
, prefix_t
* prefix
);
100 void patricia_remove(patricia_tree_t
* patricia
, patricia_node_t
* node
);
101 patricia_tree_t
*New_Patricia(int maxbits
);
102 void Clear_Patricia(patricia_tree_t
* patricia
, void_fn_t func
);
103 void Destroy_Patricia(patricia_tree_t
* patricia
, void_fn_t func
);
104 void patricia_process(patricia_tree_t
* patricia
, void_fn_t func
);
105 void init_patricia(void);
109 prefix_t
*ascii2prefix(int family
, char *string
);
111 patricia_node_t
*make_and_lookup(patricia_tree_t
* tree
, const char *string
);
112 patricia_node_t
*make_and_lookup_ip(patricia_tree_t
* tree
, struct sockaddr
*, int bitlen
);
115 #define PATRICIA_MAXBITS 128
116 #define PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f))
117 #define PATRICIA_NBYTE(x) ((x) >> 3)
119 #define PATRICIA_DATA_GET(node, type) (type *)((node)->data)
120 #define PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value))
122 #define PATRICIA_WALK(Xhead, Xnode) \
124 patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \
125 patricia_node_t **Xsp = Xstack; \
126 patricia_node_t *Xrn = (Xhead); \
127 while ((Xnode = Xrn)) { \
130 #define PATRICIA_WALK_ALL(Xhead, Xnode) \
132 patricia_node_t *Xstack[PATRICIA_MAXBITS+1]; \
133 patricia_node_t **Xsp = Xstack; \
134 patricia_node_t *Xrn = (Xhead); \
135 while ((Xnode = Xrn)) { \
138 #define PATRICIA_WALK_BREAK { \
139 if (Xsp != Xstack) { \
142 Xrn = (patricia_node_t *) 0; \
146 #define PATRICIA_WALK_END \
152 } else if (Xrn->r) { \
154 } else if (Xsp != Xstack) { \
157 Xrn = (patricia_node_t *) 0; \
162 #endif /* _PATRICIA_H */