pcap_lookupnet返回不正确的IP地址 [英] pcap_lookupnet returns incorrect IP address
问题描述
继libpcap的文档例如code产生以下code,应报告(在本例中为eth0)在给定的接口的IP地址[错误检查为简洁起见省略]
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;
}
然而,这导致在192.168.1.0,而不是正确的192.168.1.100。不同的子网产量10.0.0.0而不是正确10.0.0.107这使我相信的libpcap是不正确复制最后一个字节不同的机器上运行此。我手动转换由pcap_lookupnet归还,以确保它不是与使用INET_NTOA的(我也试过inet_ntop,具有相同的结果)问题的整数。继这个问题code:
在Linux上接口的获得IP地址
报告正确的IP地址。这是libpcap的一个bug或我做错了什么?
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?
推荐答案
您声明,这应报告给定的接口的IP地址是不正确。
Your statement "which should report the IP address of the given interface" is incorrect.
从手册页:
pcap_lookupnet()被用来确定IPv4网络号码和
面具
与网络设备装置相关联。无论NETP和maskp是
bpf_u_int32指针。
pcap_lookupnet() is used to determine the IPv4 network number and mask associated with the network device device. Both netp and maskp are bpf_u_int32 pointers.
你确定你有分别10.0.0.107或192.168.1.100网络号?听起来相当不寻常的。
are you sure you have a network number of 10.0.0.107 or 192.168.1.100 respectively? Sounds rather unusual.
这篇关于pcap_lookupnet返回不正确的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!