选择频道和频率以监控pcap

时间:2018-07-02 21:59:02

标签: c pcap libpcap

我有以下用于监视无线AP的Pcap代码,它正在工作,它正在输出SSID,但是在启动程序时会选择一个随机的频率和信道。如何在不使用iw之类的外部程序的情况下以编程方式选择频道和频率?

struct bpf_program fp; 
bpf_u_int32 netp;
char *errbuf;
char *filter = "type mgt subtype beacon";

if((handle = pcap_create(dev, errbuf)) == NULL)
{
     notify("[0]pcap create error");
     exit(EXIT_FAILURE);
}

/*
 * Check if device can be put into monitor mode
 */
if(pcap_can_set_rfmon(handle) == 0)
{
    notify("[0]Monitor mode can not be set.\n");
    exit(EXIT_FAILURE);
}

/*
 * Attempt to put the card into monitor mode
 */
if(pcap_set_rfmon(handle, 1) != 0)
{
    notify("[0]Failed to set monitor mode.\n");
    exit(EXIT_FAILURE);
}

pcap_set_snaplen(handle, 2048);  // Set the snapshot length to 2048
pcap_set_promisc(handle, 0); // Turn promiscuous mode off
pcap_set_timeout(handle, 512);

if(pcap_activate(handle) != 0)
{
    notify("[0]pcap_activate() failed\n");
    exit(EXIT_FAILURE);
}

/*
 * Compile a filter to sniff 802.11 probe requests
 * type mgt subtype probe-req
 */
if(pcap_compile(handle, &fp, filter, 0, PCAP_NETMASK_UNKNOWN) == -1)
{
    notify("[0]filter compile error");
    exit(EXIT_FAILURE);
}

/*
 * Set the compiled filter
 */
if(pcap_setfilter(handle, &fp) == -1)
{
    notify("[0]set filter error");
    exit(EXIT_FAILURE);
}

status = 1;

pcap_loop(handle, -1, got_wifi_packet, NULL);

pcap_set_rfmon(handle, 0);
pcap_freecode(&fp);
pcap_close(handle);

0 个答案:

没有答案