tcp头&;旗帜

tcp头&;旗帜,tcp,header,flags,libpcap,Tcp,Header,Flags,Libpcap,我最近开始使用libpcap和linux特定的库以及netinet/tcp.h之类的头文件编写包嗅探器。 问题是:当我使用THU OFF(tcp)*4获取tcp头时,其值通常小于20字节。好的,我知道,它的格式不正确,但Wireshark正在显示其他值(20在紧急指针之后的TCP标头中没有数据偏移字段;在紧急指针之后有选项。相反,请删除 #if BYTE_ORDER == LITTLE_ENDIAN u_int th_x2:4, /* (unused) */ th_o

我最近开始使用libpcap和linux特定的库以及netinet/tcp.h之类的头文件编写包嗅探器。
问题是:当我使用THU OFF(tcp)*4获取tcp头时,其值通常小于20字节。好的,我知道,它的格式不正确,但Wireshark正在显示其他值(20在紧急指针之后的TCP标头中没有数据偏移字段;在紧急指针之后有选项。相反,请删除

#if BYTE_ORDER == LITTLE_ENDIAN
    u_int th_x2:4,    /* (unused)    */
    th_off:4;         /* data offset */
#endif
#if BYTE_ORDER == BIG_ENDIAN
    u_int th_off:4,   /* data offset */
    th_x2:4;          /* (unused)    */
#endif
从你的结构和移动

u_char  th_offx2;               /* data offset, rsvd */
#define TH_OFF(th)      (((th)->th_offx2 & 0xf0) >> 4)
来替换它,所以结构看起来像

struct nread_tcp {
    u_short th_sport; /* source port            */
    u_short th_dport; /* destination port       */
    u_short th_seq;   /* sequence number        */
    u_short 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_NS       0x100
#define TH_RS       0xE00

    u_short th_win; /* window */
    u_short th_sum; /* checksum */
    u_short th_urp; /* urgent pointer */
};

然后重新编译你的程序,看看是否有效。

以下是对我有效的方法

u_short th_seq;   /* sequence number        */
u_short th_ack;   /* acknowledgement number */
改为:

u_int th_seq;   /* sequence number        */
u_int th_ack;   /* acknowledgement number */

感谢您的提示,但不幸的是,结果是一样的:(如果在调用
pcap\u-datalink()
之后调用
pcap\u-open\u-live(),
pcap\u-open\u-offline()
pcap\u-activate()
,它会返回什么值,并且您是否正确处理了中所述类型的链接层标头?
struct nread_tcp {
    u_short th_sport; /* source port            */
    u_short th_dport; /* destination port       */
    u_short th_seq;   /* sequence number        */
    u_short 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_NS       0x100
#define TH_RS       0xE00

    u_short th_win; /* window */
    u_short th_sum; /* checksum */
    u_short th_urp; /* urgent pointer */
};
u_short th_seq;   /* sequence number        */
u_short th_ack;   /* acknowledgement number */
u_int th_seq;   /* sequence number        */
u_int th_ack;   /* acknowledgement number */