From 281d236835e546a71b96da4045b4998752f89eba Mon Sep 17 00:00:00 2001 From: netblue30 Date: Sun, 16 Jan 2022 08:53:39 -0500 Subject: more on nettrace --- src/fnettrace/radix.c | 104 +++++--------------------------------------------- 1 file changed, 10 insertions(+), 94 deletions(-) (limited to 'src/fnettrace/radix.c') diff --git a/src/fnettrace/radix.c b/src/fnettrace/radix.c index 96d6bcf41..c800c8708 100644 --- a/src/fnettrace/radix.c +++ b/src/fnettrace/radix.c @@ -25,6 +25,12 @@ #include "radix.h" #include "fnettrace.h" +typedef struct rnode_t { + struct rnode_t *zero; + struct rnode_t *one; + char *name; +} RNode; + RNode *head = 0; int radix_nodes = 0; @@ -35,10 +41,7 @@ static inline RNode *addOne(RNode *ptr, uint32_t ip, uint32_t mask, char *name) RNode *node = malloc(sizeof(RNode)); if (!node) errExit("malloc"); - radix_nodes++; memset(node, 0, sizeof(RNode)); - node->ip = ip; - node->mask = mask; if (name) { node->name = strdup(name); if (!node->name) @@ -57,8 +60,6 @@ static inline RNode *addZero(RNode *ptr, uint32_t ip, uint32_t mask, char *name) if (!node) errExit("malloc"); memset(node, 0, sizeof(RNode)); - node->ip = ip; - node->mask = mask; if (name) { node->name = strdup(name); if (!node->name) @@ -71,7 +72,7 @@ static inline RNode *addZero(RNode *ptr, uint32_t ip, uint32_t mask, char *name) // add to radix tree -void radix_add(uint32_t ip, uint32_t mask, char *name) { +char *radix_add(uint32_t ip, uint32_t mask, char *name) { assert(name); uint32_t m = 0x80000000; uint32_t lastm = 0; @@ -80,6 +81,7 @@ void radix_add(uint32_t ip, uint32_t mask, char *name) { memset(head, 0, sizeof(RNode)); } RNode *ptr = head; + radix_nodes++; int i; for (i = 0; i < 32; i++, m >>= 1) { @@ -99,32 +101,12 @@ void radix_add(uint32_t ip, uint32_t mask, char *name) { if (!ptr->name) errExit("strdup"); } -} - -// find first match -char *radix_find_first(uint32_t ip) { - if (!head) - return NULL; - uint32_t m = 0x80000000; - RNode *ptr = head; - - int i; - for (i = 0; i < 32; i++, m >>= 1) { - if (m & ip) - ptr = ptr->one; - else - ptr = ptr->zero; - if (!ptr) - return NULL; - if (ptr->name) - return ptr->name; - } - return NULL; + return ptr->name; } // find last match -char *radix_find_last(uint32_t ip) { +char *radix_longest_prefix_match(uint32_t ip) { if (!head) return NULL; @@ -147,69 +129,3 @@ char *radix_find_last(uint32_t ip) { return (rv)? rv->name: NULL; } -static void radix_print_node(RNode *ptr, int level) { - assert(ptr); - - int i; - for (i = 0; i < level; i++) - printf(" "); - printf("%08x %08x", ptr->ip, ptr->mask); - if (ptr->name) - printf(" (%s)\n", ptr->name); - else - printf(" (NULL)\n"); - - if (ptr->zero) - radix_print_node(ptr->zero, level + 1); - if (ptr->one) - radix_print_node(ptr->one, level + 1); -} - -void radix_print(void) { - if (!head) { - printf("radix tree is empty\n"); - return; - } - - printf("radix IPv4 tree\n"); - radix_print_node(head, 0); -} - - -static inline int mask2cidr(uint32_t mask) { - uint32_t m = 0x80000000; - int i; - int cnt = 0; - for (i = 0; i < 32; i++, m = m >> 1) { - if (mask & m) - cnt++; - } - - return cnt; -} - -static void radix_build_list_node(RNode *ptr) { - assert(ptr); - - - if (ptr->name) { - printf("%d.%d.%d.%d/%d %s\n", PRINT_IP(ptr->ip), mask2cidr(ptr->mask), ptr->name); - return; - } - else { - if (ptr->zero) - radix_build_list_node(ptr->zero); - if (ptr->one) - radix_build_list_node(ptr->one); - } -} - -void radix_build_list(void) { - if (!head) { - printf("radix tree is empty\n"); - return; - } - - radix_build_list_node(head); -} - -- cgit v1.2.3-54-g00ecf