update IPv6 utils

This commit is contained in:
luwenpeng
2024-02-21 15:06:48 +08:00
parent bd3735d3c4
commit 5e5ac458f2
8 changed files with 118 additions and 20 deletions

View File

@@ -6,7 +6,7 @@
#include "dablooms.h" #include "dablooms.h"
#include "udp_utils.h" #include "udp_utils.h"
#include "ipv4_helpers.h" #include "ipv4_helpers.h"
#include "ipv6_helpers.h" #include "ipv6_utils.h"
#include "eviction_filter.h" #include "eviction_filter.h"
struct eviction_filter_key struct eviction_filter_key
@@ -66,14 +66,14 @@ static inline int packet_get_eviction_filter_key(const struct packet *packet, st
if (l3_layer->type == LAYER_TYPE_IPV4) if (l3_layer->type == LAYER_TYPE_IPV4)
{ {
const struct ip *iphdr = (const struct ip *)l3_layer->hdr_ptr; const struct ip *iphdr = (const struct ip *)l3_layer->hdr_ptr;
key->src_addr.v4 = ipv4_hdr_get_net_order_saddr(iphdr); key->src_addr.v4 = ipv4_hdr_get_src_in_addr(iphdr);
key->dst_addr.v4 = ipv4_hdr_get_net_order_daddr(iphdr); key->dst_addr.v4 = ipv4_hdr_get_dst_in_addr(iphdr);
} }
if (l3_layer->type == LAYER_TYPE_IPV6) if (l3_layer->type == LAYER_TYPE_IPV6)
{ {
const struct ip6_hdr *iphdr = (const struct ip6_hdr *)l3_layer->hdr_ptr; const struct ip6_hdr *iphdr = (const struct ip6_hdr *)l3_layer->hdr_ptr;
key->src_addr.v6 = ipv6_hdr_get_net_order_saddr(iphdr); key->src_addr.v6 = ipv6_hdr_get_src_in6_addr(iphdr);
key->dst_addr.v6 = ipv6_hdr_get_net_order_daddr(iphdr); key->dst_addr.v6 = ipv6_hdr_get_dst_in6_addr(iphdr);
} }
const struct udphdr *udphdr = (const struct udphdr *)l4_layer->hdr_ptr; const struct udphdr *udphdr = (const struct udphdr *)l4_layer->hdr_ptr;

View File

@@ -31,7 +31,12 @@ struct log_context
int log_file_reopen_day; int log_file_reopen_day;
}; };
struct log_context g_log_context; struct log_context g_log_context = {
.config = {.output = LOG_OUTPUT_STDERR, .level = LOG_DEBUG},
.log_fd = -1,
.log_file_reopen_day = 0,
};
struct log_context *g_log_ctx = &g_log_context; struct log_context *g_log_ctx = &g_log_context;
static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

View File

@@ -109,6 +109,16 @@ static inline uint32_t ipv4_hdr_get_dst_addr(const struct ip *hdr)
return ntohl(hdr->ip_dst.s_addr); return ntohl(hdr->ip_dst.s_addr);
} }
static inline struct in_addr ipv4_hdr_get_src_in_addr(const struct ip *hdr)
{
return hdr->ip_src;
}
static inline struct in_addr ipv4_hdr_get_dst_in_addr(const struct ip *hdr)
{
return hdr->ip_dst;
}
static inline uint8_t ipv4_hdr_get_opt_len(const struct ip *hdr) static inline uint8_t ipv4_hdr_get_opt_len(const struct ip *hdr)
{ {
return ipv4_hdr_get_hdr_len(hdr) - sizeof(struct ip); return ipv4_hdr_get_hdr_len(hdr) - sizeof(struct ip);
@@ -219,6 +229,16 @@ static inline void ipv4_hdr_set_dst_addr(struct ip *hdr, uint32_t daddr)
hdr->ip_dst.s_addr = htonl(daddr); hdr->ip_dst.s_addr = htonl(daddr);
} }
static inline void ipv4_hdr_set_src_in_addr(struct ip *hdr, struct in_addr saddr)
{
hdr->ip_src = saddr;
}
static inline void ipv4_hdr_set_dst_in_addr(struct ip *hdr, struct in_addr daddr)
{
hdr->ip_dst = daddr;
}
static inline void ipv4_hdr_set_opt_len(struct ip *hdr, uint8_t opt_len) static inline void ipv4_hdr_set_opt_len(struct ip *hdr, uint8_t opt_len)
{ {
ipv4_hdr_set_hdr_len(hdr, opt_len + sizeof(struct ip)); ipv4_hdr_set_hdr_len(hdr, opt_len + sizeof(struct ip));

View File

@@ -1,5 +1,5 @@
#ifndef _IPV6_HELPERS_H #ifndef _IPV6_UTILS_H
#define _IPV6_HELPERS_H #define _IPV6_UTILS_H
#ifdef __cpluscplus #ifdef __cpluscplus
extern "C" extern "C"
@@ -37,6 +37,10 @@ extern "C"
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/ */
/******************************************************************************
* get
******************************************************************************/
static inline uint8_t ipv6_hdr_get_version(const struct ip6_hdr *hdr) static inline uint8_t ipv6_hdr_get_version(const struct ip6_hdr *hdr)
{ {
return (ntohl(hdr->ip6_flow) & 0xf0000000) >> 28; return (ntohl(hdr->ip6_flow) & 0xf0000000) >> 28;
@@ -67,18 +71,64 @@ static inline uint8_t ipv6_hdr_get_hop_limit(const struct ip6_hdr *hdr)
return hdr->ip6_hlim; return hdr->ip6_hlim;
} }
static inline struct in6_addr ipv6_hdr_get_net_order_saddr(const struct ip6_hdr *hdr) static inline struct in6_addr ipv6_hdr_get_src_in6_addr(const struct ip6_hdr *hdr)
{ {
return hdr->ip6_src; return hdr->ip6_src;
} }
static inline struct in6_addr ipv6_hdr_get_net_order_daddr(const struct ip6_hdr *hdr) static inline struct in6_addr ipv6_hdr_get_dst_in6_addr(const struct ip6_hdr *hdr)
{ {
return hdr->ip6_dst; return hdr->ip6_dst;
} }
// TODO IPv6 extension headers // TODO IPv6 extension headers
/******************************************************************************
* set
******************************************************************************/
static inline void ipv6_hdr_set_version(struct ip6_hdr *hdr, uint8_t version)
{
hdr->ip6_flow = htonl((ntohl(hdr->ip6_flow) & 0x0fffffff) | (version << 28));
}
static inline void ipv6_hdr_set_traffic_class(struct ip6_hdr *hdr, uint8_t traffic_class)
{
hdr->ip6_flow = htonl((ntohl(hdr->ip6_flow) & 0xf00fffff) | (traffic_class << 20));
}
static inline void ipv6_hdr_set_flow_label(struct ip6_hdr *hdr, uint32_t flow_label)
{
hdr->ip6_flow = htonl((ntohl(hdr->ip6_flow) & 0xfff00000) | flow_label);
}
static inline void ipv6_hdr_set_payload_len(struct ip6_hdr *hdr, uint16_t payload_len)
{
hdr->ip6_plen = htons(payload_len);
}
static inline void ipv6_hdr_set_next_header(struct ip6_hdr *hdr, uint8_t next_header)
{
hdr->ip6_nxt = next_header;
}
static inline void ipv6_hdr_set_hop_limit(struct ip6_hdr *hdr, uint8_t hop_limit)
{
hdr->ip6_hlim = hop_limit;
}
static inline void ipv6_hdr_set_src_in6_addr(struct ip6_hdr *hdr, struct in6_addr src_addr)
{
hdr->ip6_src = src_addr;
}
static inline void ipv6_hdr_set_dst_in6_addr(struct ip6_hdr *hdr, struct in6_addr dst_addr)
{
hdr->ip6_dst = dst_addr;
}
// TODO IPv6 extension headers
#ifdef __cpluscplus #ifdef __cpluscplus
} }
#endif #endif

View File

@@ -13,6 +13,7 @@
#include "udp_utils.h" #include "udp_utils.h"
#include "tcp_utils.h" #include "tcp_utils.h"
#include "ipv4_utils.h" #include "ipv4_utils.h"
#include "ipv6_utils.h"
#define likely(expr) __builtin_expect((expr), 1) #define likely(expr) __builtin_expect((expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0) #define unlikely(expr) __builtin_expect((expr), 0)
@@ -889,7 +890,7 @@ static inline const char *parse_ipv6(struct packet *handler, const char *data, u
{ {
return data; return data;
} }
uint8_t next_proto = ((struct ip6_hdr *)data)->ip6_nxt; uint8_t next_proto = ipv6_hdr_get_next_header((const struct ip6_hdr *)data);
SET_LAYER(handler, layer, LAYER_TYPE_IPV6, sizeof(struct ip6_hdr), data, len); SET_LAYER(handler, layer, LAYER_TYPE_IPV6, sizeof(struct ip6_hdr), data, len);
// TODO ipv6 fragment // TODO ipv6 fragment

View File

@@ -14,8 +14,8 @@ target_link_libraries(gtest_tcp_utils packet gtest)
add_executable(gtest_ipv4_utils gtest_ipv4_utils.cpp) add_executable(gtest_ipv4_utils gtest_ipv4_utils.cpp)
target_link_libraries(gtest_ipv4_utils packet gtest) target_link_libraries(gtest_ipv4_utils packet gtest)
add_executable(gtest_ipv6_helpers gtest_ipv6_helpers.cpp) add_executable(gtest_ipv6_utils gtest_ipv6_utils.cpp)
target_link_libraries(gtest_ipv6_helpers packet gtest) target_link_libraries(gtest_ipv6_utils packet gtest)
add_executable(gtest_packet_helpers gtest_packet_helpers.cpp) add_executable(gtest_packet_helpers gtest_packet_helpers.cpp)
target_link_libraries(gtest_packet_helpers packet gtest) target_link_libraries(gtest_packet_helpers packet gtest)
@@ -25,5 +25,5 @@ gtest_discover_tests(gtest_packet)
gtest_discover_tests(gtest_udp_utils) gtest_discover_tests(gtest_udp_utils)
gtest_discover_tests(gtest_tcp_utils) gtest_discover_tests(gtest_tcp_utils)
gtest_discover_tests(gtest_ipv4_utils) gtest_discover_tests(gtest_ipv4_utils)
gtest_discover_tests(gtest_ipv6_helpers) gtest_discover_tests(gtest_ipv6_utils)
gtest_discover_tests(gtest_packet_helpers) gtest_discover_tests(gtest_packet_helpers)

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "ipv6_helpers.h" #include "ipv6_utils.h"
/* /*
* Internet Protocol Version 6, Src: fe80::250:56ff:fe69:dc00, Dst: ff02::1:2 * Internet Protocol Version 6, Src: fe80::250:56ff:fe69:dc00, Dst: ff02::1:2
@@ -21,7 +21,7 @@ unsigned char data[] = {
0x60, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x11, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x50, 0x56, 0xff, 0xfe, 0x69, 0xdc, 0x00, 0xff, 0x02, 0x60, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x11, 0x01, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x50, 0x56, 0xff, 0xfe, 0x69, 0xdc, 0x00, 0xff, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02}; 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02};
TEST(IPV6_HELPERS, TEST) TEST(IPV6_UTILS, GET)
{ {
char src_str[INET6_ADDRSTRLEN]; char src_str[INET6_ADDRSTRLEN];
char dst_str[INET6_ADDRSTRLEN]; char dst_str[INET6_ADDRSTRLEN];
@@ -32,14 +32,36 @@ TEST(IPV6_HELPERS, TEST)
EXPECT_TRUE(ipv6_hdr_get_payload_len(hdr) == 60); EXPECT_TRUE(ipv6_hdr_get_payload_len(hdr) == 60);
EXPECT_TRUE(ipv6_hdr_get_next_header(hdr) == 17); EXPECT_TRUE(ipv6_hdr_get_next_header(hdr) == 17);
EXPECT_TRUE(ipv6_hdr_get_hop_limit(hdr) == 1); EXPECT_TRUE(ipv6_hdr_get_hop_limit(hdr) == 1);
struct in6_addr src_addr = ipv6_hdr_get_net_order_saddr(hdr); struct in6_addr src_addr = ipv6_hdr_get_src_in6_addr(hdr);
struct in6_addr dst_addr = ipv6_hdr_get_net_order_daddr(hdr); struct in6_addr dst_addr = ipv6_hdr_get_dst_in6_addr(hdr);
inet_ntop(AF_INET6, &src_addr, src_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &src_addr, src_str, INET6_ADDRSTRLEN);
inet_ntop(AF_INET6, &dst_addr, dst_str, INET6_ADDRSTRLEN); inet_ntop(AF_INET6, &dst_addr, dst_str, INET6_ADDRSTRLEN);
EXPECT_TRUE(strcmp(src_str, "fe80::250:56ff:fe69:dc00") == 0); EXPECT_TRUE(strcmp(src_str, "fe80::250:56ff:fe69:dc00") == 0);
EXPECT_TRUE(strcmp(dst_str, "ff02::1:2") == 0); EXPECT_TRUE(strcmp(dst_str, "ff02::1:2") == 0);
} }
TEST(IPV6_UTILS, SET)
{
char buff[40] = {0};
struct in6_addr src_addr;
struct in6_addr dst_addr;
struct ip6_hdr *hdr = (struct ip6_hdr *)buff;
inet_pton(AF_INET6, "fe80::250:56ff:fe69:dc00", &src_addr);
inet_pton(AF_INET6, "ff02::1:2", &dst_addr);
ipv6_hdr_set_version(hdr, 6);
ipv6_hdr_set_traffic_class(hdr, 0);
ipv6_hdr_set_flow_label(hdr, 0);
ipv6_hdr_set_payload_len(hdr, 60);
ipv6_hdr_set_next_header(hdr, 17);
ipv6_hdr_set_hop_limit(hdr, 1);
ipv6_hdr_set_src_in6_addr(hdr, src_addr);
ipv6_hdr_set_dst_in6_addr(hdr, dst_addr);
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);

View File

@@ -11,8 +11,8 @@ extern "C"
#include "session_private.h" #include "session_private.h"
#include "timestamp.h" #include "timestamp.h"
#include "session_manager.h" #include "session_manager.h"
#include "ipv4_helpers.h" #include "tcp_utils.h"
#include "tcp_helpers.h" #include "ipv4_utils.h"
#include "test_packets.h" #include "test_packets.h"
struct session_manager_config config = { struct session_manager_config config = {