Compare commits
No commits in common. "9e91d8f82629df8b0f25edf0d159d88215b9f0c7" and "657b8b17b9a8fc29761b31159c099ccae1903e4e" have entirely different histories.
9e91d8f826
...
657b8b17b9
110
trie/trie.c
110
trie/trie.c
|
@ -1,11 +1,8 @@
|
||||||
#include "trie.h"
|
#include "trie.h"
|
||||||
|
|
||||||
#define RULES_FILE "../data/routing-data"
|
#define INPUT_FILE "../data/routing-data"
|
||||||
#define TEST_FILE "../data/test-data"
|
|
||||||
#define TEST_ROWS 70 /*17217*/
|
|
||||||
#define READ_MODE "r"
|
#define READ_MODE "r"
|
||||||
#define RULE_NOT_FOUND -1
|
#define GET_BIT(key, bit) ( ( key.l[ bit / 64 ] & (1ul << (63 - bit % 64)) ) != 0 )
|
||||||
#define GET_BIT(key, bit) ( ( key.s[ bit / 16 ] & (1ul << (15 - bit % 16)) ) != 0 )
|
|
||||||
|
|
||||||
|
|
||||||
trie_node_t create_node() {
|
trie_node_t create_node() {
|
||||||
|
@ -23,12 +20,12 @@ trie_holder_t create_holder() {
|
||||||
int trie_insert(trie_holder_t t, ipv6_t key, int val, int mask) {
|
int trie_insert(trie_holder_t t, ipv6_t key, int val, int mask) {
|
||||||
trie_node_t iter = t->_root;
|
trie_node_t iter = t->_root;
|
||||||
trie_node_t* next = &t->_root;
|
trie_node_t* next = &t->_root;
|
||||||
int i;
|
int i = 0;
|
||||||
|
|
||||||
for (i = 0; i < mask; ++i) {
|
for (;i <= 128 - mask; ++i) {
|
||||||
if (!*(next = GET_BIT(key, i) ? &iter->_l : &iter->_r))
|
if (!*(next = GET_BIT(key, i) ? &iter->_l : &iter->_r))
|
||||||
*next = create_node();
|
*next = create_node();
|
||||||
|
|
||||||
iter = *next;
|
iter = *next;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,37 +45,19 @@ int trie_insert(trie_holder_t t, ipv6_t key, int val, int mask) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int trie_search(trie_holder_t t, ipv6_t key) {
|
int trie_search(trie_holder_t t, ipv6_t key) {
|
||||||
trie_node_t iter = t->_root;
|
return 0;
|
||||||
trie_node_t next;
|
|
||||||
int i = 0;
|
|
||||||
int ret = RULE_NOT_FOUND;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------------*/
|
|
||||||
/* TESTING */
|
|
||||||
/*--------------------------------------------------------------------------------*/
|
|
||||||
#include <assert.h>
|
|
||||||
|
|
||||||
int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */
|
int parse_line(ipv6_t* key, int* val, int* mask, FILE* f) { /*! assumes only good input */
|
||||||
memset(key, 0, sizeof(ipv6_t));
|
memset(key, 0, sizeof(ipv6_t));
|
||||||
|
|
||||||
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
|
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx",
|
||||||
&key->s[0], &key->s[1], &key->s[2], &key->s[3],
|
&key->s[0], &key->s[1], &key->s[2], &key->s[3], &key->s[4],
|
||||||
&key->s[4], &key->s[5], &key->s[6], &key->s[7]);
|
&key->s[5], &key->s[6], &key->s[7]);
|
||||||
|
|
||||||
if (feof(f)) return 0; /* ends with newline */
|
if (feof(f)) return 0; /* ends with newline */
|
||||||
while (fgetc(f) != '/');
|
while (fgetc(f) != '/');
|
||||||
|
@ -91,13 +70,12 @@ int load_input(trie_holder_t t) {
|
||||||
int val;
|
int val;
|
||||||
int mask;
|
int mask;
|
||||||
ipv6_t key;
|
ipv6_t key;
|
||||||
|
|
||||||
FILE* f;
|
FILE* f;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
|
||||||
if (!(f = fopen(RULES_FILE, READ_MODE))) {
|
if (!(f = fopen(INPUT_FILE, READ_MODE)))
|
||||||
puts("cant open input file");
|
perror("cant open input file");
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (parse_line(&key, &val, &mask, f)) {
|
while (parse_line(&key, &val, &mask, f)) {
|
||||||
trie_insert(t, key, val, mask);
|
trie_insert(t, key, val, mask);
|
||||||
|
@ -106,58 +84,22 @@ int load_input(trie_holder_t t) {
|
||||||
return cnt;
|
return cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_tests(ipv6_t** rips, int** rref) {
|
|
||||||
ipv6_t* ips;
|
|
||||||
int* ref;
|
|
||||||
int row;
|
|
||||||
FILE* f;
|
|
||||||
|
|
||||||
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]);
|
|
||||||
|
|
||||||
|
|
||||||
while (fgetc(f) != ' ');
|
|
||||||
fscanf(f, "%d", &(ref[row]));
|
|
||||||
}
|
|
||||||
|
|
||||||
*rips = ips;
|
|
||||||
*rref = ref;
|
|
||||||
return TEST_ROWS;
|
|
||||||
}
|
|
||||||
|
|
||||||
int fire_tests(trie_holder_t t, ipv6_t* ips, int* ref, int num_tests ) {
|
|
||||||
int row;
|
|
||||||
int wrong = 0;
|
|
||||||
|
|
||||||
for (row = 0; row < TEST_ROWS; ++ row) {
|
|
||||||
if (trie_search(t, ips[row]) != ref[row]) {
|
|
||||||
printf("%d %d @ %d\n", trie_search(t, ips[row]), ref[row], row );
|
|
||||||
++wrong;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return wrong;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
ipv6_t* ips;
|
|
||||||
int* ref;
|
|
||||||
|
|
||||||
trie_holder_t t = create_holder();
|
trie_holder_t t = create_holder();
|
||||||
printf("Loaded %d rules\n", load_input(t) );
|
|
||||||
printf("Loaded %d tests\n", load_tests(&ips, &ref) );
|
|
||||||
printf("%d tests wrong\n", fire_tests(t, ips, ref, TEST_ROWS) );
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
unsigned key[4] = {8927349, 9872346, 898097878, 928398629};
|
||||||
|
int i;
|
||||||
|
00000000100010000011100001110101 00000000100101101010001111011010 00110101100001111110001011010110 00110111010101100011110100100101
|
||||||
|
00000000100010000011100001110101 00000000100101101010001111011010 00110101100001111110001011010110 00110111010101100011110100100101
|
||||||
|
|
||||||
|
for (i = 0; i < 128; ++i) {
|
||||||
|
printf("%d", GET_BIT(key, i));
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
return 0;
|
||||||
|
*/
|
||||||
|
|
||||||
|
printf("Loaded %d rules\n", load_input(t) );
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue