mirror of
https://github.com/ratspeak/rsReticulum
synced 2026-11-27 13:52:16 +00:00
runtime: report interface startup failures and qualify strict sharing
This commit is contained in:
1 parent
c0fa43664b
commit
795058e5bf
5 files changed
+216
-1
No files matched your search
@@ -39,6 +39,36 @@ deadline and validated identity recall. Long-lived announce observation uses
|
||||
owns exactly one registration. Finite lookups do not install or remove
|
||||
announce handlers.
|
||||
|
||||
## Explicit shared-instance ownership
|
||||
|
||||
Applications that require authenticated shared control can opt into
|
||||
`reticulum::init_with_policy` and `shared_instance::InstancePolicy`:
|
||||
|
||||
- `Configured` retains the normal config-driven automatic owner/client behavior.
|
||||
- `Standalone` owns local interfaces without exposing shared IPC.
|
||||
- `SharedOwner` binds both configured shared endpoints or fails; it never joins
|
||||
another owner. `SharedOwnerAt(endpoint)` selects them in memory without
|
||||
rewriting the configuration file.
|
||||
- `SharedClient(credentials)` requires the selected packet endpoint and
|
||||
authenticated interface-status RPC before becoming ready. It reauthenticates
|
||||
on reconnect and never falls back to local interfaces.
|
||||
|
||||
`SharedInstanceEndpoint::Tcp` is loopback-only. Explicit Unix endpoints use
|
||||
Linux/Android abstract instance names. Keys are opaque HMAC bytes, supplied via
|
||||
`SharedInstanceCredentials::new`; its debug representation redacts the key.
|
||||
`credentials.test().await` checks availability without starting a runtime.
|
||||
|
||||
Observe reconnect/authentication state through `shared_instance_state()` and
|
||||
typed control failures through `query_control_result()`. Client runtimes reject
|
||||
dynamic local interface spawns. Non-fatal configured-interface startup failures
|
||||
are available through `startup_interface_failures()`; other interfaces can
|
||||
remain usable.
|
||||
|
||||
Upstream packet IPC is unauthenticated: successful RPC authenticates the control
|
||||
endpoint, not the identity of the packet socket. Selecting a trustworthy matching
|
||||
pair remains the application's responsibility. These policies do not move
|
||||
AutoInterface's interoperable UDP ports or alter Reticulum wire formats.
|
||||
|
||||
## Stability
|
||||
|
||||
The application prelude is the recommended integration path, but the workspace
|
||||
|
||||
Generated
+1
@@ -1255,6 +1255,7 @@ dependencies = [
|
||||
"thiserror 2.0.20",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -39,3 +39,23 @@ pub mod legacy {
|
||||
let _ = std::mem::size_of::<LinkSessionHandle>();
|
||||
}
|
||||
}
|
||||
|
||||
/// Strict application ownership is additive; legacy imports above remain valid.
|
||||
pub mod shared_ownership {
|
||||
use rns_runtime::shared_instance::{
|
||||
InstancePolicy, SharedInstanceCredentials, SharedInstanceEndpoint,
|
||||
};
|
||||
|
||||
pub fn compile_surface() {
|
||||
let endpoint = SharedInstanceEndpoint::Tcp {
|
||||
packet_port: 37428,
|
||||
control_port: 37429,
|
||||
};
|
||||
let credentials = SharedInstanceCredentials::new(endpoint.clone(), vec![1; 17]).unwrap();
|
||||
let _ = InstancePolicy::SharedClient(credentials);
|
||||
let _ = InstancePolicy::SharedOwnerAt(endpoint);
|
||||
let _ = rns_runtime::reticulum::init_with_policy;
|
||||
let _ = rns_runtime::reticulum::ReticulumHandle::shared_instance_state;
|
||||
let _ = rns_runtime::reticulum::ReticulumHandle::startup_interface_failures;
|
||||
}
|
||||
}
|
||||
@@ -407,6 +407,7 @@ pub struct ReticulumHandle {
|
||||
/// still install a stamper and start publishing.
|
||||
pub discovery: Arc<DiscoveryRuntime>,
|
||||
startup_rnode_runtimes: Vec<StartupRNodeRuntime>,
|
||||
startup_interface_failures: Vec<(String, String)>,
|
||||
#[cfg(feature = "ble")]
|
||||
deferred_android_ble_rnodes: Vec<interface_factory::BleRNodeInterfaceConfig>,
|
||||
shutdown_coordinator: RuntimeShutdownCoordinator,
|
||||
@@ -1080,6 +1081,13 @@ struct LocalDiscoveryInterface {
|
||||
}
|
||||
|
||||
impl ReticulumHandle {
|
||||
/// Non-fatal configured-interface startup failures, as `(name, reason)`.
|
||||
/// Other interfaces remain usable. Empty for shared clients, which do not
|
||||
/// start the caller's configured interfaces.
|
||||
pub fn startup_interface_failures(&self) -> &[(String, String)] {
|
||||
&self.startup_interface_failures
|
||||
}
|
||||
|
||||
/// Last authenticated shared-client state. Generic automatic clients do
|
||||
/// not opt into this policy and return `None`.
|
||||
pub fn shared_instance_state(&self) -> Option<SharedInstanceState> {
|
||||
@@ -2819,6 +2827,10 @@ pub async fn init_with_policy(
|
||||
InstancePolicy::Configured => {}
|
||||
InstancePolicy::Standalone => rc.share_instance = false,
|
||||
InstancePolicy::SharedOwner => rc.share_instance = true,
|
||||
InstancePolicy::SharedOwnerAt(endpoint) => {
|
||||
endpoint.validate()?;
|
||||
endpoint.apply(&mut rc);
|
||||
}
|
||||
InstancePolicy::SharedClient(credentials) => credentials.apply(&mut rc),
|
||||
}
|
||||
|
||||
@@ -2948,7 +2960,9 @@ pub async fn init_with_policy(
|
||||
let configured_policy = matches!(policy, InstancePolicy::Configured);
|
||||
let instance_mode = if matches!(
|
||||
policy,
|
||||
InstancePolicy::SharedOwner | InstancePolicy::SharedClient(_)
|
||||
InstancePolicy::SharedOwner
|
||||
| InstancePolicy::SharedOwnerAt(_)
|
||||
| InstancePolicy::SharedClient(_)
|
||||
) {
|
||||
// This extra permit covers control-listener installation after packet
|
||||
// registration consumes its own permit.
|
||||
@@ -3340,6 +3354,7 @@ pub async fn init_with_policy(
|
||||
*discovery_runtime.store.lock().await = Some(Arc::new(store));
|
||||
}
|
||||
let mut startup_rnode_runtimes = Vec::new();
|
||||
let mut startup_interface_failures = Vec::new();
|
||||
#[cfg(feature = "ble")]
|
||||
let deferred_android_ble_rnodes = collect_deferred_android_ble_rnodes(
|
||||
&interfaces,
|
||||
@@ -3435,6 +3450,10 @@ pub async fn init_with_policy(
|
||||
return Err(ReticulumError::Interface(error.to_string()).into());
|
||||
}
|
||||
Err(error) => {
|
||||
startup_interface_failures.push((
|
||||
interface_config_name(iface_config).to_string(),
|
||||
error.to_string(),
|
||||
));
|
||||
tracing::warn!("failed to register interface: {error}");
|
||||
}
|
||||
}
|
||||
@@ -3446,6 +3465,8 @@ pub async fn init_with_policy(
|
||||
return Err(ReticulumError::Interface(e).into());
|
||||
} else {
|
||||
drop(spawn_permit);
|
||||
startup_interface_failures
|
||||
.push((interface_config_name(iface_config).to_string(), e.clone()));
|
||||
tracing::warn!("failed to spawn interface: {}", e);
|
||||
}
|
||||
}
|
||||
@@ -3470,6 +3491,7 @@ pub async fn init_with_policy(
|
||||
network_identity: network_identity.clone(),
|
||||
discovery: discovery_runtime,
|
||||
startup_rnode_runtimes,
|
||||
startup_interface_failures,
|
||||
#[cfg(feature = "ble")]
|
||||
deferred_android_ble_rnodes,
|
||||
shutdown_coordinator: shutdown_coordinator.clone(),
|
||||
@@ -9655,6 +9677,7 @@ loglevel = 7
|
||||
network_identity: None,
|
||||
discovery: Arc::new(DiscoveryRuntime::default()),
|
||||
startup_rnode_runtimes: Vec::new(),
|
||||
startup_interface_failures: Vec::new(),
|
||||
#[cfg(feature = "ble")]
|
||||
deferred_android_ble_rnodes: Vec::new(),
|
||||
shutdown_coordinator,
|
||||
|
||||
@@ -222,6 +222,8 @@ pub enum InstancePolicy {
|
||||
Standalone,
|
||||
/// Bind both configured local listeners or fail; never become a client.
|
||||
SharedOwner,
|
||||
/// Bind an explicitly selected local endpoint pair, without rewriting config.
|
||||
SharedOwnerAt(SharedInstanceEndpoint),
|
||||
/// Require both selected endpoints; never start local interfaces.
|
||||
SharedClient(SharedInstanceCredentials),
|
||||
}
|
||||
@@ -725,4 +727,143 @@ mod tests {
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn missing_or_silent_rpc_drops_unauthenticated_packet_connection() {
|
||||
let packet = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let (_, control_port) = ports();
|
||||
let credentials = credentials(packet.local_addr().unwrap().port(), control_port, 1);
|
||||
assert_eq!(
|
||||
credentials.test().await,
|
||||
Err(SharedInstanceError::ControlUnavailable)
|
||||
);
|
||||
let (mut stream, _) = packet.accept().await.unwrap();
|
||||
assert_eq!(stream.read(&mut [0]).await.unwrap(), 0);
|
||||
|
||||
let control = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, control_port))
|
||||
.await
|
||||
.unwrap();
|
||||
let test = tokio::spawn(async move { credentials.test().await });
|
||||
let (mut stream, _) = packet.accept().await.unwrap();
|
||||
let (mut silent_control, _) = control.accept().await.unwrap();
|
||||
assert!(matches!(
|
||||
test.await.unwrap(),
|
||||
Err(SharedInstanceError::TimedOut | SharedInstanceError::ControlUnavailable)
|
||||
));
|
||||
assert_eq!(stream.read(&mut [0]).await.unwrap(), 0);
|
||||
assert_eq!(silent_control.read(&mut [0]).await.unwrap(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancelled_admission_closes_both_connections() {
|
||||
let packet = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let control = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let credentials = credentials(
|
||||
packet.local_addr().unwrap().port(),
|
||||
control.local_addr().unwrap().port(),
|
||||
1,
|
||||
);
|
||||
let shutdown = ShutdownSignal::new();
|
||||
let task_shutdown = shutdown.clone();
|
||||
let (tx, _rx) = mpsc::channel(8);
|
||||
let task = tokio::spawn(async move {
|
||||
spawn_authenticated_client(credentials, 1, tx, task_shutdown).await
|
||||
});
|
||||
let (mut packet, _) = packet.accept().await.unwrap();
|
||||
let (mut control, _) = control.accept().await.unwrap();
|
||||
shutdown.trigger();
|
||||
assert!(matches!(
|
||||
task.await.unwrap(),
|
||||
Err(SharedInstanceError::Cancelled)
|
||||
));
|
||||
assert_eq!(packet.read(&mut [0]).await.unwrap(), 0);
|
||||
assert_eq!(control.read(&mut [0]).await.unwrap(), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn explicit_owner_selectors_are_in_memory_and_reusable_after_shutdown() {
|
||||
let (packet_port, control_port) = ports();
|
||||
let config = TestConfig::new(1, 2, 1);
|
||||
let original = std::fs::read(config.0.join("config")).unwrap();
|
||||
let endpoint = SharedInstanceEndpoint::Tcp {
|
||||
packet_port,
|
||||
control_port,
|
||||
};
|
||||
let owner = config
|
||||
.start(InstancePolicy::SharedOwnerAt(endpoint.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
SharedInstanceCredentials::new(endpoint, vec![1; 32])
|
||||
.unwrap()
|
||||
.test()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(std::fs::read(config.0.join("config")).unwrap(), original);
|
||||
owner.shutdown_and_wait().await;
|
||||
let _packet = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, packet_port))
|
||||
.await
|
||||
.unwrap();
|
||||
let _control = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, control_port))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "linux", target_os = "android"))]
|
||||
#[tokio::test]
|
||||
async fn abstract_unix_owner_and_authenticated_client_roundtrip() {
|
||||
let config = TestConfig::new(1, 2, 1);
|
||||
let endpoint = SharedInstanceEndpoint::Unix {
|
||||
instance_name: format!(
|
||||
"strict-test-{}",
|
||||
hex::encode(rns_crypto::random::random_bytes(12))
|
||||
),
|
||||
};
|
||||
let owner = config
|
||||
.start(InstancePolicy::SharedOwnerAt(endpoint.clone()))
|
||||
.await
|
||||
.unwrap();
|
||||
let client_config = TestConfig::new(3, 4, 9);
|
||||
let credentials = SharedInstanceCredentials::new(endpoint, vec![1; 32]).unwrap();
|
||||
credentials.test().await.unwrap();
|
||||
let client = client_config
|
||||
.start(InstancePolicy::SharedClient(credentials))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
client.shared_instance_state(),
|
||||
Some(SharedInstanceState::Ready)
|
||||
);
|
||||
assert!(matches!(
|
||||
client
|
||||
.query_control_result(TransportQuery::GetInterfaceStats)
|
||||
.await,
|
||||
Ok(TransportQueryResponse::InterfaceStats(_))
|
||||
));
|
||||
client.shutdown_and_wait().await;
|
||||
owner.shutdown_and_wait().await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn occupied_auto_port_is_reported_without_breaking_tcp() {
|
||||
let auto = std::net::UdpSocket::bind("[::]:0").unwrap();
|
||||
let data_port = auto.local_addr().unwrap().port();
|
||||
let (tcp_port, _) = ports();
|
||||
let config = TestConfig::new(1, 2, 1);
|
||||
std::fs::write(config.0.join("config"), format!(
|
||||
"[reticulum]\nshare_instance = No\n[interfaces]\n[[LAN]]\ntype = AutoInterface\nenabled = Yes\ndata_port = {data_port}\n[[TCP]]\ntype = TCPServerInterface\nenabled = Yes\nlisten_ip = 127.0.0.1\nlisten_port = {tcp_port}\n"
|
||||
)).unwrap();
|
||||
let runtime = config.start(InstancePolicy::Standalone).await.unwrap();
|
||||
assert_eq!(runtime.startup_interface_failures().len(), 1);
|
||||
assert_eq!(runtime.startup_interface_failures()[0].0, "LAN");
|
||||
assert!(
|
||||
runtime.startup_interface_failures()[0]
|
||||
.1
|
||||
.contains("socket bind")
|
||||
);
|
||||
let _tcp = tokio::net::TcpStream::connect((std::net::Ipv4Addr::LOCALHOST, tcp_port))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(auto.local_addr().unwrap().port(), data_port);
|
||||
runtime.shutdown_and_wait().await;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user