从 nfq_get_payload 获取数据
当我使用 'nfq_get_payload(nfqData, &data);' 时然后打印“数据”,我得到的都是E,我还应该做些什么来获取有效负载吗?
ret = nfq_get_payload(nfqData, &data);
if (ret >= 0)
printf("payload_len=%d \nPayload: %s", ret, data);
When I use 'nfq_get_payload(nfqData, &data);' and then print 'data', all I get are E's, is there something else that I should be doing to get the payload?
ret = nfq_get_payload(nfqData, &data);
if (ret >= 0)
printf("payload_len=%d \nPayload: %s", ret, data);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
data 参数的类型为 char**,在本例中我们可以将其视为指向字符列表的指针。该调用将数据设置为数据包的字符列表。然而,即使“c string”也是 char* 类型,这里返回的数据也不是 c 字符串,因为它嵌入了空值。如果您尝试使用 %s 打印它,您将只能获得第一个空值之前的数据。
我敢打赌您会收到一个 IP 数据包,因为 IP 数据包标头的第一个字节 几乎总是 0x45,在 ASCII 中是字母“E”。如果后面跟着一个 TOS 字段,该字段几乎总是 0 或 NULL。这个 0x45,0x00 是一个有效的 c 字符串,这就是您得到 E 的原因。
您确实想使用返回值 ret 作为循环计数器并打印数据包的每个字节:
看看 为什么 printf 不只打印一个字节打印十六进制时? 有关打印缓冲区的讨论。
the data parameter is of type char**, which in this case we can think of as a pointer to a list of chars. The call sets data to the list of chars of the packet. However, even though a "c string" is also of type char* the data returned here is not a a c-string because it has embedded nulls. If you try to print it with %s you'll only get data up to the first null.
I bet you're getting a IP packet, because the first byte of a IP packet header is nearly always 0x45, which in ASCII is the letter 'E'. This if followed by a TOS field, which is almost always 0, or NULL. This 0x45,0x00 is a valid c-string and is why you're getting an E.
You really want to use the return value, ret, as a loop counter and print each byte of the packet:
Take a look at Why does printf not print out just one byte when printing hex? for a discussion of printing buffers.