2015-09-02 24 views
3

我有一個問題,其中pcap_datalink()總是返回1。據我瞭解這是LINKTYPE_ETHERNET。但是,我使用的設備是無線網卡,在我的情況下是en0爲什麼pcap_datalink()總是返回1(以太網),即使在無線設備上?

這阻止了我將卡放入監視器模式,並停止WLAN濾波器的工作。我試着在OSX和Linux上運行這個結果相同的結果。我也以root身份運行。

下面是導致問題的代碼部分。例如,假設dev設置爲en0(Mac上的無線設備)。

#include <stdio.h> 
#include <pcap.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]) 
{ 
    pcap_t *pcap_h; 
    char *dev, errbuf[PCAP_ERRBUF_SIZE]; 
    struct bpf_program fp; 
    struct pcap_pkthdr header; 
    const u_char *packet; 

    if(argc < 2) 
    { 
     printf("Usage: %s device\n", argv[0]); 
     exit(EXIT_FAILURE); 
    } 

    dev = argv[1]; 

    if((pcap_h = pcap_create(dev, errbuf)) == NULL) 
    { 
     printf("pcap_create() failed: %s\n", errbuf); 
     exit(EXIT_FAILURE); 
    } 

    if(pcap_can_set_rfmon(pcap_h) == 0) 
    { 
     printf("Monitor mode can not be set.\n"); 
    } 

    if(pcap_set_rfmon(pcap_h, 1) != 0) 
    { 
     printf("Failed to set monitor mode.\n"); 
     exit(EXIT_FAILURE); 
    } 

    if(pcap_activate(pcap_h) != 0) 
    { 
     printf("pcap_activate() failed\n"); 
     exit(EXIT_FAILURE); 
    } 

    /* 
    * Compile a filter to sniff 802.11 probe requests 
    * Filter: type mgt subtype probe-req 
    */ 
    if(pcap_compile(pcap_h, &fp, "type mgt subtype probe-req", 0, PCAP_NETMASK_UNKNOWN) == -1) 
    { 
     printf("pcap_compile() failed: %s\n", pcap_geterr(pcap_h)); 
     exit(EXIT_FAILURE); 
    } 

    /* 
    * Set the compiled filter 
    */ 
    if(pcap_setfilter(pcap_h, &fp) == -1) 
    { 
     printf("pcap_setfilter() failed: %s\n", pcap_geterr(pcap_h)); 
     exit(EXIT_FAILURE); 
    } 

    pcap_freecode(&fp); 

    packet = pcap_next(pcap_h, &header); 

    printf("Header: %d\n", header.len); 
    pcap_close(pcap_h); 
    return 0; 
} 

任何想法就是爲什麼pcap_datalink()總是返回1

編輯

更新後的代碼,並調用pcap_activate()前加入pcap_set_rfmon()。我得到一個錯誤:

pcap_compile() failed: 802.11 link-layer types supported only on 802.11 
+0

什麼錯誤你好嗎和哪裏? –

+0

我沒有收到錯誤,我收到的任何錯誤,例如顯示器模式和過濾器,都是因爲我的無線設備被報告具有以太網鏈接層(這是此問題的標題)......無法使用監視模式。 –

+0

'en0'應該是以太網接口的名稱,而不是無線接口。在這種情況下,如果你問PCAP的鏈接類型,那麼'LINKTYPE_ETHERNET'(== 1)是一個非常合理的結果。現在幾乎每臺機器都有一個以太網接口,包括那些也有無線接口的機器。你確定你正在看你打算看的界面嗎? –

回答

1

舒爾這是何等的是從把卡插入監控模式阻止你,並從工作停止你的WLAN的過濾器,或做你該做這個調用pcap_datalink()作爲試圖找出問題的檢查?

注意的是,從PCAP-LINKTYPE(7):

For a live capture or ``savefile'', libpcap supplies, as the return value of the pcap_datalink(3PCAP) routine, a value that indicates the type of link-layer header at the beginning of the packets it provides. This is not necessarily the type of link-layer header that the packets being captured have on the network from which they're being captured; for example, packets from an IEEE 802.11 network might be provided by libpcap with Ethernet headers that the network adapter or the network adapter driver generates from the 802.11 headers.

,所以我不會拿這個LINKTYPE_ETHERNET/DLT_EN10MB返回值是一個問題的肯定指示在這裏。

編輯: 此外,pcap_set_rfmon()應該是叫前把手被激活,這是不是在你的代碼可見。

pcap對於應該做的事情是很敏感的。查看pcap_can_set_rfmonpcap_set_rfmon的手冊頁。

的順序應該是:

  • pcap_create
  • pcap_can_set_rfmon
  • pcap_set_rfmon(如果到目前爲止好)
  • 那麼只有到那時,pcap_activate
+0

更新的代碼。仍然面臨問題。 –

+0

有趣。但我不明白你怎麼稱呼pcap_set_rfmon(pcap_h,0)。從手冊頁,我寧願做pcap_set_rfmon(pcap_h,1):手冊頁上說:「如果rfmon(第二個參數)非零,將設置監視器模式,否則不會設置。」此外,在追逐這個問題的這一點上,我會嘗試pcap_can_set_rfmon()。 – jbm

+0

我試過'pcap_can_set_rfmon()'。將更新代碼在那裏顯示它,並且可能最好我保存在那裏:)。我用'1'嘗試過,仍然有同樣的問題。 –

相關問題