C++ 将字符串数据包转换为 iphdr语言 - 字符串数据包的格式应该是什么?
C++ Converting string packet to iphdr - what should be the format of string packet?
所以我有这样一行代码:
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. */
};
我使用 wireshark 捕获了一个 DNS 数据包,并得到了此示例数据包:
0000 e0 8e 3c 1c c0 07 ac bc 32 83 84 d9 08 00 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
我删除了 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 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_header>协议,我得到48 个"0">而不是 17。
我也想知道,为什么协议不在第 9 个字节上?我假设它应该在 9 日基于 iphr 的结构。
非常感谢任何人的帮助。多谢!
你的基本假设有一些问题。您正在使用一个字符串,并且假设如果将其转换为某个结构定义,它将自动(并且自动神奇地(将其转换为该结构定义的正确二进制表示形式。事实并非如此。假设您有一个结构'struct Test { unsigned int t; }'
和一个字符串'std::string st = "12"'
。你做'struct Test *pt = st.c_str();'
."12"的 ASCII 表示形式将0x31 0x32,因此现在 *pt 指向以 31 32 开头的内存位置。将其转换为整数(假设我们有一个大端系统,并假设无符号 int 是两个字节(会导致 0x3132(十进制 12594(。
相关文章:
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 发送固定大小的 UDP 数据包
- pcap_handler回调仅在使用 NPCAP v0.9991 时包含空数据包
- 在 c++ 中解析数据包数据的最佳方法是什么?
- 接受函数在发送数据包时等待
- 如何在 omnet++ 中发送自定义数据包?
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 如果我在块中编写字符串文字,是否会从数据部分复制整个字符串数据?
- 数据包访问实践
- 在C++中创建一个简单的数据包路由器,如何跟踪"客户端"?
- 德拉吉诺 LG01-S 收到异常数据包并停止工作
- 无法在 c++ 中将字符串分配给字符串数据类型
- 将数据包从C++服务器发送到NodeJs服务器时出现MessagePack解码错误
- 使用C++将UDP数据包存储在Structure中
- FFmpeg av_read_frame从音频流返回数据包
- 为什么操作系统正在更改我的数据包的指定传出端口
- 是否可以将多个结构作为一个数据包存储在一个函数中,然后传递给其他函数并在那里提取?
- C++ 将字符串数据包转换为 iphdr语言 - 字符串数据包的格式应该是什么?