Reduce packet_get_xxx() calls

This commit is contained in:
luwenpeng
2023-12-21 20:03:43 +08:00
parent cd47b46e7a
commit fa4cc898d8
4 changed files with 125 additions and 18 deletions

View File

@@ -6,6 +6,8 @@
#include <netinet/udp.h>
#include "packet_helpers.h"
#include "tcp_helpers.h"
#include "udp_helpers.h"
/******************************************************************************
* Private API
@@ -202,7 +204,7 @@ uint16_t packet_get_tcp_sport(const struct packet *pkt)
const char *hdr_ptr = packet_get_tcp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohs(((struct tcphdr *)hdr_ptr)->source);
return tcp_hdr_get_sport((struct tcphdr *)hdr_ptr);
}
else
{
@@ -215,7 +217,7 @@ uint16_t packet_get_tcp_dport(const struct packet *pkt)
const char *hdr_ptr = packet_get_tcp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohs(((struct tcphdr *)hdr_ptr)->dest);
return tcp_hdr_get_dport((struct tcphdr *)hdr_ptr);
}
else
{
@@ -228,7 +230,7 @@ uint32_t packet_get_tcp_seq(const struct packet *pkt)
const char *hdr_ptr = packet_get_tcp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohl(((struct tcphdr *)hdr_ptr)->seq);
return tcp_hdr_get_seq((struct tcphdr *)hdr_ptr);
}
else
{
@@ -241,7 +243,7 @@ uint32_t packet_get_tcp_ack(const struct packet *pkt)
const char *hdr_ptr = packet_get_tcp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohl(((struct tcphdr *)hdr_ptr)->ack_seq);
return tcp_hdr_get_ack((struct tcphdr *)hdr_ptr);
}
else
{
@@ -254,7 +256,7 @@ uint8_t packet_get_tcp_flags(const struct packet *pkt)
const char *hdr_ptr = packet_get_tcp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ((struct tcphdr *)hdr_ptr)->th_flags;
return tcp_hdr_get_flags((struct tcphdr *)hdr_ptr);
}
else
{
@@ -397,7 +399,7 @@ uint16_t packet_get_inner_udp_sport(const struct packet *pkt)
const char *hdr_ptr = packet_get_inner_udp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohs(((struct udphdr *)hdr_ptr)->uh_sport);
return udp_hdr_get_sport((struct udphdr *)hdr_ptr);
}
else
{
@@ -410,7 +412,7 @@ uint16_t packet_get_inner_udp_dport(const struct packet *pkt)
const char *hdr_ptr = packet_get_inner_udp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohs(((struct udphdr *)hdr_ptr)->uh_dport);
return udp_hdr_get_dport((struct udphdr *)hdr_ptr);
}
else
{
@@ -481,7 +483,7 @@ uint16_t packet_get_outer_udp_sport(const struct packet *pkt)
const char *hdr_ptr = packet_get_outer_udp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohs(((struct udphdr *)hdr_ptr)->uh_sport);
return udp_hdr_get_sport((struct udphdr *)hdr_ptr);
}
else
{
@@ -494,7 +496,7 @@ uint16_t packet_get_outer_udp_dport(const struct packet *pkt)
const char *hdr_ptr = packet_get_outer_udp_hdr_ptr(pkt);
if (hdr_ptr)
{
return ntohs(((struct udphdr *)hdr_ptr)->uh_dport);
return udp_hdr_get_dport((struct udphdr *)hdr_ptr);
}
else
{

72
src/packet/tcp_helpers.h Normal file
View File

@@ -0,0 +1,72 @@
#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>
static inline uint16_t tcp_hdr_get_sport(const struct tcphdr *hdr)
{
return ntohs(hdr->source);
}
static inline uint16_t tcp_hdr_get_dport(const struct tcphdr *hdr)
{
return ntohs(hdr->dest);
}
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
{
return ntohl(hdr->seq);
}
static inline uint32_t tcp_hdr_get_ack(const struct tcphdr *hdr)
{
return ntohl(hdr->ack_seq);
}
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;
}
#ifdef __cpluscplus
}
#endif
#endif

27
src/packet/udp_helpers.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef _UDP_HELPERS_H
#define _UDP_HELPERS_H
#ifdef __cpluscplus
extern "C"
{
#endif
#include <arpa/inet.h>
#define __FAVOR_BSD 1
#include <netinet/udp.h>
static inline uint16_t udp_hdr_get_sport(const struct udphdr *hdr)
{
return ntohs(hdr->uh_sport);
}
static inline uint16_t udp_hdr_get_dport(const struct udphdr *hdr)
{
return ntohs(hdr->uh_dport);
}
#ifdef __cpluscplus
}
#endif
#endif