]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/patricia.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 static char copyright
[] =
16 "This product includes software developed by the University of Michigan, Merit"
17 "Network, Inc., and their contributors.";
20 #include <assert.h> /* assert */
21 #include <ctype.h> /* isdigit */
22 #include <errno.h> /* errno */
23 #include <math.h> /* sin */
24 #include <stddef.h> /* NULL */
25 #include <stdio.h> /* sprintf, fprintf, stderr */
26 #include <stdlib.h> /* free, atol, calloc */
27 #include <string.h> /* memcpy, strchr, strlen */
34 * convert prefix information to bytes
37 prefix_tochar (prefix_t
* prefix
)
42 return ((u_char
*) & prefix
->sin
);
46 comp_with_mask (void *addr
, void *dest
, u_int mask
)
49 if ( /* mask/8 == 0 || */ memcmp (addr
, dest
, mask
/ 8) == 0) {
51 int m
= ((-1) << (8 - (mask
% 8)));
53 if (mask
% 8 == 0 || (((u_char
*)addr
)[n
] & m
) == (((u_char
*)dest
)[n
] & m
))
60 patricia_new_prefix (struct irc_in_addr
*dest
, int bitlen
)
62 prefix_t
*prefix
= NULL
;
64 prefix
= malloc(sizeof (prefix_t
));
65 memcpy (&prefix
->sin
, dest
, 16);
67 prefix
->bitlen
= (bitlen
>= 0)? bitlen
: 128;
68 prefix
->ref_count
= 1;
73 patricia_ref_prefix (prefix_t
* prefix
)
77 if (prefix
->ref_count
== 0) {
78 /* make a copy in case of a static prefix */
79 return (patricia_new_prefix (&prefix
->sin
, prefix
->bitlen
));
82 /* fprintf(stderr, "[A %s, %d]\n", prefix_toa (prefix), prefix->ref_count); */
87 patricia_deref_prefix (prefix_t
* prefix
)
91 /* for secure programming, raise an assert. no static prefix can call this */
92 assert (prefix
->ref_count
> 0);
95 assert (prefix
->ref_count
>= 0);
96 if (prefix
->ref_count
<= 0) {
104 /* #define PATRICIA_DEBUG 1 */
106 /* these routines support continuous mask only */
109 patricia_new_tree (int maxbits
)
111 patricia_tree_t
*patricia
= calloc(1, sizeof *patricia
);
113 patricia
->maxbits
= maxbits
;
114 patricia
->head
= NULL
;
115 patricia
->num_active_node
= 0;
116 assert (maxbits
<= PATRICIA_MAXBITS
); /* XXX */
122 patricia_clear_tree (patricia_tree_t
*patricia
, void_fn_t func
)
125 if (patricia
->head
) {
127 patricia_node_t
*Xstack
[PATRICIA_MAXBITS
+1];
128 patricia_node_t
**Xsp
= Xstack
;
129 patricia_node_t
*Xrn
= patricia
->head
;
132 patricia_node_t
*l
= Xrn
->l
;
133 patricia_node_t
*r
= Xrn
->r
;
136 if (Xrn
->slots
&& func
)
138 patricia_deref_prefix (Xrn
->prefix
);
141 patricia
->num_active_node
--;
150 } else if (Xsp
!= Xstack
) {
153 Xrn
= (patricia_node_t
*) 0;
157 assert (patricia
->num_active_node
== 0);
158 /* free(patricia); */
163 patricia_destroy_tree (patricia_tree_t
*patricia
, void_fn_t func
)
165 patricia_clear_tree (patricia
, func
);
171 * if func is supplied, it will be called as func(node->prefix)
175 patricia_process (patricia_tree_t
*patricia
, void_fn_t func
)
177 patricia_node_t
*node
;
180 PATRICIA_WALK (patricia
->head
, node
) {
186 patricia_walk_inorder(patricia_node_t
*node
, void_fn_t func
)
192 n
+= patricia_walk_inorder(node
->l
, func
);
201 n
+= patricia_walk_inorder(node
->r
, func
);
209 patricia_search_exact (patricia_tree_t
*patricia
, struct irc_in_addr
*sin
, unsigned char bitlen
)
211 patricia_node_t
*node
;
216 assert (bitlen
<= patricia
->maxbits
);
218 if (patricia
->head
== NULL
)
221 node
= patricia
->head
;
222 addr
= (u_char
*)sin
;
224 while (node
->bit
< bitlen
) {
225 if (BIT_TEST (addr
[node
->bit
>> 3], 0x80 >> (node
->bit
& 0x07)))
234 if (node
->bit
> bitlen
|| node
->prefix
== NULL
)
236 assert (node
->bit
== bitlen
);
237 assert (node
->bit
== node
->prefix
->bitlen
);
238 if (comp_with_mask (prefix_tochar (node
->prefix
), addr
, bitlen
)) {
245 /* if inclusive != 0, "best" may be the given prefix itself */
247 patricia_search_best2 (patricia_tree_t
*patricia
, struct irc_in_addr
*sin
, unsigned char bitlen
, int inclusive
)
249 patricia_node_t
*node
;
250 patricia_node_t
*stack
[PATRICIA_MAXBITS
+ 1];
256 assert (bitlen
<= patricia
->maxbits
);
258 if (patricia
->head
== NULL
)
261 node
= patricia
->head
;
262 addr
= (u_char
*)sin
;
264 while (node
->bit
< bitlen
) {
270 if (BIT_TEST (addr
[node
->bit
>> 3], 0x80 >> (node
->bit
& 0x07))) {
281 if (inclusive
&& node
&& node
->prefix
)
289 if (comp_with_mask (prefix_tochar (node
->prefix
),
291 node
->prefix
->bitlen
)) {
300 patricia_search_best (patricia_tree_t
*patricia
, struct irc_in_addr
*sin
, unsigned char bitlen
)
302 return (patricia_search_best2 (patricia
, sin
, bitlen
, 1));
307 patricia_lookup (patricia_tree_t
*patricia
, prefix_t
*prefix
)
309 patricia_node_t
*node
, *new_node
, *parent
, *glue
;
310 u_char
*addr
, *test_addr
;
311 u_int bitlen
, check_bit
, differ_bit
;
316 assert (prefix
->bitlen
<= patricia
->maxbits
);
318 /* if new trie, create the first node */
319 if (patricia
->head
== NULL
) {
320 node
= malloc(sizeof *node
);
321 node
->bit
= prefix
->bitlen
;
322 node
->prefix
= patricia_ref_prefix (prefix
);
324 node
->l
= node
->r
= NULL
;
325 patricia
->head
= node
;
326 patricia
->num_active_node
++;
330 addr
= prefix_touchar (prefix
);
331 bitlen
= prefix
->bitlen
;
332 node
= patricia
->head
;
334 /* while ( bitlength of tree node < bitlength of node we're searching for || the node has no prefix */
335 while (node
->bit
< bitlen
|| node
->prefix
== NULL
) {
336 /* check that we're not at the lowest leaf i.e. node->bit is less than max bits */
337 if (node
->bit
< patricia
->maxbits
&&
338 (addr
[node
->bit
>> 3]) & (0x80 >> (node
->bit
& 0x07))) {
352 assert (node
->prefix
);
354 test_addr
= prefix_touchar (node
->prefix
);
355 /* find the first bit different */
356 check_bit
= (node
->bit
< bitlen
)? node
->bit
: bitlen
;
358 for (i
= 0; i
*8 < check_bit
; i
++) {
359 if ((r
= (addr
[i
] ^ test_addr
[i
])) == 0) {
360 differ_bit
= (i
+ 1) * 8;
363 /* I know the better way, but for now */
364 for (j
= 0; j
< 8; j
++) {
365 if ((r
) & ((0x80 >> j
)))
370 differ_bit
= i
* 8 + j
;
373 if (differ_bit
> check_bit
)
374 differ_bit
= check_bit
;
377 parent
= node
->parent
;
378 while (parent
&& parent
->bit
>= differ_bit
) {
380 parent
= node
->parent
;
383 if (differ_bit
== bitlen
&& node
->bit
== bitlen
) {
387 node
->prefix
= patricia_ref_prefix (prefix
);
391 new_node
= malloc(sizeof *new_node
);
392 new_node
->bit
= prefix
->bitlen
;
393 new_node
->prefix
= patricia_ref_prefix (prefix
);
394 new_node
->parent
= NULL
;
395 new_node
->l
= new_node
->r
= NULL
;
396 patricia
->num_active_node
++;
398 if (node
->bit
== differ_bit
) {
399 new_node
->parent
= node
;
400 if (node
->bit
< patricia
->maxbits
&&
401 (addr
[node
->bit
>> 3]) & (0x80 >> (node
->bit
& 0x07))) {
402 assert (node
->r
== NULL
);
406 assert (node
->l
== NULL
);
412 if (bitlen
== differ_bit
) {
413 if (bitlen
< patricia
->maxbits
&&
414 (test_addr
[bitlen
>> 3]) & (0x80 >> (bitlen
& 0x07))) {
420 new_node
->parent
= node
->parent
;
421 if (node
->parent
== NULL
) {
422 assert (patricia
->head
== node
);
423 patricia
->head
= new_node
;
425 else if (node
->parent
->r
== node
) {
426 node
->parent
->r
= new_node
;
429 node
->parent
->l
= new_node
;
431 node
->parent
= new_node
;
434 glue
= malloc(sizeof *glue
);
435 glue
->bit
= differ_bit
;
437 glue
->parent
= node
->parent
;
438 patricia
->num_active_node
++;
439 if (differ_bit
< patricia
->maxbits
&&
440 (addr
[differ_bit
>> 3]) & (0x80 >> (differ_bit
& 0x07))) {
448 new_node
->parent
= glue
;
450 if (node
->parent
== NULL
) {
451 assert (patricia
->head
== node
);
452 patricia
->head
= glue
;
454 else if (node
->parent
->r
== node
) {
455 node
->parent
->r
= glue
;
458 node
->parent
->l
= glue
;
466 patricia_remove (patricia_tree_t
*patricia
, patricia_node_t
*node
)
468 patricia_node_t
*parent
, *child
;
473 if (node
->r
&& node
->l
) {
474 /* this might be a placeholder node -- have to check and make sure
475 * there is a prefix aossciated with it ! */
476 if (node
->prefix
!= NULL
)
477 patricia_deref_prefix (node
->prefix
);
482 if (node
->r
== NULL
&& node
->l
== NULL
) {
483 parent
= node
->parent
;
484 patricia_deref_prefix (node
->prefix
);
486 patricia
->num_active_node
--;
488 if (parent
== NULL
) {
489 assert (patricia
->head
== node
);
490 patricia
->head
= NULL
;
494 if (parent
->r
== node
) {
499 assert (parent
->l
== node
);
507 /* we need to remove parent too */
509 if (parent
->parent
== NULL
) {
510 assert (patricia
->head
== parent
);
511 patricia
->head
= child
;
513 else if (parent
->parent
->r
== parent
) {
514 parent
->parent
->r
= child
;
517 assert (parent
->parent
->l
== parent
);
518 parent
->parent
->l
= child
;
520 child
->parent
= parent
->parent
;
522 patricia
->num_active_node
--;
533 parent
= node
->parent
;
534 child
->parent
= parent
;
536 patricia_deref_prefix (node
->prefix
);
538 patricia
->num_active_node
--;
540 if (parent
== NULL
) {
541 assert (patricia
->head
== node
);
542 patricia
->head
= child
;
546 if (parent
->r
== node
) {
550 assert (parent
->l
== node
);
558 refnode(patricia_tree_t
*tree
, struct irc_in_addr
*sin
, int bitlen
) {
559 patricia_node_t
*node
;
562 node
= patricia_search_exact(tree
, sin
, PATRICIA_MAXBITS
);
565 prefix
= patricia_new_prefix(sin
, bitlen
);
566 node
= patricia_lookup(tree
, prefix
);
567 node
->slots
= (void **)malloc(PATRICIA_MAXSLOTS
* sizeof(void *));
568 memset(node
->slots
, 0, PATRICIA_MAXSLOTS
* sizeof(void *));
569 patricia_deref_prefix(prefix
);
570 } else if (node
->prefix
) {
571 patricia_ref_prefix(node
->prefix
);
578 derefnode(patricia_tree_t
*tree
, patricia_node_t
*node
) {
579 if (!node
|| !node
->prefix
)
582 if (node
->prefix
->ref_count
== 1) {
584 patricia_remove(tree
, node
);
586 patricia_deref_prefix(node
->prefix
);