]> jfr.im git - irc/rqf/shadowircd.git/blame - libratbox/include/rb_patricia.h
Cope with OPENSSL_VERSION_NUMBER not being a long.
[irc/rqf/shadowircd.git] / libratbox / include / rb_patricia.h
CommitLineData
b57f37fb 1/*
b57f37fb
WP
2 * Dave Plonka <plonka@doit.wisc.edu>
3 *
4 * This product includes software developed by the University of Michigan,
5 * Merit Network, Inc., and their contributors.
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
27/* typedef unsigned int u_int; */
b57f37fb
WP
28#define rb_prefix_touchar(prefix) ((unsigned char *)&(prefix)->add.sin)
29#define MAXLINE 1024
30#define BIT_TEST(f, b) ((f) & (b))
31
32typedef struct _rb_prefix_t
33{
94b4fbf9
VY
34 unsigned short family; /* AF_INET | AF_INET6 */
35 unsigned short bitlen; /* same as mask? */
b57f37fb
WP
36 int ref_count; /* reference count */
37 union
38 {
39 struct in_addr sin;
40#ifdef RB_IPV6
41 struct in6_addr sin6;
42#endif /* RB_IPV6 */
43 }
44 add;
45}
46rb_prefix_t;
47
48
49typedef struct _rb_patricia_node_t
50{
94b4fbf9 51 unsigned int bit; /* flag if this node used */
b57f37fb
WP
52 rb_prefix_t *prefix; /* who we are in patricia tree */
53 struct _rb_patricia_node_t *l, *r; /* left and right children */
54 struct _rb_patricia_node_t *parent; /* may be used */
55 void *data;
56}
57rb_patricia_node_t;
58
59typedef struct _rb_patricia_tree_t
60{
61 rb_patricia_node_t *head;
94b4fbf9 62 unsigned int maxbits; /* for IP, 32 bit addresses */
b57f37fb
WP
63 int num_active_node; /* for debug purpose */
64}
65rb_patricia_tree_t;
66
67
94b4fbf9
VY
68rb_patricia_node_t *rb_match_ip(rb_patricia_tree_t *tree, struct sockaddr *ip);
69rb_patricia_node_t *rb_match_ip_exact(rb_patricia_tree_t *tree, struct sockaddr *ip,
70 unsigned int len);
71rb_patricia_node_t *rb_match_string(rb_patricia_tree_t *tree, const char *string);
72rb_patricia_node_t *rb_match_exact_string(rb_patricia_tree_t *tree, const char *string);
73rb_patricia_node_t *rb_patricia_search_exact(rb_patricia_tree_t *patricia, rb_prefix_t *prefix);
74rb_patricia_node_t *rb_patricia_search_best(rb_patricia_tree_t *patricia, rb_prefix_t *prefix);
75rb_patricia_node_t *rb_patricia_search_best2(rb_patricia_tree_t *patricia,
76 rb_prefix_t *prefix, int inclusive);
77rb_patricia_node_t *rb_patricia_lookup(rb_patricia_tree_t *patricia, rb_prefix_t *prefix);
b57f37fb 78
94b4fbf9 79void rb_patricia_remove(rb_patricia_tree_t *patricia, rb_patricia_node_t *node);
b57f37fb 80rb_patricia_tree_t *rb_new_patricia(int maxbits);
94b4fbf9
VY
81void rb_clear_patricia(rb_patricia_tree_t *patricia, void (*func) (void *));
82void rb_destroy_patricia(rb_patricia_tree_t *patricia, void (*func) (void *));
83void rb_patricia_process(rb_patricia_tree_t *patricia, void (*func) (rb_prefix_t *, void *));
b57f37fb
WP
84void rb_init_patricia(void);
85
86
87#if 0
88rb_prefix_t *ascii2prefix(int family, char *string);
89#endif
94b4fbf9
VY
90rb_patricia_node_t *make_and_lookup(rb_patricia_tree_t *tree, const char *string);
91rb_patricia_node_t *make_and_lookup_ip(rb_patricia_tree_t *tree, struct sockaddr *, int bitlen);
b57f37fb
WP
92
93
94#define RB_PATRICIA_MAXBITS 128
95#define RB_PATRICIA_NBIT(x) (0x80 >> ((x) & 0x7f))
96#define RB_PATRICIA_NBYTE(x) ((x) >> 3)
97
98#define RB_PATRICIA_DATA_GET(node, type) (type *)((node)->data)
99#define RB_PATRICIA_DATA_SET(node, value) ((node)->data = (void *)(value))
100
101#define RB_PATRICIA_WALK(Xhead, Xnode) \
102 do { \
103 rb_patricia_node_t *Xstack[RB_PATRICIA_MAXBITS+1]; \
104 rb_patricia_node_t **Xsp = Xstack; \
105 rb_patricia_node_t *Xrn = (Xhead); \
106 while ((Xnode = Xrn)) { \
107 if (Xnode->prefix)
108
109#define RB_PATRICIA_WALK_ALL(Xhead, Xnode) \
110do { \
111 rb_patricia_node_t *Xstack[RB_PATRICIA_MAXBITS+1]; \
112 rb_patricia_node_t **Xsp = Xstack; \
113 rb_patricia_node_t *Xrn = (Xhead); \
114 while ((Xnode = Xrn)) { \
115 if (1)
116
117#define RB_PATRICIA_WALK_BREAK { \
118 if (Xsp != Xstack) { \
119 Xrn = *(--Xsp); \
120 } else { \
121 Xrn = (rb_patricia_node_t *) 0; \
122 } \
123 continue; }
124
125#define RB_PATRICIA_WALK_END \
126 if (Xrn->l) { \
127 if (Xrn->r) { \
128 *Xsp++ = Xrn->r; \
129 } \
130 Xrn = Xrn->l; \
131 } else if (Xrn->r) { \
132 Xrn = Xrn->r; \
133 } else if (Xsp != Xstack) { \
134 Xrn = *(--Xsp); \
135 } else { \
136 Xrn = (rb_patricia_node_t *) 0; \
137 } \
138 } \
139 } while (0)
140
141#endif /* _RB_PATRICIA_H */