Esp8266 Pcap SD - Ino
Esp8266 Pcap SD - Ino
===========================================
Copyright (c) 2017 Stefan Kremser
github.com/spacehuhn
===========================================
PLEASE NOTE:
The ESP8266's promiscuous mode is usable, but will return mostly malformed
packets and packets that are cut off after x bytes.
This example is more of a proof-of-concept. If you want something more
reliable, please have a look at the ESP32.
*/
extern "C" {
#include "user_interface.h"
}
/* will be executed on every packet the ESP8266 gets while beeing in promiscuous
mode */
void sniffer(uint8_t *buf, uint16_t len) {
if(fileOpen){
uint32_t timestamp = now(); //current timestamp
uint32_t microseconds = (unsigned int)(micros() - millis() * 1000); //micro
seconds offset (0 - 999)
pcap.newPacketSD(timestamp, microseconds, len, buf); //write packet to file
}
}
/* open a new file */
void openFile(){
Serial.println("opened: "+filename);
/* start Serial */
Serial.begin(115200);
delay(2000);
Serial.println();
Serial.println("starting...");
/* initialize SD card */
if(!SD.begin()) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
openFile();
/* setup wifi */
wifi_set_opmode(STATION_MODE); //put the ESP into Station-Mode
wifi_promiscuous_enable(0); //disable promiscuous first
WiFi.disconnect(); //disconnect any WiFi connection just to be safe
wifi_set_promiscuous_rx_cb(sniffer); //set callback function for our sniffer
wifi_set_channel(ch); //switch to our selected channel
wifi_promiscuous_enable(1); //enable promiscuous
Serial.println("Sniffer started!");
/* Channel Hopping */
if(CHANNEL_HOPPING){
if(currentTime - lastChannelChange >= HOP_INTERVAL){
lastChannelChange = currentTime;
ch++; //increase channel
if(ch > MAX_CHANNEL) ch = 1;
wifi_set_channel(ch); //switch to new channel
Serial.println( "Switched to channel " + (String)ch );
}
}