【问题标题】:Access TCP Flags From Struct [duplicate]从结构访问 TCP 标志 [重复]
【发布时间】:2017-07-22 22:43:56
【问题描述】:

http://www.tcpdump.org/sniffex.c 脚本中有 TCP 结构:

struct sniff_tcp {
    u_short th_sport;               /* source port */
    u_short th_dport;               /* destination port */
    tcp_seq th_seq;                 /* sequence number */
    tcp_seq th_ack;                 /* acknowledgement number */
    u_char  th_offx2;               /* data offset, rsvd */
    #define TH_OFF(th)      (((th)->th_offx2 & 0xf0) >> 4)
    u_char  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
    #define TH_ECE  0x40
    #define TH_CWR  0x80
    #define TH_FLAGS        (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
    u_short th_win;                 /* window */
    u_short th_sum;                 /* checksum */
    u_short th_urp;                 /* urgent pointer */
};

如何确定是否设置了特定标志?对于每个标志,我想知道它是 1 还是 0。

【问题讨论】:

  • C 不是脚本语言。

标签: c tcp


【解决方案1】:

您需要以类似于以下方式测试标志:

struct sniff_tcp *ptr = …;
if (ptr->th_flags & TH_FIN)
  puts ("FIN set");
if (ptr->th_flags & TH_SYN)
  puts ("SYN set");

等等。这是你要问的吗?

(您可能必须在结构定义中应用#pragma packpack 属性,以处理未对齐的访问。)

【讨论】:

  • 我会使用 netinet/tcp.h 中的定义来防止不必要的错误。
猜你喜欢
  • 2016-05-09
  • 2017-11-24
  • 1970-01-01
  • 2017-04-21
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多