//! Portmapper integration.
//!
//! Wraps the real [`portmapper`] crate when the `portmapper` feature is enabled,
//! or provides a no-op stub otherwise.
use std::net::SocketAddrV4;
use tokio::sync::watch;
/// Configuration for the portmapper service (UPnP, PCP, NAT-PMP).
///
/// Port mapping asks the local router to open an external port so peers can
/// reach this endpoint directly, improving connectivity behind NATs. The
/// discovery step (UPnP uses SSDP multicast) can, however, trigger firewall
/// prompts on some networks — see [`PortmapperConfig::Disabled`].
///
/// Used with [`crate::endpoint::Builder::portmapper_config`].
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum PortmapperConfig {
/// Enable portmapping with default settings.
///
/// This is the default.
#[non_exhaustive]
Enabled {},
/// Disable portmapping.
///
/// Skips the UPnP/PCP/NAT-PMP gateway probing. Use this to avoid the
/// SSDP multicast discovery that can raise firewall dialogs (notably on
/// macOS), at the cost of potentially worse direct connectivity behind
/// some NATs.
Disabled,
}
impl Default for PortmapperConfig {
fn default() -> Self {
PortmapperConfig::Enabled {}
}
}
pub(crate) fn create_client(config: &PortmapperConfig) -> Client {
match config {
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
PortmapperConfig::Enabled {} => Client::Enabled(::portmapper::Client::default()),
_ => {
let (tx, rx) = watch::channel(None);
Client::Disabled { _tx: tx, rx }
}
}
}
/// Portmapper client: either the real implementation or a no-op.
///
/// The disabled variant is used when the `portmapper` feature is off, on wasm,
/// or when portmapping is disabled via [`PortmapperConfig::Disabled`].
#[derive(Debug)]
pub(crate) enum Client {
/// The real portmapper client (requires the `portmapper` feature).
#[cfg(all(not(wasm_browser), feature = "portmapper"))]
Enabled(::portmapper::Client),
/// No-op: keeps the sender alive so the receiver never closes.
Disabled {
_tx: watch::Sender