Packet sniffing too slow?

rado555
Posts: 1
Joined: Sat Dec 03, 2022 12:05 pm

Packet sniffing too slow?

Postby rado555 » Sat Dec 03, 2022 12:13 pm

I've simplified packet sniffing example just to measure utilization of the wifi network so my packet callback just counts the packets on fixed channel (obtained from PC connected to the same network). Then it prints the packets count and data size once per second. When I run speedtest on other device which downloads data at 30mbit/s, I'm getting very low numbers of packets. What can be the problem?
Output of the sketch looks like this while testing the data download at 30mbit/s:
*** Data:11 Ctrl:0 Mgmt:9 Misc:0 data=322 B/s
*** Data:24 Ctrl:0 Mgmt:9 Misc:0 data=916 B/s
*** Data:166 Ctrl:0 Mgmt:9 Misc:0 data=5002 B/s
*** Data:41 Ctrl:0 Mgmt:10 Misc:0 data=1263 B/s
*** Data:19 Ctrl:0 Mgmt:10 Misc:0 data=570 B/s
*** Data:155 Ctrl:0 Mgmt:10 Misc:0 data=4604 B/s
*** Data:14 Ctrl:0 Mgmt:8 Misc:0 data=416 B/s
*** Data:12 Ctrl:0 Mgmt:9 Misc:0 data=360 B/s
*** Data:20 Ctrl:0 Mgmt:10 Misc:0 data=677 B/s
*** Data:14 Ctrl:0 Mgmt:10 Misc:0 data=420 B/s
*** Data:29 Ctrl:0 Mgmt:8 Misc:0 data=836 B/s

Complete code:

Code: Select all

#include <WiFi.h>

#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"

#include <string.h>
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"

#include "lwip/err.h"
#include "lwip/sys.h"


uint8_t level = 0, channel = 1;

static wifi_country_t wifi_country = { .cc = "CN", .schan = 1, .nchan = 13 };  //Most recent esp32 library struct

typedef struct {
  unsigned frame_ctrl : 16;
  unsigned duration_id : 16;
  uint8_t addr1[6]; /* receiver address */
  uint8_t addr2[6]; /* sender address */
  uint8_t addr3[6]; /* filtering address */
  unsigned sequence_ctrl : 16;
  uint8_t addr4[6]; /* optional */
} wifi_ieee80211_mac_hdr_t;

typedef struct {
  wifi_ieee80211_mac_hdr_t hdr;
  uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */
} wifi_ieee80211_packet_t;


volatile unsigned int cntMgmt = 0;
volatile unsigned int cntData = 0;
volatile unsigned int cntCtrl = 0;
volatile unsigned int cntMisc = 0;
volatile unsigned long lenData = 0;

void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type) {
  const wifi_promiscuous_pkt_t *ppkt = (wifi_promiscuous_pkt_t *)buff;

  switch (type) {
    case WIFI_PKT_MGMT: ++cntMgmt; break;
    case WIFI_PKT_CTRL: ++cntCtrl; break;
    case WIFI_PKT_DATA:
      ++cntData;
      lenData += ppkt->rx_ctrl.sig_len;
      break;
    default: ++cntMisc; break;
  }
}

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  delay(10);


  nvs_flash_init();
  tcpip_adapter_init();
  //ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
  wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  ESP_ERROR_CHECK(esp_wifi_init(&cfg));
  ESP_ERROR_CHECK(esp_wifi_set_country(&wifi_country)); /* set country for channel range [1, 13] */
  ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
  ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));


  ESP_ERROR_CHECK(esp_wifi_start());
  esp_wifi_set_promiscuous(true);
  esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler);

  esp_wifi_set_channel(6, WIFI_SECOND_CHAN_NONE);
}

void loop() {
  delay(1000);  // wait for a second

  auto rate = lenData;

  printf("*** Data:%u Ctrl:%u Mgmt:%u Misc:%u   data=%lu B/s\n", cntData, cntCtrl, cntMgmt, cntMisc, lenData);

  cntData = 0;
  cntCtrl = 0;
  cntMgmt = 0;
  cntMisc = 0;
  lenData = 0;
}


Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 131 guests