Add ipv4 helpers

This commit is contained in:
luwenpeng
2023-12-29 15:15:18 +08:00
parent d47efe6aad
commit 529234029f
3 changed files with 128 additions and 1 deletions

71
src/packet/ipv4_helpers.h Normal file
View File

@@ -0,0 +1,71 @@
#ifndef _IPV4_HELPERS_H
#define _IPV4_HELPERS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <arpa/inet.h>
#include <netinet/ip.h>
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 uint16_t ipv4_hdr_get_frag_offset(const struct ip *hdr)
{
return ntohs(hdr->ip_off) & IP_OFFMASK;
}
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_src(const struct ip *hdr)
{
return ntohl(hdr->ip_src.s_addr);
}
static inline uint32_t ipv4_hdr_get_dst(const struct ip *hdr)
{
return ntohl(hdr->ip_dst.s_addr);
}
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;
}
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -10,6 +10,11 @@ add_executable(gtest_packet_helpers gtest_packet_helpers.cpp)
target_include_directories(gtest_packet_helpers PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(gtest_packet_helpers PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(gtest_packet_helpers packet gtest) target_link_libraries(gtest_packet_helpers packet gtest)
add_executable(gtest_ipv4_helpers gtest_ipv4_helpers.cpp)
target_include_directories(gtest_ipv4_helpers PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(gtest_ipv4_helpers packet gtest)
include(GoogleTest) include(GoogleTest)
gtest_discover_tests(gtest_packet) gtest_discover_tests(gtest_packet)
gtest_discover_tests(gtest_packet_helpers) gtest_discover_tests(gtest_packet_helpers)
gtest_discover_tests(gtest_ipv4_helpers)

View File

@@ -0,0 +1,51 @@
#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 hdr[] = {0x45, 0x00, 0x00, 0x2c, 0xff, 0xff, 0x20, 0x00, 0x7f, 0x06, 0x4d, 0x8b, 0xc0, 0xa8, 0x24, 0x67, 0xc0, 0xa8, 0x28, 0x89};
TEST(IPV4_HELPERS, GET)
{
EXPECT_TRUE(ipv4_hdr_get_ipid((struct ip *)hdr) == 65535);
EXPECT_TRUE(ipv4_hdr_get_flags((struct ip *)hdr) == 1);
EXPECT_TRUE(ipv4_hdr_get_frag_offset((struct ip *)hdr) == 0);
EXPECT_TRUE(ipv4_hdr_get_ttl((struct ip *)hdr) == 127);
EXPECT_TRUE(ipv4_hdr_get_protocol((struct ip *)hdr) == 6);
EXPECT_TRUE(ipv4_hdr_get_checksum((struct ip *)hdr) == 0x4d8b);
EXPECT_TRUE(ipv4_hdr_get_src((struct ip *)hdr) == 0xc0a82467);
EXPECT_TRUE(ipv4_hdr_get_dst((struct ip *)hdr) == 0xc0a82889);
EXPECT_TRUE(ipv4_hdr_has_flag_rf((struct ip *)hdr) == false);
EXPECT_TRUE(ipv4_hdr_has_flag_df((struct ip *)hdr) == false);
EXPECT_TRUE(ipv4_hdr_has_flag_mf((struct ip *)hdr) == true);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}