CDN_DNS/radix/radix.c

293 lines
7.1 KiB
C
Raw Normal View History

2024-06-07 16:57:34 +02:00
#include "radix.h"
2024-06-09 10:34:36 +02:00
#define GET_BIT(key, mask) ( ((key[0] & mask[0]) | (key[1] & mask[1])) != 0 )
2024-06-09 16:28:41 +02:00
#define SHIFT_MASK(mask) mask[0] >>= 1, mask[1] = (mask[1] >> 1) | (!mask[0] * (1ul << 63))
2024-06-09 10:34:36 +02:00
int radix_insert(radix_holder_t, record_t);
int radix_search(radix_holder_t, record_t);
radix_holder_t create_holder() {
radix_holder_t ret = calloc(1, sizeof(struct radix_holder));
return ret;
}
2024-06-09 16:28:41 +02:00
static radix_node_t create_node(record_t record) {
2024-06-09 10:34:36 +02:00
radix_node_t ret = malloc(sizeof(struct radix_node));
memcpy(&ret->_data, record, sizeof(struct routing_record));
ret->_l = ret->_r = NULL;
2024-06-07 16:57:34 +02:00
return ret;
}
2024-06-07 18:18:28 +02:00
static radix_node_t clone_node(radix_node_t n) {
2024-06-09 10:34:36 +02:00
radix_node_t ret = malloc(sizeof(struct radix_node));
2024-06-07 16:57:34 +02:00
memcpy(ret, n, sizeof(struct radix_node));
return ret;
}
2024-06-07 18:18:28 +02:00
static void split_node(radix_node_t n, unsigned bit) {
2024-06-07 16:57:34 +02:00
radix_node_t tmp = clone_node(n);
2024-06-09 10:34:36 +02:00
n->_data._mask = bit;
2024-06-07 16:57:34 +02:00
n->_l = n->_r = tmp;
2024-06-09 17:36:35 +02:00
n->_data._rule = INVALID_RULE;
2024-06-07 16:57:34 +02:00
}
int destory_holder(struct radix_holder* t) {
destory_node(t->_root);
free(t);
return 1;
}
2024-06-09 16:28:41 +02:00
static int destory_node(struct radix_node* n) {
2024-06-07 16:57:34 +02:00
int ret = 0;
if (!n) return ret;
ret = destory_node(n->_l) + destory_node(n->_r);
free(n);
return ret;
}
2024-06-09 10:34:36 +02:00
int radix_insert(radix_holder_t t, record_t /*restrict*/ r) {
2024-06-07 16:57:34 +02:00
unsigned char b;
2024-06-09 10:34:36 +02:00
unsigned char ibit = 0;
unsigned long mask[2] = {0x01ul << 63, 0x00};
2024-06-09 16:28:41 +02:00
radix_node_t node;
radix_node_t* next;
if (!r || !t)
return 0;
node = t->_root;
next = &t->_root;
2024-06-07 16:57:34 +02:00
2024-06-09 10:34:36 +02:00
if (!node) {
t->_root = create_node(r);
2024-06-07 16:57:34 +02:00
return t->_size = 1;
}
2024-06-09 10:34:36 +02:00
/*todo zkontrolovat mezni hodnoty, masku 128 a tak.. */
for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) {
b = GET_BIT(r->_ip, mask);
if (ibit == node->_data._mask) { /* node ends */
2024-06-07 16:57:34 +02:00
next = b ? &node->_l : &node->_r;
2024-06-09 10:34:36 +02:00
if (!*next) { /* add child */
*next = create_node(r);
2024-06-07 16:57:34 +02:00
++t->_size;
return 1;
}
2024-06-09 10:34:36 +02:00
2024-06-07 16:57:34 +02:00
node = *next;
continue;
}
2024-06-09 10:34:36 +02:00
if (b != GET_BIT(node->_data._ip, mask)) {/* bit differs -> split node */
split_node(node, ibit);
next = b ? &node->_l : &node->_r; /* new node */
*next = create_node(r);
++t->_size;
return 1;
}
2024-06-07 16:57:34 +02:00
}
2024-06-09 10:34:36 +02:00
/* insert before */
if (node->_data._mask > r->_mask) {
2024-06-08 09:04:34 +02:00
split_node(node, ibit);
2024-06-09 10:34:36 +02:00
b = GET_BIT(node->_data._ip, mask);
2024-06-08 09:04:34 +02:00
next = b ? &node->_r : &node->_l;
*next = NULL;
2024-06-09 10:34:36 +02:00
memcpy(&node->_data, r, sizeof(struct routing_record));
++t->_size;
return 1;
2024-06-08 09:04:34 +02:00
}
2024-06-09 10:34:36 +02:00
/* exact match */
if (node->_data._mask == r->_mask && ibit == r->_mask) {
2024-06-09 17:36:35 +02:00
if (node->_data._rule != INVALID_RULE)
2024-06-09 10:34:36 +02:00
return 0;
/* make valid */
node->_data._rule = r->_rule;
++t->_size;
return 1;
}
printf("!!!!!!!!!!!!!!\n");
return 0;
2024-06-07 16:57:34 +02:00
}
2024-06-09 10:34:36 +02:00
int radix_search(radix_holder_t t, record_t r) {
2024-06-09 16:28:41 +02:00
radix_node_t iter;
int ibit, ret;
unsigned char b;
unsigned long mask[2] = {0x01ul << 63, 0x00};
if (!r)
2024-06-07 18:18:28 +02:00
return INVALID_RULE;
2024-06-09 16:28:41 +02:00
if (!t || !t->_root)
return r->_rule = INVALID_RULE;
2024-06-07 18:18:28 +02:00
2024-06-09 16:28:41 +02:00
iter = t->_root;
2024-06-09 17:36:35 +02:00
for (ibit = 0; ibit < r->_mask; ++ibit, SHIFT_MASK(mask)) {
2024-06-09 16:28:41 +02:00
b = GET_BIT(r->_ip, mask);
2024-06-09 17:36:35 +02:00
if (ibit < iter->_data._mask && /* bit match */
2024-06-09 16:28:41 +02:00
b == GET_BIT(iter->_data._ip, mask))
2024-06-07 18:18:28 +02:00
continue;
2024-06-09 17:36:35 +02:00
if (ibit < iter->_data._mask) { /* bit mismatch */
return r->_rule = INVALID_RULE;
}
2024-06-07 18:18:28 +02:00
2024-06-09 17:36:35 +02:00
/* mask ends with full match */
ret = r->_rule = iter->_data._rule != INVALID_RULE ? iter->_data._rule : INVALID_RULE;
iter = b ? iter->_l : iter->_r;
2024-06-07 18:18:28 +02:00
if (!iter)
return ret;
}
2024-06-09 17:36:35 +02:00
ret = r->_rule = iter->_data._rule != INVALID_RULE ? iter->_data._rule : INVALID_RULE;
2024-06-07 18:18:28 +02:00
return ret;
2024-06-07 16:57:34 +02:00
}
/*--------------------------------------------------------------------------------*/
/* TESTING */
/*--------------------------------------------------------------------------------*/
#include <assert.h>
2024-06-09 10:34:36 +02:00
#include <arpa/inet.h>
2024-06-07 16:57:34 +02:00
#define RULES_FILE "../data/routing-data"
#define TEST_FILE "../data/test-data"
2024-06-09 16:28:41 +02:00
#define TEST_ROWS 20 /*66319*/
2024-06-07 16:57:34 +02:00
#define READ_MODE "r"
2024-06-09 10:34:36 +02:00
int parse_line(FILE* f, record_t r) {
2024-06-09 16:28:41 +02:00
int ikey, jkey, mask, rule;
2024-06-09 10:34:36 +02:00
unsigned long key[2] = {0,0};
struct in6_addr addr;
char ipv6_cidr[50];
char *separator;
2024-06-07 16:57:34 +02:00
2024-06-09 10:34:36 +02:00
if (fgets(ipv6_cidr, sizeof(ipv6_cidr), f) == NULL) {
if (!feof(f))
printf("error reading\n");
return 0;
}
2024-06-07 16:57:34 +02:00
2024-06-09 10:34:36 +02:00
ipv6_cidr[strcspn(ipv6_cidr, "\n")] = 0;
separator = strchr(ipv6_cidr, '/');
if (!separator) {
printf("invalid CIDR notation\n");
2024-06-09 16:28:41 +02:00
return 0;
2024-06-09 10:34:36 +02:00
}
*separator++ = '\0';
2024-06-09 16:28:41 +02:00
mask = atoi(separator);
2024-06-07 16:57:34 +02:00
2024-06-09 10:34:36 +02:00
separator = strchr(separator, ' ');
2024-06-09 16:28:41 +02:00
rule = atoi(++separator);
2024-06-09 10:34:36 +02:00
2024-06-09 16:28:41 +02:00
if (mask < 0 || mask > 128) {
2024-06-09 10:34:36 +02:00
printf("invalid prefix length\n");
2024-06-09 16:28:41 +02:00
return 0;
2024-06-09 10:34:36 +02:00
}
2024-06-09 16:28:41 +02:00
r->_mask = mask;
2024-06-09 10:34:36 +02:00
2024-06-09 16:28:41 +02:00
if (rule < 0 || rule > 65536) {
2024-06-09 10:34:36 +02:00
printf("invalid pop\n");
2024-06-09 16:28:41 +02:00
return 0;
2024-06-09 10:34:36 +02:00
}
2024-06-09 16:28:41 +02:00
r->_rule = rule;
2024-06-09 10:34:36 +02:00
if (inet_pton(AF_INET6, ipv6_cidr, &addr) != 1) {
printf("%s invalid address\n", ipv6_cidr);
return 0;
}
for (ikey = 0; ikey < 2; ++ikey) {
for (jkey = 0; jkey < 8; ++jkey) {
key[ikey] = (key[ikey] << 8) | addr.__in6_u.__u6_addr8[jkey + (ikey * 8)];
}
}
r->_ip[0] = key[0];
r->_ip[1] = key[1];
2024-06-09 16:28:41 +02:00
return 1;
2024-06-07 16:57:34 +02:00
}
int load_input(radix_holder_t t) {
2024-06-09 10:34:36 +02:00
struct routing_record r;
2024-06-07 16:57:34 +02:00
FILE* f;
int cnt = 0;
if (!(f = fopen(RULES_FILE, READ_MODE))) {
puts("cant open input file");
2024-06-09 16:28:41 +02:00
return 0;
2024-06-07 16:57:34 +02:00
}
2024-06-09 10:34:36 +02:00
while (parse_line(f, &r)) {
if (radix_insert(t, &r))
++cnt;
else
printf("WARNING: conflicting insert\n");
2024-06-07 16:57:34 +02:00
}
fclose(f);
return cnt;
}
2024-06-09 16:28:41 +02:00
int load_tests(struct routing_record** ref) {
2024-06-07 16:57:34 +02:00
FILE* f;
2024-06-09 16:28:41 +02:00
int cnt = 0;
struct routing_record* r = malloc(sizeof(struct routing_record) * TEST_ROWS);
if (!(f = fopen(TEST_FILE, READ_MODE))) {
2024-06-07 16:57:34 +02:00
puts("cant open test file");
2024-06-09 16:28:41 +02:00
return 0;
2024-06-07 16:57:34 +02:00
}
2024-06-09 16:28:41 +02:00
for (cnt = 0; cnt < TEST_ROWS; ++cnt)
if (!parse_line(f, &r[cnt]))
printf("test parse failed %d\n", cnt);
2024-06-07 16:57:34 +02:00
fclose(f);
2024-06-09 16:28:41 +02:00
*ref = r;
return cnt;
2024-06-07 16:57:34 +02:00
}
2024-06-09 16:28:41 +02:00
int fire_tests(radix_holder_t t, struct routing_record* r) {
2024-06-07 16:57:34 +02:00
int row;
int wrong = 0;
2024-06-09 16:28:41 +02:00
unsigned short ref_pop;
2024-06-09 10:34:36 +02:00
2024-06-09 16:28:41 +02:00
for (row = 0; row < TEST_ROWS; ++row, ++r) {
ref_pop = r->_rule;
radix_search(t, r);
2024-06-09 17:36:35 +02:00
if ( ref_pop != r->_rule) {
2024-06-09 16:28:41 +02:00
printf("ref: %d got: %d @ %d\n", ref_pop, r->_rule, row );
2024-06-07 16:57:34 +02:00
++wrong;
}
}
2024-06-09 10:34:36 +02:00
2024-06-09 16:28:41 +02:00
/*
free(r);
2024-06-09 10:34:36 +02:00
*/
2024-06-09 16:28:41 +02:00
return wrong;
2024-06-09 10:34:36 +02:00
return 0;
2024-06-07 16:57:34 +02:00
}
int main() {
2024-06-09 16:28:41 +02:00
struct routing_record* ref;
2024-06-07 16:57:34 +02:00
radix_holder_t t = create_holder();
printf("Loaded %d rules\n", load_input(t) );
2024-06-09 16:28:41 +02:00
printf("Loaded %d tests\n", load_tests(&ref) );
printf("%d tests wrong\n", fire_tests(t, ref) );
2024-06-07 18:18:28 +02:00
2024-06-07 16:57:34 +02:00
destory_holder(t);
return 0;
}