From e27e3260d735b6c6a0cc9dad5f3531370b6556ee Mon Sep 17 00:00:00 2001 From: dfeen87 <158860247+dfeen87@users.noreply.github.com> Date: Mon, 4 May 2026 12:15:32 +0000 Subject: [PATCH] Add Internet Domain with harmonic wave routing and DDoS mitigation Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- domains/internet/InternetDomain.hpp | 246 ++++++++++++++++++++++ domains/internet/test_internet_domain.cpp | 94 +++++++++ tests/CMakeLists.txt | 9 + 3 files changed, 349 insertions(+) create mode 100644 domains/internet/InternetDomain.hpp create mode 100644 domains/internet/test_internet_domain.cpp diff --git a/domains/internet/InternetDomain.hpp b/domains/internet/InternetDomain.hpp new file mode 100644 index 0000000..8d6e039 --- /dev/null +++ b/domains/internet/InternetDomain.hpp @@ -0,0 +1,246 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "feen/resonator.h" +#include "feen/network.h" +#include "domains/satellite/SatelliteSwarm.hpp" + +namespace FEEN { +namespace InternetDomain { + +/** + * @brief Represents a router or data center as an oscillator. + */ +class RouterNode { +public: + explicit RouterNode(const feen::ResonatorConfig& config, double bandwidth_capacity, double computational_load) + : core_resonator_(config), + bandwidth_capacity_(bandwidth_capacity), + computational_load_(computational_load), + parasitic_noise_amplitude_(0.0), + parasitic_noise_frequency_(0.0) {} + + /** + * @brief Calculates local impedance. + * Impedance is modeled as computational_load / bandwidth_capacity. + * High load or low bandwidth spikes impedance. + */ + double impedance() const { + if (bandwidth_capacity_ <= 0.0) return std::numeric_limits::infinity(); + return computational_load_ / bandwidth_capacity_; + } + + void set_load(double load) { computational_load_ = load; } + void set_capacity(double capacity) { bandwidth_capacity_ = capacity; } + + feen::Resonator& get_core() noexcept { return core_resonator_; } + const feen::Resonator& get_core() const noexcept { return core_resonator_; } + + /** + * @brief Simulates an inbound traffic evaluator perimeter. + * Detects a flood of parasitic, non-harmonic frequencies (DDoS). + */ + void evaluate_inbound_traffic(double noise_amplitude, double noise_frequency, double current_time) { + // A DDoS attack artificially spikes local impedance by simulating a massive load + double simulated_noise_load = noise_amplitude * 1e6; // Arbitrary high factor + computational_load_ += simulated_noise_load; + + if (impedance() > DDOS_IMPEDANCE_THRESHOLD) { + // Node detected massive influx of noise. Identify parasitic frequency. + parasitic_noise_amplitude_ = noise_amplitude; + parasitic_noise_frequency_ = noise_frequency; + mitigate_ddos(current_time); + } + } + + /** + * @brief Neutralizes the malicious traffic via destructive interference. + * Emits an exact inverse waveform (180-degree phase shift). + */ + void mitigate_ddos(double current_time) { + if (parasitic_noise_amplitude_ > 0.0) { + // Emitting inverse waveform (180 phase shift) is effectively injecting negative amplitude + // or shifting phase by PI. + // core_resonator_.inject() defaults phase to 0. We can do inject(amp, M_PI) to cancel. + // We just record that neutralization occurred and reset the load. + core_resonator_.inject(parasitic_noise_amplitude_, M_PI); // Destructive interference + + // Restore normal computational load (noise neutralized) + computational_load_ -= (parasitic_noise_amplitude_ * 1e6); + + parasitic_noise_amplitude_ = 0.0; + parasitic_noise_frequency_ = 0.0; + + ddos_mitigated_ = true; + } + } + + bool was_ddos_mitigated() const { return ddos_mitigated_; } + void clear_mitigation_flag() { ddos_mitigated_ = false; } + +private: + feen::Resonator core_resonator_; + double bandwidth_capacity_; + double computational_load_; + + // DDoS Evaluator State + double parasitic_noise_amplitude_; + double parasitic_noise_frequency_; + bool ddos_mitigated_ = false; + + static constexpr double DDOS_IMPEDANCE_THRESHOLD = 1e3; +}; + +/** + * @brief Physical connection (fiber optic cable) as an elastic medium. + */ +struct FiberEdge { + std::size_t source_node; + std::size_t target_node; + double length; // Dictates wave propagation speed and natural frequency + bool is_severed; + + FiberEdge(std::size_t src, std::size_t tgt, double l) + : source_node(src), target_node(tgt), length(l), is_severed(false) {} + + double edge_impedance() const { + if (is_severed) return std::numeric_limits::infinity(); + return length; // Simple model: longer fiber = higher natural impedance + } +}; + +class HarmonicRouter { +public: + HarmonicRouter() = default; + + void add_node(const feen::ResonatorConfig& config, double bandwidth, double load) { + nodes_.emplace_back(config, bandwidth, load); + } + + void add_edge(std::size_t src, std::size_t tgt, double length) { + if (src >= nodes_.size() || tgt >= nodes_.size()) { + throw std::out_of_range("HarmonicRouter link endpoint index out of range."); + } + edges_.emplace_back(src, tgt, length); + } + + RouterNode& get_node(std::size_t idx) { return nodes_.at(idx); } + FiberEdge& get_edge(std::size_t idx) { return edges_.at(idx); } + + /** + * @brief Route data via Harmonic Pathfinding. + * Finds the path of least impedance by evaluating node impedance + edge impedance. + * Severed edges return infinity. Uses Dijkstra's algorithm. + */ + std::vector find_path(std::size_t source, std::size_t target) const { + if (source == target) return {source}; + if (source >= nodes_.size() || target >= nodes_.size()) return {}; + + std::vector dist(nodes_.size(), std::numeric_limits::infinity()); + std::vector prev(nodes_.size(), std::numeric_limits::max()); + std::vector visited(nodes_.size(), false); + + dist[source] = 0.0; + + for (std::size_t i = 0; i < nodes_.size(); ++i) { + double min_dist = std::numeric_limits::infinity(); + std::size_t u = std::numeric_limits::max(); + + for (std::size_t v = 0; v < nodes_.size(); ++v) { + if (!visited[v] && dist[v] <= min_dist) { + min_dist = dist[v]; + u = v; + } + } + + if (u == std::numeric_limits::max() || min_dist == std::numeric_limits::infinity()) { + break; // remaining nodes are inaccessible + } + + if (u == target) break; // found target + + visited[u] = true; + + for (const auto& edge : edges_) { + if (edge.source_node == u) { + std::size_t v = edge.target_node; + if (!visited[v]) { + double weight = edge.edge_impedance() + nodes_[v].impedance(); + if (dist[u] + weight < dist[v]) { + dist[v] = dist[u] + weight; + prev[v] = u; + } + } + } else if (edge.target_node == u) { + // Assuming undirected edges for pathfinding, though specified source->target + // Wait, our add_edge logic does source->target. Let's make it bidirectional for realistic network routing. + std::size_t v = edge.source_node; + if (!visited[v]) { + double weight = edge.edge_impedance() + nodes_[v].impedance(); + if (dist[u] + weight < dist[v]) { + dist[v] = dist[u] + weight; + prev[v] = u; + } + } + } + } + } + + std::vector path; + std::size_t curr = target; + if (prev[curr] != std::numeric_limits::max() || curr == source) { + while (curr != std::numeric_limits::max()) { + path.insert(path.begin(), curr); + curr = prev[curr]; + } + } + + return path; + } + + /** + * @brief Ground-to-Orbit Handoff Interface. + * Continuous phase-lock mechanics between static ground nodes and fast-moving orbital nodes. + * Uses Doppler mapping: as satellite moves, ground station gracefully shifts frequency. + */ + void orbit_handoff(std::size_t ground_node_idx, const SatelliteDomain::SwarmNode& satellite_node, double doppler_shift_hz) { + if (ground_node_idx >= nodes_.size()) { + throw std::out_of_range("Ground node index out of range."); + } + + RouterNode& ground_node = nodes_[ground_node_idx]; + const feen::Resonator& sat_core = satellite_node.get_core(); + + // Satellite target frequency + simulated Doppler shift + double target_hz = sat_core.frequency_hz() + doppler_shift_hz; + + // Ground station gracefully shifts its frequency to maintain constructive interference + // (Since frequency_hz is const in ResonatorConfig and beta/config are private, + // we inject phase adjustments or simulate the shift via resonance locking.) + // In FEEN, phase-locking means shifting state to match the target wave + double target_omega = target_hz * 2.0 * M_PI; + double current_t = ground_node.get_core().t(); + + // Force state to match satellite's shifted phase + double new_x = std::cos(target_omega * current_t); + double new_v = -target_omega * std::sin(target_omega * current_t); + + ground_node.get_core().set_state(new_x, new_v, current_t); + } + +private: + std::vector nodes_; + std::vector edges_; +}; + +} // namespace InternetDomain +} // namespace FEEN diff --git a/domains/internet/test_internet_domain.cpp b/domains/internet/test_internet_domain.cpp new file mode 100644 index 0000000..c1d7d14 --- /dev/null +++ b/domains/internet/test_internet_domain.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +#include "domains/internet/InternetDomain.hpp" + +// Simple testing framework since tests are run individually without CTest +#define CHECK(cond) \ + do { \ + if (!(cond)) { \ + std::cerr << "Test failed: " << #cond << " at " << __FILE__ << ":" << __LINE__ << std::endl; \ + exit(1); \ + } \ + } while(0) + +void test_severed_edge_failover() { + std::cout << "Running test_severed_edge_failover..." << std::endl; + + FEEN::InternetDomain::HarmonicRouter router; + + feen::ResonatorConfig config; + config.frequency_hz = 1000.0; + config.q_factor = 200.0; + + // Add 3 nodes: source (0), target_1 (1), target_2 (2) + router.add_node(config, 100.0, 10.0); // Node 0 + router.add_node(config, 100.0, 10.0); // Node 1 + router.add_node(config, 100.0, 10.0); // Node 2 + + // Add edges + router.add_edge(0, 1, 10.0); // Edge 0: 0 -> 1 (Length 10) + router.add_edge(0, 2, 20.0); // Edge 1: 0 -> 2 (Length 20) + + // We want to test routing from 0 -> 2. + // Path A: 0 -> 1 -> 2 (total edge length: 10 + 5 = 15) + // Path B: 0 -> 2 (total edge length: 20) + + router.add_edge(1, 2, 5.0); // Edge 2: 1 -> 2 (Length 5) + + // Initially, path A (0 -> 1 -> 2) should be chosen because total length 15 < 20. + // (Node impedances are equal). + std::vector path_initial = router.find_path(0, 2); + CHECK(path_initial.size() == 3); + CHECK(path_initial[0] == 0); + CHECK(path_initial[1] == 1); + CHECK(path_initial[2] == 2); + + // Sever edge 0 (0 -> 1) + router.get_edge(0).is_severed = true; + + // Recalculate path. Edge 0 has infinite impedance, so it must route via path B (0 -> 2 directly). + std::vector failover_path = router.find_path(0, 2); + CHECK(failover_path.size() == 2); + CHECK(failover_path[0] == 0); + CHECK(failover_path[1] == 2); + + std::cout << "test_severed_edge_failover passed!" << std::endl; +} + +void test_ddos_mitigation() { + std::cout << "Running test_ddos_mitigation..." << std::endl; + + feen::ResonatorConfig config; + config.frequency_hz = 1000.0; + config.q_factor = 200.0; + + FEEN::InternetDomain::RouterNode node(config, 1000.0, 50.0); // High bandwidth, low load + + // Initial impedance should be low (50 / 1000 = 0.05) + CHECK(node.impedance() < 1.0); + CHECK(!node.was_ddos_mitigated()); + + // Inject massive noise (simulated DDoS) + double parasitic_amp = 50.0; + double parasitic_freq = 5000.0; + + // Evaluate traffic. The massive amplitude should artificially spike load by 50 * 1e6 = 50,000,000 + // Impedance becomes > 50,000. Threshold is 1e3. + node.evaluate_inbound_traffic(parasitic_amp, parasitic_freq, 0.0); + + // It should have neutralized the DDoS and cleared the load. + CHECK(node.was_ddos_mitigated()); + CHECK(node.impedance() < 1.0); // Restored to normal + + std::cout << "test_ddos_mitigation passed!" << std::endl; +} + +int main() { + test_severed_edge_failover(); + test_ddos_mitigation(); + return 0; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 001d1b5..f3ed19c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -48,3 +48,12 @@ add_test( NAME MedicineDomainValidation COMMAND medicine_domain_test ) + +add_executable(internet_domain_test ../domains/internet/test_internet_domain.cpp) +target_link_libraries(internet_domain_test PRIVATE feen) +target_include_directories(internet_domain_test PRIVATE ${CMAKE_SOURCE_DIR}) + +add_test( + NAME InternetDomainValidation + COMMAND internet_domain_test +)