mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-27 13:52:16 +00:00
transport: accept valid tunnel syntheses and bind tunnels as in Python
This commit is contained in:
1 parent
acd59b178e
commit
af65181e49
9 files changed
+338
-114
No files matched your search
@@ -12,6 +12,7 @@
|
||||
- The receiving side of a Link counts its Link proof as sent traffic, as in Python Reticulum, so it no longer answers keepalives before the Link is established and its traffic totals include the proof.
|
||||
- A connectable I2P interface now starts alongside a Unix-socket shared instance or another connectable I2P interface, and an I2P interface runs its server and one connection for every configured peer, as in Python Reticulum.
|
||||
- After a restart, a hub no longer gives a previous client's saved paths to a new client that reuses its interface name, and it keeps saved tunnels that had no live interface, as in Python Reticulum.
|
||||
- Transport hubs now accept tunnel synthesis, so a Python client that reconnects gets its paths back without announcing again, and losing a tunnel's interface no longer drops paths the hub learned through other interfaces, as in Python Reticulum.
|
||||
|
||||
## 1.4.0 - 2026-10-12
|
||||
|
||||
|
||||
@@ -49,6 +49,10 @@ accepted clients report as `parent_id`. `InterfaceEntry::parent_id` carries it
|
||||
into transport; set it from `InterfaceHandle::parent_id`. Persisted tunnels now
|
||||
load unbound into the tunnel table, so `TransportActor::pending_tunnel_entries`
|
||||
is gone.
|
||||
`TunnelSynthesisData::unpack` accepts exactly 176 bytes. Announces join the
|
||||
tunnel named by their interface's latest synthesis, which `handle_tunnel` and
|
||||
`TunnelTable::insert` record and `TunnelTable::void_interface` clears; writing
|
||||
`interface_id` directly no longer rebinds an interface.
|
||||
|
||||
The compatibility floor remains unchanged. Its cumulative exact-removal list
|
||||
also retains the already-reviewed [1.3 migration](1.3.md) for discovery ownership
|
||||
|
||||
@@ -1848,6 +1848,7 @@ pub fn rns_transport::tunnel::TunnelTable::len(&self) -> usize
|
||||
pub fn rns_transport::tunnel::TunnelTable::new() -> Self
|
||||
pub fn rns_transport::tunnel::TunnelTable::remove(&mut self, &[u8; 32]) -> core::option::Option<rns_transport::tunnel::TunnelEntry>
|
||||
pub fn rns_transport::tunnel::TunnelTable::tunnel_paths(&self) -> impl core::iter::traits::iterator::Iterator<Item = (&[u8; 16], &rns_transport::tunnel::TunnelPath)>
|
||||
pub fn rns_transport::tunnel::TunnelTable::void_interface(&mut self, rns_transport::messages::InterfaceId) -> usize
|
||||
impl core::default::Default for rns_transport::tunnel::TunnelTable
|
||||
pub fn rns_transport::tunnel::TunnelTable::default() -> Self
|
||||
pub fn rns_transport::tunnel::build_tunnel_synthesis_packet(&rns_identity::identity::Identity, [u8; 32]) -> core::option::Option<alloc::vec::Vec<u8>>
|
||||
|
||||
+2
-2
@@ -51,8 +51,8 @@
|
||||
"tier": "provisional",
|
||||
"compatibility": "reviewed-snapshot",
|
||||
"snapshot": "api/snapshots/rns-transport.txt",
|
||||
"itemCount": 1908,
|
||||
"sha256": "e191078827715eb320d27cf15131b75271f104eb18f05a802d895254493516a9"
|
||||
"itemCount": 1909,
|
||||
"sha256": "cda7a77a69823b00d4d642aa2fdee09e74ece48eed7dd43dfc707b0da2f1e861"
|
||||
},
|
||||
{
|
||||
"name": "rns-link",
|
||||
|
||||
@@ -765,60 +765,29 @@ impl TransportActor {
|
||||
return;
|
||||
}
|
||||
|
||||
// Tunnel-synthesis: validate the Ed25519 signature, install the
|
||||
// tunnel, and replay any cached paths for it. Uses TUNNEL_TIMEOUT
|
||||
// (short) rather than DESTINATION_TIMEOUT (long) because the
|
||||
// tunnel-layer binding is more volatile than identity lifetime.
|
||||
// Tunnel synthesis: install the tunnel, and replay its cached paths,
|
||||
// once the signature verifies (Transport.py:2788-2805).
|
||||
if header.destination_hash == tunnel_synth_dest {
|
||||
if let Some(payload_start) = self.data_payload_offset(raw, header) {
|
||||
let payload = &raw[payload_start..];
|
||||
if let Some(synth_data) = crate::tunnel::TunnelSynthesisData::unpack(payload) {
|
||||
let ed25519_pub_bytes: [u8; 32] = {
|
||||
let mut buf = [0u8; 32];
|
||||
// First 32 bytes of the 64-byte public_key blob are
|
||||
// the Ed25519 signing key; remaining 32 are X25519.
|
||||
buf.copy_from_slice(&synth_data.public_key[..32]);
|
||||
buf
|
||||
};
|
||||
|
||||
match rns_crypto::ed25519::Ed25519PublicKey::from_bytes(&ed25519_pub_bytes) {
|
||||
Ok(verify_key) => {
|
||||
let mut signed_data = Vec::with_capacity(112);
|
||||
signed_data.extend_from_slice(&synth_data.public_key);
|
||||
signed_data.extend_from_slice(&synth_data.interface_hash);
|
||||
signed_data.extend_from_slice(&synth_data.random_hash);
|
||||
|
||||
if verify_key
|
||||
.verify(&signed_data, &synth_data.signature)
|
||||
.is_ok()
|
||||
{
|
||||
let tunnel_id = synth_data.tunnel_id();
|
||||
debug!(
|
||||
tunnel_id = hex::encode(&tunnel_id[..16]),
|
||||
interface_id, "tunnel synthesis request validated"
|
||||
);
|
||||
crate::tunnel::handle_tunnel(
|
||||
&mut self.tunnel_table,
|
||||
tunnel_id,
|
||||
interface_id,
|
||||
TUNNEL_TIMEOUT as f64,
|
||||
);
|
||||
self.state_dirty = true;
|
||||
|
||||
self.restore_tunnel_paths(&tunnel_id, interface_id);
|
||||
} else {
|
||||
debug!(
|
||||
interface_id,
|
||||
"tunnel synthesis signature verification failed"
|
||||
);
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
debug!(interface_id, "tunnel synthesis invalid Ed25519 public key");
|
||||
}
|
||||
match crate::tunnel::TunnelSynthesisData::unpack(&raw[payload_start..])
|
||||
.filter(crate::tunnel::TunnelSynthesisData::is_signed)
|
||||
{
|
||||
Some(synthesis) => {
|
||||
let tunnel_id = synthesis.tunnel_id();
|
||||
debug!(
|
||||
tunnel_id = hex::encode(&tunnel_id[..16]),
|
||||
interface_id, "tunnel synthesis request validated"
|
||||
);
|
||||
crate::tunnel::handle_tunnel(
|
||||
&mut self.tunnel_table,
|
||||
tunnel_id,
|
||||
interface_id,
|
||||
TUNNEL_TIMEOUT as f64,
|
||||
);
|
||||
self.state_dirty = true;
|
||||
self.restore_tunnel_paths(&tunnel_id, interface_id);
|
||||
}
|
||||
} else {
|
||||
trace!(interface_id, "tunnel synthesis data too short or malformed");
|
||||
None => debug!(interface_id, "invalid tunnel synthesis"),
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -696,47 +696,13 @@ impl TransportActor {
|
||||
}
|
||||
}
|
||||
|
||||
/// On interface drop: remove tunnel-bound paths and re-announce the
|
||||
/// affected local destinations on remaining interfaces.
|
||||
/// On interface drop the interface names no tunnel, and the tunnels
|
||||
/// bound to it keep their paths for their next synthesis.
|
||||
pub(super) fn void_tunnel_interface(&mut self, interface_id: InterfaceId) {
|
||||
let mut voided_destinations: Vec<[u8; 16]> = Vec::new();
|
||||
|
||||
let tunnel_ids: Vec<[u8; 32]> = self
|
||||
.tunnel_table
|
||||
.iter()
|
||||
.filter(|(_, entry)| entry.interface_id == Some(interface_id))
|
||||
.map(|(id, _)| *id)
|
||||
.collect();
|
||||
|
||||
for tunnel_id in &tunnel_ids {
|
||||
if let Some(tunnel) = self.tunnel_table.get(tunnel_id) {
|
||||
for dest_hash in tunnel.tunnel_paths.keys() {
|
||||
voided_destinations.push(*dest_hash);
|
||||
self.path_table.remove(dest_hash);
|
||||
self.state_dirty = true;
|
||||
}
|
||||
}
|
||||
if let Some(tunnel) = self.tunnel_table.get_mut(tunnel_id) {
|
||||
tunnel.interface_id = None;
|
||||
debug!(
|
||||
tunnel_id = hex::encode(&tunnel_id[..16]),
|
||||
"voided tunnel interface"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for dest_hash in &voided_destinations {
|
||||
if self.local_destinations.contains(dest_hash)
|
||||
&& let Some(tx) = self.destination_channels.get(dest_hash)
|
||||
&& let Err(e) =
|
||||
tx.try_send(crate::link_messages::DestinationEvent::AnnounceRequested(
|
||||
crate::link_messages::AnnounceRequest::normal(String::new()),
|
||||
))
|
||||
{
|
||||
self.channel_drops += 1;
|
||||
warn!(dest = hex::encode(dest_hash), drops = self.channel_drops, err = %e,
|
||||
"failed to send re-announce after tunnel void (channel full)");
|
||||
}
|
||||
let voided = self.tunnel_table.void_interface(interface_id);
|
||||
if voided > 0 {
|
||||
debug!(interface_id, voided, "voided tunnel interface");
|
||||
self.state_dirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1025,6 +1025,8 @@ impl TransportActor {
|
||||
existing.owner.retire();
|
||||
}
|
||||
self.path_table.drop_all_via(id);
|
||||
// A new interface starts without a tunnel (Interface.py:127).
|
||||
self.void_tunnel_interface(id);
|
||||
self.link_table.remove_interface(id);
|
||||
self.retire_recursive_discovery_interface(id);
|
||||
self.retire_path_request_admissions(id);
|
||||
@@ -3293,8 +3295,8 @@ mod tests {
|
||||
}
|
||||
|
||||
/// Deregistering an interface must cascade through every routing table
|
||||
/// that points at it: paths via the interface, tunnels anchored to it,
|
||||
/// and the interface entry itself all go away in one shot.
|
||||
/// that points at it: paths via the interface and the interface entry
|
||||
/// go away in one shot, and tunnels anchored to it unbind.
|
||||
#[test]
|
||||
fn test_deregister_cleans_path_and_tunnel_tables() {
|
||||
let (mut actor, _tx) = TransportActor::new();
|
||||
@@ -8893,17 +8895,13 @@ mod tests {
|
||||
actor.path_table.insert(dest_hash, path_entry);
|
||||
assert!(actor.path_table.has_path(&dest_hash));
|
||||
|
||||
// Void the interface
|
||||
actor.void_tunnel_interface(1);
|
||||
|
||||
// Path should be removed
|
||||
assert!(!actor.path_table.has_path(&dest_hash));
|
||||
|
||||
// Tunnel interface should be voided
|
||||
assert_eq!(
|
||||
actor.tunnel_table.get(&tunnel_id).unwrap().interface_id,
|
||||
None
|
||||
);
|
||||
// Voiding only unbinds; deregistration drops the paths via 1.
|
||||
assert!(actor.path_table.has_path(&dest_hash));
|
||||
let tunnel = actor.tunnel_table.get(&tunnel_id).unwrap();
|
||||
assert_eq!(tunnel.interface_id, None);
|
||||
assert!(tunnel.tunnel_paths.contains_key(&dest_hash));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
//! Tunnel binding, persistence and cache replay through the production actor
|
||||
//! paths.
|
||||
//! Tunnel synthesis, binding, persistence and cache replay through the
|
||||
//! production actor paths.
|
||||
use super::*;
|
||||
use crate::persistence::{
|
||||
PersistedPathEntry, PersistedTunnelEntry, PersistedTunnelPath, PersistedTunnelTable,
|
||||
};
|
||||
use crate::tunnel::{TunnelPath, handle_tunnel};
|
||||
use crate::tunnel::{
|
||||
TunnelPath, TunnelSynthesisData, build_tunnel_synthesis_packet, handle_tunnel,
|
||||
};
|
||||
use rns_identity::identity::Identity;
|
||||
use rns_wire::flags::HeaderType;
|
||||
|
||||
fn hub() -> TransportActor {
|
||||
@@ -30,6 +33,15 @@ fn inbound(actor: &mut TransportActor, raw: Bytes, interface_id: InterfaceId) {
|
||||
});
|
||||
}
|
||||
|
||||
fn synthesis(identity: &Identity, interface: &str) -> (Bytes, [u8; 32]) {
|
||||
let raw =
|
||||
build_tunnel_synthesis_packet(identity, crate::tunnel::interface_hash(interface)).unwrap();
|
||||
let tunnel_id = TunnelSynthesisData::unpack(&raw[rns_wire::constants::HEADER_MINSIZE..])
|
||||
.unwrap()
|
||||
.tunnel_id();
|
||||
(Bytes::from(raw), tunnel_id)
|
||||
}
|
||||
|
||||
fn carries(actor: &TransportActor, tunnel_id: &[u8; 32], dest: &[u8; 16]) -> bool {
|
||||
actor
|
||||
.tunnel_table
|
||||
@@ -49,6 +61,153 @@ fn cache_announce(dir: &std::path::Path, raw: &[u8], interface: Option<&str>) ->
|
||||
packet_hash
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn valid_synthesis_installs_a_tunnel_that_collects_announces() {
|
||||
let mut actor = hub();
|
||||
let _rx = attach(&mut actor, 1, "hub/client_1");
|
||||
let (raw, tunnel_id) = synthesis(&Identity::new(), "TCPInterface[Uplink/127.0.0.1:4242]");
|
||||
inbound(&mut actor, raw, 1);
|
||||
assert!(
|
||||
actor.tunnel_table.get(&tunnel_id).is_some(),
|
||||
"a valid synthesis installs its tunnel"
|
||||
);
|
||||
|
||||
let (announce, dest) = make_valid_announce("wp4.tunnel.collect", 0);
|
||||
inbound(&mut actor, announce, 1);
|
||||
assert!(
|
||||
carries(&actor, &tunnel_id, &dest),
|
||||
"announces on the tunnel's interface join the tunnel"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn synthesis_with_a_bad_signature_or_extra_bytes_installs_nothing() {
|
||||
let mut actor = hub();
|
||||
let _rx: Vec<_> = (1..=3)
|
||||
.map(|id| attach(&mut actor, id, &format!("hub/client_{id}")))
|
||||
.collect();
|
||||
let (valid, valid_id) = synthesis(&Identity::new(), "uplink-a");
|
||||
inbound(&mut actor, valid, 1);
|
||||
assert!(
|
||||
actor.tunnel_table.get(&valid_id).is_some(),
|
||||
"control: a valid synthesis installs"
|
||||
);
|
||||
|
||||
let (raw, tampered_id) = synthesis(&Identity::new(), "uplink-b");
|
||||
let mut tampered = raw.to_vec();
|
||||
*tampered.last_mut().unwrap() ^= 0x01;
|
||||
inbound(&mut actor, Bytes::from(tampered), 2);
|
||||
assert!(
|
||||
actor.tunnel_table.get(&tampered_id).is_none(),
|
||||
"a flipped signature bit is rejected"
|
||||
);
|
||||
|
||||
let (raw, trailing_id) = synthesis(&Identity::new(), "uplink-c");
|
||||
let mut trailing = raw.to_vec();
|
||||
trailing.push(0);
|
||||
inbound(&mut actor, Bytes::from(trailing), 3);
|
||||
assert!(
|
||||
actor.tunnel_table.get(&trailing_id).is_none(),
|
||||
"a payload longer than 176 bytes is rejected (Transport.py:2791)"
|
||||
);
|
||||
assert_eq!(actor.tunnel_table.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_newer_synthesis_on_an_interface_takes_its_later_announces() {
|
||||
let mut actor = hub();
|
||||
let _rx = attach(&mut actor, 1, "hub/client_1");
|
||||
let (older, older_id) = synthesis(&Identity::new(), "uplink");
|
||||
let (newer, newer_id) = synthesis(&Identity::new(), "uplink");
|
||||
inbound(&mut actor, older, 1);
|
||||
inbound(&mut actor, newer, 1);
|
||||
assert!(
|
||||
actor.tunnel_table.get(&older_id).is_some() && actor.tunnel_table.get(&newer_id).is_some(),
|
||||
"both syntheses install"
|
||||
);
|
||||
|
||||
let (announce, dest) = make_valid_announce("wp4.tunnel.newer", 0);
|
||||
inbound(&mut actor, announce, 1);
|
||||
assert!(
|
||||
carries(&actor, &newer_id, &dest),
|
||||
"the last synthesis owns the interface (Transport.py:2827, :2834)"
|
||||
);
|
||||
assert!(
|
||||
!carries(&actor, &older_id, &dest),
|
||||
"the superseded tunnel takes no new paths"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn losing_a_tunnel_interface_keeps_paths_through_other_interfaces() {
|
||||
let mut actor = hub();
|
||||
let _tunnel_rx = attach(&mut actor, 1, "hub/client_1");
|
||||
let _uplink_rx = attach(&mut actor, 2, "uplink");
|
||||
let tunnel_id = [0x77; 32];
|
||||
handle_tunnel(&mut actor.tunnel_table, tunnel_id, 1, TUNNEL_TIMEOUT as f64);
|
||||
|
||||
let identity = Identity::new();
|
||||
let emitted = now_f64() as u64;
|
||||
let (via_tunnel, dest) = make_announce_for_with_random_blob(
|
||||
&identity,
|
||||
"wp4.tunnel.elsewhere",
|
||||
1,
|
||||
random_blob(1, emitted - 10),
|
||||
);
|
||||
inbound(&mut actor, via_tunnel, 1);
|
||||
assert!(carries(&actor, &tunnel_id, &dest));
|
||||
let (fresher, _) = make_announce_for_with_random_blob(
|
||||
&identity,
|
||||
"wp4.tunnel.elsewhere",
|
||||
1,
|
||||
random_blob(2, emitted),
|
||||
);
|
||||
inbound(&mut actor, fresher, 2);
|
||||
assert_eq!(
|
||||
actor.path_table.get(&dest).map(|path| path.interface_id),
|
||||
Some(2)
|
||||
);
|
||||
|
||||
actor.deregister_interface(1);
|
||||
assert_eq!(
|
||||
actor
|
||||
.path_table
|
||||
.get_live(&dest)
|
||||
.map(|path| path.interface_id),
|
||||
Some(2),
|
||||
"losing the tunnel's interface only unbinds the tunnel (Transport.py:1031-1033)"
|
||||
);
|
||||
assert!(
|
||||
carries(&actor, &tunnel_id, &dest),
|
||||
"the tunnel keeps its paths for the next synthesis"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_replacement_registration_does_not_inherit_the_tunnel_binding() {
|
||||
let mut actor = hub();
|
||||
let (entry, _old_rx) = make_test_interface("hub/client_4");
|
||||
actor.handle_message(TransportMessage::RegisterInterface { id: 4, entry });
|
||||
let tunnel_id = [0x66; 32];
|
||||
handle_tunnel(&mut actor.tunnel_table, tunnel_id, 4, TUNNEL_TIMEOUT as f64);
|
||||
|
||||
let (replacement, _new_rx) = make_test_interface("hub/client_4");
|
||||
actor.handle_message(TransportMessage::RegisterInterface {
|
||||
id: 4,
|
||||
entry: replacement,
|
||||
});
|
||||
let (announce, dest) = make_valid_announce("wp4.tunnel.replaced", 0);
|
||||
inbound(&mut actor, announce, 4);
|
||||
assert!(
|
||||
actor.path_table.has_path(&dest),
|
||||
"control: the replacement is live"
|
||||
);
|
||||
assert!(
|
||||
!carries(&actor, &tunnel_id, &dest),
|
||||
"a replacement interface starts without a tunnel (Interface.py:127)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_unbound_tunnel_persists_without_an_interface_hash() {
|
||||
let dir = unique_temp_dir("wp4-unbound-tunnel");
|
||||
|
||||
@@ -27,16 +27,24 @@ pub struct TunnelPath {
|
||||
#[derive(Clone)]
|
||||
pub struct TunnelTable {
|
||||
entries: HashMap<[u8; 32], TunnelEntry>,
|
||||
/// Python's `interface.tunnel_id`: the tunnel each interface's latest
|
||||
/// synthesis named, which may outlive that tunnel as it does in Python.
|
||||
by_interface: HashMap<InterfaceId, [u8; 32]>,
|
||||
}
|
||||
|
||||
impl TunnelTable {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
entries: HashMap::new(),
|
||||
by_interface: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// A bound entry also becomes its interface's tunnel.
|
||||
pub fn insert(&mut self, entry: TunnelEntry) {
|
||||
if let Some(interface_id) = entry.interface_id {
|
||||
self.by_interface.insert(interface_id, entry.tunnel_id);
|
||||
}
|
||||
self.entries.insert(entry.tunnel_id, entry);
|
||||
}
|
||||
|
||||
@@ -68,10 +76,26 @@ impl TunnelTable {
|
||||
self.entries.iter_mut()
|
||||
}
|
||||
|
||||
/// The tunnel that announces arriving on `interface_id` join
|
||||
/// (Transport.py:2467-2476).
|
||||
pub fn get_mut_by_interface(&mut self, interface_id: InterfaceId) -> Option<&mut TunnelEntry> {
|
||||
self.entries
|
||||
.values_mut()
|
||||
.find(|entry| entry.interface_id == Some(interface_id))
|
||||
let tunnel_id = self.by_interface.get(&interface_id)?;
|
||||
self.entries.get_mut(tunnel_id)
|
||||
}
|
||||
|
||||
/// Forget `interface_id`: it names no tunnel, and the tunnels bound to it
|
||||
/// become unbound but keep their paths (Transport.py:1031-1033). Returns
|
||||
/// how many tunnels were unbound.
|
||||
pub fn void_interface(&mut self, interface_id: InterfaceId) -> usize {
|
||||
self.by_interface.remove(&interface_id);
|
||||
let mut voided = 0;
|
||||
for entry in self.entries.values_mut() {
|
||||
if entry.interface_id == Some(interface_id) {
|
||||
entry.interface_id = None;
|
||||
voided += 1;
|
||||
}
|
||||
}
|
||||
voided
|
||||
}
|
||||
|
||||
pub fn tunnel_paths(&self) -> impl Iterator<Item = (&[u8; 16], &TunnelPath)> {
|
||||
@@ -84,7 +108,7 @@ impl TunnelTable {
|
||||
let now = crate::now_f64();
|
||||
|
||||
let before = self.entries.len();
|
||||
self.entries.retain(|_, entry| entry.expires > now);
|
||||
self.entries.retain(|_, entry| !is_stale(entry, now));
|
||||
before - self.entries.len()
|
||||
}
|
||||
|
||||
@@ -95,7 +119,7 @@ impl TunnelTable {
|
||||
let to_remove: Vec<[u8; 32]> = self
|
||||
.entries
|
||||
.iter()
|
||||
.filter(|(_, entry)| entry.expires <= now)
|
||||
.filter(|(_, entry)| is_stale(entry, now))
|
||||
.take(limit)
|
||||
.map(|(hash, _)| *hash)
|
||||
.collect();
|
||||
@@ -113,6 +137,11 @@ impl Default for TunnelTable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Expired, or set to expire implausibly late (Transport.py:1022-1027).
|
||||
fn is_stale(entry: &TunnelEntry, now: f64) -> bool {
|
||||
entry.expires <= now || entry.expires > now + 2.0 * crate::constants::TUNNEL_TIMEOUT as f64
|
||||
}
|
||||
|
||||
/// Payload of a tunnel-synthesis announce. Total wire size = 176 bytes.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TunnelSynthesisData {
|
||||
@@ -137,8 +166,9 @@ impl TunnelSynthesisData {
|
||||
data
|
||||
}
|
||||
|
||||
/// Python accepts exactly 176 bytes (Transport.py:2790-2791).
|
||||
pub fn unpack(data: &[u8]) -> Option<Self> {
|
||||
if data.len() < 176 {
|
||||
if data.len() != 176 {
|
||||
return None;
|
||||
}
|
||||
let mut public_key = [0u8; 64];
|
||||
@@ -166,9 +196,23 @@ impl TunnelSynthesisData {
|
||||
tunnel_id_data.extend_from_slice(&self.interface_hash);
|
||||
sha256(&tunnel_id_data)
|
||||
}
|
||||
|
||||
/// The signature verifies under the Ed25519 half of `public_key`
|
||||
/// (Transport.py:2799-2805).
|
||||
pub(crate) fn is_signed(&self) -> bool {
|
||||
let Ok(signer) = rns_identity::identity::Identity::from_public_key(&self.public_key) else {
|
||||
return false;
|
||||
};
|
||||
let mut signed = Vec::with_capacity(112);
|
||||
signed.extend_from_slice(&self.public_key);
|
||||
signed.extend_from_slice(&self.interface_hash);
|
||||
signed.extend_from_slice(&self.random_hash);
|
||||
signer.verify(&signed, &self.signature)
|
||||
}
|
||||
}
|
||||
|
||||
/// Install or refresh a tunnel from a validated tunnel-synthesis announce.
|
||||
/// Install or refresh a tunnel from a validated tunnel-synthesis announce,
|
||||
/// making it the receiving interface's tunnel (Transport.py:2819-2834).
|
||||
/// A reappearing tunnel keeps its existing path set — only the interface
|
||||
/// binding and expiry move forward, so in-flight routes aren't lost when the
|
||||
/// underlying link flaps.
|
||||
@@ -184,6 +228,7 @@ pub fn handle_tunnel(
|
||||
if let Some(existing) = tunnel_table.get_mut(&tunnel_id) {
|
||||
existing.interface_id = Some(interface_id);
|
||||
existing.expires = expires;
|
||||
tunnel_table.by_interface.insert(interface_id, tunnel_id);
|
||||
} else {
|
||||
let entry = TunnelEntry {
|
||||
tunnel_id,
|
||||
@@ -291,6 +336,25 @@ mod tests {
|
||||
assert_eq!(unpacked.signature, data.signature);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn synthesis_payload_must_be_exactly_176_bytes() {
|
||||
let packed = TunnelSynthesisData {
|
||||
public_key: [0x42; 64],
|
||||
interface_hash: [0xAB; 32],
|
||||
random_hash: [0xCD; 16],
|
||||
signature: [0xEF; 64],
|
||||
}
|
||||
.pack();
|
||||
assert!(TunnelSynthesisData::unpack(&packed).is_some());
|
||||
assert!(TunnelSynthesisData::unpack(&packed[..175]).is_none());
|
||||
let mut trailing = packed.clone();
|
||||
trailing.push(0);
|
||||
assert!(
|
||||
TunnelSynthesisData::unpack(&trailing).is_none(),
|
||||
"Python requires exactly 176 bytes (Transport.py:2791)"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tunnel_synthesis_id() {
|
||||
let data = TunnelSynthesisData {
|
||||
@@ -325,6 +389,39 @@ mod tests {
|
||||
assert_eq!(table.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_interface_keeps_naming_the_tunnel_of_its_latest_synthesis() {
|
||||
let mut table = TunnelTable::new();
|
||||
let (older, newer) = ([0x0A; 32], [0x0B; 32]);
|
||||
handle_tunnel(&mut table, older, 1, 600.0);
|
||||
handle_tunnel(&mut table, newer, 1, 600.0);
|
||||
assert_eq!(
|
||||
table.get_mut_by_interface(1).map(|tunnel| tunnel.tunnel_id),
|
||||
Some(newer)
|
||||
);
|
||||
assert_eq!(
|
||||
table.get(&older).unwrap().interface_id,
|
||||
Some(1),
|
||||
"the superseded tunnel keeps its own binding (Transport.py:2827-2834)"
|
||||
);
|
||||
|
||||
handle_tunnel(&mut table, newer, 2, 600.0);
|
||||
assert_eq!(
|
||||
table.get_mut_by_interface(1).map(|tunnel| tunnel.tunnel_id),
|
||||
Some(newer),
|
||||
"interface 1 still names the tunnel that moved, as Python's interface.tunnel_id does"
|
||||
);
|
||||
|
||||
assert_eq!(table.void_interface(1), 1);
|
||||
assert!(table.get_mut_by_interface(1).is_none());
|
||||
assert_eq!(table.get(&older).unwrap().interface_id, None);
|
||||
assert_eq!(table.get(&newer).unwrap().interface_id, Some(2));
|
||||
assert_eq!(
|
||||
table.get_mut_by_interface(2).map(|tunnel| tunnel.tunnel_id),
|
||||
Some(newer)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tunnel_cull_expired() {
|
||||
let mut table = TunnelTable::new();
|
||||
@@ -340,6 +437,35 @@ mod tests {
|
||||
assert_eq!(table.len(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tunnels_with_excessive_expiry_are_culled() {
|
||||
let now = crate::now_f64();
|
||||
let limit = 2.0 * crate::constants::TUNNEL_TIMEOUT as f64;
|
||||
let culls: [fn(&mut TunnelTable) -> usize; 2] = [TunnelTable::cull_expired, |table| {
|
||||
table.cull_expired_batch(usize::MAX)
|
||||
}];
|
||||
for cull in culls {
|
||||
let mut table = TunnelTable::new();
|
||||
for (tunnel_id, expires) in [
|
||||
([0x01; 32], now + limit + 60.0),
|
||||
([0x02; 32], now + limit - 60.0),
|
||||
] {
|
||||
table.insert(TunnelEntry {
|
||||
tunnel_id,
|
||||
interface_id: None,
|
||||
tunnel_paths: HashMap::new(),
|
||||
expires,
|
||||
});
|
||||
}
|
||||
assert_eq!(cull(&mut table), 1);
|
||||
assert!(
|
||||
table.get(&[0x01; 32]).is_none(),
|
||||
"Python culls an expiry beyond twice TUNNEL_TIMEOUT (Transport.py:1022-1024)"
|
||||
);
|
||||
assert!(table.get(&[0x02; 32]).is_some());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tunnel_iter() {
|
||||
let mut table = TunnelTable::new();
|
||||
|
||||
Reference in new issue
Block a user