Add tuple2 & tuple4 & tuple5 & tuple6
This commit is contained in:
19
src/tuple/CMakeLists.txt
Normal file
19
src/tuple/CMakeLists.txt
Normal file
@@ -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)
|
||||
263
src/tuple/gtest_tuple.cpp
Normal file
263
src/tuple/gtest_tuple.cpp
Normal file
@@ -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();
|
||||
}
|
||||
452
src/tuple/tuple.cpp
Normal file
452
src/tuple/tuple.cpp
Normal file
@@ -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);
|
||||
}
|
||||
89
src/tuple/tuple.h
Normal file
89
src/tuple/tuple.h
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user