打印嗅探数据包的序列号
我正在使用 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) 吗?因为它不是这样工作的!
我的另一个问题是变量声明后的数字是多少?
tcph_hlen:4 中的 4 是什么意思
i am using pcap to create a packet sniffer.
i have this tcp structure:
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;
how can i print the sequence number?
should i use htons(sequence_number)?? because it isn't working this way!!
my other question is what is the number after the variable declaration?
what does 4 mean in tcph_hlen:4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果编程语言是 C,请注意您的结构不正确,因为您没有指定字段的大小。例如,序列号是32位,“int”可以是16位或64位。对于 seqnum,您应该使用 uint32_t。
也就是说,如果您从网络读取了 TCP 数据包,则序列号是网络顺序(大端),因此,要打印它,您需要调用 ntohl(网络到主机 - 长)。
If the programming language is C, note your struct is incorrect since you do not specify the sizes of the fields. For instance, the sequence number is 32 bits and "int" may be 16 or 64 bits. For seqnum, you should use uint32_t.
This being said, if you have read the TCP packet from the network, the sequence number is in network order (big-endian) and therefore, to print it, you need to call ntohl (network to host - long).