ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/patricia.c
Revision: 7847
Committed: Sun Nov 6 16:58:50 2016 UTC (9 years, 8 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/patricia.c
File size: 23033 byte(s)
Log Message:
- aptricia.c, patricia.h: add patricia_try_search_exact_addr() and patricia_try_search_best_addr()

File Contents

# User Rev Content
1 michael 5042 /*
2 michael 6259 * $Id$
3 michael 5042 * Dave Plonka <plonka@doit.wisc.edu>
4     *
5     * This file had been called "radix.c" in the MRT sources.
6     *
7     * I renamed it to "patricia.c" since it's not an implementation of a general
8     * radix trie. Also I pulled in various requirements from "prefix.c" and
9     * "demo.c" so that it could be used as a standalone API.
10 michael 5046 *
11     * Copyright (c) 1999-2013
12     *
13     * The Regents of the University of Michigan ("The Regents") and Merit
14     * Network, Inc.
15     *
16     * Redistributions of source code must retain the above copyright notice,
17     * this list of conditions and the following disclaimer.
18     *
19     * Redistributions in binary form must reproduce the above copyright
20     * notice, this list of conditions and the following disclaimer in the
21     * documentation and/or other materials provided with the distribution.
22     *
23     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24     * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25     * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26     * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27     * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33     * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 michael 5042 */
35    
36     #include <assert.h> /* assert */
37     #include <ctype.h> /* isdigit */
38     #include <errno.h> /* errno */
39     #include <math.h> /* sin */
40     #include <stddef.h> /* NULL */
41     #include <stdio.h> /* sprintf, fprintf, stderr */
42     #include <stdlib.h> /* free, atol, calloc */
43     #include <string.h> /* memcpy, strchr, strlen */
44     #include <sys/types.h> /* BSD: for inet_addr */
45     #include <sys/socket.h> /* BSD, Linux: for inet_addr */
46     #include <netinet/in.h> /* BSD, Linux: for inet_addr */
47     #include <arpa/inet.h> /* BSD, Linux, Solaris: for inet_addr */
48    
49     #include "patricia.h"
50 michael 5047 #include "memory.h"
51 michael 5042
52     /* { from prefix.c */
53    
54     /* prefix_tochar
55     * convert prefix information to bytes
56     */
57 michael 7806 static unsigned char *
58 michael 7845 prefix_tochar(prefix_t *prefix)
59 michael 5042 {
60 michael 7834 if (prefix == NULL)
61     return NULL;
62     return (unsigned char *)&prefix->add.sin;
63 michael 5042 }
64    
65 michael 6607 static int
66 michael 7834 comp_with_mask(void *addr, void *dest, unsigned int mask)
67 michael 5042 {
68 michael 7834 if ( /* mask/8 == 0 || */ memcmp(addr, dest, mask / 8) == 0)
69     {
70     int n = mask / 8;
71     int m = ((-1) << (8 - (mask % 8)));
72 michael 5042
73 michael 7834 if (mask % 8 == 0 || (((unsigned char *)addr)[n] & m) == (((unsigned char *)dest)[n] & m))
74     return 1;
75     }
76 michael 5042
77 michael 7834 return 0;
78 michael 5042 }
79    
80 michael 6607 /*
81 michael 5042 * convert prefix information to ascii string with length
82     */
83 michael 7824 const char *
84 michael 7846 patricia_prefix_toa(const prefix_t *prefix, int with_len)
85 michael 5042 {
86 michael 7839 static char buf[INET6_ADDRSTRLEN + sizeof("/128")];
87 michael 5042
88 michael 7839 assert(prefix);
89 michael 7834 assert(prefix->ref_count >= 0);
90 michael 7839 assert((prefix->family == AF_INET && prefix->bitlen <= 32) ||
91     (prefix->family == AF_INET6 && prefix->bitlen <= 128));
92 michael 7834
93 michael 7839 inet_ntop(prefix->family, &prefix->add.sin6, buf, INET6_ADDRSTRLEN);
94 michael 7834
95 michael 7839 if (with_len)
96     sprintf(buf + strlen(buf), "/%d", prefix->bitlen);
97     return buf;
98 michael 5042 }
99    
100 michael 7814 static prefix_t *
101 michael 7834 New_Prefix2(int family, void *dest, int bitlen, prefix_t *prefix)
102 michael 5042 {
103 michael 7834 int dynamic_allocated = 0;
104 michael 7841 int addr_size = 0;
105 michael 5042
106 michael 7841 switch (family)
107 michael 7834 {
108 michael 7841 case AF_INET:
109     addr_size = sizeof(struct in_addr);
110     break;
111     case AF_INET6:
112     addr_size = sizeof(struct in6_addr);
113     break;
114     default: return NULL;
115 michael 7834 }
116 michael 5042
117 michael 7841 if (!prefix)
118 michael 7834 {
119 michael 7841 prefix = xcalloc(sizeof(prefix_t));
120     dynamic_allocated = 1;
121 michael 7834 }
122    
123 michael 7841 memcpy(&prefix->add.sin6, dest, addr_size);
124     prefix->bitlen = (bitlen >= 0) ? bitlen : addr_size * 8;
125 michael 7834 prefix->family = family;
126 michael 7841 prefix->ref_count = dynamic_allocated == 1;
127 michael 7834
128 michael 7846 /* fprintf(stderr, "[C %s, %d]\n", patricia_prefix_toa(prefix), prefix->ref_count); */
129 michael 7834 return prefix;
130 michael 5042 }
131    
132 michael 7814 static prefix_t *
133 michael 7845 New_Prefix(int family, void *dest, int bitlen)
134 michael 5042 {
135 michael 7834 return New_Prefix2(family, dest, bitlen, NULL);
136 michael 5042 }
137    
138     /* ascii2prefix
139     */
140 michael 7814 static prefix_t *
141 michael 7834 ascii2prefix(int family, const char *string)
142 michael 5042 {
143 michael 7842 int bitlen, maxbitlen = 0;
144 michael 7840 union
145     {
146     struct in_addr sin;
147     struct in6_addr sin6;
148     } sin;
149 michael 5042
150 michael 7840 assert(string);
151 michael 5042
152 michael 7840 /* Easy way to handle both families */
153 michael 7834 if (family == 0)
154     {
155     family = AF_INET;
156 michael 5044
157 michael 7834 if (strchr (string, ':'))
158     family = AF_INET6;
159     }
160 michael 5042
161 michael 7834 if (family == AF_INET)
162 michael 7840 maxbitlen = sizeof(struct in_addr) * 8;
163 michael 7834 else if (family == AF_INET6)
164     maxbitlen = sizeof(struct in6_addr) * 8;
165 michael 5042
166 michael 7846 const char *const cp = strchr(string, '/');
167     if (cp)
168 michael 7834 {
169 michael 7846 char save[MAXLINE];
170    
171 michael 7842 bitlen = atoi(cp + 1);
172 michael 5042
173 michael 7834 /* *cp = '\0'; */
174 michael 7846 /* Copy the string to save. Avoid destroying the string */
175 michael 7834 assert(cp - string < MAXLINE);
176     memcpy(save, string, cp - string);
177 michael 5045
178 michael 7834 save[cp - string] = '\0';
179     string = save;
180    
181     if (bitlen < 0 || bitlen > maxbitlen)
182     bitlen = maxbitlen;
183     }
184     else
185     bitlen = maxbitlen;
186    
187 michael 7840 if (inet_pton(family, string, &sin) <= 0)
188 michael 7834 return NULL;
189 michael 7840 return New_Prefix(family, &sin, bitlen);
190 michael 5042 }
191    
192 michael 5048 static prefix_t *
193 michael 7834 Ref_Prefix(prefix_t *prefix)
194 michael 5042 {
195 michael 7834 if (prefix == NULL)
196     return NULL;
197    
198     if (prefix->ref_count == 0)
199 michael 7846 /* Make a copy in case of a static prefix */
200 michael 7834 return New_Prefix2(prefix->family, &prefix->add, prefix->bitlen, NULL);
201    
202     prefix->ref_count++;
203    
204 michael 7846 /* fprintf(stderr, "[A %s, %d]\n", patricia_prefix_toa(prefix), prefix->ref_count); */
205 michael 7834 return prefix;
206 michael 5042 }
207    
208 michael 5048 static void
209 michael 7834 Deref_Prefix(prefix_t *prefix)
210 michael 5042 {
211 michael 7834 if (prefix == NULL)
212     return;
213 michael 5042
214 michael 7837 /* For secure programming, raise an assert. No static prefix can call this */
215 michael 7834 assert(prefix->ref_count > 0);
216 michael 7837 if (--prefix->ref_count <= 0)
217 michael 7834 xfree(prefix);
218 michael 5042 }
219    
220     /* } */
221    
222     /* #define PATRICIA_DEBUG 1 */
223    
224     /* these routines support continuous mask only */
225    
226     patricia_tree_t *
227 michael 7846 patricia_new(unsigned int maxbits)
228 michael 5042 {
229 michael 7834 patricia_tree_t *patricia = xcalloc(sizeof *patricia);
230     patricia->maxbits = maxbits;
231 michael 7830
232 michael 7834 assert(maxbits <= PATRICIA_MAXBITS); /* XXX */
233     return patricia;
234 michael 5042 }
235    
236     /*
237     * if func is supplied, it will be called as func(node->data)
238     * before deleting the node
239     */
240     void
241 michael 7846 patricia_clear(patricia_tree_t *patricia, void (*func)(void *))
242 michael 5042 {
243 michael 7834 assert(patricia);
244 michael 5042
245 michael 7834 if (patricia->head)
246     {
247     patricia_node_t *Xstack[PATRICIA_MAXBITS + 1];
248     patricia_node_t **Xsp = Xstack;
249     patricia_node_t *Xrn = patricia->head;
250 michael 5042
251 michael 7834 while (Xrn)
252     {
253     patricia_node_t *l = Xrn->l;
254     patricia_node_t *r = Xrn->r;
255 michael 5042
256 michael 7834 if (Xrn->prefix)
257     {
258     Deref_Prefix(Xrn->prefix);
259 michael 5042
260 michael 7834 if (Xrn->data && func)
261     func(Xrn->data);
262     }
263     else
264     {
265     assert(Xrn->data == NULL);
266     }
267    
268     xfree (Xrn);
269     patricia->num_active_node--;
270    
271     if (l)
272     {
273     if (r)
274     {
275     *Xsp++ = r;
276 michael 5042 }
277 michael 7834
278     Xrn = l;
279     }
280     else if (r)
281     {
282     Xrn = r;
283     }
284     else if (Xsp != Xstack)
285     {
286     Xrn = *(--Xsp);
287     }
288     else
289     {
290     Xrn = NULL;
291     }
292 michael 5042 }
293 michael 7834 }
294    
295     assert(patricia->num_active_node == 0);
296     /* xfree (patricia); */
297 michael 5042 }
298    
299     void
300 michael 7846 patricia_destroy(patricia_tree_t *patricia, void (*func)(void *))
301 michael 5042 {
302 michael 7846 patricia_clear(patricia, func);
303 michael 7834 xfree(patricia);
304 michael 5042 }
305    
306     /*
307     * if func is supplied, it will be called as func(node->prefix, node->data)
308     */
309     void
310 michael 7834 patricia_process(patricia_tree_t *patricia, void (*func)(prefix_t *, void *))
311 michael 5042 {
312 michael 7834 patricia_node_t *node;
313 michael 5042
314 michael 7834 assert(func);
315    
316 michael 7846 PATRICIA_WALK(patricia->head, node) {
317 michael 7838 func(node->prefix, node->data);
318 michael 7834 } PATRICIA_WALK_END;
319 michael 5042 }
320    
321     patricia_node_t *
322 michael 7834 patricia_search_exact(patricia_tree_t *patricia, prefix_t *prefix)
323 michael 5042 {
324 michael 7834 patricia_node_t *node;
325     unsigned char *addr;
326     unsigned int bitlen;
327 michael 5042
328 michael 7834 assert(patricia);
329     assert(prefix);
330     assert(prefix->bitlen <= patricia->maxbits);
331 michael 5042
332 michael 7834 if (patricia->head == NULL)
333     return NULL;
334 michael 5042
335 michael 7834 node = patricia->head;
336 michael 7838 addr = prefix_touchar(prefix);
337 michael 7834 bitlen = prefix->bitlen;
338 michael 5042
339 michael 7834 while (node->bit < bitlen)
340     {
341     if (BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
342     {
343 michael 5042 #ifdef PATRICIA_DEBUG
344 michael 7834 if (node->prefix)
345     fprintf(stderr, "patricia_search_exact: take right %s/%d\n",
346 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
347 michael 7834 else
348     fprintf(stderr, "patricia_search_exact: take right at %u\n", node->bit);
349     #endif /* PATRICIA_DEBUG */
350     node = node->r;
351     }
352     else
353     {
354 michael 5042 #ifdef PATRICIA_DEBUG
355 michael 7834 if (node->prefix)
356     fprintf(stderr, "patricia_search_exact: take left %s/%d\n",
357 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
358 michael 7834 else
359     fprintf(stderr, "patricia_search_exact: take left at %u\n", node->bit);
360     #endif /* PATRICIA_DEBUG */
361     node = node->l;
362 michael 5042 }
363    
364 michael 7834 if (node == NULL)
365     return NULL;
366     }
367    
368 michael 5042 #ifdef PATRICIA_DEBUG
369 michael 7834 if (node->prefix)
370     fprintf(stderr, "patricia_search_exact: stop at %s/%d\n",
371 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
372 michael 7834 else
373     fprintf(stderr, "patricia_search_exact: stop at %u\n", node->bit);
374     #endif /* PATRICIA_DEBUG */
375    
376     if (node->bit > bitlen || node->prefix == NULL)
377     return NULL;
378    
379     assert(node->bit == bitlen);
380     assert(node->bit == node->prefix->bitlen);
381    
382     if (comp_with_mask(prefix_tochar(node->prefix), prefix_tochar(prefix), bitlen))
383     {
384 michael 5042 #ifdef PATRICIA_DEBUG
385 michael 7834 fprintf(stderr, "patricia_search_exact: found %s/%d\n",
386 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
387 michael 7834 #endif /* PATRICIA_DEBUG */
388     return node;
389     }
390    
391     return NULL;
392 michael 5042 }
393    
394     /* if inclusive != 0, "best" may be the given prefix itself */
395     patricia_node_t *
396 michael 7834 patricia_search_best2(patricia_tree_t *patricia, prefix_t *prefix, int inclusive)
397 michael 5042 {
398 michael 7834 patricia_node_t *node;
399     patricia_node_t *stack[PATRICIA_MAXBITS + 1];
400     unsigned char *addr;
401     unsigned int bitlen;
402     int cnt = 0;
403 michael 5042
404 michael 7834 assert(patricia);
405     assert(prefix);
406     assert(prefix->bitlen <= patricia->maxbits);
407 michael 5042
408 michael 7834 if (patricia->head == NULL)
409     return NULL;
410 michael 5042
411 michael 7834 node = patricia->head;
412 michael 7838 addr = prefix_touchar(prefix);
413 michael 7834 bitlen = prefix->bitlen;
414 michael 5042
415 michael 7834 while (node->bit < bitlen)
416     {
417     if (node->prefix)
418     {
419 michael 5042 #ifdef PATRICIA_DEBUG
420 michael 7834 fprintf(stderr, "patricia_search_best: push %s/%d\n",
421 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
422 michael 7834 #endif /* PATRICIA_DEBUG */
423     stack[cnt++] = node;
424     }
425 michael 5042
426 michael 7838 if (BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
427 michael 7834 {
428 michael 5042 #ifdef PATRICIA_DEBUG
429 michael 7834 if (node->prefix)
430     fprintf(stderr, "patricia_search_best: take right %s/%d\n",
431 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
432 michael 7834 else
433     fprintf(stderr, "patricia_search_best: take right at %u\n", node->bit);
434     #endif /* PATRICIA_DEBUG */
435     node = node->r;
436     }
437     else
438     {
439 michael 5042 #ifdef PATRICIA_DEBUG
440 michael 7834 if (node->prefix)
441     fprintf(stderr, "patricia_search_best: take left %s/%d\n",
442 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
443 michael 7834 else
444     fprintf(stderr, "patricia_search_best: take left at %u\n", node->bit);
445     #endif /* PATRICIA_DEBUG */
446     node = node->l;
447 michael 5042 }
448    
449 michael 7834 if (node == NULL)
450     break;
451     }
452 michael 5042
453 michael 7834 if (inclusive && node && node->prefix)
454     stack[cnt++] = node;
455    
456 michael 5042 #ifdef PATRICIA_DEBUG
457 michael 7834 if (node == NULL)
458     fprintf(stderr, "patricia_search_best: stop at null\n");
459     else if (node->prefix)
460     fprintf(stderr, "patricia_search_best: stop at %s/%d\n",
461 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
462 michael 7834 else
463     fprintf(stderr, "patricia_search_best: stop at %u\n", node->bit);
464     #endif /* PATRICIA_DEBUG */
465 michael 5042
466 michael 7834 if (cnt <= 0)
467     return NULL;
468 michael 5042
469 michael 7834 while (--cnt >= 0)
470     {
471     node = stack[cnt];
472 michael 5042 #ifdef PATRICIA_DEBUG
473 michael 7834 fprintf(stderr, "patricia_search_best: pop %s/%d\n",
474 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
475 michael 7834 #endif /* PATRICIA_DEBUG */
476    
477     if (comp_with_mask(prefix_tochar(node->prefix),
478     prefix_tochar(prefix), node->prefix->bitlen) && node->prefix->bitlen <= bitlen)
479     {
480 michael 5042 #ifdef PATRICIA_DEBUG
481 michael 7834 fprintf(stderr, "patricia_search_best: found %s/%d\n",
482 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
483 michael 7834 #endif /* PATRICIA_DEBUG */
484     return node;
485 michael 5042 }
486 michael 7834 }
487    
488     return NULL;
489 michael 5042 }
490    
491     patricia_node_t *
492 michael 7834 patricia_search_best(patricia_tree_t *patricia, prefix_t *prefix)
493 michael 5042 {
494 michael 7834 return patricia_search_best2(patricia, prefix, 1);
495 michael 5042 }
496    
497     patricia_node_t *
498 michael 7834 patricia_lookup(patricia_tree_t *patricia, prefix_t *prefix)
499 michael 5042 {
500 michael 7834 patricia_node_t *node, *new_node, *parent, *glue;
501     unsigned char *addr, *test_addr;
502     unsigned int bitlen, check_bit, differ_bit;
503     int j, r;
504 michael 5042
505 michael 7834 assert(patricia);
506     assert(prefix);
507     assert(prefix->bitlen <= patricia->maxbits);
508 michael 5042
509 michael 7834 if (patricia->head == NULL)
510     {
511     node = xcalloc(sizeof *node);
512     node->bit = prefix->bitlen;
513     node->prefix = Ref_Prefix (prefix);
514     patricia->head = node;
515 michael 5042 #ifdef PATRICIA_DEBUG
516 michael 7834 fprintf(stderr, "patricia_lookup: new_node #0 %s/%d (head)\n",
517 michael 7846 patricia_prefix_toa(prefix), prefix->bitlen);
518 michael 7834 #endif /* PATRICIA_DEBUG */
519     patricia->num_active_node++;
520     return node;
521     }
522 michael 5042
523 michael 7838 addr = prefix_touchar(prefix);
524 michael 7834 bitlen = prefix->bitlen;
525     node = patricia->head;
526 michael 5042
527 michael 7834 while (node->bit < bitlen || node->prefix == NULL)
528     {
529     if (node->bit < patricia->maxbits && BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
530     {
531     if (node->r == NULL)
532     break;
533     #ifdef PATRICIA_DEBUG
534     if (node->prefix)
535     fprintf(stderr, "patricia_lookup: take right %s/%d\n",
536 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
537 michael 7834 else
538     fprintf(stderr, "patricia_lookup: take right at %u\n", node->bit);
539     #endif /* PATRICIA_DEBUG */
540 michael 5042
541 michael 7834 node = node->r;
542     }
543     else
544     {
545     if (node->l == NULL)
546     break;
547 michael 5042 #ifdef PATRICIA_DEBUG
548 michael 7834 if (node->prefix)
549     fprintf(stderr, "patricia_lookup: take left %s/%d\n",
550 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
551 michael 7834 else
552     fprintf(stderr, "patricia_lookup: take left at %u\n", node->bit);
553     #endif /* PATRICIA_DEBUG */
554 michael 5042
555 michael 7834 node = node->l;
556 michael 5042 }
557    
558 michael 7834 assert(node);
559     }
560    
561 michael 7838 assert(node->prefix);
562 michael 5042 #ifdef PATRICIA_DEBUG
563 michael 7834 fprintf(stderr, "patricia_lookup: stop at %s/%d\n",
564 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
565 michael 5042 #endif /* PATRICIA_DEBUG */
566    
567 michael 7838 test_addr = prefix_touchar(node->prefix);
568 michael 7834
569     /* Find the first bit different */
570     check_bit = (node->bit < bitlen) ? node->bit : bitlen;
571     differ_bit = 0;
572    
573     for (unsigned int i = 0; i * 8 < check_bit; i++)
574     {
575     if ((r = (addr[i] ^ test_addr[i])) == 0)
576     {
577     differ_bit = (i + 1) * 8;
578     continue;
579 michael 5042 }
580 michael 7834
581     /* I know the better way, but for now */
582     for (j = 0; j < 8; j++)
583     {
584     if (BIT_TEST (r, (0x80 >> j)))
585     break;
586     }
587    
588     /* Must be found */
589 michael 7838 assert(j < 8);
590 michael 7834 differ_bit = i * 8 + j;
591     break;
592     }
593    
594     if (differ_bit > check_bit)
595     differ_bit = check_bit;
596 michael 5042 #ifdef PATRICIA_DEBUG
597 michael 7834 fprintf(stderr, "patricia_lookup: differ_bit %d\n", differ_bit);
598     #endif /* PATRICIA_DEBUG */
599 michael 5042
600 michael 7834 parent = node->parent;
601    
602     while (parent && parent->bit >= differ_bit)
603     {
604     node = parent;
605 michael 5042 parent = node->parent;
606 michael 7834
607 michael 5042 #ifdef PATRICIA_DEBUG
608 michael 7834 if (node->prefix)
609     fprintf(stderr, "patricia_lookup: up to %s/%d\n",
610 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
611 michael 7834 else
612     fprintf(stderr, "patricia_lookup: up to %u\n", node->bit);
613     #endif /* PATRICIA_DEBUG */
614     }
615    
616     if (differ_bit == bitlen && node->bit == bitlen)
617     {
618     if (node->prefix)
619     {
620     #ifdef PATRICIA_DEBUG
621     fprintf(stderr, "patricia_lookup: found %s/%d\n",
622 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
623 michael 7834 #endif /* PATRICIA_DEBUG */
624    
625     return node;
626 michael 5042 }
627    
628 michael 7834 node->prefix = Ref_Prefix(prefix);
629 michael 6607 #ifdef PATRICIA_DEBUG
630 michael 7834 fprintf(stderr, "patricia_lookup: new node #1 %s/%d (glue mod)\n",
631 michael 7846 patricia_prefix_toa(prefix), prefix->bitlen);
632 michael 7834 #endif /* PATRICIA_DEBUG */
633    
634     assert(node->data == NULL);
635     return node;
636     }
637    
638     new_node = xcalloc(sizeof *new_node);
639     new_node->bit = prefix->bitlen;
640 michael 7844 new_node->prefix = Ref_Prefix(prefix);
641 michael 7834 patricia->num_active_node++;
642    
643     if (node->bit == differ_bit)
644     {
645     new_node->parent = node;
646    
647     if (node->bit < patricia->maxbits && BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
648     {
649     assert(node->r == NULL);
650     node->r = new_node;
651     }
652     else
653     {
654     assert(node->l == NULL);
655     node->l = new_node;
656     }
657 michael 5042 #ifdef PATRICIA_DEBUG
658 michael 7834 fprintf(stderr, "patricia_lookup: new_node #2 %s/%d (child)\n",
659 michael 7846 patricia_prefix_toa(prefix), prefix->bitlen);
660 michael 7834 #endif /* PATRICIA_DEBUG */
661     return new_node;
662     }
663    
664     if (bitlen == differ_bit)
665     {
666     if (bitlen < patricia->maxbits && BIT_TEST(test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07)))
667     {
668     new_node->r = node;
669 michael 5042 }
670 michael 7834 else
671     {
672     new_node->l = node;
673     }
674 michael 5042
675 michael 7834 new_node->parent = node->parent;
676 michael 5042
677 michael 7834 if (node->parent == NULL)
678     {
679     assert(patricia->head == node);
680     patricia->head = new_node;
681 michael 5042 }
682 michael 7834 else if (node->parent->r == node)
683     {
684     node->parent->r = new_node;
685     }
686     else
687     {
688     node->parent->l = new_node;
689     }
690 michael 5042
691 michael 7834 node->parent = new_node;
692 michael 5042 #ifdef PATRICIA_DEBUG
693 michael 7834 fprintf(stderr, "patricia_lookup: new_node #3 %s/%d (parent)\n",
694 michael 7846 patricia_prefix_toa(prefix), prefix->bitlen);
695 michael 7834 #endif /* PATRICIA_DEBUG */
696     }
697     else
698     {
699     glue = xcalloc(sizeof *glue);
700     glue->bit = differ_bit;
701     glue->parent = node->parent;
702     patricia->num_active_node++;
703    
704     if (differ_bit < patricia->maxbits && BIT_TEST(addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07)))
705     {
706     glue->r = new_node;
707     glue->l = node;
708 michael 5042 }
709 michael 7834 else
710     {
711     glue->r = node;
712     glue->l = new_node;
713     }
714 michael 5042
715 michael 7834 new_node->parent = glue;
716    
717     if (node->parent == NULL)
718     {
719     assert(patricia->head == node);
720     patricia->head = glue;
721     }
722     else if (node->parent->r == node)
723     {
724     node->parent->r = glue;
725     }
726     else
727     {
728     node->parent->l = glue;
729     }
730    
731     node->parent = glue;
732 michael 5042 #ifdef PATRICIA_DEBUG
733 michael 7834 fprintf(stderr, "patricia_lookup: new_node #4 %s/%d (glue+node)\n",
734 michael 7846 patricia_prefix_toa(prefix), prefix->bitlen);
735 michael 7834 #endif /* PATRICIA_DEBUG */
736     }
737    
738     return new_node;
739 michael 5042 }
740    
741     void
742 michael 7834 patricia_remove(patricia_tree_t *patricia, patricia_node_t *node)
743 michael 5042 {
744 michael 7834 patricia_node_t *parent, *child;
745 michael 5042
746 michael 7834 assert(patricia);
747     assert(node);
748 michael 5042
749 michael 7834 if (node->r && node->l)
750     {
751 michael 5042 #ifdef PATRICIA_DEBUG
752 michael 7834 fprintf(stderr, "patricia_remove: #0 %s/%d (r & l)\n",
753 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
754 michael 7834 #endif /* PATRICIA_DEBUG */
755 michael 5042
756 michael 7834 /*
757     * This might be a placeholder node -- have to check and make sure
758     * there is a prefix associated with it !
759     */
760     if (node->prefix != NULL)
761     Deref_Prefix(node->prefix);
762 michael 5042
763 michael 7834 node->prefix = NULL;
764     /* Also I needed to clear data pointer -- masaki */
765     node->data = NULL;
766     return;
767     }
768 michael 5042
769 michael 7834 if (node->r == NULL && node->l == NULL)
770     {
771     #ifdef PATRICIA_DEBUG
772     fprintf(stderr, "patricia_remove: #1 %s/%d (!r & !l)\n",
773 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
774 michael 7834 #endif /* PATRICIA_DEBUG */
775 michael 5042
776 michael 7834 parent = node->parent;
777     Deref_Prefix(node->prefix);
778     xfree(node);
779     patricia->num_active_node--;
780 michael 5042
781 michael 7834 if (parent == NULL)
782     {
783     assert(patricia->head == node);
784     patricia->head = NULL;
785     return;
786     }
787 michael 5042
788 michael 7834 if (parent->r == node)
789     {
790     parent->r = NULL;
791     child = parent->l;
792 michael 5042 }
793 michael 7834 else
794     {
795     assert(parent->l == node);
796 michael 5042
797 michael 7834 parent->l = NULL;
798     child = parent->r;
799 michael 5042 }
800 michael 7834
801     if (parent->prefix)
802     return;
803    
804     /* We need to remove parent too */
805     if (parent->parent == NULL)
806     {
807     assert(patricia->head == parent);
808     patricia->head = child;
809 michael 5042 }
810 michael 7834 else if (parent->parent->r == parent)
811     {
812     parent->parent->r = child;
813     }
814     else
815     {
816     assert(parent->parent->l == parent);
817     parent->parent->l = child;
818     }
819 michael 5042
820 michael 7834 child->parent = parent->parent;
821     xfree(parent);
822 michael 5042 patricia->num_active_node--;
823 michael 7834 return;
824     }
825 michael 5042
826 michael 7834 #ifdef PATRICIA_DEBUG
827     fprintf(stderr, "patricia_remove: #2 %s/%d (r ^ l)\n",
828 michael 7846 patricia_prefix_toa(node->prefix), node->prefix->bitlen);
829 michael 7834 #endif /* PATRICIA_DEBUG */
830 michael 5042
831 michael 7834 if (node->r)
832     {
833     child = node->r;
834     }
835     else
836     {
837     assert(node->l);
838     child = node->l;
839     }
840    
841     parent = node->parent;
842     child->parent = parent;
843    
844     Deref_Prefix(node->prefix);
845     xfree(node);
846     patricia->num_active_node--;
847    
848     if (parent == NULL)
849     {
850     assert(patricia->head == node);
851     patricia->head = child;
852     return;
853     }
854    
855     if (parent->r == node)
856     {
857     parent->r = child;
858     }
859     else
860     {
861     assert(parent->l == node);
862     parent->l = child;
863     }
864 michael 5042 }
865    
866     /* { from demo.c */
867     patricia_node_t *
868 michael 7846 patricia_make_and_lookup(patricia_tree_t *tree, const char *string)
869 michael 5042 {
870 michael 7834 prefix_t *prefix = ascii2prefix(0, string);
871 michael 5042
872 michael 7834 if (prefix)
873     {
874     patricia_node_t *node = patricia_lookup(tree, prefix);
875     Deref_Prefix(prefix);
876     return node;
877     }
878    
879     return NULL;
880 michael 5042 }
881    
882 michael 7846 void
883     patricia_lookup_then_remove(patricia_tree_t *tree, const char *string)
884     {
885     patricia_node_t *node = patricia_try_search_exact(tree, string);
886     if (node)
887     patricia_remove(tree, node);
888     }
889    
890 michael 5042 patricia_node_t *
891 michael 7846 patricia_try_search_exact(patricia_tree_t *tree, const char *string)
892 michael 5042 {
893 michael 7834 prefix_t *prefix = ascii2prefix(0, string);
894 michael 7809
895 michael 7834 if (prefix)
896     {
897     patricia_node_t *node = patricia_search_exact(tree, prefix);
898     Deref_Prefix(prefix);
899     return node;
900     }
901    
902     return NULL;
903 michael 5042 }
904    
905     patricia_node_t *
906 michael 7846 patricia_try_search_best(patricia_tree_t *tree, const char *string)
907 michael 5042 {
908 michael 7834 prefix_t *prefix = ascii2prefix(0, string);
909 michael 5042
910 michael 7834 if (prefix)
911     {
912     patricia_node_t *node = patricia_search_best(tree, prefix);
913     Deref_Prefix(prefix);
914     return node;
915     }
916    
917     return NULL;
918 michael 5042 }
919 michael 7847
920     patricia_node_t *
921     patricia_try_search_exact_addr(patricia_tree_t *tree, struct sockaddr *addr, int bitlen)
922     {
923     int family;
924     void *dest;
925    
926     if (addr->sa_family == AF_INET6)
927     {
928     if (bitlen == 0 || bitlen > 128)
929     bitlen = 128;
930     family = AF_INET6;
931     dest = &((struct sockaddr_in6 *)ip)->sin6_addr;
932     }
933     else
934     {
935     if (bitlen == 0 || bitlen > 32)
936     bitlen = 32;
937     family = AF_INET;
938     dest = &((struct sockaddr_in *)ip)->sin_addr;
939     }
940    
941     prefix_t *prefix = New_Prefix(family, dest, bitlen);
942     if (prefix)
943     {
944     patricia_node_t *node = patricia_search_exact(tree, prefix);
945     Deref_Prefix(prefix);
946     return node;
947     }
948    
949     return NULL;
950     }
951    
952     patricia_node_t *
953     patricia_try_search_best_addr(patricia_tree_t *tree, struct sockaddr *addr, int bitlen)
954     {
955     int family;
956     void *dest;
957    
958     if (addr->sa_family == AF_INET6)
959     {
960     if (bitlen == 0 || bitlen > 128)
961     bitlen = 128;
962     family = AF_INET6;
963     dest = &((struct sockaddr_in6 *)ip)->sin6_addr;
964     }
965     else
966     {
967     if (bitlen == 0 || bitlen > 32)
968     bitlen = 32;
969     family = AF_INET;
970     dest = &((struct sockaddr_in *)ip)->sin_addr;
971     }
972    
973     prefix_t *prefix = New_Prefix(family, dest, bitlen);
974     if (prefix)
975     {
976     patricia_node_t *node = patricia_search_best(tree, prefix);
977     Deref_Prefix(prefix);
978     return node;
979     }
980    
981     return NULL;
982     }
983 michael 5042 /* } */

Properties

Name Value
svn:eol-style native
svn:keywords Id