🦄 refactor(directory structure): restructure and rename src to infra
This commit is contained in:
7
infra/tuple/CMakeLists.txt
Normal file
7
infra/tuple/CMakeLists.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
add_library(tuple tuple.cpp)
|
||||
target_include_directories(tuple PUBLIC ${CMAKE_CURRENT_LIST_DIR})
|
||||
target_include_directories(tuple PUBLIC ${CMAKE_SOURCE_DIR}/deps/crc32)
|
||||
target_include_directories(tuple PUBLIC ${CMAKE_SOURCE_DIR}/include)
|
||||
target_link_libraries(tuple)
|
||||
|
||||
add_subdirectory(test)
|
||||
5
infra/tuple/test/CMakeLists.txt
Normal file
5
infra/tuple/test/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
add_executable(gtest_tuple gtest_tuple.cpp)
|
||||
target_link_libraries(gtest_tuple tuple gtest)
|
||||
|
||||
include(GoogleTest)
|
||||
gtest_discover_tests(gtest_tuple)
|
||||
263
infra/tuple/test/gtest_tuple.cpp
Normal file
263
infra/tuple/test/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.addr_family = AF_INET;
|
||||
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.addr_family = AF_INET6;
|
||||
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);
|
||||
|
||||
// to_str
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple2_to_str(&tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.2-192.168.1.3");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple2_to_str(&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_to_str(&reversed_tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.3-192.168.1.2");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple2_to_str(&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.addr_family = AF_INET;
|
||||
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.addr_family = AF_INET6;
|
||||
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);
|
||||
|
||||
// to_str
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple4_to_str(&tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.2:1234-192.168.1.3:5678");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple4_to_str(&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_to_str(&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_to_str(&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.addr_family = AF_INET;
|
||||
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.addr_family = AF_INET6;
|
||||
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;
|
||||
|
||||
// to_str
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple5_to_str(&tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.2:1234-192.168.1.3:5678-6");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple5_to_str(&tuple_b, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234-2001:db8::ff00:42:832a:5678-17");
|
||||
|
||||
// reverse
|
||||
tuple5_reverse(&tuple_a, &reversed_tuple_a);
|
||||
tuple5_reverse(&tuple_b, &reversed_tuple_b);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple5_to_str(&reversed_tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.3:5678-192.168.1.2:1234-6");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple5_to_str(&reversed_tuple_b, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678-2001:db8::ff00:42:8329:1234-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.addr_family = AF_INET;
|
||||
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.domain = 0;
|
||||
|
||||
tuple_b.addr_family = AF_INET6;
|
||||
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.domain = 0;
|
||||
|
||||
// to_str
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple6_to_str(&tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.2:1234-192.168.1.3:5678-6-0");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple6_to_str(&tuple_b, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "2001:db8::ff00:42:8329:1234-2001:db8::ff00:42:832a:5678-17-0");
|
||||
|
||||
// reverse
|
||||
tuple6_reverse(&tuple_a, &reversed_tuple_a);
|
||||
tuple6_reverse(&tuple_b, &reversed_tuple_b);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple6_to_str(&reversed_tuple_a, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "192.168.1.3:5678-192.168.1.2:1234-6-0");
|
||||
memset(buf, 0, sizeof(buf));
|
||||
tuple6_to_str(&reversed_tuple_b, buf, sizeof(buf));
|
||||
EXPECT_STREQ(buf, "2001:db8::ff00:42:832a:5678-2001:db8::ff00:42:8329:1234-17-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();
|
||||
}
|
||||
424
infra/tuple/tuple.cpp
Normal file
424
infra/tuple/tuple.cpp
Normal file
@@ -0,0 +1,424 @@
|
||||
#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->addr_family, sizeof(tuple->addr_family), 0);
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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->addr_family, sizeof(tuple->addr_family), 0);
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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->addr_family, sizeof(tuple->addr_family), 0);
|
||||
hash = crc32_hash(&tuple->ip_proto, sizeof(tuple->ip_proto), hash);
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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->addr_family, sizeof(tuple->addr_family), 0);
|
||||
hash = crc32_hash(&tuple->ip_proto, sizeof(tuple->ip_proto), hash);
|
||||
hash = crc32_hash(&tuple->domain, sizeof(tuple->domain), hash);
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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->addr_family != tuple_b->addr_family)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tuple_a->addr_family == AF_INET)
|
||||
{
|
||||
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->addr_family != tuple_b->addr_family)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tuple_a->addr_family == AF_INET)
|
||||
{
|
||||
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->addr_family != tuple_b->addr_family)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tuple_a->addr_family == AF_INET)
|
||||
{
|
||||
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->domain != tuple_b->domain)
|
||||
{
|
||||
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->addr_family != tuple_b->addr_family)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tuple_a->addr_family == AF_INET)
|
||||
{
|
||||
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)
|
||||
{
|
||||
memset(out, 0, sizeof(struct tuple2));
|
||||
out->addr_family = in->addr_family;
|
||||
|
||||
memcpy(&out->src_addr, &in->dst_addr, sizeof(in->dst_addr));
|
||||
memcpy(&out->dst_addr, &in->src_addr, sizeof(in->src_addr));
|
||||
}
|
||||
|
||||
void tuple4_reverse(const struct tuple4 *in, struct tuple4 *out)
|
||||
{
|
||||
memset(out, 0, sizeof(struct tuple4));
|
||||
out->addr_family = in->addr_family;
|
||||
out->src_port = in->dst_port;
|
||||
out->dst_port = in->src_port;
|
||||
|
||||
memcpy(&out->src_addr, &in->dst_addr, sizeof(in->dst_addr));
|
||||
memcpy(&out->dst_addr, &in->src_addr, sizeof(in->src_addr));
|
||||
}
|
||||
|
||||
void tuple5_reverse(const struct tuple5 *in, struct tuple5 *out)
|
||||
{
|
||||
memset(out, 0, sizeof(struct tuple5));
|
||||
out->addr_family = in->addr_family;
|
||||
out->ip_proto = in->ip_proto;
|
||||
out->src_port = in->dst_port;
|
||||
out->dst_port = in->src_port;
|
||||
|
||||
memcpy(&out->src_addr, &in->dst_addr, sizeof(in->dst_addr));
|
||||
memcpy(&out->dst_addr, &in->src_addr, sizeof(in->src_addr));
|
||||
}
|
||||
|
||||
void tuple6_reverse(const struct tuple6 *in, struct tuple6 *out)
|
||||
{
|
||||
memset(out, 0, sizeof(struct tuple6));
|
||||
out->addr_family = in->addr_family;
|
||||
out->ip_proto = in->ip_proto;
|
||||
out->domain = in->domain;
|
||||
out->src_port = in->dst_port;
|
||||
out->dst_port = in->src_port;
|
||||
|
||||
memcpy(&out->src_addr, &in->dst_addr, sizeof(in->dst_addr));
|
||||
memcpy(&out->dst_addr, &in->src_addr, sizeof(in->src_addr));
|
||||
}
|
||||
|
||||
void tuple2_to_str(const struct tuple2 *tuple, char *buf, uint32_t size)
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN] = {0};
|
||||
char dst_addr[INET6_ADDRSTRLEN] = {0};
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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_to_str(const struct tuple4 *tuple, char *buf, uint32_t size)
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN] = {0};
|
||||
char dst_addr[INET6_ADDRSTRLEN] = {0};
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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_to_str(const struct tuple5 *tuple, char *buf, uint32_t size)
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN] = {0};
|
||||
char dst_addr[INET6_ADDRSTRLEN] = {0};
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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-%u",
|
||||
src_addr, ntohs(tuple->src_port),
|
||||
dst_addr, ntohs(tuple->dst_port),
|
||||
tuple->ip_proto);
|
||||
}
|
||||
|
||||
void tuple6_to_str(const struct tuple6 *tuple, char *buf, uint32_t size)
|
||||
{
|
||||
char src_addr[INET6_ADDRSTRLEN] = {0};
|
||||
char dst_addr[INET6_ADDRSTRLEN] = {0};
|
||||
|
||||
if (tuple->addr_family == AF_INET)
|
||||
{
|
||||
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-%u-%lu",
|
||||
src_addr, ntohs(tuple->src_port),
|
||||
dst_addr, ntohs(tuple->dst_port),
|
||||
tuple->ip_proto, tuple->domain);
|
||||
}
|
||||
80
infra/tuple/tuple.h
Normal file
80
infra/tuple/tuple.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
#include <arpa/inet.h>
|
||||
|
||||
union ip_address
|
||||
{
|
||||
struct in_addr v4; /* network order */
|
||||
struct in6_addr v6; /* network order */
|
||||
};
|
||||
|
||||
struct tuple2
|
||||
{
|
||||
uint32_t addr_family;
|
||||
union ip_address src_addr; /* network order */
|
||||
union ip_address dst_addr; /* network order */
|
||||
};
|
||||
|
||||
struct tuple4
|
||||
{
|
||||
uint32_t addr_family;
|
||||
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
|
||||
{
|
||||
uint32_t addr_family;
|
||||
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
|
||||
{
|
||||
uint32_t addr_family;
|
||||
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 domain;
|
||||
};
|
||||
|
||||
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_to_str(const struct tuple2 *tuple, char *buf, uint32_t size);
|
||||
void tuple4_to_str(const struct tuple4 *tuple, char *buf, uint32_t size);
|
||||
void tuple5_to_str(const struct tuple5 *tuple, char *buf, uint32_t size);
|
||||
void tuple6_to_str(const struct tuple6 *tuple, char *buf, uint32_t size);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user