1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908
|
/*
* mjl_prefixtree
*
* Adapted from Dave Plonka's Net::Patricia, which includes material
* from MRT. Some of the variables are named as they are in
* mjl_patricia for clarity with that code. The main improvements are
* in the bit testing, which uses a method shared with bit testing in
* mjl_patricia. Note, we cannot use a generic Patricia Trie to do
* longest matching prefix lookup, hence this tree.
*
* Copyright (C) 2016 Matthew Luckie. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Matthew Luckie ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL Matthew Luckie BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: mjl_prefixtree.c,v 1.9 2016/10/28 06:45:52 mjl Exp $
*
*/
#ifndef lint
static const char rcsid[] =
"$Id: mjl_prefixtree.c,v 1.9 2016/10/28 06:45:52 mjl Exp $";
#endif
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#if defined(DMALLOC)
#include <dmalloc.h>
#endif
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#if defined(__sun__)
# define s6_addr32 _S6_un._S6_u32
#elif !defined(s6_addr32)
# define s6_addr32 __u6_addr.__u6_addr32
#endif
#include "mjl_prefixtree.h"
struct prefixtree_node
{
union
{
prefix4_t *v4;
prefix6_t *v6;
void *raw;
} pref;
prefixtree_node_t *parent;
prefixtree_node_t *left;
prefixtree_node_t *right;
int bit;
};
struct prefixtree
{
prefixtree_node_t *head;
int v;
};
static const uint32_t uint32_netmask[] = {
0x80000000, 0xc0000000, 0xe0000000, 0xf0000000,
0xf8000000, 0xfc000000, 0xfe000000, 0xff000000,
0xff800000, 0xffc00000, 0xffe00000, 0xfff00000,
0xfff80000, 0xfffc0000, 0xfffe0000, 0xffff0000,
0xffff8000, 0xffffc000, 0xffffe000, 0xfffff000,
0xfffff800, 0xfffffc00, 0xfffffe00, 0xffffff00,
0xffffff80, 0xffffffc0, 0xffffffe0, 0xfffffff0,
0xfffffff8, 0xfffffffc, 0xfffffffe, 0xffffffff,
};
#ifdef _WIN32
static const uint16_t uint16_mask[] = {
0x8000, 0xc000, 0xe000, 0xf000,
0xf800, 0xfc00, 0xfe00, 0xff00,
0xff80, 0xffc0, 0xffe0, 0xfff0,
0xfff8, 0xfffc, 0xfffe, 0xffff,
};
#endif
#ifndef DMALLOC
prefix4_t *prefix4_alloc(struct in_addr *net, uint8_t len, void *ptr)
#else
prefix4_t *prefix4_alloc_dm(struct in_addr *net, uint8_t len, void *ptr,
const char *file, const int line)
#endif
{
prefix4_t *p;
#ifndef DMALLOC
p = malloc(sizeof(prefix4_t));
#else
p = dmalloc_malloc(file, line, sizeof(prefix4_t), DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(p == NULL)
return NULL;
p->net.s_addr = net->s_addr;
p->len = len;
p->ptr = ptr;
return p;
}
#ifndef DMALLOC
prefix4_t *prefix4_dup(const prefix4_t *item)
#else
prefix4_t *prefix4_dup_dm(const prefix4_t *item,
const char *file, const int line)
#endif
{
prefix4_t *dup;
#ifndef DMALLOC
dup = malloc(sizeof(prefix4_t));
#else
dup = dmalloc_malloc(file,line,sizeof(prefix4_t), DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(dup == NULL)
return NULL;
memcpy(dup, item, sizeof(prefix4_t));
dup->ptr = NULL;
return dup;
}
void prefix4_free(prefix4_t *pref)
{
free(pref);
return;
}
int prefix4_cmp(const prefix4_t *a, const prefix4_t *b)
{
uint32_t ua = ntohl(a->net.s_addr);
uint32_t ub = ntohl(b->net.s_addr);
if(ua < ub) return -1;
if(ua > ub) return 1;
if(a->len < b->len) return -1;
if(a->len > b->len) return 1;
return 0;
}
#ifndef DMALLOC
prefix6_t *prefix6_alloc(struct in6_addr *net, uint8_t len, void *ptr)
#else
prefix6_t *prefix6_alloc_dm(struct in6_addr *net, uint8_t len, void *ptr,
const char *file, const int line)
#endif
{
prefix6_t *p;
#ifndef DMALLOC
p = malloc(sizeof(prefix6_t));
#else
p = dmalloc_malloc(file, line, sizeof(prefix6_t), DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(p == NULL)
return NULL;
memcpy(&p->net, net, sizeof(struct in6_addr));
p->len = len;
p->ptr = ptr;
return p;
}
#ifndef DMALLOC
prefix6_t *prefix6_dup(const prefix6_t *item)
#else
prefix6_t *prefix6_dup_dm(const prefix6_t *item,
const char *file, const int line)
#endif
{
prefix6_t *dup;
#ifndef DMALLOC
dup = malloc(sizeof(prefix4_t));
#else
dup = dmalloc_malloc(file,line,sizeof(prefix4_t), DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(dup == NULL)
return NULL;
memcpy(dup, item, sizeof(prefix6_t));
dup->ptr = NULL;
return dup;
}
void prefix6_free(prefix6_t *pref)
{
free(pref);
return;
}
int prefix6_cmp(const prefix6_t *a, const prefix6_t *b)
{
int i;
#ifndef _WIN32
uint32_t ua, ub;
for(i=0; i<4; i++)
{
ua = ntohl(a->net.s6_addr32[i]);
ub = ntohl(b->net.s6_addr32[i]);
if(ua < ub) return -1;
if(ua > ub) return 1;
}
#else
uint16_t ua, ub;
for(i=0; i<8; i++)
{
ua = ntohs(a->net.u.Word[i]);
ub = ntohs(b->net.u.Word[i]);
if(ua < ub) return -1;
if(ua > ub) return 1;
}
#endif
if(a->len < b->len) return -1;
if(a->len > b->len) return 1;
return 0;
}
#ifndef DMALLOC
static prefixtree_node_t *prefixtree_node_alloc(void *pref, int bit)
#else
static prefixtree_node_t *prefixtree_node_alloc_dm(void *pref, int bit,
const char *file,
const int line)
#endif
{
prefixtree_node_t *n;
#ifndef DMALLOC
n = malloc(sizeof(prefixtree_node_t));
#else
n = dmalloc_malloc(file,line,sizeof(prefixtree_node_t),
DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(n == NULL)
return NULL;
n->pref.raw = pref;
n->bit = bit;
n->parent = n->left = n->right = NULL;
return n;
}
static int ip4_bit(const struct in_addr *ip, int bit)
{
assert(bit >= 0); assert(bit < 32);
return (ntohl(ip->s_addr) >> (31 - bit)) & 1;
}
static int ip6_bit(const struct in6_addr *ip, int bit)
{
assert(bit >= 0); assert(bit < 128);
#ifndef _WIN32
return (ntohl(ip->s6_addr32[bit/32]) >> (32 - ((bit+1) % 32))) & 1;
#else
return (ntohs(ip->u.Word[bit/16]) >> (16 - ((bit+1) % 16))) & 1;
#endif
}
static int prefix4_bit(const prefix4_t *pref, int bit)
{
assert(bit >= 0); assert(bit < 32);
return (ntohl(pref->net.s_addr) >> (31 - bit)) & 1;
}
static int prefix6_bit(const prefix6_t *pref, int bit)
{
assert(bit >= 0); assert(bit < 128);
#ifndef _WIN32
return (ntohl(pref->net.s6_addr32[bit/32]) >> (32 - ((bit+1) % 32))) & 1;
#else
return (ntohs(pref->net.u.Word[bit/16]) >> (16 - ((bit+1) % 16))) & 1;
#endif
}
static int prefix4_fbd(const prefix4_t *a, const prefix4_t *b)
{
uint32_t v;
int r;
if((v = ntohl(a->net.s_addr ^ b->net.s_addr)) == 0)
return 32;
#ifdef HAVE___BUILTIN_CLZ
r = __builtin_clz(v);
#else
r = 0;
if(v & 0xFFFF0000) { v >>= 16; r += 16; }
if(v & 0xFF00) { v >>= 8; r += 8; }
if(v & 0xF0) { v >>= 4; r += 4; }
if(v & 0xC) { v >>= 2; r += 2; }
if(v & 0x2) { r += 1; }
r = 31 - r;
#endif
return r;
}
static int prefix6_fbd(const prefix6_t *a, const prefix6_t *b)
{
uint32_t v;
int i, r;
for(i=0; i<4; i++)
{
if((v = ntohl(a->net.s6_addr32[i] ^ b->net.s6_addr32[i])) == 0)
continue;
#ifdef HAVE___BUILTIN_CLZ
r = __builtin_clz(v) + (i * 32);
#else
r = 0;
if(v & 0xFFFF0000) { v >>= 16; r += 16; }
if(v & 0xFF00) { v >>= 8; r += 8; }
if(v & 0xF0) { v >>= 4; r += 4; }
if(v & 0xC) { v >>= 2; r += 2; }
if(v & 0x2) { r += 1; }
r = (31 - r) + (i * 32);
#endif
return r;
}
return 128;
}
static int prefix4_ip_in(const prefix4_t *p4, const struct in_addr *ip4)
{
assert(p4->len >= 0); assert(p4->len <= 32);
if(p4->len == 0)
return 1;
if(((ip4->s_addr ^ p4->net.s_addr) & htonl(uint32_netmask[p4->len-1])) == 0)
return 1;
return 0;
}
static int prefix6_ip_in(const prefix6_t *p6, const struct in6_addr *ip6)
{
int i, len;
#ifndef _WIN32
uint32_t mask;
#else
uint16_t mask;
#endif
assert(p6->len >= 0); assert(p6->len <= 128);
if((len = p6->len) == 0)
return 1;
#ifndef _WIN32
for(i=0; i<4; i++)
{
/*
* handle the fact that we can only check 32 bits at a time.
* no need to change byte order as all bytes are the same
*/
if(len > 32)
mask = uint32_netmask[31];
else
mask = htonl(uint32_netmask[len-1]);
if(((ip6->s6_addr32[i] ^ p6->net.s6_addr32[i]) & mask) != 0)
return 0;
if(len <= 32)
return 1;
len -= 32;
}
#else
for(i=0; i<8; i++)
{
if(len > 16)
mask = uint16_mask[15];
else
mask = htons(uint16_mask[len-1]);
if(((ip6->u.Word[i] ^ p6->net.u.Word[i]) & mask) != 0)
return 0;
if(len <= 16)
return 1;
len -= 16;
}
#endif
return -1;
}
prefix4_t *prefixtree_find_ip4(const prefixtree_t *tree,
const struct in_addr *ip4)
{
prefixtree_node_t *x = tree->head;
prefix4_t *stack[33];
int i = 0;
assert(tree->v == 4);
/* go through the tree, assembling possible prefix matches */
while(x != NULL && x->bit < 32)
{
if(x->pref.v4 != NULL)
stack[i++] = x->pref.v4;
if(ip4_bit(ip4, x->bit) == 0)
x = x->left;
else
x = x->right;
}
if(x != NULL && x->pref.v4 != NULL)
stack[i++] = x->pref.v4;
/* go from the top of the stack, looking for the longest prefix match */
while(--i >= 0)
if(prefix4_ip_in(stack[i], ip4) != 0)
return stack[i];
return NULL;
}
prefix4_t *prefixtree_find_best4(const prefixtree_t *tree,
const prefix4_t *item)
{
prefixtree_node_t *x = tree->head;
prefix4_t *stack[33];
int i = 0;
assert(tree->v == 4);
/* go through the tree, assembling possible prefix matches */
while(x != NULL && x->bit < item->len)
{
if(x->pref.v4 != NULL && x->pref.v4->len <= item->len)
stack[i++] = x->pref.v4;
if(ip4_bit(&item->net, x->bit) == 0)
x = x->left;
else
x = x->right;
}
if(x != NULL && x->pref.v4 != NULL && x->pref.v4->len <= item->len)
stack[i++] = x->pref.v4;
/*
* go from the top of the stack, looking for the most specific
* enclosing prefix
*/
while(--i >= 0)
if(prefix4_ip_in(stack[i], &item->net) != 0)
return stack[i];
return NULL;
}
prefix6_t *prefixtree_find_ip6(const prefixtree_t *tree,
const struct in6_addr *ip6)
{
prefixtree_node_t *x = tree->head;
prefix6_t *stack[129];
int i = 0;
assert(tree->v == 6);
/* go through the tree, assembling possible prefix matches */
while(x != NULL && x->bit < 128)
{
if(x->pref.v6 != NULL)
stack[i++] = x->pref.v6;
if(ip6_bit(ip6, x->bit) == 0)
x = x->left;
else
x = x->right;
}
if(x != NULL && x->pref.v6 != NULL)
stack[i++] = x->pref.v6;
/* go from the top of the stack, looking for the longest prefix match */
while(--i >= 0)
if(prefix6_ip_in(stack[i], ip6) != 0)
return stack[i];
return NULL;
}
prefix6_t *prefixtree_find_best6(const prefixtree_t *tree,
const prefix6_t *item)
{
prefixtree_node_t *x = tree->head;
prefix6_t *stack[129];
int i = 0;
assert(tree->v == 6);
/* go through the tree, assembling possible prefix matches */
while(x != NULL && x->bit < item->len)
{
if(x->pref.v6 != NULL && x->pref.v6->len <= item->len)
stack[i++] = x->pref.v6;
if(ip6_bit(&item->net, x->bit) == 0)
x = x->left;
else
x = x->right;
}
if(x != NULL && x->pref.v6 != NULL && x->pref.v6->len <= item->len)
stack[i++] = x->pref.v6;
/*
* go from the top of the stack, looking for the most specific
* enclosing prefix
*/
while(--i >= 0)
if(prefix6_ip_in(stack[i], &item->net) != 0)
return stack[i];
return NULL;
}
#ifndef DMALLOC
prefixtree_node_t *prefixtree_insert4(prefixtree_t *tree, prefix4_t *pref)
#else
prefixtree_node_t *prefixtree_insert4_dm(prefixtree_t *tree, prefix4_t *pref,
const char *file, const int line)
#endif
{
prefixtree_node_t *t, *g, *p, *x;
struct in_addr *a;
int bit, fbd;
assert(tree->v == 4);
if((x = tree->head) == NULL)
{
#ifndef DMALLOC
t = prefixtree_node_alloc(pref, pref->len);
#else
t = prefixtree_node_alloc_dm(pref, pref->len, file, line);
#endif
if(t == NULL)
return NULL;
tree->head = t;
return t;
}
/* go through the tree until we get to the appropriate level */
while(x->bit < pref->len || x->pref.v4 == NULL)
{
if(x->bit == 32 || prefix4_bit(pref, x->bit) == 0)
{
if(x->left == NULL)
break;
x = x->left;
}
else
{
if(x->right == NULL)
break;
x = x->right;
}
}
if(x->bit < pref->len)
bit = x->bit;
else
bit = pref->len;
/* get the first bit different between the two prefixes */
fbd = prefix4_fbd(x->pref.v4, pref);
if(fbd > bit)
fbd = bit;
/*
* remember the address that we want to reference when inserting,
* before we go up the tree
*/
a = &x->pref.v4->net;
p = x->parent;
while(p != NULL && p->bit >= fbd)
{
x = p;
p = x->parent;
}
if(fbd == pref->len && x->bit == pref->len)
{
/* not going to insert the same prefix twice */
if(x->pref.v4 != NULL)
return NULL;
x->pref.v4 = pref;
return x;
}
#ifndef DMALLOC
t = prefixtree_node_alloc(pref, pref->len);
#else
t = prefixtree_node_alloc_dm(pref, pref->len, file, line);
#endif
if(t == NULL)
return NULL;
if(x->bit == fbd)
{
t->parent = x;
if(x->bit == 32 || prefix4_bit(pref, x->bit) == 0)
x->left = t;
else
x->right = t;
}
else if(pref->len == fbd)
{
if(pref->len == 32 || ip4_bit(a, pref->len) == 0)
t->left = x;
else
t->right = x;
t->parent = x->parent;
if(x->parent == NULL)
tree->head = t;
else if(x->parent->left == x)
x->parent->left = t;
else
x->parent->right = t;
x->parent = t;
}
else
{
/* insert a new branch node (with no attached prefix) into the tree */
#ifndef DMALLOC
g = prefixtree_node_alloc(NULL, fbd);
#else
g = prefixtree_node_alloc_dm(NULL, fbd, file, line);
#endif
if(g == NULL)
{
free(x);
return NULL;
}
g->parent = x->parent;
if(fbd == 32 || prefix4_bit(pref, fbd) == 0)
{
g->left = t;
g->right = x;
}
else
{
g->left = x;
g->right = t;
}
t->parent = g;
if(x->parent == NULL)
tree->head = g;
else if(x->parent->left == x)
x->parent->left = g;
else
x->parent->right = g;
x->parent = g;
}
return t;
}
#ifndef DMALLOC
prefixtree_node_t *prefixtree_insert6(prefixtree_t *tree, prefix6_t *pref)
#else
prefixtree_node_t *prefixtree_insert6_dm(prefixtree_t *tree, prefix6_t *pref,
const char *file, const int line)
#endif
{
prefixtree_node_t *t, *g, *p, *x;
struct in6_addr *a;
int bit, fbd;
assert(tree->v == 6);
if((x = tree->head) == NULL)
{
#ifndef DMALLOC
t = prefixtree_node_alloc(pref, pref->len);
#else
t = prefixtree_node_alloc_dm(pref, pref->len, file, line);
#endif
if(t == NULL)
return NULL;
tree->head = t;
return t;
}
/* go through the tree until we get to the appropriate level */
while(x->bit < pref->len || x->pref.v6 == NULL)
{
if(x->bit == 128 || prefix6_bit(pref, x->bit) == 0)
{
if(x->left == NULL)
break;
x = x->left;
}
else
{
if(x->right == NULL)
break;
x = x->right;
}
}
if(x->bit < pref->len)
bit = x->bit;
else
bit = pref->len;
/* get the first bit different between the two prefixes */
fbd = prefix6_fbd(x->pref.v6, pref);
if(fbd > bit)
fbd = bit;
/*
* remember the address that we want to reference when inserting,
* before we go up the tree
*/
a = &x->pref.v6->net;
p = x->parent;
while(p != NULL && p->bit >= fbd)
{
x = p;
p = x->parent;
}
if(fbd == pref->len && x->bit == pref->len)
{
/* not going to insert the same prefix twice */
if(x->pref.v6 != NULL)
return NULL;
x->pref.v6 = pref;
return x;
}
#ifndef DMALLOC
t = prefixtree_node_alloc(pref, pref->len);
#else
t = prefixtree_node_alloc_dm(pref, pref->len, file, line);
#endif
if(t == NULL)
return NULL;
if(x->bit == fbd)
{
t->parent = x;
if(x->bit == 128 || prefix6_bit(pref, x->bit) == 0)
x->left = t;
else
x->right = t;
}
else if(pref->len == fbd)
{
if(pref->len == 128 || ip6_bit(a, pref->len) == 0)
t->left = x;
else
t->right = x;
t->parent = x->parent;
if(x->parent == NULL)
tree->head = t;
else if(x->parent->left == x)
x->parent->left = t;
else
x->parent->right = t;
x->parent = t;
}
else
{
/* insert a new branch node (with no attached prefix) into the tree */
#ifndef DMALLOC
g = prefixtree_node_alloc(NULL, fbd);
#else
g = prefixtree_node_alloc_dm(NULL, fbd, file, line);
#endif
if(g == NULL)
{
free(x);
return NULL;
}
g->parent = x->parent;
if(fbd == 128 || prefix6_bit(pref, fbd) == 0)
{
g->left = t;
g->right = x;
}
else
{
g->left = x;
g->right = t;
}
t->parent = g;
if(x->parent == NULL)
tree->head = g;
else if(x->parent->left == x)
x->parent->left = g;
else
x->parent->right = g;
x->parent = g;
}
return t;
}
#ifndef DMALLOC
prefixtree_t *prefixtree_alloc4(void)
#else
prefixtree_t *prefixtree_alloc4_dm(const char *file, const int line)
#endif
{
prefixtree_t *tree;
#ifndef DMALLOC
tree = malloc(sizeof(prefixtree_t));
#else
tree = dmalloc_malloc(file, line, sizeof(prefixtree_t),
DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(tree == NULL)
return NULL;
tree->v = 4;
tree->head = NULL;
return tree;
}
#ifndef DMALLOC
prefixtree_t *prefixtree_alloc6(void)
#else
prefixtree_t *prefixtree_alloc6_dm(const char *file, const int line)
#endif
{
prefixtree_t *tree;
#ifndef DMALLOC
tree = malloc(sizeof(prefixtree_t));
#else
tree = dmalloc_malloc(file, line, sizeof(prefixtree_t),
DMALLOC_FUNC_MALLOC, 0, 0);
#endif
if(tree == NULL)
return NULL;
tree->v = 6;
tree->head = NULL;
return tree;
}
#ifndef DMALLOC
prefixtree_t *prefixtree_alloc(int af)
#else
prefixtree_t *prefixtree_alloc_dm(int af, const char *file, const int line)
#endif
{
#ifndef DMALLOC
if(af == AF_INET) return prefixtree_alloc4();
if(af == AF_INET6) return prefixtree_alloc6();
#else
if(af == AF_INET) return prefixtree_alloc4_dm(file, line);
if(af == AF_INET6) return prefixtree_alloc6_dm(file, line);
#endif
return NULL;
}
static void prefixtree_free_cb2(prefixtree_node_t *node, prefix_free_t cb)
{
if(node->left != NULL)
prefixtree_free_cb2(node->left, cb);
if(node->right != NULL)
prefixtree_free_cb2(node->right, cb);
if(cb != NULL && node->pref.raw != NULL)
cb(node->pref.raw);
free(node);
return;
}
void prefixtree_free_cb(prefixtree_t *tree, prefix_free_t cb)
{
if(tree == NULL)
return;
if(tree->head != NULL)
prefixtree_free_cb2(tree->head, cb);
free(tree);
return;
}
void prefixtree_free(prefixtree_t *tree)
{
if(tree == NULL)
return;
if(tree->head != NULL)
prefixtree_free_cb2(tree->head, NULL);
free(tree);
return;
}
|