【问题标题】:which field is tcp tcp_offset field in tcphdr in netinet/tcp.h file is it th_offset or doffnetinet/tcp.h 文件中 tcphdr 中的 tcp tcp_offset 字段是 th_offset 还是 doff
【发布时间】: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 中,它有doffth_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 */
      };

【问题讨论】:

    标签: c linux tcp


    【解决方案1】:

    tcphdr->th_off 是正确的 tcp 偏移量。 tcphdr=th_off 的大小。但我通过查看我正在映射 RX 而不是 TX 环的代码发现了这一点,所以我没有收到来自我的浏览器或 curl/nc 等的 Get 请求。

    我想知道为什么 linux/tcp.h 包含没有 th_off 字段的 struct tcphdr。没有 tcp 偏移字段。可能是因为没有内核代码需要使用 tcp 标头此字段是仅在用户空间程序中使用的更高级别,尽管 realtek 网络驱动程序使用我看到的文件但不使用 tcp 偏移字段

    【讨论】:

      猜你喜欢
      • 2016-03-11
      • 2011-05-12
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 2015-05-11
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多