pcap_lookupnet 返回错误的 IP 地址
以下 libpcap 文档中的示例代码生成以下代码,该代码应报告给定接口的 IP 地址(在本例中为 eth0)[为简洁起见,省略错误检查]
#include <stdio.h>
#include <pcap.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 mask;
bpf_u_int32 ip;
struct in_addr ip_addr;
/* Find the properties for the device */
pcap_lookupnet("eth0", &ip, &mask, errbuf);
ip_addr.s_addr = ip;
printf("IP Address: %s\n", inet_ntoa(ip_addr));
return 0;
}
但是,这会产生 192.168.1.0,而不是正确的 192.168.1.100 。在不同子网的不同机器上运行它会产生 10.0.0.0,而不是正确的 10.0.0.107,这让我相信 libpcap 没有正确复制最后一个八位字节。我已经手动转换了 pcap_lookupnet 返回的整数,以确保使用 inet_ntoa 不会出现问题(我也尝试过 inet_ntop,结果相同)。按照这个问题的代码: 获取 Linux 上接口的 IP 地址 报告正确的 IP 地址。这是 libpcap 中的错误还是我做错了什么?
Following example code from the libpcap documentation yields the following code which should report the IP address of the given interface (eth0 in this case) [Error checking omitted for brevity]
#include <stdio.h>
#include <pcap.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
char errbuf[PCAP_ERRBUF_SIZE];
bpf_u_int32 mask;
bpf_u_int32 ip;
struct in_addr ip_addr;
/* Find the properties for the device */
pcap_lookupnet("eth0", &ip, &mask, errbuf);
ip_addr.s_addr = ip;
printf("IP Address: %s\n", inet_ntoa(ip_addr));
return 0;
}
However, this results in 192.168.1.0, rather than the correct 192.168.1.100. Running this on a different machine on a different subnet yields 10.0.0.0 rather than the correct 10.0.0.107 which leads me to believe libpcap is not copying the last octet correctly. I've manually converted the integer returned by pcap_lookupnet to ensure it's not an issue with the use of inet_ntoa (I've also tried inet_ntop, with identical results). Following the code from this question:
Get IP address of an interface on Linux
reports the correct IP address. Is this a bug in libpcap or am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的陈述“应报告给定接口的 IP 地址”是不正确的。
从联机帮助页:
您确定您的网络号分别为 10.0.0.107 或 192.168.1.100 吗?听起来很不寻常。
Your statement "which should report the IP address of the given interface" is incorrect.
From the manpage:
are you sure you have a network number of 10.0.0.107 or 192.168.1.100 respectively? Sounds rather unusual.