Debian Codegraph @

include/patricia.h

raw symbols SCIP
1 /*
2 * $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $
3 * Dave Plonka <[email protected]>
4 *
5 * This product includes software developed by the University of Michigan,
6 * Merit Network, Inc., and their contributors.
7 *
8 * This file had been called "radix.h" in the MRT sources.
9 *
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.
13 */
14
15 /*
16 * This code originates from Dave Plonka's Net::Security perl module. An
17 * adaptation of it in C is kept at
18 * https://github.com/CAIDA/cc-common/tree/master/libpatricia. That repository
19 * is considered the upstream version for Zeek's fork. We make some custom
20 * changes to this upstream:
21 * - Replace void_fn_t with data_fn_t and prefix_data_fn_t
22 * - Add patricia_search_all method
23 *
24 * The current version is based on commit
25 * fd262ab5ac5bae8b0d4a8b5e2e723115b1846376 from that repo.
26 */
27
28 /* From copyright.txt:
29 *
30 * Copyright (c) 1997, 1998, 1999
31 *
32 *
33 * The Regents of the University of Michigan ("The Regents") and Merit Network,
34 * Inc. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions are met:
37 * 1. Redistributions of source code must retain the above
38 * copyright notice, this list of conditions and the
39 * following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above
41 * copyright notice, this list of conditions and the
42 * following disclaimer in the documentation and/or other
43 * materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of
45 * this software must display the following acknowledgement:
46 * This product includes software developed by the University of Michigan, Merit
47 * Network, Inc., and their contributors.
48 * 4. Neither the name of the University, Merit Network, nor the
49 * names of their contributors may be used to endorse or
50 * promote products derived from this software without
51 * specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND ANY
53 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
54 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
55 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
56 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
58 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
59 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 */
63
64 #ifndef _PATRICIA_H
65 #define _PATRICIA_H
66
67 #define HAVE_IPV6
68
69 /* typedef unsigned int u_int; */
70 typedef void (*void_fn_t)();
71 /* { from defs.h */
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> /* for u_* definitions (on FreeBSD 5) */
80
81 #include <errno.h> /* for EAFNOSUPPORT */
82 #ifndef EAFNOSUPPORT
83 #defined EAFNOSUPPORT WSAEAFNOSUPPORT
84 #include <winsock.h>
85 #else
86 #include <netinet/in.h> /* for struct in_addr */
87 #endif
88
89 #include <sys/socket.h> /* for AF_INET */
90
91 /* { from mrt.h */
92
93 typedef struct _prefix4_t {
94 u_short family; /* AF_INET | AF_INET6 */
95 u_short bitlen; /* same as mask? */
96 int ref_count; /* reference count */
97 struct in_addr sin;
98 } prefix4_t;
99
100 typedef struct _prefix_t {
101 u_short family; /* AF_INET | AF_INET6 */
102 u_short bitlen; /* same as mask? */
103 int ref_count; /* reference count */
104 union {
105 struct in_addr sin;
106 #ifdef HAVE_IPV6
107 struct in6_addr sin6;
108 #endif /* IPV6 */
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; /* flag if this node used */
119 prefix_t *prefix; /* who we are in patricia tree */
120 struct _patricia_node_t *l, *r; /* left and right children */
121 struct _patricia_node_t *parent; /* may be used */
122 void *data; /* pointer to data */
123 void *user1; /* pointer to usr data (ex. route flap info) */
124 } patricia_node_t;
125
126 typedef struct _patricia_tree_t {
127 patricia_node_t *head;
128 u_int maxbits; /* for IP, 32 bit addresses */
129 int num_active_node; /* for debug purpose */
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 /* { from demo.c */
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 /* _PATRICIA_H */