From 48f5aa33ca8024905e5b9e7799cd42ba5cda3d39 Mon Sep 17 00:00:00 2001 From: Thomas Gschwantner Date: Sat, 13 Apr 2019 22:16:09 +0200 Subject: radix-trie: allow both ipv4 and ipv6 debug output --- radix-trie.c | 37 +++++++++++++++++++++++++++---------- radix-trie.h | 5 +++-- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/radix-trie.c b/radix-trie.c index 6e086cf..f314913 100644 --- a/radix-trie.c +++ b/radix-trie.c @@ -245,25 +245,32 @@ static struct radix_node *lookup(struct radix_node *root, uint8_t bits, #ifdef DEBUG #include -void node_to_str(struct radix_node *node, char *buf) +void node_to_str(struct radix_node *node, char *buf, uint8_t bits) { - struct in6_addr addr; char out[INET6_ADDRSTRLEN]; char cidr[5]; + struct in_addr v4addr; + struct in6_addr v6addr; if (!node) { strcpy(buf, "-"); return; } - swap_endian(addr.s6_addr, node->bits, 128); - inet_ntop(AF_INET6, &addr, out, sizeof out); + if (bits == 32) { + swap_endian((uint8_t *)&v4addr.s_addr, node->bits, bits); + inet_ntop(AF_INET, &v4addr, out, sizeof out); + } else { + swap_endian(v6addr.s6_addr, node->bits, bits); + inet_ntop(AF_INET6, &v6addr, out, sizeof out); + } + snprintf(cidr, sizeof cidr, "/%u", node->cidr); strcpy(buf, out); strcat(buf, cidr); } -void debug_print_trie(struct radix_node *root) +static void debug_print_trie(struct radix_node *root, uint8_t bits) { char parent[INET6_ADDRSTRLEN + 4], child1[INET6_ADDRSTRLEN + 4], child2[INET6_ADDRSTRLEN + 4]; @@ -271,14 +278,24 @@ void debug_print_trie(struct radix_node *root) if (!root) return; - node_to_str(root, parent); - node_to_str(root->bit[0], child1); - node_to_str(root->bit[1], child2); + node_to_str(root, parent, bits); + node_to_str(root->bit[0], child1, bits); + node_to_str(root->bit[1], child2, bits); debug("%s -> %s, %s\n", parent, child1, child2); - debug_print_trie(root->bit[0]); - debug_print_trie(root->bit[1]); + debug_print_trie(root->bit[0], bits); + debug_print_trie(root->bit[1], bits); +} + +void debug_print_trie_v4(struct radix_trie *trie) +{ + debug_print_trie(trie->ip4_root, 32); +} + +void debug_print_trie_v6(struct radix_trie *trie) +{ + debug_print_trie(trie->ip6_root, 128); } #endif diff --git a/radix-trie.h b/radix-trie.h index 9e210af..eafda95 100644 --- a/radix-trie.h +++ b/radix-trie.h @@ -28,8 +28,9 @@ int radix_tryinsert_v6(struct radix_trie *root, const struct in6_addr *ip, uint8_t cidr, void *data); #ifdef DEBUG -void node_to_str(struct radix_node *node, char *buf); -void debug_print_trie(struct radix_node *root); +void node_to_str(struct radix_node *node, char *buf, uint8_t bits); +void debug_print_trie_v4(struct radix_trie *trie); +void debug_print_trie_v6(struct radix_trie *trie); #endif #endif -- cgit v1.2.3-59-g8ed1b