feat(core): event queue, Ethernet, switch, ARP, IPv4, ICMP, routing and firewall
First slice of the TypeScript port of the filius-ipad runtime engine, with 29 tests ported from the Swift suite. Parts not ported yet throw NotPortedError instead of guessing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
// Ported from filius-ipad (GPLv2/GPLv3):
|
||||
// State/TopologyNetworkRuntimeEngine.swift (TopologyNetworkRuntimeTopologySnapshot)
|
||||
// What the runtime starts from: the topology and every configuration, frozen
|
||||
// at the moment the simulation starts. Later protocol slices add their
|
||||
// configuration maps here (DHCP, NAT, remote link, wireless).
|
||||
|
||||
import { compareIds, type Id } from '../ids.js';
|
||||
import type { NodeKind } from '../model/node.js';
|
||||
import { canonicalMacAddress } from '../model/node.js';
|
||||
import type {
|
||||
DeviceConfiguration,
|
||||
FirewallConfiguration,
|
||||
InterfaceConfiguration,
|
||||
InterfaceKey,
|
||||
ManualRoute,
|
||||
SwitchConfiguration,
|
||||
} from './config.js';
|
||||
|
||||
export interface PortSnapshot {
|
||||
readonly id: Id;
|
||||
readonly label: string;
|
||||
/** Canonical, or undefined for a port whose MAC address is derived from its id. */
|
||||
readonly macAddress?: string;
|
||||
}
|
||||
|
||||
export function portSnapshot(id: Id, label: string, macAddress?: string): PortSnapshot {
|
||||
const canonical = canonicalMacAddress(macAddress);
|
||||
return canonical === undefined ? { id, label } : { id, label, macAddress: canonical };
|
||||
}
|
||||
|
||||
export interface NodeSnapshot {
|
||||
readonly id: Id;
|
||||
readonly kind: NodeKind;
|
||||
readonly ports: readonly PortSnapshot[];
|
||||
}
|
||||
|
||||
export interface LinkSnapshot {
|
||||
readonly id: Id;
|
||||
readonly sourceNodeId: Id;
|
||||
readonly sourcePortId: Id;
|
||||
readonly targetNodeId: Id;
|
||||
readonly targetPortId: Id;
|
||||
}
|
||||
|
||||
export interface TopologySnapshot {
|
||||
/** Sorted by id (Swift `uuidString` order). */
|
||||
readonly nodes: readonly NodeSnapshot[];
|
||||
/** Sorted by id (Swift `uuidString` order). */
|
||||
readonly links: readonly LinkSnapshot[];
|
||||
readonly deviceConfigurations: ReadonlyMap<Id, DeviceConfiguration>;
|
||||
readonly interfaceConfigurations: ReadonlyMap<InterfaceKey, InterfaceConfiguration>;
|
||||
readonly manualRoutesByNodeId: ReadonlyMap<Id, readonly ManualRoute[]>;
|
||||
readonly ripEnabledByNodeId: ReadonlyMap<Id, boolean>;
|
||||
readonly firewallConfigurationsByNodeId: ReadonlyMap<Id, FirewallConfiguration>;
|
||||
readonly switchConfigurationsByNodeId: ReadonlyMap<Id, SwitchConfiguration>;
|
||||
}
|
||||
|
||||
export interface TopologySnapshotInput {
|
||||
nodes: readonly NodeSnapshot[];
|
||||
links: readonly LinkSnapshot[];
|
||||
deviceConfigurations?: ReadonlyMap<Id, DeviceConfiguration>;
|
||||
interfaceConfigurations?: ReadonlyMap<InterfaceKey, InterfaceConfiguration>;
|
||||
manualRoutesByNodeId?: ReadonlyMap<Id, readonly ManualRoute[]>;
|
||||
ripEnabledByNodeId?: ReadonlyMap<Id, boolean>;
|
||||
firewallConfigurationsByNodeId?: ReadonlyMap<Id, FirewallConfiguration>;
|
||||
switchConfigurationsByNodeId?: ReadonlyMap<Id, SwitchConfiguration>;
|
||||
}
|
||||
|
||||
export function topologySnapshot(input: TopologySnapshotInput): TopologySnapshot {
|
||||
return {
|
||||
nodes: [...input.nodes].sort((lhs, rhs) => compareIds(lhs.id, rhs.id)),
|
||||
links: [...input.links].sort((lhs, rhs) => compareIds(lhs.id, rhs.id)),
|
||||
deviceConfigurations: input.deviceConfigurations ?? new Map(),
|
||||
interfaceConfigurations: input.interfaceConfigurations ?? new Map(),
|
||||
manualRoutesByNodeId: input.manualRoutesByNodeId ?? new Map(),
|
||||
ripEnabledByNodeId: input.ripEnabledByNodeId ?? new Map(),
|
||||
firewallConfigurationsByNodeId: input.firewallConfigurationsByNodeId ?? new Map(),
|
||||
switchConfigurationsByNodeId: input.switchConfigurationsByNodeId ?? new Map(),
|
||||
};
|
||||
}
|
||||
|
||||
/** The same snapshot with some fields replaced (Swift rebuilds it field by field). */
|
||||
export function withSnapshot(
|
||||
snapshot: TopologySnapshot,
|
||||
changes: Partial<TopologySnapshotInput>,
|
||||
): TopologySnapshot {
|
||||
return topologySnapshot({ ...snapshot, ...changes });
|
||||
}
|
||||
|
||||
export const EMPTY_TOPOLOGY_SNAPSHOT: TopologySnapshot = topologySnapshot({ nodes: [], links: [] });
|
||||
Reference in New Issue
Block a user