#include "flowood.h" #include "flowood_fun.h" #include "flwd_net.h" #include "MESA_htable.h" #include "MESA_list_queue.h" #include "MESA_handle_logger.h" #include "MESA_list_count.h" #include #include #include #include #include #include #include #include #include uchar * flwd_nat_htable_key_dup(const uchar *key, uint key_size) { const flwd_tuple5_t *stack_tuple5 = (flwd_tuple5_t *)key; flwd_tuple5_t *heap_tuple5 = (flwd_tuple5_t *)malloc(sizeof(flwd_tuple5_t)); memcpy(heap_tuple5, stack_tuple5, sizeof(flwd_tuple5_t)); if(FLWD_IP_ADDR_TYPE_V6 == stack_tuple5->addr_type){ /* 如果是v6, 需要额外mallo, memcpyIP地址, 注意free!! */ heap_tuple5->ippair_v6 = (flwd_ippair_v6_t *)malloc(sizeof(flwd_ippair_v6_t)); memcpy(&heap_tuple5->ippair_v6->sip_net_order, &stack_tuple5->ippair_v6->sip_net_order, sizeof(struct in6_addr)); memcpy(&heap_tuple5->ippair_v6->dip_net_order, &stack_tuple5->ippair_v6->dip_net_order, sizeof(struct in6_addr)); } return (uchar *)heap_tuple5; } void flwd_nat_htable_key_free(uchar *key, uint key_size) { flwd_tuple5_t *raw_tuple5 = (flwd_tuple5_t *)key; if(FLWD_IP_ADDR_TYPE_V6 == raw_tuple5->addr_type){ free(raw_tuple5->ippair_v6); } free(key); return; } uint flwd_nat_htable_key2index(const MESA_htable_handle table, const uchar * key, uint size) { const flwd_tuple5_t *tuple5 = (flwd_tuple5_t *)key; return flwd_tuple5_hash(tuple5, 0); } int flwd_nat_htable_key_cmp(const uchar * key1, uint size1, const uchar * key2, uint size2) { const flwd_tuple5_t *tp1; const flwd_tuple5_t *tp2; tp1 = (flwd_tuple5_t *)key1; tp2 = (flwd_tuple5_t *)key2; if(tp1->addr_type != tp2->addr_type){ return -1; } if(tp1->protocol != tp2->protocol){ return -1; } if(tp1->sport_net_order != tp2->sport_net_order){ return -1; } if(tp1->dport_net_order != tp2->dport_net_order){ return -1; } if(flwd_likely(FLWD_IP_ADDR_TYPE_V4 == tp1->addr_type)){ if(tp1->ippair_v4.sip_net_order != tp2->ippair_v4.sip_net_order){ return -1; } if(tp1->ippair_v4.dip_net_order != tp2->ippair_v4.dip_net_order){ return -1; } }else{ if(memcmp(&tp1->ippair_v6->sip_net_order, &tp2->ippair_v6->sip_net_order, sizeof(struct in6_addr))){ return -1; } if(memcmp(&tp1->ippair_v6->dip_net_order, &tp2->ippair_v6->dip_net_order, sizeof(struct in6_addr))){ return -1; } } return 0; }