Various fixes to patricia tree handling
This commit is contained in:
+12
-11
@@ -1694,16 +1694,14 @@ static int ac_match_handler(AC_MATCH_t *m, AC_TEXT_t *txt, AC_REP_t *match) {
|
|||||||
/* ******************************************************************** */
|
/* ******************************************************************** */
|
||||||
|
|
||||||
static int fill_prefix_v4(prefix_t *p, const struct in_addr *a, int b, int mb) {
|
static int fill_prefix_v4(prefix_t *p, const struct in_addr *a, int b, int mb) {
|
||||||
do {
|
if(b < 0 || b > mb)
|
||||||
if(b < 0 || b > mb)
|
return(-1);
|
||||||
return(-1);
|
|
||||||
|
|
||||||
memset(p, 0, sizeof(prefix_t));
|
memset(p, 0, sizeof(prefix_t));
|
||||||
memcpy(&p->add.sin, a, (mb + 7) / 8);
|
memcpy(&p->add.sin, a, (mb + 7) / 8);
|
||||||
p->family = AF_INET;
|
p->family = AF_INET;
|
||||||
p->bitlen = b;
|
p->bitlen = b;
|
||||||
p->ref_count = 0;
|
p->ref_count = 0;
|
||||||
} while (0);
|
|
||||||
|
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
@@ -1770,6 +1768,7 @@ static patricia_node_t *add_to_ptree(patricia_tree_t *tree, int family, void *ad
|
|||||||
fill_prefix_v4(&prefix, (struct in_addr *) addr, bits, tree->maxbits);
|
fill_prefix_v4(&prefix, (struct in_addr *) addr, bits, tree->maxbits);
|
||||||
|
|
||||||
node = ndpi_patricia_lookup(tree, &prefix);
|
node = ndpi_patricia_lookup(tree, &prefix);
|
||||||
|
if(node) memset(&node->value, 0, sizeof(node->value));
|
||||||
|
|
||||||
return(node);
|
return(node);
|
||||||
}
|
}
|
||||||
@@ -1877,8 +1876,10 @@ static int ndpi_add_host_ip_subprotocol(struct ndpi_detection_module_struct *ndp
|
|||||||
*/
|
*/
|
||||||
double_column = strrchr(value, ':');
|
double_column = strrchr(value, ':');
|
||||||
|
|
||||||
if(double_column)
|
if(double_column) {
|
||||||
|
double_column[0] = '\0';
|
||||||
port = atoi(&double_column[1]);
|
port = atoi(&double_column[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inet_pton(AF_INET, value, &pin);
|
inet_pton(AF_INET, value, &pin);
|
||||||
@@ -2231,7 +2232,7 @@ int ndpi_match_string(void *_automa, char *string_to_match) {
|
|||||||
int ndpi_match_string_id(void *_automa, char *string_to_match, u_int match_len, u_int16_t *id) {
|
int ndpi_match_string_id(void *_automa, char *string_to_match, u_int match_len, u_int16_t *id) {
|
||||||
AC_TEXT_t ac_input_text;
|
AC_TEXT_t ac_input_text;
|
||||||
AC_AUTOMATA_t *automa = (AC_AUTOMATA_t *) _automa;
|
AC_AUTOMATA_t *automa = (AC_AUTOMATA_t *) _automa;
|
||||||
AC_REP_t match = {NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_CATEGORY_UNSPECIFIED, NDPI_PROTOCOL_UNRATED};
|
AC_REP_t match = { NDPI_PROTOCOL_UNKNOWN, NDPI_PROTOCOL_CATEGORY_UNSPECIFIED, NDPI_PROTOCOL_UNRATED };
|
||||||
int rc;
|
int rc;
|
||||||
|
|
||||||
*id = -1;
|
*id = -1;
|
||||||
|
|||||||
+7
-7
@@ -79,15 +79,15 @@
|
|||||||
/* { from mrt.h */
|
/* { from mrt.h */
|
||||||
|
|
||||||
typedef struct the_prefix4_t {
|
typedef struct the_prefix4_t {
|
||||||
unsigned short family; /* AF_INET | AF_INET6 */
|
u_int16_t family; /* AF_INET | AF_INET6 */
|
||||||
unsigned short bitlen; /* same as mask? */
|
u_int16_t bitlen; /* same as mask? */
|
||||||
int ref_count; /* reference count */
|
int ref_count; /* reference count */
|
||||||
struct in_addr sin;
|
struct in_addr sin;
|
||||||
} prefix4_t;
|
} prefix4_t;
|
||||||
|
|
||||||
typedef struct the_prefix_t {
|
typedef struct the_prefix_t {
|
||||||
unsigned short family; /* AF_INET | AF_INET6 */
|
u_int16_t family; /* AF_INET | AF_INET6 */
|
||||||
unsigned short bitlen; /* same as mask? */
|
u_int16_t bitlen; /* same as mask? */
|
||||||
int ref_count; /* reference count */
|
int ref_count; /* reference count */
|
||||||
union {
|
union {
|
||||||
struct in_addr sin;
|
struct in_addr sin;
|
||||||
@@ -108,7 +108,7 @@ union patricia_node_value_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef struct _patricia_node_t {
|
typedef struct _patricia_node_t {
|
||||||
u_int bit; /* flag if this node used */
|
u_int16_t bit; /* flag if this node used */
|
||||||
prefix_t *prefix; /* who we are in patricia tree */
|
prefix_t *prefix; /* who we are in patricia tree */
|
||||||
struct _patricia_node_t *l, *r; /* left and right children */
|
struct _patricia_node_t *l, *r; /* left and right children */
|
||||||
struct _patricia_node_t *parent;/* may be used */
|
struct _patricia_node_t *parent;/* may be used */
|
||||||
@@ -118,7 +118,7 @@ typedef struct _patricia_node_t {
|
|||||||
|
|
||||||
typedef struct _patricia_tree_t {
|
typedef struct _patricia_tree_t {
|
||||||
patricia_node_t *head;
|
patricia_node_t *head;
|
||||||
u_int maxbits; /* for IP, 32 bit addresses */
|
u_int16_t maxbits; /* for IP, 32 bit addresses */
|
||||||
int num_active_node; /* for debug purpose */
|
int num_active_node; /* for debug purpose */
|
||||||
} patricia_tree_t;
|
} patricia_tree_t;
|
||||||
|
|
||||||
@@ -132,7 +132,7 @@ patricia_node_t * ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_
|
|||||||
int inclusive);
|
int inclusive);
|
||||||
patricia_node_t *ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix);
|
patricia_node_t *ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix);
|
||||||
void ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node);
|
void ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node);
|
||||||
patricia_tree_t *ndpi_New_Patricia (int maxbits);
|
patricia_tree_t *ndpi_New_Patricia (u_int16_t maxbits);
|
||||||
void ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func);
|
void ndpi_Clear_Patricia (patricia_tree_t *patricia, void_fn_t func);
|
||||||
void ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func);
|
void ndpi_Destroy_Patricia (patricia_tree_t *patricia, void_fn_t func);
|
||||||
void ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func);
|
void ndpi_patricia_process (patricia_tree_t *patricia, void_fn2_t func);
|
||||||
|
|||||||
+124
-114
@@ -56,7 +56,7 @@
|
|||||||
#include "ndpi_patricia.h"
|
#include "ndpi_patricia.h"
|
||||||
#include "ndpi_api.h"
|
#include "ndpi_api.h"
|
||||||
|
|
||||||
void ndpi_DeleteEntry(void *a) {
|
static void ndpi_DeleteEntry(void *a) {
|
||||||
ndpi_free(a);
|
ndpi_free(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,8 +65,7 @@ void ndpi_DeleteEntry(void *a) {
|
|||||||
/* ndpi_prefix_tochar
|
/* ndpi_prefix_tochar
|
||||||
* convert prefix information to bytes
|
* convert prefix information to bytes
|
||||||
*/
|
*/
|
||||||
u_char *
|
static u_char* ndpi_prefix_tochar (prefix_t * prefix)
|
||||||
ndpi_prefix_tochar (prefix_t * prefix)
|
|
||||||
{
|
{
|
||||||
if(prefix == NULL)
|
if(prefix == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
@@ -74,20 +73,20 @@ ndpi_prefix_tochar (prefix_t * prefix)
|
|||||||
return ((u_char *) & prefix->add.sin);
|
return ((u_char *) & prefix->add.sin);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ndpi_comp_with_mask (void *addr, void *dest, u_int mask) {
|
static int ndpi_comp_with_mask (void *addr, void *dest, u_int mask) {
|
||||||
uint32_t *pa = addr;
|
uint32_t *pa = addr;
|
||||||
uint32_t *pd = dest;
|
uint32_t *pd = dest;
|
||||||
uint32_t m;
|
uint32_t m;
|
||||||
for(;mask >= 32; mask -= 32, pa++,pd++)
|
for(;mask >= 32; mask -= 32, pa++,pd++)
|
||||||
if(*pa != *pd) return 0;
|
if(*pa != *pd) return 0;
|
||||||
if(!mask) return 1;
|
if(!mask) return 1;
|
||||||
m = htonl((~0u) << (32-mask));
|
m = htonl((~0u) << (32-mask));
|
||||||
return (*pa & m) == (*pd &m);
|
return (*pa & m) == (*pd &m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
/* this allows incomplete prefix */
|
/* this allows incomplete prefix */
|
||||||
int
|
static int ndpi_my_inet_pton (int af, const char *src, void *dst)
|
||||||
ndpi_my_inet_pton (int af, const char *src, void *dst)
|
|
||||||
{
|
{
|
||||||
if(af == AF_INET) {
|
if(af == AF_INET) {
|
||||||
int i;
|
int i;
|
||||||
@@ -106,7 +105,7 @@ ndpi_my_inet_pton (int af, const char *src, void *dst)
|
|||||||
return (0);
|
return (0);
|
||||||
c = *src++;
|
c = *src++;
|
||||||
} while (c && isdigit (c));
|
} while (c && isdigit (c));
|
||||||
xp[i] = val;
|
xp[i] = (u_char)val;
|
||||||
if(c == '\0')
|
if(c == '\0')
|
||||||
break;
|
break;
|
||||||
if(c != '.')
|
if(c != '.')
|
||||||
@@ -118,24 +117,25 @@ ndpi_my_inet_pton (int af, const char *src, void *dst)
|
|||||||
return (1);
|
return (1);
|
||||||
#if defined(PATRICIA_IPV6)
|
#if defined(PATRICIA_IPV6)
|
||||||
} else if(af == AF_INET6) {
|
} else if(af == AF_INET6) {
|
||||||
return (inet_pton (af, src, dst));
|
return (inet_pton (af, src, dst));
|
||||||
#endif /* PATRICIA_IPV6 */
|
#endif /* PATRICIA_IPV6 */
|
||||||
} else {
|
} else {
|
||||||
#ifndef NT
|
#ifndef NT
|
||||||
errno = EAFNOSUPPORT;
|
errno = EAFNOSUPPORT;
|
||||||
#endif /* NT */
|
#endif /* NT */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#define PATRICIA_MAX_THREADS 16
|
#define PATRICIA_MAX_THREADS 16
|
||||||
|
|
||||||
|
#if 0
|
||||||
/*
|
/*
|
||||||
* 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 *
|
static char * ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
||||||
ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
|
||||||
{
|
{
|
||||||
if(prefix == NULL)
|
if(prefix == NULL)
|
||||||
return ((char*)"(Null)");
|
return ((char*)"(Null)");
|
||||||
@@ -193,22 +193,20 @@ ndpi_prefix_toa2x (prefix_t *prefix, char *buff, int with_len)
|
|||||||
/* ndpi_prefix_toa2
|
/* ndpi_prefix_toa2
|
||||||
* convert prefix information to ascii string
|
* convert prefix information to ascii string
|
||||||
*/
|
*/
|
||||||
char *
|
static char * ndpi_prefix_toa2 (prefix_t *prefix, char *buff)
|
||||||
ndpi_prefix_toa2 (prefix_t *prefix, char *buff)
|
|
||||||
{
|
{
|
||||||
return (ndpi_prefix_toa2x (prefix, buff, 0));
|
return (ndpi_prefix_toa2x (prefix, buff, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ndpi_prefix_toa
|
/* ndpi_prefix_toa
|
||||||
*/
|
*/
|
||||||
char *
|
static char * ndpi_prefix_toa (prefix_t * prefix)
|
||||||
ndpi_prefix_toa (prefix_t * prefix)
|
|
||||||
{
|
{
|
||||||
return (ndpi_prefix_toa2 (prefix, (char *) NULL));
|
return (ndpi_prefix_toa2 (prefix, (char *) NULL));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
prefix_t *
|
static prefix_t * ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
||||||
ndpi_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;
|
||||||
@@ -242,8 +240,8 @@ ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
|||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix->bitlen = (bitlen >= 0)? bitlen: default_bitlen;
|
prefix->bitlen = (u_int16_t)((bitlen >= 0) ? bitlen : default_bitlen);
|
||||||
prefix->family = family;
|
prefix->family = (u_int16_t)family;
|
||||||
prefix->ref_count = 0;
|
prefix->ref_count = 0;
|
||||||
if(dynamic_allocated) {
|
if(dynamic_allocated) {
|
||||||
prefix->ref_count++;
|
prefix->ref_count++;
|
||||||
@@ -252,83 +250,14 @@ ndpi_New_Prefix2 (int family, void *dest, int bitlen, prefix_t *prefix)
|
|||||||
return (prefix);
|
return (prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix_t *
|
#if 0
|
||||||
ndpi_New_Prefix (int family, void *dest, int bitlen)
|
static prefix_t * ndpi_New_Prefix (int family, void *dest, int bitlen)
|
||||||
{
|
{
|
||||||
return (ndpi_New_Prefix2 (family, dest, bitlen, NULL));
|
return (ndpi_New_Prefix2 (family, dest, bitlen, NULL));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* ndpi_ascii2prefix */
|
static prefix_t *
|
||||||
prefix_t *
|
|
||||||
ndpi_ascii2prefix (int family, char *string)
|
|
||||||
{
|
|
||||||
long bitlen;
|
|
||||||
long maxbitlen = 0;
|
|
||||||
char *cp;
|
|
||||||
struct in_addr sin;
|
|
||||||
#if defined(PATRICIA_IPV6)
|
|
||||||
struct in6_addr sin6;
|
|
||||||
#endif /* PATRICIA_IPV6 */
|
|
||||||
char save[MAXLINE];
|
|
||||||
|
|
||||||
if(string == NULL)
|
|
||||||
return (NULL);
|
|
||||||
|
|
||||||
/* easy way to handle both families */
|
|
||||||
if(family == 0) {
|
|
||||||
family = AF_INET;
|
|
||||||
#if defined(PATRICIA_IPV6)
|
|
||||||
if(strchr (string, ':')) family = AF_INET6;
|
|
||||||
#endif /* PATRICIA_IPV6 */
|
|
||||||
}
|
|
||||||
|
|
||||||
if(family == AF_INET) {
|
|
||||||
maxbitlen = sizeof(struct in_addr) * 8;
|
|
||||||
}
|
|
||||||
#if defined(PATRICIA_IPV6)
|
|
||||||
else if(family == AF_INET6) {
|
|
||||||
maxbitlen = sizeof(struct in6_addr) * 8;
|
|
||||||
}
|
|
||||||
#endif /* PATRICIA_IPV6 */
|
|
||||||
|
|
||||||
if((cp = strchr (string, '/')) != NULL) {
|
|
||||||
bitlen = atol (cp + 1);
|
|
||||||
/* *cp = '\0'; */
|
|
||||||
/* copy the string to save. Avoid destroying the string */
|
|
||||||
assert (cp - string < MAXLINE);
|
|
||||||
memcpy (save, string, cp - string);
|
|
||||||
save[cp - string] = '\0';
|
|
||||||
string = save;
|
|
||||||
if((bitlen < 0) || (bitlen > maxbitlen))
|
|
||||||
bitlen = maxbitlen;
|
|
||||||
} else {
|
|
||||||
bitlen = maxbitlen;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(family == AF_INET) {
|
|
||||||
if(ndpi_my_inet_pton (AF_INET, string, &sin) <= 0)
|
|
||||||
return (NULL);
|
|
||||||
return (ndpi_New_Prefix (AF_INET, &sin, bitlen));
|
|
||||||
}
|
|
||||||
|
|
||||||
#if defined(PATRICIA_IPV6)
|
|
||||||
else if(family == AF_INET6) {
|
|
||||||
// Get rid of this with next IPv6 upgrade
|
|
||||||
#if defined(NT) && !defined(HAVE_INET_NTOP)
|
|
||||||
inet6_addr(string, &sin6);
|
|
||||||
return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen));
|
|
||||||
#else
|
|
||||||
if(inet_pton (AF_INET6, string, &sin6) <= 0)
|
|
||||||
return (NULL);
|
|
||||||
#endif /* NT */
|
|
||||||
return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen));
|
|
||||||
}
|
|
||||||
#endif /* PATRICIA_IPV6 */
|
|
||||||
else
|
|
||||||
return (NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
prefix_t *
|
|
||||||
ndpi_Ref_Prefix (prefix_t * prefix)
|
ndpi_Ref_Prefix (prefix_t * prefix)
|
||||||
{
|
{
|
||||||
if(prefix == NULL)
|
if(prefix == NULL)
|
||||||
@@ -342,7 +271,7 @@ ndpi_Ref_Prefix (prefix_t * prefix)
|
|||||||
return (prefix);
|
return (prefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
static void
|
||||||
ndpi_Deref_Prefix (prefix_t * prefix)
|
ndpi_Deref_Prefix (prefix_t * prefix)
|
||||||
{
|
{
|
||||||
if(prefix == NULL)
|
if(prefix == NULL)
|
||||||
@@ -358,21 +287,21 @@ ndpi_Deref_Prefix (prefix_t * prefix)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* #define PATRICIA_DEBUG 1 */
|
// #define PATRICIA_DEBUG 1
|
||||||
|
|
||||||
static int num_active_patricia = 0;
|
static int num_active_patricia = 0;
|
||||||
|
|
||||||
/* these routines support continuous mask only */
|
/* these routines support continuous mask only */
|
||||||
|
|
||||||
patricia_tree_t *
|
patricia_tree_t *
|
||||||
ndpi_New_Patricia (int maxbits)
|
ndpi_New_Patricia (u_int16_t maxbits)
|
||||||
{
|
{
|
||||||
patricia_tree_t *patricia = (patricia_tree_t*)ndpi_calloc(1, sizeof *patricia);
|
patricia_tree_t *patricia = (patricia_tree_t*)ndpi_calloc(1, sizeof *patricia);
|
||||||
|
|
||||||
patricia->maxbits = maxbits;
|
patricia->maxbits = maxbits;
|
||||||
patricia->head = NULL;
|
patricia->head = NULL;
|
||||||
patricia->num_active_node = 0;
|
patricia->num_active_node = 0;
|
||||||
assert((u_int)maxbits <= PATRICIA_MAXBITS); /* XXX */
|
assert((u_int16_t)maxbits <= PATRICIA_MAXBITS); /* XXX */
|
||||||
num_active_patricia++;
|
num_active_patricia++;
|
||||||
return (patricia);
|
return (patricia);
|
||||||
}
|
}
|
||||||
@@ -477,7 +406,7 @@ ndpi_patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
{
|
{
|
||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
u_char *addr;
|
u_char *addr;
|
||||||
u_int bitlen;
|
u_int16_t bitlen;
|
||||||
|
|
||||||
assert (patricia);
|
assert (patricia);
|
||||||
assert (prefix);
|
assert (prefix);
|
||||||
@@ -521,17 +450,19 @@ ndpi_patricia_search_exact (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
|
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
if(node->prefix)
|
if(node->prefix)
|
||||||
fprintf (stderr, "patricia_search_exact: stop at %s/%d\n",
|
fprintf (stderr, "patricia_search_exact: stop at %s/%d [node->bit: %u][bitlen: %u]\n",
|
||||||
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
ndpi_prefix_toa (node->prefix), node->prefix->bitlen, node->bit, bitlen);
|
||||||
else
|
else
|
||||||
fprintf (stderr, "patricia_search_exact: stop at %u\n", node->bit);
|
fprintf (stderr, "patricia_search_exact: stop at %u\n", node->bit);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
|
|
||||||
if(node->bit > bitlen || node->prefix == NULL)
|
if(node->bit > bitlen || node->prefix == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
|
||||||
assert (node->bit == bitlen);
|
assert (node->bit == bitlen);
|
||||||
assert (node->bit == node->prefix->bitlen);
|
assert (node->bit == node->prefix->bitlen);
|
||||||
if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix), ndpi_prefix_tochar (prefix),
|
if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix), ndpi_prefix_tochar (prefix),
|
||||||
bitlen)) {
|
bitlen)) {
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
fprintf (stderr, "patricia_search_exact: found %s/%d\n",
|
fprintf (stderr, "patricia_search_exact: found %s/%d\n",
|
||||||
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
||||||
@@ -549,7 +480,7 @@ ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inc
|
|||||||
patricia_node_t *node;
|
patricia_node_t *node;
|
||||||
patricia_node_t *stack[PATRICIA_MAXBITS + 1];
|
patricia_node_t *stack[PATRICIA_MAXBITS + 1];
|
||||||
u_char *addr;
|
u_char *addr;
|
||||||
u_int bitlen;
|
u_int16_t bitlen;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
|
||||||
assert (patricia);
|
assert (patricia);
|
||||||
@@ -564,7 +495,6 @@ ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inc
|
|||||||
bitlen = prefix->bitlen;
|
bitlen = prefix->bitlen;
|
||||||
|
|
||||||
while (node->bit < bitlen) {
|
while (node->bit < bitlen) {
|
||||||
|
|
||||||
if(node->prefix) {
|
if(node->prefix) {
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
fprintf (stderr, "patricia_search_best: push %s/%d\n",
|
fprintf (stderr, "patricia_search_best: push %s/%d\n",
|
||||||
@@ -600,8 +530,14 @@ ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inc
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(inclusive && node && node->prefix)
|
if(inclusive && node && node->prefix) {
|
||||||
|
#ifdef PATRICIA_DEBUG
|
||||||
|
fprintf (stderr, "patricia_search_best: found node %s/%d [searching %s/%d]\n",
|
||||||
|
ndpi_prefix_toa (node->prefix), node->prefix->bitlen,
|
||||||
|
ndpi_prefix_toa (prefix), prefix->bitlen);
|
||||||
|
#endif
|
||||||
stack[cnt++] = node;
|
stack[cnt++] = node;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
if(node == NULL)
|
if(node == NULL)
|
||||||
@@ -623,8 +559,8 @@ ndpi_patricia_search_best2 (patricia_tree_t *patricia, prefix_t *prefix, int inc
|
|||||||
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
||||||
#endif /* PATRICIA_DEBUG */
|
#endif /* PATRICIA_DEBUG */
|
||||||
if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix),
|
if(ndpi_comp_with_mask (ndpi_prefix_tochar (node->prefix),
|
||||||
ndpi_prefix_tochar (prefix),
|
ndpi_prefix_tochar (prefix),
|
||||||
node->prefix->bitlen) && node->prefix->bitlen <= bitlen) {
|
node->prefix->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",
|
||||||
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
ndpi_prefix_toa (node->prefix), node->prefix->bitlen);
|
||||||
@@ -648,10 +584,15 @@ ndpi_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;
|
||||||
u_int bitlen, check_bit, differ_bit;
|
u_int16_t bitlen, check_bit, differ_bit;
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
assert (patricia);
|
#ifdef PATRICIA_DEBUG
|
||||||
|
fprintf (stderr, "patricia_lookup() %s/%d (head)\n",
|
||||||
|
ndpi_prefix_toa (prefix), prefix->bitlen);
|
||||||
|
#endif /* PATRICIA_DEBUG */
|
||||||
|
|
||||||
|
assert (patricia);
|
||||||
assert (prefix);
|
assert (prefix);
|
||||||
assert (prefix->bitlen <= patricia->maxbits);
|
assert (prefix->bitlen <= patricia->maxbits);
|
||||||
|
|
||||||
@@ -676,9 +617,8 @@ ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
node = patricia->head;
|
node = patricia->head;
|
||||||
|
|
||||||
while (node->bit < bitlen || node->prefix == NULL) {
|
while (node->bit < bitlen || node->prefix == NULL) {
|
||||||
|
|
||||||
if(node->bit < patricia->maxbits &&
|
if(node->bit < patricia->maxbits &&
|
||||||
BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) {
|
BIT_TEST (addr[node->bit >> 3], 0x80 >> (node->bit & 0x07))) {
|
||||||
if(node->r == NULL)
|
if(node->r == NULL)
|
||||||
break;
|
break;
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
@@ -733,6 +673,7 @@ ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
differ_bit = i * 8 + j;
|
differ_bit = i * 8 + j;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(differ_bit > check_bit)
|
if(differ_bit > check_bit)
|
||||||
differ_bit = check_bit;
|
differ_bit = check_bit;
|
||||||
#ifdef PATRICIA_DEBUG
|
#ifdef PATRICIA_DEBUG
|
||||||
@@ -781,7 +722,7 @@ ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
if(node->bit == differ_bit) {
|
if(node->bit == differ_bit) {
|
||||||
new_node->parent = node;
|
new_node->parent = node;
|
||||||
if(node->bit < patricia->maxbits &&
|
if(node->bit < patricia->maxbits &&
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -798,7 +739,7 @@ ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
|
|
||||||
if(bitlen == differ_bit) {
|
if(bitlen == differ_bit) {
|
||||||
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 {
|
||||||
@@ -831,7 +772,7 @@ ndpi_patricia_lookup (patricia_tree_t *patricia, prefix_t *prefix)
|
|||||||
glue->data = NULL;
|
glue->data = NULL;
|
||||||
patricia->num_active_node++;
|
patricia->num_active_node++;
|
||||||
if(differ_bit < patricia->maxbits &&
|
if(differ_bit < patricia->maxbits &&
|
||||||
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;
|
||||||
}
|
}
|
||||||
@@ -969,6 +910,75 @@ ndpi_patricia_remove (patricia_tree_t *patricia, patricia_node_t *node)
|
|||||||
/* { from demo.c */
|
/* { from demo.c */
|
||||||
#if 0
|
#if 0
|
||||||
|
|
||||||
|
/* ndpi_ascii2prefix */
|
||||||
|
static prefix_t * ndpi_ascii2prefix (int family, char *string)
|
||||||
|
{
|
||||||
|
long bitlen;
|
||||||
|
long maxbitlen = 0;
|
||||||
|
char *cp;
|
||||||
|
struct in_addr sin;
|
||||||
|
#if defined(PATRICIA_IPV6)
|
||||||
|
struct in6_addr sin6;
|
||||||
|
#endif /* PATRICIA_IPV6 */
|
||||||
|
char save[MAXLINE];
|
||||||
|
|
||||||
|
if(string == NULL)
|
||||||
|
return (NULL);
|
||||||
|
|
||||||
|
/* easy way to handle both families */
|
||||||
|
if(family == 0) {
|
||||||
|
family = AF_INET;
|
||||||
|
#if defined(PATRICIA_IPV6)
|
||||||
|
if(strchr (string, ':')) family = AF_INET6;
|
||||||
|
#endif /* PATRICIA_IPV6 */
|
||||||
|
}
|
||||||
|
|
||||||
|
if(family == AF_INET) {
|
||||||
|
maxbitlen = sizeof(struct in_addr) * 8;
|
||||||
|
}
|
||||||
|
#if defined(PATRICIA_IPV6)
|
||||||
|
else if(family == AF_INET6) {
|
||||||
|
maxbitlen = sizeof(struct in6_addr) * 8;
|
||||||
|
}
|
||||||
|
#endif /* PATRICIA_IPV6 */
|
||||||
|
|
||||||
|
if((cp = strchr (string, '/')) != NULL) {
|
||||||
|
bitlen = atol (cp + 1);
|
||||||
|
/* *cp = '\0'; */
|
||||||
|
/* copy the string to save. Avoid destroying the string */
|
||||||
|
assert (cp - string < MAXLINE);
|
||||||
|
memcpy (save, string, cp - string);
|
||||||
|
save[cp - string] = '\0';
|
||||||
|
string = save;
|
||||||
|
if((bitlen < 0) || (bitlen > maxbitlen))
|
||||||
|
bitlen = maxbitlen;
|
||||||
|
} else {
|
||||||
|
bitlen = maxbitlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(family == AF_INET) {
|
||||||
|
if(ndpi_my_inet_pton (AF_INET, string, &sin) <= 0)
|
||||||
|
return (NULL);
|
||||||
|
return (ndpi_New_Prefix (AF_INET, &sin, bitlen));
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(PATRICIA_IPV6)
|
||||||
|
else if(family == AF_INET6) {
|
||||||
|
// Get rid of this with next IPv6 upgrade
|
||||||
|
#if defined(NT) && !defined(HAVE_INET_NTOP)
|
||||||
|
inet6_addr(string, &sin6);
|
||||||
|
return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen));
|
||||||
|
#else
|
||||||
|
if(inet_pton (AF_INET6, string, &sin6) <= 0)
|
||||||
|
return (NULL);
|
||||||
|
#endif /* NT */
|
||||||
|
return (ndpi_New_Prefix (AF_INET6, &sin6, bitlen));
|
||||||
|
}
|
||||||
|
#endif /* PATRICIA_IPV6 */
|
||||||
|
else
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
patricia_node_t *
|
patricia_node_t *
|
||||||
ndpi_make_and_lookup (patricia_tree_t *tree, char *string)
|
ndpi_make_and_lookup (patricia_tree_t *tree, char *string)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user