mirror of
https://github.com/isc-projects/bind9.git
synced 2026-06-09 13:12:05 -04:00
Add support for O(1) ACL processing, based on radix tree code originally
written by kevin brintnall. [RT #16288]
This commit is contained in:
parent
c7e266b7e5
commit
3181d0e359
4 changed files with 1024 additions and 0 deletions
69
lib/dns/include/dns/iptable.h
Normal file
69
lib/dns/include/dns/iptable.h
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
|
||||
* Copyright (C) 1999-2002 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
||||
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef DNS_IPTABLE_H
|
||||
#define DNS_IPTABLE_H 1
|
||||
|
||||
#include <isc/lang.h>
|
||||
#include <isc/magic.h>
|
||||
#include <isc/radix.h>
|
||||
|
||||
struct dns_iptable {
|
||||
unsigned int magic;
|
||||
isc_mem_t *mctx;
|
||||
isc_refcount_t refcount;
|
||||
isc_radix_tree_t *radix;
|
||||
ISC_LINK(dns_iptable_t) nextincache;
|
||||
};
|
||||
|
||||
#define DNS_IPTABLE_MAGIC ISC_MAGIC('T','a','b','l')
|
||||
#define DNS_IPTABLE_VALID(a) ISC_MAGIC_VALID(a, DNS_IPTABLE_MAGIC)
|
||||
|
||||
/***
|
||||
*** Functions
|
||||
***/
|
||||
|
||||
ISC_LANG_BEGINDECLS
|
||||
|
||||
isc_result_t
|
||||
dns_iptable_create(isc_mem_t *mctx, dns_iptable_t **target);
|
||||
/*
|
||||
* Create a new IP table and the underlying radix structure
|
||||
*/
|
||||
|
||||
isc_result_t
|
||||
dns_iptable_addprefix(dns_iptable_t *tab, isc_netaddr_t *addr,
|
||||
isc_uint16_t bitlen, isc_boolean_t pos);
|
||||
/*
|
||||
* Add an IP prefix to an existing IP table
|
||||
*/
|
||||
|
||||
void
|
||||
dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, isc_boolean_t pos);
|
||||
/*
|
||||
* Merge one IP table into another one.
|
||||
*/
|
||||
|
||||
void
|
||||
dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target);
|
||||
|
||||
void
|
||||
dns_iptable_detach(dns_iptable_t **tabp);
|
||||
|
||||
ISC_LANG_ENDDECLS
|
||||
|
||||
#endif /* DNS_IPTABLE_H */
|
||||
154
lib/dns/iptable.c
Normal file
154
lib/dns/iptable.c
Normal file
|
|
@ -0,0 +1,154 @@
|
|||
/*
|
||||
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
|
||||
* Copyright (C) 1999-2002 Internet Software Consortium.
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
||||
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <isc/mem.h>
|
||||
#include <isc/radix.h>
|
||||
|
||||
#include <dns/acl.h>
|
||||
|
||||
static void destroy_iptable(dns_iptable_t *dtab);
|
||||
|
||||
/*
|
||||
* Create a new IP table and the underlying radix structure
|
||||
*/
|
||||
isc_result_t
|
||||
dns_iptable_create(isc_mem_t *mctx, dns_iptable_t **target) {
|
||||
isc_result_t result;
|
||||
dns_iptable_t *tab;
|
||||
|
||||
tab = isc_mem_get(mctx, sizeof(*tab));
|
||||
if (tab == NULL)
|
||||
return (ISC_R_NOMEMORY);
|
||||
tab->mctx = mctx;
|
||||
isc_refcount_init(&tab->refcount, 1);
|
||||
tab->magic = DNS_IPTABLE_MAGIC;
|
||||
|
||||
result = isc_radix_create(mctx, &tab->radix, RADIX_MAXBITS);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
goto cleanup;
|
||||
|
||||
*target = tab;
|
||||
return (ISC_R_SUCCESS);
|
||||
|
||||
cleanup:
|
||||
dns_iptable_detach(&tab);
|
||||
return (result);
|
||||
}
|
||||
|
||||
isc_boolean_t dns_iptable_neg = ISC_FALSE;
|
||||
isc_boolean_t dns_iptable_pos = ISC_TRUE;
|
||||
|
||||
/*
|
||||
* Add an IP prefix to an existing IP table
|
||||
*/
|
||||
isc_result_t
|
||||
dns_iptable_addprefix(dns_iptable_t *tab, isc_netaddr_t *addr,
|
||||
isc_uint16_t bitlen, isc_boolean_t pos)
|
||||
{
|
||||
isc_result_t result;
|
||||
isc_prefix_t pfx;
|
||||
isc_radix_node_t *node;
|
||||
|
||||
INSIST(DNS_IPTABLE_VALID(tab));
|
||||
INSIST(tab->radix);
|
||||
|
||||
|
||||
NETADDR_TO_PREFIX_T(addr, pfx, bitlen);
|
||||
|
||||
result = isc_radix_insert(tab->radix, &node, NULL, &pfx);
|
||||
|
||||
if (result != ISC_R_SUCCESS)
|
||||
return(result);
|
||||
|
||||
/* If the node already contains data, don't overwrite it */
|
||||
if (node->data == NULL) {
|
||||
if (pos)
|
||||
node->data = &dns_iptable_pos;
|
||||
else
|
||||
node->data = &dns_iptable_neg;
|
||||
}
|
||||
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
* Merge one IP table into another one.
|
||||
*/
|
||||
void
|
||||
dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, isc_boolean_t pos)
|
||||
{
|
||||
isc_radix_node_t *node, *new_node;
|
||||
int max_node = 0;
|
||||
|
||||
RADIX_WALK (source->radix->head, node) {
|
||||
isc_radix_insert (tab->radix, &new_node, node, NULL);
|
||||
|
||||
/*
|
||||
* If we're negating a nested ACL, then we should
|
||||
* reverse the sense of every node. However, this
|
||||
* could lead to a negative node in a nested ACL
|
||||
* becoming a positive match in the parent, which
|
||||
* could be a security risk. To prevent this, we
|
||||
* just leave the negative nodes negative.
|
||||
* (XXX: does this still need to be documented?)
|
||||
*/
|
||||
if (!pos &&
|
||||
node->data &&
|
||||
*(isc_boolean_t *) node->data == ISC_TRUE)
|
||||
new_node->data = &dns_iptable_neg;
|
||||
else
|
||||
new_node->data = node->data;
|
||||
|
||||
if (node->node_num > max_node)
|
||||
max_node = node->node_num;
|
||||
} RADIX_WALK_END;
|
||||
|
||||
tab->radix->num_added_node += max_node;
|
||||
}
|
||||
|
||||
void
|
||||
dns_iptable_attach(dns_iptable_t *source, dns_iptable_t **target) {
|
||||
REQUIRE(DNS_IPTABLE_VALID(source));
|
||||
isc_refcount_increment(&source->refcount, NULL);
|
||||
*target = source;
|
||||
}
|
||||
|
||||
void
|
||||
dns_iptable_detach(dns_iptable_t **tabp) {
|
||||
dns_iptable_t *tab = *tabp;
|
||||
unsigned int refs;
|
||||
REQUIRE(DNS_IPTABLE_VALID(tab));
|
||||
isc_refcount_decrement(&tab->refcount, &refs);
|
||||
if (refs == 0)
|
||||
destroy_iptable(tab);
|
||||
*tabp = NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
destroy_iptable(dns_iptable_t *dtab) {
|
||||
|
||||
REQUIRE(DNS_IPTABLE_VALID(dtab));
|
||||
|
||||
if (dtab->radix != NULL) {
|
||||
isc_destroy_radix(dtab->radix, NULL);
|
||||
dtab->radix = NULL;
|
||||
}
|
||||
|
||||
isc_refcount_destroy(&dtab->refcount);
|
||||
dtab->magic = 0;
|
||||
isc_mem_put(dtab->mctx, dtab, sizeof(*dtab));
|
||||
}
|
||||
188
lib/isc/include/isc/radix.h
Normal file
188
lib/isc/include/isc/radix.h
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
/*
|
||||
* Copyright (c) 1999-2000
|
||||
*
|
||||
* 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 TH E 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) HO WEVER 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This source was adapted from MRT's RCS Ids:
|
||||
* Id: radix.h,v 1.6 1999/08/03 03:32:53 masaki Exp
|
||||
* Id: mrt.h,v 1.57.2.6 1999/12/28 23:41:27 labovit Exp
|
||||
* Id: defs.h,v 1.5.2.2 2000/01/15 14:19:16 masaki Exp
|
||||
*/
|
||||
|
||||
#include <isc/magic.h>
|
||||
#include <isc/types.h>
|
||||
#include <isc/mutex.h>
|
||||
#include <isc/net.h>
|
||||
#include <isc/refcount.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#ifndef _RADIX_H
|
||||
#define _RADIX_H
|
||||
|
||||
#define NETADDR_TO_PREFIX_T(na,pt,bits) \
|
||||
do { \
|
||||
memset(&(pt), 0, sizeof(pt)); \
|
||||
if((bits) && (na) != NULL) { \
|
||||
memcpy(&(pt).add.sin, &(na)->type.in, ((bits)+7)/8); \
|
||||
(pt).bitlen = (bits); \
|
||||
(pt).family = (na)->family; \
|
||||
} else \
|
||||
(pt).family = AF_INET; \
|
||||
isc_refcount_init(&(pt).refcount, 0); \
|
||||
} while(0)
|
||||
|
||||
typedef struct isc_prefix {
|
||||
unsigned int family; /* AF_INET | AF_INET6 */
|
||||
unsigned int bitlen;
|
||||
isc_refcount_t refcount;
|
||||
union {
|
||||
struct in_addr sin;
|
||||
struct in6_addr sin6;
|
||||
} add;
|
||||
} isc_prefix_t;
|
||||
|
||||
typedef void (*void_fn_t)();
|
||||
|
||||
#define isc_prefix_tochar(prefix) ((char *)&(prefix)->add.sin)
|
||||
#define isc_prefix_touchar(prefix) ((u_char *)&(prefix)->add.sin)
|
||||
|
||||
#define BIT_TEST(f, b) ((f) & (b))
|
||||
|
||||
/*
|
||||
* We need "first match" when we search the radix tree to preserve
|
||||
* compatibility with the existing ACL implementation. Radix trees
|
||||
* naturally lend themselves to "best match". In order to get "first
|
||||
* match" behavior, we remember the entries are added to the tree,
|
||||
* and when a search is made, we find all matching entries, and return
|
||||
* the one that was added first.
|
||||
*/
|
||||
|
||||
typedef struct isc_radix_node {
|
||||
isc_uint32_t bit; /* bit length of the prefix */
|
||||
isc_prefix_t *prefix; /* who we are in radix tree */
|
||||
struct isc_radix_node *l, *r; /* left and right children */
|
||||
struct isc_radix_node *parent; /* may be used */
|
||||
void *data; /* pointer to data */
|
||||
int node_num; /* which node this was in the tree,
|
||||
or -1 for glue nodes */
|
||||
} isc_radix_node_t;
|
||||
|
||||
#define RADIX_TREE_MAGIC ISC_MAGIC('R','d','x','T');
|
||||
#define RADIX_TREE_VALID(a) ISC_MAGIC_VALID(a, RADIX_TREE_MAGIC);
|
||||
|
||||
typedef struct isc_radix_tree {
|
||||
unsigned int magic;
|
||||
isc_mem_t *mctx;
|
||||
isc_radix_node_t *head;
|
||||
isc_uint32_t maxbits; /* for IP, 32 bit addresses */
|
||||
int num_active_node; /* for debugging purposes */
|
||||
int num_added_node; /* total number of nodes */
|
||||
} isc_radix_tree_t;
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target, isc_prefix_t *prefix);
|
||||
|
||||
isc_result_t
|
||||
isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target, isc_radix_node_t *source, isc_prefix_t *prefix);
|
||||
|
||||
void
|
||||
isc_radix_remove(isc_radix_tree_t *radix, isc_radix_node_t *node);
|
||||
|
||||
isc_result_t
|
||||
isc_radix_create(isc_mem_t *mctx, isc_radix_tree_t **target, int maxbits);
|
||||
|
||||
void
|
||||
isc_destroy_radix(isc_radix_tree_t *radix, void_fn_t func);
|
||||
|
||||
void
|
||||
isc_radix_process(isc_radix_tree_t *radix, void_fn_t func);
|
||||
|
||||
|
||||
#define RADIX_MAXBITS 128
|
||||
#define RADIX_NBIT(x) (0x80 >> ((x) & 0x7f))
|
||||
#define RADIX_NBYTE(x) ((x) >> 3)
|
||||
|
||||
#define RADIX_DATA_GET(node, type) (type *)((node)->data)
|
||||
#define RADIX_DATA_SET(node, value) ((node)->data = (void *)(value))
|
||||
|
||||
#define RADIX_WALK(Xhead, Xnode) \
|
||||
do { \
|
||||
isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
|
||||
isc_radix_node_t **Xsp = Xstack; \
|
||||
isc_radix_node_t *Xrn = (Xhead); \
|
||||
while ((Xnode = Xrn)) { \
|
||||
if (Xnode->prefix)
|
||||
|
||||
#define RADIX_WALK_ALL(Xhead, Xnode) \
|
||||
do { \
|
||||
isc_radix_node_t *Xstack[RADIX_MAXBITS+1]; \
|
||||
isc_radix_node_t **Xsp = Xstack; \
|
||||
isc_radix_node_t *Xrn = (Xhead); \
|
||||
while ((Xnode = Xrn)) { \
|
||||
if (1)
|
||||
|
||||
#define RADIX_WALK_BREAK { \
|
||||
if (Xsp != Xstack) { \
|
||||
Xrn = *(--Xsp); \
|
||||
} else { \
|
||||
Xrn = (radix_node_t *) 0; \
|
||||
} \
|
||||
continue; }
|
||||
|
||||
#define RADIX_WALK_END \
|
||||
if (Xrn->l) { \
|
||||
if (Xrn->r) { \
|
||||
*Xsp++ = Xrn->r; \
|
||||
} \
|
||||
Xrn = Xrn->l; \
|
||||
} else if (Xrn->r) { \
|
||||
Xrn = Xrn->r; \
|
||||
} else if (Xsp != Xstack) { \
|
||||
Xrn = *(--Xsp); \
|
||||
} else { \
|
||||
Xrn = (isc_radix_node_t *) 0; \
|
||||
} \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#endif /* _RADIX_H */
|
||||
613
lib/isc/radix.c
Normal file
613
lib/isc/radix.c
Normal file
|
|
@ -0,0 +1,613 @@
|
|||
/*
|
||||
* Copyright (c) 1999-2000
|
||||
*
|
||||
* 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 TH E 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) HO WEVER 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* This source was adapted from MRT's RCS Ids:
|
||||
* Id: radix.c,v 1.10.2.1 1999/11/29 05:16:24 masaki Exp
|
||||
* Id: prefix.c,v 1.37.2.9 2000/03/10 02:53:19 labovit Exp
|
||||
*/
|
||||
|
||||
#include <isc/mem.h>
|
||||
#include <isc/types.h>
|
||||
#include <isc/util.h>
|
||||
#include <isc/radix.h>
|
||||
|
||||
static isc_result_t
|
||||
_new_prefix(isc_mem_t *mctx, isc_prefix_t **target, int family,
|
||||
void *dest, int bitlen);
|
||||
|
||||
static void
|
||||
_deref_prefix(isc_mem_t *mctx, isc_prefix_t *prefix);
|
||||
|
||||
static isc_result_t
|
||||
_ref_prefix(isc_mem_t *mctx, isc_prefix_t **target, isc_prefix_t *prefix);
|
||||
|
||||
static int
|
||||
_comp_with_mask(void *addr, void *dest, u_int mask);
|
||||
|
||||
static void
|
||||
_clear_radix(isc_radix_tree_t *radix, void_fn_t func);
|
||||
|
||||
static isc_result_t
|
||||
_new_prefix(isc_mem_t *mctx, isc_prefix_t **target, int family, void *dest,
|
||||
int bitlen)
|
||||
{
|
||||
isc_prefix_t *prefix;
|
||||
|
||||
if (family != AF_INET6 && family != AF_INET)
|
||||
return (ISC_R_NOTIMPLEMENTED);
|
||||
|
||||
prefix = isc_mem_get(mctx, sizeof(isc_prefix_t));
|
||||
if (prefix == NULL)
|
||||
return (ISC_R_NOMEMORY);
|
||||
|
||||
if (family == AF_INET6) {
|
||||
prefix->bitlen = (bitlen >= 0) ? bitlen : 128;
|
||||
memcpy(&prefix->add.sin6, dest, 16);
|
||||
} else {
|
||||
prefix->bitlen = (bitlen >= 0) ? bitlen : 32;
|
||||
memcpy(&prefix->add.sin, dest, 4);
|
||||
}
|
||||
|
||||
prefix->family = family;
|
||||
|
||||
isc_refcount_init(&prefix->refcount, 1);
|
||||
|
||||
*target = prefix;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
static void
|
||||
_deref_prefix(isc_mem_t *mctx, isc_prefix_t *prefix) {
|
||||
size_t size;
|
||||
int refs;
|
||||
|
||||
if (prefix == NULL)
|
||||
return;
|
||||
|
||||
isc_refcount_decrement(&prefix->refcount, &refs);
|
||||
|
||||
if (refs <= 0) {
|
||||
isc_refcount_destroy(&prefix->refcount);
|
||||
|
||||
if (prefix->family == AF_INET6)
|
||||
size = sizeof(isc_prefix_t);
|
||||
else
|
||||
size = sizeof(isc_prefix_t);
|
||||
|
||||
isc_mem_put(mctx, prefix, size);
|
||||
}
|
||||
}
|
||||
|
||||
static isc_result_t
|
||||
_ref_prefix(isc_mem_t *mctx, isc_prefix_t **target, isc_prefix_t *prefix) {
|
||||
|
||||
INSIST((prefix->family == AF_INET && prefix->bitlen <= 32) ||
|
||||
(prefix->family == AF_INET6 && prefix->bitlen <= 128));
|
||||
|
||||
if (prefix != NULL) {
|
||||
if (isc_refcount_current(&prefix->refcount) == 0) {
|
||||
/* Make a copy in case of a static prefix. */
|
||||
return (_new_prefix(mctx, target, prefix->family,
|
||||
&prefix->add, prefix->bitlen));
|
||||
}
|
||||
|
||||
isc_refcount_increment(&prefix->refcount, NULL);
|
||||
}
|
||||
|
||||
*target = prefix;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
static int
|
||||
_comp_with_mask(void *addr, void *dest, u_int mask) {
|
||||
|
||||
if (memcmp(addr, dest, mask / 8) == 0) {
|
||||
int n = mask / 8;
|
||||
int m = ((~0) << (8 - (mask % 8)));
|
||||
|
||||
if ((mask % 8) == 0 ||
|
||||
(((u_char *)addr)[n] & m) == (((u_char *)dest)[n] & m))
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_radix_create(isc_mem_t *mctx, isc_radix_tree_t **target, int maxbits) {
|
||||
isc_radix_tree_t *radix;
|
||||
|
||||
radix = isc_mem_get(mctx, sizeof(isc_radix_tree_t));
|
||||
if (radix == NULL)
|
||||
return (ISC_R_NOMEMORY);
|
||||
|
||||
radix->mctx = mctx;
|
||||
radix->maxbits = maxbits;
|
||||
radix->head = NULL;
|
||||
radix->num_active_node = 0;
|
||||
radix->num_added_node = 0;
|
||||
RUNTIME_CHECK(maxbits <= RADIX_MAXBITS); /* XXX */
|
||||
radix->magic = RADIX_TREE_MAGIC;
|
||||
|
||||
*target = radix;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
* if func is supplied, it will be called as func(node->data)
|
||||
* before deleting the node
|
||||
*/
|
||||
|
||||
static void
|
||||
_clear_radix(isc_radix_tree_t *radix, void_fn_t func) {
|
||||
|
||||
REQUIRE(radix);
|
||||
|
||||
if (radix->head) {
|
||||
|
||||
isc_radix_node_t *Xstack[RADIX_MAXBITS+1];
|
||||
isc_radix_node_t **Xsp = Xstack;
|
||||
isc_radix_node_t *Xrn = radix->head;
|
||||
|
||||
while (Xrn != NULL) {
|
||||
isc_radix_node_t *l = Xrn->l;
|
||||
isc_radix_node_t *r = Xrn->r;
|
||||
|
||||
if (Xrn->prefix) {
|
||||
_deref_prefix(radix->mctx, Xrn->prefix);
|
||||
if (Xrn->data && func)
|
||||
func(Xrn->data);
|
||||
} else {
|
||||
INSIST(Xrn->data == NULL);
|
||||
}
|
||||
|
||||
isc_mem_put(radix->mctx, Xrn, sizeof(*Xrn));
|
||||
radix->num_active_node--;
|
||||
|
||||
if (l != NULL) {
|
||||
if (r != NULL) {
|
||||
*Xsp++ = r;
|
||||
}
|
||||
Xrn = l;
|
||||
} else if (r != NULL) {
|
||||
Xrn = r;
|
||||
} else if (Xsp != Xstack) {
|
||||
Xrn = *(--Xsp);
|
||||
} else {
|
||||
Xrn = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
RUNTIME_CHECK(radix->num_active_node == 0);
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
isc_destroy_radix(isc_radix_tree_t *radix, void_fn_t func)
|
||||
{
|
||||
_clear_radix(radix, func);
|
||||
isc_mem_put(radix->mctx, radix, sizeof(*radix));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* func will be called as func(node->prefix, node->data)
|
||||
*/
|
||||
void
|
||||
isc_radix_process(isc_radix_tree_t *radix, void_fn_t func)
|
||||
{
|
||||
isc_radix_node_t *node;
|
||||
|
||||
REQUIRE(func);
|
||||
|
||||
RADIX_WALK(radix->head, node) {
|
||||
func(node->prefix, node->data);
|
||||
} RADIX_WALK_END;
|
||||
}
|
||||
|
||||
|
||||
isc_result_t
|
||||
isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
|
||||
isc_prefix_t *prefix) {
|
||||
isc_radix_node_t *node;
|
||||
isc_radix_node_t *stack[RADIX_MAXBITS + 1];
|
||||
u_char *addr;
|
||||
isc_uint32_t bitlen;
|
||||
int cnt = 0;
|
||||
|
||||
REQUIRE(radix != NULL);
|
||||
REQUIRE(prefix != NULL);
|
||||
RUNTIME_CHECK(prefix->bitlen <= radix->maxbits);
|
||||
|
||||
*target = NULL;
|
||||
|
||||
if (radix->head == NULL) {
|
||||
return (ISC_R_NOTFOUND);
|
||||
}
|
||||
|
||||
node = radix->head;
|
||||
addr = isc_prefix_touchar(prefix);
|
||||
bitlen = prefix->bitlen;
|
||||
|
||||
while (node->bit < bitlen) {
|
||||
if (node->prefix)
|
||||
stack[cnt++] = node;
|
||||
|
||||
if (BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
|
||||
node = node->r;
|
||||
else
|
||||
node = node->l;
|
||||
|
||||
if (node == NULL)
|
||||
break;
|
||||
}
|
||||
|
||||
if (node && node->prefix)
|
||||
stack[cnt++] = node;
|
||||
|
||||
while (--cnt >= 0) {
|
||||
node = stack[cnt];
|
||||
|
||||
if (_comp_with_mask(isc_prefix_tochar(node->prefix),
|
||||
isc_prefix_tochar(prefix),
|
||||
node->prefix->bitlen)) {
|
||||
if ((*target == NULL) ||
|
||||
(*target)->node_num > node->node_num)
|
||||
*target = node;
|
||||
}
|
||||
}
|
||||
|
||||
if (*target == NULL) {
|
||||
return (ISC_R_NOTFOUND);
|
||||
} else {
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
}
|
||||
|
||||
isc_result_t
|
||||
isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
|
||||
isc_radix_node_t *source, isc_prefix_t *prefix)
|
||||
{
|
||||
isc_radix_node_t *node, *new_node, *parent, *glue = NULL;
|
||||
u_char *addr, *test_addr;
|
||||
isc_uint32_t bitlen, check_bit, differ_bit;
|
||||
isc_uint32_t i, j, r;
|
||||
isc_result_t result;
|
||||
|
||||
REQUIRE(radix);
|
||||
REQUIRE(prefix || (source && source->prefix));
|
||||
RUNTIME_CHECK(prefix->bitlen <= radix->maxbits);
|
||||
|
||||
if (!prefix && source && source->prefix)
|
||||
prefix = source->prefix;
|
||||
|
||||
if (radix->head == NULL) {
|
||||
node = isc_mem_get(radix->mctx, sizeof(isc_radix_node_t));
|
||||
if (node == NULL)
|
||||
return (ISC_R_NOMEMORY);
|
||||
node->bit = prefix->bitlen;
|
||||
result = _ref_prefix(radix->mctx, &node->prefix, prefix);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
isc_mem_put(radix->mctx, node,
|
||||
sizeof(isc_radix_node_t));
|
||||
return (result);
|
||||
}
|
||||
node->parent = NULL;
|
||||
node->l = node->r = NULL;
|
||||
node->data = NULL;
|
||||
node->node_num = ++radix->num_added_node;
|
||||
radix->head = node;
|
||||
radix->num_active_node++;
|
||||
*target = node;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
addr = isc_prefix_touchar(prefix);
|
||||
bitlen = prefix->bitlen;
|
||||
node = radix->head;
|
||||
|
||||
while (node->bit < bitlen || node->prefix == NULL) {
|
||||
|
||||
if (node->bit < radix->maxbits &&
|
||||
BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
|
||||
{
|
||||
if (node->r == NULL)
|
||||
break;
|
||||
node = node->r;
|
||||
} else {
|
||||
if (node->l == NULL)
|
||||
break;
|
||||
node = node->l;
|
||||
}
|
||||
|
||||
INSIST(node != NULL);
|
||||
}
|
||||
|
||||
INSIST(node->prefix);
|
||||
|
||||
test_addr = isc_prefix_touchar(node->prefix);
|
||||
/* Find the first bit different. */
|
||||
check_bit = (node->bit < bitlen) ? node->bit : bitlen;
|
||||
differ_bit = 0;
|
||||
for (i = 0; i*8 < check_bit; i++) {
|
||||
if ((r = (addr[i] ^ test_addr[i])) == 0) {
|
||||
differ_bit = (i + 1) * 8;
|
||||
continue;
|
||||
}
|
||||
/* I know the better way, but for now. */
|
||||
for (j = 0; j < 8; j++) {
|
||||
if (BIT_TEST (r, (0x80 >> j)))
|
||||
break;
|
||||
}
|
||||
/* Must be found. */
|
||||
INSIST(j < 8);
|
||||
differ_bit = i * 8 + j;
|
||||
break;
|
||||
}
|
||||
|
||||
if (differ_bit > check_bit)
|
||||
differ_bit = check_bit;
|
||||
|
||||
parent = node->parent;
|
||||
while (parent && parent->bit >= differ_bit) {
|
||||
node = parent;
|
||||
parent = node->parent;
|
||||
}
|
||||
|
||||
if (differ_bit == bitlen && node->bit == bitlen) {
|
||||
if (node->prefix) {
|
||||
*target = node;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
result = _ref_prefix(radix->mctx, &node->prefix, prefix);
|
||||
if (result != ISC_R_SUCCESS)
|
||||
return (result);
|
||||
INSIST(node->data == NULL);
|
||||
*target = node;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
new_node = isc_mem_get(radix->mctx, sizeof(isc_radix_node_t));
|
||||
if (new_node == NULL)
|
||||
return (ISC_R_NOMEMORY);
|
||||
if (node->bit != differ_bit && bitlen != differ_bit) {
|
||||
glue = isc_mem_get(radix->mctx, sizeof(isc_radix_node_t));
|
||||
if (glue == NULL) {
|
||||
isc_mem_put(radix->mctx, new_node,
|
||||
sizeof(isc_radix_node_t));
|
||||
return (ISC_R_NOMEMORY);
|
||||
}
|
||||
}
|
||||
new_node->bit = prefix->bitlen;
|
||||
result = _ref_prefix(radix->mctx, &new_node->prefix, prefix);
|
||||
if (result != ISC_R_SUCCESS) {
|
||||
isc_mem_put(radix->mctx, new_node, sizeof(isc_radix_node_t));
|
||||
if (glue != NULL)
|
||||
isc_mem_put(radix->mctx, glue,
|
||||
sizeof(isc_radix_node_t));
|
||||
return (result);
|
||||
}
|
||||
new_node->parent = NULL;
|
||||
new_node->l = new_node->r = NULL;
|
||||
radix->num_active_node++;
|
||||
|
||||
if (source) {
|
||||
/*
|
||||
* If source is non-NULL, then we're merging in a node
|
||||
* from an existing radix tree. Node_num values have to
|
||||
* remain consistent; they can't just be added in whatever
|
||||
* order came from walking the tree. So we don't increment
|
||||
* num_added_node here; instead, we add it to the node-num
|
||||
* values for each node from the nested tree, and then when
|
||||
* the whole tree is done, the calling function will bump
|
||||
* num_added_node by the highest value of node_num in the
|
||||
* tree.
|
||||
*/
|
||||
new_node->node_num = radix->num_added_node + source->node_num;
|
||||
new_node->data = source->data;
|
||||
} else {
|
||||
new_node->node_num = ++radix->num_added_node;
|
||||
new_node->data = NULL;
|
||||
}
|
||||
|
||||
if (node->bit == differ_bit) {
|
||||
INSIST(glue == NULL);
|
||||
new_node->parent = node;
|
||||
if (node->bit < radix->maxbits &&
|
||||
BIT_TEST(addr[node->bit >> 3], 0x80 >> (node->bit & 0x07)))
|
||||
{
|
||||
INSIST(node->r == NULL);
|
||||
node->r = new_node;
|
||||
} else {
|
||||
INSIST(node->l == NULL);
|
||||
node->l = new_node;
|
||||
}
|
||||
*target = new_node;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
if (bitlen == differ_bit) {
|
||||
INSIST(glue == NULL);
|
||||
if (bitlen < radix->maxbits &&
|
||||
BIT_TEST(test_addr[bitlen >> 3], 0x80 >> (bitlen & 0x07))) {
|
||||
new_node->r = node;
|
||||
} else {
|
||||
new_node->l = node;
|
||||
}
|
||||
new_node->parent = node->parent;
|
||||
if (node->parent == NULL) {
|
||||
INSIST(radix->head == node);
|
||||
radix->head = new_node;
|
||||
} else if (node->parent->r == node) {
|
||||
node->parent->r = new_node;
|
||||
} else {
|
||||
node->parent->l = new_node;
|
||||
}
|
||||
node->parent = new_node;
|
||||
} else {
|
||||
INSIST(glue != NULL);
|
||||
glue->bit = differ_bit;
|
||||
glue->prefix = NULL;
|
||||
glue->parent = node->parent;
|
||||
glue->data = NULL;
|
||||
glue->node_num = -1;
|
||||
radix->num_active_node++;
|
||||
if (differ_bit < radix->maxbits &&
|
||||
BIT_TEST(addr[differ_bit >> 3], 0x80 >> (differ_bit & 0x07))) {
|
||||
glue->r = new_node;
|
||||
glue->l = node;
|
||||
} else {
|
||||
glue->r = node;
|
||||
glue->l = new_node;
|
||||
}
|
||||
new_node->parent = glue;
|
||||
|
||||
if (node->parent == NULL) {
|
||||
INSIST(radix->head == node);
|
||||
radix->head = glue;
|
||||
} else if (node->parent->r == node) {
|
||||
node->parent->r = glue;
|
||||
} else {
|
||||
node->parent->l = glue;
|
||||
}
|
||||
node->parent = glue;
|
||||
}
|
||||
|
||||
*target = new_node;
|
||||
return (ISC_R_SUCCESS);
|
||||
}
|
||||
|
||||
void
|
||||
isc_radix_remove(isc_radix_tree_t *radix, isc_radix_node_t *node) {
|
||||
isc_radix_node_t *parent, *child;
|
||||
|
||||
REQUIRE(radix != NULL);
|
||||
REQUIRE(node != NULL);
|
||||
|
||||
if (node->r && node->l) {
|
||||
/*
|
||||
* This might be a placeholder node -- have to check and
|
||||
* make sure there is a prefix aossciated with it!
|
||||
*/
|
||||
if (node->prefix != NULL)
|
||||
_deref_prefix(radix->mctx, node->prefix);
|
||||
|
||||
node->prefix = NULL;
|
||||
/* Also I needed to clear data pointer -- masaki */
|
||||
node->data = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
if (node->r == NULL && node->l == NULL) {
|
||||
parent = node->parent;
|
||||
_deref_prefix(radix->mctx, node->prefix);
|
||||
isc_mem_put(radix->mctx, node, sizeof(*node));
|
||||
radix->num_active_node--;
|
||||
|
||||
if (parent == NULL) {
|
||||
INSIST(radix->head == node);
|
||||
radix->head = NULL;
|
||||
return;
|
||||
}
|
||||
|
||||
if (parent->r == node) {
|
||||
parent->r = NULL;
|
||||
child = parent->l;
|
||||
} else {
|
||||
INSIST(parent->l == node);
|
||||
parent->l = NULL;
|
||||
child = parent->r;
|
||||
}
|
||||
|
||||
if (parent->prefix)
|
||||
return;
|
||||
|
||||
/* We need to remove parent too. */
|
||||
|
||||
if (parent->parent == NULL) {
|
||||
INSIST(radix->head == parent);
|
||||
radix->head = child;
|
||||
} else if (parent->parent->r == parent) {
|
||||
parent->parent->r = child;
|
||||
} else {
|
||||
INSIST(parent->parent->l == parent);
|
||||
parent->parent->l = child;
|
||||
}
|
||||
child->parent = parent->parent;
|
||||
isc_mem_put(radix->mctx, parent, sizeof(*parent));
|
||||
radix->num_active_node--;
|
||||
return;
|
||||
}
|
||||
|
||||
if (node->r) {
|
||||
child = node->r;
|
||||
} else {
|
||||
INSIST(node->l != NULL);
|
||||
child = node->l;
|
||||
}
|
||||
parent = node->parent;
|
||||
child->parent = parent;
|
||||
|
||||
_deref_prefix(radix->mctx, node->prefix);
|
||||
isc_mem_put(radix->mctx, node, sizeof(*node));
|
||||
radix->num_active_node--;
|
||||
|
||||
if (parent == NULL) {
|
||||
INSIST(radix->head == node);
|
||||
radix->head = child;
|
||||
return;
|
||||
}
|
||||
|
||||
if (parent->r == node) {
|
||||
parent->r = child;
|
||||
} else {
|
||||
INSIST(parent->l == node);
|
||||
parent->l = child;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Local Variables:
|
||||
c-basic-offset: 4
|
||||
indent-tabs-mode: t
|
||||
End:
|
||||
*/
|
||||
Loading…
Reference in a new issue