diff options
| author | luwenpeng <[email protected]> | 2023-12-15 16:34:53 +0800 |
|---|---|---|
| committer | luwenpeng <[email protected]> | 2023-12-15 16:35:11 +0800 |
| commit | 7653d646d31c546ea2730bc93ddd79ad6fef4c81 (patch) | |
| tree | 2cba8bda6b1cf2d0d10c00a74eceedf6358d0b47 | |
| parent | 90a6936fd627e2f63a26e7161012723a5063392d (diff) | |
Add tuple2 & tuple4 & tuple5 & tuple6
| -rw-r--r-- | src/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/session/CMakeLists.txt | 7 | ||||
| -rw-r--r-- | src/session/gtest_session.cpp | 73 | ||||
| -rw-r--r-- | src/session/gtest_session_table.cpp | 96 | ||||
| -rw-r--r-- | src/session/session.cpp | 141 | ||||
| -rw-r--r-- | src/session/session.h | 43 | ||||
| -rw-r--r-- | src/session/session_private.h | 4 | ||||
| -rw-r--r-- | src/session/session_table.cpp | 32 | ||||
| -rw-r--r-- | src/session/session_table.h | 6 | ||||
| -rw-r--r-- | src/tuple/CMakeLists.txt | 19 | ||||
| -rw-r--r-- | src/tuple/gtest_tuple.cpp | 263 | ||||
| -rw-r--r-- | src/tuple/tuple.cpp | 452 | ||||
| -rw-r--r-- | src/tuple/tuple.h | 89 |
13 files changed, 914 insertions, 312 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b1a388f..78e1654 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(timestamp) +add_subdirectory(tuple) add_subdirectory(session) add_subdirectory(stellar)
\ No newline at end of file diff --git a/src/session/CMakeLists.txt b/src/session/CMakeLists.txt index fda4256..2b9c426 100644 --- a/src/session/CMakeLists.txt +++ b/src/session/CMakeLists.txt @@ -7,14 +7,15 @@ add_library(session_manager session_pool.cpp session_table.cpp session_timer.cpp - session_manager.cpp) + #session_manager.cpp + ) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/deps/timeout) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/packet) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/session) target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/timestamp) -target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/crc32) -target_link_libraries(session_manager timeout) +target_include_directories(session_manager PUBLIC ${CMAKE_SOURCE_DIR}/src/tuple) +target_link_libraries(session_manager timeout timestamp tuple) ############################################################################### # gtest diff --git a/src/session/gtest_session.cpp b/src/session/gtest_session.cpp index f413c58..1b870d1 100644 --- a/src/session/gtest_session.cpp +++ b/src/session/gtest_session.cpp @@ -3,8 +3,8 @@ #include "session_private.h" #define SESSION_KEY_IPV4_TCP(name) \ - struct session_key name; \ - memset(&name, 0, sizeof(struct session_key)); \ + struct tuple6 name; \ + memset(&name, 0, sizeof(struct tuple6)); \ (name).ip_type = IP_TYPE_V4; \ (name).src_addr.v4.s_addr = inet_addr("192.168.1.2"); \ (name).dst_addr.v4.s_addr = inet_addr("192.168.1.3"); \ @@ -14,8 +14,8 @@ (name).security_zone = 0; #define SESSION_KEY_IPV6_UDP(name) \ - struct session_key name; \ - memset(&name, 0, sizeof(struct session_key)); \ + struct tuple6 name; \ + memset(&name, 0, sizeof(struct tuple6)); \ (name).ip_type = IP_TYPE_V6; \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:8329", &(name).src_addr.v6); \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:832a", &(name).dst_addr.v6); \ @@ -25,8 +25,8 @@ (name).security_zone = 0; #define SESSION_KEY_IPV6_TCP(name) \ - struct session_key name; \ - memset(&name, 0, sizeof(struct session_key)); \ + struct tuple6 name; \ + memset(&name, 0, sizeof(struct tuple6)); \ (name).ip_type = IP_TYPE_V6; \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:8329", &(name).src_addr.v6); \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:832a", &(name).dst_addr.v6); \ @@ -111,67 +111,6 @@ TEST(SESSION, EV_QUEUE) } } -TEST(SESSION, SESSION_KEY) -{ - char buf[128] = {0}; - SESSION_KEY_IPV4_TCP(key1); - SESSION_KEY_IPV6_UDP(key2); - SESSION_KEY_IPV6_TCP(key3); - - struct session_key reverse_key1; - struct session_key reverse_key2; - struct session_key reverse_key3; - struct session_key reverse_temp; - - // tostring - memset(buf, 0, sizeof(buf)); - session_key_tostring(&key1, buf, sizeof(buf)); - EXPECT_STREQ(buf, "192.168.1.2:1234 -> 192.168.1.3:5678, proto: 6, zone: 0"); - memset(buf, 0, sizeof(buf)); - session_key_tostring(&key2, buf, sizeof(buf)); - EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234 -> 2001:db8::ff00:42:832a:5678, proto: 17, zone: 0"); - memset(buf, 0, sizeof(buf)); - session_key_tostring(&key3, buf, sizeof(buf)); - EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234 -> 2001:db8::ff00:42:832a:5678, proto: 6, zone: 0"); - - // reverse - session_key_reverse(&key1, &reverse_key1); - session_key_reverse(&key2, &reverse_key2); - session_key_reverse(&key3, &reverse_key3); - memset(buf, 0, sizeof(buf)); - session_key_tostring(&reverse_key1, buf, sizeof(buf)); - EXPECT_STREQ(buf, "192.168.1.3:5678 -> 192.168.1.2:1234, proto: 6, zone: 0"); - memset(buf, 0, sizeof(buf)); - session_key_tostring(&reverse_key2, buf, sizeof(buf)); - EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678 -> 2001:db8::ff00:42:8329:1234, proto: 17, zone: 0"); - memset(buf, 0, sizeof(buf)); - session_key_tostring(&reverse_key3, buf, sizeof(buf)); - EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678 -> 2001:db8::ff00:42:8329:1234, proto: 6, zone: 0"); - - // hash - EXPECT_TRUE(session_key_hash(&key1) == session_key_hash(&reverse_key1)); - EXPECT_TRUE(session_key_hash(&key2) == session_key_hash(&reverse_key2)); - EXPECT_TRUE(session_key_hash(&key3) == session_key_hash(&reverse_key3)); - - // cmp - EXPECT_TRUE(session_key_cmp(&key1, &key1) == 0); - EXPECT_TRUE(session_key_cmp(&key2, &key2) == 0); - EXPECT_TRUE(session_key_cmp(&key3, &key3) == 0); - - EXPECT_TRUE(session_key_cmp(&key1, &reverse_key1) != 0); - EXPECT_TRUE(session_key_cmp(&key2, &reverse_key2) != 0); - EXPECT_TRUE(session_key_cmp(&key3, &reverse_key3) != 0); - - session_key_reverse(&reverse_key1, &reverse_temp); - EXPECT_TRUE(session_key_cmp(&key1, &reverse_temp) == 0); - - session_key_reverse(&reverse_key2, &reverse_temp); - EXPECT_TRUE(session_key_cmp(&key2, &reverse_temp) == 0); - - session_key_reverse(&reverse_key3, &reverse_temp); - EXPECT_TRUE(session_key_cmp(&key3, &reverse_temp) == 0); -} - int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); diff --git a/src/session/gtest_session_table.cpp b/src/session/gtest_session_table.cpp index 89398be..1ffcbda 100644 --- a/src/session/gtest_session_table.cpp +++ b/src/session/gtest_session_table.cpp @@ -4,8 +4,8 @@ #include "session_table.h" #define SESSION_KEY_IPV4_TCP(name) \ - struct session_key name; \ - memset(&name, 0, sizeof(struct session_key)); \ + struct tuple6 name; \ + memset(&name, 0, sizeof(struct tuple6)); \ (name).ip_type = IP_TYPE_V4; \ (name).src_addr.v4.s_addr = inet_addr("192.168.1.2"); \ (name).dst_addr.v4.s_addr = inet_addr("192.168.1.3"); \ @@ -15,8 +15,8 @@ (name).security_zone = 0; #define SESSION_KEY_IPV6_UDP(name) \ - struct session_key name; \ - memset(&name, 0, sizeof(struct session_key)); \ + struct tuple6 name; \ + memset(&name, 0, sizeof(struct tuple6)); \ (name).ip_type = IP_TYPE_V6; \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:8329", &(name).src_addr.v6); \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:832a", &(name).dst_addr.v6); \ @@ -26,8 +26,8 @@ (name).security_zone = 0; #define SESSION_KEY_IPV6_TCP(name) \ - struct session_key name; \ - memset(&name, 0, sizeof(struct session_key)); \ + struct tuple6 name; \ + memset(&name, 0, sizeof(struct tuple6)); \ (name).ip_type = IP_TYPE_V6; \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:8329", &(name).src_addr.v6); \ inet_pton(AF_INET6, "2001:db8:0:0:0:ff00:42:832a", &(name).dst_addr.v6); \ @@ -54,17 +54,17 @@ TEST(SESSION_TABLE, OP_SESSION) struct session_pool *sess_pool = NULL; struct session_table *sess_table = NULL; - SESSION_KEY_IPV4_TCP(sess1_key); - SESSION_KEY_IPV6_UDP(sess2_key); - SESSION_KEY_IPV6_TCP(sess3_key); + SESSION_KEY_IPV4_TCP(tuple_1); + SESSION_KEY_IPV6_UDP(tuple_2); + SESSION_KEY_IPV6_TCP(tuple_3); - struct session_key sess1_key_reversed; - struct session_key sess2_key_reversed; - struct session_key sess3_key_reversed; + struct tuple6 reversed_tuple_1; + struct tuple6 reversed_tuple_2; + struct tuple6 reversed_tuple_3; - session_key_reverse(&sess1_key, &sess1_key_reversed); - session_key_reverse(&sess2_key, &sess2_key_reversed); - session_key_reverse(&sess3_key, &sess3_key_reversed); + tuple6_reverse(&tuple_1, &reversed_tuple_1); + tuple6_reverse(&tuple_2, &reversed_tuple_2); + tuple6_reverse(&tuple_3, &reversed_tuple_3); // Create sess_pool = session_pool_create(3); @@ -77,49 +77,49 @@ TEST(SESSION_TABLE, OP_SESSION) sess1 = session_pool_alloc(sess_pool); EXPECT_TRUE(sess1 != NULL); session_set_id(sess1, 1); - session_set_key(sess1, &sess1_key); + session_set_tuple6(sess1, &tuple_1); sess2 = session_pool_alloc(sess_pool); EXPECT_TRUE(sess2 != NULL); session_set_id(sess2, 2); - session_set_key(sess2, &sess2_key); + session_set_tuple6(sess2, &tuple_2); sess3 = session_pool_alloc(sess_pool); EXPECT_TRUE(sess3 != NULL); session_set_id(sess3, 3); - session_set_key(sess3, &sess3_key); + session_set_tuple6(sess3, &tuple_3); - EXPECT_TRUE(session_table_add_session(sess_table, &sess1_key, sess1) == 0); + EXPECT_TRUE(session_table_add_session(sess_table, &tuple_1, sess1) == 0); EXPECT_TRUE(session_table_get_count(sess_table) == 1); - EXPECT_TRUE(session_table_add_session(sess_table, &sess2_key, sess2) == 0); + EXPECT_TRUE(session_table_add_session(sess_table, &tuple_2, sess2) == 0); EXPECT_TRUE(session_table_get_count(sess_table) == 2); - EXPECT_TRUE(session_table_add_session(sess_table, &sess3_key, sess3) == 0); + EXPECT_TRUE(session_table_add_session(sess_table, &tuple_3, sess3) == 0); EXPECT_TRUE(session_table_get_count(sess_table) == 3); // Search - EXPECT_TRUE(session_table_find_session(sess_table, &sess1_key) == sess1); - EXPECT_TRUE(session_table_find_session(sess_table, &sess2_key) == sess2); - EXPECT_TRUE(session_table_find_session(sess_table, &sess3_key) == sess3); + EXPECT_TRUE(session_table_find_session(sess_table, &tuple_1) == sess1); + EXPECT_TRUE(session_table_find_session(sess_table, &tuple_2) == sess2); + EXPECT_TRUE(session_table_find_session(sess_table, &tuple_3) == sess3); - EXPECT_TRUE(session_table_find_session(sess_table, &sess1_key_reversed) == sess1); - EXPECT_TRUE(session_table_find_session(sess_table, &sess2_key_reversed) == sess2); - EXPECT_TRUE(session_table_find_session(sess_table, &sess3_key_reversed) == sess3); + EXPECT_TRUE(session_table_find_session(sess_table, &reversed_tuple_1) == sess1); + EXPECT_TRUE(session_table_find_session(sess_table, &reversed_tuple_2) == sess2); + EXPECT_TRUE(session_table_find_session(sess_table, &reversed_tuple_3) == sess3); // Delete - session_table_delete_session(sess_table, &sess1_key); + session_table_delete_session(sess_table, &tuple_1); EXPECT_TRUE(session_table_get_count(sess_table) == 2); - EXPECT_TRUE(session_table_find_session(sess_table, &sess1_key) == NULL); - EXPECT_TRUE(session_table_find_session(sess_table, &sess1_key_reversed) == NULL); + EXPECT_TRUE(session_table_find_session(sess_table, &tuple_1) == NULL); + EXPECT_TRUE(session_table_find_session(sess_table, &reversed_tuple_1) == NULL); - session_table_delete_session(sess_table, &sess2_key_reversed); + session_table_delete_session(sess_table, &reversed_tuple_2); EXPECT_TRUE(session_table_get_count(sess_table) == 1); - EXPECT_TRUE(session_table_find_session(sess_table, &sess2_key) == NULL); - EXPECT_TRUE(session_table_find_session(sess_table, &sess2_key_reversed) == NULL); + EXPECT_TRUE(session_table_find_session(sess_table, &tuple_2) == NULL); + EXPECT_TRUE(session_table_find_session(sess_table, &reversed_tuple_2) == NULL); - session_table_delete_session(sess_table, &sess3_key); + session_table_delete_session(sess_table, &tuple_3); EXPECT_TRUE(session_table_get_count(sess_table) == 0); - EXPECT_TRUE(session_table_find_session(sess_table, &sess3_key) == NULL); - EXPECT_TRUE(session_table_find_session(sess_table, &sess3_key_reversed) == NULL); + EXPECT_TRUE(session_table_find_session(sess_table, &tuple_3) == NULL); + EXPECT_TRUE(session_table_find_session(sess_table, &reversed_tuple_3) == NULL); // Destroy session_table_destroy(sess_table); @@ -134,9 +134,9 @@ TEST(SESSION_TABLE, FIND_OLDEST_NEWEST) struct session_pool *sess_pool = NULL; struct session_table *sess_table = NULL; - SESSION_KEY_IPV4_TCP(sess1_key); - SESSION_KEY_IPV6_UDP(sess2_key); - SESSION_KEY_IPV6_TCP(sess3_key); + SESSION_KEY_IPV4_TCP(tuple_1); + SESSION_KEY_IPV6_UDP(tuple_2); + SESSION_KEY_IPV6_TCP(tuple_3); // Create sess_pool = session_pool_create(3); @@ -153,38 +153,38 @@ TEST(SESSION_TABLE, FIND_OLDEST_NEWEST) sess1 = session_pool_alloc(sess_pool); EXPECT_TRUE(sess1 != NULL); session_set_id(sess1, 1); - session_set_key(sess1, &sess1_key); - EXPECT_TRUE(session_table_add_session(sess_table, &sess1_key, sess1) == 0); + session_set_tuple6(sess1, &tuple_1); + EXPECT_TRUE(session_table_add_session(sess_table, &tuple_1, sess1) == 0); EXPECT_TRUE(session_table_find_oldest_session(sess_table) == sess1); EXPECT_TRUE(session_table_find_newest_session(sess_table) == sess1); sess2 = session_pool_alloc(sess_pool); EXPECT_TRUE(sess2 != NULL); session_set_id(sess2, 2); - session_set_key(sess2, &sess2_key); - EXPECT_TRUE(session_table_add_session(sess_table, &sess2_key, sess2) == 0); + session_set_tuple6(sess2, &tuple_2); + EXPECT_TRUE(session_table_add_session(sess_table, &tuple_2, sess2) == 0); EXPECT_TRUE(session_table_find_oldest_session(sess_table) == sess1); EXPECT_TRUE(session_table_find_newest_session(sess_table) == sess2); sess3 = session_pool_alloc(sess_pool); EXPECT_TRUE(sess3 != NULL); session_set_id(sess3, 3); - session_set_key(sess3, &sess3_key); - EXPECT_TRUE(session_table_add_session(sess_table, &sess3_key, sess3) == 0); + session_set_tuple6(sess3, &tuple_3); + EXPECT_TRUE(session_table_add_session(sess_table, &tuple_3, sess3) == 0); EXPECT_TRUE(session_table_find_oldest_session(sess_table) == sess1); EXPECT_TRUE(session_table_find_newest_session(sess_table) == sess3); // Delete Session - session_table_delete_session(sess_table, &sess1_key); + session_table_delete_session(sess_table, &tuple_1); EXPECT_TRUE(session_table_find_oldest_session(sess_table) == sess2); EXPECT_TRUE(session_table_find_newest_session(sess_table) == sess3); - session_table_delete_session(sess_table, &sess2_key); + session_table_delete_session(sess_table, &tuple_2); EXPECT_TRUE(session_table_find_oldest_session(sess_table) == sess3); EXPECT_TRUE(session_table_find_newest_session(sess_table) == sess3); - session_table_delete_session(sess_table, &sess3_key); + session_table_delete_session(sess_table, &tuple_3); EXPECT_TRUE(session_table_find_oldest_session(sess_table) == NULL); EXPECT_TRUE(session_table_find_newest_session(sess_table) == NULL); diff --git a/src/session/session.cpp b/src/session/session.cpp index baa4721..0fbd434 100644 --- a/src/session/session.cpp +++ b/src/session/session.cpp @@ -1,6 +1,5 @@ #include <assert.h> -#include "crc32_hash.h" #include "session_private.h" #define EX_KEY_MAX_LEN 64 @@ -67,134 +66,6 @@ static bool event_queue_pop(struct event_queue *queue, uint32_t *event) } /****************************************************************************** - * session key - ******************************************************************************/ - -uint32_t session_key_hash(const struct session_key *key) -{ - uint32_t hash = crc32_hash(&key->security_zone, sizeof(key->security_zone), key->ip_proto); - - if (key->ip_type == IP_TYPE_V4) - { - uint32_t src_addr_hash = crc32_hash(&key->src_addr.v4, sizeof(key->src_addr.v4), hash); - uint32_t dst_addr_hash = crc32_hash(&key->dst_addr.v4, sizeof(key->dst_addr.v4), hash); - hash = src_addr_hash + dst_addr_hash; - } - else - { - uint32_t src_addr_hash = crc32_hash(&key->src_addr.v6, sizeof(key->src_addr.v6), hash); - uint32_t dst_addr_hash = crc32_hash(&key->dst_addr.v6, sizeof(key->dst_addr.v6), hash); - hash = src_addr_hash + dst_addr_hash; - } - - uint32_t src_port_hash = crc32_hash(&key->src_port, sizeof(key->src_port), hash); - uint32_t dst_port_hash = crc32_hash(&key->dst_port, sizeof(key->dst_port), hash); - hash = src_port_hash + dst_port_hash; - - return hash; -} - -// return 0: equal -// return -1: not equal -int session_key_cmp(const struct session_key *key1, const struct session_key *key2) -{ - if (key1->ip_type != key2->ip_type) - { - return -1; - } - - if (key1->src_port != key2->src_port) - { - return -1; - } - - if (key1->dst_port != key2->dst_port) - { - return -1; - } - - if (key1->ip_proto != key2->ip_proto) - { - return -1; - } - - if (key1->security_zone != key2->security_zone) - { - return -1; - } - - if (key1->ip_type == IP_TYPE_V4) - { - if (key1->src_addr.v4.s_addr != key2->src_addr.v4.s_addr) - { - return -1; - } - - if (key1->dst_addr.v4.s_addr != key2->dst_addr.v4.s_addr) - { - return -1; - } - } - else - { - if (memcmp(&key1->src_addr.v6, &key2->src_addr.v6, sizeof(key1->src_addr.v6)) != 0) - { - return -1; - } - - if (memcmp(&key1->dst_addr.v6, &key2->dst_addr.v6, sizeof(key1->dst_addr.v6)) != 0) - { - return -1; - } - } - - return 0; -} - -void session_key_reverse(const struct session_key *in, struct session_key *out) -{ - out->ip_type = in->ip_type; - out->src_port = in->dst_port; - out->dst_port = in->src_port; - out->ip_proto = in->ip_proto; - out->security_zone = in->security_zone; - - if (in->ip_type == IP_TYPE_V4) - { - out->src_addr.v4.s_addr = in->dst_addr.v4.s_addr; - out->dst_addr.v4.s_addr = in->src_addr.v4.s_addr; - } - else - { - memcpy(&out->src_addr.v6, &in->dst_addr.v6, sizeof(in->dst_addr.v6)); - memcpy(&out->dst_addr.v6, &in->src_addr.v6, sizeof(in->src_addr.v6)); - } -} - -void session_key_tostring(const struct session_key *key, char *buf, uint32_t buf_len) -{ - char src_addr[INET6_ADDRSTRLEN] = {0}; - char dst_addr[INET6_ADDRSTRLEN] = {0}; - - if (key->ip_type == IP_TYPE_V4) - { - inet_ntop(AF_INET, &key->src_addr.v4, src_addr, sizeof(src_addr)); - inet_ntop(AF_INET, &key->dst_addr.v4, dst_addr, sizeof(dst_addr)); - } - else - { - inet_ntop(AF_INET6, &key->src_addr.v6, src_addr, sizeof(src_addr)); - inet_ntop(AF_INET6, &key->dst_addr.v6, dst_addr, sizeof(dst_addr)); - } - - snprintf(buf, buf_len, "%s:%u -> %s:%u, proto: %u, zone: %lu", - src_addr, ntohs(key->src_port), - dst_addr, ntohs(key->dst_port), - key->ip_proto, - key->security_zone); -} - -/****************************************************************************** * session ******************************************************************************/ @@ -215,15 +86,15 @@ uint64_t session_get_id(struct session *sess) return sess->id; } -// session key -void session_set_key(struct session *sess, struct session_key *key) +// session tuple6 +void session_set_tuple6(struct session *sess, struct tuple6 *tuple) { - memcpy(&sess->key, key, sizeof(struct session_key)); + memcpy(&sess->tuple, tuple, sizeof(struct tuple6)); } -struct session_key *session_get0_key(struct session *sess) +struct tuple6 *session_get0_tuple6(struct session *sess) { - return &sess->key; + return &sess->tuple; } // session state @@ -341,7 +212,7 @@ void session_set0_cur_pkt(struct session *sess, const struct packet *pkt) sess->cur_pkt = pkt; } -struct packet *session_get0_cur_pkt(struct session *sess) +const struct packet *session_get0_cur_pkt(struct session *sess) { return sess->cur_pkt; } diff --git a/src/session/session.h b/src/session/session.h index f390128..05798e3 100644 --- a/src/session/session.h +++ b/src/session/session.h @@ -7,7 +7,8 @@ extern "C" #endif #include <stdint.h> -#include <arpa/inet.h> + +#include "tuple.h" enum session_state { @@ -39,31 +40,6 @@ enum session_event SESSION_EVENT_MAX, }; -union ip_address -{ - struct in_addr v4; /* network order */ - struct in6_addr v6; /* network order */ -}; - -enum ip_type -{ - IP_TYPE_V4, - IP_TYPE_V6, -}; - -struct session_key -{ - enum ip_type ip_type; - - // six tuple - union ip_address src_addr; /* network order */ - union ip_address dst_addr; /* network order */ - uint16_t src_port; /* network order */ - uint16_t dst_port; /* network order */ - uint16_t ip_proto; /* network order */ - uint64_t security_zone; -}; - enum session_dir { SESSION_DIR_C2S = 0, @@ -79,15 +55,6 @@ struct metadata struct session; /****************************************************************************** - * session key - ******************************************************************************/ - -uint32_t session_key_hash(const struct session_key *key); -int session_key_cmp(const struct session_key *key1, const struct session_key *key2); -void session_key_reverse(const struct session_key *in, struct session_key *out); -void session_key_tostring(const struct session_key *key, char *buf, uint32_t buf_len); - -/****************************************************************************** * session base info ******************************************************************************/ @@ -98,8 +65,8 @@ void session_set_id(struct session *sess, uint64_t id); uint64_t session_get_id(struct session *sess); // session key -void session_set_key(struct session *sess, struct session_key *key); -struct session_key *session_get0_key(struct session *sess); +void session_set_tuple6(struct session *sess, struct tuple6 *tuple); +struct tuple6 *session_get0_tuple6(struct session *sess); // session state void session_set_state(struct session *sess, enum session_state state); @@ -135,7 +102,7 @@ uint64_t session_get_last_time(struct session *sess); // session current packet void session_set0_cur_pkt(struct session *sess, const struct packet *pkt); -struct packet *session_get0_cur_pkt(struct session *sess); +const struct packet *session_get0_cur_pkt(struct session *sess); // session current dir void session_set_cur_dir(struct session *sess, enum session_dir dir); diff --git a/src/session/session_private.h b/src/session/session_private.h index 4662a32..5018c4f 100644 --- a/src/session/session_private.h +++ b/src/session/session_private.h @@ -54,7 +54,7 @@ struct session ******************************/ // session current packet - struct packet *cur_pkt; + const struct packet *cur_pkt; enum session_dir cur_dir; /****************************** @@ -91,7 +91,7 @@ struct session ******************************/ // session table key - struct session_key key; + struct tuple6 tuple; struct session *next_ptr; struct session *prev_ptr; diff --git a/src/session/session_table.cpp b/src/session/session_table.cpp index 7312ed2..a56a64a 100644 --- a/src/session/session_table.cpp +++ b/src/session/session_table.cpp @@ -20,24 +20,24 @@ struct session_table * Private API ******************************************************************************/ -static void HASH_FUNCTION_OVERWRITE(const struct session_key *key, unsigned int keylen, uint32_t *hashv) +static void HASH_FUNCTION_OVERWRITE(const struct tuple6 *tuple, unsigned int keylen, uint32_t *hashv) { - *hashv = session_key_hash(key); + *hashv = tuple6_hash(tuple); } -static int HASH_KEYCMP_OVERWRITE(const void *key1, const void *key2, size_t len) +static int HASH_KEYCMP_OVERWRITE(const void *key_a, const void *key_b, size_t len) { - struct session_key *sess_key1 = (struct session_key *)key1; - struct session_key *sess_key2 = (struct session_key *)key2; + struct tuple6 *tuple_a = (struct tuple6 *)key_a; + struct tuple6 *tuple_b = (struct tuple6 *)key_b; - if (session_key_cmp(sess_key1, sess_key2) == 0) + if (tuple6_cmp(tuple_a, tuple_b) == 0) { return 0; } - struct session_key reverse_key; - session_key_reverse(sess_key2, &reverse_key); - if (session_key_cmp(sess_key1, &reverse_key) == 0) + struct tuple6 reversed; + tuple6_reverse(tuple_b, &reversed); + if (tuple6_cmp(tuple_a, &reversed) == 0) { return 0; } @@ -153,33 +153,33 @@ void session_table_set_freecb(struct session_table *table, session_free_cb free_ } } -int session_table_add_session(struct session_table *table, const struct session_key *key, struct session *sess) +int session_table_add_session(struct session_table *table, const struct tuple6 *tuple, struct session *sess) { if (table == NULL || sess == NULL) { return -1; } - if (session_table_find_session(table, key)) + if (session_table_find_session(table, tuple)) { return -1; } - HASH_ADD(hh, table->root, key, sizeof(sess->key), sess); + HASH_ADD(hh, table->root, tuple, sizeof(sess->tuple), sess); session_table_add_session_to_linklist(table, sess); table->count++; return 0; } -void session_table_delete_session(struct session_table *table, const struct session_key *key) +void session_table_delete_session(struct session_table *table, const struct tuple6 *tuple) { if (table == NULL) { return; } - struct session *sess = session_table_find_session(table, key); + struct session *sess = session_table_find_session(table, tuple); if (sess == NULL) { return; @@ -194,7 +194,7 @@ void session_table_delete_session(struct session_table *table, const struct sess table->count--; } -struct session *session_table_find_session(struct session_table *table, const struct session_key *key) +struct session *session_table_find_session(struct session_table *table, const struct tuple6 *tuple) { if (table == NULL) { @@ -202,7 +202,7 @@ struct session *session_table_find_session(struct session_table *table, const st } struct session *sess = NULL; - HASH_FIND(hh, table->root, key, sizeof(struct session_key), sess); + HASH_FIND(hh, table->root, tuple, sizeof(struct tuple6), sess); return sess; } diff --git a/src/session/session_table.h b/src/session/session_table.h index e9f6b3e..9bd9025 100644 --- a/src/session/session_table.h +++ b/src/session/session_table.h @@ -17,9 +17,9 @@ typedef void (*session_free_cb)(struct session *sess, void *arg); void session_table_set_freecb(struct session_table *table, session_free_cb free_cb, void *arg); // return 0: success // return -1: failed -int session_table_add_session(struct session_table *table, const struct session_key *key, struct session *sess); -void session_table_delete_session(struct session_table *table, const struct session_key *key); -struct session *session_table_find_session(struct session_table *table, const struct session_key *key); +int session_table_add_session(struct session_table *table, const struct tuple6 *tuple, struct session *sess); +void session_table_delete_session(struct session_table *table, const struct tuple6 *tuple); +struct session *session_table_find_session(struct session_table *table, const struct tuple6 *tuple); struct session *session_table_find_oldest_session(struct session_table *table); struct session *session_table_find_newest_session(struct session_table *table); diff --git a/src/tuple/CMakeLists.txt b/src/tuple/CMakeLists.txt new file mode 100644 index 0000000..5da48ef --- /dev/null +++ b/src/tuple/CMakeLists.txt @@ -0,0 +1,19 @@ +############################################################################### +# tuple +############################################################################### + +add_library(tuple tuple.cpp) +target_include_directories(tuple PUBLIC ${CMAKE_SOURCE_DIR}/src/tuple) +target_include_directories(tuple PUBLIC ${CMAKE_SOURCE_DIR}/src/crc32) +target_link_libraries(tuple) + +############################################################################### +# gtest +############################################################################### + +add_executable(gtest_tuple gtest_tuple.cpp) +target_include_directories(gtest_tuple PUBLIC ${CMAKE_CURRENT_LIST_DIR}) +target_link_libraries(gtest_tuple tuple gtest) + +include(GoogleTest) +gtest_discover_tests(gtest_tuple)
\ No newline at end of file diff --git a/src/tuple/gtest_tuple.cpp b/src/tuple/gtest_tuple.cpp new file mode 100644 index 0000000..fa10f08 --- /dev/null +++ b/src/tuple/gtest_tuple.cpp @@ -0,0 +1,263 @@ +#include <gtest/gtest.h> + +#include "tuple.h" + +TEST(TUPLE, TUPLE2) +{ + char buf[128] = {0}; + struct tuple2 temp; + struct tuple2 tuple_a; + struct tuple2 tuple_b; + struct tuple2 reversed_tuple_a; + struct tuple2 reversed_tuple_b; + + memset(&temp, 0, sizeof(struct tuple2)); + memset(&tuple_a, 0, sizeof(struct tuple2)); + memset(&tuple_b, 0, sizeof(struct tuple2)); + memset(&reversed_tuple_a, 0, sizeof(struct tuple2)); + memset(&reversed_tuple_b, 0, sizeof(struct tuple2)); + + tuple_a.ip_type = IP_TYPE_V4; + tuple_a.src_addr.v4.s_addr = inet_addr("192.168.1.2"); + tuple_a.dst_addr.v4.s_addr = inet_addr("192.168.1.3"); + + tuple_b.ip_type = IP_TYPE_V6; + inet_pton(AF_INET6, "2001:db8::ff00:42:8329", &tuple_b.src_addr.v6); + inet_pton(AF_INET6, "2001:db8::ff00:42:832a", &tuple_b.dst_addr.v6); + + // tostring + memset(buf, 0, sizeof(buf)); + tuple2_tostring(&tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.2 -> 192.168.1.3"); + memset(buf, 0, sizeof(buf)); + tuple2_tostring(&tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:8329 -> 2001:db8::ff00:42:832a"); + + // reverse + tuple2_reverse(&tuple_a, &reversed_tuple_a); + tuple2_reverse(&tuple_b, &reversed_tuple_b); + memset(buf, 0, sizeof(buf)); + tuple2_tostring(&reversed_tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.3 -> 192.168.1.2"); + memset(buf, 0, sizeof(buf)); + tuple2_tostring(&reversed_tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:832a -> 2001:db8::ff00:42:8329"); + + // hash + EXPECT_TRUE(tuple2_hash(&tuple_a) == tuple2_hash(&reversed_tuple_a)); + EXPECT_TRUE(tuple2_hash(&tuple_b) == tuple2_hash(&reversed_tuple_b)); + + // cmp + EXPECT_TRUE(tuple2_cmp(&tuple_a, &tuple_a) == 0); + EXPECT_TRUE(tuple2_cmp(&tuple_b, &tuple_b) == 0); + + EXPECT_TRUE(tuple2_cmp(&tuple_a, &reversed_tuple_a) != 0); + EXPECT_TRUE(tuple2_cmp(&tuple_b, &reversed_tuple_b) != 0); + + tuple2_reverse(&reversed_tuple_a, &temp); + EXPECT_TRUE(tuple2_cmp(&tuple_a, &temp) == 0); + + tuple2_reverse(&reversed_tuple_b, &temp); + EXPECT_TRUE(tuple2_cmp(&tuple_b, &temp) == 0); +} + +TEST(TUPLE, TUPLE4) +{ + char buf[128] = {0}; + struct tuple4 temp; + struct tuple4 tuple_a; + struct tuple4 tuple_b; + struct tuple4 reversed_tuple_a; + struct tuple4 reversed_tuple_b; + + memset(&temp, 0, sizeof(struct tuple4)); + memset(&tuple_a, 0, sizeof(struct tuple4)); + memset(&tuple_b, 0, sizeof(struct tuple4)); + memset(&reversed_tuple_a, 0, sizeof(struct tuple4)); + memset(&reversed_tuple_b, 0, sizeof(struct tuple4)); + + tuple_a.ip_type = IP_TYPE_V4; + tuple_a.src_addr.v4.s_addr = inet_addr("192.168.1.2"); + tuple_a.dst_addr.v4.s_addr = inet_addr("192.168.1.3"); + tuple_a.src_port = htons(1234); + tuple_a.dst_port = htons(5678); + + tuple_b.ip_type = IP_TYPE_V6; + inet_pton(AF_INET6, "2001:db8::ff00:42:8329", &tuple_b.src_addr.v6); + inet_pton(AF_INET6, "2001:db8::ff00:42:832a", &tuple_b.dst_addr.v6); + tuple_b.src_port = htons(1234); + tuple_b.dst_port = htons(5678); + + // tostring + memset(buf, 0, sizeof(buf)); + tuple4_tostring(&tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.2:1234 -> 192.168.1.3:5678"); + memset(buf, 0, sizeof(buf)); + tuple4_tostring(&tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234 -> 2001:db8::ff00:42:832a:5678"); + + // reverse + tuple4_reverse(&tuple_a, &reversed_tuple_a); + tuple4_reverse(&tuple_b, &reversed_tuple_b); + memset(buf, 0, sizeof(buf)); + tuple4_tostring(&reversed_tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.3:5678 -> 192.168.1.2:1234"); + memset(buf, 0, sizeof(buf)); + tuple4_tostring(&reversed_tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678 -> 2001:db8::ff00:42:8329:1234"); + + // hash + EXPECT_TRUE(tuple4_hash(&tuple_a) == tuple4_hash(&reversed_tuple_a)); + EXPECT_TRUE(tuple4_hash(&tuple_b) == tuple4_hash(&reversed_tuple_b)); + + // cmp + EXPECT_TRUE(tuple4_cmp(&tuple_a, &tuple_a) == 0); + EXPECT_TRUE(tuple4_cmp(&tuple_b, &tuple_b) == 0); + + EXPECT_TRUE(tuple4_cmp(&tuple_a, &reversed_tuple_a) != 0); + EXPECT_TRUE(tuple4_cmp(&tuple_b, &reversed_tuple_b) != 0); + + tuple4_reverse(&reversed_tuple_a, &temp); + EXPECT_TRUE(tuple4_cmp(&tuple_a, &temp) == 0); + + tuple4_reverse(&reversed_tuple_b, &temp); + EXPECT_TRUE(tuple4_cmp(&tuple_b, &temp) == 0); +} + +TEST(TUPLE, TUPLE5) +{ + char buf[128] = {0}; + struct tuple5 temp; + struct tuple5 tuple_a; + struct tuple5 tuple_b; + struct tuple5 reversed_tuple_a; + struct tuple5 reversed_tuple_b; + + memset(&temp, 0, sizeof(struct tuple5)); + memset(&tuple_a, 0, sizeof(struct tuple5)); + memset(&tuple_b, 0, sizeof(struct tuple5)); + memset(&reversed_tuple_a, 0, sizeof(struct tuple5)); + memset(&reversed_tuple_b, 0, sizeof(struct tuple5)); + + tuple_a.ip_type = IP_TYPE_V4; + tuple_a.src_addr.v4.s_addr = inet_addr("192.168.1.2"); + tuple_a.dst_addr.v4.s_addr = inet_addr("192.168.1.3"); + tuple_a.src_port = htons(1234); + tuple_a.dst_port = htons(5678); + tuple_a.ip_proto = IPPROTO_TCP; + + tuple_b.ip_type = IP_TYPE_V6; + inet_pton(AF_INET6, "2001:db8::ff00:42:8329", &tuple_b.src_addr.v6); + inet_pton(AF_INET6, "2001:db8::ff00:42:832a", &tuple_b.dst_addr.v6); + tuple_b.src_port = htons(1234); + tuple_b.dst_port = htons(5678); + tuple_b.ip_proto = IPPROTO_UDP; + + // tostring + memset(buf, 0, sizeof(buf)); + tuple5_tostring(&tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.2:1234 -> 192.168.1.3:5678, proto: 6"); + memset(buf, 0, sizeof(buf)); + tuple5_tostring(&tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234 -> 2001:db8::ff00:42:832a:5678, proto: 17"); + + // reverse + tuple5_reverse(&tuple_a, &reversed_tuple_a); + tuple5_reverse(&tuple_b, &reversed_tuple_b); + memset(buf, 0, sizeof(buf)); + tuple5_tostring(&reversed_tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.3:5678 -> 192.168.1.2:1234, proto: 6"); + memset(buf, 0, sizeof(buf)); + tuple5_tostring(&reversed_tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678 -> 2001:db8::ff00:42:8329:1234, proto: 17"); + + // hash + EXPECT_TRUE(tuple5_hash(&tuple_a) == tuple5_hash(&reversed_tuple_a)); + EXPECT_TRUE(tuple5_hash(&tuple_b) == tuple5_hash(&reversed_tuple_b)); + + // cmp + EXPECT_TRUE(tuple5_cmp(&tuple_a, &tuple_a) == 0); + EXPECT_TRUE(tuple5_cmp(&tuple_b, &tuple_b) == 0); + + EXPECT_TRUE(tuple5_cmp(&tuple_a, &reversed_tuple_a) != 0); + EXPECT_TRUE(tuple5_cmp(&tuple_b, &reversed_tuple_b) != 0); + + tuple5_reverse(&reversed_tuple_a, &temp); + EXPECT_TRUE(tuple5_cmp(&tuple_a, &temp) == 0); + + tuple5_reverse(&reversed_tuple_b, &temp); + EXPECT_TRUE(tuple5_cmp(&tuple_b, &temp) == 0); +} + +TEST(TUPLE, TUPLE6) +{ + char buf[128] = {0}; + struct tuple6 temp; + struct tuple6 tuple_a; + struct tuple6 tuple_b; + struct tuple6 reversed_tuple_a; + struct tuple6 reversed_tuple_b; + + memset(&temp, 0, sizeof(struct tuple6)); + memset(&tuple_a, 0, sizeof(struct tuple6)); + memset(&tuple_b, 0, sizeof(struct tuple6)); + memset(&reversed_tuple_a, 0, sizeof(struct tuple6)); + memset(&reversed_tuple_b, 0, sizeof(struct tuple6)); + + tuple_a.ip_type = IP_TYPE_V4; + tuple_a.src_addr.v4.s_addr = inet_addr("192.168.1.2"); + tuple_a.dst_addr.v4.s_addr = inet_addr("192.168.1.3"); + tuple_a.src_port = htons(1234); + tuple_a.dst_port = htons(5678); + tuple_a.ip_proto = IPPROTO_TCP; + tuple_a.security_zone = 0; + + tuple_b.ip_type = IP_TYPE_V6; + inet_pton(AF_INET6, "2001:db8::ff00:42:8329", &tuple_b.src_addr.v6); + inet_pton(AF_INET6, "2001:db8::ff00:42:832a", &tuple_b.dst_addr.v6); + tuple_b.src_port = htons(1234); + tuple_b.dst_port = htons(5678); + tuple_b.ip_proto = IPPROTO_UDP; + tuple_b.security_zone = 0; + + // tostring + memset(buf, 0, sizeof(buf)); + tuple6_tostring(&tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.2:1234 -> 192.168.1.3:5678, proto: 6, zone: 0"); + memset(buf, 0, sizeof(buf)); + tuple6_tostring(&tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234 -> 2001:db8::ff00:42:832a:5678, proto: 17, zone: 0"); + + // reverse + tuple6_reverse(&tuple_a, &reversed_tuple_a); + tuple6_reverse(&tuple_b, &reversed_tuple_b); + memset(buf, 0, sizeof(buf)); + tuple6_tostring(&reversed_tuple_a, buf, sizeof(buf)); + EXPECT_STREQ(buf, "192.168.1.3:5678 -> 192.168.1.2:1234, proto: 6, zone: 0"); + memset(buf, 0, sizeof(buf)); + tuple6_tostring(&reversed_tuple_b, buf, sizeof(buf)); + EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678 -> 2001:db8::ff00:42:8329:1234, proto: 17, zone: 0"); + + // hash + EXPECT_TRUE(tuple6_hash(&tuple_a) == tuple6_hash(&reversed_tuple_a)); + EXPECT_TRUE(tuple6_hash(&tuple_b) == tuple6_hash(&reversed_tuple_b)); + + // cmp + EXPECT_TRUE(tuple6_cmp(&tuple_a, &tuple_a) == 0); + EXPECT_TRUE(tuple6_cmp(&tuple_b, &tuple_b) == 0); + + EXPECT_TRUE(tuple6_cmp(&tuple_a, &reversed_tuple_a) != 0); + EXPECT_TRUE(tuple6_cmp(&tuple_b, &reversed_tuple_b) != 0); + + tuple6_reverse(&reversed_tuple_a, &temp); + EXPECT_TRUE(tuple6_cmp(&tuple_a, &temp) == 0); + + tuple6_reverse(&reversed_tuple_b, &temp); + EXPECT_TRUE(tuple6_cmp(&tuple_b, &temp) == 0); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/src/tuple/tuple.cpp b/src/tuple/tuple.cpp new file mode 100644 index 0000000..9b4fed2 --- /dev/null +++ b/src/tuple/tuple.cpp @@ -0,0 +1,452 @@ +#include <string.h> +#include <stdio.h> + +#include "tuple.h" +#include "crc32_hash.h" + +uint32_t tuple2_hash(const struct tuple2 *tuple) +{ + uint32_t src_addr_hash = 0; + uint32_t dst_addr_hash = 0; + uint32_t hash = crc32_hash(&tuple->ip_type, sizeof(tuple->ip_type), 0); + + if (tuple->ip_type == IP_TYPE_V4) + { + src_addr_hash = crc32_hash(&tuple->src_addr.v4, sizeof(tuple->src_addr.v4), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v4, sizeof(tuple->dst_addr.v4), hash); + } + else + { + src_addr_hash = crc32_hash(&tuple->src_addr.v6, sizeof(tuple->src_addr.v6), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v6, sizeof(tuple->dst_addr.v6), hash); + } + hash = src_addr_hash + dst_addr_hash; + + return hash; +} + +uint32_t tuple4_hash(const struct tuple4 *tuple) +{ + uint32_t src_addr_hash = 0; + uint32_t dst_addr_hash = 0; + uint32_t src_port_hash = 0; + uint32_t dst_port_hash = 0; + uint32_t hash = crc32_hash(&tuple->ip_type, sizeof(tuple->ip_type), 0); + + if (tuple->ip_type == IP_TYPE_V4) + { + src_addr_hash = crc32_hash(&tuple->src_addr.v4, sizeof(tuple->src_addr.v4), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v4, sizeof(tuple->dst_addr.v4), hash); + } + else + { + src_addr_hash = crc32_hash(&tuple->src_addr.v6, sizeof(tuple->src_addr.v6), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v6, sizeof(tuple->dst_addr.v6), hash); + } + hash = src_addr_hash + dst_addr_hash; + + src_port_hash = crc32_hash(&tuple->src_port, sizeof(tuple->src_port), hash); + dst_port_hash = crc32_hash(&tuple->dst_port, sizeof(tuple->dst_port), hash); + hash = src_port_hash + dst_port_hash; + + return hash; +} + +uint32_t tuple5_hash(const struct tuple5 *tuple) +{ + uint32_t src_addr_hash = 0; + uint32_t dst_addr_hash = 0; + uint32_t src_port_hash = 0; + uint32_t dst_port_hash = 0; + uint32_t hash = crc32_hash(&tuple->ip_type, sizeof(tuple->ip_type), 0); + hash = crc32_hash(&tuple->ip_proto, sizeof(tuple->ip_proto), hash); + + if (tuple->ip_type == IP_TYPE_V4) + { + src_addr_hash = crc32_hash(&tuple->src_addr.v4, sizeof(tuple->src_addr.v4), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v4, sizeof(tuple->dst_addr.v4), hash); + } + else + { + src_addr_hash = crc32_hash(&tuple->src_addr.v6, sizeof(tuple->src_addr.v6), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v6, sizeof(tuple->dst_addr.v6), hash); + } + hash = src_addr_hash + dst_addr_hash; + + src_port_hash = crc32_hash(&tuple->src_port, sizeof(tuple->src_port), hash); + dst_port_hash = crc32_hash(&tuple->dst_port, sizeof(tuple->dst_port), hash); + hash = src_port_hash + dst_port_hash; + + return hash; +} + +uint32_t tuple6_hash(const struct tuple6 *tuple) +{ + uint32_t src_addr_hash = 0; + uint32_t dst_addr_hash = 0; + uint32_t src_port_hash = 0; + uint32_t dst_port_hash = 0; + uint32_t hash = crc32_hash(&tuple->ip_type, sizeof(tuple->ip_type), 0); + hash = crc32_hash(&tuple->ip_proto, sizeof(tuple->ip_proto), hash); + hash = crc32_hash(&tuple->security_zone, sizeof(tuple->security_zone), hash); + + if (tuple->ip_type == IP_TYPE_V4) + { + src_addr_hash = crc32_hash(&tuple->src_addr.v4, sizeof(tuple->src_addr.v4), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v4, sizeof(tuple->dst_addr.v4), hash); + } + else + { + src_addr_hash = crc32_hash(&tuple->src_addr.v6, sizeof(tuple->src_addr.v6), hash); + dst_addr_hash = crc32_hash(&tuple->dst_addr.v6, sizeof(tuple->dst_addr.v6), hash); + } + hash = src_addr_hash + dst_addr_hash; + + src_port_hash = crc32_hash(&tuple->src_port, sizeof(tuple->src_port), hash); + dst_port_hash = crc32_hash(&tuple->dst_port, sizeof(tuple->dst_port), hash); + hash = src_port_hash + dst_port_hash; + + return hash; +} + +int tuple2_cmp(const struct tuple2 *tuple_a, const struct tuple2 *tuple_b) +{ + if (tuple_a->ip_type != tuple_b->ip_type) + { + return -1; + } + + if (tuple_a->ip_type == IP_TYPE_V4) + { + if (tuple_a->src_addr.v4.s_addr != tuple_b->src_addr.v4.s_addr) + { + return -1; + } + + if (tuple_a->dst_addr.v4.s_addr != tuple_b->dst_addr.v4.s_addr) + { + return -1; + } + } + else + { + if (memcmp(&tuple_a->src_addr.v6, &tuple_b->src_addr.v6, sizeof(tuple_a->src_addr.v6)) != 0) + { + return -1; + } + + if (memcmp(&tuple_a->dst_addr.v6, &tuple_b->dst_addr.v6, sizeof(tuple_a->dst_addr.v6)) != 0) + { + return -1; + } + } + + return 0; +} + +int tuple4_cmp(const struct tuple4 *tuple_a, const struct tuple4 *tuple_b) +{ + if (tuple_a->src_port != tuple_b->src_port) + { + return -1; + } + + if (tuple_a->dst_port != tuple_b->dst_port) + { + return -1; + } + + if (tuple_a->ip_type != tuple_b->ip_type) + { + return -1; + } + + if (tuple_a->ip_type == IP_TYPE_V4) + { + if (tuple_a->src_addr.v4.s_addr != tuple_b->src_addr.v4.s_addr) + { + return -1; + } + + if (tuple_a->dst_addr.v4.s_addr != tuple_b->dst_addr.v4.s_addr) + { + return -1; + } + } + else + { + if (memcmp(&tuple_a->src_addr.v6, &tuple_b->src_addr.v6, sizeof(tuple_a->src_addr.v6)) != 0) + { + return -1; + } + + if (memcmp(&tuple_a->dst_addr.v6, &tuple_b->dst_addr.v6, sizeof(tuple_a->dst_addr.v6)) != 0) + { + return -1; + } + } + + return 0; +} + +int tuple5_cmp(const struct tuple5 *tuple_a, const struct tuple5 *tuple_b) +{ + if (tuple_a->ip_proto != tuple_b->ip_proto) + { + return -1; + } + + if (tuple_a->src_port != tuple_b->src_port) + { + return -1; + } + + if (tuple_a->dst_port != tuple_b->dst_port) + { + return -1; + } + + if (tuple_a->ip_type != tuple_b->ip_type) + { + return -1; + } + + if (tuple_a->ip_type == IP_TYPE_V4) + { + if (tuple_a->src_addr.v4.s_addr != tuple_b->src_addr.v4.s_addr) + { + return -1; + } + + if (tuple_a->dst_addr.v4.s_addr != tuple_b->dst_addr.v4.s_addr) + { + return -1; + } + } + else + { + if (memcmp(&tuple_a->src_addr.v6, &tuple_b->src_addr.v6, sizeof(tuple_a->src_addr.v6)) != 0) + { + return -1; + } + + if (memcmp(&tuple_a->dst_addr.v6, &tuple_b->dst_addr.v6, sizeof(tuple_a->dst_addr.v6)) != 0) + { + return -1; + } + } + + return 0; +} + +int tuple6_cmp(const struct tuple6 *tuple_a, const struct tuple6 *tuple_b) +{ + if (tuple_a->security_zone != tuple_b->security_zone) + { + return -1; + } + + if (tuple_a->ip_proto != tuple_b->ip_proto) + { + return -1; + } + + if (tuple_a->src_port != tuple_b->src_port) + { + return -1; + } + + if (tuple_a->dst_port != tuple_b->dst_port) + { + return -1; + } + + if (tuple_a->ip_type != tuple_b->ip_type) + { + return -1; + } + + if (tuple_a->ip_type == IP_TYPE_V4) + { + if (tuple_a->src_addr.v4.s_addr != tuple_b->src_addr.v4.s_addr) + { + return -1; + } + + if (tuple_a->dst_addr.v4.s_addr != tuple_b->dst_addr.v4.s_addr) + { + return -1; + } + } + else + { + if (memcmp(&tuple_a->src_addr.v6, &tuple_b->src_addr.v6, sizeof(tuple_a->src_addr.v6)) != 0) + { + return -1; + } + + if (memcmp(&tuple_a->dst_addr.v6, &tuple_b->dst_addr.v6, sizeof(tuple_a->dst_addr.v6)) != 0) + { + return -1; + } + } + + return 0; +} + +void tuple2_reverse(const struct tuple2 *in, struct tuple2 *out) +{ + out->ip_type = in->ip_type; + + if (in->ip_type == IP_TYPE_V4) + { + out->src_addr.v4.s_addr = in->dst_addr.v4.s_addr; + out->dst_addr.v4.s_addr = in->src_addr.v4.s_addr; + } + else + { + memcpy(&out->src_addr.v6, &in->dst_addr.v6, sizeof(in->dst_addr.v6)); + memcpy(&out->dst_addr.v6, &in->src_addr.v6, sizeof(in->src_addr.v6)); + } +} + +void tuple4_reverse(const struct tuple4 *in, struct tuple4 *out) +{ + out->ip_type = in->ip_type; + out->src_port = in->dst_port; + out->dst_port = in->src_port; + + if (in->ip_type == IP_TYPE_V4) + { + out->src_addr.v4.s_addr = in->dst_addr.v4.s_addr; + out->dst_addr.v4.s_addr = in->src_addr.v4.s_addr; + } + else + { + memcpy(&out->src_addr.v6, &in->dst_addr.v6, sizeof(in->dst_addr.v6)); + memcpy(&out->dst_addr.v6, &in->src_addr.v6, sizeof(in->src_addr.v6)); + } +} + +void tuple5_reverse(const struct tuple5 *in, struct tuple5 *out) +{ + out->ip_type = in->ip_type; + out->ip_proto = in->ip_proto; + out->src_port = in->dst_port; + out->dst_port = in->src_port; + + if (in->ip_type == IP_TYPE_V4) + { + out->src_addr.v4.s_addr = in->dst_addr.v4.s_addr; + out->dst_addr.v4.s_addr = in->src_addr.v4.s_addr; + } + else + { + memcpy(&out->src_addr.v6, &in->dst_addr.v6, sizeof(in->dst_addr.v6)); + memcpy(&out->dst_addr.v6, &in->src_addr.v6, sizeof(in->src_addr.v6)); + } +} + +void tuple6_reverse(const struct tuple6 *in, struct tuple6 *out) +{ + out->ip_type = in->ip_type; + out->ip_proto = in->ip_proto; + out->security_zone = in->security_zone; + out->src_port = in->dst_port; + out->dst_port = in->src_port; + + if (in->ip_type == IP_TYPE_V4) + { + out->src_addr.v4.s_addr = in->dst_addr.v4.s_addr; + out->dst_addr.v4.s_addr = in->src_addr.v4.s_addr; + } + else + { + memcpy(&out->src_addr.v6, &in->dst_addr.v6, sizeof(in->dst_addr.v6)); + memcpy(&out->dst_addr.v6, &in->src_addr.v6, sizeof(in->src_addr.v6)); + } +} + +void tuple2_tostring(const struct tuple2 *tuple, char *buf, uint32_t size) +{ + char src_addr[INET6_ADDRSTRLEN] = {0}; + char dst_addr[INET6_ADDRSTRLEN] = {0}; + + if (tuple->ip_type == IP_TYPE_V4) + { + inet_ntop(AF_INET, &tuple->src_addr.v4, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET, &tuple->dst_addr.v4, dst_addr, INET6_ADDRSTRLEN); + } + else + { + inet_ntop(AF_INET6, &tuple->src_addr.v6, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET6, &tuple->dst_addr.v6, dst_addr, INET6_ADDRSTRLEN); + } + + snprintf(buf, size, "%s -> %s", src_addr, dst_addr); +} + +void tuple4_tostring(const struct tuple4 *tuple, char *buf, uint32_t size) +{ + char src_addr[INET6_ADDRSTRLEN] = {0}; + char dst_addr[INET6_ADDRSTRLEN] = {0}; + + if (tuple->ip_type == IP_TYPE_V4) + { + inet_ntop(AF_INET, &tuple->src_addr.v4, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET, &tuple->dst_addr.v4, dst_addr, INET6_ADDRSTRLEN); + } + else + { + inet_ntop(AF_INET6, &tuple->src_addr.v6, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET6, &tuple->dst_addr.v6, dst_addr, INET6_ADDRSTRLEN); + } + + snprintf(buf, size, "%s:%u -> %s:%u", + src_addr, ntohs(tuple->src_port), + dst_addr, ntohs(tuple->dst_port)); +} + +void tuple5_tostring(const struct tuple5 *tuple, char *buf, uint32_t size) +{ + char src_addr[INET6_ADDRSTRLEN] = {0}; + char dst_addr[INET6_ADDRSTRLEN] = {0}; + + if (tuple->ip_type == IP_TYPE_V4) + { + inet_ntop(AF_INET, &tuple->src_addr.v4, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET, &tuple->dst_addr.v4, dst_addr, INET6_ADDRSTRLEN); + } + else + { + inet_ntop(AF_INET6, &tuple->src_addr.v6, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET6, &tuple->dst_addr.v6, dst_addr, INET6_ADDRSTRLEN); + } + + snprintf(buf, size, "%s:%u -> %s:%u, proto: %u", + src_addr, ntohs(tuple->src_port), + dst_addr, ntohs(tuple->dst_port), + tuple->ip_proto); +} + +void tuple6_tostring(const struct tuple6 *tuple, char *buf, uint32_t size) +{ + char src_addr[INET6_ADDRSTRLEN] = {0}; + char dst_addr[INET6_ADDRSTRLEN] = {0}; + + if (tuple->ip_type == IP_TYPE_V4) + { + inet_ntop(AF_INET, &tuple->src_addr.v4, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET, &tuple->dst_addr.v4, dst_addr, INET6_ADDRSTRLEN); + } + else + { + inet_ntop(AF_INET6, &tuple->src_addr.v6, src_addr, INET6_ADDRSTRLEN); + inet_ntop(AF_INET6, &tuple->dst_addr.v6, dst_addr, INET6_ADDRSTRLEN); + } + + snprintf(buf, size, "%s:%u -> %s:%u, proto: %u, zone: %lu", + src_addr, ntohs(tuple->src_port), + dst_addr, ntohs(tuple->dst_port), + tuple->ip_proto, tuple->security_zone); +}
\ No newline at end of file diff --git a/src/tuple/tuple.h b/src/tuple/tuple.h new file mode 100644 index 0000000..3638070 --- /dev/null +++ b/src/tuple/tuple.h @@ -0,0 +1,89 @@ +#ifndef _TUPLE_H +#define _TUPLE_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include <arpa/inet.h> + +enum ip_type +{ + IP_TYPE_V4, + IP_TYPE_V6, +}; + +union ip_address +{ + struct in_addr v4; /* network order */ + struct in6_addr v6; /* network order */ +}; + +struct tuple2 +{ + enum ip_type ip_type; + union ip_address src_addr; /* network order */ + union ip_address dst_addr; /* network order */ +}; + +struct tuple4 +{ + enum ip_type ip_type; + union ip_address src_addr; /* network order */ + union ip_address dst_addr; /* network order */ + + in_port_t src_port; /* network order */ + in_port_t dst_port; /* network order */ +}; + +struct tuple5 +{ + enum ip_type ip_type; + union ip_address src_addr; /* network order */ + union ip_address dst_addr; /* network order */ + + in_port_t src_port; /* network order */ + in_port_t dst_port; /* network order */ + + uint16_t ip_proto; /* network order */ +}; + +struct tuple6 +{ + enum ip_type ip_type; + union ip_address src_addr; /* network order */ + union ip_address dst_addr; /* network order */ + + uint16_t src_port; /* network order */ + uint16_t dst_port; /* network order */ + + uint16_t ip_proto; /* network order */ + uint64_t security_zone; +}; + +uint32_t tuple2_hash(const struct tuple2 *tuple); +uint32_t tuple4_hash(const struct tuple4 *tuple); +uint32_t tuple5_hash(const struct tuple5 *tuple); +uint32_t tuple6_hash(const struct tuple6 *tuple); + +int tuple2_cmp(const struct tuple2 *tuple_a, const struct tuple2 *tuple_b); +int tuple4_cmp(const struct tuple4 *tuple_a, const struct tuple4 *tuple_b); +int tuple5_cmp(const struct tuple5 *tuple_a, const struct tuple5 *tuple_b); +int tuple6_cmp(const struct tuple6 *tuple_a, const struct tuple6 *tuple_b); + +void tuple2_reverse(const struct tuple2 *in, struct tuple2 *out); +void tuple4_reverse(const struct tuple4 *in, struct tuple4 *out); +void tuple5_reverse(const struct tuple5 *in, struct tuple5 *out); +void tuple6_reverse(const struct tuple6 *in, struct tuple6 *out); + +void tuple2_tostring(const struct tuple2 *tuple, char *buf, uint32_t size); +void tuple4_tostring(const struct tuple4 *tuple, char *buf, uint32_t size); +void tuple5_tostring(const struct tuple5 *tuple, char *buf, uint32_t size); +void tuple6_tostring(const struct tuple6 *tuple, char *buf, uint32_t size); + +#ifdef __cpluscplus +} +#endif + +#endif |
