我有一個問題,其中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
什麼錯誤你好嗎和哪裏? –
我沒有收到錯誤,我收到的任何錯誤,例如顯示器模式和過濾器,都是因爲我的無線設備被報告具有以太網鏈接層(這是此問題的標題)......無法使用監視模式。 –
'en0'應該是以太網接口的名稱,而不是無線接口。在這種情況下,如果你問PCAP的鏈接類型,那麼'LINKTYPE_ETHERNET'(== 1)是一個非常合理的結果。現在幾乎每臺機器都有一個以太網接口,包括那些也有無線接口的機器。你確定你正在看你打算看的界面嗎? –