【问题标题】:libpcap and wireless signal capturelibpcap 和无线信号捕获
【发布时间】:2015-02-27 18:43:07
【问题描述】:

我正在尝试编写一个 c++ 应用程序 (linux),它将捕获无线数据包以及相关的信号强度(以 dBm 为单位)。捕获部分很简单,但问题是我找不到任何关于如何获取每个数据包的信号强度的文档。

它是标题的一部分吗?

这是我目前所拥有的:

printf("Device: %s\n", dev);
printf("Number of packets: %d\n", num_packets);
printf("Filter expression: %s\n", filter_exp);

/* open capture device */
    pcap_t *handler = pcap_create("wlan0", errbuf);
    if (handler == NULL)
    {
        exit(-1);
    }
    if(pcap_set_rfmon(handler,1)==0 )
    {
        printf("monitor mode enabled\n");
    }
    pcap_set_snaplen(handler, 2048);  // Set the snapshot length to 2048
    pcap_set_promisc(handler, 0); // Turn promiscuous mode off
    pcap_set_timeout(handler, 512); // Set the timeout to 512 milliseconds
    int status = pcap_activate(handler);


/* now we can set our callback function */
pcap_loop(handle, num_packets, got_packet, NULL);

这里是 got_packet 代码:

/* define ethernet header */
ethernet = (struct sniff_ethernet*)(packet);

/* define/compute ip header offset */
ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
size_ip = IP_HL(ip)*4;
if (size_ip < 20) {
    printf("   * Invalid IP header length: %u bytes\n", size_ip);
    return;
}

/* print source and destination IP addresses */
printf("       From: %s\n", inet_ntoa(ip->ip_src));
printf("         To: %s\n", inet_ntoa(ip->ip_dst));

/* determine protocol */    
switch(ip->ip_p) {
    case IPPROTO_TCP:
        printf("   Protocol: TCP\n");
        break;
    case IPPROTO_UDP:
        printf("   Protocol: UDP\n");
        return;
    case IPPROTO_ICMP:
        printf("   Protocol: ICMP\n");
        return;
    case IPPROTO_IP:
        printf("   Protocol: IP\n");
        return;
    default:
        printf("   Protocol: unknown\n");
        return;
}

/* define/compute tcp header offset */
tcp = (struct sniff_tcp*)(packet + SIZE_ETHERNET + size_ip);
size_tcp = TH_OFF(tcp)*4;
if (size_tcp < 20) {
    printf("   * Invalid TCP header length: %u bytes\n", size_tcp);
    return;
}

printf("   Src port: %d\n", ntohs(tcp->th_sport));
printf("   Dst port: %d\n", ntohs(tcp->th_dport));

/* define/compute tcp payload (segment) offset */
payload = (char *)(packet + SIZE_ETHERNET + size_ip + size_tcp);

/* compute tcp payload (segment) size */
size_payload = ntohs(ip->ip_len) - (size_ip + size_tcp);

/*
 * Print payload data; it might be binary, so don't just
 * treat it as a string.
 */
if (size_payload > 0) {
    printf("   Payload (%d bytes):\n", size_payload);
    //print_payload(payload, size_payload);
}

任何帮助将不胜感激。

更新: /******************/ 这是一个更新: 因此,根据我的研究,正如盖伊斯科特所说,我正在寻找错误的信息。我需要查看无线数据包,而是加载以太网数据包。所以这里是更新的代码:

 pcap_set_snaplen(handle, 2048);  // Set the snapshot length to 2048
    pcap_set_promisc(handle, 1);     // Turn promiscuous mode off
    pcap_set_timeout(handle, 512);   // Set the timeout to 512 milliseconds
    int status = pcap_activate(handle);

    if(pcap_set_datalink(handle, DLT_IEEE802_11_RADIO) == -1) {
        printf("Couldn't set datalink type %s: %s\n", device, pcap_geterr(handle));
    }

所以现在的问题是解析数据包,这似乎是一个相当困难的问题。我对源地址、目标地址和相关信号等感兴趣。我不知道如何匹配和加载数据包中的数据并将其与 radiotap 结构匹配。

struct ieee80211_radiotap_header {
u_int8_t    it_version; /* set to 0 */
u_int8_t    it_pad;
u_int16_t   it_len; /* entire length */
u_int32_t   it_present; /* fields present */
} __attribute__((__packed__));

/* Presence bits */
#define RADIOTAP_TSFT   0
#define RADIOTAP_FLAGS  1
#define RADIOTAP_RATE   2
#define RADIOTAP_CHANNEL    3
#define RADIOTAP_FHSS   4
#define RADIOTAP_ANTENNA_SIGNAL 5
#define RADIOTAP_ANTENNA_NOISE  6
#define RADIOTAP_LOCK_QUALITY   7
#define RADIOTAP_TX_ATTENUATION 8
#define RADIOTAP_DB_TX_ATTENUATION  9
#define RADIOTAP_DBM_TX_POWER   10
#define RADIOTAP_ANTENNA    11
#define RADIOTAP_DB_ANTENNA_SIGNAL  12

void process_packet (u_char * args, const struct pcap_pkthdr *header, const u_char * packet)
{
    struct ieee80211_radiotap_header *packet_header = (struct ieee80211_radiotap_header *) header;
    // This is where I am stuck

一旦我捕获了数据包,有人可以告诉我,如何从中提取上述值?

谢谢

【问题讨论】:

  • 好吧,经过多一点研究,我意识到我做错了。所以我要捕获的数据包实际上不是以太网数据包,而是wifi数据包。 wifi数据包有一个信号字段。

标签: c++ wireless libpcap packet-capture


【解决方案1】:

每个调用pcap_open_live()pcap_create()/pcap_activate()pcap_open_offline()的程序都应该调用pcap_datalink()来找出捕获的链路层头类型是什么。

这条规则没有个例外。

然后查看the link-layer header types page for tcpdump.org 以了解pcap_datalink() 返回的值的含义。将它们与那里列出的DLT_ 值进行比较。您可能会得到DLT_IEEE802_11,它没有信号强度信息,以及DLT_PRISM_HEADERDLT_IEEE802_11_RADIODLT_IEEE802_11_RADIO_AVS,它们有信号强度信息。有关信号强度信息(和其他无线电元数据)如何在数据包数据中表示的信息,请参阅后三种可能性的链接。

(而且,是的,这是无线电元数据的 三个 选项,因此另一个答案中给出的链接指向不完整的来源;大多数情况下,您可能会得到 radiotap 标头,而不是 AVS 或 Prism 标头。Radiotap 更通用,因为它被设计为可扩展的,但解析起来更复杂。)

【讨论】:

    【解决方案2】:

    我偶然发现了同样的问题,我想我应该分享我的解决方案。 信号强度不在PCAP回调的header内,而是在packet内。对于DLT_IEEE802_11_RADIO,信号和噪声可能是:

    void process_packet (u_char * args,
                         const struct pcap_pkthdr *header,
                         const u_char * packet){
        int8_t signal_dbm = packet[22];
        int8_t noise_dbm = packet[23];
        ....
    }
    

    但我通过查看 WireShark 找到了 2223

    更好的解决方案当然是使用radiotap (GitHub)。

    这是一个例子:

    void process_packet (u_char * args,
                         const struct pcap_pkthdr *header,
                         const u_char * packet) {
        struct ieee80211_radiotap_iterator iterator;
        int ret = ieee80211_radiotap_iterator_init(&iterator, packet, header->len, NULL);
        while (!ret) {
            ret = ieee80211_radiotap_iterator_next(&iterator);
            if (ret) break;
            switch (iterator.this_arg_index) {
                case IEEE80211_RADIOTAP_TSFT:
                    printf("timestamp: %lu\n", (uint64_t)*iterator.this_arg);
                    break;
                case IEEE80211_RADIOTAP_DBM_ANTSIGNAL:
                    printf("sig: %d\n", (int8_t)*iterator.this_arg);
                    break;
                case IEEE80211_RADIOTAP_DBM_ANTNOISE:
                    printf("noise: %d\n", (int8_t)*iterator.this_arg);
                    break;
                default:
                    break;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 2011-08-08
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      相关资源
      最近更新 更多