diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index a42ed48..0fe6e27 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,4 +1,4 @@ -add_library(common src/stream_addr.cpp src/stream_table.cpp src/bfd.cpp) +add_library(common src/addr_tuple4.cpp src/session_table.cpp src/bfd.cpp) target_include_directories(common PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include) diff --git a/common/include/addr_tuple4.h b/common/include/addr_tuple4.h new file mode 100644 index 0000000..2a7aae9 --- /dev/null +++ b/common/include/addr_tuple4.h @@ -0,0 +1,68 @@ +#ifndef _ADDR_TUPLE4_H +#define _ADDR_TUPLE4_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include + +enum addr_tuple4_type +{ + ADDR_TUPLE4_TYPE_V4, + ADDR_TUPLE4_TYPE_V6, +}; + +struct addr_tuple4_v4 +{ + struct in_addr src_addr; /* network order */ + struct in_addr dst_addr; /* network order */ + in_port_t src_port; /* network order */ + in_port_t dst_port; /* network order */ +}; + +struct addr_tuple4_v6 +{ + struct in6_addr src_addr; /* network order */ + struct in6_addr dst_addr; /* network order */ + in_port_t src_port; /* network order */ + in_port_t dst_port; /* network order */ +}; + +struct addr_tuple4 +{ + enum addr_tuple4_type addr_type; + union + { + struct addr_tuple4_v4 addr_v4; + struct addr_tuple4_v6 addr_v6; + }; +}; + +#define INIT_ADDR_V4(name, src_addr_str, src_port_num, dst_addr_str, dst_port_num) \ + struct addr_tuple4 name; \ + memset(&name, 0, sizeof(name)); \ + (name).addr_type = ADDR_TUPLE4_TYPE_V4; \ + (name).addr_v4.src_port = htons((src_port_num)); \ + (name).addr_v4.dst_port = htons((dst_port_num)); \ + inet_pton(AF_INET, (src_addr_str), &(name).addr_v4.src_addr); \ + inet_pton(AF_INET, (dst_addr_str), &(name).addr_v4.dst_addr); + +#define INIT_ADDR_V6(name, src_addr_str, src_port_num, dst_addr_str, dst_port_num) \ + struct addr_tuple4 name; \ + memset(&name, 0, sizeof(name)); \ + (name).addr_type = ADDR_TUPLE4_TYPE_V6; \ + (name).addr_v6.src_port = htons((src_port_num)); \ + (name).addr_v6.dst_port = htons((dst_port_num)); \ + inet_pton(AF_INET6, (src_addr_str), &(name).addr_v6.src_addr); \ + inet_pton(AF_INET6, (dst_addr_str), &(name).addr_v6.dst_addr); + +char *addr_tuple4_to_str(const struct addr_tuple4 *addr); +void addr_tuple4_reverse(const struct addr_tuple4 *orin, struct addr_tuple4 *out); + +#ifdef __cpluscplus +} +#endif + +#endif diff --git a/common/include/session_table.h b/common/include/session_table.h new file mode 100644 index 0000000..9b991e6 --- /dev/null +++ b/common/include/session_table.h @@ -0,0 +1,57 @@ +#ifndef _SESSION_TABLE_H +#define _SESSION_TABLE_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include +#include + +#include "uthash.h" +#include "addr_tuple4.h" + +// Note: session_addr must be initialized by memset(0) before use !!! + +typedef void fn_free_cb(void *args); + +struct session_node +{ + uint64_t session_id; /* first key */ + struct addr_tuple4 session_addr; /* second key */ + + void *val_data; + fn_free_cb *val_freecb; + + UT_hash_handle hh1; /* handle for first hash table */ + UT_hash_handle hh2; /* handle for second hash table */ +}; + +struct session_table; + +struct session_table *session_table_create(); +void session_table_destory(struct session_table *table); +uint64_t session_table_count(struct session_table *table); + +// session_addr : deep copy +// val_data : shallow copy (malloc by user, free by val_freecb) +// return 0 : suceess +// return -1 : key exists +int session_table_insert(struct session_table *table, uint64_t session_id, const struct addr_tuple4 *session_addr, void *val_data, const fn_free_cb *val_freecb); + +// return 0 : success +// return -1 : key not exists +int session_table_delete_by_id(struct session_table *table, uint64_t session_id); +int session_table_delete_by_addr(struct session_table *table, const struct addr_tuple4 *session_addr); + +// return NULL : key not exists +// return UnNULL : success +struct session_node *session_table_search_by_id(struct session_table *table, uint64_t session_id); +struct session_node *session_table_search_by_addr(struct session_table *table, const struct addr_tuple4 *session_addr); + +#ifdef __cpluscplus +} +#endif + +#endif diff --git a/common/include/stream_addr.h b/common/include/stream_addr.h deleted file mode 100644 index 2cb13a5..0000000 --- a/common/include/stream_addr.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _STREAM_ADDR_H -#define _STREAM_ADDR_H - -#ifdef __cpluscplus -extern "C" -{ -#endif - -#include - -enum stream_addr_type -{ - STREAM_ADDR_TYPE_V4, - STREAM_ADDR_TYPE_V6, -}; - -struct stream_addr_v4 -{ - struct in_addr src_addr; /* network order */ - struct in_addr dst_addr; /* network order */ - in_port_t src_port; /* network order */ - in_port_t dst_port; /* network order */ -}; - -struct stream_addr_v6 -{ - struct in6_addr src_addr; /* network order */ - struct in6_addr dst_addr; /* network order */ - in_port_t src_port; /* network order */ - in_port_t dst_port; /* network order */ -}; - -struct stream_addr -{ - enum stream_addr_type addr_type; - union - { - struct stream_addr_v4 addr_v4; - struct stream_addr_v6 addr_v6; - }; -}; - -char *stream_addr_to_str(const struct stream_addr *addr); - -#ifdef __cpluscplus -} -#endif - -#endif diff --git a/common/include/stream_table.h b/common/include/stream_table.h deleted file mode 100644 index ee477c4..0000000 --- a/common/include/stream_table.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef _STREAM_TABLE_H -#define _STREAM_TABLE_H - -#ifdef __cpluscplus -extern "C" -{ -#endif - -#include -#include - -#include "uthash.h" -#include "stream_addr.h" - -// Note: key_streamaddr must be initialized by memset(0) before use !!! - -typedef void fn_free_cb(void *args); - -struct stream_node -{ - uint64_t key_streamid; /* first key */ - struct stream_addr key_streamaddr; /* second key */ - - void *val_data; - fn_free_cb *val_freecb; - - UT_hash_handle hh1; /* handle for first hash table */ - UT_hash_handle hh2; /* handle for second hash table */ -}; - -struct stream_table; - -struct stream_table *stream_table_create(); -void stream_table_destory(struct stream_table *table); - -// key_streamaddr : deep copy -// val_data : shallow copy (malloc by user, free by val_freecb) -int stream_table_insert(struct stream_table *table, uint64_t key_streamid, const struct stream_addr *key_streamaddr, void *val_data, const fn_free_cb *val_freecb); - -int stream_table_delete_by_streamid(struct stream_table *table, uint64_t key_streamid); -int stream_table_delete_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr); - -struct stream_node *stream_table_search_by_streamid(struct stream_table *table, uint64_t key_streamid); -struct stream_node *stream_table_search_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr); - -#ifdef __cpluscplus -} -#endif - -#endif diff --git a/common/src/stream_addr.cpp b/common/src/addr_tuple4.cpp similarity index 51% rename from common/src/stream_addr.cpp rename to common/src/addr_tuple4.cpp index e8872d8..b9a67ad 100644 --- a/common/src/stream_addr.cpp +++ b/common/src/addr_tuple4.cpp @@ -1,13 +1,14 @@ #include +#include #include -#include "stream_addr.h" +#include "addr_tuple4.h" -char *stream_addr_to_str(const struct stream_addr *addr) +char *addr_tuple4_to_str(const struct addr_tuple4 *addr) { char *str_ret = NULL; - if (addr->addr_type == STREAM_ADDR_TYPE_V4) + if (addr->addr_type == ADDR_TUPLE4_TYPE_V4) { char src_addr[INET_ADDRSTRLEN] = {0}; char dst_addr[INET_ADDRSTRLEN] = {0}; @@ -18,7 +19,7 @@ char *stream_addr_to_str(const struct stream_addr *addr) asprintf(&str_ret, "%s %u %s %u", src_addr, src_port, dst_addr, dst_port); } - if (addr->addr_type == STREAM_ADDR_TYPE_V6) + if (addr->addr_type == ADDR_TUPLE4_TYPE_V6) { char src_addr[INET6_ADDRSTRLEN] = {0}; char dst_addr[INET6_ADDRSTRLEN] = {0}; @@ -30,4 +31,27 @@ char *stream_addr_to_str(const struct stream_addr *addr) } return str_ret; +} + +void addr_tuple4_reverse(const struct addr_tuple4 *orin, struct addr_tuple4 *out) +{ + memset(out, 0, sizeof(struct addr_tuple4)); + + if (orin->addr_type == ADDR_TUPLE4_TYPE_V4) + { + out->addr_type = ADDR_TUPLE4_TYPE_V4; + out->addr_v4.src_addr = orin->addr_v4.dst_addr; + out->addr_v4.dst_addr = orin->addr_v4.src_addr; + out->addr_v4.src_port = orin->addr_v4.dst_port; + out->addr_v4.dst_port = orin->addr_v4.src_port; + } + + if (orin->addr_type == ADDR_TUPLE4_TYPE_V6) + { + out->addr_type = ADDR_TUPLE4_TYPE_V6; + out->addr_v6.src_addr = orin->addr_v6.dst_addr; + out->addr_v6.dst_addr = orin->addr_v6.src_addr; + out->addr_v6.src_port = orin->addr_v6.dst_port; + out->addr_v6.dst_port = orin->addr_v6.src_port; + } } \ No newline at end of file diff --git a/common/src/session_table.cpp b/common/src/session_table.cpp new file mode 100644 index 0000000..fb141e9 --- /dev/null +++ b/common/src/session_table.cpp @@ -0,0 +1,195 @@ +#include + +#include "session_table.h" +#include "log.h" + +struct session_table +{ + struct session_node *root_by_id; + struct session_node *root_by_addr; + uint64_t session_node_count; +}; + +// Note: session_addr must be initialized by memset(0) before use !!! + +struct session_table *session_table_create() +{ + struct session_table *table = (struct session_table *)calloc(1, sizeof(struct session_table)); + assert(table); + table->session_node_count = 0; + + return table; +} + +void session_table_destory(struct session_table *table) +{ + if (table) + { + struct session_node *temp = NULL; + struct session_node *node = NULL; + HASH_ITER(hh1, table->root_by_id, node, temp) + { + HASH_DELETE(hh1, table->root_by_id, node); + HASH_DELETE(hh2, table->root_by_addr, node); + + if (node->val_freecb && node->val_data) + { + node->val_freecb(node->val_data); + } + + free(node); + node = NULL; + } + + free(table); + table = NULL; + } +} + +uint64_t session_table_count(struct session_table *table) +{ + if (table) + { + return table->session_node_count; + } + else + { + return 0; + } +} + +// session_addr : deep copy +// val_data : shallow copy (malloc by user, free by val_freecb) +int session_table_insert(struct session_table *table, uint64_t session_id, const struct addr_tuple4 *session_addr, void *val_data, const fn_free_cb *val_freecb) +{ + struct session_node *temp = NULL; + HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp); + if (temp) + { + LOG_DEBUG("session table insert: key %lu exists", session_id); + return -1; + } + + temp = (struct session_node *)calloc(1, sizeof(struct session_node)); + assert(temp); + + temp->session_id = session_id; + memcpy(&temp->session_addr, session_addr, sizeof(struct addr_tuple4)); + temp->val_data = val_data; + temp->val_freecb = val_freecb; + + HASH_ADD(hh1, table->root_by_id, session_id, sizeof(temp->session_id), temp); + HASH_ADD(hh2, table->root_by_addr, session_addr, sizeof(temp->session_addr), temp); + + LOG_DEBUG("session table insert: key %lu success", session_id); + table->session_node_count++; + + return 0; +} + +int session_table_delete_by_id(struct session_table *table, uint64_t session_id) +{ + struct session_node *temp = NULL; + HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp); + if (!temp) + { + LOG_DEBUG("session table delete: key %lu not exists", session_id); + return -1; + } + + HASH_DELETE(hh1, table->root_by_id, temp); + HASH_DELETE(hh2, table->root_by_addr, temp); + + if (temp->val_freecb && temp->val_data) + { + temp->val_freecb(temp->val_data); + temp->val_data = NULL; + } + + free(temp); + temp = NULL; + + LOG_DEBUG("session table delete: key %lu success", session_id); + table->session_node_count--; + + return 0; +} + +int session_table_delete_by_addr(struct session_table *table, const struct addr_tuple4 *session_addr) +{ + struct session_node *temp = NULL; + char *addr_str = addr_tuple4_to_str(session_addr); + HASH_FIND(hh2, table->root_by_addr, session_addr, sizeof(struct addr_tuple4), temp); + if (!temp) + { + struct addr_tuple4 reverse_addr; + addr_tuple4_reverse(session_addr, &reverse_addr); + HASH_FIND(hh2, table->root_by_addr, &reverse_addr, sizeof(struct addr_tuple4), temp); + if (!temp) + { + LOG_DEBUG("session table delete: key %s not exists", addr_str); + free(addr_str); + return -1; + } + } + + HASH_DELETE(hh1, table->root_by_id, temp); + HASH_DELETE(hh2, table->root_by_addr, temp); + + if (temp->val_freecb && temp->val_data) + { + temp->val_freecb(temp->val_data); + temp->val_data = NULL; + } + + free(temp); + temp = NULL; + + LOG_DEBUG("session table delete: key %s success", addr_str); + free(addr_str); + addr_str = NULL; + table->session_node_count--; + + return 0; +} + +struct session_node *session_table_search_by_id(struct session_table *table, uint64_t session_id) +{ + struct session_node *temp = NULL; + HASH_FIND(hh1, table->root_by_id, &session_id, sizeof(session_id), temp); + if (!temp) + { + LOG_DEBUG("session table search: key %lu not exists", session_id); + return NULL; + } + + LOG_DEBUG("session table search: key %lu success", session_id); + + return temp; +} + +struct session_node *session_table_search_by_addr(struct session_table *table, const struct addr_tuple4 *session_addr) +{ + struct session_node *temp = NULL; + char *addr_str = addr_tuple4_to_str(session_addr); + HASH_FIND(hh2, table->root_by_addr, session_addr, sizeof(struct addr_tuple4), temp); + if (!temp) + { + struct addr_tuple4 reverse_addr; + addr_tuple4_reverse(session_addr, &reverse_addr); + HASH_FIND(hh2, table->root_by_addr, &reverse_addr, sizeof(struct addr_tuple4), temp); + if (!temp) + { + LOG_DEBUG("session table search: key %s not exists", addr_str); + free(addr_str); + addr_str = NULL; + return NULL; + } + } + + LOG_DEBUG("session table search: key %s success", addr_str); + free(addr_str); + addr_str = NULL; + + return temp; +} diff --git a/common/src/stream_table.cpp b/common/src/stream_table.cpp deleted file mode 100644 index 1714dd9..0000000 --- a/common/src/stream_table.cpp +++ /dev/null @@ -1,161 +0,0 @@ -#include - -#include "stream_table.h" -#include "log.h" - -struct stream_table -{ - struct stream_node *streamid_root; - struct stream_node *streamaddr_root; -}; - -// Note: key_streamaddr must be initialized by memset(0) before use !!! - -struct stream_table *stream_table_create() -{ - struct stream_table *table = (struct stream_table *)calloc(1, sizeof(struct stream_table)); - assert(table); - - return table; -} - -void stream_table_destory(struct stream_table *table) -{ - if (table) - { - struct stream_node *temp = NULL; - struct stream_node *node = NULL; - HASH_ITER(hh1, table->streamid_root, node, temp) - { - HASH_DELETE(hh1, table->streamid_root, node); - HASH_DELETE(hh2, table->streamaddr_root, node); - - if (node->val_freecb && node->val_data) - { - node->val_freecb(node->val_data); - } - - free(node); - node = NULL; - } - - free(table); - table = NULL; - } -} - -// key_streamaddr : deep copy -// val_data : shallow copy (malloc by user, free by val_freecb) -int stream_table_insert(struct stream_table *table, uint64_t key_streamid, const struct stream_addr *key_streamaddr, void *val_data, const fn_free_cb *val_freecb) -{ - struct stream_node *temp = NULL; - HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp); - if (temp) - { - LOG_DEBUG("stream table insert: key %lu exists", key_streamid); - return -1; - } - - temp = (struct stream_node *)calloc(1, sizeof(struct stream_node)); - assert(temp); - - temp->key_streamid = key_streamid; - memcpy(&temp->key_streamaddr, key_streamaddr, sizeof(struct stream_addr)); - temp->val_data = val_data; - temp->val_freecb = val_freecb; - - HASH_ADD(hh1, table->streamid_root, key_streamid, sizeof(temp->key_streamid), temp); - HASH_ADD(hh2, table->streamaddr_root, key_streamaddr, sizeof(temp->key_streamaddr), temp); - - LOG_DEBUG("stream table insert: key %lu success", key_streamid); - - return 0; -} - -int stream_table_delete_by_streamid(struct stream_table *table, uint64_t key_streamid) -{ - struct stream_node *temp = NULL; - HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp); - if (!temp) - { - LOG_DEBUG("stream table delete: key %lu not exists", key_streamid); - return -1; - } - - HASH_DELETE(hh1, table->streamid_root, temp); - HASH_DELETE(hh2, table->streamaddr_root, temp); - - if (temp->val_freecb && temp->val_data) - { - temp->val_freecb(temp->val_data); - } - - free(temp); - temp = NULL; - - LOG_DEBUG("stream table delete: key %lu success", key_streamid); - return 0; -} - -int stream_table_delete_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr) -{ - struct stream_node *temp = NULL; - char *addr_str = stream_addr_to_str(key_streamaddr); - HASH_FIND(hh2, table->streamaddr_root, key_streamaddr, sizeof(*key_streamaddr), temp); - if (!temp) - { - LOG_DEBUG("stream table delete: key %s not exists", addr_str); - free(addr_str); - return -1; - } - - HASH_DELETE(hh1, table->streamid_root, temp); - HASH_DELETE(hh2, table->streamaddr_root, temp); - - if (temp->val_freecb && temp->val_data) - { - temp->val_freecb(temp->val_data); - } - - free(temp); - temp = NULL; - - LOG_DEBUG("stream table delete: key %s success", addr_str); - free(addr_str); - addr_str = NULL; - - return 0; -} - -struct stream_node *stream_table_search_by_streamid(struct stream_table *table, uint64_t key_streamid) -{ - struct stream_node *temp = NULL; - HASH_FIND(hh1, table->streamid_root, &key_streamid, sizeof(key_streamid), temp); - if (!temp) - { - LOG_DEBUG("stream table search: key %lu not exists", key_streamid); - return NULL; - } - - LOG_DEBUG("stream table search: key %lu success", key_streamid); - return temp; -} - -struct stream_node *stream_table_search_by_streamaddr(struct stream_table *table, const struct stream_addr *key_streamaddr) -{ - struct stream_node *temp = NULL; - char *addr_str = stream_addr_to_str(key_streamaddr); - HASH_FIND(hh2, table->streamaddr_root, key_streamaddr, sizeof(*key_streamaddr), temp); - if (!temp) - { - LOG_DEBUG("stream table search: key %s not exists", addr_str); - free(addr_str); - addr_str = NULL; - return NULL; - } - - LOG_DEBUG("stream table search: key %s success", addr_str); - free(addr_str); - addr_str = NULL; - return temp; -} diff --git a/common/test/CMakeLists.txt b/common/test/CMakeLists.txt index 4458795..bbbefd9 100644 --- a/common/test/CMakeLists.txt +++ b/common/test/CMakeLists.txt @@ -1,23 +1,23 @@ ############################################################################### -# gtest_stream_addr +# gtest_addr_tuple4 ############################################################################### -add_executable(gtest_stream_addr gtest_stream_addr.cpp) -target_include_directories(gtest_stream_addr PUBLIC ${CMAKE_SOURCE_DIR}/common/include) -target_link_libraries(gtest_stream_addr common gtest) +add_executable(gtest_addr_tuple4 gtest_addr_tuple4.cpp) +target_include_directories(gtest_addr_tuple4 PUBLIC ${CMAKE_SOURCE_DIR}/common/include) +target_link_libraries(gtest_addr_tuple4 common gtest) ############################################################################### -# gtest_stream_table +# gtest_session_table ############################################################################### -add_executable(gtest_stream_table gtest_stream_table.cpp) -target_include_directories(gtest_stream_table PUBLIC ${CMAKE_SOURCE_DIR}/common/include) -target_link_libraries(gtest_stream_table common gtest) +add_executable(gtest_session_table gtest_session_table.cpp) +target_include_directories(gtest_session_table PUBLIC ${CMAKE_SOURCE_DIR}/common/include) +target_link_libraries(gtest_session_table common gtest) ############################################################################### # gtest_discover_tests ############################################################################### include(GoogleTest) -gtest_discover_tests(gtest_stream_addr) -gtest_discover_tests(gtest_stream_table) \ No newline at end of file +gtest_discover_tests(gtest_addr_tuple4) +gtest_discover_tests(gtest_session_table) \ No newline at end of file diff --git a/common/test/gtest_addr_tuple4.cpp b/common/test/gtest_addr_tuple4.cpp new file mode 100644 index 0000000..ad9b246 --- /dev/null +++ b/common/test/gtest_addr_tuple4.cpp @@ -0,0 +1,48 @@ +#include +#include + +#include "addr_tuple4.h" + +TEST(ADDR_TUPLE4, IPV4) +{ + char *ret_str = NULL; + struct addr_tuple4 reve_addr; + + INIT_ADDR_V4(orin_addr, "1.2.3.4", 12345, "4.3.2.1", 23456) + addr_tuple4_reverse(&orin_addr, &reve_addr); + + ret_str = addr_tuple4_to_str(&orin_addr); + EXPECT_TRUE(ret_str != nullptr); + EXPECT_STREQ(ret_str, "1.2.3.4 12345 4.3.2.1 23456"); + free(ret_str); + + ret_str = addr_tuple4_to_str(&reve_addr); + EXPECT_TRUE(ret_str != nullptr); + EXPECT_STREQ(ret_str, "4.3.2.1 23456 1.2.3.4 12345"); + free(ret_str); +} + +TEST(ADDR_TUPLE4, IPV6) +{ + char *ret_str = NULL; + struct addr_tuple4 reve_addr; + + INIT_ADDR_V6(orin_addr, "1:2::3", 12345, "a:b::c", 23456); + addr_tuple4_reverse(&orin_addr, &reve_addr); + + ret_str = addr_tuple4_to_str(&orin_addr); + EXPECT_TRUE(ret_str != nullptr); + EXPECT_STREQ(ret_str, "1:2::3 12345 a:b::c 23456"); + free(ret_str); + + ret_str = addr_tuple4_to_str(&reve_addr); + EXPECT_TRUE(ret_str != nullptr); + EXPECT_STREQ(ret_str, "a:b::c 23456 1:2::3 12345"); + free(ret_str); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/common/test/gtest_session_table.cpp b/common/test/gtest_session_table.cpp new file mode 100644 index 0000000..602d441 --- /dev/null +++ b/common/test/gtest_session_table.cpp @@ -0,0 +1,242 @@ +#include +#include + +#include "session_table.h" + +TEST(STREAM_TABLE, INSERT) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == -1); + EXPECT_TRUE(session_table_count(table) == 1); + + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == -1); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Destory + session_table_destory(table); +} + +TEST(STREAM_TABLE, SEARCH_BY_ID) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + INIT_ADDR_V4(addr3, "1.1.1.1", 1111, "2.2.2.2", 2222); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Search By Session ID + struct session_node *node = NULL; + node = session_table_search_by_id(table, 1); + EXPECT_TRUE(node != nullptr); + EXPECT_STREQ((const char *)node->val_data, "HELLO"); + node = session_table_search_by_id(table, 2); + EXPECT_TRUE(node != nullptr); + EXPECT_STREQ((const char *)node->val_data, "WORLD"); + node = session_table_search_by_id(table, 3); + EXPECT_TRUE(node == nullptr); + + // TEST Destory + session_table_destory(table); +} + +TEST(STREAM_TABLE, SEARCH_BY_ADDR) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + INIT_ADDR_V4(addr3, "1.1.1.1", 1111, "2.2.2.2", 2222); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Search By Session Addr + struct session_node *node = NULL; + node = session_table_search_by_addr(table, &addr1); + EXPECT_TRUE(node != nullptr); + EXPECT_STREQ((const char *)node->val_data, "HELLO"); + node = session_table_search_by_addr(table, &addr2); + EXPECT_TRUE(node != nullptr); + EXPECT_STREQ((const char *)node->val_data, "WORLD"); + node = session_table_search_by_addr(table, &addr3); + EXPECT_TRUE(node == nullptr); + + // TEST Destory + session_table_destory(table); +} + +TEST(STREAM_TABLE, SEARCH_BY_REVERSE_ADDR) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + struct addr_tuple4 addr1_reverse; + struct addr_tuple4 addr2_reverse; + addr_tuple4_reverse(&addr1, &addr1_reverse); + addr_tuple4_reverse(&addr2, &addr2_reverse); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Search By Session Reverse Addr + struct session_node *node = NULL; + node = session_table_search_by_addr(table, &addr1_reverse); + EXPECT_TRUE(node != nullptr); + EXPECT_STREQ((const char *)node->val_data, "HELLO"); + node = session_table_search_by_addr(table, &addr2_reverse); + EXPECT_TRUE(node != nullptr); + EXPECT_STREQ((const char *)node->val_data, "WORLD"); + + // TEST Destory + session_table_destory(table); +} + +TEST(STREAM_TABLE, DELETE_BY_ID) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + struct addr_tuple4 addr1_reverse; + struct addr_tuple4 addr2_reverse; + addr_tuple4_reverse(&addr1, &addr1_reverse); + addr_tuple4_reverse(&addr2, &addr2_reverse); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Delete By Session ID + EXPECT_TRUE(session_table_delete_by_id(table, 1) == 0); + EXPECT_TRUE(session_table_search_by_id(table, 1) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr1) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr1_reverse) == NULL); + EXPECT_TRUE(session_table_count(table) == 1); + + EXPECT_TRUE(session_table_delete_by_id(table, 2) == 0); + EXPECT_TRUE(session_table_search_by_id(table, 2) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr2) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr2_reverse) == NULL); + EXPECT_TRUE(session_table_count(table) == 0); + + // TEST Destory + session_table_destory(table); +} + +TEST(STREAM_TABLE, DELETE_BY_ADDR) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + struct addr_tuple4 addr1_reverse; + struct addr_tuple4 addr2_reverse; + addr_tuple4_reverse(&addr1, &addr1_reverse); + addr_tuple4_reverse(&addr2, &addr2_reverse); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Delete By Session Addr + EXPECT_TRUE(session_table_delete_by_addr(table, &addr1) == 0); + EXPECT_TRUE(session_table_search_by_id(table, 1) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr1) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr1_reverse) == NULL); + EXPECT_TRUE(session_table_count(table) == 1); + + EXPECT_TRUE(session_table_delete_by_addr(table, &addr2) == 0); + EXPECT_TRUE(session_table_search_by_id(table, 2) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr2) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr2_reverse) == NULL); + EXPECT_TRUE(session_table_count(table) == 0); + + // TEST Destory + session_table_destory(table); +} + +TEST(STREAM_TABLE, DELETE_BY_REVERSE_ADDR) +{ + // TEST Create + struct session_table *table = session_table_create(); + EXPECT_TRUE(table != nullptr); + + char *val_hello = strdup("HELLO"); + char *val_world = strdup("WORLD"); + INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); + INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); + struct addr_tuple4 addr1_reverse; + struct addr_tuple4 addr2_reverse; + addr_tuple4_reverse(&addr1, &addr1_reverse); + addr_tuple4_reverse(&addr2, &addr2_reverse); + + // TEST Insert + EXPECT_TRUE(session_table_insert(table, 1, &addr1, val_hello, free) == 0); + EXPECT_TRUE(session_table_insert(table, 2, &addr2, val_world, free) == 0); + EXPECT_TRUE(session_table_count(table) == 2); + + // TEST Delete By Session Reverse Addr + EXPECT_TRUE(session_table_delete_by_addr(table, &addr1_reverse) == 0); + EXPECT_TRUE(session_table_search_by_id(table, 1) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr1) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr1_reverse) == NULL); + EXPECT_TRUE(session_table_count(table) == 1); + + EXPECT_TRUE(session_table_delete_by_addr(table, &addr2_reverse) == 0); + EXPECT_TRUE(session_table_search_by_id(table, 2) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr2) == NULL); + EXPECT_TRUE(session_table_search_by_addr(table, &addr2_reverse) == NULL); + EXPECT_TRUE(session_table_count(table) == 0); + + // TEST Destory + session_table_destory(table); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/common/test/gtest_stream_addr.cpp b/common/test/gtest_stream_addr.cpp deleted file mode 100644 index c98b38e..0000000 --- a/common/test/gtest_stream_addr.cpp +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include - -#include "stream_addr.h" - -TEST(STREAM_ADDR, IPV4) -{ - struct stream_addr addr; - addr.addr_type = STREAM_ADDR_TYPE_V4; - addr.addr_v4.src_port = htons(12345); - addr.addr_v4.dst_port = htons(23456); - inet_pton(AF_INET, "1.2.3.4", &addr.addr_v4.src_addr); - inet_pton(AF_INET, "4.3.2.1", &addr.addr_v4.dst_addr); - - char *ret_str = stream_addr_to_str(&addr); - EXPECT_TRUE(ret_str != nullptr); - EXPECT_STREQ(ret_str, "1.2.3.4 12345 4.3.2.1 23456"); - free(ret_str); -} - -TEST(STREAM_ADDR, IPV6) -{ - struct stream_addr addr; - addr.addr_type = STREAM_ADDR_TYPE_V6; - addr.addr_v6.src_port = htons(12345); - addr.addr_v6.dst_port = htons(23456); - inet_pton(AF_INET6, "1:2::3", &addr.addr_v6.src_addr); - inet_pton(AF_INET6, "a:b::c", &addr.addr_v6.dst_addr); - - char *ret_str = stream_addr_to_str(&addr); - EXPECT_TRUE(ret_str != nullptr); - EXPECT_STREQ(ret_str, "1:2::3 12345 a:b::c 23456"); - free(ret_str); -} - -int main(int argc, char **argv) -{ - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} \ No newline at end of file diff --git a/common/test/gtest_stream_table.cpp b/common/test/gtest_stream_table.cpp deleted file mode 100644 index 1324596..0000000 --- a/common/test/gtest_stream_table.cpp +++ /dev/null @@ -1,84 +0,0 @@ -#include -#include - -#include "stream_table.h" - -#define INIT_ADDR_V4(name, src_addr_str, src_port_num, dst_addr_str, dst_port_num) \ - struct stream_addr name; \ - memset(&name, 0, sizeof(name)); \ - (name).addr_type = STREAM_ADDR_TYPE_V4; \ - (name).addr_v4.src_port = htons((src_port_num)); \ - (name).addr_v4.dst_port = htons((dst_port_num)); \ - inet_pton(AF_INET, (src_addr_str), &(name).addr_v4.src_addr); \ - inet_pton(AF_INET, (dst_addr_str), &(name).addr_v4.dst_addr); - -#define INIT_ADDR_V6(name, src_addr_str, src_port_num, dst_addr_str, dst_port_num) \ - struct stream_addr name; \ - memset(&name, 0, sizeof(name)); \ - (name).addr_type = STREAM_ADDR_TYPE_V6; \ - (name).addr_v6.src_port = htons((src_port_num)); \ - (name).addr_v6.dst_port = htons((dst_port_num)); \ - inet_pton(AF_INET6, (src_addr_str), &(name).addr_v6.src_addr); \ - inet_pton(AF_INET6, (dst_addr_str), &(name).addr_v6.dst_addr); - -TEST(STREAM_TABLE, TEST) -{ - INIT_ADDR_V4(addr1, "1.2.3.4", 1234, "4.3.2.1", 4321); - INIT_ADDR_V6(addr2, "2:3:4::5", 2345, "5:4:3::2", 5342); - struct stream_addr addr3; - char *val_hello = strdup("HELLO"); - char *val_world = strdup("WORLD"); - - struct stream_node *node = NULL; - // TEST Create - struct stream_table *table = stream_table_create(); - EXPECT_TRUE(table != nullptr); - - // TEST Insert - EXPECT_TRUE(stream_table_insert(table, 1, &addr1, val_hello, free) == 0); - EXPECT_TRUE(stream_table_insert(table, 1, &addr1, val_hello, free) == -1); - - EXPECT_TRUE(stream_table_insert(table, 2, &addr2, val_world, free) == 0); - EXPECT_TRUE(stream_table_insert(table, 2, &addr2, val_world, free) == -1); - - // TEST Search - node = stream_table_search_by_streamid(table, 1); - EXPECT_TRUE(node != nullptr); - EXPECT_STREQ((const char *)node->val_data, "HELLO"); - node = stream_table_search_by_streamid(table, 2); - EXPECT_TRUE(node != nullptr); - EXPECT_STREQ((const char *)node->val_data, "WORLD"); - node = stream_table_search_by_streamid(table, 3); - EXPECT_TRUE(node == nullptr); - - node = stream_table_search_by_streamaddr(table, &addr1); - EXPECT_TRUE(node != nullptr); - EXPECT_STREQ((const char *)node->val_data, "HELLO"); - node = stream_table_search_by_streamaddr(table, &addr2); - EXPECT_TRUE(node != nullptr); - EXPECT_STREQ((const char *)node->val_data, "WORLD"); - node = stream_table_search_by_streamaddr(table, &addr3); - EXPECT_TRUE(node == nullptr); - - // TEST Delete - stream_table_delete_by_streamid(table, 1); - node = stream_table_search_by_streamid(table, 1); - EXPECT_TRUE(node == nullptr); - node = stream_table_search_by_streamaddr(table, &addr1); - EXPECT_TRUE(node == nullptr); - - stream_table_delete_by_streamaddr(table, &addr2); - node = stream_table_search_by_streamid(table, 2); - EXPECT_TRUE(node == nullptr); - node = stream_table_search_by_streamaddr(table, &addr1); - EXPECT_TRUE(node == nullptr); - - // TEST Destory - stream_table_destory(table); -} - -int main(int argc, char **argv) -{ - ::testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -} \ No newline at end of file