From 529234029f68567cf0692a4df867d16ceea4861e Mon Sep 17 00:00:00 2001 From: luwenpeng Date: Fri, 29 Dec 2023 15:15:18 +0800 Subject: [PATCH] Add ipv4 helpers --- src/packet/ipv4_helpers.h | 71 ++++++++++++++++++++++++++ src/packet/test/CMakeLists.txt | 7 ++- src/packet/test/gtest_ipv4_helpers.cpp | 51 ++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 src/packet/ipv4_helpers.h create mode 100644 src/packet/test/gtest_ipv4_helpers.cpp diff --git a/src/packet/ipv4_helpers.h b/src/packet/ipv4_helpers.h new file mode 100644 index 0000000..db42bea --- /dev/null +++ b/src/packet/ipv4_helpers.h @@ -0,0 +1,71 @@ +#ifndef _IPV4_HELPERS_H +#define _IPV4_HELPERS_H + +#ifdef __cpluscplus +extern "C" +{ +#endif + +#include +#include + +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 diff --git a/src/packet/test/CMakeLists.txt b/src/packet/test/CMakeLists.txt index 8e7754a..4ad5f90 100644 --- a/src/packet/test/CMakeLists.txt +++ b/src/packet/test/CMakeLists.txt @@ -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_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) gtest_discover_tests(gtest_packet) -gtest_discover_tests(gtest_packet_helpers) \ No newline at end of file +gtest_discover_tests(gtest_packet_helpers) +gtest_discover_tests(gtest_ipv4_helpers) \ No newline at end of file diff --git a/src/packet/test/gtest_ipv4_helpers.cpp b/src/packet/test/gtest_ipv4_helpers.cpp new file mode 100644 index 0000000..6f80f94 --- /dev/null +++ b/src/packet/test/gtest_ipv4_helpers.cpp @@ -0,0 +1,51 @@ +#include + +#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(); +}