Skip to main content

node_device_session/webrtc/
carrier.rs

1//! Carrier-specific inputs for the shared WebRTC answerer.
2
3use super::portmap::{NoSessionPortMapper, SessionPortMapper};
4use super::WebRtcHandle;
5use crate::messages::OpenOk;
6use async_trait::async_trait;
7use std::net::SocketAddr;
8use std::sync::Arc;
9use std::time::Instant;
10
11/// What a carrier must supply to run the shared WebRTC answerer.
12///
13/// Wire protocol, str0m session, DataChannel framing, and HTTP/WS tunnels are
14/// shared. Only ICE environment and session bookkeeping stay carrier-owned.
15#[async_trait]
16pub trait WebRtcCarrier: Send + Sync {
17    /// Instant used for str0m and ICE timers.
18    fn now(&self) -> Instant {
19        Instant::now()
20    }
21
22    /// STUN server addresses used while gathering (no TURN).
23    async fn stun_servers(&self) -> Vec<SocketAddr>;
24
25    /// Optional forced bind for the answerer's ICE UDP sockets.
26    fn bind_addr(&self) -> Option<SocketAddr> {
27        None
28    }
29
30    /// IPv4 session-port mapper (UPnP / carrier portmap). Default: unavailable.
31    fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
32        Arc::new(NoSessionPortMapper)
33    }
34
35    /// Description string passed to [`SessionPortMapper::map`].
36    fn mapping_description(&self) -> &str {
37        "webrtc"
38    }
39
40    /// Called after the handle is created and before the answerer runs.
41    ///
42    /// Carriers bind the handle into a session registry (revocation / replace).
43    /// Return `false` to abort without answering.
44    async fn bind_session(
45        &self,
46        _identity: &OpenOk,
47        _peer: [u8; 32],
48        _handle: &WebRtcHandle,
49    ) -> bool {
50        true
51    }
52}
53
54/// Minimal carrier: no STUN, no portmap, system clock.
55#[derive(Debug, Default, Clone, Copy)]
56pub struct NullWebRtcCarrier;
57
58#[async_trait]
59impl WebRtcCarrier for NullWebRtcCarrier {
60    async fn stun_servers(&self) -> Vec<SocketAddr> {
61        Vec::new()
62    }
63}
64
65/// Carrier with fixed STUN servers and an optional port mapper (tests / simple hosts).
66#[derive(Clone)]
67pub struct StaticWebRtcCarrier {
68    /// STUN addresses returned by [`WebRtcCarrier::stun_servers`].
69    pub stun: Vec<SocketAddr>,
70    /// Optional ICE UDP bind.
71    pub bind: Option<SocketAddr>,
72    /// Port mapper (defaults to [`NoSessionPortMapper`]).
73    pub mapper: Arc<dyn SessionPortMapper>,
74    /// Mapper description (e.g. `"iroh-webrtc"` / `"p2p-webrtc"`).
75    pub mapping_description: &'static str,
76}
77
78impl Default for StaticWebRtcCarrier {
79    fn default() -> Self {
80        Self {
81            stun: Vec::new(),
82            bind: None,
83            mapper: Arc::new(NoSessionPortMapper),
84            mapping_description: "webrtc",
85        }
86    }
87}
88
89#[async_trait]
90impl WebRtcCarrier for StaticWebRtcCarrier {
91    async fn stun_servers(&self) -> Vec<SocketAddr> {
92        self.stun.clone()
93    }
94
95    fn bind_addr(&self) -> Option<SocketAddr> {
96        self.bind
97    }
98
99    fn port_mapper(&self) -> Arc<dyn SessionPortMapper> {
100        self.mapper.clone()
101    }
102
103    fn mapping_description(&self) -> &str {
104        self.mapping_description
105    }
106}