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,结果相同).遵循此问题的代码:
Get IP address of an interface on Linux
报告正确的IP地址.这是libpcap中的错误,还是我做错了什么?

解决方法:

您的陈述“应该报告给定接口的IP地址”是不正确的.

从联机帮助页:

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吗?听起来很不寻常.

上一篇:【M365运维】Outlook会议室查找工具找不到会议室


下一篇:访达在 Mac 上没有响应如何修复?