c - 来自 ifconfig 和 libpcap 的接口(interface) ip 不匹配

标签 c pcap

这是我在 stackoverflow 上的第一篇文章。

我正在用 libpcap (1.2.1) 做一个数据包分析器,我成功地运行了一些示例并且我已经编写了我的分析器的一部分,然后我意识到一件事。

我的 ifconfig 是这样说的:

wlan0     Link encap:Ethernet  HWaddr 90:00:4e:96:80:b1  
          inet addr:192.168.100.100  Bcast:192.168.100.255  Mask:255.255.255.0
          inet6 addr: fe80::9200:4eff:fe96:80b1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:304853 errors:0 dropped:0 overruns:0 frame:0
          TX packets:279099 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:403507845 (403.5 MB)  TX bytes:28330813 (28.3 MB)

如您所见,IP 地址是 192.168.100.100

然后我再次编译示例以显示开发名称、网络掩码和 ip,这个示例使用 libpcap,我得到了这个:

DEV: wlan0
NET: 192.168.100.0
MASK: 255.255.255.0

我认为这是不正确的

该示例的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h> 
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    char *dev; /* name of the device to use */ 
    char *net; /* dot notation of the network address */
    char *mask;/* dot notation of the network mask    */
    int ret;   /* return code */
    char errbuf[PCAP_ERRBUF_SIZE];
    bpf_u_int32 netp; /* ip          */
    bpf_u_int32 maskp;/* subnet mask */
    struct in_addr addr;

  /* ask pcap to find a valid device for use to sniff on */
    dev = pcap_lookupdev(errbuf);

  /* error checking */
    if(dev == NULL)
    {
        printf("%s\n",errbuf);
        exit(1);
    } 

  /* print out device name */
    printf("DEV: %s\n",dev);

  /* ask pcap for the network address and mask of the device */
    ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);

    if(ret == -1)
    {
        printf("%s\n",errbuf);
        exit(1);
    }

  /* get the network address in a human readable form */
    addr.s_addr = netp;
    net = inet_ntoa(addr);

    if(net == NULL)/* thanks Scott :-P */
    {
        perror("inet_ntoa");
        exit(1);
    }

    printf("NET: %s\n",net);

  /* do the same as above for the device's mask */
    addr.s_addr = maskp;
    mask = inet_ntoa(addr);

    if(mask == NULL)
    {
        perror("inet_ntoa");
        exit(1);
    }

    printf("MASK: %s\n",mask);

    return 0;
}

怎么了?

提前致谢

最佳答案

正如代码所说,192.168.100.0网络 地址,而不是您的 IP 地址。网络地址是通过按位与您的 IP 地址和网络掩码生成的。

关于c - 来自 ifconfig 和 libpcap 的接口(interface) ip 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9434587/

相关文章:

将 malloc() 调用转换为 C 中的外部库调用

c - 在函数中使用结构数组?

c - 数组初始化需要花括号

c - GCC - 打印结构的定义

endianness - pcap 文件和字节顺序

c - 如何表示用C分隔的一位十进制数

c - 使用 pcap 示例编程

java - 使用 jNetPcap 在服务器上解码 HTTPS 请求负载

c++ - 将数据包附加到当前 PCAP 文件

ruby - PacketFu 在 Ubuntu 14.04 上的线程内捕获