mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
src/3rdparty: Rebase patricia.{h,c} on upstream version
This updates to a clang-formatted version that was merged recently.
This commit is contained in:
parent
c4d015369b
commit
01c31e9917
2 changed files with 986 additions and 1046 deletions
328
src/3rdparty/patricia.c
vendored
328
src/3rdparty/patricia.c
vendored
|
@ -1,22 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* This code originates from Dave Plonka's Net::Security perl module. An adaptation
|
* $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $
|
||||||
* of it in C is kept at https://github.com/CAIDA/cc-common/tree/master/libpatricia.
|
|
||||||
* That repository is considered the upstream version for Zeek's fork. We make some
|
|
||||||
* custom changes to this upstream:
|
|
||||||
* - Replaces void_fn_t with data_fn_t and prefix_data_fn_t
|
|
||||||
* - Adds patricia_search_all method
|
|
||||||
* - One commented-out portion of an if statement that breaks one of our tests
|
|
||||||
*
|
|
||||||
* The current version is based on commit 4a2c61374f507a420d28bd9084c976142d279605
|
|
||||||
* from that repo.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Johanna Amann <johanna@icir.org>
|
|
||||||
*
|
|
||||||
* Added patricia_search_all function.
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
* Dave Plonka <plonka@doit.wisc.edu>
|
* Dave Plonka <plonka@doit.wisc.edu>
|
||||||
*
|
*
|
||||||
* This product includes software developed by the University of Michigan,
|
* This product includes software developed by the University of Michigan,
|
||||||
|
@ -29,40 +12,24 @@
|
||||||
* "demo.c" so that it could be used as a standalone API.
|
* "demo.c" so that it could be used as a standalone API.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* From copyright.txt:
|
/*
|
||||||
|
* This code originates from Dave Plonka's Net::Security perl module. An
|
||||||
|
* adaptation of it in C is kept at
|
||||||
|
* https://github.com/CAIDA/cc-common/tree/master/libpatricia. That repository
|
||||||
|
* is considered the upstream version for Zeek's fork. We make some custom
|
||||||
|
* changes to this upstream:
|
||||||
|
* - Replaces void_fn_t with data_fn_t and prefix_data_fn_t
|
||||||
|
* - Adds patricia_search_all method
|
||||||
|
* - One commented-out portion of an if statement that breaks one of our tests
|
||||||
*
|
*
|
||||||
* Copyright (c) 1997, 1998, 1999
|
* The current version is based on commit
|
||||||
|
* fd262ab5ac5bae8b0d4a8b5e2e723115b1846376 from that repo.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Johanna Amann <johanna@icir.org>
|
||||||
*
|
*
|
||||||
*
|
* Added patricia_search_all function.
|
||||||
* The Regents of the University of Michigan ("The Regents") and Merit Network,
|
|
||||||
* Inc. 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.
|
|
||||||
* 3. All advertising materials mentioning features or use of
|
|
||||||
* this software must display the following acknowledgement:
|
|
||||||
* This product includes software developed by the University of Michigan, Merit
|
|
||||||
* Network, Inc., and their contributors.
|
|
||||||
* 4. Neither the name of the University, Merit Network, nor the
|
|
||||||
* names of their contributors may be used to endorse or
|
|
||||||
* promote products derived from this software without
|
|
||||||
* specific prior written permission.
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "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 THE REGENTS OR CONTRIBUTORS 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef UNUSED
|
#ifndef UNUSED
|
||||||
|
@ -73,23 +40,22 @@
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
UNUSED static char copyright[] =
|
UNUSED static char copyright[] = "This product includes software developed by "
|
||||||
"This product includes software developed by the University of Michigan, Merit"
|
"the University of Michigan, Merit"
|
||||||
"Network, Inc., and their contributors.";
|
"Network, Inc., and their contributors.";
|
||||||
|
|
||||||
|
#include <arpa/inet.h> /* BSD, Linux, Solaris: for inet_addr */
|
||||||
#include <assert.h> /* assert */
|
#include <assert.h> /* assert */
|
||||||
#include <ctype.h> /* isdigit */
|
#include <ctype.h> /* isdigit */
|
||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
#include <math.h> /* sin */
|
#include <math.h> /* sin */
|
||||||
|
#include <netinet/in.h> /* BSD, Linux: for inet_addr */
|
||||||
#include <stddef.h> /* NULL */
|
#include <stddef.h> /* NULL */
|
||||||
#include <stdio.h> /* sprintf, fprintf, stderr */
|
#include <stdio.h> /* sprintf, fprintf, stderr */
|
||||||
#include <stdlib.h> /* free, atol, calloc */
|
#include <stdlib.h> /* free, atol, calloc */
|
||||||
#include <string.h> /* memcpy, strchr, strlen */
|
#include <string.h> /* memcpy, strchr, strlen */
|
||||||
#include <sys/types.h> /* BSD: for inet_addr */
|
|
||||||
#include <sys/socket.h> /* BSD, Linux: for inet_addr */
|
#include <sys/socket.h> /* BSD, Linux: for inet_addr */
|
||||||
#include <netinet/in.h> /* BSD, Linux: for inet_addr */
|
#include <sys/types.h> /* BSD: for inet_addr */
|
||||||
#include <arpa/inet.h> /* BSD, Linux, Solaris: for inet_addr */
|
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#include "patricia.h"
|
#include "patricia.h"
|
||||||
|
|
||||||
|
@ -103,8 +69,7 @@ extern void out_of_memory(const char* where);
|
||||||
/* prefix_tochar
|
/* prefix_tochar
|
||||||
* convert prefix information to bytes
|
* convert prefix information to bytes
|
||||||
*/
|
*/
|
||||||
u_char *
|
u_char *prefix_tochar(prefix_t *prefix)
|
||||||
prefix_tochar (prefix_t * prefix)
|
|
||||||
{
|
{
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
@ -112,8 +77,7 @@ prefix_tochar (prefix_t * prefix)
|
||||||
return ((u_char *)&prefix->add.sin);
|
return ((u_char *)&prefix->add.sin);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int comp_with_mask(void *addr, void *dest, u_int mask)
|
||||||
comp_with_mask (void *addr, void *dest, u_int mask)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
if (/* mask/8 == 0 || */ memcmp(addr, dest, mask / 8) == 0) {
|
if (/* mask/8 == 0 || */ memcmp(addr, dest, mask / 8) == 0) {
|
||||||
|
@ -127,8 +91,7 @@ comp_with_mask (void *addr, void *dest, u_int mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* this allows imcomplete prefix */
|
/* this allows imcomplete prefix */
|
||||||
int
|
int my_inet_pton(int af, const char *src, void *dst)
|
||||||
my_inet_pton (int af, const char *src, void *dst)
|
|
||||||
{
|
{
|
||||||
if (af == AF_INET) {
|
if (af == AF_INET) {
|
||||||
int i, c, val;
|
int i, c, val;
|
||||||
|
@ -155,8 +118,10 @@ my_inet_pton (int af, const char *src, void *dst)
|
||||||
}
|
}
|
||||||
memcpy(dst, xp, sizeof(struct in_addr));
|
memcpy(dst, xp, sizeof(struct in_addr));
|
||||||
return (1);
|
return (1);
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
} else if (af == AF_INET6) {
|
} else if (af == AF_INET6) {
|
||||||
return (inet_pton(af, src, dst));
|
return (inet_pton(af, src, dst));
|
||||||
|
#endif /* HAVE_IPV6 */
|
||||||
} else {
|
} else {
|
||||||
#ifndef NT
|
#ifndef NT
|
||||||
errno = EAFNOSUPPORT;
|
errno = EAFNOSUPPORT;
|
||||||
|
@ -171,8 +136,7 @@ my_inet_pton (int af, const char *src, void *dst)
|
||||||
* convert prefix information to ascii string with length
|
* convert prefix information to ascii string with length
|
||||||
* thread safe and (almost) re-entrant implementation
|
* thread safe and (almost) re-entrant implementation
|
||||||
*/
|
*/
|
||||||
char *
|
char *prefix_toa2x(prefix_t *prefix, char *buff, int with_len)
|
||||||
prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
|
||||||
{
|
{
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
return ("(Null)");
|
return ("(Null)");
|
||||||
|
@ -204,23 +168,24 @@ prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
||||||
assert(prefix->bitlen <= sizeof(struct in_addr) * 8);
|
assert(prefix->bitlen <= sizeof(struct in_addr) * 8);
|
||||||
a = prefix_touchar(prefix);
|
a = prefix_touchar(prefix);
|
||||||
if (with_len) {
|
if (with_len) {
|
||||||
sprintf (buff, "%d.%d.%d.%d/%d", a[0], a[1], a[2], a[3],
|
sprintf(buff, "%d.%d.%d.%d/%d", a[0], a[1], a[2], a[3], prefix->bitlen);
|
||||||
prefix->bitlen);
|
} else {
|
||||||
}
|
|
||||||
else {
|
|
||||||
sprintf(buff, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
|
sprintf(buff, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
|
||||||
}
|
}
|
||||||
return (buff);
|
return (buff);
|
||||||
}
|
}
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
else if (prefix->family == AF_INET6) {
|
else if (prefix->family == AF_INET6) {
|
||||||
char *r;
|
char *r;
|
||||||
r = (char *) inet_ntop (AF_INET6, &prefix->add.sin6, buff, 48 /* a guess value */ );
|
r = (char *)inet_ntop(AF_INET6, &prefix->add.sin6, buff,
|
||||||
|
48 /* a guess value */);
|
||||||
if (r && with_len) {
|
if (r && with_len) {
|
||||||
assert(prefix->bitlen <= sizeof(struct in6_addr) * 8);
|
assert(prefix->bitlen <= sizeof(struct in6_addr) * 8);
|
||||||
sprintf(buff + strlen(buff), "/%d", prefix->bitlen);
|
sprintf(buff + strlen(buff), "/%d", prefix->bitlen);
|
||||||
}
|
}
|
||||||
return (buff);
|
return (buff);
|
||||||
}
|
}
|
||||||
|
#endif /* HAVE_IPV6 */
|
||||||
else
|
else
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
@ -228,42 +193,39 @@ prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
||||||
/* prefix_toa2
|
/* prefix_toa2
|
||||||
* convert prefix information to ascii string
|
* convert prefix information to ascii string
|
||||||
*/
|
*/
|
||||||
char *
|
char *prefix_toa2(prefix_t *prefix, char *buff)
|
||||||
prefix_toa2 (prefix_t *prefix, char *buff)
|
|
||||||
{
|
{
|
||||||
return (prefix_toa2x(prefix, buff, 0));
|
return (prefix_toa2x(prefix, buff, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* prefix_toa
|
/* prefix_toa
|
||||||
*/
|
*/
|
||||||
char *
|
char *prefix_toa(prefix_t *prefix)
|
||||||
prefix_toa (prefix_t * prefix)
|
|
||||||
{
|
{
|
||||||
return (prefix_toa2(prefix, (char *)NULL));
|
return (prefix_toa2(prefix, (char *)NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix_t *
|
prefix_t *New_Prefix2(int family, void *dest, int bitlen, prefix_t *prefix)
|
||||||
New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
|
||||||
{
|
{
|
||||||
int dynamic_allocated = 0;
|
int dynamic_allocated = 0;
|
||||||
int default_bitlen = sizeof(struct in_addr) * 8;
|
int default_bitlen = sizeof(struct in_addr) * 8;
|
||||||
|
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
if (family == AF_INET6) {
|
if (family == AF_INET6) {
|
||||||
default_bitlen = sizeof(struct in6_addr) * 8;
|
default_bitlen = sizeof(struct in6_addr) * 8;
|
||||||
if (prefix == NULL) {
|
if (prefix == NULL) {
|
||||||
prefix = calloc(1, sizeof(prefix_t));
|
prefix = calloc(1, sizeof(prefix_t));
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
out_of_memory("patricia/new_prefix2: unable to allocate memory");
|
out_of_memory("patricia/new_prefix2: unable to allocate memory");
|
||||||
|
|
||||||
dynamic_allocated++;
|
dynamic_allocated++;
|
||||||
}
|
}
|
||||||
memcpy(&prefix->add.sin6, dest, sizeof(struct in6_addr));
|
memcpy(&prefix->add.sin6, dest, sizeof(struct in6_addr));
|
||||||
}
|
} else
|
||||||
else
|
#endif /* HAVE_IPV6 */
|
||||||
if (family == AF_INET) {
|
if (family == AF_INET) {
|
||||||
if (prefix == NULL) {
|
if (prefix == NULL) {
|
||||||
#ifndef NT
|
#ifndef NT
|
||||||
prefix = calloc(1, sizeof (prefix_t));
|
prefix = calloc(1, sizeof(prefix4_t));
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
out_of_memory("patricia/new_prefix2: unable to allocate memory");
|
out_of_memory("patricia/new_prefix2: unable to allocate memory");
|
||||||
#else
|
#else
|
||||||
|
@ -277,8 +239,7 @@ New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
||||||
dynamic_allocated++;
|
dynamic_allocated++;
|
||||||
}
|
}
|
||||||
memcpy(&prefix->add.sin, dest, sizeof(struct in_addr));
|
memcpy(&prefix->add.sin, dest, sizeof(struct in_addr));
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -288,25 +249,26 @@ New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
||||||
if (dynamic_allocated) {
|
if (dynamic_allocated) {
|
||||||
prefix->ref_count++;
|
prefix->ref_count++;
|
||||||
}
|
}
|
||||||
/* fprintf(stderr, "[C %s, %d]\n", prefix_toa (prefix), prefix->ref_count); */
|
/* fprintf(stderr, "[C %s, %d]\n", prefix_toa (prefix), prefix->ref_count);
|
||||||
|
*/
|
||||||
return (prefix);
|
return (prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix_t *
|
prefix_t *New_Prefix(int family, void *dest, int bitlen)
|
||||||
New_Prefix (int family, void *dest, int bitlen)
|
|
||||||
{
|
{
|
||||||
return (New_Prefix2(family, dest, bitlen, NULL));
|
return (New_Prefix2(family, dest, bitlen, NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ascii2prefix
|
/* ascii2prefix
|
||||||
*/
|
*/
|
||||||
prefix_t *
|
prefix_t *ascii2prefix(int family, char *string)
|
||||||
ascii2prefix (int family, char *string)
|
|
||||||
{
|
{
|
||||||
u_long bitlen, maxbitlen = 0;
|
u_long bitlen, maxbitlen = 0;
|
||||||
char *cp;
|
char *cp;
|
||||||
struct in_addr sin;
|
struct in_addr sin;
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
struct in6_addr sin6;
|
struct in6_addr sin6;
|
||||||
|
#endif /* HAVE_IPV6 */
|
||||||
int result;
|
int result;
|
||||||
char save[MAXLINE];
|
char save[MAXLINE];
|
||||||
|
|
||||||
|
@ -316,15 +278,20 @@ ascii2prefix (int family, char *string)
|
||||||
/* easy way to handle both families */
|
/* easy way to handle both families */
|
||||||
if (family == 0) {
|
if (family == 0) {
|
||||||
family = AF_INET;
|
family = AF_INET;
|
||||||
if (strchr (string, ':')) family = AF_INET6;
|
#ifdef HAVE_IPV6
|
||||||
|
if (strchr(string, ':'))
|
||||||
|
family = AF_INET6;
|
||||||
|
#endif /* HAVE_IPV6 */
|
||||||
}
|
}
|
||||||
|
|
||||||
if (family == AF_INET) {
|
if (family == AF_INET) {
|
||||||
maxbitlen = sizeof(struct in_addr) * 8;
|
maxbitlen = sizeof(struct in_addr) * 8;
|
||||||
}
|
}
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
else if (family == AF_INET6) {
|
else if (family == AF_INET6) {
|
||||||
maxbitlen = sizeof(struct in6_addr) * 8;
|
maxbitlen = sizeof(struct in6_addr) * 8;
|
||||||
}
|
}
|
||||||
|
#endif /* HAVE_IPV6 */
|
||||||
|
|
||||||
if ((cp = strchr(string, '/')) != NULL) {
|
if ((cp = strchr(string, '/')) != NULL) {
|
||||||
bitlen = atol(cp + 1);
|
bitlen = atol(cp + 1);
|
||||||
|
@ -334,10 +301,9 @@ ascii2prefix (int family, char *string)
|
||||||
memcpy(save, string, cp - string);
|
memcpy(save, string, cp - string);
|
||||||
save[cp - string] = '\0';
|
save[cp - string] = '\0';
|
||||||
string = save;
|
string = save;
|
||||||
if (bitlen > maxbitlen)
|
if (/* bitlen < 0 || */ bitlen > maxbitlen)
|
||||||
bitlen = maxbitlen;
|
bitlen = maxbitlen;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
bitlen = maxbitlen;
|
bitlen = maxbitlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,6 +313,7 @@ ascii2prefix (int family, char *string)
|
||||||
return (New_Prefix(AF_INET, &sin, bitlen));
|
return (New_Prefix(AF_INET, &sin, bitlen));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
else if (family == AF_INET6) {
|
else if (family == AF_INET6) {
|
||||||
// Get rid of this with next IPv6 upgrade
|
// Get rid of this with next IPv6 upgrade
|
||||||
#if defined(NT) && !defined(HAVE_INET_NTOP)
|
#if defined(NT) && !defined(HAVE_INET_NTOP)
|
||||||
|
@ -358,12 +325,12 @@ ascii2prefix (int family, char *string)
|
||||||
#endif /* NT */
|
#endif /* NT */
|
||||||
return (New_Prefix(AF_INET6, &sin6, bitlen));
|
return (New_Prefix(AF_INET6, &sin6, bitlen));
|
||||||
}
|
}
|
||||||
|
#endif /* HAVE_IPV6 */
|
||||||
else
|
else
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix_t *
|
prefix_t *Ref_Prefix(prefix_t *prefix)
|
||||||
Ref_Prefix (prefix_t * prefix)
|
|
||||||
{
|
{
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
@ -372,16 +339,17 @@ Ref_Prefix (prefix_t * prefix)
|
||||||
return (New_Prefix2(prefix->family, &prefix->add, prefix->bitlen, NULL));
|
return (New_Prefix2(prefix->family, &prefix->add, prefix->bitlen, NULL));
|
||||||
}
|
}
|
||||||
prefix->ref_count++;
|
prefix->ref_count++;
|
||||||
/* fprintf(stderr, "[A %s, %d]\n", prefix_toa (prefix), prefix->ref_count); */
|
/* fprintf(stderr, "[A %s, %d]\n", prefix_toa (prefix), prefix->ref_count);
|
||||||
|
*/
|
||||||
return (prefix);
|
return (prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void Deref_Prefix(prefix_t *prefix)
|
||||||
Deref_Prefix (prefix_t * prefix)
|
|
||||||
{
|
{
|
||||||
if (prefix == NULL)
|
if (prefix == NULL)
|
||||||
return;
|
return;
|
||||||
/* for secure programming, raise an assert. no static prefix can call this */
|
/* for secure programming, raise an assert. no static prefix can call this
|
||||||
|
*/
|
||||||
assert(prefix->ref_count > 0);
|
assert(prefix->ref_count > 0);
|
||||||
|
|
||||||
prefix->ref_count--;
|
prefix->ref_count--;
|
||||||
|
@ -400,8 +368,7 @@ static int num_active_patricia = 0;
|
||||||
|
|
||||||
/* these routines support continuous mask only */
|
/* these routines support continuous mask only */
|
||||||
|
|
||||||
patricia_tree_t *
|
patricia_tree_t *New_Patricia(int maxbits)
|
||||||
New_Patricia (int maxbits)
|
|
||||||
{
|
{
|
||||||
patricia_tree_t *patricia = calloc(1, sizeof *patricia);
|
patricia_tree_t *patricia = calloc(1, sizeof *patricia);
|
||||||
if (patricia == NULL)
|
if (patricia == NULL)
|
||||||
|
@ -415,14 +382,12 @@ New_Patricia (int maxbits)
|
||||||
return (patricia);
|
return (patricia);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if func is supplied, it will be called as func(node->data)
|
* if func is supplied, it will be called as func(node->data)
|
||||||
* before deleting the node
|
* before deleting the node
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void Clear_Patricia(patricia_tree_t *patricia, data_fn_t func)
|
||||||
Clear_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
|
||||||
{
|
{
|
||||||
assert(patricia);
|
assert(patricia);
|
||||||
if (patricia->head) {
|
if (patricia->head) {
|
||||||
|
@ -439,8 +404,7 @@ Clear_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
||||||
Deref_Prefix(Xrn->prefix);
|
Deref_Prefix(Xrn->prefix);
|
||||||
if (Xrn->data && func)
|
if (Xrn->data && func)
|
||||||
func(Xrn->data);
|
func(Xrn->data);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(Xrn->data == NULL);
|
assert(Xrn->data == NULL);
|
||||||
}
|
}
|
||||||
Delete(Xrn);
|
Delete(Xrn);
|
||||||
|
@ -464,33 +428,30 @@ Clear_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
||||||
/* Delete (patricia); */
|
/* Delete (patricia); */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Destroy_Patricia(patricia_tree_t *patricia, data_fn_t func)
|
||||||
void
|
|
||||||
Destroy_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
|
||||||
{
|
{
|
||||||
Clear_Patricia(patricia, func);
|
Clear_Patricia(patricia, func);
|
||||||
Delete(patricia);
|
Delete(patricia);
|
||||||
num_active_patricia--;
|
num_active_patricia--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* if func is supplied, it will be called as func(node->prefix, node->data)
|
* if func is supplied, it will be called as func(node->prefix, node->data)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void
|
void patricia_process(patricia_tree_t *patricia, prefix_data_fn_t func)
|
||||||
patricia_process (patricia_tree_t *patricia, prefix_data_fn_t func)
|
|
||||||
{
|
{
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
assert(func);
|
assert(func);
|
||||||
|
|
||||||
PATRICIA_WALK (patricia->head, node) {
|
PATRICIA_WALK(patricia->head, node)
|
||||||
|
{
|
||||||
func(node->prefix, node->data);
|
func(node->prefix, node->data);
|
||||||
} PATRICIA_WALK_END;
|
}
|
||||||
|
PATRICIA_WALK_END;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t
|
size_t patricia_walk_inorder(patricia_node_t *node, prefix_data_fn_t func)
|
||||||
patricia_walk_inorder(patricia_node_t *node, prefix_data_fn_t func)
|
|
||||||
{
|
{
|
||||||
size_t n = 0;
|
size_t n = 0;
|
||||||
assert(func);
|
assert(func);
|
||||||
|
@ -511,9 +472,8 @@ patricia_walk_inorder(patricia_node_t *node, prefix_data_fn_t func)
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
patricia_node_t *patricia_search_exact(patricia_tree_t *patricia,
|
||||||
patricia_node_t *
|
prefix_t *prefix)
|
||||||
patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
|
||||||
{
|
{
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
u_char *addr;
|
u_char *addr;
|
||||||
|
@ -538,19 +498,16 @@ patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
fprintf(stderr, "patricia_search_exact: take right %s/%d\n",
|
fprintf(stderr, "patricia_search_exact: take right %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_exact: take right at %u\n",
|
fprintf(stderr, "patricia_search_exact: take right at %u\n", node->bit);
|
||||||
node->bit);
|
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->r;
|
node = node->r;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
if (node->prefix)
|
if (node->prefix)
|
||||||
fprintf(stderr, "patricia_search_exact: take left %s/%d\n",
|
fprintf(stderr, "patricia_search_exact: take left %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_exact: take left at %u\n",
|
fprintf(stderr, "patricia_search_exact: take left at %u\n", node->bit);
|
||||||
node->bit);
|
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->l;
|
node = node->l;
|
||||||
}
|
}
|
||||||
|
@ -581,8 +538,8 @@ patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
int patricia_search_all(patricia_tree_t *patricia, prefix_t *prefix,
|
||||||
patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_t ***list, int *n)
|
patricia_node_t ***list, int *n)
|
||||||
{
|
{
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
patricia_node_t *stack[PATRICIA_MAXBITS + 1];
|
patricia_node_t *stack[PATRICIA_MAXBITS + 1];
|
||||||
|
@ -600,7 +557,7 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
||||||
*n = 0;
|
*n = 0;
|
||||||
|
|
||||||
if (patricia->head == NULL)
|
if (patricia->head == NULL)
|
||||||
return (NULL);
|
return 0;
|
||||||
|
|
||||||
node = patricia->head;
|
node = patricia->head;
|
||||||
addr = prefix_touchar(prefix);
|
addr = prefix_touchar(prefix);
|
||||||
|
@ -622,8 +579,7 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
||||||
fprintf(stderr, "patricia_search_all: take right %s/%d\n",
|
fprintf(stderr, "patricia_search_all: take right %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_all: take right at %d\n",
|
fprintf(stderr, "patricia_search_all: take right at %d\n", node->bit);
|
||||||
node->bit);
|
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->r;
|
node = node->r;
|
||||||
} else {
|
} else {
|
||||||
|
@ -632,8 +588,7 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
||||||
fprintf(stderr, "patricia_search_all: take left %s/%d\n",
|
fprintf(stderr, "patricia_search_all: take left %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_all: take left at %d\n",
|
fprintf(stderr, "patricia_search_all: take left at %d\n", node->bit);
|
||||||
node->bit);
|
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->l;
|
node = node->l;
|
||||||
}
|
}
|
||||||
|
@ -656,9 +611,10 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
|
|
||||||
if (cnt <= 0)
|
if (cnt <= 0)
|
||||||
return false;
|
return 0;
|
||||||
|
|
||||||
// ok, now we have an upper bound of how much we can return. Let's just alloc that...
|
// ok, now we have an upper bound of how much we can return. Let's just alloc
|
||||||
|
// that...
|
||||||
patricia_node_t **outlist = calloc(cnt, sizeof(patricia_node_t *));
|
patricia_node_t **outlist = calloc(cnt, sizeof(patricia_node_t *));
|
||||||
if (outlist == NULL)
|
if (outlist == NULL)
|
||||||
out_of_memory("patricia/patricia_search_all: unable to allocate memory");
|
out_of_memory("patricia/patricia_search_all: unable to allocate memory");
|
||||||
|
@ -669,7 +625,8 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
||||||
fprintf(stderr, "patricia_search_all: pop %s/%d\n",
|
fprintf(stderr, "patricia_search_all: pop %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
if (comp_with_mask (prefix_tochar (node->prefix), prefix_tochar (prefix), node->prefix->bitlen)) {
|
if (comp_with_mask(prefix_tochar(node->prefix), prefix_tochar(prefix),
|
||||||
|
node->prefix->bitlen)) {
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
fprintf(stderr, "patricia_search_all: found %s/%d\n",
|
fprintf(stderr, "patricia_search_all: found %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
|
@ -679,13 +636,12 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*list = outlist;
|
*list = outlist;
|
||||||
return (*n == 0);
|
return *n != 0 ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* if inclusive != 0, "best" may be the given prefix itself */
|
/* if inclusive != 0, "best" may be the given prefix itself */
|
||||||
patricia_node_t *
|
patricia_node_t *patricia_search_best2(patricia_tree_t *patricia,
|
||||||
patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusive)
|
prefix_t *prefix, int inclusive)
|
||||||
{
|
{
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
patricia_node_t *stack[PATRICIA_MAXBITS + 1];
|
patricia_node_t *stack[PATRICIA_MAXBITS + 1];
|
||||||
|
@ -720,19 +676,16 @@ patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusiv
|
||||||
fprintf(stderr, "patricia_search_best: take right %s/%d\n",
|
fprintf(stderr, "patricia_search_best: take right %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_best: take right at %u\n",
|
fprintf(stderr, "patricia_search_best: take right at %u\n", node->bit);
|
||||||
node->bit);
|
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->r;
|
node = node->r;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
if (node->prefix)
|
if (node->prefix)
|
||||||
fprintf(stderr, "patricia_search_best: take left %s/%d\n",
|
fprintf(stderr, "patricia_search_best: take left %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_best: take left at %u\n",
|
fprintf(stderr, "patricia_search_best: take left at %u\n", node->bit);
|
||||||
node->bit);
|
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->l;
|
node = node->l;
|
||||||
}
|
}
|
||||||
|
@ -763,9 +716,9 @@ patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusiv
|
||||||
fprintf(stderr, "patricia_search_best: pop %s/%d\n",
|
fprintf(stderr, "patricia_search_best: pop %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
if (comp_with_mask (prefix_tochar (node->prefix),
|
if (comp_with_mask(prefix_tochar(node->prefix), prefix_tochar(prefix),
|
||||||
prefix_tochar (prefix),
|
node->prefix->bitlen) &&
|
||||||
node->prefix->bitlen) && node->prefix->bitlen <= bitlen) {
|
node->prefix->bitlen <= bitlen) {
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
fprintf(stderr, "patricia_search_best: found %s/%d\n",
|
fprintf(stderr, "patricia_search_best: found %s/%d\n",
|
||||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||||
|
@ -776,16 +729,13 @@ patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusiv
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
patricia_node_t *patricia_search_best(patricia_tree_t *patricia,
|
||||||
patricia_node_t *
|
prefix_t *prefix)
|
||||||
patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix)
|
|
||||||
{
|
{
|
||||||
return (patricia_search_best2(patricia, prefix, 1));
|
return (patricia_search_best2(patricia, prefix, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
patricia_node_t *patricia_lookup(patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
patricia_node_t *
|
|
||||||
patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|
||||||
{
|
{
|
||||||
patricia_node_t *node, *new_node, *parent, *glue;
|
patricia_node_t *node, *new_node, *parent, *glue;
|
||||||
u_char *addr, *test_addr;
|
u_char *addr, *test_addr;
|
||||||
|
@ -800,7 +750,6 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
node = calloc(1, sizeof *node);
|
node = calloc(1, sizeof *node);
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
||||||
|
|
||||||
node->bit = prefix->bitlen;
|
node->bit = prefix->bitlen;
|
||||||
node->prefix = Ref_Prefix(prefix);
|
node->prefix = Ref_Prefix(prefix);
|
||||||
node->parent = NULL;
|
node->parent = NULL;
|
||||||
|
@ -833,8 +782,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
fprintf(stderr, "patricia_lookup: take right at %u\n", node->bit);
|
fprintf(stderr, "patricia_lookup: take right at %u\n", node->bit);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
node = node->r;
|
node = node->r;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
if (node->l == NULL)
|
if (node->l == NULL)
|
||||||
break;
|
break;
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
|
@ -852,8 +800,8 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
|
|
||||||
assert(node->prefix);
|
assert(node->prefix);
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
fprintf (stderr, "patricia_lookup: stop at %s/%d\n",
|
fprintf(stderr, "patricia_lookup: stop at %s/%d\n", prefix_toa(node->prefix),
|
||||||
prefix_toa (node->prefix), node->prefix->bitlen);
|
node->prefix->bitlen);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
|
|
||||||
test_addr = prefix_touchar(node->prefix);
|
test_addr = prefix_touchar(node->prefix);
|
||||||
|
@ -914,7 +862,6 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
new_node = calloc(1, sizeof *new_node);
|
new_node = calloc(1, sizeof *new_node);
|
||||||
if (new_node == NULL)
|
if (new_node == NULL)
|
||||||
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
||||||
|
|
||||||
new_node->bit = prefix->bitlen;
|
new_node->bit = prefix->bitlen;
|
||||||
new_node->prefix = Ref_Prefix(prefix);
|
new_node->prefix = Ref_Prefix(prefix);
|
||||||
new_node->parent = NULL;
|
new_node->parent = NULL;
|
||||||
|
@ -928,8 +875,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) {
|
BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) {
|
||||||
assert(node->r == NULL);
|
assert(node->r == NULL);
|
||||||
node->r = new_node;
|
node->r = new_node;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(node->l == NULL);
|
assert(node->l == NULL);
|
||||||
node->l = new_node;
|
node->l = new_node;
|
||||||
}
|
}
|
||||||
|
@ -944,19 +890,16 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
if (bitlen < patricia->maxbits &&
|
if (bitlen < patricia->maxbits &&
|
||||||
BIT_TEST(test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) {
|
BIT_TEST(test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) {
|
||||||
new_node->r = node;
|
new_node->r = node;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
new_node->l = node;
|
new_node->l = node;
|
||||||
}
|
}
|
||||||
new_node->parent = node->parent;
|
new_node->parent = node->parent;
|
||||||
if (node->parent == NULL) {
|
if (node->parent == NULL) {
|
||||||
assert(patricia->head == node);
|
assert(patricia->head == node);
|
||||||
patricia->head = new_node;
|
patricia->head = new_node;
|
||||||
}
|
} else if (node->parent->r == node) {
|
||||||
else if (node->parent->r == node) {
|
|
||||||
node->parent->r = new_node;
|
node->parent->r = new_node;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
node->parent->l = new_node;
|
node->parent->l = new_node;
|
||||||
}
|
}
|
||||||
node->parent = new_node;
|
node->parent = new_node;
|
||||||
|
@ -964,12 +907,10 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
fprintf(stderr, "patricia_lookup: new_node #3 %s/%d (parent)\n",
|
fprintf(stderr, "patricia_lookup: new_node #3 %s/%d (parent)\n",
|
||||||
prefix_toa(prefix), prefix->bitlen);
|
prefix_toa(prefix), prefix->bitlen);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
glue = calloc(1, sizeof *glue);
|
glue = calloc(1, sizeof *glue);
|
||||||
if (glue == NULL)
|
if (glue == NULL)
|
||||||
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
||||||
|
|
||||||
glue->bit = differ_bit;
|
glue->bit = differ_bit;
|
||||||
glue->prefix = NULL;
|
glue->prefix = NULL;
|
||||||
glue->parent = node->parent;
|
glue->parent = node->parent;
|
||||||
|
@ -979,8 +920,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
BIT_TEST(addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07))) {
|
BIT_TEST(addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07))) {
|
||||||
glue->r = new_node;
|
glue->r = new_node;
|
||||||
glue->l = node;
|
glue->l = node;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
glue->r = node;
|
glue->r = node;
|
||||||
glue->l = new_node;
|
glue->l = new_node;
|
||||||
}
|
}
|
||||||
|
@ -989,11 +929,9 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
if (node->parent == NULL) {
|
if (node->parent == NULL) {
|
||||||
assert(patricia->head == node);
|
assert(patricia->head == node);
|
||||||
patricia->head = glue;
|
patricia->head = glue;
|
||||||
}
|
} else if (node->parent->r == node) {
|
||||||
else if (node->parent->r == node) {
|
|
||||||
node->parent->r = glue;
|
node->parent->r = glue;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
node->parent->l = glue;
|
node->parent->l = glue;
|
||||||
}
|
}
|
||||||
node->parent = glue;
|
node->parent = glue;
|
||||||
|
@ -1005,9 +943,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
||||||
return (new_node);
|
return (new_node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void patricia_remove(patricia_tree_t *patricia, patricia_node_t *node)
|
||||||
void
|
|
||||||
patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|
||||||
{
|
{
|
||||||
patricia_node_t *parent, *child;
|
patricia_node_t *parent, *child;
|
||||||
|
|
||||||
|
@ -1049,8 +985,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
||||||
if (parent->r == node) {
|
if (parent->r == node) {
|
||||||
parent->r = NULL;
|
parent->r = NULL;
|
||||||
child = parent->l;
|
child = parent->l;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(parent->l == node);
|
assert(parent->l == node);
|
||||||
parent->l = NULL;
|
parent->l = NULL;
|
||||||
child = parent->r;
|
child = parent->r;
|
||||||
|
@ -1064,11 +999,9 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
||||||
if (parent->parent == NULL) {
|
if (parent->parent == NULL) {
|
||||||
assert(patricia->head == parent);
|
assert(patricia->head == parent);
|
||||||
patricia->head = child;
|
patricia->head = child;
|
||||||
}
|
} else if (parent->parent->r == parent) {
|
||||||
else if (parent->parent->r == parent) {
|
|
||||||
parent->parent->r = child;
|
parent->parent->r = child;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(parent->parent->l == parent);
|
assert(parent->parent->l == parent);
|
||||||
parent->parent->l = child;
|
parent->parent->l = child;
|
||||||
}
|
}
|
||||||
|
@ -1084,8 +1017,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
if (node->r) {
|
if (node->r) {
|
||||||
child = node->r;
|
child = node->r;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(node->l);
|
assert(node->l);
|
||||||
child = node->l;
|
child = node->l;
|
||||||
}
|
}
|
||||||
|
@ -1104,8 +1036,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
||||||
|
|
||||||
if (parent->r == node) {
|
if (parent->r == node) {
|
||||||
parent->r = child;
|
parent->r = child;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
assert(parent->l == node);
|
assert(parent->l == node);
|
||||||
parent->l = child;
|
parent->l = child;
|
||||||
}
|
}
|
||||||
|
@ -1113,8 +1044,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
||||||
|
|
||||||
/* { from demo.c */
|
/* { from demo.c */
|
||||||
|
|
||||||
patricia_node_t *
|
patricia_node_t *make_and_lookup(patricia_tree_t *tree, char *string)
|
||||||
make_and_lookup (patricia_tree_t *tree, char *string)
|
|
||||||
{
|
{
|
||||||
prefix_t *prefix;
|
prefix_t *prefix;
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
|
@ -1126,8 +1056,7 @@ make_and_lookup (patricia_tree_t *tree, char *string)
|
||||||
return (node);
|
return (node);
|
||||||
}
|
}
|
||||||
|
|
||||||
patricia_node_t *
|
patricia_node_t *try_search_exact(patricia_tree_t *tree, char *string)
|
||||||
try_search_exact (patricia_tree_t *tree, char *string)
|
|
||||||
{
|
{
|
||||||
prefix_t *prefix;
|
prefix_t *prefix;
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
|
@ -1136,17 +1065,15 @@ try_search_exact (patricia_tree_t *tree, char *string)
|
||||||
printf("try_search_exact: %s/%d\n", prefix_toa(prefix), prefix->bitlen);
|
printf("try_search_exact: %s/%d\n", prefix_toa(prefix), prefix->bitlen);
|
||||||
if ((node = patricia_search_exact(tree, prefix)) == NULL) {
|
if ((node = patricia_search_exact(tree, prefix)) == NULL) {
|
||||||
printf("try_search_exact: not found\n");
|
printf("try_search_exact: not found\n");
|
||||||
}
|
} else {
|
||||||
else {
|
printf("try_search_exact: %s/%d found\n", prefix_toa(node->prefix),
|
||||||
printf ("try_search_exact: %s/%d found\n",
|
node->prefix->bitlen);
|
||||||
prefix_toa (node->prefix), node->prefix->bitlen);
|
|
||||||
}
|
}
|
||||||
Deref_Prefix(prefix);
|
Deref_Prefix(prefix);
|
||||||
return (node);
|
return (node);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void lookup_then_remove(patricia_tree_t *tree, char *string)
|
||||||
lookup_then_remove (patricia_tree_t *tree, char *string)
|
|
||||||
{
|
{
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
|
|
||||||
|
@ -1154,8 +1081,7 @@ lookup_then_remove (patricia_tree_t *tree, char *string)
|
||||||
patricia_remove(tree, node);
|
patricia_remove(tree, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
patricia_node_t *
|
patricia_node_t *try_search_best(patricia_tree_t *tree, char *string)
|
||||||
try_search_best (patricia_tree_t *tree, char *string)
|
|
||||||
{
|
{
|
||||||
prefix_t *prefix;
|
prefix_t *prefix;
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
|
@ -1165,8 +1091,8 @@ try_search_best (patricia_tree_t *tree, char *string)
|
||||||
if ((node = patricia_search_best(tree, prefix)) == NULL)
|
if ((node = patricia_search_best(tree, prefix)) == NULL)
|
||||||
printf("try_search_best: not found\n");
|
printf("try_search_best: not found\n");
|
||||||
else
|
else
|
||||||
printf ("try_search_best: %s/%d found\n",
|
printf("try_search_best: %s/%d found\n", prefix_toa(node->prefix),
|
||||||
prefix_toa (node->prefix), node->prefix->bitlen);
|
node->prefix->bitlen);
|
||||||
Deref_Prefix(prefix);
|
Deref_Prefix(prefix);
|
||||||
return (node);
|
return (node);
|
||||||
}
|
}
|
||||||
|
|
70
src/3rdparty/patricia.h
vendored
70
src/3rdparty/patricia.h
vendored
|
@ -1,16 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* This code originates from Dave Plonka's Net::Security perl module. An adaptation
|
* $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $
|
||||||
* of it in C is kept at https://github.com/CAIDA/cc-common/tree/master/libpatricia.
|
|
||||||
* That repository is considered the upstream version for Zeek's fork. We make some
|
|
||||||
* custom changes to this upstream:
|
|
||||||
* - Replace void_fn_t with data_fn_t and prefix_data_fn_t
|
|
||||||
* - Add patricia_search_all method
|
|
||||||
*
|
|
||||||
* The current version is based on commit 4a2c61374f507a420d28bd9084c976142d279605
|
|
||||||
* from that repo.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Dave Plonka <plonka@doit.wisc.edu>
|
* Dave Plonka <plonka@doit.wisc.edu>
|
||||||
*
|
*
|
||||||
* This product includes software developed by the University of Michigan,
|
* This product includes software developed by the University of Michigan,
|
||||||
|
@ -23,6 +12,19 @@
|
||||||
* some other things it could be used as a standalone API.
|
* some other things it could be used as a standalone API.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This code originates from Dave Plonka's Net::Security perl module. An
|
||||||
|
* adaptation of it in C is kept at
|
||||||
|
* https://github.com/CAIDA/cc-common/tree/master/libpatricia. That repository
|
||||||
|
* is considered the upstream version for Zeek's fork. We make some custom
|
||||||
|
* changes to this upstream:
|
||||||
|
* - Replace void_fn_t with data_fn_t and prefix_data_fn_t
|
||||||
|
* - Add patricia_search_all method
|
||||||
|
*
|
||||||
|
* The current version is based on commit
|
||||||
|
* fd262ab5ac5bae8b0d4a8b5e2e723115b1846376 from that repo.
|
||||||
|
*/
|
||||||
|
|
||||||
/* From copyright.txt:
|
/* From copyright.txt:
|
||||||
*
|
*
|
||||||
* Copyright (c) 1997, 1998, 1999
|
* Copyright (c) 1997, 1998, 1999
|
||||||
|
@ -53,14 +55,19 @@
|
||||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
* 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
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#ifndef _PATRICIA_H
|
||||||
|
#define _PATRICIA_H
|
||||||
|
|
||||||
|
#define HAVE_IPV6
|
||||||
|
|
||||||
|
/* typedef unsigned int u_int; */
|
||||||
|
typedef void (*void_fn_t)();
|
||||||
/* { from defs.h */
|
/* { from defs.h */
|
||||||
#define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
|
#define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
|
||||||
#define MAXLINE 1024
|
#define MAXLINE 1024
|
||||||
|
@ -96,7 +103,9 @@ typedef struct _prefix_t {
|
||||||
int ref_count; /* reference count */
|
int ref_count; /* reference count */
|
||||||
union {
|
union {
|
||||||
struct in_addr sin;
|
struct in_addr sin;
|
||||||
|
#ifdef HAVE_IPV6
|
||||||
struct in6_addr sin6;
|
struct in6_addr sin6;
|
||||||
|
#endif /* IPV6 */
|
||||||
} add;
|
} add;
|
||||||
} prefix_t;
|
} prefix_t;
|
||||||
|
|
||||||
|
@ -120,12 +129,14 @@ typedef struct _patricia_tree_t {
|
||||||
int num_active_node; /* for debug purpose */
|
int num_active_node; /* for debug purpose */
|
||||||
} patricia_tree_t;
|
} patricia_tree_t;
|
||||||
|
|
||||||
|
patricia_node_t *patricia_search_exact(patricia_tree_t *patricia,
|
||||||
patricia_node_t *patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix);
|
prefix_t *prefix);
|
||||||
bool patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_t ***list, int *n);
|
int patricia_search_all(patricia_tree_t *patricia, prefix_t *prefix,
|
||||||
patricia_node_t *patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix);
|
patricia_node_t ***list, int *n);
|
||||||
patricia_node_t * patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix,
|
patricia_node_t *patricia_search_best(patricia_tree_t *patricia,
|
||||||
int inclusive);
|
prefix_t *prefix);
|
||||||
|
patricia_node_t *patricia_search_best2(patricia_tree_t *patricia,
|
||||||
|
prefix_t *prefix, int inclusive);
|
||||||
patricia_node_t *patricia_lookup(patricia_tree_t *patricia, prefix_t *prefix);
|
patricia_node_t *patricia_lookup(patricia_tree_t *patricia, prefix_t *prefix);
|
||||||
void patricia_remove(patricia_tree_t *patricia, patricia_node_t *node);
|
void patricia_remove(patricia_tree_t *patricia, patricia_node_t *node);
|
||||||
patricia_tree_t *New_Patricia(int maxbits);
|
patricia_tree_t *New_Patricia(int maxbits);
|
||||||
|
@ -139,11 +150,9 @@ char *prefix_toa (prefix_t * prefix);
|
||||||
|
|
||||||
/* { from demo.c */
|
/* { from demo.c */
|
||||||
|
|
||||||
prefix_t *
|
prefix_t *ascii2prefix(int family, char *string);
|
||||||
ascii2prefix (int family, char *string);
|
|
||||||
|
|
||||||
patricia_node_t *
|
patricia_node_t *make_and_lookup(patricia_tree_t *tree, char *string);
|
||||||
make_and_lookup (patricia_tree_t *tree, char *string);
|
|
||||||
|
|
||||||
/* } */
|
/* } */
|
||||||
|
|
||||||
|
@ -170,13 +179,15 @@ do { \
|
||||||
while ((Xnode = Xrn)) { \
|
while ((Xnode = Xrn)) { \
|
||||||
if (1)
|
if (1)
|
||||||
|
|
||||||
#define PATRICIA_WALK_BREAK { \
|
#define PATRICIA_WALK_BREAK \
|
||||||
|
{ \
|
||||||
if (Xsp != Xstack) { \
|
if (Xsp != Xstack) { \
|
||||||
Xrn = *(--Xsp); \
|
Xrn = *(--Xsp); \
|
||||||
} else { \
|
} else { \
|
||||||
Xrn = (patricia_node_t *)0; \
|
Xrn = (patricia_node_t *)0; \
|
||||||
} \
|
} \
|
||||||
continue; }
|
continue; \
|
||||||
|
}
|
||||||
|
|
||||||
#define PATRICIA_WALK_END \
|
#define PATRICIA_WALK_END \
|
||||||
if (Xrn->l) { \
|
if (Xrn->l) { \
|
||||||
|
@ -192,4 +203,7 @@ do { \
|
||||||
Xrn = (patricia_node_t *)0; \
|
Xrn = (patricia_node_t *)0; \
|
||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
} while (0)
|
} \
|
||||||
|
while (0)
|
||||||
|
|
||||||
|
#endif /* _PATRICIA_H */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue