Commit b75a2b97 authored by Evan Hunt's avatar Evan Hunt

improve readability of radix code

- use RADIX_V4, RADIX_V6, RADIX_V4_ECS, and RADIX_V6_ECS as array
  indices instead of 0 through 3.
- remove some unused macros

(cherry picked from commit f7f20b12)
parent 8da265c2
......@@ -234,12 +234,13 @@ dns_acl_match2(const isc_netaddr_t *reqaddr,
/* Found a match. */
if (result == ISC_R_SUCCESS && node != NULL) {
int off = ISC_RADIX_OFF(&pfx);
match_num = node->node_num[off];
if (*(isc_boolean_t *) node->data[off])
int fam = ISC_RADIX_FAMILY(&pfx);
match_num = node->node_num[fam];
if (*(isc_boolean_t *) node->data[fam]) {
*match = match_num;
else
} else {
*match = -match_num;
}
}
isc_refcount_destroy(&pfx.refcount);
......@@ -264,17 +265,19 @@ dns_acl_match2(const isc_netaddr_t *reqaddr,
result = isc_radix_search(acl->iptable->radix, &node, &pfx);
if (result == ISC_R_SUCCESS && node != NULL) {
int off = ISC_RADIX_OFF(&pfx);
int off = ISC_RADIX_FAMILY(&pfx);
if (match_num == -1 ||
node->node_num[off] < match_num)
{
match_num = node->node_num[off];
if (scope != NULL)
if (scope != NULL) {
*scope = node->bit;
if (*(isc_boolean_t *) node->data[off])
}
if (*(isc_boolean_t *) node->data[off]) {
*match = match_num;
else
} else {
*match = -match_num;
}
}
}
......
......@@ -86,16 +86,17 @@ dns_iptable_addprefix2(dns_iptable_t *tab, isc_netaddr_t *addr,
if (pfx.family == AF_UNSPEC) {
/* "any" or "none" */
INSIST(pfx.bitlen == 0);
for (i = 0; i < 4; i++) {
if (node->data[i] == NULL)
for (i = 0; i < RADIX_FAMILIES; i++) {
if (node->data[i] == NULL) {
node->data[i] = pos ? &dns_iptable_pos
: &dns_iptable_neg;
}
}
} else {
/* any other prefix */
int offset = ISC_RADIX_OFF(&pfx);
if (node->data[offset] == NULL) {
node->data[offset] = pos ? &dns_iptable_pos
int fam = ISC_RADIX_FAMILY(&pfx);
if (node->data[fam] == NULL) {
node->data[fam] = pos ? &dns_iptable_pos
: &dns_iptable_neg;
}
}
......@@ -129,7 +130,7 @@ dns_iptable_merge(dns_iptable_t *tab, dns_iptable_t *source, isc_boolean_t pos)
* could be a security risk. To prevent this, we
* just leave the negative nodes negative.
*/
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
if (!pos) {
if (node->data[i] &&
*(isc_boolean_t *) node->data[i])
......
......@@ -9,13 +9,6 @@
* information regarding copyright ownership.
*/
/*
* 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>
......@@ -92,8 +85,17 @@ typedef void (*isc_radix_processfunc_t)(isc_prefix_t *, void **);
* addresses.
*/
#define ISC_RADIX_OFF(p) \
((((p)->family == AF_INET6) ? 1 : 0) + ((p)->ecs ? 2 : 0))
#define RADIX_NOECS 0
#define RADIX_ECS 2
#define RADIX_V4 0
#define RADIX_V6 1
#define RADIX_V4_ECS 2
#define RADIX_V6_ECS 3
#define RADIX_FAMILIES 4
#define ISC_RADIX_FAMILY(p) \
((((p)->family == AF_INET6) ? RADIX_V6 : RADIX_V4) + \
((p)->ecs ? RADIX_ECS : RADIX_NOECS))
typedef struct isc_radix_node {
isc_mem_t *mctx;
......@@ -101,8 +103,8 @@ typedef struct isc_radix_node {
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[4]; /* pointers to IPv4 and IPV6 data */
int node_num[4]; /* which node this was in the tree,
void *data[RADIX_FAMILIES]; /* pointers to IPv4 and IPV6 data */
int node_num[RADIX_FAMILIES]; /* which node this was in the tree,
or -1 for glue nodes */
} isc_radix_node_t;
......@@ -201,9 +203,6 @@ isc_radix_process(isc_radix_tree_t *radix, isc_radix_processfunc_t func);
#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]; \
......@@ -212,22 +211,6 @@ isc_radix_process(isc_radix_tree_t *radix, isc_radix_processfunc_t func);
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) { \
......
......@@ -163,7 +163,6 @@ isc_radix_create(isc_mem_t *mctx, isc_radix_tree_t **target, int maxbits) {
static void
_clear_radix(isc_radix_tree_t *radix, isc_radix_destroyfunc_t func) {
REQUIRE(radix != NULL);
if (radix->head != NULL) {
......@@ -180,10 +179,10 @@ _clear_radix(isc_radix_tree_t *radix, isc_radix_destroyfunc_t func) {
if (func != NULL)
func(Xrn->data);
} else {
INSIST(Xrn->data[0] == NULL &&
Xrn->data[1] == NULL &&
Xrn->data[2] == NULL &&
Xrn->data[3] == NULL);
INSIST(Xrn->data[RADIX_V4] == NULL &&
Xrn->data[RADIX_V6] == NULL &&
Xrn->data[RADIX_V4_ECS] == NULL &&
Xrn->data[RADIX_V6_ECS] == NULL);
}
isc_mem_put(radix->mctx, Xrn, sizeof(*Xrn));
......@@ -238,7 +237,7 @@ isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
isc_radix_node_t *stack[RADIX_MAXBITS + 1];
u_char *addr;
isc_uint32_t bitlen;
int toff = -1, cnt = 0;
int tfam = -1, cnt = 0;
REQUIRE(radix != NULL);
REQUIRE(prefix != NULL);
......@@ -281,13 +280,13 @@ isc_radix_search(isc_radix_tree_t *radix, isc_radix_node_t **target,
isc_prefix_tochar(prefix),
node->prefix->bitlen))
{
int off = ISC_RADIX_OFF(prefix);
if (node->node_num[off] != -1 &&
int fam = ISC_RADIX_FAMILY(prefix);
if (node->node_num[fam] != -1 &&
((*target == NULL) ||
(*target)->node_num[toff] > node->node_num[off]))
(*target)->node_num[tfam] > node->node_num[fam]))
{
*target = node;
toff = off;
tfam = fam;
}
}
}
......@@ -327,8 +326,9 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (node == NULL)
return (ISC_R_NOMEMORY);
node->bit = bitlen;
for (i = 0; i < 4; i++)
for (i = 0; i < RADIX_FAMILIES; i++) {
node->node_num[i] = -1;
}
node->prefix = NULL;
result = _ref_prefix(radix->mctx, &node->prefix, prefix);
if (result != ISC_R_SUCCESS) {
......@@ -347,21 +347,23 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
* added to num_added_node at the end of
* the merge operation--we don't do it here.
*/
for (i = 0; i < 4; i++) {
if (source->node_num[i] != -1)
for (i = 0; i < RADIX_FAMILIES; i++) {
if (source->node_num[i] != -1) {
node->node_num[i] =
radix->num_added_node +
source->node_num[i];
}
node->data[i] = source->data[i];
}
} else {
int next = ++radix->num_added_node;
if (fam == AF_UNSPEC) {
/* "any" or "none" */
for (i = 0; i < 4; i++)
for (i = 0; i < RADIX_FAMILIES; i++) {
node->node_num[i] = next;
}
} else {
node->node_num[ISC_RADIX_OFF(prefix)] = next;
node->node_num[ISC_RADIX_FAMILY(prefix)] = next;
}
memset(node->data, 0, sizeof(node->data));
......@@ -397,14 +399,14 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
/* 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) {
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)))
if (BIT_TEST(r, (0x80 >> j)))
break;
}
/* Must be found. */
......@@ -427,7 +429,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
/* Set node_num only if it hasn't been set before */
if (source != NULL) {
/* Merging nodes */
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
if (node->node_num[i] == -1 &&
source->node_num[i] != -1) {
node->node_num[i] =
......@@ -440,7 +442,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (fam == AF_UNSPEC) {
/* "any" or "none" */
int next = radix->num_added_node + 1;
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
if (node->node_num[i] == -1) {
node->node_num[i] =
next;
......@@ -449,10 +451,11 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
}
}
} else {
int off = ISC_RADIX_OFF(prefix);
if (node->node_num[off] == -1)
node->node_num[off] =
int foff = ISC_RADIX_FAMILY(prefix);
if (node->node_num[foff] == -1) {
node->node_num[foff] =
++radix->num_added_node;
}
}
}
*target = node;
......@@ -463,13 +466,20 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (result != ISC_R_SUCCESS)
return (result);
}
INSIST(node->data[0] == NULL && node->node_num[0] == -1 &&
node->data[1] == NULL && node->node_num[1] == -1 &&
node->data[2] == NULL && node->node_num[2] == -1 &&
node->data[3] == NULL && node->node_num[3] == -1);
/* Check everything is null and empty before we proceed */
INSIST(node->data[RADIX_V4] == NULL &&
node->node_num[RADIX_V4] == -1 &&
node->data[RADIX_V6] == NULL &&
node->node_num[RADIX_V6] == -1 &&
node->data[RADIX_V4_ECS] == NULL &&
node->node_num[RADIX_V4_ECS] == -1 &&
node->data[RADIX_V6_ECS] == NULL &&
node->node_num[RADIX_V6_ECS] == -1);
if (source != NULL) {
/* Merging node */
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
int cur = radix->num_added_node;
if (source->node_num[i] != -1) {
node->node_num[i] =
......@@ -481,10 +491,11 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
int next = ++radix->num_added_node;
if (fam == AF_UNSPEC) {
/* "any" or "none" */
for (i = 0; i < 4; i++)
for (i = 0; i < RADIX_FAMILIES; i++) {
node->node_num[i] = next;
}
} else {
node->node_num[ISC_RADIX_OFF(prefix)] = next;
node->node_num[ISC_RADIX_FAMILY(prefix)] = next;
}
}
*target = node;
......@@ -514,7 +525,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
}
new_node->parent = NULL;
new_node->l = new_node->r = NULL;
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
new_node->node_num[i] = -1;
new_node->data[i] = NULL;
}
......@@ -522,7 +533,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
if (source != NULL) {
/* Merging node */
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
int cur = radix->num_added_node;
if (source->node_num[i] != -1) {
new_node->node_num[i] =
......@@ -534,10 +545,11 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
int next = ++radix->num_added_node;
if (fam == AF_UNSPEC) {
/* "any" or "none" */
for (i = 0; i < 4; i++)
for (i = 0; i < RADIX_FAMILIES; i++) {
new_node->node_num[i] = next;
}
} else {
new_node->node_num[ISC_RADIX_OFF(prefix)] = next;
new_node->node_num[ISC_RADIX_FAMILY(prefix)] = next;
}
memset(new_node->data, 0, sizeof(new_node->data));
}
......@@ -581,7 +593,7 @@ isc_radix_insert(isc_radix_tree_t *radix, isc_radix_node_t **target,
glue->bit = differ_bit;
glue->prefix = NULL;
glue->parent = node->parent;
for (i = 0; i < 4; i++) {
for (i = 0; i < RADIX_FAMILIES; i++) {
glue->data[i] = NULL;
glue->node_num[i] = -1;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment