* This was then yanked out of the ratbox/devel/src tree and stuffed into
* libratbox and had function names changed, but otherwise not really altered.
*
- * $Id: patricia.c 24244 2007-08-22 19:04:55Z androsyn $
* Dave Plonka <plonka@doit.wisc.edu>
*
* This product includes software developed by the University of Michigan,
* #define NOTYET 1
* #define PATRICIA_DEBUG 1
*/
-
+
#define PREFIX_HEAP_COUNT 1024
#define NODE_HEAP_COUNT 1024
#define PATRICIA_HEAP_COUNT 128
* convert prefix information to bytes
*/
static uint8_t *
-prefix_tochar(rb_prefix_t * prefix)
+prefix_tochar(rb_prefix_t *prefix)
{
if(prefix == NULL)
return (NULL);
- return ((uint8_t *) & prefix->add.sin);
+ return ((uint8_t *)&prefix->add.sin);
}
static int
int n = mask / 8;
int m = ((-1) << (8 - (mask % 8)));
- if(mask % 8 == 0 || (((uint8_t *) addr)[n] & m) == (((uint8_t *) dest)[n] & m))
+ if(mask % 8 == 0 || (((uint8_t *)addr)[n] & m) == (((uint8_t *)dest)[n] & m))
return (1);
}
return (0);
}
+
#ifdef NOTYET
static char *
-prefix_toa2x(rb_prefix_t * prefix, char *buf, int buf_len, int with_len)
+prefix_toa2x(rb_prefix_t *prefix, char *buf, int buf_len, int with_len)
{
static char tmp[6];
if(prefix == NULL)
{
- strcpy(buf, "(NULL)");
+ rb_strlcpy(buf, "(NULL)", buf_len);
return (buf);
}
inet_ntop(prefix->family, &prefix->add.sin, buf, buf_len);
*/
static char *
-prefix_toa2(rb_prefix_t * prefix, char *buff, int buf_len)
+prefix_toa2(rb_prefix_t *prefix, char *buff, int buf_len)
{
return (prefix_toa2x(prefix, buff, buf_len, 0));
}
+
static char *
-prefix_toa(rb_prefix_t * prefix)
+prefix_toa(rb_prefix_t *prefix)
{
#ifdef RB_IPV6
static char buf[INET6_ADDRSTRLEN + 6];
}
#endif
static rb_prefix_t *
-New_Prefix2(int family, void *dest, int bitlen, rb_prefix_t * prefix)
+New_Prefix2(int family, void *dest, int bitlen, rb_prefix_t *prefix)
{
int dynamic_allocated = 0;
#ifdef RB_IPV6
{
if(prefix == NULL)
{
- prefix = rb_malloc(sizeof(rb_prefix_t));
+ prefix = rb_malloc(sizeof(rb_prefix_t));
dynamic_allocated++;
}
memcpy(&prefix->add.sin, dest, 4);
}
static rb_prefix_t *
-Ref_Prefix(rb_prefix_t * prefix)
+Ref_Prefix(rb_prefix_t *prefix)
{
if(prefix == NULL)
return (NULL);
}
static void
-Deref_Prefix(rb_prefix_t * prefix)
+Deref_Prefix(rb_prefix_t *prefix)
{
if(prefix == NULL)
return;
*/
void
-rb_clear_patricia(rb_patricia_tree_t * patricia, void (*func)(void *))
+rb_clear_patricia(rb_patricia_tree_t *patricia, void (*func) (void *))
{
assert(patricia);
if(patricia->head)
rb_patricia_node_t **Xsp = Xstack;
rb_patricia_node_t *Xrn = patricia->head;
- while (Xrn)
+ while(Xrn)
{
rb_patricia_node_t *l = Xrn->l;
rb_patricia_node_t *r = Xrn->r;
}
else
{
- Xrn = (rb_patricia_node_t *) 0;
+ Xrn = (rb_patricia_node_t *)0;
}
}
}
void
-rb_destroy_patricia(rb_patricia_tree_t * patricia, void (*func)(void *))
+rb_destroy_patricia(rb_patricia_tree_t *patricia, void (*func) (void *))
{
rb_clear_patricia(patricia, func);
num_active_patricia--;
*/
void
-rb_patricia_process(rb_patricia_tree_t * patricia, void (*func)(rb_prefix_t *, void *))
+rb_patricia_process(rb_patricia_tree_t *patricia, void (*func) (rb_prefix_t *, void *))
{
rb_patricia_node_t *node;
assert(func);
}
rb_patricia_node_t *
-rb_patricia_search_exact(rb_patricia_tree_t * patricia, rb_prefix_t * prefix)
+rb_patricia_search_exact(rb_patricia_tree_t *patricia, rb_prefix_t *prefix)
{
rb_patricia_node_t *node;
uint8_t *addr;
addr = rb_prefix_touchar(prefix);
bitlen = prefix->bitlen;
- while (node->bit < bitlen)
+ while(node->bit < bitlen)
{
if(BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
/* if inclusive != 0, "best" may be the given prefix itself */
rb_patricia_node_t *
-rb_patricia_search_best2(rb_patricia_tree_t * patricia, rb_prefix_t * prefix, int inclusive)
+rb_patricia_search_best2(rb_patricia_tree_t *patricia, rb_prefix_t *prefix, int inclusive)
{
rb_patricia_node_t *node;
rb_patricia_node_t *stack[RB_PATRICIA_MAXBITS + 1];
addr = rb_prefix_touchar(prefix);
bitlen = prefix->bitlen;
- while (node->bit < bitlen)
+ while(node->bit < bitlen)
{
if(node->prefix)
if(cnt <= 0)
return (NULL);
- while (--cnt >= 0)
+ while(--cnt >= 0)
{
node = stack[cnt];
#ifdef PATRICIA_DEBUG
rb_patricia_node_t *
-rb_patricia_search_best(rb_patricia_tree_t * patricia, rb_prefix_t * prefix)
+rb_patricia_search_best(rb_patricia_tree_t *patricia, rb_prefix_t *prefix)
{
return (rb_patricia_search_best2(patricia, prefix, 1));
}
rb_patricia_node_t *
-rb_patricia_lookup(rb_patricia_tree_t * patricia, rb_prefix_t * prefix)
+rb_patricia_lookup(rb_patricia_tree_t *patricia, rb_prefix_t *prefix)
{
rb_patricia_node_t *node, *new_node, *parent, *glue;
uint8_t *addr, *test_addr;
if(patricia->head == NULL)
{
- node = rb_malloc(sizeof(rb_patricia_node_t));
+ node = rb_malloc(sizeof(rb_patricia_node_t));
node->bit = prefix->bitlen;
node->prefix = Ref_Prefix(prefix);
node->parent = NULL;
bitlen = prefix->bitlen;
node = patricia->head;
- while (node->bit < bitlen || node->prefix == NULL)
+ while(node->bit < bitlen || node->prefix == NULL)
{
if(node->bit < patricia->maxbits &&
/* find the first bit different */
check_bit = (node->bit < bitlen) ? node->bit : bitlen;
differ_bit = 0;
- for (i = 0; i * 8 < check_bit; i++)
+ for(i = 0; i * 8 < check_bit; i++)
{
if((r = (addr[i] ^ test_addr[i])) == 0)
{
continue;
}
/* I know the better way, but for now */
- for (j = 0; j < 8; j++)
+ for(j = 0; j < 8; j++)
{
if(BIT_TEST(r, (0x80 >> j)))
break;
#endif /* PATRICIA_DEBUG */
parent = node->parent;
- while (parent && parent->bit >= differ_bit)
+ while(parent && parent->bit >= differ_bit)
{
node = parent;
parent = node->parent;
return (node);
}
- new_node = rb_malloc(sizeof(rb_patricia_node_t));
+ new_node = rb_malloc(sizeof(rb_patricia_node_t));
new_node->bit = prefix->bitlen;
new_node->prefix = Ref_Prefix(prefix);
new_node->parent = NULL;
void
-rb_patricia_remove(rb_patricia_tree_t * patricia, rb_patricia_node_t * node)
+rb_patricia_remove(rb_patricia_tree_t *patricia, rb_patricia_node_t *node)
{
rb_patricia_node_t *parent, *child;
}
rb_patricia_node_t *
-make_and_lookup_ip(rb_patricia_tree_t * tree, struct sockaddr *in, int bitlen)
+make_and_lookup_ip(rb_patricia_tree_t *tree, struct sockaddr *in, int bitlen)
{
rb_prefix_t *prefix;
rb_patricia_node_t *node;
void *ipptr = NULL;
#ifdef RB_IPV6
if(in->sa_family == AF_INET6)
- ipptr = &((struct sockaddr_in6 *)in)->sin6_addr;
- else
+ ipptr = &((struct sockaddr_in6 *)in)->sin6_addr;
+ else
#endif
ipptr = &((struct sockaddr_in *)in)->sin_addr;
-
+
prefix = New_Prefix(in->sa_family, ipptr, bitlen);
if(prefix == NULL)
rb_patricia_node_t *
-make_and_lookup(rb_patricia_tree_t * tree, const char *string)
+make_and_lookup(rb_patricia_tree_t *tree, const char *string)
{
rb_prefix_t *prefix;
rb_patricia_node_t *node;
#ifdef NOTYET
static rb_patricia_node_t *
-try_search_exact(rb_patricia_tree_t * tree, char *string)
+try_search_exact(rb_patricia_tree_t *tree, char *string)
{
rb_prefix_t *prefix;
rb_patricia_node_t *node;
}
void
-lookup_then_remove(rb_patricia_tree_t * tree, char *string)
+lookup_then_remove(rb_patricia_tree_t *tree, char *string)
{
rb_patricia_node_t *node;
#endif
rb_patricia_node_t *
-rb_match_ip(rb_patricia_tree_t * tree, struct sockaddr *ip)
+rb_match_ip(rb_patricia_tree_t *tree, struct sockaddr *ip)
{
rb_prefix_t *prefix;
rb_patricia_node_t *node;
len = 128;
family = AF_INET6;
ipptr = &((struct sockaddr_in6 *)ip)->sin6_addr;
- } else {
+ }
+ else
+ {
len = 32;
family = AF_INET;
ipptr = &((struct sockaddr_in *)ip)->sin_addr;
}
#endif
-
+
if((prefix = New_Prefix(family, ipptr, len)) != NULL)
{
node = rb_patricia_search_best(tree, prefix);
}
rb_patricia_node_t *
-rb_match_ip_exact(rb_patricia_tree_t * tree, struct sockaddr *ip, unsigned int len)
+rb_match_ip_exact(rb_patricia_tree_t *tree, struct sockaddr *ip, unsigned int len)
{
rb_prefix_t *prefix;
rb_patricia_node_t *node;
len = 128;
family = AF_INET6;
ipptr = &((struct sockaddr_in6 *)ip)->sin6_addr;
- } else {
+ }
+ else
+ {
if(len > 32)
len = 32;
family = AF_INET;
ipptr = &((struct sockaddr_in *)ip)->sin_addr;
}
#endif
-
+
if((prefix = New_Prefix(family, ipptr, len)) != NULL)
{
node = rb_patricia_search_exact(tree, prefix);
rb_patricia_node_t *
-rb_match_string(rb_patricia_tree_t * tree, const char *string)
+rb_match_string(rb_patricia_tree_t *tree, const char *string)
{
rb_patricia_node_t *node;
rb_prefix_t *prefix;
}
rb_patricia_node_t *
-rb_match_exact_string(rb_patricia_tree_t * tree, const char *string)
+rb_match_exact_string(rb_patricia_tree_t *tree, const char *string)
{
rb_prefix_t *prefix;
rb_patricia_node_t *node;