diff --git a/crates/rns-runtime/src/interface_factory.rs b/crates/rns-runtime/src/interface_factory.rs index 1abf9a5..b34b8a8 100644 --- a/crates/rns-runtime/src/interface_factory.rs +++ b/crates/rns-runtime/src/interface_factory.rs @@ -2034,6 +2034,12 @@ pub struct InterfacePostInit { pub announces_to_internal: Option, /// Configured `gravity`; `None` takes `[reticulum] default_gravity`. pub gravity: Option, + /// Configured `hop_limiter`, clamped to `0..=PATHFINDER_M`; 0 is `None`, off. + pub hop_limiter: Option, + /// Whether connections this interface accepts copy `hop_limiter`: TCP + /// server, Backbone and I2P listeners do, AutoInterface peers do not + /// (TCPInterface.py, BackboneInterface.py, I2PInterface.py). + pub spawned_hop_limiter: bool, /// Required announce stamp value on this interface, clamped to the stamp /// cost cap. `None` follows the global `[stamps] destination_cost`. pub destination_cost: Option, @@ -2139,6 +2145,13 @@ impl InterfacePostInit { announces_from_internal, announces_to_internal: config_bool(section, "announces_to_internal")?, gravity: config_int(section, "gravity")?, + hop_limiter: config_int(section, "hop_limiter")? + .map(rns_transport::destination_filter::FilterPolicy::clamp_hop_limiter) + .filter(|limit| *limit > 0), + spawned_hop_limiter: matches!( + section.get("type"), + Some("TCPServerInterface" | "BackboneInterface" | "I2PInterface") + ), destination_cost: section .get_float("destination_cost") .map(rns_transport::stamps::StampPolicy::clamp_cost), diff --git a/crates/rns-runtime/src/reticulum.rs b/crates/rns-runtime/src/reticulum.rs index 225d39f..a42b400 100644 --- a/crates/rns-runtime/src/reticulum.rs +++ b/crates/rns-runtime/src/reticulum.rs @@ -104,6 +104,7 @@ struct SpawnInheritance { announces_from_internal: bool, announces_to_internal: Option, gravity: i64, + hop_limiter: Option, } impl SpawnInheritance { @@ -5252,6 +5253,8 @@ struct OwnedInterfaceHandle { /// The address a discoverable interface learns at runtime (I2P b32). discovery_address: Option>>, hardware_failures: Option, + /// A peer Python spawns for its configured interface (I2PInterface.py). + configured_peer: bool, } /// A driver's failures Python answers with `RNS.panic()` when @@ -5429,6 +5432,7 @@ impl From for OwnedInterfaceHandle { driver: None, discovery_address: None, hardware_failures: None, + configured_peer: false, } } } @@ -5440,6 +5444,7 @@ impl From for OwnedInterfaceHandle driver: Some(spawned.driver), discovery_address: None, hardware_failures: None, + configured_peer: false, } } } @@ -6058,6 +6063,7 @@ async fn prepare_interface_with_role_and_overrides( entry.announces_from_internal = parent.announces_from_internal; entry.announces_to_internal = parent.announces_to_internal; entry.gravity = parent.gravity; + entry.hop_limiter = parent.hop_limiter; } entry.destination_cost = child_stamp_costs.0; entry.pr_cost = child_stamp_costs.1; @@ -6198,6 +6204,8 @@ fn clone_interface_post_init( announces_from_internal: post_init.announces_from_internal, announces_to_internal: post_init.announces_to_internal, gravity: post_init.gravity, + hop_limiter: post_init.hop_limiter, + spawned_hop_limiter: post_init.spawned_hop_limiter, destination_cost: post_init.destination_cost, pr_cost: post_init.pr_cost, autoconnect_source: post_init.autoconnect_source.clone(), @@ -6272,7 +6280,10 @@ async fn prepare_interface_with_post_init( )); } let OwnedInterfaceHandle { - interface, driver, .. + interface, + driver, + configured_peer, + .. } = owned; let handle = interface; // Outbound = physical capability AND `outgoing` config flag. An @@ -6363,6 +6374,9 @@ async fn prepare_interface_with_post_init( announces_from_internal: post_init.announces_from_internal, announces_to_internal: post_init.announces_to_internal, gravity: post_init.gravity.unwrap_or(0), + hop_limiter: post_init + .hop_limiter + .filter(|_| post_init.spawned_hop_limiter), }), }, ); @@ -6395,6 +6409,11 @@ async fn prepare_interface_with_post_init( entry.announces_to_internal = post_init.announces_to_internal; entry.gravity = post_init.gravity.unwrap_or(0); entry.shared_medium = post_init.shared_medium; + // Reticulum.py sets it on the configured interface, not on an RNodeMulti + // subinterface or an I2P peer. + entry.hop_limiter = post_init + .hop_limiter + .filter(|_| handle.parent_id.is_none() && !configured_peer); entry.destination_cost = post_init.destination_cost; entry.pr_cost = post_init.pr_cost; Ok(PreparedInterfaceRegistration { @@ -10826,7 +10845,10 @@ async fn spawn_configured_interface_driver( ) .await { - Ok(client) => handles.push(client.into()), + Ok(client) => handles.push(OwnedInterfaceHandle { + configured_peer: true, + ..client.into() + }), Err(e) => { for spawned in handles { rollback_unreserved_interface(spawned, InterfaceKind::Standard).await; diff --git a/crates/rns-runtime/src/reticulum/tests/paths_and_filters.rs b/crates/rns-runtime/src/reticulum/tests/paths_and_filters.rs index 3f8b4fb..226f6cb 100644 --- a/crates/rns-runtime/src/reticulum/tests/paths_and_filters.rs +++ b/crates/rns-runtime/src/reticulum/tests/paths_and_filters.rs @@ -21,3 +21,134 @@ fn config_dir_type_lists_fill_the_filter_policy() { load_config_dir_lists(empty.path(), &mut rc); assert!(rc.filter.known_types.is_empty() && rc.filter.flagged_types.is_empty()); } + +fn post_init(kind: &str, hop_limiter: &str) -> interface_factory::InterfacePostInit { + let mut section = ConfigSection::new(); + section.set("type", kind); + section.set("hop_limiter", hop_limiter); + interface_factory::InterfacePostInit::try_from_section(§ion).unwrap() +} + +#[test] +fn hop_limiter_parses_like_upstream() { + for (value, expected) in [ + ("3", Some(3)), + ("300", Some(128)), + ("0", None), + ("-2", None), + ] { + assert_eq!( + post_init("TCPClientInterface", value).hop_limiter, + expected, + "{value}" + ); + } + let mut section = ConfigSection::new(); + section.set("hop_limiter", "many"); + assert!(interface_factory::InterfacePostInit::try_from_section(§ion).is_err()); + for (kind, spawned) in [ + ("TCPServerInterface", true), + ("BackboneInterface", true), + ("I2PInterface", true), + ("AutoInterface", false), + ("RNodeMultiInterface", false), + ] { + assert_eq!(post_init(kind, "3").spawned_hop_limiter, spawned, "{kind}"); + } +} + +async fn registered_hop_limiter(transport_rx: &mut mpsc::Receiver) -> Option { + let msg = transport_rx + .recv() + .await + .map(unwrap_owned_event) + .expect("registration"); + let TransportMessage::RegisterInterface { entry, .. } = msg else { + panic!("expected RegisterInterface"); + }; + entry.hop_limiter +} + +/// Reticulum.py sets `hop_limiter` on the configured interface; TCP server, +/// Backbone and I2P listeners copy it to accepted connections, while +/// AutoInterface peers, RNodeMulti subinterfaces and configured I2P peers +/// go without (bb63dd44). +#[tokio::test] +async fn hop_limiter_reaches_the_interfaces_upstream_gives_it() { + let (transport_tx, mut transport_rx) = mpsc::channel::(4); + let interface_controls: InterfaceControlMap = Arc::new(std::sync::Mutex::new(HashMap::new())); + let interface_registry = InterfaceRegistry::default(); + for (parent_id, kind, child_limit) in [ + (940_001, "TCPServerInterface", Some(3)), + (940_011, "AutoInterface", None), + ] { + register_interface_with_post_init( + &transport_tx, + test_interface_handle(parent_id, None, kind), + &post_init(kind, "3"), + None, + &interface_controls, + &interface_registry, + InterfaceKind::Standard, + ) + .await + .unwrap(); + assert_eq!( + registered_hop_limiter(&mut transport_rx).await, + Some(3), + "{kind}" + ); + + let (role, inherited, ifac_key, ifac_size) = + child_registration_from_parent(&interface_controls, Some(parent_id)); + register_interface_handle_with_role_and_overrides( + &transport_tx, + test_interface_handle(parent_id + 1, Some(parent_id), "child"), + role, + inherited, + ifac_key, + ifac_size, + &interface_controls, + &interface_registry, + InterfaceKind::Standard, + false, + ) + .await + .unwrap(); + assert_eq!( + registered_hop_limiter(&mut transport_rx).await, + child_limit, + "{kind}" + ); + } + + register_interface_with_post_init( + &transport_tx, + test_interface_handle(940_021, Some(940_020), "RNodeMulti subinterface"), + &post_init("RNodeMultiInterface", "3"), + None, + &interface_controls, + &interface_registry, + InterfaceKind::Standard, + ) + .await + .unwrap(); + assert_eq!(registered_hop_limiter(&mut transport_rx).await, None); + + register_owned_interface_with_post_init( + &transport_tx, + OwnedInterfaceHandle { + configured_peer: true, + ..test_interface_handle(940_031, None, "I2P to peer").into() + }, + &post_init("I2PInterface", "3"), + None, + &interface_controls, + &interface_registry, + InterfaceKind::Standard, + None, + ) + .await + .unwrap(); + assert_eq!(registered_hop_limiter(&mut transport_rx).await, None); +} diff --git a/crates/rns-transport/src/actor/inbound.rs b/crates/rns-transport/src/actor/inbound.rs index bbbf0ae..753f5ff 100644 --- a/crates/rns-transport/src/actor/inbound.rs +++ b/crates/rns-transport/src/actor/inbound.rs @@ -300,8 +300,15 @@ impl TransportActor { return; } - // Transport.HOP_LIMITER, on wire hops (Transport.py). - if parsed.hops >= self.hop_limiter { + // Interface.hop_limiter, then Transport.HOP_LIMITER, on wire hops (Transport.py). + let interface_limit = self + .interfaces + .get(&packet.interface_id) + .and_then(|entry| entry.hop_limiter) + .filter(|limit| *limit > 0); + if interface_limit.is_some_and(|limit| parsed.hops >= limit) + || parsed.hops >= self.hop_limiter + { trace!( hops = parsed.hops, interface_id = packet.interface_id, diff --git a/crates/rns-transport/src/actor/mod.rs b/crates/rns-transport/src/actor/mod.rs index 4ff9b03..71d218c 100644 --- a/crates/rns-transport/src/actor/mod.rs +++ b/crates/rns-transport/src/actor/mod.rs @@ -2624,6 +2624,7 @@ mod tests { mod discovery_api; mod discovery_public_api; mod held_announces; + mod hop_limiter; mod inbound_queues; mod interface_stats; mod link_endpoint_dispatch; diff --git a/crates/rns-transport/src/actor/tests/hop_limiter.rs b/crates/rns-transport/src/actor/tests/hop_limiter.rs new file mode 100644 index 0000000..f1a1692 --- /dev/null +++ b/crates/rns-transport/src/actor/tests/hop_limiter.rs @@ -0,0 +1,32 @@ +//! Per-interface hop limits on inbound packets (Transport.py 1.6.1, bb63dd44). + +use super::*; + +/// The interface's limit applies before the instance-wide one, on wire hops; +/// 0 is off, as `None` is. +#[test] +fn interface_hop_limiter_drops_at_its_limit() { + let (mut actor, _tx) = TransportActor::new(); + let mut keep = Vec::new(); + for (id, limit) in [(1, Some(1)), (2, Some(0)), (3, None)] { + let (mut entry, rx) = make_test_interface(&format!("iface{id}")); + entry.hop_limiter = limit; + actor.interfaces.insert(id, entry); + keep.push(rx); + } + let mut learned = Vec::new(); + for id in 1..=3 { + for hops in [0, 1] { + let (raw, dest) = make_valid_announce(&format!("hoplimit.i{id}.h{hops}"), hops); + actor.on_inbound(InboundPacket { + raw, + interface_id: id, + rssi: None, + snr: None, + q: None, + }); + learned.push(actor.path_table.has_path(&dest)); + } + } + assert_eq!(learned, [true, false, true, true, true, true]); +} diff --git a/crates/rns-transport/src/messages.rs b/crates/rns-transport/src/messages.rs index 22db0de..e3b81e0 100644 --- a/crates/rns-transport/src/messages.rs +++ b/crates/rns-transport/src/messages.rs @@ -428,6 +428,9 @@ pub struct InterfaceEntry { /// as on a radio, a serial line or UDP. A queued announce heard again on /// an interface that is not shared is not rebroadcast there. pub shared_medium: bool, + /// Packets arriving with this many wire hops or more are dropped before + /// the instance-wide limit; `None` or 0 is off (Transport.py). + pub hop_limiter: Option, /// Configured announce stamp requirement; `None` follows /// `StampPolicy::destination_cost`. Resolve with `StampPolicy::interface_destination_cost`. pub destination_cost: Option, @@ -520,6 +523,7 @@ impl InterfaceEntry { announces_to_internal: None, gravity: 0, shared_medium: false, + hop_limiter: None, destination_cost: None, pr_cost: None, received: ReceivedBytes::default(),