Skip to main content

fips_core/config/
transport.rs

1//! Transport configuration types.
2//!
3//! Generic transport instance handling (single vs. named) and
4//! transport-specific configuration structs.
5
6use std::collections::HashMap;
7use std::net::{IpAddr, SocketAddr};
8
9use serde::{Deserialize, Serialize};
10
11/// Parse an `external_addr` config string against a known bind port,
12/// producing the absolute `SocketAddr` to advertise on Nostr.
13///
14/// Accepts either a bare IP (`"198.51.100.1"` or `"[::1]"`) — in which
15/// case the bind port is appended — or a full `host:port` form
16/// (`"198.51.100.1:443"` or `"[::1]:443"`). Returns `None` on any parse
17/// error. IPv6 must use bracket notation when supplying a port.
18fn parse_external_advert_addr(raw: &str, bind_port: u16) -> Option<SocketAddr> {
19    if let Ok(sa) = raw.parse::<SocketAddr>() {
20        return Some(sa);
21    }
22    let ip: IpAddr = raw.parse().ok()?;
23    Some(SocketAddr::new(ip, bind_port))
24}
25
26/// Extract the port from a `bind_addr` string. Returns `None` if the
27/// string can't be parsed (e.g. a bare hostname without port).
28fn parse_bind_port(raw: &str) -> Option<u16> {
29    raw.parse::<SocketAddr>().ok().map(|sa| sa.port())
30}
31
32/// Default UDP bind address.
33const DEFAULT_UDP_BIND_ADDR: &str = "0.0.0.0:2121";
34
35/// Default UDP MTU (IPv6 minimum).
36const DEFAULT_UDP_MTU: u16 = 1280;
37
38/// Default UDP receive buffer size (16 MiB).
39///
40/// At sustained multi-Gbps single-stream the kernel UDP queue
41/// drained ~113 kpps × ~1.5 KiB ≈ 170 MiB/s, so a few-hundred-ms
42/// userspace stall would fill a 2 MiB buffer in <20 ms — small
43/// enough that ordinary jitter (GC, allocator-coalesce, scheduler
44/// context-switch on a busy host) trips RcvbufErrors and tanks TCP
45/// throughput via cwnd-halving. 16 MiB gives ~100 ms of headroom.
46///
47/// On platforms whose `net.core.rmem_max` is smaller than this, the
48/// UDP socket layer falls back to SO_RCVBUFFORCE (CAP_NET_ADMIN
49/// required) before honouring the kernel ceiling. See
50/// `transport/udp/socket.rs::UdpRawSocket::open`.
51const DEFAULT_UDP_RECV_BUF: usize = 16 * 1024 * 1024;
52
53/// Default UDP send buffer size (8 MiB). Mirrors the receive-side
54/// reasoning at half the size — outbound burst absorption matters
55/// less because we control the producer rate via the rx_loop's
56/// per-drain sendmmsg flush.
57const DEFAULT_UDP_SEND_BUF: usize = 8 * 1024 * 1024;
58
59/// UDP transport instance configuration.
60#[derive(Debug, Clone, Default, Serialize, Deserialize)]
61#[serde(deny_unknown_fields)]
62pub struct UdpConfig {
63    /// Bind address (`bind_addr`). Defaults to "0.0.0.0:2121".
64    ///
65    /// When `outbound_only = true`, this field is ignored and the transport
66    /// binds to `0.0.0.0:0` (kernel-assigned ephemeral port) regardless.
67    #[serde(default, skip_serializing_if = "Option::is_none")]
68    pub bind_addr: Option<String>,
69
70    /// UDP MTU (`mtu`). Defaults to 1280 (IPv6 minimum).
71    #[serde(default, skip_serializing_if = "Option::is_none")]
72    pub mtu: Option<u16>,
73
74    /// UDP receive buffer size in bytes (`recv_buf_size`). Defaults to 16 MiB.
75    #[serde(default, skip_serializing_if = "Option::is_none")]
76    pub recv_buf_size: Option<usize>,
77
78    /// UDP send buffer size in bytes (`send_buf_size`). Defaults to 8 MiB.
79    #[serde(default, skip_serializing_if = "Option::is_none")]
80    pub send_buf_size: Option<usize>,
81
82    /// Whether this transport should be advertised on Nostr overlay discovery.
83    /// Default: false. Implicitly forced false when `outbound_only = true`.
84    #[serde(default, skip_serializing_if = "Option::is_none")]
85    pub advertise_on_nostr: Option<bool>,
86
87    /// Whether UDP should be advertised as directly reachable (`host:port`) on
88    /// Nostr. When false and advertised, UDP is emitted as `addr: "nat"` to
89    /// trigger rendezvous traversal.
90    ///
91    /// Default: false.
92    #[serde(default, skip_serializing_if = "Option::is_none")]
93    pub public: Option<bool>,
94    /// Optional explicit public address to advertise when `public: true`
95    /// is set. Takes precedence over both the bound address and any
96    /// STUN-derived autodiscovery. Accepts either a bare IP
97    /// (`"198.51.100.1"` — the configured `bind_addr` port is appended)
98    /// or a full `host:port` (`"198.51.100.1:443"`). Useful when the
99    /// public IP isn't on a local interface (e.g. AWS EIP / cloud 1:1
100    /// NAT) and the operator wants to skip STUN autodiscovery for a
101    /// deterministic value.
102    #[serde(default, skip_serializing_if = "Option::is_none")]
103    pub external_addr: Option<String>,
104    /// Outbound-only mode. When true, the transport binds to a kernel-
105    /// assigned ephemeral port (`0.0.0.0:0`) instead of the configured
106    /// `bind_addr`, refuses inbound handshake msg1, and is never
107    /// advertised on Nostr regardless of `advertise_on_nostr`. Use this
108    /// to participate in the mesh as a pure client — initiate outbound
109    /// links without exposing an inbound listener on a known port.
110    /// Default: false.
111    #[serde(default, skip_serializing_if = "Option::is_none")]
112    pub outbound_only: Option<bool>,
113
114    /// Accept inbound handshake msg1 from new peers. Default: true.
115    /// Setting this to false combined with `auto_connect: true` on
116    /// peer-side configurations gives a "client" posture: this node
117    /// initiates outbound links but refuses inbound handshakes from
118    /// unfamiliar addresses. The Node-level gate at
119    /// `src/node/handlers/handshake.rs` carves out msg1 from peers
120    /// already established on this transport (so rekey continues to
121    /// work) — see ISSUE-2026-0004.
122    #[serde(default, skip_serializing_if = "Option::is_none")]
123    pub accept_connections: Option<bool>,
124}
125
126impl UdpConfig {
127    /// Get the bind address, using default if not configured.
128    ///
129    /// When `outbound_only = true`, returns `0.0.0.0:0` so the kernel picks
130    /// an ephemeral source port and no listener is exposed on a known port.
131    pub fn bind_addr(&self) -> &str {
132        if self.outbound_only() {
133            "0.0.0.0:0"
134        } else {
135            self.bind_addr.as_deref().unwrap_or(DEFAULT_UDP_BIND_ADDR)
136        }
137    }
138
139    /// Get the UDP MTU, using default if not configured.
140    pub fn mtu(&self) -> u16 {
141        self.mtu.unwrap_or(DEFAULT_UDP_MTU)
142    }
143
144    /// Get the receive buffer size, using default if not configured.
145    pub fn recv_buf_size(&self) -> usize {
146        self.recv_buf_size.unwrap_or(DEFAULT_UDP_RECV_BUF)
147    }
148
149    /// Get the send buffer size, using default if not configured.
150    pub fn send_buf_size(&self) -> usize {
151        self.send_buf_size.unwrap_or(DEFAULT_UDP_SEND_BUF)
152    }
153
154    /// Whether this UDP transport should be advertised on Nostr discovery.
155    /// Always false when `outbound_only = true`.
156    pub fn advertise_on_nostr(&self) -> bool {
157        if self.outbound_only() {
158            false
159        } else {
160            self.advertise_on_nostr.unwrap_or(false)
161        }
162    }
163
164    /// Whether this UDP transport should be advertised as directly reachable.
165    pub fn is_public(&self) -> bool {
166        self.public.unwrap_or(false)
167    }
168
169    /// Parse `external_addr` against the configured `bind_addr` port,
170    /// returning the absolute `SocketAddr` to advertise on Nostr.
171    /// Returns `None` if `external_addr` is unset or malformed, or if
172    /// the port cannot be inferred.
173    pub fn external_advert_addr(&self) -> Option<SocketAddr> {
174        let raw = self.external_addr.as_deref()?;
175        let bind_port = parse_bind_port(self.bind_addr())?;
176        parse_external_advert_addr(raw, bind_port)
177    }
178
179    /// Whether this transport runs in outbound-only mode. Default: false.
180    pub fn outbound_only(&self) -> bool {
181        self.outbound_only.unwrap_or(false)
182    }
183
184    /// Whether this transport accepts inbound handshakes. Default: true.
185    pub fn accept_connections(&self) -> bool {
186        self.accept_connections.unwrap_or(true)
187    }
188}
189
190/// Default simulated transport MTU (IPv6 minimum).
191#[cfg(feature = "sim-transport")]
192const DEFAULT_SIM_MTU: u16 = 1280;
193
194/// Default simulated network registry name.
195#[cfg(feature = "sim-transport")]
196const DEFAULT_SIM_NETWORK: &str = "default";
197
198/// In-memory simulated transport instance configuration.
199///
200/// This transport is intended for production-backed simulations. It uses the
201/// normal node/session/routing stack, but delivers transport packets through a
202/// registered in-process network that can model latency, throughput, loss, and
203/// churn without binding real sockets.
204#[cfg(feature = "sim-transport")]
205#[derive(Debug, Clone, Default, Serialize, Deserialize)]
206#[serde(deny_unknown_fields)]
207pub struct SimTransportConfig {
208    /// Registry name of the in-process simulated network.
209    #[serde(default, skip_serializing_if = "Option::is_none")]
210    pub network: Option<String>,
211
212    /// Address of this simulated endpoint within the network.
213    #[serde(default, skip_serializing_if = "Option::is_none")]
214    pub addr: Option<String>,
215
216    /// Transport MTU. Defaults to 1280.
217    #[serde(default, skip_serializing_if = "Option::is_none")]
218    pub mtu: Option<u16>,
219
220    /// Whether discovery should auto-connect to discovered peers.
221    #[serde(default, skip_serializing_if = "Option::is_none")]
222    pub auto_connect: Option<bool>,
223
224    /// Accept inbound handshake msg1 from new peers. Default: true.
225    #[serde(default, skip_serializing_if = "Option::is_none")]
226    pub accept_connections: Option<bool>,
227}
228
229#[cfg(feature = "sim-transport")]
230impl SimTransportConfig {
231    /// Registry name of the in-process simulated network.
232    pub fn network(&self) -> &str {
233        self.network.as_deref().unwrap_or(DEFAULT_SIM_NETWORK)
234    }
235
236    /// Get the simulated MTU.
237    pub fn mtu(&self) -> u16 {
238        self.mtu.unwrap_or(DEFAULT_SIM_MTU)
239    }
240
241    /// Whether this transport auto-connects to discovered peers.
242    pub fn auto_connect(&self) -> bool {
243        self.auto_connect.unwrap_or(false)
244    }
245
246    /// Whether this transport accepts inbound handshakes.
247    pub fn accept_connections(&self) -> bool {
248        self.accept_connections.unwrap_or(true)
249    }
250}
251
252/// Transport instances - either a single config or named instances.
253///
254/// Allows both simple single-instance config:
255/// ```yaml
256/// transports:
257///   udp:
258///     bind_addr: "0.0.0.0:2121"
259/// ```
260///
261/// And multiple named instances:
262/// ```yaml
263/// transports:
264///   udp:
265///     main:
266///       bind_addr: "0.0.0.0:2121"
267///     backup:
268///       bind_addr: "192.168.1.100:2122"
269/// ```
270#[derive(Debug, Clone, Serialize, Deserialize)]
271#[serde(untagged)]
272pub enum TransportInstances<T> {
273    /// Single unnamed instance (config fields directly under transport type).
274    Single(T),
275    /// Multiple named instances.
276    Named(HashMap<String, T>),
277}
278
279impl<T> TransportInstances<T> {
280    /// Get the number of instances.
281    pub fn len(&self) -> usize {
282        match self {
283            TransportInstances::Single(_) => 1,
284            TransportInstances::Named(map) => map.len(),
285        }
286    }
287
288    /// Check if there are no instances.
289    pub fn is_empty(&self) -> bool {
290        match self {
291            TransportInstances::Single(_) => false,
292            TransportInstances::Named(map) => map.is_empty(),
293        }
294    }
295
296    /// Iterate over all instances as (name, config) pairs.
297    ///
298    /// Single instances have `None` as the name.
299    /// Named instances have `Some(name)`.
300    pub fn iter(&self) -> impl Iterator<Item = (Option<&str>, &T)> {
301        match self {
302            TransportInstances::Single(config) => vec![(None, config)].into_iter(),
303            TransportInstances::Named(map) => map
304                .iter()
305                .map(|(k, v)| (Some(k.as_str()), v))
306                .collect::<Vec<_>>()
307                .into_iter(),
308        }
309    }
310}
311
312impl<T> Default for TransportInstances<T> {
313    fn default() -> Self {
314        TransportInstances::Named(HashMap::new())
315    }
316}
317
318/// Default Ethernet EtherType (FIPS default).
319const DEFAULT_ETHERNET_ETHERTYPE: u16 = 0x2121;
320
321/// Default Ethernet receive buffer size (2 MB).
322const DEFAULT_ETHERNET_RECV_BUF: usize = 2 * 1024 * 1024;
323
324/// Default Ethernet send buffer size (2 MB).
325const DEFAULT_ETHERNET_SEND_BUF: usize = 2 * 1024 * 1024;
326
327/// Default beacon announcement interval in seconds.
328const DEFAULT_BEACON_INTERVAL_SECS: u64 = 30;
329
330/// Minimum beacon announcement interval in seconds.
331const MIN_BEACON_INTERVAL_SECS: u64 = 10;
332
333/// Ethernet transport instance configuration.
334///
335/// EthernetConfig is always compiled (for config parsing on any platform),
336/// but the transport runtime currently requires Linux or macOS raw sockets.
337#[derive(Debug, Clone, Default, Serialize, Deserialize)]
338#[serde(deny_unknown_fields)]
339pub struct EthernetConfig {
340    /// Network interface name (e.g., "eth0", "enp3s0"). Required.
341    pub interface: String,
342
343    /// Custom EtherType (default: 0x2121).
344    #[serde(default, skip_serializing_if = "Option::is_none")]
345    pub ethertype: Option<u16>,
346
347    /// MTU override. Defaults to the interface's MTU minus 1 (for frame type prefix).
348    /// Cannot exceed the interface's actual MTU.
349    #[serde(default, skip_serializing_if = "Option::is_none")]
350    pub mtu: Option<u16>,
351
352    /// Receive buffer size in bytes. Default: 2 MB.
353    #[serde(default, skip_serializing_if = "Option::is_none")]
354    pub recv_buf_size: Option<usize>,
355
356    /// Send buffer size in bytes. Default: 2 MB.
357    #[serde(default, skip_serializing_if = "Option::is_none")]
358    pub send_buf_size: Option<usize>,
359
360    /// Listen for discovery beacons from other nodes. Default: true.
361    #[serde(default, skip_serializing_if = "Option::is_none")]
362    pub discovery: Option<bool>,
363
364    /// Broadcast announcement beacons on the LAN. Default: false.
365    #[serde(default, skip_serializing_if = "Option::is_none")]
366    pub announce: Option<bool>,
367
368    /// Auto-connect to discovered peers. Default: false.
369    #[serde(default, skip_serializing_if = "Option::is_none")]
370    pub auto_connect: Option<bool>,
371
372    /// Accept incoming connection attempts. Default: false.
373    #[serde(default, skip_serializing_if = "Option::is_none")]
374    pub accept_connections: Option<bool>,
375
376    /// Optional discovery scope carried in Ethernet beacons.
377    ///
378    /// When set, this transport ignores Ethernet beacons from other scopes.
379    /// This is a discovery/noise filter, not an access-control mechanism. If
380    /// unset, the node-level LAN discovery scope is used when available.
381    #[serde(default, skip_serializing_if = "Option::is_none")]
382    pub discovery_scope: Option<String>,
383
384    /// Announcement beacon interval in seconds. Default: 30.
385    #[serde(default, skip_serializing_if = "Option::is_none")]
386    pub beacon_interval_secs: Option<u64>,
387}
388
389impl EthernetConfig {
390    /// Get the EtherType, using default if not configured.
391    pub fn ethertype(&self) -> u16 {
392        self.ethertype.unwrap_or(DEFAULT_ETHERNET_ETHERTYPE)
393    }
394
395    /// Get the receive buffer size, using default if not configured.
396    pub fn recv_buf_size(&self) -> usize {
397        self.recv_buf_size.unwrap_or(DEFAULT_ETHERNET_RECV_BUF)
398    }
399
400    /// Get the send buffer size, using default if not configured.
401    pub fn send_buf_size(&self) -> usize {
402        self.send_buf_size.unwrap_or(DEFAULT_ETHERNET_SEND_BUF)
403    }
404
405    /// Whether to listen for discovery beacons. Default: true.
406    pub fn discovery(&self) -> bool {
407        self.discovery.unwrap_or(true)
408    }
409
410    /// Whether to broadcast announcement beacons. Default: false.
411    pub fn announce(&self) -> bool {
412        self.announce.unwrap_or(false)
413    }
414
415    /// Whether to auto-connect to discovered peers. Default: false.
416    pub fn auto_connect(&self) -> bool {
417        self.auto_connect.unwrap_or(false)
418    }
419
420    /// Whether to accept incoming connections. Default: false.
421    pub fn accept_connections(&self) -> bool {
422        self.accept_connections.unwrap_or(false)
423    }
424
425    /// Optional discovery scope for Ethernet beacons.
426    pub fn discovery_scope(&self) -> Option<&str> {
427        self.discovery_scope.as_deref().filter(|s| !s.is_empty())
428    }
429
430    /// Get the beacon interval, clamped to minimum. Default: 30s.
431    pub fn beacon_interval_secs(&self) -> u64 {
432        self.beacon_interval_secs
433            .unwrap_or(DEFAULT_BEACON_INTERVAL_SECS)
434            .max(MIN_BEACON_INTERVAL_SECS)
435    }
436}
437
438// ============================================================================
439// TCP Transport Configuration
440// ============================================================================
441
442/// Default TCP MTU (conservative, matches typical Ethernet MSS minus overhead).
443const DEFAULT_TCP_MTU: u16 = 1400;
444
445/// Default TCP connect timeout in milliseconds.
446const DEFAULT_TCP_CONNECT_TIMEOUT_MS: u64 = 5000;
447
448/// Default TCP keepalive interval in seconds.
449const DEFAULT_TCP_KEEPALIVE_SECS: u64 = 30;
450
451/// Default TCP receive buffer size (2 MB).
452const DEFAULT_TCP_RECV_BUF: usize = 2 * 1024 * 1024;
453
454/// Default TCP send buffer size (2 MB).
455const DEFAULT_TCP_SEND_BUF: usize = 2 * 1024 * 1024;
456
457/// Default maximum inbound TCP connections.
458const DEFAULT_TCP_MAX_INBOUND: usize = 256;
459
460/// TCP transport instance configuration.
461#[derive(Debug, Clone, Default, Serialize, Deserialize)]
462#[serde(deny_unknown_fields)]
463pub struct TcpConfig {
464    /// Listen address (e.g., "0.0.0.0:443"). If not set, outbound-only.
465    #[serde(default, skip_serializing_if = "Option::is_none")]
466    pub bind_addr: Option<String>,
467
468    /// Default MTU for TCP connections. Defaults to 1400.
469    /// Per-connection MTU is derived from TCP_MAXSEG when available.
470    #[serde(default, skip_serializing_if = "Option::is_none")]
471    pub mtu: Option<u16>,
472
473    /// Outbound connect timeout in milliseconds. Defaults to 5000.
474    #[serde(default, skip_serializing_if = "Option::is_none")]
475    pub connect_timeout_ms: Option<u64>,
476
477    /// Enable TCP_NODELAY (disable Nagle). Defaults to true.
478    #[serde(default, skip_serializing_if = "Option::is_none")]
479    pub nodelay: Option<bool>,
480
481    /// TCP keepalive interval in seconds. 0 = disabled. Defaults to 30.
482    #[serde(default, skip_serializing_if = "Option::is_none")]
483    pub keepalive_secs: Option<u64>,
484
485    /// TCP receive buffer size in bytes. Defaults to 2 MB.
486    #[serde(default, skip_serializing_if = "Option::is_none")]
487    pub recv_buf_size: Option<usize>,
488
489    /// TCP send buffer size in bytes. Defaults to 2 MB.
490    #[serde(default, skip_serializing_if = "Option::is_none")]
491    pub send_buf_size: Option<usize>,
492
493    /// Maximum simultaneous inbound connections. Defaults to 256.
494    #[serde(default, skip_serializing_if = "Option::is_none")]
495    pub max_inbound_connections: Option<usize>,
496
497    /// Whether this transport should be advertised on Nostr overlay discovery.
498    /// Default: false.
499    #[serde(default, skip_serializing_if = "Option::is_none")]
500    pub advertise_on_nostr: Option<bool>,
501
502    /// Optional explicit public address to advertise. Required when
503    /// `bind_addr` is wildcard (e.g. `"0.0.0.0:443"`) and
504    /// `advertise_on_nostr: true`, since TCP has no STUN equivalent
505    /// for autodiscovery. Accepts either a bare IP (`"198.51.100.1"`
506    /// — the configured `bind_addr` port is appended) or a full
507    /// `host:port`. Common pattern on AWS EIP / cloud 1:1 NAT setups
508    /// where the public IP isn't bindable on the host.
509    #[serde(default, skip_serializing_if = "Option::is_none")]
510    pub external_addr: Option<String>,
511}
512
513impl TcpConfig {
514    /// Get the default MTU.
515    pub fn mtu(&self) -> u16 {
516        self.mtu.unwrap_or(DEFAULT_TCP_MTU)
517    }
518
519    /// Get the connect timeout in milliseconds.
520    pub fn connect_timeout_ms(&self) -> u64 {
521        self.connect_timeout_ms
522            .unwrap_or(DEFAULT_TCP_CONNECT_TIMEOUT_MS)
523    }
524
525    /// Whether TCP_NODELAY is enabled. Default: true.
526    pub fn nodelay(&self) -> bool {
527        self.nodelay.unwrap_or(true)
528    }
529
530    /// Get the keepalive interval in seconds. 0 = disabled. Default: 30.
531    pub fn keepalive_secs(&self) -> u64 {
532        self.keepalive_secs.unwrap_or(DEFAULT_TCP_KEEPALIVE_SECS)
533    }
534
535    /// Get the receive buffer size. Default: 2 MB.
536    pub fn recv_buf_size(&self) -> usize {
537        self.recv_buf_size.unwrap_or(DEFAULT_TCP_RECV_BUF)
538    }
539
540    /// Get the send buffer size. Default: 2 MB.
541    pub fn send_buf_size(&self) -> usize {
542        self.send_buf_size.unwrap_or(DEFAULT_TCP_SEND_BUF)
543    }
544
545    /// Get the maximum number of inbound connections. Default: 256.
546    pub fn max_inbound_connections(&self) -> usize {
547        self.max_inbound_connections
548            .unwrap_or(DEFAULT_TCP_MAX_INBOUND)
549    }
550
551    /// Whether this TCP transport should be advertised on Nostr discovery.
552    pub fn advertise_on_nostr(&self) -> bool {
553        self.advertise_on_nostr.unwrap_or(false)
554    }
555
556    /// Parse `external_addr` against the configured `bind_addr` port,
557    /// returning the absolute `SocketAddr` to advertise on Nostr.
558    /// Returns `None` if `external_addr` is unset or malformed, or if
559    /// `bind_addr` is unset / unparseable so no port can be inferred.
560    pub fn external_advert_addr(&self) -> Option<SocketAddr> {
561        let raw = self.external_addr.as_deref()?;
562        let bind_port = parse_bind_port(self.bind_addr.as_deref()?)?;
563        parse_external_advert_addr(raw, bind_port)
564    }
565}
566
567// ============================================================================
568// Tor Transport Configuration
569// ============================================================================
570
571/// Default Tor SOCKS5 proxy address.
572const DEFAULT_TOR_SOCKS5_ADDR: &str = "127.0.0.1:9050";
573
574/// Default Tor control port address.
575const DEFAULT_TOR_CONTROL_ADDR: &str = "/run/tor/control";
576
577/// Default Tor control cookie file path (Debian standard location).
578const DEFAULT_TOR_COOKIE_PATH: &str = "/var/run/tor/control.authcookie";
579
580/// Default Tor connect timeout in milliseconds (120s — Tor circuit
581/// establishment can take 30-60s on first connect, plus SOCKS5 handshake).
582const DEFAULT_TOR_CONNECT_TIMEOUT_MS: u64 = 120_000;
583
584/// Default Tor MTU (same as TCP).
585const DEFAULT_TOR_MTU: u16 = 1400;
586
587/// Default max inbound connections via onion service.
588const DEFAULT_TOR_MAX_INBOUND: usize = 64;
589
590/// Default HiddenServiceDir hostname file path.
591const DEFAULT_HOSTNAME_FILE: &str = "/var/lib/tor/fips_onion_service/hostname";
592
593/// Default directory mode bind address.
594const DEFAULT_DIRECTORY_BIND_ADDR: &str = "127.0.0.1:8443";
595
596/// Default advertised onion port for Nostr overlay discovery. Matches the
597/// Tor convention of `HiddenServicePort 443 127.0.0.1:<bind_port>` in torrc.
598const DEFAULT_TOR_ADVERTISED_PORT: u16 = 443;
599
600/// Tor transport instance configuration.
601///
602/// Supports three modes:
603/// - `socks5`: Outbound-only connections through a Tor SOCKS5 proxy.
604/// - `control_port`: Full bidirectional support — outbound via SOCKS5
605///   plus inbound via Tor onion service managed through the control port.
606/// - `directory`: Full bidirectional support — outbound via SOCKS5,
607///   inbound via a Tor-managed `HiddenServiceDir` onion service. No
608///   control port needed. Enables Tor `Sandbox 1` mode.
609#[derive(Debug, Clone, Default, Serialize, Deserialize)]
610#[serde(deny_unknown_fields)]
611pub struct TorConfig {
612    /// Tor access mode: "socks5", "control_port", or "directory".
613    /// Default: "socks5".
614    #[serde(default, skip_serializing_if = "Option::is_none")]
615    pub mode: Option<String>,
616
617    /// SOCKS5 proxy address (host:port). Defaults to "127.0.0.1:9050".
618    #[serde(default, skip_serializing_if = "Option::is_none")]
619    pub socks5_addr: Option<String>,
620
621    /// Outbound connect timeout in milliseconds. Defaults to 120000 (120s).
622    /// Tor circuit establishment can take 30-60s, so this must be generous.
623    #[serde(default, skip_serializing_if = "Option::is_none")]
624    pub connect_timeout_ms: Option<u64>,
625
626    /// Default MTU for Tor connections. Defaults to 1400.
627    #[serde(default, skip_serializing_if = "Option::is_none")]
628    pub mtu: Option<u16>,
629
630    /// Control port address: a Unix socket path (`/run/tor/control`) or
631    /// TCP address (`host:port`). Unix sockets are preferred for security.
632    /// Defaults to "/run/tor/control".
633    #[serde(default, skip_serializing_if = "Option::is_none")]
634    pub control_addr: Option<String>,
635
636    /// Control port authentication method:
637    /// `"cookie"` (read from default path),
638    /// `"cookie:/path/to/cookie"` (read from specified path), or
639    /// `"password:secret"` (password auth). Default: `"cookie"`.
640    #[serde(default, skip_serializing_if = "Option::is_none")]
641    pub control_auth: Option<String>,
642
643    /// Path to the Tor control cookie file. Used when control_auth is "cookie".
644    /// Defaults to "/var/run/tor/control.authcookie".
645    #[serde(default, skip_serializing_if = "Option::is_none")]
646    pub cookie_path: Option<String>,
647
648    /// Maximum number of inbound connections via onion service. Default: 64.
649    #[serde(default, skip_serializing_if = "Option::is_none")]
650    pub max_inbound_connections: Option<usize>,
651
652    /// Directory-mode onion service configuration. Only valid in
653    /// "directory" mode. Tor manages the onion service via HiddenServiceDir
654    /// in torrc; fips reads the .onion hostname from a file.
655    #[serde(default, skip_serializing_if = "Option::is_none")]
656    pub directory_service: Option<DirectoryServiceConfig>,
657
658    /// Whether this transport should be advertised on Nostr overlay discovery.
659    /// Default: false.
660    #[serde(default, skip_serializing_if = "Option::is_none")]
661    pub advertise_on_nostr: Option<bool>,
662
663    /// Public-facing onion port published in Nostr overlay adverts. Must
664    /// match the virtual port in torrc's `HiddenServicePort <port>
665    /// 127.0.0.1:<bind_port>` directive — that is the port other peers
666    /// will use to reach this onion. Default: 443.
667    #[serde(default, skip_serializing_if = "Option::is_none")]
668    pub advertised_port: Option<u16>,
669}
670
671/// Directory-mode onion service configuration.
672///
673/// In `directory` mode, Tor manages the onion service via `HiddenServiceDir`
674/// in torrc. FIPS reads the `.onion` address from the hostname file and
675/// binds a local TCP listener for Tor to forward inbound connections to.
676/// This mode requires no control port and enables Tor's `Sandbox 1`.
677#[derive(Debug, Clone, Default, Serialize, Deserialize)]
678#[serde(deny_unknown_fields)]
679pub struct DirectoryServiceConfig {
680    /// Path to the Tor-managed hostname file containing the .onion address.
681    /// Defaults to "/var/lib/tor/fips_onion_service/hostname".
682    #[serde(default, skip_serializing_if = "Option::is_none")]
683    pub hostname_file: Option<String>,
684
685    /// Local bind address for the listener that Tor forwards inbound
686    /// connections to. Must match the target in torrc's `HiddenServicePort`.
687    /// Defaults to "127.0.0.1:8443".
688    #[serde(default, skip_serializing_if = "Option::is_none")]
689    pub bind_addr: Option<String>,
690}
691
692impl DirectoryServiceConfig {
693    /// Path to the hostname file. Default: "/var/lib/tor/fips_onion_service/hostname".
694    pub fn hostname_file(&self) -> &str {
695        self.hostname_file
696            .as_deref()
697            .unwrap_or(DEFAULT_HOSTNAME_FILE)
698    }
699
700    /// Local bind address for the listener. Default: "127.0.0.1:8443".
701    pub fn bind_addr(&self) -> &str {
702        self.bind_addr
703            .as_deref()
704            .unwrap_or(DEFAULT_DIRECTORY_BIND_ADDR)
705    }
706}
707
708impl TorConfig {
709    /// Get the access mode. Default: "socks5".
710    pub fn mode(&self) -> &str {
711        self.mode.as_deref().unwrap_or("socks5")
712    }
713
714    /// Get the SOCKS5 proxy address. Default: "127.0.0.1:9050".
715    pub fn socks5_addr(&self) -> &str {
716        self.socks5_addr
717            .as_deref()
718            .unwrap_or(DEFAULT_TOR_SOCKS5_ADDR)
719    }
720
721    /// Get the control port address. Default: "/run/tor/control".
722    pub fn control_addr(&self) -> &str {
723        self.control_addr
724            .as_deref()
725            .unwrap_or(DEFAULT_TOR_CONTROL_ADDR)
726    }
727
728    /// Get the control auth string. Default: "cookie".
729    pub fn control_auth(&self) -> &str {
730        self.control_auth.as_deref().unwrap_or("cookie")
731    }
732
733    /// Get the cookie file path. Default: "/var/run/tor/control.authcookie".
734    pub fn cookie_path(&self) -> &str {
735        self.cookie_path
736            .as_deref()
737            .unwrap_or(DEFAULT_TOR_COOKIE_PATH)
738    }
739
740    /// Get the connect timeout in milliseconds. Default: 120000.
741    pub fn connect_timeout_ms(&self) -> u64 {
742        self.connect_timeout_ms
743            .unwrap_or(DEFAULT_TOR_CONNECT_TIMEOUT_MS)
744    }
745
746    /// Get the default MTU. Default: 1400.
747    pub fn mtu(&self) -> u16 {
748        self.mtu.unwrap_or(DEFAULT_TOR_MTU)
749    }
750
751    /// Get the max inbound connections. Default: 64.
752    pub fn max_inbound_connections(&self) -> usize {
753        self.max_inbound_connections
754            .unwrap_or(DEFAULT_TOR_MAX_INBOUND)
755    }
756
757    /// Whether this Tor transport should be advertised on Nostr discovery.
758    pub fn advertise_on_nostr(&self) -> bool {
759        self.advertise_on_nostr.unwrap_or(false)
760    }
761
762    /// Public-facing onion port published in Nostr overlay adverts.
763    /// Default: 443.
764    pub fn advertised_port(&self) -> u16 {
765        self.advertised_port.unwrap_or(DEFAULT_TOR_ADVERTISED_PORT)
766    }
767}
768
769// ============================================================================
770// WebRTC Transport Configuration
771// ============================================================================
772
773/// Default WebRTC data-channel MTU.
774const DEFAULT_WEBRTC_MTU: u16 = 1200;
775
776/// Default WebRTC connection timeout in milliseconds.
777const DEFAULT_WEBRTC_CONNECT_TIMEOUT_MS: u64 = 30_000;
778
779/// Default non-trickle ICE gathering timeout in milliseconds.
780const DEFAULT_WEBRTC_ICE_GATHER_TIMEOUT_MS: u64 = 10_000;
781
782/// Default maximum simultaneous WebRTC peer connections.
783const DEFAULT_WEBRTC_MAX_CONNECTIONS: usize = 32;
784
785/// Default WebRTC data channel label.
786const DEFAULT_WEBRTC_DATA_CHANNEL_LABEL: &str = "fips";
787
788/// WebRTC transport instance configuration.
789///
790/// WebRTC uses Nostr gift-wrapped signaling from `node.discovery.nostr` and
791/// carries ordinary FIPS datagrams over an SCTP data channel.
792#[derive(Debug, Clone, Default, Serialize, Deserialize)]
793#[serde(deny_unknown_fields)]
794pub struct WebRtcConfig {
795    /// Whether this transport should be advertised on Nostr overlay discovery.
796    /// Default: false.
797    #[serde(default, skip_serializing_if = "Option::is_none")]
798    pub advertise_on_nostr: Option<bool>,
799
800    /// Whether to automatically connect to discovered WebRTC peers.
801    /// Default: false.
802    #[serde(default, skip_serializing_if = "Option::is_none")]
803    pub auto_connect: Option<bool>,
804
805    /// Accept inbound WebRTC offers. Default: true.
806    #[serde(default, skip_serializing_if = "Option::is_none")]
807    pub accept_connections: Option<bool>,
808
809    /// Data-channel MTU. Defaults to 1200.
810    #[serde(default, skip_serializing_if = "Option::is_none")]
811    pub mtu: Option<u16>,
812
813    /// Maximum simultaneous WebRTC peer connections. Defaults to 32.
814    #[serde(default, skip_serializing_if = "Option::is_none")]
815    pub max_connections: Option<usize>,
816
817    /// Outbound connect timeout in milliseconds. Defaults to 30000.
818    #[serde(default, skip_serializing_if = "Option::is_none")]
819    pub connect_timeout_ms: Option<u64>,
820
821    /// Non-trickle ICE gathering timeout in milliseconds. Defaults to 10000.
822    #[serde(default, skip_serializing_if = "Option::is_none")]
823    pub ice_gather_timeout_ms: Option<u64>,
824
825    /// Data channel label. Defaults to "fips".
826    #[serde(default, skip_serializing_if = "Option::is_none")]
827    pub data_channel_label: Option<String>,
828
829    /// Ordered data channel delivery. Default: false.
830    #[serde(default, skip_serializing_if = "Option::is_none")]
831    pub ordered: Option<bool>,
832
833    /// Maximum retransmits for partial reliability. Default: 0.
834    /// Set to null/omit for the unreliable datagram-like default.
835    #[serde(default, skip_serializing_if = "Option::is_none")]
836    pub max_retransmits: Option<u16>,
837
838    /// Override signaling relays for this transport. When unset,
839    /// `node.discovery.nostr.dm_relays` is used.
840    #[serde(default, skip_serializing_if = "Option::is_none")]
841    pub signal_relays: Option<Vec<String>>,
842
843    /// Override STUN servers for this transport. When unset,
844    /// `node.discovery.nostr.stun_servers` is used.
845    #[serde(default, skip_serializing_if = "Option::is_none")]
846    pub stun_servers: Option<Vec<String>>,
847}
848
849impl WebRtcConfig {
850    /// Whether this WebRTC transport should be advertised on Nostr discovery.
851    pub fn advertise_on_nostr(&self) -> bool {
852        self.advertise_on_nostr.unwrap_or(false)
853    }
854
855    /// Whether this transport auto-connects to discovered peers.
856    pub fn auto_connect(&self) -> bool {
857        self.auto_connect.unwrap_or(false)
858    }
859
860    /// Whether this transport accepts inbound offers.
861    pub fn accept_connections(&self) -> bool {
862        self.accept_connections.unwrap_or(true)
863    }
864
865    /// Get the data-channel MTU.
866    pub fn mtu(&self) -> u16 {
867        self.mtu.unwrap_or(DEFAULT_WEBRTC_MTU)
868    }
869
870    /// Get the maximum number of peer connections.
871    pub fn max_connections(&self) -> usize {
872        self.max_connections
873            .unwrap_or(DEFAULT_WEBRTC_MAX_CONNECTIONS)
874    }
875
876    /// Get the connect timeout in milliseconds.
877    pub fn connect_timeout_ms(&self) -> u64 {
878        self.connect_timeout_ms
879            .unwrap_or(DEFAULT_WEBRTC_CONNECT_TIMEOUT_MS)
880    }
881
882    /// Get the ICE gathering timeout in milliseconds.
883    pub fn ice_gather_timeout_ms(&self) -> u64 {
884        self.ice_gather_timeout_ms
885            .unwrap_or(DEFAULT_WEBRTC_ICE_GATHER_TIMEOUT_MS)
886    }
887
888    /// Get the data channel label.
889    pub fn data_channel_label(&self) -> &str {
890        self.data_channel_label
891            .as_deref()
892            .unwrap_or(DEFAULT_WEBRTC_DATA_CHANNEL_LABEL)
893    }
894
895    /// Whether the data channel is ordered.
896    pub fn ordered(&self) -> bool {
897        self.ordered.unwrap_or(false)
898    }
899
900    /// Get the configured max retransmits. Default is 0.
901    pub fn max_retransmits(&self) -> u16 {
902        self.max_retransmits.unwrap_or(0)
903    }
904
905    /// Resolve signaling relays, falling back to node discovery relays.
906    pub fn signal_relays<'a>(&'a self, fallback: &'a [String]) -> Vec<String> {
907        self.signal_relays
908            .as_ref()
909            .filter(|relays| !relays.is_empty())
910            .cloned()
911            .unwrap_or_else(|| fallback.to_vec())
912    }
913
914    /// Resolve STUN servers, falling back to node discovery STUN servers.
915    pub fn stun_servers<'a>(&'a self, fallback: &'a [String]) -> Vec<String> {
916        self.stun_servers
917            .as_ref()
918            .filter(|servers| !servers.is_empty())
919            .cloned()
920            .unwrap_or_else(|| fallback.to_vec())
921    }
922}
923
924// ============================================================================
925// BLE Transport Configuration
926// ============================================================================
927
928/// Default BLE L2CAP PSM (dynamic range).
929const DEFAULT_BLE_PSM: u16 = 0x0085;
930
931/// Default BLE MTU for L2CAP CoC connections.
932const DEFAULT_BLE_MTU: u16 = 2048;
933
934/// Default maximum concurrent BLE connections.
935const DEFAULT_BLE_MAX_CONNECTIONS: usize = 7;
936
937/// Default BLE connect timeout in milliseconds.
938const DEFAULT_BLE_CONNECT_TIMEOUT_MS: u64 = 10_000;
939
940/// Default BLE probe cooldown in seconds. After probing an address
941/// (success or failure), wait this long before probing it again.
942const DEFAULT_BLE_PROBE_COOLDOWN_SECS: u64 = 30;
943
944/// BLE transport instance configuration.
945///
946/// BleConfig is always compiled (for config parsing on any platform),
947/// but the transport runtime requires Linux and the `ble` feature.
948#[derive(Debug, Clone, Default, Serialize, Deserialize)]
949#[serde(deny_unknown_fields)]
950pub struct BleConfig {
951    /// HCI adapter name (e.g., "hci0"). Required.
952    #[serde(default, skip_serializing_if = "Option::is_none")]
953    pub adapter: Option<String>,
954
955    /// L2CAP PSM for FIPS connections. Default: 0x0085 (133).
956    #[serde(default, skip_serializing_if = "Option::is_none")]
957    pub psm: Option<u16>,
958
959    /// Default MTU for BLE connections. Default: 2048.
960    #[serde(default, skip_serializing_if = "Option::is_none")]
961    pub mtu: Option<u16>,
962
963    /// Maximum concurrent BLE connections. Default: 7.
964    #[serde(default, skip_serializing_if = "Option::is_none")]
965    pub max_connections: Option<usize>,
966
967    /// Outbound connect timeout in milliseconds. Default: 10000.
968    #[serde(default, skip_serializing_if = "Option::is_none")]
969    pub connect_timeout_ms: Option<u64>,
970
971    /// Broadcast BLE advertisements. Default: true.
972    #[serde(default, skip_serializing_if = "Option::is_none")]
973    pub advertise: Option<bool>,
974
975    /// Listen for BLE advertisements. Default: true.
976    #[serde(default, skip_serializing_if = "Option::is_none")]
977    pub scan: Option<bool>,
978
979    /// Auto-connect to discovered BLE peers. Default: false.
980    #[serde(default, skip_serializing_if = "Option::is_none")]
981    pub auto_connect: Option<bool>,
982
983    /// Accept incoming BLE connections. Default: true.
984    #[serde(default, skip_serializing_if = "Option::is_none")]
985    pub accept_connections: Option<bool>,
986
987    /// Probe cooldown in seconds. After probing a BLE address, wait
988    /// this long before probing the same address again. Default: 30.
989    #[serde(default, skip_serializing_if = "Option::is_none")]
990    pub probe_cooldown_secs: Option<u64>,
991}
992
993impl BleConfig {
994    /// Get the adapter name. Default: "hci0".
995    pub fn adapter(&self) -> &str {
996        self.adapter.as_deref().unwrap_or("hci0")
997    }
998
999    /// Get the L2CAP PSM. Default: 0x0085.
1000    pub fn psm(&self) -> u16 {
1001        self.psm.unwrap_or(DEFAULT_BLE_PSM)
1002    }
1003
1004    /// Get the default MTU. Default: 2048.
1005    pub fn mtu(&self) -> u16 {
1006        self.mtu.unwrap_or(DEFAULT_BLE_MTU)
1007    }
1008
1009    /// Get the maximum concurrent connections. Default: 7.
1010    pub fn max_connections(&self) -> usize {
1011        self.max_connections.unwrap_or(DEFAULT_BLE_MAX_CONNECTIONS)
1012    }
1013
1014    /// Get the connect timeout in milliseconds. Default: 10000.
1015    pub fn connect_timeout_ms(&self) -> u64 {
1016        self.connect_timeout_ms
1017            .unwrap_or(DEFAULT_BLE_CONNECT_TIMEOUT_MS)
1018    }
1019
1020    /// Whether to broadcast advertisements. Default: true.
1021    pub fn advertise(&self) -> bool {
1022        self.advertise.unwrap_or(true)
1023    }
1024
1025    /// Whether to scan for advertisements. Default: true.
1026    pub fn scan(&self) -> bool {
1027        self.scan.unwrap_or(true)
1028    }
1029
1030    /// Whether to auto-connect to discovered peers. Default: false.
1031    pub fn auto_connect(&self) -> bool {
1032        self.auto_connect.unwrap_or(false)
1033    }
1034
1035    /// Whether to accept incoming connections. Default: true.
1036    pub fn accept_connections(&self) -> bool {
1037        self.accept_connections.unwrap_or(true)
1038    }
1039
1040    /// Get the probe cooldown in seconds. Default: 30.
1041    pub fn probe_cooldown_secs(&self) -> u64 {
1042        self.probe_cooldown_secs
1043            .unwrap_or(DEFAULT_BLE_PROBE_COOLDOWN_SECS)
1044    }
1045}
1046
1047// ============================================================================
1048// TransportsConfig
1049// ============================================================================
1050
1051/// Transports configuration section.
1052///
1053/// Each transport type can have either a single instance (config directly
1054/// under the type name) or multiple named instances.
1055#[derive(Debug, Clone, Default, Serialize, Deserialize)]
1056pub struct TransportsConfig {
1057    /// UDP transport instances.
1058    #[serde(default, skip_serializing_if = "is_transport_empty")]
1059    pub udp: TransportInstances<UdpConfig>,
1060
1061    /// In-memory simulated transport instances.
1062    #[cfg(feature = "sim-transport")]
1063    #[serde(default, skip_serializing_if = "is_transport_empty")]
1064    pub sim: TransportInstances<SimTransportConfig>,
1065
1066    /// Ethernet transport instances.
1067    #[serde(default, skip_serializing_if = "is_transport_empty")]
1068    pub ethernet: TransportInstances<EthernetConfig>,
1069
1070    /// TCP transport instances.
1071    #[serde(default, skip_serializing_if = "is_transport_empty")]
1072    pub tcp: TransportInstances<TcpConfig>,
1073
1074    /// Tor transport instances.
1075    #[serde(default, skip_serializing_if = "is_transport_empty")]
1076    pub tor: TransportInstances<TorConfig>,
1077
1078    /// WebRTC transport instances.
1079    #[serde(default, skip_serializing_if = "is_transport_empty")]
1080    pub webrtc: TransportInstances<WebRtcConfig>,
1081
1082    /// BLE transport instances.
1083    #[serde(default, skip_serializing_if = "is_transport_empty")]
1084    pub ble: TransportInstances<BleConfig>,
1085}
1086
1087/// Helper for skip_serializing_if on TransportInstances.
1088fn is_transport_empty<T>(instances: &TransportInstances<T>) -> bool {
1089    instances.is_empty()
1090}
1091
1092impl TransportsConfig {
1093    /// Check if any transports are configured.
1094    pub fn is_empty(&self) -> bool {
1095        self.udp.is_empty()
1096            && {
1097                #[cfg(feature = "sim-transport")]
1098                {
1099                    self.sim.is_empty()
1100                }
1101                #[cfg(not(feature = "sim-transport"))]
1102                {
1103                    true
1104                }
1105            }
1106            && self.ethernet.is_empty()
1107            && self.tcp.is_empty()
1108            && self.tor.is_empty()
1109            && self.webrtc.is_empty()
1110            && self.ble.is_empty()
1111    }
1112
1113    /// Merge another TransportsConfig into this one.
1114    ///
1115    /// Non-empty transport sections from `other` replace those in `self`.
1116    pub fn merge(&mut self, other: TransportsConfig) {
1117        if !other.udp.is_empty() {
1118            self.udp = other.udp;
1119        }
1120        #[cfg(feature = "sim-transport")]
1121        if !other.sim.is_empty() {
1122            self.sim = other.sim;
1123        }
1124        if !other.ethernet.is_empty() {
1125            self.ethernet = other.ethernet;
1126        }
1127        if !other.tcp.is_empty() {
1128            self.tcp = other.tcp;
1129        }
1130        if !other.tor.is_empty() {
1131            self.tor = other.tor;
1132        }
1133        if !other.webrtc.is_empty() {
1134            self.webrtc = other.webrtc;
1135        }
1136        if !other.ble.is_empty() {
1137            self.ble = other.ble;
1138        }
1139    }
1140}
1141
1142#[cfg(test)]
1143mod tests {
1144    use super::*;
1145
1146    #[test]
1147    fn parse_external_addr_accepts_bare_ipv4_with_appended_bind_port() {
1148        let sa = parse_external_advert_addr("198.51.100.1", 2121).unwrap();
1149        assert_eq!(sa.to_string(), "198.51.100.1:2121");
1150    }
1151
1152    #[test]
1153    fn parse_external_addr_accepts_full_ipv4_socket_addr() {
1154        let sa = parse_external_advert_addr("198.51.100.1:443", 2121).unwrap();
1155        assert_eq!(sa.to_string(), "198.51.100.1:443");
1156        // Explicit port wins over the bind port we passed in.
1157    }
1158
1159    #[test]
1160    fn parse_external_addr_accepts_bare_ipv6_with_appended_bind_port() {
1161        let sa = parse_external_advert_addr("2001:db8::1", 443).unwrap();
1162        assert_eq!(sa.to_string(), "[2001:db8::1]:443");
1163    }
1164
1165    #[test]
1166    fn parse_external_addr_accepts_bracketed_ipv6_with_explicit_port() {
1167        let sa = parse_external_advert_addr("[2001:db8::1]:8443", 443).unwrap();
1168        assert_eq!(sa.to_string(), "[2001:db8::1]:8443");
1169    }
1170
1171    #[test]
1172    fn parse_external_addr_rejects_garbage() {
1173        assert!(parse_external_advert_addr("not-an-ip", 443).is_none());
1174        assert!(parse_external_advert_addr("", 443).is_none());
1175    }
1176
1177    #[test]
1178    fn udp_external_advert_addr_combines_with_bind_port_default() {
1179        let cfg = UdpConfig {
1180            external_addr: Some("198.51.100.1".to_string()),
1181            ..UdpConfig::default()
1182        };
1183        // bind_addr unset, so default DEFAULT_UDP_BIND_ADDR (0.0.0.0:2121) applies.
1184        let sa = cfg.external_advert_addr().unwrap();
1185        assert_eq!(sa.to_string(), "198.51.100.1:2121");
1186    }
1187
1188    #[test]
1189    fn udp_external_advert_addr_with_explicit_full_socket_addr_overrides_bind_port() {
1190        let cfg = UdpConfig {
1191            bind_addr: Some("0.0.0.0:2121".to_string()),
1192            external_addr: Some("198.51.100.1:9999".to_string()),
1193            ..UdpConfig::default()
1194        };
1195        let sa = cfg.external_advert_addr().unwrap();
1196        assert_eq!(sa.to_string(), "198.51.100.1:9999");
1197    }
1198
1199    #[test]
1200    fn udp_external_advert_addr_returns_none_when_unset() {
1201        let cfg = UdpConfig::default();
1202        assert!(cfg.external_advert_addr().is_none());
1203    }
1204
1205    #[test]
1206    fn tcp_external_advert_addr_requires_bind_port() {
1207        let cfg = TcpConfig {
1208            external_addr: Some("198.51.100.1".to_string()),
1209            ..TcpConfig::default()
1210        };
1211        // bind_addr unset → no port to combine with → None.
1212        assert!(cfg.external_advert_addr().is_none());
1213
1214        let cfg = TcpConfig {
1215            bind_addr: Some("0.0.0.0:443".to_string()),
1216            external_addr: Some("198.51.100.1".to_string()),
1217            ..TcpConfig::default()
1218        };
1219        let sa = cfg.external_advert_addr().unwrap();
1220        assert_eq!(sa.to_string(), "198.51.100.1:443");
1221    }
1222
1223    #[test]
1224    fn tcp_external_advert_addr_with_full_socket_addr_independent_of_bind() {
1225        let cfg = TcpConfig {
1226            bind_addr: Some("0.0.0.0:443".to_string()),
1227            external_addr: Some("198.51.100.1:8443".to_string()),
1228            ..TcpConfig::default()
1229        };
1230        let sa = cfg.external_advert_addr().unwrap();
1231        assert_eq!(sa.to_string(), "198.51.100.1:8443");
1232    }
1233
1234    #[test]
1235    fn parse_bind_port_extracts_from_socket_addr_strings() {
1236        assert_eq!(parse_bind_port("0.0.0.0:2121"), Some(2121));
1237        assert_eq!(parse_bind_port("[::]:443"), Some(443));
1238        assert_eq!(parse_bind_port("not-a-socket-addr"), None);
1239    }
1240}