我在C中编写前缀树来存储IP前缀。 密钥是IP前缀。 我想将它用于32位或128位密钥(IPv4 / IPv6地址)。
insert / remove / lookup函数需要为ipv4或ipv6变体调用不同的bitop函数。
我怎么能在C中做到这一点?
最后,我想拥有以下结构和功能:
typedef struct tree_node6_t {
ipv6_addr prefix;
u_int8_t len;
struct tree_node6_t* parent;
struct tree_node6_t* lchild;
struct tree_node6_t* rchild;
void* data;
} tree_node6;
typedef struct tree_node4_t {
ipv4_addr prefix;
u_int8_t len;
struct tree_node4_t* parent;
struct tree_node4_t* lchild;
struct tree_node4_t* rchild;
void* data;
} tree_node;
void tree_insert4(tree_node* root, tree_node* new_node, const unsigned int level);
void tree_insert6(tree_node* root, tree_node* new_node, const unsigned int level);
tree_node* tree_lookup4(const tree_node* root_node, const ipv4_addr* prefix, const u_int8_t prefix_len, unsigned int* level);
tree_node* tree_lookup6(const tree_node* root_node, const ipv6_addr* prefix, const u_int8_t prefix_len, unsigned int* level);
感谢任何提示:=)
答案 0 :(得分:1)
您可以使用每个IPv4地址都可以映射到IPv6地址的事实。
答案 1 :(得分:1)
您可以使用另一个typedef来声明ip类型 然后,您可以在编译时使用预处理程序指令更改它:
#ifdef USE_NODE4
typedef ipv4_addr ADDRESSTYPE ;
#define TREEINSERT tree_insert4
#define TREELOOKUP tree_lookup4
#else
typedef ipv6_addr ADDRESSTYPE ;
#define TREEINSERT tree_insert6
#define TREELOOKUP tree_lookup6
#endif
typedef struct tree_node_general {
ADDRESSTYPE prefix;
u_int8_t len;
struct tree_node_general* parent;
struct tree_node_general* lchild;
struct tree_node_general* rchild;
void* data;
} tree_node;
void TREEINSERT (tree_node* root, tree_node* new_node, const unsigned int level);
tree_node* TREELOOKUP (const tree_node* root_node, const ADDRESSTYPE* prefix, const u_int8_t prefix_len, unsigned int* level);