]>
jfr.im git - irc/quakenet/newserv.git/blob - patricia/patricialib.c
2 * $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $
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.c" in the MRT sources.
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.
15 char patricia_copyright
[] =
16 "This product includes software developed by the University of Michigan, Merit"
17 "Network, Inc., and their contributors.";
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 */
31 * convert prefix information to bytes
34 prefix_tochar (prefix_t
* prefix
)
39 return ((u_char
*) & prefix
->sin
);
43 comp_with_mask (void *addr
, void *dest
, u_int mask
)
46 if ( /* mask/8 == 0 || */ memcmp (addr
, dest
, mask
/ 8) == 0) {
48 int m
= ((-1) << (8 - (mask
% 8)));
50 if (mask
% 8 == 0 || (((u_char
*)addr
)[n
] & m
) == (((u_char
*)dest
)[n
] & m
))
58 patricia_new_prefix (struct irc_in_addr
*dest
, int bitlen
)
60 prefix_t
*prefix
= newprefix();
63 memcpy (&prefix
->sin
, dest
, 16);
64 prefix
->bitlen
= (bitlen
>= 0)? bitlen
: 128;
65 prefix
->ref_count
= 1;
71 patricia_ref_prefix (prefix_t
* prefix
)
75 if (prefix
->ref_count
== 0) {
76 /* make a copy in case of a static prefix */
77 return (patricia_new_prefix (&prefix
->sin
, prefix
->bitlen
));
84 patricia_deref_prefix (prefix_t
* prefix
)
88 /* for secure programming, raise an assert. no static prefix can call this */
89 assert (prefix
->ref_count
> 0);
92 if (prefix
->ref_count
<= 0) {
98 /* these routines support continuous mask only */
101 patricia_new_tree (int maxbits
)
103 patricia_tree_t
*patricia
= calloc(1, sizeof *patricia
);
105 patricia
->maxbits
= maxbits
;
106 patricia
->head
= NULL
;
107 patricia
->num_active_node
= 0;
108 assert (maxbits
<= PATRICIA_MAXBITS
); /* XXX */
114 patricia_clear_tree (patricia_tree_t
*patricia
, void_fn_t func
)
117 if (patricia
->head
) {
119 patricia_node_t
*Xstack
[PATRICIA_MAXBITS
+1];
120 patricia_node_t
**Xsp
= Xstack
;
121 patricia_node_t
*Xrn
= patricia
->head
;
124 patricia_node_t
*l
= Xrn
->l
;
125 patricia_node_t
*r
= Xrn
->r
;
128 if (Xrn
->exts
&& func
)
130 patricia_deref_prefix (Xrn
->prefix
);
133 patricia
->num_active_node
--;
142 } else if (Xsp
!= Xstack
) {
145 Xrn
= (patricia_node_t
*) 0;
149 assert (patricia
->num_active_node
== 0);
153 patricia_destroy_tree (patricia_tree_t
*patricia
, void_fn_t func
)
155 patricia_clear_tree (patricia
, func
);
157 /*TODO: EXTENSIONS!*/
162 * if func is supplied, it will be called as func(node->prefix)
166 patricia_process (patricia_tree_t
*patricia
, void_fn_t func
)
168 patricia_node_t
*node
;
171 PATRICIA_WALK (patricia
->head
, node
) {
177 patricia_walk_inorder(patricia_node_t
*node
, void_fn_t func
)
183 n
+= patricia_walk_inorder(node
->l
, func
);
192 n
+= patricia_walk_inorder(node
->r
, func
);
200 patricia_search_exact (patricia_tree_t
*patricia
, struct irc_in_addr
*sin
, unsigned char bitlen
)
202 patricia_node_t
*node
;
207 assert (bitlen
<= patricia
->maxbits
);
209 if (patricia
->head
== NULL
)
212 node
= patricia
->head
;
213 addr
= (u_char
*)sin
;
215 while (node
->bit
< bitlen
) {
216 if (is_bit_set(addr
,node
->bit
))
225 if (node
->bit
> bitlen
|| node
->prefix
== NULL
)
227 assert (node
->bit
== bitlen
);
228 assert (node
->bit
== node
->prefix
->bitlen
);
229 if (comp_with_mask (prefix_tochar (node
->prefix
), addr
, bitlen
)) {
236 /* if inclusive != 0, "best" may be the given prefix itself */
238 patricia_search_best2 (patricia_tree_t
*patricia
, struct irc_in_addr
*sin
, unsigned char bitlen
, int inclusive
)
240 patricia_node_t
*node
;
241 patricia_node_t
*stack
[PATRICIA_MAXBITS
+ 1];
247 assert (bitlen
<= patricia
->maxbits
);
249 if (patricia
->head
== NULL
)
252 node
= patricia
->head
;
253 addr
= (u_char
*)sin
;
255 while (node
->bit
< bitlen
) {
261 if (is_bit_set(addr
,node
->bit
)) {
272 if (inclusive
&& node
&& node
->prefix
)
280 if (comp_with_mask (prefix_tochar (node
->prefix
),
282 node
->prefix
->bitlen
)) {
291 patricia_search_best (patricia_tree_t
*patricia
, struct irc_in_addr
*sin
, unsigned char bitlen
)
293 return (patricia_search_best2 (patricia
, sin
, bitlen
, 1));
298 patricia_lookup (patricia_tree_t
*patricia
, prefix_t
*prefix
)
300 patricia_node_t
*node
, *new_node
, *parent
, *glue
;
301 u_char
*addr
, *test_addr
;
302 u_int bitlen
, check_bit
, differ_bit
;
307 assert (prefix
->bitlen
<= patricia
->maxbits
);
309 /* if new trie, create the first node */
310 if (patricia
->head
== NULL
) {
311 node
= patricia_new_node(patricia
, prefix
->bitlen
, patricia_ref_prefix (prefix
));
312 patricia
->head
= node
;
316 addr
= prefix_touchar (prefix
);
317 bitlen
= prefix
->bitlen
;
318 node
= patricia
->head
;
320 /* while ( bitlength of tree node < bitlength of node we're searching for || the node has no prefix */
321 while (node
->bit
< bitlen
|| node
->prefix
== NULL
) {
322 /* check that we're not at the lowest leaf i.e. node->bit is less than max bits */
323 if (node
->bit
< patricia
->maxbits
&&
324 (is_bit_set(addr
,node
->bit
))) {
338 assert (node
->prefix
);
340 test_addr
= prefix_touchar (node
->prefix
);
341 /* find the first bit different */
342 check_bit
= (node
->bit
< bitlen
)? node
->bit
: bitlen
;
344 for (i
= 0; i
*8 < check_bit
; i
++) {
345 if ((r
= (addr
[i
] ^ test_addr
[i
])) == 0) {
346 differ_bit
= (i
+ 1) * 8;
349 /* I know the better way, but for now */
350 for (j
= 0; j
< 8; j
++) {
351 if ((r
) & ((0x80 >> j
)))
356 differ_bit
= i
* 8 + j
;
359 if (differ_bit
> check_bit
)
360 differ_bit
= check_bit
;
363 parent
= node
->parent
;
364 while (parent
&& parent
->bit
>= differ_bit
) {
366 parent
= node
->parent
;
369 if (differ_bit
== bitlen
&& node
->bit
== bitlen
) {
371 node
->prefix
= patricia_ref_prefix (prefix
);
376 new_node
= patricia_new_node(patricia
, prefix
->bitlen
, patricia_ref_prefix (prefix
));
377 if (node
->bit
== differ_bit
) {
378 new_node
->parent
= node
;
379 if (node
->bit
< patricia
->maxbits
&&
380 (is_bit_set(addr
, node
->bit
))) {
381 assert (node
->r
== NULL
);
385 assert (node
->l
== NULL
);
391 if (bitlen
== differ_bit
) {
392 if (bitlen
< patricia
->maxbits
&&
393 (is_bit_set(test_addr
,bitlen
))) {
399 new_node
->parent
= node
->parent
;
400 if (node
->parent
== NULL
) {
401 assert (patricia
->head
== node
);
402 patricia
->head
= new_node
;
404 else if (node
->parent
->r
== node
) {
405 node
->parent
->r
= new_node
;
408 node
->parent
->l
= new_node
;
410 node
->parent
= new_node
;
411 new_node
->usercount
= node
->usercount
;
414 glue
= patricia_new_node(patricia
, differ_bit
, NULL
);
415 glue
->parent
= node
->parent
;
416 if (differ_bit
< patricia
->maxbits
&&
417 (is_bit_set(addr
, differ_bit
))) {
425 new_node
->parent
= glue
;
427 if (node
->parent
== NULL
) {
428 assert (patricia
->head
== node
);
429 patricia
->head
= glue
;
431 else if (node
->parent
->r
== node
) {
432 node
->parent
->r
= glue
;
435 node
->parent
->l
= glue
;
438 glue
->usercount
= node
->usercount
;
445 patricia_remove (patricia_tree_t
*patricia
, patricia_node_t
*node
)
447 patricia_node_t
*parent
, *child
;
452 if (node
->r
&& node
->l
) {
453 /* this might be a placeholder node -- have to check and make sure
454 * there is a prefix aossciated with it ! */
455 if (node
->prefix
!= NULL
)
456 patricia_deref_prefix (node
->prefix
);
461 if (node
->r
== NULL
&& node
->l
== NULL
) {
462 parent
= node
->parent
;
463 patricia_deref_prefix (node
->prefix
);
465 patricia
->num_active_node
--;
467 if (parent
== NULL
) {
468 assert (patricia
->head
== node
);
469 patricia
->head
= NULL
;
473 if (parent
->r
== node
) {
478 assert (parent
->l
== node
);
486 /* we need to remove parent too */
488 if (parent
->parent
== NULL
) {
489 assert (patricia
->head
== parent
);
490 patricia
->head
= child
;
492 else if (parent
->parent
->r
== parent
) {
493 parent
->parent
->r
= child
;
496 assert (parent
->parent
->l
== parent
);
497 parent
->parent
->l
= child
;
499 child
->parent
= parent
->parent
;
501 patricia
->num_active_node
--;
512 parent
= node
->parent
;
513 child
->parent
= parent
;
515 patricia_deref_prefix (node
->prefix
);
517 patricia
->num_active_node
--;
519 if (parent
== NULL
) {
520 assert (patricia
->head
== node
);
521 patricia
->head
= child
;
525 if (parent
->r
== node
) {
529 assert (parent
->l
== node
);
536 refnode(patricia_tree_t
*tree
, struct irc_in_addr
*sin
, int bitlen
) {
537 patricia_node_t
*node
;
540 node
= patricia_search_exact(tree
, sin
, bitlen
);
543 prefix
= patricia_new_prefix(sin
, bitlen
);
544 node
= patricia_lookup(tree
, prefix
);
545 patricia_deref_prefix(prefix
);
546 } else if (node
->prefix
) {
547 patricia_ref_prefix(node
->prefix
);
554 derefnode(patricia_tree_t
*tree
, patricia_node_t
*node
) {
555 if (!node
|| !node
->prefix
)
558 if (node
->prefix
->ref_count
== 1) {
559 patricia_remove(tree
, node
);
561 patricia_deref_prefix(node
->prefix
);
564 patricia_node_t
*patricia_new_node(patricia_tree_t
*patricia
, unsigned char bit
,prefix_t
*prefix
) {
565 patricia_node_t
*new_node
= newnode();
566 memset(new_node
->exts
, 0, PATRICIA_MAXSLOTS
* sizeof(void *));
568 new_node
->prefix
= prefix
;
569 new_node
->usercount
= 0;
570 new_node
->parent
= NULL
;
571 new_node
->l
= new_node
->r
= NULL
;
572 patricia
->num_active_node
++;
576 void node_increment_usercount( patricia_node_t
*node
) {
583 void node_decrement_usercount( patricia_node_t
*node
) {