Compare commits
2 Commits
752d9ce8b5
...
84ec123c75
Author | SHA1 | Date |
---|---|---|
hladu357 | 84ec123c75 | |
hladu357 | 0c295a6827 |
34
trie/trie.c
34
trie/trie.c
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#define INPUT_FILE "../data/routing-data"
|
#define INPUT_FILE "../data/routing-data"
|
||||||
#define READ_MODE "r"
|
#define READ_MODE "r"
|
||||||
#define GET_BIT(key, bit) (( key[ bit / (sizeof(unsigned) * 8) ] & (1 << ((sizeof(unsigned) * 8 - 1) - (bit % (sizeof(unsigned) * 8)))) ) != 0 )
|
#define GET_BIT(key, bit) ( ( key.l[ bit / 64 ] & (1ul << (63 - bit % 64)) ) != 0 )
|
||||||
|
|
||||||
|
|
||||||
trie_node_t create_node() {
|
trie_node_t create_node() {
|
||||||
|
@ -17,11 +17,11 @@ trie_holder_t create_holder() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int trie_insert(trie_holder_t t, const unsigned key[4], 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 = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (;i <= 128 - 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();
|
||||||
|
@ -30,7 +30,10 @@ int trie_insert(trie_holder_t t, const unsigned key[4], int val, int mask) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iter->_rule_valid == VALID_RULE) {
|
if (iter->_rule_valid == VALID_RULE) {
|
||||||
printf("WARNING: conflicting rule %x:%x:%x:%x", key[0], key[1], key[2], key[3]);
|
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]);
|
||||||
|
|
||||||
printf("/%d %d\n", mask, val);
|
printf("/%d %d\n", mask, val);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -38,12 +41,10 @@ int trie_insert(trie_holder_t t, const unsigned key[4], int val, int mask) {
|
||||||
iter->_rule_valid = VALID_RULE;
|
iter->_rule_valid = VALID_RULE;
|
||||||
iter->_rule = val;
|
iter->_rule = val;
|
||||||
|
|
||||||
/* printf("inserted rule: %x:%x:%x:%x", key[0], key[1], key[2], key[3]); */
|
|
||||||
/* printf("/%d %d\n", mask, val); */
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int trie_search(trie_holder_t t, const unsigned key[4]) {
|
int trie_search(trie_holder_t t, ipv6_t key) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -51,27 +52,24 @@ int trie_search(trie_holder_t t, const unsigned key[4]) {
|
||||||
/*------------------------------------------------------------*/
|
/*------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|
||||||
int parse_line(unsigned key[4], 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));
|
||||||
unsigned short* tmp = (unsigned short*)key;
|
|
||||||
memset(tmp, 0, sizeof(short[8]));
|
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]);
|
||||||
|
|
||||||
fscanf(f, "%hx:%hx:%hx:%hx:%hx:%hx:%hx:%hx", &tmp[0], &tmp[1], &tmp[2], &tmp[3], &tmp[4], &tmp[5], &tmp[6], &tmp[7]);
|
|
||||||
if (feof(f)) return 0; /* ends with newline */
|
if (feof(f)) return 0; /* ends with newline */
|
||||||
while (fgetc(f) != '/');
|
while (fgetc(f) != '/');
|
||||||
|
|
||||||
fscanf(f, "%d %d", mask, val);
|
fscanf(f, "%d %d", mask, val);
|
||||||
|
|
||||||
/* printf("Loaded address: %x:%x:%x:%x", key[0], key[1], key[2], key[3]); */
|
|
||||||
/* printf("/%d %d\n", *mask, *val); */
|
|
||||||
|
|
||||||
return !feof(f);
|
return !feof(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int load_input(trie_holder_t t) {
|
int load_input(trie_holder_t t) {
|
||||||
int val;
|
int val;
|
||||||
int mask;
|
int mask;
|
||||||
unsigned key[4];
|
ipv6_t key;
|
||||||
|
|
||||||
FILE* f;
|
FILE* f;
|
||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
|
@ -79,7 +77,7 @@ int load_input(trie_holder_t t) {
|
||||||
if (!(f = fopen(INPUT_FILE, READ_MODE)))
|
if (!(f = fopen(INPUT_FILE, READ_MODE)))
|
||||||
perror("cant open input file");
|
perror("cant open input file");
|
||||||
|
|
||||||
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);
|
||||||
++cnt;
|
++cnt;
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@
|
||||||
|
|
||||||
union ipv6 {
|
union ipv6 {
|
||||||
unsigned short s[8];
|
unsigned short s[8];
|
||||||
unsigned long l[2];
|
unsigned int i[4];
|
||||||
|
unsigned long l[2];
|
||||||
};
|
};
|
||||||
|
|
||||||
#define VALID_RULE 1
|
#define VALID_RULE 1
|
||||||
|
@ -36,8 +37,8 @@ struct trie_node* create_node();
|
||||||
int destory_holder(struct trie_holder*);
|
int destory_holder(struct trie_holder*);
|
||||||
int destory_node(struct trie_node*);
|
int destory_node(struct trie_node*);
|
||||||
|
|
||||||
int trie_insert(struct trie_holder*, const unsigned key[4], int val, int mask);
|
int trie_insert(struct trie_holder*, ipv6_t key, int val, int mask);
|
||||||
rule_t trie_search(struct trie_holder*, const unsigned key[4]);
|
rule_t trie_search(struct trie_holder*, ipv6_t key);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue