Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
246 changes: 246 additions & 0 deletions domains/internet/InternetDomain.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
#pragma once

#include <vector>
#include <cmath>
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <iostream>
#include <complex>
#include <string>
#include <limits>

#include "feen/resonator.h"
#include "feen/network.h"
Comment on lines +6 to +14
#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<double>::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;
Comment on lines +52 to +60
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
Comment on lines +69 to +75

// 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<double>::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);
}
Comment on lines +125 to +134

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<std::size_t> 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<double> dist(nodes_.size(), std::numeric_limits<double>::infinity());
std::vector<std::size_t> prev(nodes_.size(), std::numeric_limits<std::size_t>::max());
std::vector<bool> visited(nodes_.size(), false);

dist[source] = 0.0;

for (std::size_t i = 0; i < nodes_.size(); ++i) {
double min_dist = std::numeric_limits<double>::infinity();
std::size_t u = std::numeric_limits<std::size_t>::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<std::size_t>::max() || min_dist == std::numeric_limits<double>::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.
Comment on lines +184 to +185
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<std::size_t> path;
std::size_t curr = target;
if (prev[curr] != std::numeric_limits<std::size_t>::max() || curr == source) {
while (curr != std::numeric_limits<std::size_t>::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<RouterNode> nodes_;
std::vector<FiberEdge> edges_;
};

} // namespace InternetDomain
} // namespace FEEN
94 changes: 94 additions & 0 deletions domains/internet/test_internet_domain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include <iostream>
#include <vector>
#include <cassert>
#include <cmath>
#include <limits>

#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)
Comment on lines +9 to +16

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<std::size_t> 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<std::size_t> 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;
}
9 changes: 9 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Loading