-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathESPShark.cpp
86 lines (77 loc) · 2.64 KB
/
ESPShark.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* This file is part of the Capibara zero
* project(https://capibarazero.github.io/).
*
* Copyright (c) 2024 Andrea Canale.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ESPShark.hpp"
#include "esp_private/wifi.h"
#include "PCAP.h"
#include <TimeLib.h>
static PCAP pcap = PCAP();
static unsigned long int last_save = millis();
unsigned long int sniffed_packets = 0;
static esp_err_t wifi_recv_callback(void *buffer, uint16_t len, void *eb)
{
uint32_t timestamp = now(); // current timestamp
uint32_t microseconds = (unsigned int)(micros() - millis() * 1000); // micro seconds offset (0 - 999)
pcap.newPacketSD(timestamp, microseconds, len, (uint8_t *)buffer);
sniffed_packets++;
if (millis() - last_save >= 2000) {
pcap.flushFile();
last_save = millis();
}
esp_wifi_internal_free_rx_buffer(eb);
return ESP_OK;
}
ESPShark::ESPShark(const char *filename) {
File fptr = SD.open(filename, FILE_WRITE);
fptr.close();
pcap.filename = filename;
pcap.network = 1;
pcap.openFile(SD);
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, wifi_recv_callback);
}
ESPShark::ESPShark(const char *filename, wifi_interface_t interface) {
File fptr = SD.open(filename, FILE_WRITE);
fptr.close();
pcap.filename = filename;
pcap.network = 1;
pcap.openFile(SD);
esp_wifi_internal_reg_rxcb(interface, wifi_recv_callback);
}
ESPShark::ESPShark(const char *filename, wifi_rxcb_t cb) {
File fptr = SD.open(filename, FILE_WRITE);
fptr.close();
pcap.filename = filename;
pcap.network = 1;
pcap.openFile(SD);
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, cb);
}
ESPShark::ESPShark(const char *filename, wifi_rxcb_t cb, wifi_interface_t interface) {
File fptr = SD.open(filename, FILE_WRITE);
fptr.close();
pcap.filename = filename;
pcap.network = 1;
pcap.openFile(SD);
esp_wifi_internal_reg_rxcb(interface, cb);
}
ESPShark::~ESPShark() {
pcap.closeFile();
esp_wifi_internal_reg_rxcb(WIFI_IF_STA, NULL);
}
unsigned long int ESPShark::get_sniffed_packets() {
return sniffed_packets;
}