Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

ICMP Discovery responsive UI

Andrei Vladescube277df81c0f
diff --git a/src/Abstracts/ANetworkController.cpp b/src/Abstracts/ANetworkController.cpp
index 2d548266..fcb4cbcf 100644
--- a/src/Abstracts/ANetworkController.cpp
+++ b/src/Abstracts/ANetworkController.cpp
@@ -125,10 +125,37 @@ void ANetworkController::handleDiscovery(const TerminalCommand &cmd)
const std::string deviceIP = phy_interface == phy_interface_t::phy_wifi ? wifiService.getLocalIP() : ethernetService.getLocalIP();
icmpService.startDiscoveryTask(deviceIP);
+
while (!icmpService.isReady()) {
- vTaskDelay(pdMS_TO_TICKS(50));
+ terminalView.print("HERE");
+ // Display logs
+ auto batch = icmpService.fetchICMPLog();
+ for (auto& ln : batch) {
+ terminalView.println(ln);
+ }
+
+ // Enter Press to stop
+ int terminalKey = terminalInput.readChar();
+ if (terminalKey == '\n' || terminalKey == '\r') {
+ icmpService.stopICMPService();
+ break;
+ }
+ char deviceKey = deviceInput.readChar();
+ if (deviceKey == KEY_OK) {
+ icmpService.stopICMPService();
+ break;
+ }
+
+ vTaskDelay(pdMS_TO_TICKS(100));
}
- terminalView.println(icmpService.getReport());
+
+ // Flush final logs
+ for (auto& ln : icmpService.fetchICMPLog()) {
+ terminalView.println(ln);
+ }
+
+ ICMPService::clearICMPLogging();
+ //terminalView.println(icmpService.getReport());
}
/*
diff --git a/src/Services/ICMPService.cpp b/src/Services/ICMPService.cpp
index 13abb264..c1346d6c 100644
--- a/src/Services/ICMPService.cpp
+++ b/src/Services/ICMPService.cpp
@@ -13,6 +13,10 @@ extern "C"
#include "esp_ping.h"
}
+portMUX_TYPE ICMPService::icmpMux = portMUX_INITIALIZER_UNLOCKED;
+std::vector<std::string> ICMPService::icmpLog;
+bool ICMPService::stopICMPFlag = false;
+
// Task params
struct ICMPTaskParams
{
@@ -90,8 +94,11 @@ void ICMPService::startDiscoveryTask(const std::string deviceIP)
TaskHandle_t pingTaskHandle = nullptr;
uint32_t targetsResponded = 0;
+ pushICMPLog("Discovery: scanning network for devices...");
+
if (!ip4addr_aton(deviceIPcopy.c_str(), &targetIP))
{
+ pushICMPLog("Discovery: failed to parse IP address " + deviceIP);
report = "Discovery: failed to parse IP address " + deviceIP + "\r\n";
return;
}
@@ -104,6 +111,13 @@ void ICMPService::startDiscoveryTask(const std::string deviceIP)
for (uint8_t targetIndex = 1; targetIndex < 255; targetIndex++)
{
+ if (ICMPService::getICMPServiceStatus() == true)
+ {
+ pushICMPLog("Discovery: stopped by user");
+ report = "Discovery: stopped by user\r\n";
+ return;
+ }
+
if (targetIndex == deviceIndex)
continue;
@@ -124,14 +138,19 @@ void ICMPService::startDiscoveryTask(const std::string deviceIP)
vTaskDelay(pdMS_TO_TICKS(10));
}
if (this->pingRC == ping_rc_t::ping_ok){
+ pushICMPLog("Device found: " + targetIPStr);
deviceDiscoveryReport += "Device found: " + targetIPStr + "\r\n";
targetsResponded++;
}
deviceDiscoveryReport.append(report);
}
+ pushICMPLog(std::to_string(targetsResponded) + " devices up, pinged " +
+ std::to_string(254 - targetsResponded) + " devices down");
+
deviceDiscoveryReport += std::to_string(targetsResponded) + " devices up, pinged " +
std::to_string(254 - targetsResponded) + " devices down\r\n";
+
// Put all the results here, can be called with getReport
report = std::move(deviceDiscoveryReport);
ready = true;
@@ -257,3 +276,34 @@ void ICMPService::pingAPI(void *pvParams)
delete params;
vTaskDelete(nullptr);
}
+
+void ICMPService::clearICMPLogging() {
+ portENTER_CRITICAL(&icmpMux);
+ icmpLog.clear();
+ stopICMPFlag = false;
+ portEXIT_CRITICAL(&icmpMux);
+}
+
+void ICMPService::pushICMPLog(const std::string& line) {
+ portENTER_CRITICAL(&icmpMux);
+ icmpLog.push_back(line);
+ if (icmpLog.size() > ICMP_LOG_MAX) {
+ size_t excess = icmpLog.size() - ICMP_LOG_MAX;
+ icmpLog.erase(icmpLog.begin(), icmpLog.begin() + excess);
+ }
+ portEXIT_CRITICAL(&icmpMux);
+}
+
+std::vector<std::string> ICMPService::fetchICMPLog() {
+ std::vector<std::string> batch;
+ portENTER_CRITICAL(&ICMPService::icmpMux);
+ batch.swap(ICMPService::icmpLog);
+ portEXIT_CRITICAL(&ICMPService::icmpMux);
+ return batch;
+}
+
+void ICMPService::stopICMPService(){
+ portENTER_CRITICAL(&ICMPService::icmpMux);
+ ICMPService::stopICMPFlag = true;
+ portEXIT_CRITICAL(&ICMPService::icmpMux);
+}
\ No newline at end of file
diff --git a/src/Services/ICMPService.h b/src/Services/ICMPService.h
index 6332a14b..e83c8d3c 100644
--- a/src/Services/ICMPService.h
+++ b/src/Services/ICMPService.h
@@ -1,5 +1,7 @@
#pragma once
#include <string>
+#include <vector>
+#include <freertos/FreeRTOS.h>
enum phy_interface_t {
phy_none,
@@ -38,6 +40,11 @@ public:
// Task entry
static void pingAPI(void *pvParams);
+ // Responsive ICMP logging
+ std::vector<std::string> fetchICMPLog();
+ static void clearICMPLogging();
+ static void stopICMPService();
+
private:
bool ready = false;
int pingMedianMs = -1;
@@ -46,5 +53,14 @@ private:
std::string report;
ping_rc_t pingRC = ping_rc_t::ping_error;
+ // Log buffer thread safe
+ static portMUX_TYPE icmpMux;
+ static std::vector<std::string> icmpLog;
+ static constexpr size_t ICMP_LOG_MAX = 200;
+ static bool stopICMPFlag;
+
+ static void pushICMPLog(const std::string& line);
+ static bool getICMPServiceStatus() { return stopICMPFlag; }
+ // Clears non-static variables
void cleanupICMPService();
};