mirror of
https://github.com/zeek/zeek.git
synced 2025-10-01 22:28: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
|
||||
* 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.
|
||||
*/
|
||||
/*
|
||||
* $Id: patricia.c,v 1.7 2005/12/07 20:46:41 dplonka Exp $
|
||||
* Dave Plonka <plonka@doit.wisc.edu>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/* 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>
|
||||
*
|
||||
*
|
||||
* 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.
|
||||
* Added patricia_search_all function.
|
||||
*/
|
||||
|
||||
#ifndef UNUSED
|
||||
|
@ -73,23 +40,22 @@
|
|||
#endif
|
||||
#endif
|
||||
|
||||
UNUSED static char copyright[] =
|
||||
"This product includes software developed by the University of Michigan, Merit"
|
||||
UNUSED static char copyright[] = "This product includes software developed by "
|
||||
"the University of Michigan, Merit"
|
||||
"Network, Inc., and their contributors.";
|
||||
|
||||
#include <arpa/inet.h> /* BSD, Linux, Solaris: for inet_addr */
|
||||
#include <assert.h> /* assert */
|
||||
#include <ctype.h> /* isdigit */
|
||||
#include <errno.h> /* errno */
|
||||
#include <math.h> /* sin */
|
||||
#include <netinet/in.h> /* BSD, Linux: for inet_addr */
|
||||
#include <stddef.h> /* NULL */
|
||||
#include <stdio.h> /* sprintf, fprintf, stderr */
|
||||
#include <stdlib.h> /* free, atol, calloc */
|
||||
#include <string.h> /* memcpy, strchr, strlen */
|
||||
#include <sys/types.h> /* BSD: for inet_addr */
|
||||
#include <sys/socket.h> /* BSD, Linux: for inet_addr */
|
||||
#include <netinet/in.h> /* BSD, Linux: for inet_addr */
|
||||
#include <arpa/inet.h> /* BSD, Linux, Solaris: for inet_addr */
|
||||
#include <stdbool.h>
|
||||
#include <sys/types.h> /* BSD: for inet_addr */
|
||||
|
||||
#include "patricia.h"
|
||||
|
||||
|
@ -103,8 +69,7 @@ extern void out_of_memory(const char* where);
|
|||
/* prefix_tochar
|
||||
* convert prefix information to bytes
|
||||
*/
|
||||
u_char *
|
||||
prefix_tochar (prefix_t * prefix)
|
||||
u_char *prefix_tochar(prefix_t *prefix)
|
||||
{
|
||||
if (prefix == NULL)
|
||||
return (NULL);
|
||||
|
@ -112,8 +77,7 @@ prefix_tochar (prefix_t * prefix)
|
|||
return ((u_char *)&prefix->add.sin);
|
||||
}
|
||||
|
||||
int
|
||||
comp_with_mask (void *addr, void *dest, u_int mask)
|
||||
int comp_with_mask(void *addr, void *dest, u_int mask)
|
||||
{
|
||||
|
||||
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 */
|
||||
int
|
||||
my_inet_pton (int af, const char *src, void *dst)
|
||||
int my_inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
if (af == AF_INET) {
|
||||
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));
|
||||
return (1);
|
||||
#ifdef HAVE_IPV6
|
||||
} else if (af == AF_INET6) {
|
||||
return (inet_pton(af, src, dst));
|
||||
#endif /* HAVE_IPV6 */
|
||||
} else {
|
||||
#ifndef NT
|
||||
errno = EAFNOSUPPORT;
|
||||
|
@ -171,8 +136,7 @@ my_inet_pton (int af, const char *src, void *dst)
|
|||
* convert prefix information to ascii string with length
|
||||
* thread safe and (almost) re-entrant implementation
|
||||
*/
|
||||
char *
|
||||
prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
||||
char *prefix_toa2x(prefix_t *prefix, char *buff, int with_len)
|
||||
{
|
||||
if (prefix == 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);
|
||||
a = prefix_touchar(prefix);
|
||||
if (with_len) {
|
||||
sprintf (buff, "%d.%d.%d.%d/%d", a[0], a[1], a[2], a[3],
|
||||
prefix->bitlen);
|
||||
}
|
||||
else {
|
||||
sprintf(buff, "%d.%d.%d.%d/%d", a[0], a[1], a[2], a[3], prefix->bitlen);
|
||||
} else {
|
||||
sprintf(buff, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
|
||||
}
|
||||
return (buff);
|
||||
}
|
||||
#ifdef HAVE_IPV6
|
||||
else if (prefix->family == AF_INET6) {
|
||||
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) {
|
||||
assert(prefix->bitlen <= sizeof(struct in6_addr) * 8);
|
||||
sprintf(buff + strlen(buff), "/%d", prefix->bitlen);
|
||||
}
|
||||
return (buff);
|
||||
}
|
||||
#endif /* HAVE_IPV6 */
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
@ -228,42 +193,39 @@ prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
|||
/* prefix_toa2
|
||||
* convert prefix information to ascii string
|
||||
*/
|
||||
char *
|
||||
prefix_toa2 (prefix_t *prefix, char *buff)
|
||||
char *prefix_toa2(prefix_t *prefix, char *buff)
|
||||
{
|
||||
return (prefix_toa2x(prefix, buff, 0));
|
||||
}
|
||||
|
||||
/* prefix_toa
|
||||
*/
|
||||
char *
|
||||
prefix_toa (prefix_t * prefix)
|
||||
char *prefix_toa(prefix_t *prefix)
|
||||
{
|
||||
return (prefix_toa2(prefix, (char *)NULL));
|
||||
}
|
||||
|
||||
prefix_t *
|
||||
New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
||||
prefix_t *New_Prefix2(int family, void *dest, int bitlen, prefix_t *prefix)
|
||||
{
|
||||
int dynamic_allocated = 0;
|
||||
int default_bitlen = sizeof(struct in_addr) * 8;
|
||||
|
||||
#ifdef HAVE_IPV6
|
||||
if (family == AF_INET6) {
|
||||
default_bitlen = sizeof(struct in6_addr) * 8;
|
||||
if (prefix == NULL) {
|
||||
prefix = calloc(1, sizeof(prefix_t));
|
||||
if (prefix == NULL)
|
||||
out_of_memory("patricia/new_prefix2: unable to allocate memory");
|
||||
|
||||
dynamic_allocated++;
|
||||
}
|
||||
memcpy(&prefix->add.sin6, dest, sizeof(struct in6_addr));
|
||||
}
|
||||
else
|
||||
} else
|
||||
#endif /* HAVE_IPV6 */
|
||||
if (family == AF_INET) {
|
||||
if (prefix == NULL) {
|
||||
#ifndef NT
|
||||
prefix = calloc(1, sizeof (prefix_t));
|
||||
prefix = calloc(1, sizeof(prefix4_t));
|
||||
if (prefix == NULL)
|
||||
out_of_memory("patricia/new_prefix2: unable to allocate memory");
|
||||
#else
|
||||
|
@ -277,8 +239,7 @@ New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
|||
dynamic_allocated++;
|
||||
}
|
||||
memcpy(&prefix->add.sin, dest, sizeof(struct in_addr));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
|
@ -288,25 +249,26 @@ New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
|||
if (dynamic_allocated) {
|
||||
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);
|
||||
}
|
||||
|
||||
prefix_t *
|
||||
New_Prefix (int family, void *dest, int bitlen)
|
||||
prefix_t *New_Prefix(int family, void *dest, int bitlen)
|
||||
{
|
||||
return (New_Prefix2(family, dest, bitlen, NULL));
|
||||
}
|
||||
|
||||
/* ascii2prefix
|
||||
*/
|
||||
prefix_t *
|
||||
ascii2prefix (int family, char *string)
|
||||
prefix_t *ascii2prefix(int family, char *string)
|
||||
{
|
||||
u_long bitlen, maxbitlen = 0;
|
||||
char *cp;
|
||||
struct in_addr sin;
|
||||
#ifdef HAVE_IPV6
|
||||
struct in6_addr sin6;
|
||||
#endif /* HAVE_IPV6 */
|
||||
int result;
|
||||
char save[MAXLINE];
|
||||
|
||||
|
@ -316,15 +278,20 @@ ascii2prefix (int family, char *string)
|
|||
/* easy way to handle both families */
|
||||
if (family == 0) {
|
||||
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) {
|
||||
maxbitlen = sizeof(struct in_addr) * 8;
|
||||
}
|
||||
#ifdef HAVE_IPV6
|
||||
else if (family == AF_INET6) {
|
||||
maxbitlen = sizeof(struct in6_addr) * 8;
|
||||
}
|
||||
#endif /* HAVE_IPV6 */
|
||||
|
||||
if ((cp = strchr(string, '/')) != NULL) {
|
||||
bitlen = atol(cp + 1);
|
||||
|
@ -334,10 +301,9 @@ ascii2prefix (int family, char *string)
|
|||
memcpy(save, string, cp - string);
|
||||
save[cp - string] = '\0';
|
||||
string = save;
|
||||
if (bitlen > maxbitlen)
|
||||
if (/* bitlen < 0 || */ bitlen > maxbitlen)
|
||||
bitlen = maxbitlen;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
bitlen = maxbitlen;
|
||||
}
|
||||
|
||||
|
@ -347,6 +313,7 @@ ascii2prefix (int family, char *string)
|
|||
return (New_Prefix(AF_INET, &sin, bitlen));
|
||||
}
|
||||
|
||||
#ifdef HAVE_IPV6
|
||||
else if (family == AF_INET6) {
|
||||
// Get rid of this with next IPv6 upgrade
|
||||
#if defined(NT) && !defined(HAVE_INET_NTOP)
|
||||
|
@ -358,12 +325,12 @@ ascii2prefix (int family, char *string)
|
|||
#endif /* NT */
|
||||
return (New_Prefix(AF_INET6, &sin6, bitlen));
|
||||
}
|
||||
#endif /* HAVE_IPV6 */
|
||||
else
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
prefix_t *
|
||||
Ref_Prefix (prefix_t * prefix)
|
||||
prefix_t *Ref_Prefix(prefix_t *prefix)
|
||||
{
|
||||
if (prefix == NULL)
|
||||
return (NULL);
|
||||
|
@ -372,16 +339,17 @@ Ref_Prefix (prefix_t * prefix)
|
|||
return (New_Prefix2(prefix->family, &prefix->add, prefix->bitlen, NULL));
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
void
|
||||
Deref_Prefix (prefix_t * prefix)
|
||||
void Deref_Prefix(prefix_t *prefix)
|
||||
{
|
||||
if (prefix == NULL)
|
||||
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);
|
||||
|
||||
prefix->ref_count--;
|
||||
|
@ -400,8 +368,7 @@ static int num_active_patricia = 0;
|
|||
|
||||
/* these routines support continuous mask only */
|
||||
|
||||
patricia_tree_t *
|
||||
New_Patricia (int maxbits)
|
||||
patricia_tree_t *New_Patricia(int maxbits)
|
||||
{
|
||||
patricia_tree_t *patricia = calloc(1, sizeof *patricia);
|
||||
if (patricia == NULL)
|
||||
|
@ -415,14 +382,12 @@ New_Patricia (int maxbits)
|
|||
return (patricia);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* if func is supplied, it will be called as func(node->data)
|
||||
* before deleting the node
|
||||
*/
|
||||
|
||||
void
|
||||
Clear_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
||||
void Clear_Patricia(patricia_tree_t *patricia, data_fn_t func)
|
||||
{
|
||||
assert(patricia);
|
||||
if (patricia->head) {
|
||||
|
@ -439,8 +404,7 @@ Clear_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
|||
Deref_Prefix(Xrn->prefix);
|
||||
if (Xrn->data && func)
|
||||
func(Xrn->data);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assert(Xrn->data == NULL);
|
||||
}
|
||||
Delete(Xrn);
|
||||
|
@ -464,33 +428,30 @@ Clear_Patricia (patricia_tree_t *patricia, data_fn_t func)
|
|||
/* 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);
|
||||
Delete(patricia);
|
||||
num_active_patricia--;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* if func is supplied, it will be called as func(node->prefix, node->data)
|
||||
*/
|
||||
|
||||
void
|
||||
patricia_process (patricia_tree_t *patricia, prefix_data_fn_t func)
|
||||
void patricia_process(patricia_tree_t *patricia, prefix_data_fn_t func)
|
||||
{
|
||||
patricia_node_t *node;
|
||||
assert(func);
|
||||
|
||||
PATRICIA_WALK (patricia->head, node) {
|
||||
PATRICIA_WALK(patricia->head, node)
|
||||
{
|
||||
func(node->prefix, node->data);
|
||||
} PATRICIA_WALK_END;
|
||||
}
|
||||
PATRICIA_WALK_END;
|
||||
}
|
||||
|
||||
size_t
|
||||
patricia_walk_inorder(patricia_node_t *node, prefix_data_fn_t func)
|
||||
size_t patricia_walk_inorder(patricia_node_t *node, prefix_data_fn_t func)
|
||||
{
|
||||
size_t n = 0;
|
||||
assert(func);
|
||||
|
@ -511,9 +472,8 @@ patricia_walk_inorder(patricia_node_t *node, prefix_data_fn_t func)
|
|||
return n;
|
||||
}
|
||||
|
||||
|
||||
patricia_node_t *
|
||||
patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
||||
patricia_node_t *patricia_search_exact(patricia_tree_t *patricia,
|
||||
prefix_t *prefix)
|
||||
{
|
||||
patricia_node_t *node;
|
||||
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",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
else
|
||||
fprintf (stderr, "patricia_search_exact: take right at %u\n",
|
||||
node->bit);
|
||||
fprintf(stderr, "patricia_search_exact: take right at %u\n", node->bit);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
node = node->r;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
#ifdef PATRICIA_DEBUG
|
||||
if (node->prefix)
|
||||
fprintf(stderr, "patricia_search_exact: take left %s/%d\n",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
else
|
||||
fprintf (stderr, "patricia_search_exact: take left at %u\n",
|
||||
node->bit);
|
||||
fprintf(stderr, "patricia_search_exact: take left at %u\n", node->bit);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
node = node->l;
|
||||
}
|
||||
|
@ -581,8 +538,8 @@ patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
|||
return (NULL);
|
||||
}
|
||||
|
||||
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 ***list, int *n)
|
||||
{
|
||||
patricia_node_t *node;
|
||||
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;
|
||||
|
||||
if (patricia->head == NULL)
|
||||
return (NULL);
|
||||
return 0;
|
||||
|
||||
node = patricia->head;
|
||||
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",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
else
|
||||
fprintf (stderr, "patricia_search_all: take right at %d\n",
|
||||
node->bit);
|
||||
fprintf(stderr, "patricia_search_all: take right at %d\n", node->bit);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
node = node->r;
|
||||
} 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",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
else
|
||||
fprintf (stderr, "patricia_search_all: take left at %d\n",
|
||||
node->bit);
|
||||
fprintf(stderr, "patricia_search_all: take left at %d\n", node->bit);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
node = node->l;
|
||||
}
|
||||
|
@ -656,9 +611,10 @@ patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_
|
|||
#endif /* PATRICIA_DEBUG */
|
||||
|
||||
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 *));
|
||||
if (outlist == NULL)
|
||||
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",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
#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
|
||||
fprintf(stderr, "patricia_search_all: found %s/%d\n",
|
||||
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;
|
||||
return (*n == 0);
|
||||
return *n != 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
/* if inclusive != 0, "best" may be the given prefix itself */
|
||||
patricia_node_t *
|
||||
patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inclusive)
|
||||
patricia_node_t *patricia_search_best2(patricia_tree_t *patricia,
|
||||
prefix_t *prefix, int inclusive)
|
||||
{
|
||||
patricia_node_t *node;
|
||||
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",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
else
|
||||
fprintf (stderr, "patricia_search_best: take right at %u\n",
|
||||
node->bit);
|
||||
fprintf(stderr, "patricia_search_best: take right at %u\n", node->bit);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
node = node->r;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
#ifdef PATRICIA_DEBUG
|
||||
if (node->prefix)
|
||||
fprintf(stderr, "patricia_search_best: take left %s/%d\n",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
else
|
||||
fprintf (stderr, "patricia_search_best: take left at %u\n",
|
||||
node->bit);
|
||||
fprintf(stderr, "patricia_search_best: take left at %u\n", node->bit);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
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",
|
||||
prefix_toa(node->prefix), node->prefix->bitlen);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
if (comp_with_mask (prefix_tochar (node->prefix),
|
||||
prefix_tochar (prefix),
|
||||
node->prefix->bitlen) && node->prefix->bitlen <= bitlen) {
|
||||
if (comp_with_mask(prefix_tochar(node->prefix), prefix_tochar(prefix),
|
||||
node->prefix->bitlen) &&
|
||||
node->prefix->bitlen <= bitlen) {
|
||||
#ifdef PATRICIA_DEBUG
|
||||
fprintf(stderr, "patricia_search_best: found %s/%d\n",
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
patricia_node_t *
|
||||
patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix)
|
||||
patricia_node_t *patricia_search_best(patricia_tree_t *patricia,
|
||||
prefix_t *prefix)
|
||||
{
|
||||
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;
|
||||
u_char *addr, *test_addr;
|
||||
|
@ -800,7 +750,6 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||
node = calloc(1, sizeof *node);
|
||||
if (node == NULL)
|
||||
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
||||
|
||||
node->bit = prefix->bitlen;
|
||||
node->prefix = Ref_Prefix(prefix);
|
||||
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);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
node = node->r;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (node->l == NULL)
|
||||
break;
|
||||
#ifdef PATRICIA_DEBUG
|
||||
|
@ -852,8 +800,8 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||
|
||||
assert(node->prefix);
|
||||
#ifdef PATRICIA_DEBUG
|
||||
fprintf (stderr, "patricia_lookup: stop at %s/%d\n",
|
||||
prefix_toa (node->prefix), node->prefix->bitlen);
|
||||
fprintf(stderr, "patricia_lookup: stop at %s/%d\n", prefix_toa(node->prefix),
|
||||
node->prefix->bitlen);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
|
||||
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);
|
||||
if (new_node == NULL)
|
||||
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
||||
|
||||
new_node->bit = prefix->bitlen;
|
||||
new_node->prefix = Ref_Prefix(prefix);
|
||||
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))) {
|
||||
assert(node->r == NULL);
|
||||
node->r = new_node;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assert(node->l == NULL);
|
||||
node->l = new_node;
|
||||
}
|
||||
|
@ -944,19 +890,16 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||
if (bitlen < patricia->maxbits &&
|
||||
BIT_TEST(test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) {
|
||||
new_node->r = node;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
new_node->l = node;
|
||||
}
|
||||
new_node->parent = node->parent;
|
||||
if (node->parent == NULL) {
|
||||
assert(patricia->head == node);
|
||||
patricia->head = new_node;
|
||||
}
|
||||
else if (node->parent->r == node) {
|
||||
} else if (node->parent->r == node) {
|
||||
node->parent->r = new_node;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
node->parent->l = 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",
|
||||
prefix_toa(prefix), prefix->bitlen);
|
||||
#endif /* PATRICIA_DEBUG */
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
glue = calloc(1, sizeof *glue);
|
||||
if (glue == NULL)
|
||||
out_of_memory("patricia/patricia_lookup: unable to allocate memory");
|
||||
|
||||
glue->bit = differ_bit;
|
||||
glue->prefix = NULL;
|
||||
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))) {
|
||||
glue->r = new_node;
|
||||
glue->l = node;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
glue->r = node;
|
||||
glue->l = new_node;
|
||||
}
|
||||
|
@ -989,11 +929,9 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||
if (node->parent == NULL) {
|
||||
assert(patricia->head == node);
|
||||
patricia->head = glue;
|
||||
}
|
||||
else if (node->parent->r == node) {
|
||||
} else if (node->parent->r == node) {
|
||||
node->parent->r = glue;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
node->parent->l = glue;
|
||||
}
|
||||
node->parent = glue;
|
||||
|
@ -1005,9 +943,7 @@ patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||
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;
|
||||
|
||||
|
@ -1049,8 +985,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|||
if (parent->r == node) {
|
||||
parent->r = NULL;
|
||||
child = parent->l;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assert(parent->l == node);
|
||||
parent->l = NULL;
|
||||
child = parent->r;
|
||||
|
@ -1064,11 +999,9 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|||
if (parent->parent == NULL) {
|
||||
assert(patricia->head == parent);
|
||||
patricia->head = child;
|
||||
}
|
||||
else if (parent->parent->r == parent) {
|
||||
} else if (parent->parent->r == parent) {
|
||||
parent->parent->r = child;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assert(parent->parent->l == parent);
|
||||
parent->parent->l = child;
|
||||
}
|
||||
|
@ -1084,8 +1017,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|||
#endif /* PATRICIA_DEBUG */
|
||||
if (node->r) {
|
||||
child = node->r;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assert(node->l);
|
||||
child = node->l;
|
||||
}
|
||||
|
@ -1104,8 +1036,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|||
|
||||
if (parent->r == node) {
|
||||
parent->r = child;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
assert(parent->l == node);
|
||||
parent->l = child;
|
||||
}
|
||||
|
@ -1113,8 +1044,7 @@ patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|||
|
||||
/* { from demo.c */
|
||||
|
||||
patricia_node_t *
|
||||
make_and_lookup (patricia_tree_t *tree, char *string)
|
||||
patricia_node_t *make_and_lookup(patricia_tree_t *tree, char *string)
|
||||
{
|
||||
prefix_t *prefix;
|
||||
patricia_node_t *node;
|
||||
|
@ -1126,8 +1056,7 @@ make_and_lookup (patricia_tree_t *tree, char *string)
|
|||
return (node);
|
||||
}
|
||||
|
||||
patricia_node_t *
|
||||
try_search_exact (patricia_tree_t *tree, char *string)
|
||||
patricia_node_t *try_search_exact(patricia_tree_t *tree, char *string)
|
||||
{
|
||||
prefix_t *prefix;
|
||||
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);
|
||||
if ((node = patricia_search_exact(tree, prefix)) == NULL) {
|
||||
printf("try_search_exact: not found\n");
|
||||
}
|
||||
else {
|
||||
printf ("try_search_exact: %s/%d found\n",
|
||||
prefix_toa (node->prefix), node->prefix->bitlen);
|
||||
} else {
|
||||
printf("try_search_exact: %s/%d found\n", prefix_toa(node->prefix),
|
||||
node->prefix->bitlen);
|
||||
}
|
||||
Deref_Prefix(prefix);
|
||||
return (node);
|
||||
}
|
||||
|
||||
void
|
||||
lookup_then_remove (patricia_tree_t *tree, char *string)
|
||||
void lookup_then_remove(patricia_tree_t *tree, char *string)
|
||||
{
|
||||
patricia_node_t *node;
|
||||
|
||||
|
@ -1154,8 +1081,7 @@ lookup_then_remove (patricia_tree_t *tree, char *string)
|
|||
patricia_remove(tree, node);
|
||||
}
|
||||
|
||||
patricia_node_t *
|
||||
try_search_best (patricia_tree_t *tree, char *string)
|
||||
patricia_node_t *try_search_best(patricia_tree_t *tree, char *string)
|
||||
{
|
||||
prefix_t *prefix;
|
||||
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)
|
||||
printf("try_search_best: not found\n");
|
||||
else
|
||||
printf ("try_search_best: %s/%d found\n",
|
||||
prefix_toa (node->prefix), node->prefix->bitlen);
|
||||
printf("try_search_best: %s/%d found\n", prefix_toa(node->prefix),
|
||||
node->prefix->bitlen);
|
||||
Deref_Prefix(prefix);
|
||||
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
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* $Id: patricia.h,v 1.6 2005/12/07 20:53:01 dplonka Exp $
|
||||
* Dave Plonka <plonka@doit.wisc.edu>
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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:
|
||||
*
|
||||
* Copyright (c) 1997, 1998, 1999
|
||||
|
@ -53,14 +55,19 @@
|
|||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#ifndef _PATRICIA_H
|
||||
#define _PATRICIA_H
|
||||
|
||||
#define HAVE_IPV6
|
||||
|
||||
/* typedef unsigned int u_int; */
|
||||
typedef void (*void_fn_t)();
|
||||
/* { from defs.h */
|
||||
#define prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
|
||||
#define MAXLINE 1024
|
||||
|
@ -96,7 +103,9 @@ typedef struct _prefix_t {
|
|||
int ref_count; /* reference count */
|
||||
union {
|
||||
struct in_addr sin;
|
||||
#ifdef HAVE_IPV6
|
||||
struct in6_addr sin6;
|
||||
#endif /* IPV6 */
|
||||
} add;
|
||||
} prefix_t;
|
||||
|
||||
|
@ -120,12 +129,14 @@ typedef struct _patricia_tree_t {
|
|||
int num_active_node; /* for debug purpose */
|
||||
} patricia_tree_t;
|
||||
|
||||
|
||||
patricia_node_t *patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix);
|
||||
bool patricia_search_all (patricia_tree_t *patricia, prefix_t *prefix, patricia_node_t ***list, int *n);
|
||||
patricia_node_t *patricia_search_best (patricia_tree_t *patricia, prefix_t *prefix);
|
||||
patricia_node_t * patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix,
|
||||
int inclusive);
|
||||
patricia_node_t *patricia_search_exact(patricia_tree_t *patricia,
|
||||
prefix_t *prefix);
|
||||
int patricia_search_all(patricia_tree_t *patricia, prefix_t *prefix,
|
||||
patricia_node_t ***list, int *n);
|
||||
patricia_node_t *patricia_search_best(patricia_tree_t *patricia,
|
||||
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);
|
||||
void patricia_remove(patricia_tree_t *patricia, patricia_node_t *node);
|
||||
patricia_tree_t *New_Patricia(int maxbits);
|
||||
|
@ -139,11 +150,9 @@ char *prefix_toa (prefix_t * prefix);
|
|||
|
||||
/* { from demo.c */
|
||||
|
||||
prefix_t *
|
||||
ascii2prefix (int family, char *string);
|
||||
prefix_t *ascii2prefix(int family, char *string);
|
||||
|
||||
patricia_node_t *
|
||||
make_and_lookup (patricia_tree_t *tree, char *string);
|
||||
patricia_node_t *make_and_lookup(patricia_tree_t *tree, char *string);
|
||||
|
||||
/* } */
|
||||
|
||||
|
@ -170,13 +179,15 @@ do { \
|
|||
while ((Xnode = Xrn)) { \
|
||||
if (1)
|
||||
|
||||
#define PATRICIA_WALK_BREAK { \
|
||||
#define PATRICIA_WALK_BREAK \
|
||||
{ \
|
||||
if (Xsp != Xstack) { \
|
||||
Xrn = *(--Xsp); \
|
||||
} else { \
|
||||
Xrn = (patricia_node_t *)0; \
|
||||
} \
|
||||
continue; }
|
||||
continue; \
|
||||
}
|
||||
|
||||
#define PATRICIA_WALK_END \
|
||||
if (Xrn->l) { \
|
||||
|
@ -192,4 +203,7 @@ do { \
|
|||
Xrn = (patricia_node_t *)0; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
} \
|
||||
while (0)
|
||||
|
||||
#endif /* _PATRICIA_H */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue