update IPv4 utils

This commit is contained in:
luwenpeng
2024-02-21 14:34:34 +08:00
parent 03f428681e
commit bd3735d3c4
11 changed files with 543 additions and 235 deletions

View File

@@ -17,8 +17,8 @@ struct dupkt_filter_key
// IPv4
uint16_t ip_id;
struct in_addr src_addr; /* network order */
struct in_addr dst_addr; /* network order */
uint32_t src_addr; /* host order */
uint32_t dst_addr; /* host order */
} __attribute__((__packed__));
struct dupkt_filter
@@ -53,9 +53,9 @@ static inline int packet_get_dupkt_filter_key(const struct packet *packet, struc
memset(key, 0, sizeof(struct dupkt_filter_key));
const struct ip *iphdr = (const struct ip *)ipv4_layer->hdr_ptr;
key->ip_id = ipv4_hdr_get_id(iphdr);
key->src_addr = ipv4_hdr_get_net_order_saddr(iphdr);
key->dst_addr = ipv4_hdr_get_net_order_daddr(iphdr);
key->ip_id = ipv4_hdr_get_ipid(iphdr);
key->src_addr = ipv4_hdr_get_src_addr(iphdr);
key->dst_addr = ipv4_hdr_get_dst_addr(iphdr);
const struct tcphdr *tcphdr = (const struct tcphdr *)tcp_layer->hdr_ptr;
key->seq = tcp_hdr_get_seq(tcphdr);

View File

@@ -1,146 +0,0 @@
#ifndef _IPV4_HELPERS_H
#define _IPV4_HELPERS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <arpa/inet.h>
#include <netinet/ip.h>
/*
* Internet Header Format
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Version| IHL |Type of Service| Total Length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Identification |Flags| Fragment Offset |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Time to Live | Protocol | Header Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Destination Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options | Padding |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
static inline uint8_t ipv4_hdr_get_version(const struct ip *hdr)
{
return hdr->ip_v;
}
static inline uint8_t ipv4_hdr_get_hl(const struct ip *hdr)
{
return hdr->ip_hl << 2;
}
static inline uint8_t ipv4_hdr_get_tos(const struct ip *hdr)
{
return hdr->ip_tos;
}
static inline uint16_t ipv4_hdr_get_len(const struct ip *hdr)
{
return ntohs(hdr->ip_len);
}
static inline uint16_t ipv4_hdr_get_ipid(const struct ip *hdr)
{
return ntohs(hdr->ip_id);
}
static inline uint8_t ipv4_hdr_get_flags(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & (~IP_OFFMASK)) >> 13;
}
static inline bool ipv4_hdr_has_flag_rf(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_RF) != 0;
}
static inline bool ipv4_hdr_has_flag_df(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_DF) != 0;
}
static inline bool ipv4_hdr_has_flag_mf(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_MF) != 0;
}
static inline uint16_t ipv4_hdr_get_frag_offset(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_OFFMASK) << 3;
}
static inline uint8_t ipv4_hdr_get_ttl(const struct ip *hdr)
{
return hdr->ip_ttl;
}
static inline uint8_t ipv4_hdr_get_protocol(const struct ip *hdr)
{
return hdr->ip_p;
}
static inline uint16_t ipv4_hdr_get_checksum(const struct ip *hdr)
{
return ntohs(hdr->ip_sum);
}
static inline uint32_t ipv4_hdr_get_host_order_saddr(const struct ip *hdr)
{
return ntohl(hdr->ip_src.s_addr);
}
static inline uint32_t ipv4_hdr_get_host_order_daddr(const struct ip *hdr)
{
return ntohl(hdr->ip_dst.s_addr);
}
static inline struct in_addr ipv4_hdr_get_net_order_saddr(const struct ip *hdr)
{
return hdr->ip_src;
}
static inline struct in_addr ipv4_hdr_get_net_order_daddr(const struct ip *hdr)
{
return hdr->ip_dst;
}
static inline uint8_t ipv4_hdr_get_opt_len(const struct ip *hdr)
{
return ipv4_hdr_get_hl(hdr) - sizeof(struct ip);
}
static inline const uint8_t *ipv4_hdr_get_opt_ptr(const struct ip *hdr)
{
return (const uint8_t *)hdr + sizeof(struct ip);
}
static inline void ipv4_hdr_set_ipid(struct ip *hdr, uint16_t ipid)
{
hdr->ip_id = htons(ipid);
}
static inline void ipv4_hdr_set_host_order_saddr(struct ip *hdr, uint32_t saddr)
{
hdr->ip_src.s_addr = htonl(saddr);
}
static inline void ipv4_hdr_set_host_order_daddr(struct ip *hdr, uint32_t daddr)
{
hdr->ip_dst.s_addr = htonl(daddr);
}
#ifdef __cpluscplus
}
#endif
#endif

237
src/packet/ipv4_utils.h Normal file
View File

@@ -0,0 +1,237 @@
#ifndef _IPV4_UTILS_H
#define _IPV4_UTILS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <string.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
/*
* Internet Header Format
*
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* |Version| IHL |Type of Service| Total Length |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Identification |Flags| Fragment Offset |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Time to Live | Protocol | Header Checksum |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Destination Address |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options | Padding |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/******************************************************************************
* get
******************************************************************************/
static inline uint8_t ipv4_hdr_get_version(const struct ip *hdr)
{
return hdr->ip_v;
}
static inline uint8_t ipv4_hdr_get_hdr_len(const struct ip *hdr)
{
return hdr->ip_hl << 2;
}
static inline uint8_t ipv4_hdr_get_tos(const struct ip *hdr)
{
return hdr->ip_tos;
}
static inline uint16_t ipv4_hdr_get_total_len(const struct ip *hdr)
{
return ntohs(hdr->ip_len);
}
static inline uint16_t ipv4_hdr_get_ipid(const struct ip *hdr)
{
return ntohs(hdr->ip_id);
}
static inline uint8_t ipv4_hdr_get_flags(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & (~IP_OFFMASK)) >> 13;
}
static inline bool ipv4_hdr_get_rf_flag(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_RF) != 0;
}
static inline bool ipv4_hdr_get_df_flag(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_DF) != 0;
}
static inline bool ipv4_hdr_get_mf_flag(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_MF) != 0;
}
static inline uint16_t ipv4_hdr_get_frag_offset(const struct ip *hdr)
{
return (ntohs(hdr->ip_off) & IP_OFFMASK) << 3;
}
static inline uint8_t ipv4_hdr_get_ttl(const struct ip *hdr)
{
return hdr->ip_ttl;
}
static inline uint8_t ipv4_hdr_get_proto(const struct ip *hdr)
{
return hdr->ip_p;
}
static inline uint16_t ipv4_hdr_get_checksum(const struct ip *hdr)
{
return ntohs(hdr->ip_sum);
}
static inline uint32_t ipv4_hdr_get_src_addr(const struct ip *hdr)
{
return ntohl(hdr->ip_src.s_addr);
}
static inline uint32_t ipv4_hdr_get_dst_addr(const struct ip *hdr)
{
return ntohl(hdr->ip_dst.s_addr);
}
static inline uint8_t ipv4_hdr_get_opt_len(const struct ip *hdr)
{
return ipv4_hdr_get_hdr_len(hdr) - sizeof(struct ip);
}
static inline const char *ipv4_hdr_get_opt_data(const struct ip *hdr)
{
return (const char *)hdr + sizeof(struct ip);
}
/******************************************************************************
* set
******************************************************************************/
static inline void ipv4_hdr_set_version(struct ip *hdr, uint8_t version)
{
hdr->ip_v = version;
}
static inline void ipv4_hdr_set_hdr_len(struct ip *hdr, uint8_t hdr_len)
{
hdr->ip_hl = hdr_len >> 2;
}
static inline void ipv4_hdr_set_tos(struct ip *hdr, uint8_t tos)
{
hdr->ip_tos = tos;
}
static inline void ipv4_hdr_set_total_len(struct ip *hdr, uint16_t total_len)
{
hdr->ip_len = htons(total_len);
}
static inline void ipv4_hdr_set_ipid(struct ip *hdr, uint16_t ipid)
{
hdr->ip_id = htons(ipid);
}
static inline void ipv4_hdr_set_flags(struct ip *hdr, uint8_t flags)
{
hdr->ip_off = htons((flags << 13) | (ntohs(hdr->ip_off) & IP_OFFMASK));
}
static inline void ipv4_hdr_set_rf_flag(struct ip *hdr, bool flag)
{
if (flag)
{
hdr->ip_off = htons(ntohs(hdr->ip_off) | IP_RF);
}
else
{
hdr->ip_off = htons(ntohs(hdr->ip_off) & ~IP_RF);
}
}
static inline void ipv4_hdr_set_df_flag(struct ip *hdr, bool flag)
{
if (flag)
{
hdr->ip_off = htons(ntohs(hdr->ip_off) | IP_DF);
}
else
{
hdr->ip_off = htons(ntohs(hdr->ip_off) & ~IP_DF);
}
}
static inline void ipv4_hdr_set_mf_flag(struct ip *hdr, bool flag)
{
if (flag)
{
hdr->ip_off = htons(ntohs(hdr->ip_off) | IP_MF);
}
else
{
hdr->ip_off = htons(ntohs(hdr->ip_off) & ~IP_MF);
}
}
static inline void ipv4_hdr_set_frag_offset(struct ip *hdr, uint16_t frag_offset)
{
hdr->ip_off = htons((frag_offset >> 3) | (ntohs(hdr->ip_off) & ~IP_OFFMASK));
}
static inline void ipv4_hdr_set_ttl(struct ip *hdr, uint8_t ttl)
{
hdr->ip_ttl = ttl;
}
static inline void ipv4_hdr_set_protocol(struct ip *hdr, uint8_t protocol)
{
hdr->ip_p = protocol;
}
static inline void ipv4_hdr_set_checksum(struct ip *hdr, uint16_t checksum)
{
hdr->ip_sum = htons(checksum);
}
static inline void ipv4_hdr_set_src_addr(struct ip *hdr, uint32_t saddr)
{
hdr->ip_src.s_addr = htonl(saddr);
}
static inline void ipv4_hdr_set_dst_addr(struct ip *hdr, uint32_t daddr)
{
hdr->ip_dst.s_addr = htonl(daddr);
}
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));
}
// must be called after ipv4_hdr_set_opt_len
static inline void ipv4_hdr_set_opt_data(struct ip *hdr, const char *opt_data)
{
memcpy((char *)hdr + sizeof(struct ip), opt_data, ipv4_hdr_get_opt_len(hdr));
}
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -12,6 +12,7 @@
#include "packet.h"
#include "udp_utils.h"
#include "tcp_utils.h"
#include "ipv4_utils.h"
#define likely(expr) __builtin_expect((expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0)
@@ -859,8 +860,8 @@ static inline const char *parse_ipv4(struct packet *handler, const char *data, u
return data;
}
struct ip *hdr = (struct ip *)data;
uint8_t next_proto = hdr->ip_p;
uint16_t hdr_len = (hdr->ip_hl & 0xf) * 4u;
uint8_t next_proto = ipv4_get_proto(hdr);
uint16_t hdr_len = ipv4_hdr_get_hdr_len(hdr);
SET_LAYER(handler, layer, LAYER_TYPE_IPV4, hdr_len, data, len);
// ip fragmented

View File

@@ -11,8 +11,8 @@ target_link_libraries(gtest_udp_utils packet gtest)
add_executable(gtest_tcp_utils gtest_tcp_utils.cpp)
target_link_libraries(gtest_tcp_utils packet gtest)
add_executable(gtest_ipv4_helpers gtest_ipv4_helpers.cpp)
target_link_libraries(gtest_ipv4_helpers packet gtest)
add_executable(gtest_ipv4_utils gtest_ipv4_utils.cpp)
target_link_libraries(gtest_ipv4_utils packet gtest)
add_executable(gtest_ipv6_helpers gtest_ipv6_helpers.cpp)
target_link_libraries(gtest_ipv6_helpers packet gtest)
@@ -24,6 +24,6 @@ include(GoogleTest)
gtest_discover_tests(gtest_packet)
gtest_discover_tests(gtest_udp_utils)
gtest_discover_tests(gtest_tcp_utils)
gtest_discover_tests(gtest_ipv4_helpers)
gtest_discover_tests(gtest_ipv4_utils)
gtest_discover_tests(gtest_ipv6_helpers)
gtest_discover_tests(gtest_packet_helpers)

View File

@@ -1,58 +0,0 @@
#include <gtest/gtest.h>
#include "ipv4_helpers.h"
/*
* Internet Protocol Version 4, Src: 192.168.36.103, Dst: 192.168.40.137
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 44
* Identification: 0xffff (65535)
* 001. .... = Flags: 0x1, More fragments
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..1. .... = More fragments: Set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 127
* Protocol: TCP (6)
* Header Checksum: 0x4d8b [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x4d8b]
* Source Address: 192.168.36.103
* Destination Address: 192.168.40.137
* [Reassembled IPv4 in frame: 5]
* Data (24 bytes)
*/
unsigned char data[] = {0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x20, 0x00, 0x7f, 0x06, 0x4d, 0x8b, 0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89};
TEST(IPV4_HELPERS, TEST)
{
const struct ip *hdr = (struct ip *)data;
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ipv4_hdr_get_hl(hdr) == 20);
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_len(hdr) == 44);
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 65535);
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 1);
EXPECT_TRUE(ipv4_hdr_has_flag_rf(hdr) == false);
EXPECT_TRUE(ipv4_hdr_has_flag_df(hdr) == false);
EXPECT_TRUE(ipv4_hdr_has_flag_mf(hdr) == true);
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
EXPECT_TRUE(ipv4_hdr_get_protocol(hdr) == 6);
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x4d8b);
EXPECT_TRUE(ipv4_hdr_get_host_order_saddr(hdr) == 0xc0a82467);
EXPECT_TRUE(ipv4_hdr_get_host_order_daddr(hdr) == 0xc0a82889);
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_opt_ptr(hdr) == data + 20);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -0,0 +1,274 @@
#include <gtest/gtest.h>
#include "ipv4_utils.h"
/******************************************************************************
* more fragment
******************************************************************************/
/*
* Internet Protocol Version 4, Src: 192.168.36.103, Dst: 192.168.40.137
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 44
* Identification: 0xffff (65535)
* 001. .... = Flags: 0x1, More fragments
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..1. .... = More fragments: Set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 127
* Protocol: TCP (6)
* Header Checksum: 0x4d8b [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x4d8b]
* Source Address: 192.168.36.103
* Destination Address: 192.168.40.137
* [Reassembled IPv4 in frame: 5]
* Data (24 bytes)
*/
unsigned char data1[] = {0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x20, 0x00, 0x7f, 0x06, 0x4d, 0x8b, 0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89};
TEST(IPV4_UTILS, GET1)
{
const struct ip *hdr = (struct ip *)data1;
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ipv4_hdr_get_hdr_len(hdr) == 20);
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_total_len(hdr) == 44);
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 65535);
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 1);
EXPECT_TRUE(ipv4_hdr_get_rf_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_df_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_mf_flag(hdr) == true);
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
EXPECT_TRUE(ipv4_hdr_get_proto(hdr) == 6);
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x4d8b);
EXPECT_TRUE(ipv4_hdr_get_src_addr(hdr) == 0xc0a82467);
EXPECT_TRUE(ipv4_hdr_get_dst_addr(hdr) == 0xc0a82889);
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_opt_data(hdr) == data + 20);
}
TEST(IPV4_UTILS, SET1)
{
char buff[20] = {0};
struct ip *hdr = (struct ip *)buff;
ipv4_hdr_set_version(hdr, 4);
ipv4_hdr_set_hdr_len(hdr, 20);
ipv4_hdr_set_tos(hdr, 0);
ipv4_hdr_set_total_len(hdr, 44);
ipv4_hdr_set_ipid(hdr, 65535);
ipv4_hdr_set_frag_offset(hdr, 0);
ipv4_hdr_set_ttl(hdr, 127);
ipv4_hdr_set_protocol(hdr, 6);
ipv4_hdr_set_checksum(hdr, 0x4d8b);
ipv4_hdr_set_src_addr(hdr, 0xc0a82467);
ipv4_hdr_set_dst_addr(hdr, 0xc0a82889);
ipv4_hdr_set_opt_len(hdr, 0);
ipv4_hdr_set_opt_data(hdr, NULL);
ipv4_hdr_set_flags(hdr, 1);
EXPECT_TRUE(memcmp(buff, data1, 20) == 0);
ipv4_hdr_set_rf_flag(hdr, false);
ipv4_hdr_set_df_flag(hdr, false);
ipv4_hdr_set_mf_flag(hdr, true);
EXPECT_TRUE(memcmp(buff, data1, 20) == 0);
}
/******************************************************************************
* fragment offset
******************************************************************************/
/*
* Frame 5: 60 bytes on wire (480 bits), 60 bytes captured (480 bits)
* Ethernet II, Src: Fortinet_cc:87:22 (e8:1c:ba:cc:87:22), Dst: EvocInte_2f:35:b8 (00:22:46:2f:35:b8)
* Internet Protocol Version 4, Src: 192.168.36.103, Dst: 192.168.40.137
* 0100 .... = Version: 4
* .... 0101 = Header Length: 20 bytes (5)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 44
* Identification: 0xffff (65535)
* 000. .... = Flags: 0x0
* 0... .... = Reserved bit: Not set
* .0.. .... = Don't fragment: Not set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0011 = Fragment Offset: 24
* Time to Live: 127
* Protocol: TCP (6)
* Header Checksum: 0x6d88 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0x6d88]
* Source Address: 192.168.36.103
* Destination Address: 192.168.40.137
* [2 IPv4 Fragments (48 bytes): #4(24), #5(24)]
* [Frame: 4, payload: 0-23 (24 bytes)]
* [Frame: 5, payload: 24-47 (24 bytes)]
* [Fragment count: 2]
* [Reassembled IPv4 length: 48]
* [Reassembled IPv4 data: f4a5270f9107248703d518e75018ff005e9200003132330af4a5270f9107248b03d518e7…]
* Transmission Control Protocol, Src Port: 62629, Dst Port: 9999, Seq: 1, Ack: 1, Len: 28
* Data (28 bytes)
* Data: 3132330af4a5270f9107248b03d518e75018ff00301600006162630a
* [Length: 28]
*/
unsigned char data2[] = {0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x00, 0x03, 0x7f, 0x06, 0x6d, 0x88, 0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89};
TEST(IPV4_UTILS, GET2)
{
const struct ip *hdr = (struct ip *)data2;
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ipv4_hdr_get_hdr_len(hdr) == 20);
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_total_len(hdr) == 44);
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 65535);
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_rf_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_df_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_mf_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 24);
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 127);
EXPECT_TRUE(ipv4_hdr_get_proto(hdr) == 6);
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0x6d88);
EXPECT_TRUE(ipv4_hdr_get_src_addr(hdr) == 0xc0a82467);
EXPECT_TRUE(ipv4_hdr_get_dst_addr(hdr) == 0xc0a82889);
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_opt_data(hdr) == data + 20);
}
TEST(IPV4_UTILS, SET2)
{
char buff[20] = {0};
struct ip *hdr = (struct ip *)buff;
ipv4_hdr_set_version(hdr, 4);
ipv4_hdr_set_hdr_len(hdr, 20);
ipv4_hdr_set_tos(hdr, 0);
ipv4_hdr_set_total_len(hdr, 44);
ipv4_hdr_set_ipid(hdr, 65535);
ipv4_hdr_set_frag_offset(hdr, 24);
ipv4_hdr_set_ttl(hdr, 127);
ipv4_hdr_set_protocol(hdr, 6);
ipv4_hdr_set_checksum(hdr, 0x6d88);
ipv4_hdr_set_src_addr(hdr, 0xc0a82467);
ipv4_hdr_set_dst_addr(hdr, 0xc0a82889);
ipv4_hdr_set_opt_len(hdr, 0);
ipv4_hdr_set_opt_data(hdr, NULL);
ipv4_hdr_set_flags(hdr, 0);
EXPECT_TRUE(memcmp(buff, data2, 20) == 0);
ipv4_hdr_set_rf_flag(hdr, false);
ipv4_hdr_set_df_flag(hdr, false);
ipv4_hdr_set_mf_flag(hdr, false);
EXPECT_TRUE(memcmp(buff, data2, 20) == 0);
}
/******************************************************************************
* options
******************************************************************************/
/*
* Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
* 0100 .... = Version: 4
* .... 1111 = Header Length: 60 bytes (15)
* Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
* 0000 00.. = Differentiated Services Codepoint: Default (0)
* .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
* Total Length: 124
* Identification: 0x0000 (0)
* 010. .... = Flags: 0x2, Don't fragment
* 0... .... = Reserved bit: Not set
* .1.. .... = Don't fragment: Set
* ..0. .... = More fragments: Not set
* ...0 0000 0000 0000 = Fragment Offset: 0
* Time to Live: 64
* Protocol: ICMP (1)
* Header Checksum: 0xfd30 [correct]
* [Header checksum status: Good]
* [Calculated Checksum: 0xfd30]
* Source Address: 127.0.0.1
* Destination Address: 127.0.0.1
* Options: (40 bytes), Commercial Security
* IP Option - Commercial Security (40 bytes)
* Type: 134
* 1... .... = Copy on fragmentation: Yes
* .00. .... = Class: Control (0)
* ...0 0110 = Number: Commercial IP security option (6)
* Length: 40
* DOI: 1
* Tag Type: Restrictive Category Bitmap (1)
* Sensitivity Level: 1
* Categories: 0,2,4,5,6,239
*/
unsigned char data3[] = {
0x4f, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x40, 0x00, 0x40, 0x01, 0xfd, 0x30, 0x7f, 0x00, 0x00, 0x01, 0x7f, 0x00, 0x00, 0x01, 0x86, 0x28, 0x00, 0x00, 0x00, 0x01,
0x01, 0x22, 0x00, 0x01, 0xae, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
TEST(IPV4_UTILS, GET3)
{
const struct ip *hdr = (struct ip *)data3;
EXPECT_TRUE(ipv4_hdr_get_version(hdr) == 4);
EXPECT_TRUE(ipv4_hdr_get_hdr_len(hdr) == 60);
EXPECT_TRUE(ipv4_hdr_get_tos(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_total_len(hdr) == 124);
EXPECT_TRUE(ipv4_hdr_get_ipid(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_flags(hdr) == 2);
EXPECT_TRUE(ipv4_hdr_get_rf_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_df_flag(hdr) == true);
EXPECT_TRUE(ipv4_hdr_get_mf_flag(hdr) == false);
EXPECT_TRUE(ipv4_hdr_get_frag_offset(hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_ttl(hdr) == 64);
EXPECT_TRUE(ipv4_hdr_get_proto(hdr) == 1);
EXPECT_TRUE(ipv4_hdr_get_checksum(hdr) == 0xfd30);
EXPECT_TRUE(ipv4_hdr_get_src_addr(hdr) == 0x7f000001);
EXPECT_TRUE(ipv4_hdr_get_dst_addr(hdr) == 0x7f000001);
EXPECT_TRUE(ipv4_hdr_get_opt_len(hdr) == 40);
EXPECT_TRUE(ipv4_hdr_get_opt_data(hdr) == data + 20);
}
TEST(IPV4_UTILS, SET3)
{
char buff[60] = {0};
struct ip *hdr = (struct ip *)buff;
ipv4_hdr_set_version(hdr, 4);
ipv4_hdr_set_hdr_len(hdr, 60);
ipv4_hdr_set_tos(hdr, 0);
ipv4_hdr_set_total_len(hdr, 124);
ipv4_hdr_set_ipid(hdr, 0);
ipv4_hdr_set_frag_offset(hdr, 0);
ipv4_hdr_set_ttl(hdr, 64);
ipv4_hdr_set_protocol(hdr, 1);
ipv4_hdr_set_checksum(hdr, 0xfd30);
ipv4_hdr_set_src_addr(hdr, 0x7f000001);
ipv4_hdr_set_dst_addr(hdr, 0x7f000001);
ipv4_hdr_set_opt_len(hdr, 40);
ipv4_hdr_set_opt_data(hdr, data3 + 20);
ipv4_hdr_set_flags(hdr, 2);
EXPECT_TRUE(memcmp(buff, data3, 60) == 0);
ipv4_hdr_set_rf_flag(hdr, false);
ipv4_hdr_set_df_flag(hdr, true);
ipv4_hdr_set_mf_flag(hdr, false);
EXPECT_TRUE(memcmp(buff, data3, 60) == 0);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -36,8 +36,8 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SYN_DUP)
packet_parse(&pkt, (const char *)tcp_pkt_c2s_syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
const struct layer_record *ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);
@@ -88,8 +88,8 @@ TEST(TCP_DUPKT_FILTER_ENABLE, S2C_DUP)
packet_parse(&pkt, (const char *)tcp_pkt_s2c_synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack));
const struct layer_record *ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);
@@ -111,7 +111,7 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP_FILTER)
struct session *sess = NULL;
struct session_manager *mgr = NULL;
const struct layer_record *ipv4_layer;
const struct ip *hdr;
struct ip *hdr;
char tcp_pkt_c2s_syn_retransmission[1500] = {0};
timestamp_update();
@@ -134,8 +134,8 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP_FILTER)
packet_parse(&pkt, (const char *)tcp_pkt_c2s_syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);
@@ -148,8 +148,8 @@ TEST(TCP_DUPKT_FILTER_ENABLE, SKIP_FILTER)
packet_parse(&pkt, (const char *)tcp_pkt_c2s_syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1235);
hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1235);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);
@@ -211,8 +211,8 @@ TEST(TCP_DUPKT_FILTER_DISABLE, C2S_DUPKT)
packet_parse(&pkt, (const char *)tcp_pkt_c2s_syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
const struct layer_record *ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);
@@ -266,8 +266,8 @@ TEST(TCP_DUPKT_FILTER_DISABLE, S2C_DUP)
packet_parse(&pkt, (const char *)tcp_pkt_s2c_synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack));
const struct layer_record *ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);

View File

@@ -4,8 +4,8 @@ static void overwrite_ipv4_saddr(struct packet *pkt, uint32_t saddr)
{
const struct layer_record *ipv4_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_host_order_saddr((struct ip *)hdr, saddr);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_src_addr(hdr, saddr);
}
#if 1

View File

@@ -4,8 +4,8 @@ static void overwrite_ipv4_saddr(struct packet *pkt, uint32_t saddr)
{
const struct layer_record *ipv4_layer = packet_get_innermost_layer(pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_host_order_saddr((struct ip *)hdr, saddr);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_src_addr(hdr, saddr);
}
#if 1

View File

@@ -387,8 +387,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYN_RETRANSMISSION)
packet_parse(&pkt, (const char *)tcp_pkt_c2s_syn_retransmission, sizeof(tcp_pkt1_c2s_syn));
const struct layer_record *ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);
@@ -485,8 +485,8 @@ TEST(TCP_INIT_TO_OPENING, BY_SYNACK_RETRANSMISSION)
packet_parse(&pkt, (const char *)tcp_pkt_s2c_synack_retransmission, sizeof(tcp_pkt2_s2c_syn_ack));
const struct layer_record *ipv4_layer = packet_get_innermost_layer(&pkt, LAYER_TYPE_IPV4);
EXPECT_TRUE(ipv4_layer);
const struct ip *hdr = (const struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid((struct ip *)hdr, 0x1234);
struct ip *hdr = (struct ip *)ipv4_layer->hdr_ptr;
ipv4_hdr_set_ipid(hdr, 0x1234);
printf("<= packet parse\n\n");
sess = session_manager_update_session(mgr, &pkt);
EXPECT_TRUE(sess);