CDN_DNS/trie/trie.c

163 lines
3.9 KiB
C
Raw Normal View History

2024-06-05 14:40:59 +02:00
#include "trie.h"
2024-06-06 10:33:28 +02:00
#define RULES_FILE "../data/routing-data"
#define TEST_FILE "../data/test-data"
2024-06-06 11:49:08 +02:00
#define TEST_ROWS 70 /*17217*/
2024-06-05 14:40:59 +02:00
#define READ_MODE "r"
2024-06-06 11:49:08 +02:00
#define RULE_NOT_FOUND -1
#define GET_BIT(key, bit) ( ( key.s[ bit / 16 ] & (1ul << (15 - bit % 16)) ) != 0 )
2024-06-05 14:40:59 +02:00
trie_node_t create_node() {
trie_node_t ret = calloc(1, sizeof(struct trie_node)); /* sets null childs and invalid */
return ret;
}
trie_holder_t create_holder() {
trie_holder_t ret = malloc(sizeof(struct trie_holder));
ret->_size = 0;
ret->_root = create_node();
return ret;
}
2024-06-05 14:49:30 +02:00
int trie_insert(trie_holder_t t, ipv6_t key, int val, int mask) {
2024-06-05 14:40:59 +02:00
trie_node_t iter = t->_root;
trie_node_t* next = &t->_root;
2024-06-06 10:33:28 +02:00
int i;
2024-06-05 14:49:30 +02:00
2024-06-06 10:33:28 +02:00
for (i = 0; i < mask; ++i) {
2024-06-05 14:40:59 +02:00
if (!*(next = GET_BIT(key, i) ? &iter->_l : &iter->_r))
*next = create_node();
2024-06-06 10:33:28 +02:00
2024-06-05 14:40:59 +02:00
iter = *next;
}
if (iter->_rule_valid == VALID_RULE) {
2024-06-05 14:49:30 +02:00
printf("WARNING: conflicting rule %x:%x:%x:%x:%x:%x:%x:%x",
key.s[0], key.s[1], key.s[2], key.s[3],
key.s[4], key.s[5], key.s[6], key.s[7]);
2024-06-05 14:40:59 +02:00
printf("/%d %d\n", mask, val);
return 0;
}
iter->_rule_valid = VALID_RULE;
iter->_rule = val;
return 1;
}
2024-06-05 14:49:30 +02:00
int trie_search(trie_holder_t t, ipv6_t key) {
2024-06-06 10:33:28 +02:00
trie_node_t iter = t->_root;
trie_node_t next;
int i = 0;
2024-06-06 11:49:08 +02:00
int ret = RULE_NOT_FOUND;
2024-06-06 10:33:28 +02:00
for (;i < 128; ++i) {
if (iter->_rule_valid)
ret = iter->_rule;
if (!(next = GET_BIT(key, i) ? iter->_l : iter->_r))
break;
iter = next;
}
return ret;
2024-06-05 14:40:59 +02:00
}
2024-06-06 11:49:08 +02:00
/*--------------------------------------------------------------------------------*/
/* TESTING */
/*--------------------------------------------------------------------------------*/
#include <assert.h>
2024-06-05 14:49:30 +02:00
int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */
memset(key, 0, sizeof(ipv6_t));
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
2024-06-06 10:33:28 +02:00
&key->s[0], &key->s[1], &key->s[2], &key->s[3],
&key->s[4], &key->s[5], &key->s[6], &key->s[7]);
2024-06-05 14:40:59 +02:00
if (feof(f)) return 0; /* ends with newline */
while (fgetc(f) != '/');
fscanf(f, "%d %d", mask, val);
return !feof(f);
}
int load_input(trie_holder_t t) {
int val;
int mask;
2024-06-05 14:49:30 +02:00
ipv6_t key;
2024-06-05 14:40:59 +02:00
FILE* f;
int cnt = 0;
2024-06-06 10:33:28 +02:00
if (!(f = fopen(RULES_FILE, READ_MODE))) {
puts("cant open input file");
exit(1);
}
2024-06-05 14:40:59 +02:00
2024-06-05 14:49:30 +02:00
while (parse_line(&key, &val, &mask, f)) {
2024-06-05 14:40:59 +02:00
trie_insert(t, key, val, mask);
++cnt;
}
return cnt;
}
2024-06-06 10:33:28 +02:00
int load_tests(ipv6_t** rips, int** rref) {
ipv6_t* ips;
int* ref;
int row;
FILE* f;
2024-06-05 14:40:59 +02:00
2024-06-06 10:33:28 +02:00
if (!(f = fopen( TEST_FILE, READ_MODE ))) {
puts("cant open test file");
exit(1);
}
ips = calloc( TEST_ROWS, sizeof(ipv6_t) );
ref = calloc( TEST_ROWS, sizeof(int) );
for (row = 0; row < TEST_ROWS; ++ row) {
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
&ips[row].s[0], &ips[row].s[1], &ips[row].s[2], &ips[row].s[3],
&ips[row].s[4], &ips[row].s[5], &ips[row].s[6], &ips[row].s[7]);
2024-06-06 11:49:08 +02:00
2024-06-06 10:33:28 +02:00
while (fgetc(f) != ' ');
fscanf(f, "%d", &(ref[row]));
}
2024-06-05 14:40:59 +02:00
2024-06-06 10:33:28 +02:00
*rips = ips;
*rref = ref;
return TEST_ROWS;
}
int fire_tests(trie_holder_t t, ipv6_t* ips, int* ref, int num_tests ) {
int row;
2024-06-06 11:49:08 +02:00
int wrong = 0;
2024-06-06 10:33:28 +02:00
for (row = 0; row < TEST_ROWS; ++ row) {
2024-06-06 11:49:08 +02:00
if (trie_search(t, ips[row]) != ref[row]) {
printf("%d %d @ %d\n", trie_search(t, ips[row]), ref[row], row );
++wrong;
}
2024-06-05 14:40:59 +02:00
}
2024-06-06 11:49:08 +02:00
return wrong;
2024-06-06 10:33:28 +02:00
}
int main() {
ipv6_t* ips;
2024-06-06 11:49:08 +02:00
int* ref;
2024-06-06 10:33:28 +02:00
trie_holder_t t = create_holder();
2024-06-05 14:40:59 +02:00
printf("Loaded %d rules\n", load_input(t) );
2024-06-06 10:33:28 +02:00
printf("Loaded %d tests\n", load_tests(&ips, &ref) );
2024-06-06 11:49:08 +02:00
printf("%d tests wrong\n", fire_tests(t, ips, ref, TEST_ROWS) );
2024-06-06 10:33:28 +02:00
2024-06-05 14:40:59 +02:00
return 0;
}