update TCP utils

This commit is contained in:
luwenpeng
2024-02-21 11:49:20 +08:00
parent c37f9869a6
commit 03f428681e
7 changed files with 341 additions and 178 deletions

View File

@@ -11,6 +11,7 @@
#include "uthash.h"
#include "packet.h"
#include "udp_utils.h"
#include "tcp_utils.h"
#define likely(expr) __builtin_expect((expr), 1)
#define unlikely(expr) __builtin_expect((expr), 0)
@@ -977,7 +978,7 @@ static inline const char *parse_tcp(struct packet *handler, const char *data, ui
{
return data;
}
uint16_t hdr_len = ((struct tcphdr *)data)->th_off << 2;
uint16_t hdr_len = tcp_hdr_get_hdr_len((struct tcphdr *)data);
SET_LAYER(handler, layer, LAYER_TYPE_TCP, hdr_len, data, len);
return layer->pld_ptr;

View File

@@ -1,146 +0,0 @@
#ifndef _TCP_HELPERS_H
#define _TCP_HELPERS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <arpa/inet.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
/*
* TCP 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source Port | Destination Port |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Acknowledgment Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Data | |U|A|P|R|S|F| |
* | Offset| Reserved |R|C|S|S|Y|I| Window |
* | | |G|K|H|T|N|N| |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Checksum | Urgent Pointer |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options | Padding |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | data |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
static inline uint16_t tcp_hdr_get_host_order_sport(const struct tcphdr *hdr)
{
return ntohs(hdr->th_sport);
}
static inline uint16_t tcp_hdr_get_host_order_dport(const struct tcphdr *hdr)
{
return ntohs(hdr->th_dport);
}
static inline uint16_t tcp_hdr_get_net_order_sport(const struct tcphdr *hdr)
{
return hdr->th_sport;
}
static inline uint16_t tcp_hdr_get_net_order_dport(const struct tcphdr *hdr)
{
return hdr->th_dport;
}
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
{
return ntohl(hdr->th_seq);
}
static inline uint32_t tcp_hdr_get_ack(const struct tcphdr *hdr)
{
return ntohl(hdr->th_ack);
}
static inline uint8_t tcp_hdr_get_doff(const struct tcphdr *hdr)
{
return hdr->th_off << 2;
}
static inline uint8_t tcp_hdr_get_flags(const struct tcphdr *hdr)
{
return hdr->th_flags;
}
static inline bool tcp_hdr_has_flag_urg(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_URG;
}
static inline bool tcp_hdr_has_flag_ack(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_ACK;
}
static inline bool tcp_hdr_has_flag_psh(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_PUSH;
}
static inline bool tcp_hdr_has_flag_rst(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_RST;
}
static inline bool tcp_hdr_has_flag_syn(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_SYN;
}
static inline bool tcp_hdr_has_flag_fin(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_FIN;
}
static inline uint16_t tcp_hdr_get_window(const struct tcphdr *hdr)
{
return ntohs(hdr->th_win);
}
static inline uint16_t tcp_hdr_get_checksum(const struct tcphdr *hdr)
{
return ntohs(hdr->th_sum);
}
static inline uint16_t tcp_hdr_get_urg_ptr(const struct tcphdr *hdr)
{
return ntohs(hdr->th_urp);
}
static inline uint16_t tcp_hdr_get_opt_len(const struct tcphdr *hdr)
{
return tcp_hdr_get_doff(hdr) - sizeof(struct tcphdr);
}
static inline const uint8_t *tcp_hdr_get_opt_ptr(const struct tcphdr *hdr)
{
return ((const uint8_t *)hdr) + sizeof(struct tcphdr);
}
static inline void tcp_hdr_set_flags(struct tcphdr *hdr, uint8_t flags)
{
hdr->th_flags = flags;
}
static inline void tcp_hdr_set_flag_rst(struct tcphdr *hdr)
{
hdr->th_flags |= TH_RST;
}
#ifdef __cpluscplus
}
#endif
#endif

263
src/packet/tcp_utilis.h Normal file
View File

@@ -0,0 +1,263 @@
#ifndef _TCP_UTILS_H
#define _TCP_UTILS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <string.h>
#include <arpa/inet.h>
#define __FAVOR_BSD 1
#include <netinet/tcp.h>
/*
* TCP 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
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Source Port | Destination Port |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Sequence Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Acknowledgment Number |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Data | |U|A|P|R|S|F| |
* | Offset| Reserved |R|C|S|S|Y|I| Window |
* | | |G|K|H|T|N|N| |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Checksum | Urgent Pointer |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | Options | Padding |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | data |
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
*/
/******************************************************************************
* get
******************************************************************************/
static inline uint16_t tcp_hdr_get_src_port(const struct tcphdr *hdr)
{
return ntohs(hdr->th_sport);
}
static inline uint16_t tcp_hdr_get_dst_port(const struct tcphdr *hdr)
{
return ntohs(hdr->th_dport);
}
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
{
return ntohl(hdr->th_seq);
}
static inline uint32_t tcp_hdr_get_ack(const struct tcphdr *hdr)
{
return ntohl(hdr->th_ack);
}
static inline uint8_t tcp_hdr_get_hdr_len(const struct tcphdr *hdr)
{
return hdr->th_off << 2;
}
static inline uint8_t tcp_hdr_get_flags(const struct tcphdr *hdr)
{
return hdr->th_flags;
}
static inline bool tcp_hdr_get_urg_flag(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_URG;
}
static inline bool tcp_hdr_get_ack_flag(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_ACK;
}
static inline bool tcp_hdr_get_push_flag(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_PUSH;
}
static inline bool tcp_hdr_get_rst_flag(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_RST;
}
static inline bool tcp_hdr_get_syn_flag(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_SYN;
}
static inline bool tcp_hdr_get_fin_flag(const struct tcphdr *hdr)
{
return hdr->th_flags & TH_FIN;
}
static inline uint16_t tcp_hdr_get_window(const struct tcphdr *hdr)
{
return ntohs(hdr->th_win);
}
static inline uint16_t tcp_hdr_get_checksum(const struct tcphdr *hdr)
{
return ntohs(hdr->th_sum);
}
static inline uint16_t tcp_hdr_get_urg_ptr(const struct tcphdr *hdr)
{
return ntohs(hdr->th_urp);
}
static inline uint16_t tcp_hdr_get_opt_len(const struct tcphdr *hdr)
{
return tcp_hdr_get_hdr_len(hdr) - sizeof(struct tcphdr);
}
static inline const char *tcp_hdr_get_opt_data(const struct tcphdr *hdr)
{
return ((const char *)hdr) + sizeof(struct tcphdr);
}
/******************************************************************************
* set
******************************************************************************/
static inline void tcp_hdr_set_src_port(struct tcphdr *hdr, uint16_t port)
{
hdr->th_sport = htons(port);
}
static inline void tcp_hdr_set_dst_port(struct tcphdr *hdr, uint16_t port)
{
hdr->th_dport = htons(port);
}
static inline void tcp_hdr_set_seq(struct tcphdr *hdr, uint32_t seq)
{
hdr->th_seq = htonl(seq);
}
static inline void tcp_hdr_set_ack(struct tcphdr *hdr, uint32_t ack)
{
hdr->th_ack = htonl(ack);
}
static inline void tcp_hdr_set_hdr_len(struct tcphdr *hdr, uint8_t offset)
{
hdr->th_off = offset >> 2;
}
static inline void tcp_hdr_set_flags(struct tcphdr *hdr, uint8_t flags)
{
hdr->th_flags = flags;
}
static inline void tcp_hdr_set_urg_flag(struct tcphdr *hdr, bool flag)
{
if (flag)
{
hdr->th_flags |= TH_URG;
}
else
{
hdr->th_flags &= ~TH_URG;
}
}
static inline void tcp_hdr_set_ack_flag(struct tcphdr *hdr, bool flag)
{
if (flag)
{
hdr->th_flags |= TH_ACK;
}
else
{
hdr->th_flags &= ~TH_ACK;
}
}
static inline void tcp_hdr_set_push_flag(struct tcphdr *hdr, bool flag)
{
if (flag)
{
hdr->th_flags |= TH_PUSH;
}
else
{
hdr->th_flags &= ~TH_PUSH;
}
}
static inline void tcp_hdr_set_rst_flag(struct tcphdr *hdr, bool flag)
{
if (flag)
{
hdr->th_flags |= TH_RST;
}
else
{
hdr->th_flags &= ~TH_RST;
}
}
static inline void tcp_hdr_set_syn_flag(struct tcphdr *hdr, bool flag)
{
if (flag)
{
hdr->th_flags |= TH_SYN;
}
else
{
hdr->th_flags &= ~TH_SYN;
}
}
static inline void tcp_hdr_set_fin_flag(struct tcphdr *hdr, bool flag)
{
if (flag)
{
hdr->th_flags |= TH_FIN;
}
else
{
hdr->th_flags &= ~TH_FIN;
}
}
static inline void tcp_hdr_set_window(struct tcphdr *hdr, uint16_t window)
{
hdr->th_win = htons(window);
}
static inline void tcp_hdr_set_checksum(struct tcphdr *hdr, uint16_t checksum)
{
hdr->th_sum = htons(checksum);
}
static inline void tcp_hdr_set_urg_ptr(struct tcphdr *hdr, uint16_t ptr)
{
hdr->th_urp = htons(ptr);
}
static inline void tcp_hdr_set_opt_len(struct tcphdr *hdr, uint16_t len)
{
hdr->th_off = (sizeof(struct tcphdr) + len) >> 2;
}
// must be called after tcp_hdr_set_opt_len
static inline void tcp_hdr_set_opt_data(struct tcphdr *hdr, const char *ptr)
{
memcpy((char *)hdr + sizeof(struct tcphdr), ptr, tcp_hdr_get_opt_len(hdr));
}
#ifdef __cpluscplus
}
#endif
#endif

View File

@@ -8,8 +8,8 @@ target_link_libraries(gtest_packet packet gtest)
add_executable(gtest_udp_utils gtest_udp_utils.cpp)
target_link_libraries(gtest_udp_utils packet gtest)
add_executable(gtest_tcp_helpers gtest_tcp_helpers.cpp)
target_link_libraries(gtest_tcp_helpers 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)
@@ -23,7 +23,7 @@ target_link_libraries(gtest_packet_helpers packet gtest)
include(GoogleTest)
gtest_discover_tests(gtest_packet)
gtest_discover_tests(gtest_udp_utils)
gtest_discover_tests(gtest_tcp_helpers)
gtest_discover_tests(gtest_tcp_utils)
gtest_discover_tests(gtest_ipv4_helpers)
gtest_discover_tests(gtest_ipv6_helpers)
gtest_discover_tests(gtest_packet_helpers)

View File

@@ -1,6 +1,6 @@
#include <gtest/gtest.h>
#include "tcp_helpers.h"
#include "tcp_utils.h"
/*
* Transmission Control Protocol, Src Port: 55555, Dst Port: 40856, Seq: 0, Ack: 1, Len: 0
@@ -74,26 +74,71 @@ unsigned char data[] = {
0xd9, 0x03, 0x9f, 0x98, 0xec, 0x5f, 0xc6, 0x3c, 0x3b, 0x12, 0x47, 0x92, 0xa0, 0x12, 0xaa, 0xaa, 0xfe, 0x30, 0x00, 0x00, 0x02, 0x04, 0xff, 0xd7, 0x04, 0x02,
0x08, 0x0a, 0xa4, 0xef, 0xa3, 0xcc, 0xa4, 0xef, 0xa3, 0xcc, 0x01, 0x03, 0x03, 0x07};
TEST(TCP_HELPERS, TEST)
TEST(TCP_UTILS, GET)
{
const struct tcphdr *hdr = (struct tcphdr *)data;
EXPECT_TRUE(tcp_hdr_get_host_order_sport(hdr) == 55555);
EXPECT_TRUE(tcp_hdr_get_host_order_dport(hdr) == 40856);
EXPECT_TRUE(tcp_hdr_get_src_port(hdr) == 55555);
EXPECT_TRUE(tcp_hdr_get_dst_port(hdr) == 40856);
EXPECT_TRUE(tcp_hdr_get_seq(hdr) == 3965699644);
EXPECT_TRUE(tcp_hdr_get_ack(hdr) == 991053714);
EXPECT_TRUE(tcp_hdr_get_doff(hdr) == 40);
EXPECT_TRUE(tcp_hdr_get_hdr_len(hdr) == 40);
EXPECT_TRUE(tcp_hdr_get_flags(hdr) == 0x012);
EXPECT_TRUE(tcp_hdr_has_flag_urg(hdr) == false);
EXPECT_TRUE(tcp_hdr_has_flag_ack(hdr) == true);
EXPECT_TRUE(tcp_hdr_has_flag_psh(hdr) == false);
EXPECT_TRUE(tcp_hdr_has_flag_rst(hdr) == false);
EXPECT_TRUE(tcp_hdr_has_flag_syn(hdr) == true);
EXPECT_TRUE(tcp_hdr_has_flag_fin(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_urg_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_ack_flag(hdr) == true);
EXPECT_TRUE(tcp_hdr_get_push_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_rst_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_syn_flag(hdr) == true);
EXPECT_TRUE(tcp_hdr_get_fin_flag(hdr) == false);
EXPECT_TRUE(tcp_hdr_get_window(hdr) == 43690);
EXPECT_TRUE(tcp_hdr_get_checksum(hdr) == 0xfe30);
EXPECT_TRUE(tcp_hdr_get_urg_ptr(hdr) == 0);
EXPECT_TRUE(tcp_hdr_get_opt_len(hdr) == 20);
EXPECT_TRUE(tcp_hdr_get_opt_ptr(hdr) == data + 20);
EXPECT_TRUE(tcp_hdr_get_opt_data(hdr) == data + 20);
}
TEST(TCP_UTILS, SET1)
{
char buff[40];
struct tcphdr *hdr = (struct tcphdr *)buff;
tcp_hdr_set_src_port(hdr, 55555);
tcp_hdr_set_dst_port(hdr, 40856);
tcp_hdr_set_seq(hdr, 3965699644);
tcp_hdr_set_ack(hdr, 991053714);
tcp_hdr_set_hdr_len(hdr, 40);
tcp_hdr_set_flags(hdr, 0x012);
tcp_hdr_set_window(hdr, 43690);
tcp_hdr_set_checksum(hdr, 0xfe30);
tcp_hdr_set_urg_ptr(hdr, 0);
tcp_hdr_set_opt_len(hdr, 20);
tcp_hdr_set_opt_data(hdr, data + 20);
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
}
TEST(TCP_UTILS, SET2)
{
char buff[40] = {0};
struct tcphdr *hdr = (struct tcphdr *)buff;
tcp_hdr_set_src_port(hdr, 55555);
tcp_hdr_set_dst_port(hdr, 40856);
tcp_hdr_set_seq(hdr, 3965699644);
tcp_hdr_set_ack(hdr, 991053714);
tcp_hdr_set_hdr_len(hdr, 40);
tcp_hdr_set_urg_flag(hdr, false);
tcp_hdr_set_ack_flag(hdr, true);
tcp_hdr_set_push_flag(hdr, false);
tcp_hdr_set_rst_flag(hdr, false);
tcp_hdr_set_syn_flag(hdr, true);
tcp_hdr_set_fin_flag(hdr, false);
tcp_hdr_set_window(hdr, 43690);
tcp_hdr_set_checksum(hdr, 0xfe30);
tcp_hdr_set_urg_ptr(hdr, 0);
tcp_hdr_set_opt_len(hdr, 20);
tcp_hdr_set_opt_data(hdr, data + 20);
EXPECT_TRUE(memcmp(buff, data, 40) == 0);
}
int main(int argc, char **argv)