2024-06-05 14:40:59 +02:00
|
|
|
#include "trie.h"
|
|
|
|
|
|
|
|
#define INPUT_FILE "../data/routing-data"
|
|
|
|
#define READ_MODE "r"
|
2024-06-05 15:01:35 +02:00
|
|
|
#define GET_BIT(key, bit) ( ( key.l[ bit / 64 ] & (1ul << (63 - bit % 64)) ) != 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;
|
|
|
|
int i = 0;
|
2024-06-05 14:49:30 +02:00
|
|
|
|
2024-06-05 14:40:59 +02:00
|
|
|
for (;i <= 128 - mask; ++i) {
|
|
|
|
if (!*(next = GET_BIT(key, i) ? &iter->_l : &iter->_r))
|
|
|
|
*next = create_node();
|
|
|
|
|
|
|
|
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-05 14:40:59 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
&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;
|
|
|
|
|
|
|
|
if (!(f = fopen(INPUT_FILE, READ_MODE)))
|
|
|
|
perror("cant open input file");
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
trie_holder_t t = create_holder();
|
|
|
|
|
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|