Add Packet/IPv4/TCP/UDP helpers API

This commit is contained in:
luwenpeng
2024-01-03 09:57:06 +08:00
parent 529234029f
commit 6b3fc4b560
16 changed files with 502 additions and 956 deletions

View File

@@ -10,24 +10,53 @@ extern "C"
#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_sport(const struct tcphdr *hdr)
{
return ntohs(hdr->source);
return ntohs(hdr->th_sport);
}
static inline uint16_t tcp_hdr_get_dport(const struct tcphdr *hdr)
{
return ntohs(hdr->dest);
return ntohs(hdr->th_dport);
}
static inline uint32_t tcp_hdr_get_seq(const struct tcphdr *hdr)
{
return ntohl(hdr->seq);
return ntohl(hdr->th_seq);
}
static inline uint32_t tcp_hdr_get_ack(const struct tcphdr *hdr)
{
return ntohl(hdr->ack_seq);
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)
@@ -65,6 +94,31 @@ 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);
}
#ifdef __cpluscplus
}
#endif