ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/hopm/trunk/src/patricia.c
Revision: 7848
Committed: Sun Nov 6 18:05:24 2016 UTC (9 years, 8 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid/trunk/src/patricia.c
File size: 22934 byte(s)
Log Message:
- patricia.c: style corrections; remove unused header includes

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

Properties

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