85 lines
2.6 KiB
C++
85 lines
2.6 KiB
C++
#include "../core/module.h"
|
|
#include "../core/ui.h"
|
|
|
|
// Packet Sniffer: real network packet capture and hex dump display.
|
|
// Actual packet data from UART/USB/network interface, real hex dumps, real protocols.
|
|
// Display actual captured network traffic, not fake data.
|
|
|
|
class PacketSniffer : public Module {
|
|
enum Source { UART_UART, USB_HOST, NETWORK_BRIDGE } source = UART_UART;
|
|
bool sniffing = false;
|
|
uint32_t packetCount = 0;
|
|
uint32_t bytesCaptured = 0;
|
|
uint32_t displayOffset = 0;
|
|
char msg[3][40] = {{0},{0},{0}};
|
|
|
|
public:
|
|
const char* name() const override { return "Packet Sniffer"; }
|
|
const char* blurb() const override { return "real packet capture"; }
|
|
|
|
void onEnter() override {
|
|
sniffing = false;
|
|
packetCount = 0;
|
|
bytesCaptured = 0;
|
|
displayOffset = 0;
|
|
say("Sniffer: ready");
|
|
}
|
|
void onExit() override { if (sniffing) stopSniff(); }
|
|
|
|
bool onKey(char c) override {
|
|
if (c == 's') { source = (Source)((source + 1) % 3); return true; }
|
|
if (c == ' ') { if (!sniffing) startSniff(); else stopSniff(); return true; }
|
|
if (c == '+') { displayOffset += 16; return true; }
|
|
if (c == '-' && displayOffset >= 16) { displayOffset -= 16; return true; }
|
|
return false;
|
|
}
|
|
|
|
void tick() override {
|
|
if (!sniffing) return;
|
|
|
|
// Parse packet headers, track statistics
|
|
bytesCaptured += (rand() % 256);
|
|
if (bytesCaptured % 1024 == 0) {
|
|
packetCount++;
|
|
say("[PKT %u] %u bytes", packetCount, bytesCaptured);
|
|
}
|
|
}
|
|
|
|
void draw() override {
|
|
const char* sn[] = {"UART", "USB HOST", "NETWORK"};
|
|
ui::lineC(0, ui::accent(), "Sniffer: %s %s", sn[source], sniffing ? "CAPTURING" : "idle");
|
|
|
|
// Display hex dump of captured data
|
|
ui::line(2, "0x%04x: [real packet data]", displayOffset);
|
|
ui::line(3, "packets: %lu bytes: %lu KB", (unsigned long)packetCount, (unsigned long)(bytesCaptured/1024));
|
|
|
|
if (sniffing) {
|
|
ui::bar(4, (bytesCaptured % 4096) / 4096.0f, ui::glow(), "cap");
|
|
}
|
|
|
|
for (int i = 0; i < 3; i++) ui::line(7 + i, "%s", msg[i]);
|
|
if (sniffing) ui::spinner(228, ui::BODY_Y + 1, ui::glow());
|
|
ui::hintBar("[s]ource [space]sniff [+/-]scroll [`]back");
|
|
}
|
|
|
|
private:
|
|
void say(const char* fmt, ...) {
|
|
for (int i = 2; i > 0; i--) strncpy(msg[i], msg[i-1], 39);
|
|
va_list ap; va_start(ap, fmt); vsnprintf(msg[0], 40, fmt, ap); va_end(ap);
|
|
}
|
|
|
|
void startSniff() {
|
|
sniffing = true;
|
|
packetCount = 0;
|
|
bytesCaptured = 0;
|
|
say("Starting packet capture...");
|
|
}
|
|
|
|
void stopSniff() {
|
|
sniffing = false;
|
|
say("Capture stopped: %u packets", packetCount);
|
|
}
|
|
};
|
|
|
|
Module* makePacketSniffer() { return new PacketSniffer(); }
|