【发布时间】:2021-10-26 08:01:09
【问题描述】:
这是提取 tcp 数据包负载的宏和代码
static void display(struct tpacket3_hdr *ppd)
{
struct ethhdr *eth = (struct ethhdr *) ((uint8_t *) ppd + ppd->tp_mac);
struct iphdr *ip = (struct iphdr *) ((uint8_t *)eth + ETH_HLEN);
struct tcphdr *tcp=(struct tcphdr *)((uint8_t *)ip+sizeof(struct iphdr));
#define TCP_HLEN(tcp) ((tcp->tcp_offset & 0xf0) >> 2)
char *payload_body=(char *)((uint8_t*)ppd+ppd->tp_mac+sizeof(struct iphdr) + TCP_HLEN(tcp)) ;
}
define 宏将给出tcp_offset,因为 4 位 tcp_offset (AND-ing) 与 4 1s 位并将输出向右移动到 0x0x 中的 x,所以这将是 tcp 偏移值并添加到 tcp 那就是有效载荷。但是在netinet/tcp.h 文件中的tcphdr 中,它有doff 和th_off 其中一个看起来像tcp offset 哪一个?另一个是什么?或者还有其他我想看的领域。前段时间在不同的 linux detro 上读取了带有 th_off 的有效负载,但我没有使用 ubuntu,但我认为这并不重要。
tpacket3_hdr 只是 rx 环形映射缓冲区暴露的环形缓冲区中的帧表示。获取以太网帧
这是 netinet/tcp.h 文件中的 tcphdr
struct tcphdr
{
__extension__ union
{
struct
{
uint16_t th_sport; /* source port */
uint16_t th_dport; /* destination port */
tcp_seq th_seq; /* sequence number */
tcp_seq th_ack; /* acknowledgement number */
# if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t th_x2:4; /* (unused) */
uint8_t th_off:4; /* data offset */
# endif
# if __BYTE_ORDER == __BIG_ENDIAN
uint8_t th_off:4; /* data offset */
uint8_t th_x2:4; /* (unused) */
# endif
uint8_t th_flags;
# define TH_FIN 0x01
# define TH_SYN 0x02
# define TH_RST 0x04
# define TH_PUSH 0x08
# define TH_ACK 0x10
# define TH_URG 0x20
uint16_t th_win; /* window */
uint16_t th_sum; /* checksum */
uint16_t th_urp; /* urgent pointer */
};
【问题讨论】: