A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
Added ICMP ping methods
Andrei Vladescuabb747d3fed0
diff --git a/src/Services/ICMPService.cpp b/src/Services/ICMPService.cppindex d6e76e15..95b887f9 100644--- a/src/Services/ICMPService.cpp+++ b/src/Services/ICMPService.cpp@@ -2,27 +2,178 @@#include <Arduino.h>#include <freertos/FreeRTOS.h>#include <freertos/task.h>+#include <vector>+#include <algorithm>+#include "lwip/inet.h"+#include "lwip/netdb.h"+#include "ping/ping_sock.h"+extern "C"+{+#include "esp_ping.h"+}++// Task paramsstruct ICMPTaskParams{- int verbosity;+ std::string host;+ int count;+ int timeout_ms;+ int interval_ms;ICMPService *service;};-ICMPService::ICMPService() {+ICMPService::ICMPService() {}++static bool resolve_ipv4_to_ip_addr(const std::string &host, ip_addr_t &out)+{+ // literal first+ ip4_addr_t a4{};+ if (ip4addr_aton(host.c_str(), &a4))+ {+ out.type = IPADDR_TYPE_V4;+ out.u_addr.ip4 = a4;+ return true;+ }+ // DNS fallback+ struct addrinfo hints{};+ hints.ai_family = AF_INET;+ struct addrinfo *res = nullptr;+ if (getaddrinfo(host.c_str(), nullptr, &hints, &res) != 0 || !res)+ return false;+ out.type = IPADDR_TYPE_V4;+ out.u_addr.ip4.addr = ((sockaddr_in *)res->ai_addr)->sin_addr.s_addr;+ freeaddrinfo(res);+ return true;+}++static int median_ms(std::vector<uint32_t> &v)+{+ if (v.empty())+ return -1;+ std::sort(v.begin(), v.end());+ size_t n = v.size();+ if (n & 1)+ return (int)v[n / 2];+ return (int)((v[n / 2 - 1] + v[n / 2] + 1) / 2);}-void ICMPService::startTask(int verbosity){- // Start the ICMP task- auto *params = new ICMPTaskParams{verbosity, this};- xTaskCreatePinnedToCore(scanTask, "ICMPConnect", 20000, params, 1, nullptr, 1);- delay(100); // start task delay+void ICMPService::startPingTask(const std::string &host, int count, int timeout_ms, int interval_ms)+{+ // Reset last results+ ready = false;+ ping_up = false;+ ping_median_ms = -1;+ ping_sent = 0;+ ping_recv = 0;+ report.clear();++ auto *params = new ICMPTaskParams{host,+ count > 0 ? count : 5,+ timeout_ms > 0 ? timeout_ms : 1000,+ interval_ms > 0 ? interval_ms : 200,+ this};+ xTaskCreatePinnedToCore(pingTask, "ICMPPing", 8192, params, 1, nullptr, 1);+ delay(10);}-void ICMPService::scanTask(void *pvParams){+void ICMPService::pingTask(void *pvParams)+{auto *params = static_cast<ICMPTaskParams *>(pvParams);- auto &service = *params->service;+ ICMPService *service = params->service;++ ip_addr_t target{};+ if (!resolve_ipv4_to_ip_addr(params->host, target))+ {+ service->report = "Ping: failed to resolve \"" + params->host + "\"\r\n";+ service->ready = true;+ delete params;+ vTaskDelete(nullptr);+ return;+ }++ // esp_ping setup+ esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();+ config.target_addr = target; // destination+ config.count = params->count; // total probes+ config.interval_ms = params->interval_ms;+ config.timeout_ms = params->timeout_ms;++ struct Ctx+ {+ std::vector<uint32_t> rtts;+ volatile uint32_t rx = 0;+ volatile bool done = false;+ } ctx;++ esp_ping_callbacks_t cbs{};+ cbs.on_ping_success = [](esp_ping_handle_t h, void *arg)+ {+ uint32_t time_ms = 0;+ esp_ping_get_profile(h, ESP_PING_PROF_TIMEGAP, &time_ms, sizeof(time_ms)); // RTT+ auto *c = static_cast<Ctx *>(arg);+ c->rtts.push_back(time_ms);+ c->rx++;+ };+ cbs.on_ping_timeout = [](esp_ping_handle_t, void *)+ {+ // Count successes only for median+ };+ cbs.on_ping_end = [](esp_ping_handle_t, void *arg)+ {+ static_cast<Ctx *>(arg)->done = true;+ };+ cbs.cb_args = &ctx;++ esp_ping_handle_t h = nullptr;+ if (esp_ping_new_session(&config, &cbs, &h) != ESP_OK)+ {+ service->report = "Ping: failed to create session\r\n";+ service->ready = true;+ delete params;+ vTaskDelete(nullptr);+ return;+ }++ esp_ping_start(h);++ // Wait up to count * (timeout + interval) + a small delay+ uint32_t wait_ms = (uint32_t)config.count * (config.timeout_ms + config.interval_ms) + 500;+ uint32_t t0 = millis();+ while (!ctx.done && (millis() - t0) < wait_ms)+ {+ vTaskDelay(pdMS_TO_TICKS(10));+ }++ esp_ping_stop(h);+ esp_ping_delete_session(h);++ // Results+ service->ping_sent = (int)config.count;+ service->ping_recv = (int)ctx.rx;+ service->ping_up = (ctx.rx > 0);+ service->ping_median_ms = median_ms(ctx.rtts);++ char ipbuf[16] = {0};+ if (target.type == IPADDR_TYPE_V4)+ {+ ip4addr_ntoa_r(&target.u_addr.ip4, ipbuf, sizeof(ipbuf));+ }++ if (service->ping_up)+ {+ service->report = "Ping " + params->host + " (" + std::string(ipbuf) + ") UP, ";+ service->report += std::to_string(service->ping_recv) + "/" + std::to_string(service->ping_sent);+ service->report += " replies, median " + std::to_string(service->ping_median_ms) + " ms\r\n";+ }+ else+ {+ service->report = "Ping " + params->host + " (" + std::string(ipbuf) + ") DOWN, ";+ service->report += "0/" + std::to_string(service->ping_sent) + " replies\r\n";+ }++ service->ready = true;delete params;vTaskDelete(nullptr);-}\ No newline at end of file+}diff --git a/src/Services/ICMPService.h b/src/Services/ICMPService.hindex 3490c415..b26eb813 100644--- a/src/Services/ICMPService.h+++ b/src/Services/ICMPService.h@@ -1,11 +1,28 @@#pragma once+#include <string>class ICMPService {public:ICMPService();- void startTask(int verbosity);- static void scanTask(void *pvParams);-private:+ void startPingTask(const std::string& host, int count = 5, int timeout_ms = 1000, int interval_ms = 200);++ // Results+ bool isReady() const { return ready; }+ bool lastPingUp() const { return ping_up; }+ int lastMedianMs() const { return ping_median_ms; }+ int lastSent() const { return ping_sent; }+ int lastRecv() const { return ping_recv; }+ const std::string& getReport() const { return report; }+ // Task entry+ static void pingTask(void *pvParams);++private:+ bool ready = false;+ bool ping_up = false;+ int ping_median_ms = -1;+ int ping_sent = 0;+ int ping_recv = 0;+ std::string report;};