iroh/
portmapper.rs

1//! Portmapper integration.
2//!
3//! Wraps the real [`portmapper`] crate when the `portmapper` feature is enabled,
4//! or provides a no-op stub otherwise.
5
6use std::net::SocketAddrV4;
7
8use tokio::sync::watch;
9
10/// Configuration for the portmapper service (UPnP, PCP, NAT-PMP).
11///
12/// Port mapping asks the local router to open an external port so peers can
13/// reach this endpoint directly, improving connectivity behind NATs. The
14/// discovery step (UPnP uses SSDP multicast) can, however, trigger firewall
15/// prompts on some networks — see [`PortmapperConfig::Disabled`].
16///
17/// Used with [`crate::endpoint::Builder::portmapper_config`].
18#[derive(Debug, Clone)]
19#[non_exhaustive]
20pub enum PortmapperConfig {
21    /// Enable portmapping with default settings.
22    ///
23    /// This is the default.
24    #[non_exhaustive]
25    Enabled {},
26    /// Disable portmapping.
27    ///
28    /// Skips the UPnP/PCP/NAT-PMP gateway probing. Use this to avoid the
29    /// SSDP multicast discovery that can raise firewall dialogs (notably on
30    /// macOS), at the cost of potentially worse direct connectivity behind
31    /// some NATs.
32    Disabled,
33}
34
35impl Default for PortmapperConfig {
36    fn default() -> Self {
37        PortmapperConfig::Enabled {}
38    }
39}
40
41pub(crate) fn create_client(config: &PortmapperConfig) -> Client {
42    match config {
43        #[cfg(all(not(wasm_browser), feature = "portmapper"))]
44        PortmapperConfig::Enabled {} => Client::Enabled(::portmapper::Client::default()),
45        _ => {
46            let (tx, rx) = watch::channel(None);
47            Client::Disabled { _tx: tx, rx }
48        }
49    }
50}
51
52/// Portmapper client: either the real implementation or a no-op.
53///
54/// The disabled variant is used when the `portmapper` feature is off, on wasm,
55/// or when portmapping is disabled via [`PortmapperConfig::Disabled`].
56#[derive(Debug)]
57pub(crate) enum Client {
58    /// The real portmapper client (requires the `portmapper` feature).
59    #[cfg(all(not(wasm_browser), feature = "portmapper"))]
60    Enabled(::portmapper::Client),
61    /// No-op: keeps the sender alive so the receiver never closes.
62    Disabled {
63        _tx: watch::Sender<Option<SocketAddrV4>>,
64        rx: watch::Receiver<Option<SocketAddrV4>>,
65    },
66}
67
68impl Client {
69    pub(crate) fn procure_mapping(&self) {
70        match self {
71            #[cfg(all(not(wasm_browser), feature = "portmapper"))]
72            Client::Enabled(c) => c.procure_mapping(),
73            Client::Disabled { .. } => {}
74        }
75    }
76
77    pub(crate) fn update_local_port(&self, _port: std::num::NonZeroU16) {
78        match self {
79            #[cfg(all(not(wasm_browser), feature = "portmapper"))]
80            Client::Enabled(c) => c.update_local_port(_port),
81            Client::Disabled { .. } => {}
82        }
83    }
84
85    pub(crate) fn deactivate(&self) {
86        match self {
87            #[cfg(all(not(wasm_browser), feature = "portmapper"))]
88            Client::Enabled(c) => c.deactivate(),
89            Client::Disabled { .. } => {}
90        }
91    }
92
93    pub(crate) fn watch_external_address(&self) -> watch::Receiver<Option<SocketAddrV4>> {
94        match self {
95            #[cfg(all(not(wasm_browser), feature = "portmapper"))]
96            Client::Enabled(c) => c.watch_external_address(),
97            Client::Disabled { rx, .. } => rx.clone(),
98        }
99    }
100}