]> jfr.im git - irc/quakenet/newserv.git/blame - patricia/patricia.c
move patricia files ready for module
[irc/quakenet/newserv.git] / patricia / patricia.c
CommitLineData
526e7c1d
P
1/*
2 * $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $
3 * Dave Plonka <plonka@doit.wisc.edu>
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.c" in the MRT sources.
9 *
10 * I renamed it to "patricia.c" since it's not an implementation of a general
11 * radix trie. Also I pulled in various requirements from "prefix.c" and
12 * "demo.c" so that it could be used as a standalone API.
13 */
14
6f8dfee9 15char patricia_copyright[] =
526e7c1d
P
16"This product includes software developed by the University of Michigan, Merit"
17"Network, Inc., and their contributors.";
18
526e7c1d
P
19#include <assert.h> /* assert */
20#include <ctype.h> /* isdigit */
21#include <errno.h> /* errno */
22#include <math.h> /* sin */
23#include <stddef.h> /* NULL */
24#include <stdio.h> /* sprintf, fprintf, stderr */
25#include <stdlib.h> /* free, atol, calloc */
26#include <string.h> /* memcpy, strchr, strlen */
27
28#include "patricia.h"
29
526e7c1d
P
30/* prefix_tochar
31 * convert prefix information to bytes
32 */
33u_char *
34prefix_tochar (prefix_t * prefix)
35{
36 if (prefix == NULL)
37 return (NULL);
38
39 return ((u_char *) & prefix->sin);
40}
41
42int
43comp_with_mask (void *addr, void *dest, u_int mask)
44{
45
46 if ( /* mask/8 == 0 || */ memcmp (addr, dest, mask / 8) == 0) {
47 int n = mask / 8;
48 int m = ((-1) << (8 - (mask % 8)));
49
50 if (mask % 8 == 0 || (((u_char *)addr)[n] & m) == (((u_char *)dest)[n] & m))
51 return (1);
52 }
53 return (0);
54}
55
56prefix_t *
57patricia_new_prefix (struct irc_in_addr *dest, int bitlen)
58{
59 prefix_t *prefix = NULL;
60
61 prefix = malloc(sizeof (prefix_t));
62 memcpy (&prefix->sin, dest, 16);
63
64 prefix->bitlen = (bitlen >= 0)? bitlen: 128;
65 prefix->ref_count = 1;
66 return prefix;
67}
68
69prefix_t *
70patricia_ref_prefix (prefix_t * prefix)
71{
72 if (prefix == NULL)
73 return (NULL);
74 if (prefix->ref_count == 0) {
75 /* make a copy in case of a static prefix */
76 return (patricia_new_prefix (&prefix->sin, prefix->bitlen));
77 }
78 prefix->ref_count++;
526e7c1d
P
79 return (prefix);
80}
81
82void
83patricia_deref_prefix (prefix_t * prefix)
84{
85 if (prefix == NULL)
86 return;
87 /* for secure programming, raise an assert. no static prefix can call this */
88 assert (prefix->ref_count > 0);
89
90 prefix->ref_count--;
526e7c1d
P
91 if (prefix->ref_count <= 0) {
92 free(prefix);
93 return;
94 }
95}
96
526e7c1d
P
97/* these routines support continuous mask only */
98
99patricia_tree_t *
100patricia_new_tree (int maxbits)
101{
102 patricia_tree_t *patricia = calloc(1, sizeof *patricia);
103
104 patricia->maxbits = maxbits;
105 patricia->head = NULL;
106 patricia->num_active_node = 0;
107 assert (maxbits <= PATRICIA_MAXBITS); /* XXX */
108 return (patricia);
109}
110
111
112void
113patricia_clear_tree (patricia_tree_t *patricia, void_fn_t func)
114{
115 assert (patricia);
116 if (patricia->head) {
117
118 patricia_node_t *Xstack[PATRICIA_MAXBITS+1];
119 patricia_node_t **Xsp = Xstack;
120 patricia_node_t *Xrn = patricia->head;
121
122 while (Xrn) {
123 patricia_node_t *l = Xrn->l;
124 patricia_node_t *r = Xrn->r;
125
126 if (Xrn->prefix) {
a8ba1373
P
127 if (Xrn->exts && func)
128 func(Xrn->exts);
526e7c1d
P
129 patricia_deref_prefix (Xrn->prefix);
130 }
131 free(Xrn);
132 patricia->num_active_node--;
133
134 if (l) {
135 if (r) {
136 *Xsp++ = r;
137 }
138 Xrn = l;
139 } else if (r) {
140 Xrn = r;
141 } else if (Xsp != Xstack) {
142 Xrn = *(--Xsp);
143 } else {
144 Xrn = (patricia_node_t *) 0;
145 }
146 }
147 }
148 assert (patricia->num_active_node == 0);
149 /* free(patricia); */
150}
151
526e7c1d
P
152void
153patricia_destroy_tree (patricia_tree_t *patricia, void_fn_t func)
154{
155 patricia_clear_tree (patricia, func);
156 free(patricia);
157}
158
159
160/*
161 * if func is supplied, it will be called as func(node->prefix)
162 */
163
164void
165patricia_process (patricia_tree_t *patricia, void_fn_t func)
166{
167 patricia_node_t *node;
168 assert (func);
169
170 PATRICIA_WALK (patricia->head, node) {
171 func (node->prefix);
172 } PATRICIA_WALK_END;
173}
174
175size_t
176patricia_walk_inorder(patricia_node_t *node, void_fn_t func)
177{
178 size_t n = 0;
179 assert(func);
180
181 if (node->l) {
182 n += patricia_walk_inorder(node->l, func);
183 }
184
185 if (node->prefix) {
186 func(node->prefix);
187 n++;
188 }
189
190 if (node->r) {
191 n += patricia_walk_inorder(node->r, func);
192 }
193
194 return n;
195}
196
197
198patricia_node_t *
199patricia_search_exact (patricia_tree_t *patricia, struct irc_in_addr *sin, unsigned char bitlen)
200{
201 patricia_node_t *node;
202 u_char *addr;
203
204 assert (patricia);
205 assert (sin);
206 assert (bitlen <= patricia->maxbits);
207
208 if (patricia->head == NULL)
209 return (NULL);
210
211 node = patricia->head;
212 addr = (u_char *)sin;
213
214 while (node->bit < bitlen) {
215 if (BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
216 node = node->r;
217 else
218 node = node->l;
219
220 if (node == NULL)
221 return (NULL);
222 }
223
224 if (node->bit > bitlen || node->prefix == NULL)
225 return (NULL);
226 assert (node->bit == bitlen);
227 assert (node->bit == node->prefix->bitlen);
228 if (comp_with_mask (prefix_tochar (node->prefix), addr, bitlen)) {
229 return (node);
230 }
231 return (NULL);
232}
233
234
235/* if inclusive != 0, "best" may be the given prefix itself */
236patricia_node_t *
237patricia_search_best2 (patricia_tree_t *patricia, struct irc_in_addr *sin, unsigned char bitlen, int inclusive)
238{
239 patricia_node_t *node;
240 patricia_node_t *stack[PATRICIA_MAXBITS + 1];
241 u_char *addr;
242 int cnt = 0;
243
244 assert (patricia);
245 assert (sin);
246 assert (bitlen <= patricia->maxbits);
247
248 if (patricia->head == NULL)
249 return (NULL);
250
251 node = patricia->head;
252 addr = (u_char *)sin;
253
254 while (node->bit < bitlen) {
255
256 if (node->prefix) {
257 stack[cnt++] = node;
258 }
259
260 if (BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) {
261 node = node->r;
262 }
263 else {
264 node = node->l;
265 }
266
267 if (node == NULL)
268 break;
269 }
270
271 if (inclusive && node && node->prefix)
272 stack[cnt++] = node;
273
274 if (cnt <= 0)
275 return (NULL);
276
277 while (--cnt >= 0) {
278 node = stack[cnt];
279 if (comp_with_mask (prefix_tochar (node->prefix),
280 addr,
281 node->prefix->bitlen)) {
282 return (node);
283 }
284 }
285 return (NULL);
286}
287
288
289patricia_node_t *
290patricia_search_best (patricia_tree_t *patricia, struct irc_in_addr *sin, unsigned char bitlen)
291{
292 return (patricia_search_best2 (patricia, sin, bitlen, 1));
293}
294
295
296patricia_node_t *
297patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
298{
299 patricia_node_t *node, *new_node, *parent, *glue;
300 u_char *addr, *test_addr;
301 u_int bitlen, check_bit, differ_bit;
302 int i, j, r;
303
304 assert (patricia);
305 assert (prefix);
306 assert (prefix->bitlen <= patricia->maxbits);
307
308 /* if new trie, create the first node */
309 if (patricia->head == NULL) {
310 node = malloc(sizeof *node);
a8ba1373 311 memset(node->exts, 0, PATRICIA_MAXSLOTS * sizeof(void *));
526e7c1d
P
312 node->bit = prefix->bitlen;
313 node->prefix = patricia_ref_prefix (prefix);
314 node->parent = NULL;
315 node->l = node->r = NULL;
24d73e9c 316 node->usercount = 0;
526e7c1d
P
317 patricia->head = node;
318 patricia->num_active_node++;
319 return (node);
320 }
321
322 addr = prefix_touchar (prefix);
323 bitlen = prefix->bitlen;
324 node = patricia->head;
325
326 /* while ( bitlength of tree node < bitlength of node we're searching for || the node has no prefix */
327 while (node->bit < bitlen || node->prefix == NULL) {
328 /* check that we're not at the lowest leaf i.e. node->bit is less than max bits */
329 if (node->bit < patricia->maxbits &&
330 (addr[node->bit >> 3]) & (0x80 >> (node->bit & 0x07))) {
331 if (node->r == NULL)
332 break;
333 node = node->r;
334 }
335 else {
336 if (node->l == NULL)
337 break;
338 node = node->l;
339 }
340
341 assert (node);
342 }
343
344 assert (node->prefix);
345
346 test_addr = prefix_touchar (node->prefix);
347 /* find the first bit different */
348 check_bit = (node->bit < bitlen)? node->bit: bitlen;
349 differ_bit = 0;
350 for (i = 0; i*8 < check_bit; i++) {
351 if ((r = (addr[i] ^ test_addr[i])) == 0) {
352 differ_bit = (i + 1) * 8;
353 continue;
354 }
355 /* I know the better way, but for now */
356 for (j = 0; j < 8; j++) {
357 if ((r) & ((0x80 >> j)))
358 break;
359 }
360 /* must be found */
361 assert (j < 8);
362 differ_bit = i * 8 + j;
363 break;
364 }
365 if (differ_bit > check_bit)
366 differ_bit = check_bit;
367
368
369 parent = node->parent;
370 while (parent && parent->bit >= differ_bit) {
371 node = parent;
372 parent = node->parent;
373 }
374
375 if (differ_bit == bitlen && node->bit == bitlen) {
376 if (node->prefix) {
377 return (node);
378 }
379 node->prefix = patricia_ref_prefix (prefix);
380 return (node);
381 }
382
383 new_node = malloc(sizeof *new_node);
a8ba1373 384 memset(new_node->exts, 0, PATRICIA_MAXSLOTS * sizeof(void *));
526e7c1d
P
385 new_node->bit = prefix->bitlen;
386 new_node->prefix = patricia_ref_prefix (prefix);
387 new_node->parent = NULL;
388 new_node->l = new_node->r = NULL;
24d73e9c 389 new_node->usercount = 0;
526e7c1d
P
390 patricia->num_active_node++;
391
392 if (node->bit == differ_bit) {
393 new_node->parent = node;
394 if (node->bit < patricia->maxbits &&
395 (addr[node->bit >> 3]) & (0x80 >> (node->bit & 0x07))) {
396 assert (node->r == NULL);
397 node->r = new_node;
398 }
399 else {
400 assert (node->l == NULL);
401 node->l = new_node;
402 }
403 return (new_node);
404 }
405
406 if (bitlen == differ_bit) {
407 if (bitlen < patricia->maxbits &&
408 (test_addr[bitlen >> 3]) & (0x80 >> (bitlen & 0x07))) {
409 new_node->r = node;
410 }
411 else {
412 new_node->l = node;
413 }
414 new_node->parent = node->parent;
415 if (node->parent == NULL) {
416 assert (patricia->head == node);
417 patricia->head = new_node;
418 }
419 else if (node->parent->r == node) {
420 node->parent->r = new_node;
421 }
422 else {
423 node->parent->l = new_node;
424 }
425 node->parent = new_node;
426 }
427 else {
428 glue = malloc(sizeof *glue);
a8ba1373 429 memset(glue->exts, 0, PATRICIA_MAXSLOTS * sizeof(void *));
526e7c1d
P
430 glue->bit = differ_bit;
431 glue->prefix = NULL;
432 glue->parent = node->parent;
24d73e9c 433 glue->usercount = 0;
526e7c1d
P
434 patricia->num_active_node++;
435 if (differ_bit < patricia->maxbits &&
436 (addr[differ_bit >> 3]) & (0x80 >> (differ_bit & 0x07))) {
437 glue->r = new_node;
438 glue->l = node;
439 }
440 else {
441 glue->r = node;
442 glue->l = new_node;
443 }
444 new_node->parent = glue;
445
446 if (node->parent == NULL) {
447 assert (patricia->head == node);
448 patricia->head = glue;
449 }
450 else if (node->parent->r == node) {
451 node->parent->r = glue;
452 }
453 else {
454 node->parent->l = glue;
455 }
456 node->parent = glue;
457 }
458 return (new_node);
459}
460
461void
462patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
463{
464 patricia_node_t *parent, *child;
465
466 assert (patricia);
467 assert (node);
468
469 if (node->r && node->l) {
470 /* this might be a placeholder node -- have to check and make sure
471 * there is a prefix aossciated with it ! */
472 if (node->prefix != NULL)
473 patricia_deref_prefix (node->prefix);
474 node->prefix = NULL;
475 return;
476 }
477
478 if (node->r == NULL && node->l == NULL) {
479 parent = node->parent;
480 patricia_deref_prefix (node->prefix);
481 free(node);
482 patricia->num_active_node--;
483
484 if (parent == NULL) {
485 assert (patricia->head == node);
486 patricia->head = NULL;
487 return;
488 }
489
490 if (parent->r == node) {
491 parent->r = NULL;
492 child = parent->l;
493 }
494 else {
495 assert (parent->l == node);
496 parent->l = NULL;
497 child = parent->r;
498 }
499
500 if (parent->prefix)
501 return;
502
503 /* we need to remove parent too */
504
505 if (parent->parent == NULL) {
506 assert (patricia->head == parent);
507 patricia->head = child;
508 }
509 else if (parent->parent->r == parent) {
510 parent->parent->r = child;
511 }
512 else {
513 assert (parent->parent->l == parent);
514 parent->parent->l = child;
515 }
516 child->parent = parent->parent;
517 free(parent);
518 patricia->num_active_node--;
519 return;
520 }
521
522 if (node->r) {
523 child = node->r;
524 }
525 else {
526 assert (node->l);
527 child = node->l;
528 }
529 parent = node->parent;
530 child->parent = parent;
531
532 patricia_deref_prefix (node->prefix);
533 free(node);
534 patricia->num_active_node--;
535
536 if (parent == NULL) {
537 assert (patricia->head == node);
538 patricia->head = child;
539 return;
540 }
541
542 if (parent->r == node) {
543 parent->r = child;
544 }
545 else {
546 assert (parent->l == node);
547 parent->l = child;
548 }
549}
550
526e7c1d
P
551
552patricia_node_t *
553refnode(patricia_tree_t *tree, struct irc_in_addr *sin, int bitlen) {
554 patricia_node_t *node;
555 prefix_t *prefix;
556
01a924c2 557 node = patricia_search_exact(tree, sin, bitlen);
526e7c1d
P
558
559 if (node == NULL) {
560 prefix = patricia_new_prefix(sin, bitlen);
561 node = patricia_lookup(tree, prefix);
526e7c1d
P
562 patricia_deref_prefix(prefix);
563 } else if (node->prefix) {
564 patricia_ref_prefix(node->prefix);
565 }
566
567 return node;
568}
569
570void
571derefnode(patricia_tree_t *tree, patricia_node_t *node) {
572 if (!node || !node->prefix)
573 return;
574
575 if (node->prefix->ref_count == 1) {
526e7c1d
P
576 patricia_remove(tree, node);
577 } else
578 patricia_deref_prefix(node->prefix);
579}