2011-01-30 19 views
0
00000 45 00 02 87 8a 97 40 00 40 06 af d7 7f 00 00 01 [email protected]@....... 
00016 7f 00 00 01 e0 56 00 50 0d 46 70 a8 0c e2 41 70 .....V.P.Fp...Ap 
00032 80 18 01 01 b9 70 00 00 01 01 08 0a 00 64 fa 32 .....p.......d.2 

这是Wireshark/tcpdump/my代码如何表示存储在转储文件中的TCP数据包的一部分。 第三行的第一个值,80,它是32字节的正确TCP数据偏移量,或8个32位(4字节)字。但是,当(在我的代码中),struct tcp_hdr通过这个有效载荷(包括ip头部偏移量等)被创建时,读取my_tcphdr->doff的结果是6(相当于24字节)。从无符号指针读取4位值int:TCP数据偏移量读取受损

假设从结构中读取这些数据不够健康。

如何在给定(称为正确)指针的情况下将此4位(0xF0?)值读取到某个int:unsigned char *doff = tcp_payload+MAGIC_16

回答

3

你会提取字节并将它移到右边4个位置。

unsigned char *doff = tcp_payload+MAGIC_16 
unsigned char doffval = (*doff) >>4; 

至于为什么你的struct tcp_hdr没有“工作”,你应该发布。也许它使用了错误的endian,或者struct填充,所以它不符合线上格式。