【问题标题】:How to lookup wireless device with pcap_lookupdev?如何使用 pcap_lookupdev 查找无线设备?
【发布时间】:2012-10-20 04:44:58
【问题描述】:

这是我的代码,到目前为止。


#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 *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;

    char *dev; /* name of the device to use */

    printf("Asking pcap to find a valid device for use to sniff on.\n");
    dev = pcap_lookupdev(errbuf);
    if(dev == NULL) {
        printf("pcap_lookupdev ERROR:  %s\n",errbuf);
        exit(1);
    }

    printf("Printing out device name.\n");
    printf("DEV: %s\n",dev);

    printf("Asking pcap for the network address and mask of the device.\n");
    ret = pcap_lookupnet(dev,&netp,&maskp,errbuf);
    if(ret == -1) {
        printf("Unable to retrieve the network address and mask of the device.  Error Description:  %s\n",errbuf);
        exit(1);
    }

    printf("Get the network address in a human readable form,\n");
    addr.s_addr = netp;
    net = inet_ntoa(addr);
    if(net == NULL) {
        printf("Unable to retrieve network address in human readable form.\n");
        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) {
        printf("Unable to retrieve device mask in human readable form.  ");
        perror("inet_ntoa");
        exit(1);
    }

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

/*
Output:

Asking pcap to find a valid device for use to sniff on.
Printing out device name.
DEV: eth0
Asking pcap for the network address and mask of the device.
Unable to retrieve the network address and mask of the device.  Error Description:  eth0: no IPv4 address assigned

*/

这是我的问题:我需要做什么才能让 pcap_lookupdev 查找无线设备(即 wlan0)?

【问题讨论】:

    标签: c++ linux wireless libpcap


    【解决方案1】:

    pcap_findalldevsdocumentation 暗示了 pcap_lookupdev() 找不到任何合适的网络设备的原因:

    请注意,可能存在无法打开的网络设备 pcap_open_live() 由进程调用 pcap_findalldevs(),因为, 例如,该进程可能没有足够的权限来打开 它们用于捕获;如果是这样,这些设备将不会出现在列表中

    您可能没有必要的权限来捕获任何可用网络设备上的流量。尝试使用 sudo 运行你的程序,假设你在你的机器上有管理员权限。

    更多信息请参见requirement of root privileges for libpcap functions

    【讨论】:

      【解决方案2】:

      pcap_lookupdev() 将查找一个设备。那可能不是您想要的设备;如果您同时拥有有线和无线接口设备,它很可能会找到有线设备,这不是,因为它不查看无线设备,这是因为它恰好是它找到的第一个设备。

      没有办法告诉它查看无线设备。

      要获取 libpcap 可以处理的所有个设备的列表,请使用pcap_findalldevs()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多