一个嗅探分组的打印序列号
-
20-09-2019 - |
题
我使用PCAP来创建分组嗅探器。结果, 我有这个TCP结构:
typedef struct TSP_header{
unsigned short int sport;
unsigned short int dport;
unsigned int seqnum;
unsigned int acknum;
unsigned char reserved:4, offset:4;
unsigned int
tcp_res1:4, //little-endian
tcph_hlen:4, //length of tcp header in 32-bit words
tcph_fin:1, //Finish flag "fin"
tcph_syn:1, //Synchronize sequence numbers to start a connection
tcph_rst:1, //Reset flag
tcph_psh:1, //Push, sends data to the application
tcph_ack:1, //acknowledge
tcph_urg:1, //urgent pointer
tcph_res2:2;
unsigned short int tcph_win;
unsigned short int tcph_chksum;
unsigned short int tcph_urgptr;
}TSP_header;
如何打印序列号?结果 我应该使用htons(sequence_number)??因为它不是以这种方式工作!
我的另一个问题是,什么是变量声明之后多少?点击 什么4意味着tcph_hlen:4
解决方案
如果编程语言是C,注意你的结构是不正确,因为你不指定字段的大小。例如,该序列号是32位,并且“INT”可以是16或64位。对于SEQNUM,你应该使用uint32_t的。
这就是说,如果你已经从网络读取的TCP数据包,序列号是网络顺序(big-endian)的,因此,将其打印出来,你需要调用再用ntohl(网络到主机 - 长)。
不隶属于 StackOverflow