负TCP序列号(C,LibPcap,TCP)

时间:2015-04-10 13:05:50

标签: c tcp network-programming libpcap

我正在使用libpcap,无法从此结构中访问序列号变量。

要获取TCP序列号我现在正在使用ntohl(tcp->th_seq)并且它给了我一些正面的序列号,它们似乎是有效的(在wireshark中)但它也给了我很多负面的TCP数字。

我是否错误地访问了变量或者是否需要转换负数TCP号?

struct sniff_tcp *tcp;

typedef u_int tcp_seq;

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 */
};

-----Console:-----------------------------------
Packet number 24:
current time: 2015-04-10 14:14:48.990 
   From: x.x.x.x
     To: y.y.y.y
   Protocol: TCP
   Src port: 443
   Dst port: 53111
    Seq Num: 943553986  // This is valid in wireshark
   ACK Detected

Packet number 25:
current time: 2015-04-10 14:14:48.990 
   From: x.x.x.x
     To: y.y.y.y
   Protocol: TCP
   Src port: 53111
   Dst port: 443
    Seq Num: -1759841006  // I'm not sure what to make of this
   ACK Detected

1 个答案:

答案 0 :(得分:3)

您没有显示如何打印该号码。可能你只是使用错误的格式说明符进行打印。 ntohl()返回的数字类型为uint32_t,因此必须按以下方式打印:

#include <inttypes.h>

printf("%" PRIu32, ntohl(tcp->th_seq));

这里PRIu32是您的平台打印32位无符号整数的正确格式说明符。