Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;将字符串数据包转换为iphdr-字符串数据包的格式应该是什么?_C++_Udp_Hex_Protocols_Wireshark - Fatal编程技术网

C++ C++;将字符串数据包转换为iphdr-字符串数据包的格式应该是什么?

C++ C++;将字符串数据包转换为iphdr-字符串数据包的格式应该是什么?,c++,udp,hex,protocols,wireshark,C++,Udp,Hex,Protocols,Wireshark,我有一行代码: struct iphdr *ip_header = (struct iphdr*) packet.c_str(); 从ip.h: struct iphdr { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ihl:4; unsigned int version:4; #elif __BYTE_ORDER == __BIG_ENDIAN unsigned int version:4; unsi

我有一行代码:

struct iphdr *ip_header = (struct iphdr*) packet.c_str();
从ip.h:

struct iphdr
  {
#if __BYTE_ORDER == __LITTLE_ENDIAN
    unsigned int ihl:4;
    unsigned int version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
    unsigned int version:4;
    unsigned int ihl:4;
#else
# error    "Please fix <bits/endian.h>"
#endif
    u_int8_t tos;
    u_int16_t tot_len;
    u_int16_t id;
    u_int16_t frag_off;
    u_int8_t ttl;
    u_int8_t protocol;
    u_int16_t check;
    u_int32_t saddr;
    u_int32_t daddr;
    /*The options start here. */
  };
我删除了eth标题,因此我只剩下以下内容:

0000   45 00
0010   00 3f 51 45 00 00 40 11 aa b3 c0 a8 fe 65 c0 a8
0020   fe fe 0e 76 00 35 00 2b d5 1c 9c 0a 01 00 00 01
0030   00 00 00 00 00 00 03 77 77 77 06 67 6f 6f 67 6c
0040   65 03 63 6f 6d 02 70 68 00 00 01 00 01
第一部分(45 00 3f 51 45 00 00 40 11)翻译为:

45     0100 .... = Version: 4
       .... 0101 = Header Length: 20 bytes (5)
00     Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
00 3f  Total Length: 63
51 45  Identification: 0x5145 (20805)
00 00  Flags: 0x00
       Fragment offset: 0
40     Time to live: 64
11     Protocol: UDP (17)
我的问题是:字符串变量包的格式应该是什么?我试过这个:

std::string packet = "45 00 00 3f 51 45 00 00 40 11";
但是对于ip_头->协议,我得到的是48“0”而不是17

我还想知道,为什么协议不在第9字节?根据iphdr的结构,我假设应该在9号


非常感谢任何人的帮助。非常感谢

你的基本假设有一些问题。您使用的是一个字符串,并且假设如果您将其转换为某个结构定义,它将自动(并且神奇地自动)将其转换为该结构定义的正确二进制表示形式。事实并非如此。假设您有一个结构
'struct Test{unsigned int t;}'
和一个字符串
'std::string st=“12”
。您可以执行
“struct Test*pt=st.c_str();”。“12”的ASCII表示形式是0x31 0x32,因此现在*pt指向一个从31 32开始的内存位置。将其转换为整数(假设我们有一个big-endian系统,并且假设无符号int为两个字节)将导致0x3132(十进制12594)

数据包不应包含文本。您在Wireshark中看到的是二进制数据的十六进制表示。数据本身不包含这些字符。(就像“小猫”这个词不是小猫一样。)
std::string packet = "45 00 00 3f 51 45 00 00 40 11";