2011-07-28 2 views
1

ここまでは私のコードです。pcap_lookupdevで無線デバイスを検索する方法は?


#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)を検索するにはどうすればよいですか?

答えて

2

documentationためpcap_findalldevs pcap_lookupdev()任意の適切なネットワークデバイスを見つけていない理由でヒント: は、pcap_open_liveで開くことができないネットワークデバイスがあるかもしれないことを

注()たとえば、 のプロセスがpcap_findalldevs()を呼び出しているため、そのプロセスにはの特権がありません。 キャプチャ用に開きます。その場合、それらのデバイスはリストに表示されません。

使用可能なネットワークデバイスのトラフィックをキャプチャするために必要なアクセス権を持っていない可能性があります。マシンに管理者権限があると仮定して、sudoでプログラムを実行してみてください。

詳細については、requirement of root privileges for libpcap functionsを参照してください。

0

pcap_lookupdev()は、のデバイス番号を検索します。それはあなたが望むデバイスではないかもしれません。有線と無線の両方のインタフェースデバイスをお持ちの場合は、有線デバイスが見つかる可能性があります。ではありません。ワイヤレスデバイスが見つからないため、見つかった最初のデバイスです。

無線デバイスでのみと表示する方法はありません。

のリストを取得するには、libpcapで処理できるすべてのデバイスpcap_findalldevs()にします。

関連する問題