Skip to main content

fips_core/node/
mod.rs

1//! FIPS Node Entity
2//!
3//! Top-level structure representing a running FIPS instance. The Node
4//! holds all state required for mesh routing: identity, tree state,
5//! Bloom filters, coordinate caches, transports, links, and peers.
6
7mod acl;
8mod bloom;
9mod decrypt_worker;
10mod discovery_rate_limit;
11mod encrypt_worker;
12mod handlers;
13mod lifecycle;
14mod rate_limit;
15mod retry;
16mod routing;
17mod routing_error_rate_limit;
18pub(crate) mod session;
19pub(crate) mod session_wire;
20pub(crate) mod stats;
21pub(crate) mod stats_history;
22#[cfg(test)]
23mod tests;
24mod tree;
25pub(crate) mod wire;
26
27use self::discovery_rate_limit::{DiscoveryBackoff, DiscoveryForwardRateLimiter};
28use self::rate_limit::HandshakeRateLimiter;
29use self::routing::{LearnedRouteTable, LearnedRouteTableSnapshot};
30use self::routing_error_rate_limit::RoutingErrorRateLimiter;
31use self::wire::{
32    ESTABLISHED_HEADER_SIZE, FLAG_CE, FLAG_KEY_EPOCH, FLAG_SP, build_encrypted,
33    build_established_header, prepend_inner_header,
34};
35use crate::bloom::BloomState;
36use crate::cache::CoordCache;
37use crate::config::{NostrDiscoveryPolicy, PeerConfig, RoutingMode};
38use crate::node::session::SessionEntry;
39use crate::node::session_wire::{FSP_PHASE_ESTABLISHED, FspCommonPrefix};
40use crate::peer::{ActivePeer, PeerConnection};
41#[cfg(any(target_os = "linux", target_os = "macos"))]
42use crate::transport::ethernet::EthernetTransport;
43use crate::transport::tcp::TcpTransport;
44use crate::transport::tor::TorTransport;
45use crate::transport::udp::UdpTransport;
46#[cfg(feature = "webrtc-transport")]
47use crate::transport::webrtc::WebRtcTransport;
48use crate::transport::{
49    ConnectionState, Link, LinkId, PacketRx, PacketTx, TransportAddr, TransportError,
50    TransportHandle, TransportId,
51};
52use crate::tree::TreeState;
53use crate::upper::hosts::HostMap;
54use crate::upper::icmp_rate_limit::IcmpRateLimiter;
55use crate::upper::tun::{TunError, TunOutboundRx, TunState, TunTx};
56use crate::utils::index::IndexAllocator;
57use crate::{
58    Config, ConfigError, FipsAddress, Identity, IdentityError, LinkMessageType, NodeAddr,
59    PeerIdentity, encode_npub,
60};
61use rand::Rng;
62use std::collections::{HashMap, HashSet, VecDeque};
63use std::fmt;
64use std::sync::Arc;
65use std::thread::JoinHandle;
66use thiserror::Error;
67use tracing::{debug, warn};
68
69const LOCAL_SEND_FAILURE_FAST_DEAD_WINDOW: std::time::Duration = std::time::Duration::from_secs(3);
70
71fn fmp_plaintext_is_bulk_session_datagram(plaintext: &[u8]) -> bool {
72    if !plaintext
73        .first()
74        .is_some_and(|ty| *ty == LinkMessageType::SessionDatagram.to_byte())
75    {
76        return false;
77    }
78    let Some(fsp_payload) = plaintext.get(crate::protocol::SESSION_DATAGRAM_HEADER_SIZE..) else {
79        return false;
80    };
81    FspCommonPrefix::parse(fsp_payload)
82        .is_some_and(|prefix| prefix.phase == FSP_PHASE_ESTABLISHED && !prefix.is_unencrypted())
83}
84
85/// Half-range of the symmetric jitter applied to per-session rekey timers.
86///
87/// Each FMP/FSP session draws an offset uniformly from
88/// `[-REKEY_JITTER_SECS, +REKEY_JITTER_SECS]` seconds at construction and
89/// after each cutover. This preserves the configured mean interval while
90/// reducing dual-initiation bursts in symmetric-start meshes.
91pub(crate) const REKEY_JITTER_SECS: i64 = 15;
92
93/// Errors related to node operations.
94#[derive(Debug, Error)]
95pub enum NodeError {
96    #[error("node not started")]
97    NotStarted,
98
99    #[error("node already started")]
100    AlreadyStarted,
101
102    #[error("node already stopped")]
103    AlreadyStopped,
104
105    #[error("transport not found: {0}")]
106    TransportNotFound(TransportId),
107
108    #[error("no transport available for type: {0}")]
109    NoTransportForType(String),
110
111    #[error("link not found: {0}")]
112    LinkNotFound(LinkId),
113
114    #[error("connection not found: {0}")]
115    ConnectionNotFound(LinkId),
116
117    #[error("peer not found: {0:?}")]
118    PeerNotFound(NodeAddr),
119
120    #[error("peer already exists: {0:?}")]
121    PeerAlreadyExists(NodeAddr),
122
123    #[error("connection already exists for link: {0}")]
124    ConnectionAlreadyExists(LinkId),
125
126    #[error("invalid peer npub '{npub}': {reason}")]
127    InvalidPeerNpub { npub: String, reason: String },
128
129    #[error("discovery error: {0}")]
130    Discovery(String),
131
132    #[error("access denied: {0}")]
133    AccessDenied(String),
134
135    #[error("max connections exceeded: {max}")]
136    MaxConnectionsExceeded { max: usize },
137
138    #[error("max peers exceeded: {max}")]
139    MaxPeersExceeded { max: usize },
140
141    #[error("max links exceeded: {max}")]
142    MaxLinksExceeded { max: usize },
143
144    #[error("handshake incomplete for link {0}")]
145    HandshakeIncomplete(LinkId),
146
147    #[error("no session available for link {0}")]
148    NoSession(LinkId),
149
150    #[error("promotion failed for link {link_id}: {reason}")]
151    PromotionFailed { link_id: LinkId, reason: String },
152
153    #[error("send failed to {node_addr}: {reason}")]
154    SendFailed { node_addr: NodeAddr, reason: String },
155
156    #[error("mtu exceeded forwarding to {node_addr}: packet {packet_size} > mtu {mtu}")]
157    MtuExceeded {
158        node_addr: NodeAddr,
159        packet_size: usize,
160        mtu: u16,
161    },
162
163    #[error("config error: {0}")]
164    Config(#[from] ConfigError),
165
166    #[error("identity error: {0}")]
167    Identity(#[from] IdentityError),
168
169    #[error("TUN error: {0}")]
170    Tun(#[from] TunError),
171
172    #[error("index allocation failed: {0}")]
173    IndexAllocationFailed(String),
174
175    #[error("handshake failed: {0}")]
176    HandshakeFailed(String),
177
178    #[error("transport error: {0}")]
179    TransportError(String),
180
181    #[error("local route unavailable: {0}")]
182    LocalRouteUnavailable(String),
183
184    #[error("bootstrap handoff failed: {0}")]
185    BootstrapHandoff(String),
186}
187
188impl NodeError {
189    pub(in crate::node) fn from_transport_error(error: TransportError) -> Self {
190        if error.is_local_route_unavailable() {
191            Self::LocalRouteUnavailable(error.to_string())
192        } else {
193            Self::TransportError(error.to_string())
194        }
195    }
196
197    pub(in crate::node) fn is_local_route_unavailable(&self) -> bool {
198        matches!(self, Self::LocalRouteUnavailable(_))
199    }
200}
201
202/// Source-attributed packet delivered by a node running without a system TUN.
203#[derive(Debug, Clone, PartialEq, Eq)]
204pub struct NodeDeliveredPacket {
205    /// FIPS node address that originated the packet.
206    pub source_node_addr: NodeAddr,
207    /// Source Nostr public key when the node has learned it.
208    pub source_npub: Option<String>,
209    /// Destination FIPS address from the IPv6 packet.
210    pub destination: FipsAddress,
211    /// Full IPv6 packet after FIPS session decapsulation.
212    pub packet: Vec<u8>,
213}
214
215#[derive(Debug, Clone)]
216struct IdentityCacheEntry {
217    node_addr: NodeAddr,
218    pubkey: secp256k1::PublicKey,
219    npub: String,
220    last_seen_ms: u64,
221}
222
223impl IdentityCacheEntry {
224    fn new(
225        node_addr: NodeAddr,
226        pubkey: secp256k1::PublicKey,
227        npub: String,
228        last_seen_ms: u64,
229    ) -> Self {
230        Self {
231            node_addr,
232            pubkey,
233            npub,
234            last_seen_ms,
235        }
236    }
237}
238
239/// App-owned packet channels for embedding FIPS without a system TUN.
240#[derive(Debug)]
241pub struct ExternalPacketIo {
242    /// Send outbound IPv6 packets into the node.
243    pub outbound_tx: crate::upper::tun::TunOutboundTx,
244    /// Receive inbound IPv6 packets delivered by FIPS sessions.
245    pub inbound_rx: tokio::sync::mpsc::Receiver<NodeDeliveredPacket>,
246}
247
248/// App-owned endpoint data channels for embedding FIPS without a daemon.
249#[derive(Debug)]
250pub(crate) struct EndpointDataIo {
251    /// Send endpoint data commands into the node RX loop.
252    ///
253    /// Bounded with a generous default so normal sender bursts do not
254    /// stall on semaphore acquisition. macOS pacing happens at the UDP
255    /// egress thread where the real Wi-Fi/interface bottleneck is visible;
256    /// constraining this app queue instead caused the inner TCP flow to
257    /// collapse under iperf. `FIPS_ENDPOINT_DATA_QUEUE_CAP` overrides the
258    /// default for benches.
259    pub(crate) command_tx: tokio::sync::mpsc::Sender<NodeEndpointCommand>,
260    /// Receive endpoint data delivered by FIPS sessions.
261    ///
262    /// Unbounded so the rx_loop's send on inbound packet delivery is a
263    /// wait-free push (no semaphore acquire), and so we can drop the
264    /// per-packet cross-task relay that previously sat between the node
265    /// task and the `FipsEndpoint::recv()` consumer. Backpressure is
266    /// naturally bounded — the rx_loop both produces here and runs the
267    /// same runtime that schedules the consumer, so a stalled consumer
268    /// stalls production too.
269    pub(crate) event_rx: tokio::sync::mpsc::UnboundedReceiver<NodeEndpointEvent>,
270    /// Clone of the event_tx exposed for in-process loopback (e.g.
271    /// `FipsEndpoint::send` to self_npub). Lets the endpoint inject an
272    /// event into the same queue without going through the encrypt /
273    /// decrypt path, while keeping every consumer reading from a single
274    /// channel.
275    pub(crate) event_tx: tokio::sync::mpsc::UnboundedSender<NodeEndpointEvent>,
276}
277
278fn endpoint_data_command_capacity(requested: usize) -> usize {
279    if let Ok(raw) = std::env::var("FIPS_ENDPOINT_DATA_QUEUE_CAP")
280        && let Ok(value) = raw.trim().parse::<usize>()
281        && value > 0
282    {
283        return value;
284    }
285
286    requested.max(1).max(32_768)
287}
288
289/// Commands accepted by the node endpoint data service.
290#[derive(Debug)]
291pub(crate) enum NodeEndpointCommand {
292    /// Send with an explicit response channel — used by callers that
293    /// care whether the local-stack handoff succeeded (e.g.
294    /// `blocking_send` waits for the runtime to accept the send).
295    Send {
296        remote: PeerIdentity,
297        payload: Vec<u8>,
298        queued_at: Option<std::time::Instant>,
299        response_tx: tokio::sync::oneshot::Sender<Result<(), NodeError>>,
300    },
301    /// **Fire-and-forget** variant of `Send` — no oneshot allocation,
302    /// no per-packet result channel. Used by the data-plane fast path
303    /// (`FipsEndpoint::send`) where the caller already discards the
304    /// result. Saves one oneshot::channel() allocation per outbound
305    /// packet on the application's send hot path.
306    SendOneway {
307        remote: PeerIdentity,
308        payload: Vec<u8>,
309        queued_at: Option<std::time::Instant>,
310    },
311    PeerSnapshot {
312        response_tx: tokio::sync::oneshot::Sender<Vec<NodeEndpointPeer>>,
313    },
314    RelaySnapshot {
315        response_tx: tokio::sync::oneshot::Sender<Vec<NodeEndpointRelayStatus>>,
316    },
317    UpdateRelays {
318        advert_relays: Vec<String>,
319        dm_relays: Vec<String>,
320        response_tx: tokio::sync::oneshot::Sender<Result<(), NodeError>>,
321    },
322    /// Replace the runtime peer list. Newly added auto-connect peers get
323    /// `initiate_peer_connection` immediately; removed peers are dropped
324    /// from the retry queue (the regular liveness timeout reaps any active
325    /// session). Existing entries are kept and their `addresses` field is
326    /// refreshed so the next retry sees the latest hints.
327    UpdatePeers {
328        peers: Vec<crate::config::PeerConfig>,
329        response_tx: tokio::sync::oneshot::Sender<Result<UpdatePeersOutcome, NodeError>>,
330    },
331}
332
333/// Reports what changed in response to `UpdatePeers`.
334#[derive(Debug, Clone, Default, PartialEq, Eq)]
335pub(crate) struct UpdatePeersOutcome {
336    pub(crate) added: usize,
337    pub(crate) removed: usize,
338    pub(crate) updated: usize,
339    pub(crate) unchanged: usize,
340}
341
342/// Endpoint data events emitted by the node session receive path.
343#[derive(Debug)]
344pub(crate) enum NodeEndpointEvent {
345    Data {
346        source_node_addr: NodeAddr,
347        source_npub: Option<String>,
348        payload: Vec<u8>,
349        queued_at: Option<std::time::Instant>,
350    },
351}
352
353/// Authenticated peer state exposed to embedded endpoint callers.
354#[derive(Debug, Clone, PartialEq, Eq)]
355pub(crate) struct NodeEndpointPeer {
356    pub(crate) npub: String,
357    pub(crate) transport_addr: Option<String>,
358    pub(crate) transport_type: Option<String>,
359    pub(crate) link_id: u64,
360    pub(crate) srtt_ms: Option<u64>,
361    pub(crate) packets_sent: u64,
362    pub(crate) packets_recv: u64,
363    pub(crate) bytes_sent: u64,
364    pub(crate) bytes_recv: u64,
365}
366
367/// Live Nostr relay state exposed to embedded endpoint callers.
368#[derive(Debug, Clone, PartialEq, Eq)]
369pub(crate) struct NodeEndpointRelayStatus {
370    pub(crate) url: String,
371    pub(crate) status: String,
372}
373
374/// Node operational state.
375#[derive(Clone, Copy, Debug, PartialEq, Eq)]
376pub enum NodeState {
377    /// Created but not started.
378    Created,
379    /// Starting up (initializing transports).
380    Starting,
381    /// Fully operational.
382    Running,
383    /// Shutting down.
384    Stopping,
385    /// Stopped.
386    Stopped,
387}
388
389impl NodeState {
390    /// Check if node is operational.
391    pub fn is_operational(&self) -> bool {
392        matches!(self, NodeState::Running)
393    }
394
395    /// Check if node can be started.
396    pub fn can_start(&self) -> bool {
397        matches!(self, NodeState::Created | NodeState::Stopped)
398    }
399
400    /// Check if node can be stopped.
401    pub fn can_stop(&self) -> bool {
402        matches!(self, NodeState::Running)
403    }
404}
405
406impl fmt::Display for NodeState {
407    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
408        let s = match self {
409            NodeState::Created => "created",
410            NodeState::Starting => "starting",
411            NodeState::Running => "running",
412            NodeState::Stopping => "stopping",
413            NodeState::Stopped => "stopped",
414        };
415        write!(f, "{}", s)
416    }
417}
418
419/// Recent request tracking for dedup and reverse-path forwarding.
420///
421/// When a LookupRequest is forwarded through a node, the node stores the
422/// request_id and which peer sent it. When the corresponding LookupResponse
423/// arrives, it's forwarded back to that peer (reverse-path forwarding).
424/// The `response_forwarded` flag prevents response routing loops.
425#[derive(Clone, Debug)]
426pub(crate) struct RecentRequest {
427    /// The peer who sent this request to us.
428    pub(crate) from_peer: NodeAddr,
429    /// When we received this request (Unix milliseconds).
430    pub(crate) timestamp_ms: u64,
431    /// Whether we've already forwarded a response for this request.
432    /// Prevents response routing loops when convergent request paths
433    /// create bidirectional entries in recent_requests.
434    pub(crate) response_forwarded: bool,
435}
436
437impl RecentRequest {
438    pub(crate) fn new(from_peer: NodeAddr, timestamp_ms: u64) -> Self {
439        Self {
440            from_peer,
441            timestamp_ms,
442            response_forwarded: false,
443        }
444    }
445
446    /// Check if this entry has expired (older than expiry_ms).
447    pub(crate) fn is_expired(&self, current_time_ms: u64, expiry_ms: u64) -> bool {
448        current_time_ms.saturating_sub(self.timestamp_ms) > expiry_ms
449    }
450}
451
452/// Key for addr_to_link reverse lookup.
453type AddrKey = (TransportId, TransportAddr);
454
455/// Per-transport kernel drop tracking for congestion detection.
456///
457/// Sampled every tick (1s). The `dropping` flag indicates whether new
458/// kernel drops were observed since the previous sample.
459#[derive(Debug, Default)]
460struct TransportDropState {
461    /// Previous `recv_drops` sample (cumulative counter).
462    prev_drops: u64,
463    /// True if drops increased since the last sample.
464    dropping: bool,
465}
466
467/// State for a link waiting for transport-level connection establishment.
468///
469/// For connection-oriented transports (TCP, Tor), the transport connect runs
470/// asynchronously. This struct holds the data needed to complete the handshake
471/// once the connection is ready.
472struct PendingConnect {
473    /// The link that was created for this connection.
474    link_id: LinkId,
475    /// Which transport is being used.
476    transport_id: TransportId,
477    /// The remote address being connected to.
478    remote_addr: TransportAddr,
479    /// The peer identity (for handshake initiation).
480    peer_identity: PeerIdentity,
481}
482
483/// A running FIPS node instance.
484///
485/// This is the top-level container holding all node state.
486///
487/// ## Peer Lifecycle
488///
489/// Peers go through two phases:
490/// 1. **Connection phase** (`connections`): Handshake in progress, indexed by LinkId
491/// 2. **Active phase** (`peers`): Authenticated, indexed by NodeAddr
492///
493/// The `addr_to_link` map enables dispatching incoming packets to the right
494/// connection before authentication completes.
495// Discovery lookup constants moved to config: node.discovery.attempt_timeouts_secs, node.discovery.ttl
496pub struct Node {
497    // === Identity ===
498    /// This node's cryptographic identity.
499    identity: Identity,
500
501    /// Random epoch generated at startup for peer restart detection.
502    /// Exchanged inside Noise handshake messages so peers can detect restarts.
503    startup_epoch: [u8; 8],
504
505    /// Instant when the node was created, for uptime reporting.
506    started_at: std::time::Instant,
507
508    // === Configuration ===
509    /// Loaded configuration.
510    config: Config,
511
512    // === State ===
513    /// Node operational state.
514    state: NodeState,
515
516    /// Whether this is a leaf-only node.
517    is_leaf_only: bool,
518
519    // === Spanning Tree ===
520    /// Local spanning tree state.
521    tree_state: TreeState,
522
523    // === Bloom Filter ===
524    /// Local Bloom filter state.
525    bloom_state: BloomState,
526
527    // === Routing ===
528    /// Address -> coordinates cache (from session setup and discovery).
529    coord_cache: CoordCache,
530    /// Locally learned reverse-path next-hop hints.
531    learned_routes: LearnedRouteTable,
532    /// Recent discovery requests (dedup + reverse-path forwarding).
533    /// Maps request_id → RecentRequest.
534    recent_requests: HashMap<u64, RecentRequest>,
535    /// Per-destination path MTU lookup, keyed by FipsAddress (mirrors
536    /// `coord_cache.entries[*].path_mtu`). Sync read-only access from
537    /// the TUN reader/writer threads at TCP MSS clamp time so the
538    /// SYN/SYN-ACK clamp can use the smaller of the local-egress floor
539    /// and the learned per-destination path MTU.
540    path_mtu_lookup: Arc<std::sync::RwLock<HashMap<crate::FipsAddress, u16>>>,
541
542    // === Transports & Links ===
543    /// Active transports (owned by Node).
544    transports: HashMap<TransportId, TransportHandle>,
545    /// Per-transport kernel drop tracking for congestion detection.
546    transport_drops: HashMap<TransportId, TransportDropState>,
547    /// Active links.
548    links: HashMap<LinkId, Link>,
549    /// Reverse lookup: (transport_id, remote_addr) -> link_id.
550    addr_to_link: HashMap<AddrKey, LinkId>,
551
552    // === Packet Channel ===
553    /// Packet sender for transports.
554    packet_tx: Option<PacketTx>,
555    /// Packet receiver (for event loop).
556    packet_rx: Option<PacketRx>,
557
558    // === Connections (Handshake Phase) ===
559    /// Pending connections (handshake in progress).
560    /// Indexed by LinkId since we don't know the peer's identity yet.
561    connections: HashMap<LinkId, PeerConnection>,
562
563    // === Peers (Active Phase) ===
564    /// Authenticated peers.
565    /// Indexed by NodeAddr (verified identity).
566    peers: HashMap<NodeAddr, ActivePeer>,
567
568    // === End-to-End Sessions ===
569    /// Session table for end-to-end encrypted sessions.
570    /// Keyed by remote NodeAddr.
571    sessions: HashMap<NodeAddr, SessionEntry>,
572
573    // === Identity Cache ===
574    /// Maps FipsAddress prefix bytes (bytes 1-15) to cached peer identity data.
575    /// Enables reverse lookup from IPv6 destination to session/routing identity.
576    identity_cache: HashMap<[u8; 15], IdentityCacheEntry>,
577
578    // === Pending TUN Packets ===
579    /// Packets queued while waiting for session establishment.
580    /// Keyed by destination NodeAddr, bounded per-dest and total.
581    pending_tun_packets: HashMap<NodeAddr, VecDeque<Vec<u8>>>,
582    /// Endpoint data payloads queued while waiting for session establishment.
583    pending_endpoint_data: HashMap<NodeAddr, VecDeque<Vec<u8>>>,
584    // === Pending Discovery Lookups ===
585    /// Tracks in-flight discovery lookups. Maps target NodeAddr to the
586    /// initiation timestamp (Unix ms). Prevents duplicate flood queries.
587    pending_lookups: HashMap<NodeAddr, handlers::discovery::PendingLookup>,
588
589    // === Resource Limits ===
590    /// Maximum connections (0 = unlimited).
591    max_connections: usize,
592    /// Maximum peers (0 = unlimited).
593    max_peers: usize,
594    /// Maximum links (0 = unlimited).
595    max_links: usize,
596
597    // === Counters ===
598    /// Next link ID to allocate.
599    next_link_id: u64,
600    /// Next transport ID to allocate.
601    next_transport_id: u32,
602
603    // === Node Statistics ===
604    /// Routing, forwarding, discovery, and error signal counters.
605    stats: stats::NodeStats,
606
607    /// Time-series history of node-level metrics (1s/1m rings).
608    stats_history: stats_history::StatsHistory,
609
610    // === TUN Interface ===
611    /// TUN device state.
612    tun_state: TunState,
613    /// TUN interface name (for cleanup).
614    tun_name: Option<String>,
615    /// TUN packet sender channel.
616    tun_tx: Option<TunTx>,
617    /// Receiver for outbound packets from the TUN reader.
618    tun_outbound_rx: Option<TunOutboundRx>,
619    /// App-owned packet sink used by embedded/no-TUN integrations.
620    external_packet_tx: Option<tokio::sync::mpsc::Sender<NodeDeliveredPacket>>,
621    /// Endpoint data command receiver used by embedded/no-daemon integrations.
622    endpoint_command_rx: Option<tokio::sync::mpsc::Receiver<NodeEndpointCommand>>,
623    /// Endpoint data event sink used by embedded/no-daemon integrations.
624    endpoint_event_tx: Option<tokio::sync::mpsc::UnboundedSender<NodeEndpointEvent>>,
625    /// Off-task FMP-encrypt + UDP-send worker pool. `None` if not yet
626    /// spawned (set up in `start()` once transports are running).
627    /// `Some(pool)` once available; the pool internally holds
628    /// per-worker mpsc senders and round-robins jobs across them.
629    /// See `node::encrypt_worker` for the rationale and layout.
630    encrypt_workers: Option<encrypt_worker::EncryptWorkerPool>,
631    /// Off-task FMP + FSP decrypt + delivery worker pool. Mirror of
632    /// `encrypt_workers` for the receive side.
633    decrypt_workers: Option<decrypt_worker::DecryptWorkerPool>,
634    /// Set of sessions that have been registered with the decrypt
635    /// shard worker pool. Used by rx_loop to decide between fast-path
636    /// dispatch (worker owns the session) and legacy in-place decrypt
637    /// (worker doesn't have it yet). Per the data-plane restructure,
638    /// the worker owns its session state directly — there's no shared
639    /// `Arc<RwLock<HashMap>>` of cipher / replay state anymore, only
640    /// this set tracks **whether** the worker has been told about a
641    /// given session.
642    decrypt_registered_sessions: std::collections::HashSet<(TransportId, u32)>,
643    /// Fallback channel: decrypt worker bounces non-fast-path packets
644    /// (anything that's not bulk EndpointData) back here for rx_loop
645    /// to handle via the legacy path. Drained by a new rx_loop arm.
646    decrypt_fallback_rx:
647        Option<tokio::sync::mpsc::UnboundedReceiver<decrypt_worker::DecryptWorkerEvent>>,
648    decrypt_fallback_tx: tokio::sync::mpsc::UnboundedSender<decrypt_worker::DecryptWorkerEvent>,
649    /// TUN reader thread handle.
650    tun_reader_handle: Option<JoinHandle<()>>,
651    /// TUN writer thread handle.
652    tun_writer_handle: Option<JoinHandle<()>>,
653    /// Shutdown pipe: writing to this fd unblocks the TUN reader thread on macOS.
654    /// On Linux, deleting the interface via netlink serves the same purpose.
655    #[cfg(target_os = "macos")]
656    tun_shutdown_fd: Option<std::os::unix::io::RawFd>,
657
658    // === DNS Responder ===
659    /// Receiver for resolved identities from the DNS responder.
660    dns_identity_rx: Option<crate::upper::dns::DnsIdentityRx>,
661    /// DNS responder task handle.
662    dns_task: Option<tokio::task::JoinHandle<()>>,
663
664    // === Index-Based Session Dispatch ===
665    /// Allocator for session indices.
666    index_allocator: IndexAllocator,
667    /// O(1) lookup: (transport_id, our_index) → NodeAddr.
668    /// This maps our session index to the peer that uses it.
669    peers_by_index: HashMap<(TransportId, u32), NodeAddr>,
670    /// Pending outbound handshakes by our sender_idx.
671    /// Tracks which LinkId corresponds to which session index.
672    pending_outbound: HashMap<(TransportId, u32), LinkId>,
673
674    // === Rate Limiting ===
675    /// Rate limiter for msg1 processing (DoS protection).
676    msg1_rate_limiter: HandshakeRateLimiter,
677    /// Rate limiter for ICMP Packet Too Big messages.
678    icmp_rate_limiter: IcmpRateLimiter,
679    /// Rate limiter for routing error signals (CoordsRequired / PathBroken).
680    routing_error_rate_limiter: RoutingErrorRateLimiter,
681    /// Rate limiter for source-side CoordsRequired/PathBroken responses.
682    coords_response_rate_limiter: RoutingErrorRateLimiter,
683    /// Backoff for failed discovery lookups (originator-side).
684    discovery_backoff: DiscoveryBackoff,
685    /// Rate limiter for forwarded discovery requests (transit-side).
686    discovery_forward_limiter: DiscoveryForwardRateLimiter,
687
688    // === Pending Transport Connects ===
689    /// Links waiting for transport-level connection establishment before
690    /// sending handshake msg1. For connection-oriented transports (TCP, Tor),
691    /// the transport connect runs in the background; the tick handler polls
692    /// connection_state() and initiates the handshake when connected.
693    pending_connects: Vec<PendingConnect>,
694
695    // === Connection Retry ===
696    /// Retry state for peers whose outbound connections have failed.
697    /// Keyed by NodeAddr. Entries are created when a handshake times out
698    /// or fails, and removed on successful promotion or when max retries
699    /// are exhausted.
700    retry_pending: HashMap<NodeAddr, retry::RetryState>,
701
702    /// Optional Nostr/STUN overlay discovery coordinator for `udp:nat` peers.
703    nostr_discovery: Option<Arc<crate::discovery::nostr::NostrDiscovery>>,
704    /// mDNS / DNS-SD responder + browser for local-link peer discovery.
705    /// Identity is unverified at this layer — the Noise XX handshake
706    /// initiated against an mDNS-observed endpoint is what proves the
707    /// peer holds the matching private key.
708    lan_discovery: Option<Arc<crate::discovery::lan::LanDiscovery>>,
709    /// Same-host JSON registry under `~/.fips/instances`. Records are
710    /// loopback routing hints only; peer identity is still verified by the
711    /// Noise handshake.
712    local_instance_registry: Option<crate::discovery::local::LocalInstanceRegistry>,
713    local_instance_started_at_ms: Option<u64>,
714    last_local_instance_publish_ms: Option<u64>,
715    last_local_instance_scan_ms: Option<u64>,
716    /// Wall-clock ms when Nostr discovery successfully started, used to
717    /// schedule the one-shot startup advert sweep after a settle delay.
718    /// `None` until discovery comes up; remains `None` if discovery is
719    /// disabled or failed to start.
720    nostr_discovery_started_at_ms: Option<u64>,
721    /// Whether the one-shot startup advert sweep has run. Set to true
722    /// after the first sweep fires (under `policy: open`); thereafter
723    /// only the per-tick `queue_open_discovery_retries` continues.
724    startup_open_discovery_sweep_done: bool,
725    /// Per-peer UDP transports adopted from NAT traversal handoff.
726    bootstrap_transports: HashSet<TransportId>,
727    /// Originating peer npub (bech32) for each adopted bootstrap
728    /// transport, captured at `adopt_established_traversal` time.
729    /// Populated alongside `bootstrap_transports`; cleared in
730    /// `cleanup_bootstrap_transport_if_unused`. Used by the rx loop to
731    /// route fatal-protocol-mismatch observations back to the
732    /// Nostr-discovery `failure_state` for long cooldown application.
733    bootstrap_transport_npubs: HashMap<TransportId, String>,
734    /// Peers that should not be used as reply-learned fallback transit for
735    /// other destinations. Direct lookups to the peer are still permitted.
736    discovery_fallback_transit_blocked_peers: HashSet<NodeAddr>,
737
738    // === Periodic Parent Re-evaluation ===
739    /// Timestamp of last periodic parent re-evaluation (for pacing).
740    last_parent_reeval: Option<crate::time::Instant>,
741
742    // === Congestion Logging ===
743    /// Timestamp of last congestion detection log (rate-limited to 5s).
744    last_congestion_log: Option<std::time::Instant>,
745
746    // === Mesh Size Estimate ===
747    /// Cached estimated mesh size (computed once per tick from bloom filters).
748    estimated_mesh_size: Option<u64>,
749    /// Timestamp of last mesh size log emission.
750    last_mesh_size_log: Option<std::time::Instant>,
751
752    // === Bloom Self-Plausibility ===
753    /// Rate-limit state for the self-plausibility WARN. Fires at most
754    /// once per 60s globally when our own outgoing FilterAnnounce has
755    /// an FPR above `node.bloom.max_inbound_fpr`, signalling either
756    /// aggregation drift or an ingress bypass.
757    last_self_warn: Option<std::time::Instant>,
758
759    // === Local Outbound Liveness ===
760    /// Set when a `transport.send` returned a local-side io error
761    /// (`NetworkUnreachable` / `HostUnreachable` / `AddrNotAvailable`),
762    /// cleared on the next successful send. Used by
763    /// `check_link_heartbeats` to compress the dead-timeout to
764    /// `fast_link_dead_timeout_secs` while our outbound is observed
765    /// broken — direct kernel evidence beats waiting on receive-silence.
766    last_local_send_failure_at: Option<std::time::Instant>,
767    /// Set when the rx loop could not complete its 1s maintenance work
768    /// inside the watchdog timeout. Link-dead detection may be valid during
769    /// overload, but traversal cooldown should not punish a path just because
770    /// our own scheduler/worker queue was late.
771    last_rx_loop_maintenance_timeout_at: Option<std::time::Instant>,
772
773    // === Display Names ===
774    /// Human-readable names for configured peers (alias or short npub).
775    /// Populated at startup from peer config.
776    peer_aliases: HashMap<NodeAddr, String>,
777    /// Scheduler weight for explicitly configured peers. Built when config
778    /// changes so the packet hot path only does a NodeAddr hash lookup.
779    configured_peer_send_weights: HashMap<NodeAddr, u8>,
780
781    /// Reloadable peer ACL state from standard allow/deny files.
782    peer_acl: acl::PeerAclReloader,
783
784    // === Host Map ===
785    /// Static hostname → npub mapping for DNS resolution.
786    /// Built at construction from peer aliases and /etc/fips/hosts.
787    host_map: Arc<HostMap>,
788}
789
790impl Node {
791    /// Create a new node from configuration.
792    pub fn new(config: Config) -> Result<Self, NodeError> {
793        config.validate()?;
794        let identity = config.create_identity()?;
795        let node_addr = *identity.node_addr();
796        let is_leaf_only = config.is_leaf_only();
797
798        let (decrypt_fallback_tx, decrypt_fallback_rx) = tokio::sync::mpsc::unbounded_channel();
799        let decrypt_fallback_rx = Some(decrypt_fallback_rx);
800
801        let mut startup_epoch = [0u8; 8];
802        rand::rng().fill_bytes(&mut startup_epoch);
803
804        let mut bloom_state = if is_leaf_only {
805            BloomState::leaf_only(node_addr)
806        } else {
807            BloomState::new(node_addr)
808        };
809        bloom_state.set_update_debounce_ms(config.node.bloom.update_debounce_ms);
810
811        let tun_state = if config.tun.enabled {
812            TunState::Configured
813        } else {
814            TunState::Disabled
815        };
816
817        // Initialize tree state with signed self-declaration
818        let mut tree_state = TreeState::new(node_addr);
819        tree_state.set_parent_hysteresis(config.node.tree.parent_hysteresis);
820        tree_state.set_hold_down(config.node.tree.hold_down_secs);
821        tree_state.set_flap_dampening(
822            config.node.tree.flap_threshold,
823            config.node.tree.flap_window_secs,
824            config.node.tree.flap_dampening_secs,
825        );
826        tree_state
827            .sign_declaration(&identity)
828            .expect("signing own declaration should never fail");
829
830        let coord_cache = CoordCache::new(
831            config.node.cache.coord_size,
832            config.node.cache.coord_ttl_secs * 1000,
833        );
834        let rl = &config.node.rate_limit;
835        let msg1_rate_limiter = HandshakeRateLimiter::with_params(
836            rate_limit::TokenBucket::with_params(rl.handshake_burst, rl.handshake_rate),
837            config.node.limits.max_pending_inbound,
838        );
839
840        let max_connections = config.node.limits.max_connections;
841        let max_peers = config.node.limits.max_peers;
842        let max_links = config.node.limits.max_links;
843        let coords_response_interval_ms = config.node.session.coords_response_interval_ms;
844        let backoff_base_secs = config.node.discovery.backoff_base_secs;
845        let backoff_max_secs = config.node.discovery.backoff_max_secs;
846        let forward_min_interval_secs = config.node.discovery.forward_min_interval_secs;
847
848        let (host_map, peer_acl) = Self::host_map_and_peer_acl(&config);
849        let configured_peer_send_weights = Self::configured_peer_send_weights(&config);
850
851        Ok(Self {
852            identity,
853            startup_epoch,
854            started_at: std::time::Instant::now(),
855            config,
856            state: NodeState::Created,
857            is_leaf_only,
858            tree_state,
859            bloom_state,
860            coord_cache,
861            learned_routes: LearnedRouteTable::default(),
862            recent_requests: HashMap::new(),
863            transports: HashMap::new(),
864            transport_drops: HashMap::new(),
865            links: HashMap::new(),
866            addr_to_link: HashMap::new(),
867            packet_tx: None,
868            packet_rx: None,
869            connections: HashMap::new(),
870            peers: HashMap::new(),
871            sessions: HashMap::new(),
872            identity_cache: HashMap::new(),
873            pending_tun_packets: HashMap::new(),
874            pending_endpoint_data: HashMap::new(),
875            pending_lookups: HashMap::new(),
876            max_connections,
877            max_peers,
878            max_links,
879            next_link_id: 1,
880            next_transport_id: 1,
881            stats: stats::NodeStats::new(),
882            stats_history: stats_history::StatsHistory::new(),
883            tun_state,
884            tun_name: None,
885            tun_tx: None,
886            tun_outbound_rx: None,
887            external_packet_tx: None,
888            endpoint_command_rx: None,
889            endpoint_event_tx: None,
890            encrypt_workers: None,
891            decrypt_workers: None,
892            decrypt_registered_sessions: std::collections::HashSet::new(),
893            decrypt_fallback_tx,
894            decrypt_fallback_rx,
895            tun_reader_handle: None,
896            tun_writer_handle: None,
897            #[cfg(target_os = "macos")]
898            tun_shutdown_fd: None,
899            dns_identity_rx: None,
900            dns_task: None,
901            index_allocator: IndexAllocator::new(),
902            peers_by_index: HashMap::new(),
903            pending_outbound: HashMap::new(),
904            msg1_rate_limiter,
905            icmp_rate_limiter: IcmpRateLimiter::new(),
906            routing_error_rate_limiter: RoutingErrorRateLimiter::new(),
907            coords_response_rate_limiter: RoutingErrorRateLimiter::with_interval(
908                std::time::Duration::from_millis(coords_response_interval_ms),
909            ),
910            discovery_backoff: DiscoveryBackoff::with_params(backoff_base_secs, backoff_max_secs),
911            discovery_forward_limiter: DiscoveryForwardRateLimiter::with_interval(
912                std::time::Duration::from_secs(forward_min_interval_secs),
913            ),
914            pending_connects: Vec::new(),
915            retry_pending: HashMap::new(),
916            nostr_discovery: None,
917            nostr_discovery_started_at_ms: None,
918            lan_discovery: None,
919            local_instance_registry: None,
920            local_instance_started_at_ms: None,
921            last_local_instance_publish_ms: None,
922            last_local_instance_scan_ms: None,
923            startup_open_discovery_sweep_done: false,
924            bootstrap_transports: HashSet::new(),
925            bootstrap_transport_npubs: HashMap::new(),
926            discovery_fallback_transit_blocked_peers: HashSet::new(),
927            last_parent_reeval: None,
928            last_congestion_log: None,
929            estimated_mesh_size: None,
930            last_mesh_size_log: None,
931            last_self_warn: None,
932            last_local_send_failure_at: None,
933            last_rx_loop_maintenance_timeout_at: None,
934            peer_aliases: HashMap::new(),
935            configured_peer_send_weights,
936            peer_acl,
937            host_map,
938            path_mtu_lookup: Arc::new(std::sync::RwLock::new(HashMap::new())),
939        })
940    }
941
942    /// Create a node with a specific identity.
943    ///
944    /// This constructor validates cross-field config invariants before
945    /// constructing the node, same as [`Node::new`].
946    pub fn with_identity(identity: Identity, config: Config) -> Result<Self, NodeError> {
947        config.validate()?;
948        let node_addr = *identity.node_addr();
949
950        let (decrypt_fallback_tx, decrypt_fallback_rx) = tokio::sync::mpsc::unbounded_channel();
951        let decrypt_fallback_rx = Some(decrypt_fallback_rx);
952
953        let mut startup_epoch = [0u8; 8];
954        rand::rng().fill_bytes(&mut startup_epoch);
955
956        let tun_state = if config.tun.enabled {
957            TunState::Configured
958        } else {
959            TunState::Disabled
960        };
961
962        // Initialize tree state with signed self-declaration
963        let mut tree_state = TreeState::new(node_addr);
964        tree_state.set_parent_hysteresis(config.node.tree.parent_hysteresis);
965        tree_state.set_hold_down(config.node.tree.hold_down_secs);
966        tree_state.set_flap_dampening(
967            config.node.tree.flap_threshold,
968            config.node.tree.flap_window_secs,
969            config.node.tree.flap_dampening_secs,
970        );
971        tree_state
972            .sign_declaration(&identity)
973            .expect("signing own declaration should never fail");
974
975        let mut bloom_state = BloomState::new(node_addr);
976        bloom_state.set_update_debounce_ms(config.node.bloom.update_debounce_ms);
977
978        let coord_cache = CoordCache::new(
979            config.node.cache.coord_size,
980            config.node.cache.coord_ttl_secs * 1000,
981        );
982        let rl = &config.node.rate_limit;
983        let msg1_rate_limiter = HandshakeRateLimiter::with_params(
984            rate_limit::TokenBucket::with_params(rl.handshake_burst, rl.handshake_rate),
985            config.node.limits.max_pending_inbound,
986        );
987
988        let max_connections = config.node.limits.max_connections;
989        let max_peers = config.node.limits.max_peers;
990        let max_links = config.node.limits.max_links;
991        let coords_response_interval_ms = config.node.session.coords_response_interval_ms;
992
993        let (host_map, peer_acl) = Self::host_map_and_peer_acl(&config);
994        let configured_peer_send_weights = Self::configured_peer_send_weights(&config);
995
996        Ok(Self {
997            identity,
998            startup_epoch,
999            started_at: std::time::Instant::now(),
1000            config,
1001            state: NodeState::Created,
1002            is_leaf_only: false,
1003            tree_state,
1004            bloom_state,
1005            coord_cache,
1006            learned_routes: LearnedRouteTable::default(),
1007            recent_requests: HashMap::new(),
1008            transports: HashMap::new(),
1009            transport_drops: HashMap::new(),
1010            links: HashMap::new(),
1011            addr_to_link: HashMap::new(),
1012            packet_tx: None,
1013            packet_rx: None,
1014            connections: HashMap::new(),
1015            peers: HashMap::new(),
1016            sessions: HashMap::new(),
1017            identity_cache: HashMap::new(),
1018            pending_tun_packets: HashMap::new(),
1019            pending_endpoint_data: HashMap::new(),
1020            pending_lookups: HashMap::new(),
1021            max_connections,
1022            max_peers,
1023            max_links,
1024            next_link_id: 1,
1025            next_transport_id: 1,
1026            stats: stats::NodeStats::new(),
1027            stats_history: stats_history::StatsHistory::new(),
1028            tun_state,
1029            tun_name: None,
1030            tun_tx: None,
1031            tun_outbound_rx: None,
1032            external_packet_tx: None,
1033            endpoint_command_rx: None,
1034            endpoint_event_tx: None,
1035            encrypt_workers: None,
1036            decrypt_workers: None,
1037            decrypt_registered_sessions: std::collections::HashSet::new(),
1038            decrypt_fallback_tx,
1039            decrypt_fallback_rx,
1040            tun_reader_handle: None,
1041            tun_writer_handle: None,
1042            #[cfg(target_os = "macos")]
1043            tun_shutdown_fd: None,
1044            dns_identity_rx: None,
1045            dns_task: None,
1046            index_allocator: IndexAllocator::new(),
1047            peers_by_index: HashMap::new(),
1048            pending_outbound: HashMap::new(),
1049            msg1_rate_limiter,
1050            icmp_rate_limiter: IcmpRateLimiter::new(),
1051            routing_error_rate_limiter: RoutingErrorRateLimiter::new(),
1052            coords_response_rate_limiter: RoutingErrorRateLimiter::with_interval(
1053                std::time::Duration::from_millis(coords_response_interval_ms),
1054            ),
1055            discovery_backoff: DiscoveryBackoff::new(),
1056            discovery_forward_limiter: DiscoveryForwardRateLimiter::new(),
1057            pending_connects: Vec::new(),
1058            retry_pending: HashMap::new(),
1059            nostr_discovery: None,
1060            nostr_discovery_started_at_ms: None,
1061            lan_discovery: None,
1062            local_instance_registry: None,
1063            local_instance_started_at_ms: None,
1064            last_local_instance_publish_ms: None,
1065            last_local_instance_scan_ms: None,
1066            startup_open_discovery_sweep_done: false,
1067            bootstrap_transports: HashSet::new(),
1068            bootstrap_transport_npubs: HashMap::new(),
1069            discovery_fallback_transit_blocked_peers: HashSet::new(),
1070            last_parent_reeval: None,
1071            last_congestion_log: None,
1072            estimated_mesh_size: None,
1073            last_mesh_size_log: None,
1074            last_self_warn: None,
1075            last_local_send_failure_at: None,
1076            last_rx_loop_maintenance_timeout_at: None,
1077            peer_aliases: HashMap::new(),
1078            configured_peer_send_weights,
1079            peer_acl,
1080            host_map,
1081            path_mtu_lookup: Arc::new(std::sync::RwLock::new(HashMap::new())),
1082        })
1083    }
1084
1085    /// Create a leaf-only node (simplified state).
1086    pub fn leaf_only(config: Config) -> Result<Self, NodeError> {
1087        let mut node = Self::new(config)?;
1088        node.is_leaf_only = true;
1089        node.bloom_state = BloomState::leaf_only(*node.identity.node_addr());
1090        Ok(node)
1091    }
1092
1093    fn host_map_and_peer_acl(config: &Config) -> (Arc<HostMap>, acl::PeerAclReloader) {
1094        let base_host_map = HostMap::from_peer_configs(config.peers());
1095        if !config.node.system_files_enabled {
1096            return (
1097                Arc::new(base_host_map.clone()),
1098                acl::PeerAclReloader::memory_only(base_host_map),
1099            );
1100        }
1101
1102        let mut host_map = base_host_map.clone();
1103        let hosts_path = std::path::PathBuf::from(crate::upper::hosts::DEFAULT_HOSTS_PATH);
1104        let hosts_file = HostMap::load_hosts_file(std::path::Path::new(
1105            crate::upper::hosts::DEFAULT_HOSTS_PATH,
1106        ));
1107        host_map.merge(hosts_file);
1108        let peer_acl = acl::PeerAclReloader::with_alias_sources(
1109            std::path::PathBuf::from(acl::DEFAULT_PEERS_ALLOW_PATH),
1110            std::path::PathBuf::from(acl::DEFAULT_PEERS_DENY_PATH),
1111            base_host_map,
1112            hosts_path,
1113        );
1114        (Arc::new(host_map), peer_acl)
1115    }
1116
1117    fn configured_peer_send_weights(config: &Config) -> HashMap<NodeAddr, u8> {
1118        config
1119            .peers()
1120            .iter()
1121            .filter_map(|peer| {
1122                PeerIdentity::from_npub(&peer.npub).ok().map(|identity| {
1123                    (
1124                        *identity.node_addr(),
1125                        encrypt_worker::EXPLICIT_PEER_SEND_WEIGHT,
1126                    )
1127                })
1128            })
1129            .collect()
1130    }
1131
1132    fn send_weight_for_peer(&self, peer_addr: &NodeAddr) -> u8 {
1133        self.configured_peer_send_weights
1134            .get(peer_addr)
1135            .copied()
1136            .unwrap_or(encrypt_worker::DEFAULT_SEND_WEIGHT)
1137    }
1138
1139    /// Create transport instances from configuration.
1140    ///
1141    /// Returns a vector of TransportHandles for all configured transports.
1142    async fn create_transports(&mut self, packet_tx: &PacketTx) -> Vec<TransportHandle> {
1143        let mut transports = Vec::new();
1144
1145        // Collect UDP configs with optional names to avoid borrow conflicts
1146        let udp_instances: Vec<_> = self
1147            .config
1148            .transports
1149            .udp
1150            .iter()
1151            .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1152            .collect();
1153
1154        // Create UDP transport instances
1155        for (name, udp_config) in udp_instances {
1156            let transport_id = self.allocate_transport_id();
1157            let udp = UdpTransport::new(transport_id, name, udp_config, packet_tx.clone());
1158            transports.push(TransportHandle::Udp(udp));
1159        }
1160
1161        #[cfg(feature = "sim-transport")]
1162        {
1163            let sim_instances: Vec<_> = self
1164                .config
1165                .transports
1166                .sim
1167                .iter()
1168                .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1169                .collect();
1170
1171            for (name, sim_config) in sim_instances {
1172                let transport_id = self.allocate_transport_id();
1173                let sim = crate::transport::sim::SimTransport::new(
1174                    transport_id,
1175                    name,
1176                    sim_config,
1177                    packet_tx.clone(),
1178                );
1179                transports.push(TransportHandle::Sim(sim));
1180            }
1181        }
1182
1183        // Create Ethernet transport instances where raw-socket support exists.
1184        #[cfg(any(target_os = "linux", target_os = "macos"))]
1185        {
1186            let eth_instances: Vec<_> = self
1187                .config
1188                .transports
1189                .ethernet
1190                .iter()
1191                .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1192                .collect();
1193            let xonly = self.identity.pubkey();
1194            for (name, eth_config) in eth_instances {
1195                let mut eth_config = eth_config;
1196                if eth_config.discovery_scope.is_none() {
1197                    eth_config.discovery_scope = self.lan_discovery_scope();
1198                }
1199                let transport_id = self.allocate_transport_id();
1200                let mut eth =
1201                    EthernetTransport::new(transport_id, name, eth_config, packet_tx.clone());
1202                eth.set_local_pubkey(xonly);
1203                transports.push(TransportHandle::Ethernet(eth));
1204            }
1205        }
1206
1207        // Create TCP transport instances
1208        let tcp_instances: Vec<_> = self
1209            .config
1210            .transports
1211            .tcp
1212            .iter()
1213            .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1214            .collect();
1215
1216        for (name, tcp_config) in tcp_instances {
1217            let transport_id = self.allocate_transport_id();
1218            let tcp = TcpTransport::new(transport_id, name, tcp_config, packet_tx.clone());
1219            transports.push(TransportHandle::Tcp(tcp));
1220        }
1221
1222        // Create Tor transport instances
1223        let tor_instances: Vec<_> = self
1224            .config
1225            .transports
1226            .tor
1227            .iter()
1228            .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1229            .collect();
1230
1231        for (name, tor_config) in tor_instances {
1232            let transport_id = self.allocate_transport_id();
1233            let tor = TorTransport::new(transport_id, name, tor_config, packet_tx.clone());
1234            transports.push(TransportHandle::Tor(tor));
1235        }
1236
1237        let webrtc_instances: Vec<_> = self
1238            .config
1239            .transports
1240            .webrtc
1241            .iter()
1242            .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1243            .collect();
1244
1245        #[cfg(feature = "webrtc-transport")]
1246        {
1247            for (name, webrtc_config) in webrtc_instances {
1248                let transport_id = self.allocate_transport_id();
1249                match WebRtcTransport::new(
1250                    transport_id,
1251                    name,
1252                    webrtc_config,
1253                    packet_tx.clone(),
1254                    &self.identity,
1255                    &self.config.node.discovery.nostr,
1256                ) {
1257                    Ok(webrtc) => transports.push(TransportHandle::WebRtc(Box::new(webrtc))),
1258                    Err(err) => {
1259                        warn!(
1260                            transport_id = %transport_id,
1261                            error = %err,
1262                            "failed to initialize WebRTC transport"
1263                        );
1264                    }
1265                }
1266            }
1267        }
1268        #[cfg(not(feature = "webrtc-transport"))]
1269        if !webrtc_instances.is_empty() {
1270            warn!("WebRTC transport configured but this build lacks WebRTC transport support");
1271        }
1272
1273        // Create BLE transport instances
1274        #[cfg(bluer_available)]
1275        {
1276            let ble_instances: Vec<_> = self
1277                .config
1278                .transports
1279                .ble
1280                .iter()
1281                .map(|(name, config)| (name.map(|s| s.to_string()), config.clone()))
1282                .collect();
1283
1284            #[cfg(all(bluer_available, not(test)))]
1285            for (name, ble_config) in ble_instances {
1286                let transport_id = self.allocate_transport_id();
1287                let adapter = ble_config.adapter().to_string();
1288                let mtu = ble_config.mtu();
1289                match crate::transport::ble::io::BluerIo::new(&adapter, mtu).await {
1290                    Ok(io) => {
1291                        let mut ble = crate::transport::ble::BleTransport::new(
1292                            transport_id,
1293                            name,
1294                            ble_config,
1295                            io,
1296                            packet_tx.clone(),
1297                        );
1298                        ble.set_local_pubkey(self.identity.pubkey().serialize());
1299                        transports.push(TransportHandle::Ble(ble));
1300                    }
1301                    Err(e) => {
1302                        tracing::warn!(adapter = %adapter, error = %e, "failed to initialize BLE adapter");
1303                    }
1304                }
1305            }
1306
1307            #[cfg(any(not(bluer_available), test))]
1308            if !ble_instances.is_empty() {
1309                #[cfg(not(test))]
1310                tracing::warn!("BLE transport configured but this build lacks BlueZ support");
1311            }
1312        }
1313
1314        transports
1315    }
1316
1317    /// Find an operational transport that matches the given transport type name.
1318    ///
1319    /// Adopted UDP bootstrap transports are point-to-point sockets handed off
1320    /// from Nostr/STUN traversal. They must not be reused for ordinary
1321    /// `udp host:port` dials discovered through static config, mDNS, or overlay
1322    /// adverts: on macOS a `send_to` through the wrong adopted socket can fail
1323    /// with `EINVAL`, and even on platforms that allow it the packet would use
1324    /// the wrong 5-tuple/NAT mapping. Prefer configured transports and make the
1325    /// choice deterministic by lowest transport id instead of HashMap order.
1326    fn find_transport_for_type(&self, transport_type: &str) -> Option<TransportId> {
1327        self.transports
1328            .iter()
1329            .filter(|(id, handle)| {
1330                handle.transport_type().name == transport_type
1331                    && handle.is_operational()
1332                    && !self.bootstrap_transports.contains(id)
1333            })
1334            .min_by_key(|(id, _)| id.as_u32())
1335            .map(|(id, _)| *id)
1336    }
1337
1338    /// Resolve an Ethernet peer address ("interface/mac") to a transport ID
1339    /// and binary TransportAddr.
1340    ///
1341    /// Finds the Ethernet transport instance bound to the named interface
1342    /// and parses the MAC portion into a 6-byte TransportAddr.
1343    #[allow(unused_variables)]
1344    fn resolve_ethernet_addr(
1345        &self,
1346        addr_str: &str,
1347    ) -> Result<(TransportId, TransportAddr), NodeError> {
1348        #[cfg(any(target_os = "linux", target_os = "macos"))]
1349        {
1350            let (iface, mac_str) = addr_str.split_once('/').ok_or_else(|| {
1351                NodeError::NoTransportForType(format!(
1352                    "invalid Ethernet address format '{}': expected 'interface/mac'",
1353                    addr_str
1354                ))
1355            })?;
1356
1357            // Find the Ethernet transport bound to this interface
1358            let transport_id = self
1359                .transports
1360                .iter()
1361                .find(|(_, handle)| {
1362                    handle.transport_type().name == "ethernet"
1363                        && handle.is_operational()
1364                        && handle.interface_name() == Some(iface)
1365                })
1366                .map(|(id, _)| *id)
1367                .ok_or_else(|| {
1368                    NodeError::NoTransportForType(format!(
1369                        "no operational Ethernet transport for interface '{}'",
1370                        iface
1371                    ))
1372                })?;
1373
1374            let mac = crate::transport::ethernet::parse_mac_string(mac_str).map_err(|e| {
1375                NodeError::NoTransportForType(format!("invalid MAC in '{}': {}", addr_str, e))
1376            })?;
1377
1378            Ok((transport_id, TransportAddr::from_bytes(&mac)))
1379        }
1380        #[cfg(not(any(target_os = "linux", target_os = "macos")))]
1381        {
1382            Err(NodeError::NoTransportForType(
1383                "Ethernet transport is not supported on this platform".to_string(),
1384            ))
1385        }
1386    }
1387
1388    /// Resolve a BLE address string (`"adapter/AA:BB:CC:DD:EE:FF"`) to a
1389    /// (TransportId, TransportAddr) pair by finding the BLE transport
1390    /// instance matching the adapter name.
1391    #[cfg(bluer_available)]
1392    fn resolve_ble_addr(&self, addr_str: &str) -> Result<(TransportId, TransportAddr), NodeError> {
1393        let ta = TransportAddr::from_string(addr_str);
1394        let adapter = crate::transport::ble::addr::adapter_from_addr(&ta).ok_or_else(|| {
1395            NodeError::NoTransportForType(format!(
1396                "invalid BLE address format '{}': expected 'adapter/mac'",
1397                addr_str
1398            ))
1399        })?;
1400
1401        // Find the BLE transport for this adapter
1402        let transport_id = self
1403            .transports
1404            .iter()
1405            .find(|(_, handle)| handle.transport_type().name == "ble" && handle.is_operational())
1406            .map(|(id, _)| *id)
1407            .ok_or_else(|| {
1408                NodeError::NoTransportForType(format!(
1409                    "no operational BLE transport for adapter '{}'",
1410                    adapter
1411                ))
1412            })?;
1413
1414        // Validate the address format
1415        crate::transport::ble::addr::BleAddr::parse(addr_str).map_err(|e| {
1416            NodeError::NoTransportForType(format!("invalid BLE address '{}': {}", addr_str, e))
1417        })?;
1418
1419        Ok((transport_id, TransportAddr::from_string(addr_str)))
1420    }
1421
1422    // === Identity Accessors ===
1423
1424    /// Get this node's identity.
1425    pub fn identity(&self) -> &Identity {
1426        &self.identity
1427    }
1428
1429    /// Get this node's NodeAddr.
1430    pub fn node_addr(&self) -> &NodeAddr {
1431        self.identity.node_addr()
1432    }
1433
1434    /// Get this node's npub.
1435    pub fn npub(&self) -> String {
1436        self.identity.npub()
1437    }
1438
1439    /// Return a human-readable display name for a NodeAddr.
1440    ///
1441    /// Lookup order:
1442    /// 1. Host map hostname (from peer aliases + /etc/fips/hosts)
1443    /// 2. Configured peer alias or short npub (from startup map)
1444    /// 3. Active peer's short npub (e.g., inbound peer not in config)
1445    /// 4. Session endpoint's short npub (end-to-end, may not be direct peer)
1446    /// 5. Truncated NodeAddr hex (unknown address)
1447    pub(crate) fn peer_display_name(&self, addr: &NodeAddr) -> String {
1448        if let Some(hostname) = self.host_map.lookup_hostname(addr) {
1449            return hostname.to_string();
1450        }
1451        if let Some(name) = self.peer_aliases.get(addr) {
1452            return name.clone();
1453        }
1454        if let Some(peer) = self.peers.get(addr) {
1455            return peer.identity().short_npub();
1456        }
1457        if let Some(entry) = self.sessions.get(addr) {
1458            let (xonly, _) = entry.remote_pubkey().x_only_public_key();
1459            return PeerIdentity::from_pubkey(xonly).short_npub();
1460        }
1461        addr.short_hex()
1462    }
1463
1464    /// Tear down a `peers_by_index` entry **and** keep the shard-owned
1465    /// decrypt-worker state coherent: removes the same `cache_key`
1466    /// from the registered-sessions tracking set and tells the
1467    /// assigned shard worker to drop its `OwnedSessionState` entry.
1468    ///
1469    /// Use this instead of a bare `self.peers_by_index.remove(&key)`
1470    /// at every session-lifecycle teardown site (rekey cross-connection
1471    /// swap, peer disconnect, dispatch session-rotation) so the worker
1472    /// doesn't keep stale ciphers / replay windows around. The
1473    /// follow-up `RegisterSession` for the NEW key (if any) will then
1474    /// install the fresh state on the same shard.
1475    pub(in crate::node) fn deregister_session_index(&mut self, cache_key: (TransportId, u32)) {
1476        // Find the peer that owns this index BEFORE removing it from
1477        // the index map, so we can decide whether the deregistration
1478        // also tears down the peer's connected UDP socket.
1479        let owning_peer = self.peers_by_index.get(&cache_key).copied();
1480        self.peers_by_index.remove(&cache_key);
1481        if self.decrypt_registered_sessions.remove(&cache_key)
1482            && let Some(workers) = self.decrypt_workers.as_ref()
1483        {
1484            workers.unregister_session(cache_key);
1485        }
1486        // Tear down the per-peer connected UDP socket *only* if no
1487        // other peers_by_index entry still resolves to this peer.
1488        // Rekey drain calls into this helper with the OLD session
1489        // index while the NEW index is already installed and points
1490        // at the same peer — there the connect()-ed 5-tuple is
1491        // still valid for the new session and we must not close it.
1492        // Peer-teardown sites (CrossConnection swap, stale-index
1493        // fall-through in encrypted.rs, disconnect handler) call
1494        // here when this is the peer's last index, so the connected
1495        // socket goes away with the peer.
1496        if let Some(peer_addr) = owning_peer {
1497            let peer_has_other_index = self
1498                .peers_by_index
1499                .values()
1500                .any(|other| *other == peer_addr);
1501            if !peer_has_other_index {
1502                self.clear_connected_udp_for_peer(&peer_addr);
1503            }
1504        }
1505    }
1506
1507    /// Ensure the current FMP receive index resolves to this peer.
1508    ///
1509    /// Rekey msg1/msg2 handlers pre-register the pending index before
1510    /// cutover, but losing that registration in a debug build used to
1511    /// panic in the cutover path. Repairing the map here is safe: the
1512    /// peer has already promoted the pending session, and the decrypt
1513    /// worker registration immediately after cutover depends on the
1514    /// same `(transport_id, our_index)` key.
1515    pub(in crate::node) fn ensure_current_session_index_registered(
1516        &mut self,
1517        node_addr: &NodeAddr,
1518        context: &'static str,
1519    ) -> bool {
1520        let Some(peer) = self.peers.get(node_addr) else {
1521            return false;
1522        };
1523        let Some(transport_id) = peer.transport_id() else {
1524            warn!(
1525                peer = %self.peer_display_name(node_addr),
1526                context,
1527                "Cannot register current session index without transport id"
1528            );
1529            return false;
1530        };
1531        let Some(our_index) = peer.our_index() else {
1532            warn!(
1533                peer = %self.peer_display_name(node_addr),
1534                context,
1535                "Cannot register current session index without local index"
1536            );
1537            return false;
1538        };
1539
1540        let cache_key = (transport_id, our_index.as_u32());
1541        match self.peers_by_index.get(&cache_key).copied() {
1542            Some(existing) if existing == *node_addr => true,
1543            Some(existing) => {
1544                warn!(
1545                    peer = %self.peer_display_name(node_addr),
1546                    previous_owner = %self.peer_display_name(&existing),
1547                    transport_id = %transport_id,
1548                    our_index = %our_index,
1549                    context,
1550                    "Repairing current session index with stale owner"
1551                );
1552                self.peers_by_index.insert(cache_key, *node_addr);
1553                true
1554            }
1555            None => {
1556                warn!(
1557                    peer = %self.peer_display_name(node_addr),
1558                    transport_id = %transport_id,
1559                    our_index = %our_index,
1560                    context,
1561                    "Repairing missing current session index"
1562                );
1563                self.peers_by_index.insert(cache_key, *node_addr);
1564                true
1565            }
1566        }
1567    }
1568
1569    // === Configuration ===
1570
1571    /// Get the configuration.
1572    pub fn config(&self) -> &Config {
1573        &self.config
1574    }
1575
1576    /// Calculate the effective IPv6 MTU that can be sent over FIPS.
1577    ///
1578    /// Delegates to `upper::icmp::effective_ipv6_mtu()` with this node's
1579    /// transport MTU. Returns the maximum IPv6 packet size (including
1580    /// IPv6 header) that can be transmitted through the FIPS mesh.
1581    pub fn effective_ipv6_mtu(&self) -> u16 {
1582        crate::upper::icmp::effective_ipv6_mtu(self.transport_mtu())
1583    }
1584
1585    /// Get the transport MTU governing the global TUN-boundary MSS clamp.
1586    ///
1587    /// Returns the **minimum** MTU across all operational transports, or
1588    /// 1280 (IPv6 minimum) as fallback. Used for initial TUN configuration
1589    /// where a specific egress transport isn't yet known: the resulting
1590    /// `effective_ipv6_mtu` (transport_mtu - 77) and `max_mss`
1591    /// (effective_mtu - 60) form a conservative ceiling that fits ANY
1592    /// configured-transport's egress, eliminating PMTU-D black holes that
1593    /// would otherwise occur when a flow's actual egress is smaller than
1594    /// the clamp ceiling assumed at TUN init.
1595    ///
1596    /// Returning the smallest (rather than the first-iterated, which used
1597    /// to vary across HashMap iteration order + async-startup race) makes
1598    /// the clamp deterministic across daemon restarts.
1599    ///
1600    /// See `ISSUE-2026-0011` for the empirical investigation.
1601    pub fn transport_mtu(&self) -> u16 {
1602        let min_operational = self
1603            .transports
1604            .values()
1605            .filter(|h| h.is_operational())
1606            .map(|h| h.mtu())
1607            .min();
1608        if let Some(mtu) = min_operational {
1609            return mtu;
1610        }
1611        // Fallback to config: try UDP first, then Ethernet
1612        if let Some((_, cfg)) = self.config.transports.udp.iter().next() {
1613            return cfg.mtu();
1614        }
1615        1280
1616    }
1617
1618    // === State ===
1619
1620    /// Get the node state.
1621    pub fn state(&self) -> NodeState {
1622        self.state
1623    }
1624
1625    /// Get the node uptime.
1626    pub fn uptime(&self) -> std::time::Duration {
1627        self.started_at.elapsed()
1628    }
1629
1630    /// Check if node is operational.
1631    pub fn is_running(&self) -> bool {
1632        self.state.is_operational()
1633    }
1634
1635    /// Check if this is a leaf-only node.
1636    pub fn is_leaf_only(&self) -> bool {
1637        self.is_leaf_only
1638    }
1639
1640    // === Tree State ===
1641
1642    /// Get the tree state.
1643    pub fn tree_state(&self) -> &TreeState {
1644        &self.tree_state
1645    }
1646
1647    /// Get mutable tree state.
1648    pub fn tree_state_mut(&mut self) -> &mut TreeState {
1649        &mut self.tree_state
1650    }
1651
1652    // === Bloom State ===
1653
1654    /// Get the Bloom filter state.
1655    pub fn bloom_state(&self) -> &BloomState {
1656        &self.bloom_state
1657    }
1658
1659    /// Get mutable Bloom filter state.
1660    pub fn bloom_state_mut(&mut self) -> &mut BloomState {
1661        &mut self.bloom_state
1662    }
1663
1664    // === Mesh Size Estimate ===
1665
1666    /// Get the cached estimated mesh size.
1667    pub fn estimated_mesh_size(&self) -> Option<u64> {
1668        self.estimated_mesh_size
1669    }
1670
1671    /// Compute and cache the estimated mesh size from bloom filters.
1672    ///
1673    /// Uses the spanning tree partition: parent's filter covers nodes reachable
1674    /// upward, children's filters cover disjoint subtrees downward. The sum
1675    /// of estimated entry counts plus one (self) approximates total network size.
1676    pub(crate) fn compute_mesh_size(&mut self) {
1677        let my_addr = *self.tree_state.my_node_addr();
1678        let parent_id = *self.tree_state.my_declaration().parent_id();
1679        let is_root = self.tree_state.is_root();
1680
1681        let max_fpr = self.config.node.bloom.max_inbound_fpr;
1682        let mut total: f64 = 1.0; // count self
1683        let mut child_count: u32 = 0;
1684        let mut has_data = false;
1685
1686        // Parent's filter: nodes reachable upward through the tree.
1687        // If any contributing filter is above the FPR cap, we refuse to
1688        // estimate rather than substitute a partial/biased aggregate —
1689        // Node.estimated_mesh_size is already Option<u64> and consumers
1690        // (control socket, fipstop, periodic debug log) handle None.
1691        if !is_root
1692            && let Some(parent) = self.peers.get(&parent_id)
1693            && let Some(filter) = parent.inbound_filter()
1694        {
1695            match filter.estimated_count(max_fpr) {
1696                Some(n) => {
1697                    total += n;
1698                    has_data = true;
1699                }
1700                None => {
1701                    self.estimated_mesh_size = None;
1702                    return;
1703                }
1704            }
1705        }
1706
1707        // Children's filters: each child's subtree is disjoint
1708        for (peer_addr, peer) in &self.peers {
1709            if peer_addr == &parent_id {
1710                continue;
1711            }
1712            if let Some(decl) = self.tree_state.peer_declaration(peer_addr)
1713                && *decl.parent_id() == my_addr
1714            {
1715                child_count += 1;
1716                if let Some(filter) = peer.inbound_filter() {
1717                    match filter.estimated_count(max_fpr) {
1718                        Some(n) => {
1719                            total += n;
1720                            has_data = true;
1721                        }
1722                        None => {
1723                            self.estimated_mesh_size = None;
1724                            return;
1725                        }
1726                    }
1727                }
1728            }
1729        }
1730
1731        if !has_data {
1732            self.estimated_mesh_size = None;
1733            return;
1734        }
1735
1736        let size = total.round() as u64;
1737        self.estimated_mesh_size = Some(size);
1738
1739        // Periodic logging (reuse MMP default interval: 30s)
1740        let now = std::time::Instant::now();
1741        let should_log = match self.last_mesh_size_log {
1742            None => true,
1743            Some(last) => {
1744                now.duration_since(last)
1745                    >= std::time::Duration::from_secs(self.config.node.mmp.log_interval_secs)
1746            }
1747        };
1748        if should_log {
1749            tracing::debug!(
1750                estimated_mesh_size = size,
1751                peers = self.peers.len(),
1752                children = child_count,
1753                "Mesh size estimate"
1754            );
1755            self.last_mesh_size_log = Some(now);
1756        }
1757    }
1758
1759    // === Coord Cache ===
1760
1761    /// Get the coordinate cache.
1762    pub fn coord_cache(&self) -> &CoordCache {
1763        &self.coord_cache
1764    }
1765
1766    /// Get mutable coordinate cache.
1767    pub fn coord_cache_mut(&mut self) -> &mut CoordCache {
1768        &mut self.coord_cache
1769    }
1770
1771    // === Node Statistics ===
1772
1773    /// Get the node statistics.
1774    pub fn stats(&self) -> &stats::NodeStats {
1775        &self.stats
1776    }
1777
1778    /// Get mutable node statistics.
1779    pub(crate) fn stats_mut(&mut self) -> &mut stats::NodeStats {
1780        &mut self.stats
1781    }
1782
1783    /// Get the stats history collector.
1784    pub fn stats_history(&self) -> &stats_history::StatsHistory {
1785        &self.stats_history
1786    }
1787
1788    /// Sample the current node state into the stats history ring.
1789    /// Called once per tick from the RX loop.
1790    pub(crate) fn record_stats_history(&mut self) {
1791        let fwd = &self.stats.forwarding;
1792        let peers_with_mmp: Vec<f64> = self
1793            .peers
1794            .values()
1795            .filter_map(|p| p.mmp().map(|m| m.metrics.loss_rate()))
1796            .collect();
1797        let loss_rate = if peers_with_mmp.is_empty() {
1798            0.0
1799        } else {
1800            peers_with_mmp.iter().sum::<f64>() / peers_with_mmp.len() as f64
1801        };
1802
1803        let snap = stats_history::Snapshot {
1804            mesh_size: self.estimated_mesh_size,
1805            tree_depth: self.tree_state.my_coords().depth() as u32,
1806            peer_count: self.peers.len() as u64,
1807            parent_switches_total: self.stats.tree.parent_switches,
1808            bytes_in_total: fwd.received_bytes,
1809            bytes_out_total: fwd.forwarded_bytes + fwd.originated_bytes,
1810            packets_in_total: fwd.received_packets,
1811            packets_out_total: fwd.forwarded_packets + fwd.originated_packets,
1812            loss_rate,
1813            active_sessions: self.sessions.len() as u64,
1814        };
1815
1816        let now = std::time::Instant::now();
1817        let peer_snaps: Vec<stats_history::PeerSnapshot> = self
1818            .peers
1819            .values()
1820            .map(|p| {
1821                let stats = p.link_stats();
1822                let (srtt_ms, loss_rate, ecn_ce) = match p.mmp() {
1823                    Some(m) => (
1824                        m.metrics.srtt_ms(),
1825                        Some(m.metrics.loss_rate()),
1826                        m.receiver.ecn_ce_count() as u64,
1827                    ),
1828                    None => (None, None, 0),
1829                };
1830                stats_history::PeerSnapshot {
1831                    node_addr: *p.node_addr(),
1832                    last_seen: now,
1833                    srtt_ms,
1834                    loss_rate,
1835                    bytes_in_total: stats.bytes_recv,
1836                    bytes_out_total: stats.bytes_sent,
1837                    packets_in_total: stats.packets_recv,
1838                    packets_out_total: stats.packets_sent,
1839                    ecn_ce_total: ecn_ce,
1840                }
1841            })
1842            .collect();
1843
1844        self.stats_history.tick(now, &snap, &peer_snaps);
1845    }
1846
1847    // === TUN Interface ===
1848
1849    /// Get the TUN state.
1850    pub fn tun_state(&self) -> TunState {
1851        self.tun_state
1852    }
1853
1854    /// Get the TUN interface name, if active.
1855    pub fn tun_name(&self) -> Option<&str> {
1856        self.tun_name.as_deref()
1857    }
1858
1859    // === Resource Limits ===
1860
1861    /// Set the maximum number of connections (handshake phase).
1862    pub fn set_max_connections(&mut self, max: usize) {
1863        self.max_connections = max;
1864    }
1865
1866    /// Set the maximum number of peers (authenticated).
1867    pub fn set_max_peers(&mut self, max: usize) {
1868        self.max_peers = max;
1869    }
1870
1871    /// Returns false when starting more outbound work would exceed a resource
1872    /// cap. A cap of `0` means uncapped.
1873    pub(crate) fn outbound_admission_check(&self) -> bool {
1874        let connection_used = self
1875            .connections
1876            .len()
1877            .saturating_add(self.pending_connects.len());
1878        let peer_allowed = self.max_peers == 0 || self.peers.len() < self.max_peers;
1879        let connection_allowed =
1880            self.max_connections == 0 || connection_used < self.max_connections;
1881        let link_allowed = self.max_links == 0 || self.links.len() < self.max_links;
1882        peer_allowed && connection_allowed && link_allowed
1883    }
1884
1885    /// Admission for public/open-discovery outbound work. This includes the
1886    /// general connection/link caps and, when open Nostr discovery is enabled,
1887    /// the configured non-peer budget.
1888    pub(crate) fn open_discovery_outbound_admission_check(&self) -> bool {
1889        if !self.outbound_admission_check() {
1890            return false;
1891        }
1892
1893        let nostr = &self.config.node.discovery.nostr;
1894        if !nostr.enabled || nostr.policy != NostrDiscoveryPolicy::Open {
1895            return true;
1896        }
1897
1898        let configured_npubs = self
1899            .config
1900            .peers()
1901            .iter()
1902            .map(|peer| peer.npub.clone())
1903            .collect::<HashSet<_>>();
1904        self.open_discovery_enqueue_budget(&configured_npubs) > 0
1905    }
1906
1907    /// Like `outbound_admission_check`, but for racing a better path to a
1908    /// peer that is already authenticated. This may temporarily add a
1909    /// connection/link, but it does not consume a new peer slot.
1910    pub(crate) fn outbound_direct_refresh_admission_check(&self) -> bool {
1911        let connection_used = self
1912            .connections
1913            .len()
1914            .saturating_add(self.pending_connects.len());
1915        let connection_allowed =
1916            self.max_connections == 0 || connection_used < self.max_connections;
1917        let link_allowed = self.max_links == 0 || self.links.len() < self.max_links;
1918        connection_allowed && link_allowed
1919    }
1920
1921    /// Set the maximum number of links.
1922    pub fn set_max_links(&mut self, max: usize) {
1923        self.max_links = max;
1924    }
1925
1926    // === Counts ===
1927
1928    /// Number of pending connections (handshake in progress).
1929    pub fn connection_count(&self) -> usize {
1930        self.connections.len()
1931    }
1932
1933    /// Number of authenticated peers.
1934    pub fn peer_count(&self) -> usize {
1935        self.peers.len()
1936    }
1937
1938    /// Number of active links.
1939    pub fn link_count(&self) -> usize {
1940        self.links.len()
1941    }
1942
1943    /// Number of active transports.
1944    pub fn transport_count(&self) -> usize {
1945        self.transports.len()
1946    }
1947
1948    // === Transport Management ===
1949
1950    /// Allocate a new transport ID.
1951    pub fn allocate_transport_id(&mut self) -> TransportId {
1952        let id = TransportId::new(self.next_transport_id);
1953        self.next_transport_id += 1;
1954        id
1955    }
1956
1957    /// Get a transport by ID.
1958    pub fn get_transport(&self, id: &TransportId) -> Option<&TransportHandle> {
1959        self.transports.get(id)
1960    }
1961
1962    /// Get mutable transport by ID.
1963    pub fn get_transport_mut(&mut self, id: &TransportId) -> Option<&mut TransportHandle> {
1964        self.transports.get_mut(id)
1965    }
1966
1967    /// Iterate over transport IDs.
1968    pub fn transport_ids(&self) -> impl Iterator<Item = &TransportId> {
1969        self.transports.keys()
1970    }
1971
1972    /// Get the packet receiver for the event loop.
1973    pub fn packet_rx(&mut self) -> Option<&mut PacketRx> {
1974        self.packet_rx.as_mut()
1975    }
1976
1977    // === Link Management ===
1978
1979    /// Allocate a new link ID.
1980    pub fn allocate_link_id(&mut self) -> LinkId {
1981        let id = LinkId::new(self.next_link_id);
1982        self.next_link_id += 1;
1983        id
1984    }
1985
1986    /// Add a link.
1987    pub fn add_link(&mut self, link: Link) -> Result<(), NodeError> {
1988        if self.max_links > 0 && self.links.len() >= self.max_links {
1989            return Err(NodeError::MaxLinksExceeded {
1990                max: self.max_links,
1991            });
1992        }
1993        let link_id = link.link_id();
1994        let transport_id = link.transport_id();
1995        let remote_addr = link.remote_addr().clone();
1996
1997        self.links.insert(link_id, link);
1998        self.addr_to_link
1999            .insert((transport_id, remote_addr), link_id);
2000        Ok(())
2001    }
2002
2003    /// Get a link by ID.
2004    pub fn get_link(&self, link_id: &LinkId) -> Option<&Link> {
2005        self.links.get(link_id)
2006    }
2007
2008    /// Get a mutable link by ID.
2009    pub fn get_link_mut(&mut self, link_id: &LinkId) -> Option<&mut Link> {
2010        self.links.get_mut(link_id)
2011    }
2012
2013    /// Find link ID by transport address.
2014    pub fn find_link_by_addr(
2015        &self,
2016        transport_id: TransportId,
2017        addr: &TransportAddr,
2018    ) -> Option<LinkId> {
2019        self.addr_to_link
2020            .get(&(transport_id, addr.clone()))
2021            .copied()
2022    }
2023
2024    /// Remove a link.
2025    ///
2026    /// Only removes the addr_to_link reverse lookup if it still points to this
2027    /// link. In cross-connection scenarios, a newer link may have replaced the
2028    /// entry for the same address.
2029    pub fn remove_link(&mut self, link_id: &LinkId) -> Option<Link> {
2030        if let Some(link) = self.links.remove(link_id) {
2031            // Clean up reverse lookup only if it still maps to this link
2032            let key = (link.transport_id(), link.remote_addr().clone());
2033            if self.addr_to_link.get(&key) == Some(link_id) {
2034                self.addr_to_link.remove(&key);
2035            }
2036            Some(link)
2037        } else {
2038            None
2039        }
2040    }
2041
2042    pub(crate) fn cleanup_bootstrap_transport_if_unused(&mut self, transport_id: TransportId) {
2043        if !self.bootstrap_transports.contains(&transport_id) {
2044            return;
2045        }
2046
2047        let transport_in_use = self
2048            .links
2049            .values()
2050            .any(|link| link.transport_id() == transport_id)
2051            || self
2052                .connections
2053                .values()
2054                .any(|conn| conn.transport_id() == Some(transport_id))
2055            || self
2056                .peers
2057                .values()
2058                .any(|peer| peer.transport_id() == Some(transport_id))
2059            || self
2060                .pending_connects
2061                .iter()
2062                .any(|pending| pending.transport_id == transport_id);
2063
2064        if transport_in_use {
2065            return;
2066        }
2067
2068        tracing::debug!(
2069            transport_id = %transport_id,
2070            "bootstrap transport has no remaining references; dropping"
2071        );
2072
2073        self.bootstrap_transports.remove(&transport_id);
2074        self.bootstrap_transport_npubs.remove(&transport_id);
2075        self.transport_drops.remove(&transport_id);
2076        self.transports.remove(&transport_id);
2077    }
2078
2079    /// Iterate over all links.
2080    pub fn links(&self) -> impl Iterator<Item = &Link> {
2081        self.links.values()
2082    }
2083
2084    // === Connection Management (Handshake Phase) ===
2085
2086    /// Add a pending connection.
2087    pub fn add_connection(&mut self, connection: PeerConnection) -> Result<(), NodeError> {
2088        let link_id = connection.link_id();
2089
2090        if self.connections.contains_key(&link_id) {
2091            return Err(NodeError::ConnectionAlreadyExists(link_id));
2092        }
2093
2094        if self.max_connections > 0 && self.connections.len() >= self.max_connections {
2095            return Err(NodeError::MaxConnectionsExceeded {
2096                max: self.max_connections,
2097            });
2098        }
2099
2100        self.connections.insert(link_id, connection);
2101        Ok(())
2102    }
2103
2104    /// Get a connection by LinkId.
2105    pub fn get_connection(&self, link_id: &LinkId) -> Option<&PeerConnection> {
2106        self.connections.get(link_id)
2107    }
2108
2109    /// Get a mutable connection by LinkId.
2110    pub fn get_connection_mut(&mut self, link_id: &LinkId) -> Option<&mut PeerConnection> {
2111        self.connections.get_mut(link_id)
2112    }
2113
2114    /// Remove a connection.
2115    pub fn remove_connection(&mut self, link_id: &LinkId) -> Option<PeerConnection> {
2116        self.connections.remove(link_id)
2117    }
2118
2119    /// Iterate over all connections.
2120    pub fn connections(&self) -> impl Iterator<Item = &PeerConnection> {
2121        self.connections.values()
2122    }
2123
2124    // === Peer Management (Active Phase) ===
2125
2126    /// Get a peer by NodeAddr.
2127    pub fn get_peer(&self, node_addr: &NodeAddr) -> Option<&ActivePeer> {
2128        self.peers.get(node_addr)
2129    }
2130
2131    /// Get a mutable peer by NodeAddr.
2132    pub fn get_peer_mut(&mut self, node_addr: &NodeAddr) -> Option<&mut ActivePeer> {
2133        self.peers.get_mut(node_addr)
2134    }
2135
2136    /// Remove a peer.
2137    pub fn remove_peer(&mut self, node_addr: &NodeAddr) -> Option<ActivePeer> {
2138        self.peers.remove(node_addr)
2139    }
2140
2141    /// Iterate over all peers.
2142    pub fn peers(&self) -> impl Iterator<Item = &ActivePeer> {
2143        self.peers.values()
2144    }
2145
2146    /// Reference to the Nostr discovery handle if discovery is enabled.
2147    /// Used by control queries (`show_peers` per-peer Nostr-traversal
2148    /// state) to read failure-state without taking shared ownership.
2149    pub fn nostr_discovery_handle(&self) -> Option<&crate::discovery::nostr::NostrDiscovery> {
2150        self.nostr_discovery.as_deref()
2151    }
2152
2153    /// Iterate over all peer node IDs.
2154    pub fn peer_ids(&self) -> impl Iterator<Item = &NodeAddr> {
2155        self.peers.keys()
2156    }
2157
2158    /// Iterate over peers that can send traffic.
2159    pub fn sendable_peers(&self) -> impl Iterator<Item = &ActivePeer> {
2160        self.peers.values().filter(|p| p.can_send())
2161    }
2162
2163    /// Number of peers that can send traffic.
2164    pub fn sendable_peer_count(&self) -> usize {
2165        self.peers.values().filter(|p| p.can_send()).count()
2166    }
2167
2168    pub(crate) fn set_discovery_fallback_transit_allowed(
2169        &mut self,
2170        peer_addr: NodeAddr,
2171        allowed: bool,
2172    ) {
2173        if allowed {
2174            self.discovery_fallback_transit_blocked_peers
2175                .remove(&peer_addr);
2176        } else {
2177            self.discovery_fallback_transit_blocked_peers
2178                .insert(peer_addr);
2179        }
2180    }
2181
2182    pub(crate) fn configured_discovery_fallback_transit(
2183        &self,
2184        peer_addr: &NodeAddr,
2185    ) -> Option<bool> {
2186        self.configured_peer(peer_addr)
2187            .map(|peer| peer.discovery_fallback_transit)
2188    }
2189
2190    pub(crate) fn configured_peer(&self, peer_addr: &NodeAddr) -> Option<&PeerConfig> {
2191        self.config.peers().iter().find(|peer| {
2192            PeerIdentity::from_npub(&peer.npub)
2193                .ok()
2194                .is_some_and(|identity| identity.node_addr() == peer_addr)
2195        })
2196    }
2197
2198    pub(crate) fn discovery_fallback_transit_for_promotion(&self, peer_addr: &NodeAddr) -> bool {
2199        if let Some(retry_state) = self.retry_pending.get(peer_addr) {
2200            return retry_state.peer_config.discovery_fallback_transit;
2201        }
2202
2203        if let Some(allowed) = self.configured_discovery_fallback_transit(peer_addr) {
2204            return allowed;
2205        }
2206
2207        self.config.node.discovery.nostr.policy != crate::config::NostrDiscoveryPolicy::Open
2208    }
2209
2210    // === End-to-End Sessions ===
2211
2212    /// Get a session by remote NodeAddr.
2213    /// Disable the discovery forward rate limiter (for tests).
2214    #[cfg(test)]
2215    pub(crate) fn disable_discovery_forward_rate_limit(&mut self) {
2216        self.discovery_forward_limiter
2217            .set_interval(std::time::Duration::ZERO);
2218    }
2219
2220    #[cfg(test)]
2221    pub(crate) fn get_session(&self, remote: &NodeAddr) -> Option<&SessionEntry> {
2222        self.sessions.get(remote)
2223    }
2224
2225    /// Get a mutable session by remote NodeAddr.
2226    #[cfg(test)]
2227    pub(crate) fn get_session_mut(&mut self, remote: &NodeAddr) -> Option<&mut SessionEntry> {
2228        self.sessions.get_mut(remote)
2229    }
2230
2231    /// Remove a session.
2232    #[cfg(test)]
2233    pub(crate) fn remove_session(&mut self, remote: &NodeAddr) -> Option<SessionEntry> {
2234        self.sessions.remove(remote)
2235    }
2236
2237    /// Read the path_mtu_lookup entry for a destination FipsAddress.
2238    #[cfg(test)]
2239    pub(crate) fn path_mtu_lookup_get(&self, fips_addr: &crate::FipsAddress) -> Option<u16> {
2240        self.path_mtu_lookup
2241            .read()
2242            .ok()
2243            .and_then(|map| map.get(fips_addr).copied())
2244    }
2245
2246    /// Write a path_mtu_lookup entry directly (for tests that pre-seed the map).
2247    #[cfg(test)]
2248    pub(crate) fn path_mtu_lookup_insert(&self, fips_addr: crate::FipsAddress, mtu: u16) {
2249        if let Ok(mut map) = self.path_mtu_lookup.write() {
2250            map.insert(fips_addr, mtu);
2251        }
2252    }
2253
2254    /// Number of end-to-end sessions.
2255    pub fn session_count(&self) -> usize {
2256        self.sessions.len()
2257    }
2258
2259    /// Iterate over all session entries (for control queries).
2260    pub(crate) fn session_entries(&self) -> impl Iterator<Item = (&NodeAddr, &SessionEntry)> {
2261        self.sessions.iter()
2262    }
2263
2264    // === Identity Cache ===
2265
2266    /// Register a node in the identity cache for FipsAddress → NodeAddr lookup.
2267    pub(crate) fn register_identity(
2268        &mut self,
2269        node_addr: NodeAddr,
2270        pubkey: secp256k1::PublicKey,
2271    ) -> bool {
2272        let mut prefix = [0u8; 15];
2273        prefix.copy_from_slice(&node_addr.as_bytes()[0..15]);
2274        if let Some(entry) = self.identity_cache.get(&prefix)
2275            && entry.node_addr == node_addr
2276            && entry.pubkey == pubkey
2277        {
2278            // Endpoint sends pass the same PeerIdentity on every packet. Once
2279            // validated, avoid re-deriving NodeAddr from the public key in the
2280            // data path; that hash showed up in macOS sender profiles.
2281            return true;
2282        }
2283
2284        let (xonly, _) = pubkey.x_only_public_key();
2285        let derived_node_addr = NodeAddr::from_pubkey(&xonly);
2286        if derived_node_addr != node_addr {
2287            debug!(
2288                claimed_node_addr = %node_addr,
2289                derived_node_addr = %derived_node_addr,
2290                "Rejected identity cache entry with mismatched public key"
2291            );
2292            return false;
2293        }
2294
2295        let now_ms = Self::now_ms();
2296        if let Some(entry) = self.identity_cache.get_mut(&prefix)
2297            && entry.node_addr == node_addr
2298        {
2299            entry.pubkey = pubkey;
2300            entry.last_seen_ms = now_ms;
2301            return true;
2302        }
2303
2304        let npub = encode_npub(&xonly);
2305        self.identity_cache.insert(
2306            prefix,
2307            IdentityCacheEntry::new(node_addr, pubkey, npub, now_ms),
2308        );
2309        // LRU eviction
2310        let max = self.config.node.cache.identity_size;
2311        if self.identity_cache.len() > max
2312            && let Some(oldest_key) = self
2313                .identity_cache
2314                .iter()
2315                .min_by_key(|(_, entry)| entry.last_seen_ms)
2316                .map(|(k, _)| *k)
2317        {
2318            self.identity_cache.remove(&oldest_key);
2319        }
2320        true
2321    }
2322
2323    /// Look up a destination by FipsAddress prefix (bytes 1-15 of the IPv6 address).
2324    pub(crate) fn lookup_by_fips_prefix(
2325        &mut self,
2326        prefix: &[u8; 15],
2327    ) -> Option<(NodeAddr, secp256k1::PublicKey)> {
2328        if let Some(entry) = self.identity_cache.get_mut(prefix) {
2329            entry.last_seen_ms = Self::now_ms(); // LRU touch
2330            Some((entry.node_addr, entry.pubkey))
2331        } else {
2332            None
2333        }
2334    }
2335
2336    /// Check if a node's identity is in the cache (without LRU touch).
2337    pub(crate) fn has_cached_identity(&self, addr: &NodeAddr) -> bool {
2338        let mut prefix = [0u8; 15];
2339        prefix.copy_from_slice(&addr.as_bytes()[0..15]);
2340        self.identity_cache.contains_key(&prefix)
2341    }
2342
2343    /// Number of identity cache entries.
2344    pub fn identity_cache_len(&self) -> usize {
2345        self.identity_cache.len()
2346    }
2347
2348    /// Iterate over identity cache entries.
2349    ///
2350    /// Returns `(NodeAddr, PublicKey, last_seen_ms)` for each cached identity.
2351    /// Used by the `show_identity_cache` control query.
2352    pub fn identity_cache_iter(
2353        &self,
2354    ) -> impl Iterator<Item = (&NodeAddr, &secp256k1::PublicKey, u64)> {
2355        self.identity_cache
2356            .values()
2357            .map(|entry| (&entry.node_addr, &entry.pubkey, entry.last_seen_ms))
2358    }
2359
2360    /// Configured maximum identity cache size.
2361    pub fn identity_cache_max(&self) -> usize {
2362        self.config.node.cache.identity_size
2363    }
2364
2365    /// Number of pending discovery lookups.
2366    pub fn pending_lookup_count(&self) -> usize {
2367        self.pending_lookups.len()
2368    }
2369
2370    /// Iterate over pending discovery lookups for diagnostics.
2371    pub fn pending_lookups_iter(
2372        &self,
2373    ) -> impl Iterator<Item = (&NodeAddr, &handlers::discovery::PendingLookup)> {
2374        self.pending_lookups.iter()
2375    }
2376
2377    /// Number of recent discovery requests tracked.
2378    pub fn recent_request_count(&self) -> usize {
2379        self.recent_requests.len()
2380    }
2381
2382    /// Count of destinations with queued TUN packets awaiting session setup.
2383    pub fn pending_tun_destinations(&self) -> usize {
2384        self.pending_tun_packets.len()
2385    }
2386
2387    /// Total TUN packets queued across all destinations.
2388    pub fn pending_tun_total_packets(&self) -> usize {
2389        self.pending_tun_packets.values().map(|q| q.len()).sum()
2390    }
2391
2392    /// Iterate over retry state for diagnostics.
2393    pub fn retry_state_iter(&self) -> impl Iterator<Item = (&NodeAddr, &retry::RetryState)> {
2394        self.retry_pending.iter()
2395    }
2396
2397    // === Routing ===
2398
2399    /// Check if a peer is a tree neighbor (parent or child in the spanning tree).
2400    ///
2401    /// Returns true if the peer is our current tree parent, or if the peer
2402    /// has declared us as their parent (making them our child).
2403    pub(crate) fn is_tree_peer(&self, peer_addr: &NodeAddr) -> bool {
2404        // Peer is our parent
2405        if !self.tree_state.is_root() && self.tree_state.my_declaration().parent_id() == peer_addr {
2406            return true;
2407        }
2408        // Peer is our child (their declaration names us as parent)
2409        if let Some(decl) = self.tree_state.peer_declaration(peer_addr)
2410            && decl.parent_id() == self.node_addr()
2411        {
2412            return true;
2413        }
2414        false
2415    }
2416
2417    /// Find next hop for a destination node address.
2418    ///
2419    /// Routing priority:
2420    /// 1. Destination is self → `None` (local delivery)
2421    /// 2. Destination is a direct peer → that peer
2422    /// 3. Reply-learned routes in `reply_learned` mode. These are locally
2423    ///    observed reverse paths, selected with weighted multipath plus
2424    ///    periodic coordinate/tree exploration.
2425    /// 4. Bloom filter candidates with cached dest coords → among peers whose
2426    ///    bloom filter contains the destination, pick the one that minimizes
2427    ///    tree distance to the destination, with
2428    ///    `(link_cost, tree_distance_to_dest, node_addr)` tie-breaking.
2429    ///    The self-distance check ensures only peers strictly closer to the
2430    ///    destination than us are considered (prevents routing loops).
2431    /// 5. Greedy tree routing fallback (requires cached dest coords)
2432    /// 6. No route → `None`
2433    ///
2434    /// Both the bloom filter and tree routing paths require cached destination
2435    /// coordinates (checked in `coord_cache`). Without coordinates, the node
2436    /// cannot make loop-free forwarding decisions. The caller should signal
2437    /// `CoordsRequired` back to the source when `None` is returned for a
2438    /// non-local destination.
2439    pub fn find_next_hop(&mut self, dest_node_addr: &NodeAddr) -> Option<&ActivePeer> {
2440        // 1. Local delivery
2441        if dest_node_addr == self.node_addr() {
2442            return None;
2443        }
2444
2445        // 2. Healthy direct peer. Stale direct links remain usable, but in
2446        // reply-learned mode they must not hide a live mesh route learned from
2447        // recent traffic or discovery.
2448        let direct_peer_can_send = self
2449            .peers
2450            .get(dest_node_addr)
2451            .is_some_and(|peer| peer.can_send());
2452        if let Some(peer) = self.peers.get(dest_node_addr)
2453            && peer.is_healthy()
2454        {
2455            return Some(peer);
2456        }
2457
2458        let now_ms = Self::now_ms();
2459
2460        let sendable_learned_peers = if self.config.node.routing.mode == RoutingMode::ReplyLearned {
2461            Some(
2462                self.peers
2463                    .iter()
2464                    .filter(|(_, peer)| peer.can_send())
2465                    .map(|(addr, _)| *addr)
2466                    .collect::<HashSet<_>>(),
2467            )
2468        } else {
2469            None
2470        };
2471
2472        // 3. Optional reply-learned routing. These entries are not peer
2473        // claims; they are local observations of which peer carried traffic
2474        // or a verified lookup response back from the destination. Most
2475        // packets use weighted multipath over learned routes, but periodic
2476        // fallback exploration lets coord/bloom/tree routes discover better
2477        // candidates.
2478        let explore_fallback = sendable_learned_peers.as_ref().is_some_and(|sendable| {
2479            self.learned_routes.should_explore_fallback(
2480                dest_node_addr,
2481                now_ms,
2482                self.config.node.routing.learned_fallback_explore_interval,
2483                |addr| sendable.contains(addr),
2484            )
2485        });
2486        if let Some(sendable) = &sendable_learned_peers
2487            && !explore_fallback
2488            && let Some(next_hop_addr) =
2489                self.learned_routes
2490                    .select_next_hop(dest_node_addr, now_ms, |addr| sendable.contains(addr))
2491        {
2492            return self.peers.get(&next_hop_addr);
2493        }
2494
2495        // Look up cached destination coordinates (required by both bloom and tree paths).
2496        let Some(dest_coords) = self
2497            .coord_cache
2498            .get_and_touch(dest_node_addr, now_ms)
2499            .cloned()
2500        else {
2501            if let Some(sendable) = &sendable_learned_peers
2502                && let Some(next_hop_addr) =
2503                    self.learned_routes
2504                        .select_next_hop(dest_node_addr, now_ms, |addr| sendable.contains(addr))
2505            {
2506                return self.peers.get(&next_hop_addr);
2507            }
2508            if direct_peer_can_send {
2509                return self.peers.get(dest_node_addr);
2510            }
2511            return None;
2512        };
2513
2514        // 4. Bloom filter candidates — requires dest_coords for loop-free selection.
2515        //    If no candidate is strictly closer, fall through to tree routing.
2516        let coordinate_route_addr = {
2517            let candidates: Vec<&ActivePeer> = self.destination_in_filters(dest_node_addr);
2518            if !candidates.is_empty() {
2519                self.select_best_candidate(&candidates, &dest_coords)
2520                    .map(|peer| *peer.node_addr())
2521            } else {
2522                None
2523            }
2524        };
2525        if let Some(next_hop_addr) = coordinate_route_addr {
2526            return self.peers.get(&next_hop_addr);
2527        }
2528
2529        // 5. Greedy tree routing fallback
2530        let tree_route_addr = self
2531            .tree_state
2532            .find_next_hop(&dest_coords)
2533            .filter(|next_hop_id| {
2534                self.peers
2535                    .get(next_hop_id)
2536                    .is_some_and(|peer| peer.can_send())
2537            });
2538        if let Some(next_hop_addr) = tree_route_addr {
2539            return self.peers.get(&next_hop_addr);
2540        }
2541        if explore_fallback {
2542            return sendable_learned_peers.as_ref().and_then(|sendable| {
2543                self.learned_routes
2544                    .select_next_hop(dest_node_addr, now_ms, |addr| sendable.contains(addr))
2545                    .and_then(|next_hop_addr| self.peers.get(&next_hop_addr))
2546            });
2547        }
2548
2549        if let Some(sendable) = &sendable_learned_peers
2550            && let Some(next_hop_addr) =
2551                self.learned_routes
2552                    .select_next_hop(dest_node_addr, now_ms, |addr| sendable.contains(addr))
2553        {
2554            return self.peers.get(&next_hop_addr);
2555        }
2556
2557        if direct_peer_can_send {
2558            return self.peers.get(dest_node_addr);
2559        }
2560
2561        None
2562    }
2563
2564    pub(in crate::node) fn learn_reverse_route(
2565        &mut self,
2566        destination: NodeAddr,
2567        next_hop: NodeAddr,
2568    ) {
2569        if self.config.node.routing.mode != RoutingMode::ReplyLearned
2570            || destination == *self.node_addr()
2571        {
2572            return;
2573        }
2574        let now_ms = Self::now_ms();
2575        self.learned_routes.learn(
2576            destination,
2577            next_hop,
2578            now_ms,
2579            self.config.node.routing.learned_ttl_secs,
2580            self.config.node.routing.max_learned_routes_per_dest,
2581        );
2582    }
2583
2584    pub(in crate::node) fn record_route_failure(
2585        &mut self,
2586        destination: NodeAddr,
2587        next_hop: NodeAddr,
2588    ) {
2589        if self.config.node.routing.mode != RoutingMode::ReplyLearned {
2590            return;
2591        }
2592        self.learned_routes.record_failure(&destination, &next_hop);
2593    }
2594
2595    pub(crate) fn learned_route_table_snapshot(&self, now_ms: u64) -> LearnedRouteTableSnapshot {
2596        self.learned_routes.snapshot(now_ms)
2597    }
2598
2599    pub(in crate::node) fn purge_learned_routes(&mut self, now_ms: u64) {
2600        self.learned_routes.purge_expired(now_ms);
2601    }
2602
2603    /// Select the best peer from a set of bloom filter candidates.
2604    ///
2605    /// Uses distance from each candidate's tree coordinates to the destination
2606    /// as the primary metric (after link_cost). Only selects peers that are
2607    /// strictly closer to the destination than we are (self-distance check
2608    /// prevents routing loops).
2609    ///
2610    /// Ordering: `(link_cost, distance_to_dest, node_addr)`.
2611    fn select_best_candidate<'a>(
2612        &'a self,
2613        candidates: &[&'a ActivePeer],
2614        dest_coords: &crate::tree::TreeCoordinate,
2615    ) -> Option<&'a ActivePeer> {
2616        let my_distance = self.tree_state.my_coords().distance_to(dest_coords);
2617
2618        let mut best: Option<(&ActivePeer, f64, usize)> = None;
2619
2620        for &candidate in candidates {
2621            if !candidate.can_send() {
2622                continue;
2623            }
2624
2625            let cost = candidate.link_cost();
2626
2627            let dist = self
2628                .tree_state
2629                .peer_coords(candidate.node_addr())
2630                .map(|pc| pc.distance_to(dest_coords))
2631                .unwrap_or(usize::MAX);
2632
2633            // Self-distance check: only consider peers strictly closer
2634            // to the destination than we are (prevents routing loops)
2635            if dist >= my_distance {
2636                continue;
2637            }
2638
2639            let dominated = match &best {
2640                None => true,
2641                Some((_, best_cost, best_dist)) => {
2642                    cost < *best_cost
2643                        || (cost == *best_cost && dist < *best_dist)
2644                        || (cost == *best_cost
2645                            && dist == *best_dist
2646                            && candidate.node_addr() < best.as_ref().unwrap().0.node_addr())
2647                }
2648            };
2649
2650            if dominated {
2651                best = Some((candidate, cost, dist));
2652            }
2653        }
2654
2655        best.map(|(peer, _, _)| peer)
2656    }
2657
2658    /// Check if a destination is in any peer's bloom filter.
2659    pub fn destination_in_filters(&self, dest: &NodeAddr) -> Vec<&ActivePeer> {
2660        self.peers.values().filter(|p| p.may_reach(dest)).collect()
2661    }
2662
2663    /// Get the TUN packet sender channel.
2664    ///
2665    /// Returns None if TUN is not active or the node hasn't been started.
2666    pub fn tun_tx(&self) -> Option<&TunTx> {
2667        self.tun_tx.as_ref()
2668    }
2669
2670    /// Attach app-owned packet I/O for embedded operation without a system TUN.
2671    ///
2672    /// This must be called before [`Node::start`] and requires `tun.enabled =
2673    /// false`. Outbound packets sent to the returned sender are processed by the
2674    /// normal session pipeline. Inbound packets delivered by FIPS sessions are
2675    /// sent to the returned receiver with source attribution.
2676    pub fn attach_external_packet_io(
2677        &mut self,
2678        capacity: usize,
2679    ) -> Result<ExternalPacketIo, NodeError> {
2680        if self.state != NodeState::Created {
2681            return Err(NodeError::Config(ConfigError::Validation(
2682                "external packet I/O must be attached before node start".to_string(),
2683            )));
2684        }
2685        if self.config.tun.enabled {
2686            return Err(NodeError::Config(ConfigError::Validation(
2687                "external packet I/O requires tun.enabled=false".to_string(),
2688            )));
2689        }
2690
2691        let capacity = capacity.max(1);
2692        let (outbound_tx, outbound_rx) = tokio::sync::mpsc::channel(capacity);
2693        let (inbound_tx, inbound_rx) = tokio::sync::mpsc::channel(capacity);
2694        self.tun_outbound_rx = Some(outbound_rx);
2695        self.external_packet_tx = Some(inbound_tx);
2696
2697        Ok(ExternalPacketIo {
2698            outbound_tx,
2699            inbound_rx,
2700        })
2701    }
2702
2703    /// Attach app-owned endpoint data I/O for embedded operation.
2704    ///
2705    /// Commands sent to the returned sender are processed by the node RX loop.
2706    /// Incoming endpoint data is emitted as source-attributed events.
2707    pub(crate) fn attach_endpoint_data_io(
2708        &mut self,
2709        capacity: usize,
2710    ) -> Result<EndpointDataIo, NodeError> {
2711        if self.state != NodeState::Created {
2712            return Err(NodeError::Config(ConfigError::Validation(
2713                "endpoint data I/O must be attached before node start".to_string(),
2714            )));
2715        }
2716
2717        let command_capacity = endpoint_data_command_capacity(capacity);
2718        let (command_tx, command_rx) = tokio::sync::mpsc::channel(command_capacity);
2719        // Inbound endpoint-data events use an unbounded channel — see
2720        // `EndpointDataIo::event_rx` docs for the rationale (kills the
2721        // per-packet semaphore + the cross-task relay task that used to
2722        // sit on top of this channel).
2723        let (event_tx, event_rx) = tokio::sync::mpsc::unbounded_channel();
2724        self.endpoint_command_rx = Some(command_rx);
2725        self.endpoint_event_tx = Some(event_tx.clone());
2726
2727        Ok(EndpointDataIo {
2728            command_tx,
2729            event_rx,
2730            event_tx,
2731        })
2732    }
2733
2734    pub(crate) fn pubkey_for_node_addr(&self, addr: &NodeAddr) -> Option<secp256k1::PublicKey> {
2735        let mut prefix = [0u8; 15];
2736        prefix.copy_from_slice(&addr.as_bytes()[0..15]);
2737        self.identity_cache
2738            .get(&prefix)
2739            .filter(|entry| &entry.node_addr == addr)
2740            .map(|entry| entry.pubkey)
2741    }
2742
2743    pub(crate) fn npub_for_node_addr(&self, addr: &NodeAddr) -> Option<String> {
2744        let mut prefix = [0u8; 15];
2745        prefix.copy_from_slice(&addr.as_bytes()[0..15]);
2746        self.identity_cache
2747            .get(&prefix)
2748            .filter(|entry| &entry.node_addr == addr)
2749            .map(|entry| entry.npub.clone())
2750    }
2751
2752    pub(in crate::node) fn deliver_external_ipv6_packet(
2753        &self,
2754        src_addr: &NodeAddr,
2755        packet: Vec<u8>,
2756    ) {
2757        let Some(external_packet_tx) = &self.external_packet_tx else {
2758            return;
2759        };
2760        if packet.len() < 40 {
2761            return;
2762        }
2763        let Ok(destination) = FipsAddress::from_slice(&packet[24..40]) else {
2764            return;
2765        };
2766        let delivered = NodeDeliveredPacket {
2767            source_node_addr: *src_addr,
2768            source_npub: self.npub_for_node_addr(src_addr),
2769            destination,
2770            packet,
2771        };
2772        if let Err(error) = external_packet_tx.try_send(delivered) {
2773            debug!(error = %error, "Failed to deliver packet to external app sink");
2774        }
2775    }
2776
2777    // === Sending ===
2778
2779    /// Encrypt and send a link-layer message to an authenticated peer.
2780    ///
2781    /// The plaintext should include the message type byte followed by the
2782    /// message-specific payload (e.g., `[0x50, reason]` for Disconnect).
2783    ///
2784    /// The send path prepends a 4-byte session-relative timestamp (inner
2785    /// header) before encryption. The full 16-byte outer header is used
2786    /// as AAD for the AEAD construction.
2787    ///
2788    /// This is the standard path for sending any link-layer control message
2789    /// to a peer over their encrypted Noise session.
2790    pub(super) async fn send_encrypted_link_message(
2791        &mut self,
2792        node_addr: &NodeAddr,
2793        plaintext: &[u8],
2794    ) -> Result<(), NodeError> {
2795        self.send_encrypted_link_message_with_ce(node_addr, plaintext, false)
2796            .await
2797    }
2798
2799    /// Update the local-outbound-broken signal from a `transport.send`
2800    /// outcome. Sets `last_local_send_failure_at` on local-side io
2801    /// errors (NetworkUnreachable / HostUnreachable / AddrNotAvailable);
2802    /// clears it on success. The reaper consults this in
2803    /// `check_link_heartbeats` to switch to `fast_link_dead_timeout_secs`.
2804    pub(in crate::node) fn note_local_send_outcome(
2805        &mut self,
2806        result: &Result<usize, TransportError>,
2807    ) {
2808        match result {
2809            Ok(_) => {
2810                if self.last_local_send_failure_at.is_some() {
2811                    self.last_local_send_failure_at = None;
2812                }
2813            }
2814            Err(error) if error.is_local_route_unavailable() => {
2815                self.last_local_send_failure_at = Some(std::time::Instant::now());
2816            }
2817            Err(_) => {}
2818        }
2819    }
2820
2821    /// Return the active dead-timeout after considering recent local route
2822    /// failures. The fast-dead signal is intentionally short-lived: on the
2823    /// UDP worker path a send call can return before the kernel result is
2824    /// observed, so a single stale route error must not compress liveness for
2825    /// the whole normal dead-timeout window.
2826    pub(in crate::node) fn local_send_failure_dead_timeout(
2827        &mut self,
2828        now: std::time::Instant,
2829        dead_timeout: std::time::Duration,
2830        fast_dead_timeout: std::time::Duration,
2831    ) -> std::time::Duration {
2832        match self.last_local_send_failure_at {
2833            Some(t) if now.duration_since(t) <= LOCAL_SEND_FAILURE_FAST_DEAD_WINDOW => {
2834                fast_dead_timeout.min(dead_timeout)
2835            }
2836            Some(_) => {
2837                self.last_local_send_failure_at = None;
2838                dead_timeout
2839            }
2840            None => dead_timeout,
2841        }
2842    }
2843
2844    pub(in crate::node) fn mark_rx_loop_maintenance_timeout(&mut self) {
2845        self.last_rx_loop_maintenance_timeout_at = Some(std::time::Instant::now());
2846    }
2847
2848    pub(in crate::node) fn rx_loop_maintenance_timed_out_recently(&self) -> bool {
2849        let Some(t) = self.last_rx_loop_maintenance_timeout_at else {
2850            return false;
2851        };
2852        let grace = std::time::Duration::from_secs(self.config.node.link_dead_timeout_secs.max(1));
2853        std::time::Instant::now().duration_since(t) <= grace
2854    }
2855
2856    /// Like `send_encrypted_link_message` but allows setting the FMP CE flag.
2857    ///
2858    /// Used by the forwarding path to relay congestion signals hop-by-hop.
2859    pub(super) async fn send_encrypted_link_message_with_ce(
2860        &mut self,
2861        node_addr: &NodeAddr,
2862        plaintext: &[u8],
2863        ce_flag: bool,
2864    ) -> Result<(), NodeError> {
2865        let peer = self
2866            .peers
2867            .get_mut(node_addr)
2868            .ok_or(NodeError::PeerNotFound(*node_addr))?;
2869
2870        let their_index = peer.their_index().ok_or_else(|| NodeError::SendFailed {
2871            node_addr: *node_addr,
2872            reason: "no their_index".into(),
2873        })?;
2874        let transport_id = peer.transport_id().ok_or_else(|| NodeError::SendFailed {
2875            node_addr: *node_addr,
2876            reason: "no transport_id".into(),
2877        })?;
2878        let remote_addr = peer
2879            .current_addr()
2880            .cloned()
2881            .ok_or_else(|| NodeError::SendFailed {
2882                node_addr: *node_addr,
2883                reason: "no current_addr".into(),
2884            })?;
2885        #[cfg(any(target_os = "linux", target_os = "macos"))]
2886        let connected_socket = peer.connected_udp();
2887
2888        // Prepend 4-byte session-relative timestamp (inner header)
2889        let timestamp_ms = peer.session_elapsed_ms();
2890
2891        // MMP: read spin bit value before entering session borrow
2892        let sp_flag = peer.mmp().map(|mmp| mmp.spin_bit.tx_bit()).unwrap_or(false);
2893        let mut flags = if sp_flag { FLAG_SP } else { 0 };
2894        if ce_flag {
2895            flags |= FLAG_CE;
2896        }
2897        if peer.current_k_bit() {
2898            flags |= FLAG_KEY_EPOCH;
2899        }
2900
2901        let session = peer
2902            .noise_session_mut()
2903            .ok_or_else(|| NodeError::SendFailed {
2904                node_addr: *node_addr,
2905                reason: "no noise session".into(),
2906            })?;
2907
2908        // Build 16-byte outer header upfront. The inner-plaintext
2909        // layout is `[ts:4 LE][plaintext...]`, so its length is exactly
2910        // `INNER_TS_LEN + plaintext.len()` — no need to build the Vec
2911        // just to measure it. The worker path uses this length to size
2912        // the wire buffer directly; the legacy path below still
2913        // materialises a separate `inner_plaintext` Vec for the inline
2914        // encrypt-and-send call.
2915        const INNER_TS_LEN: usize = 4;
2916        let counter = session.current_send_counter();
2917        let inner_len = INNER_TS_LEN + plaintext.len();
2918        let payload_len = inner_len as u16;
2919        let header = build_established_header(their_index, counter, flags, payload_len);
2920
2921        // **UDP send fast path.** The encrypt-worker pool is always
2922        // spawned at lifecycle start (workers = num_cpus) in
2923        // production, so this branch is taken for every authentic
2924        // send on every UDP-transported established session. The
2925        // AEAD work + sendmsg syscall run on a dedicated OS thread;
2926        // the rx_loop only builds the wire buffer + reserves the
2927        // counter inline.
2928        //
2929        // Other transport kinds (BLE, TCP, sim, ethernet) fall
2930        // through to the inline encrypt + transport.send path
2931        // below — those don't have raw-fd / sendmmsg / UDP_GSO
2932        // benefits to expose through the worker pool, so the simpler
2933        // synchronous send is the right shape for them.
2934        //
2935        // The `encrypt_workers.is_some()` check below is true in
2936        // production (lifecycle::start spawns the pool); it stays
2937        // checked rather than `expect()`-ed because unit tests
2938        // construct `Node` without calling `start()`.
2939        let transport_for_send = self
2940            .transports
2941            .get(&transport_id)
2942            .ok_or(NodeError::TransportNotFound(transport_id))?;
2943        match transport_for_send.connection_state(&remote_addr) {
2944            ConnectionState::Connected => {}
2945            other => {
2946                if matches!(other, ConnectionState::None) {
2947                    let _ = transport_for_send.connect(&remote_addr).await;
2948                }
2949                return Err(NodeError::SendFailed {
2950                    node_addr: *node_addr,
2951                    reason: format!("transport connection not ready: {:?}", other),
2952                });
2953            }
2954        }
2955        let is_udp = matches!(transport_for_send, TransportHandle::Udp(_));
2956        if let Some(workers) = self.encrypt_workers.as_ref().cloned()
2957            && is_udp
2958            && let Some(cipher_clone) = session.send_cipher_clone()
2959        {
2960            {
2961                // Reserve the counter on the session so subsequent
2962                // sends don't reuse it. `current_send_counter` only
2963                // peeks; we advance via `take_send_counter`.
2964                let reserved_counter =
2965                    session
2966                        .take_send_counter()
2967                        .map_err(|e| NodeError::SendFailed {
2968                            node_addr: *node_addr,
2969                            reason: format!("counter reservation failed: {}", e),
2970                        })?;
2971                debug_assert_eq!(reserved_counter, counter);
2972                // Re-derive the header with the now-locked-in counter
2973                // value (same value, but the call sequence is more
2974                // explicit).
2975                let header =
2976                    build_established_header(their_index, reserved_counter, flags, payload_len);
2977                let transport = transport_for_send;
2978                // Snapshot the per-peer connected UDP socket before
2979                // resolving the fallback address. On the established
2980                // steady-state path this socket already carries the
2981                // kernel peer address, so re-parsing the configured
2982                // transport address and touching the DNS cache on every
2983                // packet is pure overhead on the sender hot path.
2984                let send_target = {
2985                    if let TransportHandle::Udp(udp) = transport {
2986                        let socket_addr = {
2987                            #[cfg(any(target_os = "linux", target_os = "macos"))]
2988                            {
2989                                match connected_socket.as_ref() {
2990                                    Some(socket) => Some(socket.peer_addr()),
2991                                    None => udp.resolve_for_off_task(&remote_addr).await.ok(),
2992                                }
2993                            }
2994                            #[cfg(not(any(target_os = "linux", target_os = "macos")))]
2995                            {
2996                                udp.resolve_for_off_task(&remote_addr).await.ok()
2997                            }
2998                        };
2999                        match (udp.async_socket(), socket_addr) {
3000                            (Some(socket), Some(socket_addr)) => Some((socket, socket_addr)),
3001                            _ => None,
3002                        }
3003                    } else {
3004                        None
3005                    }
3006                };
3007                if let Some((socket, socket_addr)) = send_target {
3008                    // Build the wire buffer **directly** from
3009                    // `plaintext` with a single allocation:
3010                    //   `[16 header][4 ts][plaintext...]` with
3011                    // +16 trailing capacity for the AEAD tag.
3012                    // The worker seals `wire_buf[16..]` in
3013                    // place and appends the tag — no second
3014                    // alloc, no second memcpy.
3015                    //
3016                    // Previous design built `inner_plaintext`
3017                    // via `prepend_inner_header` (1 alloc + 1
3018                    // copy) and then let the worker memcpy
3019                    // header + plaintext into a fresh Vec
3020                    // (another alloc + copy). At ~100 kpps the
3021                    // saved alloc/copy is ~150 MB/sec of memory
3022                    // bandwidth on the hot rx_loop + worker.
3023                    let wire_capacity = ESTABLISHED_HEADER_SIZE + inner_len + 16;
3024                    let mut wire_buf = Vec::with_capacity(wire_capacity);
3025                    wire_buf.extend_from_slice(&header);
3026                    wire_buf.extend_from_slice(&timestamp_ms.to_le_bytes());
3027                    wire_buf.extend_from_slice(plaintext);
3028                    let predicted_bytes = wire_capacity;
3029                    // Stats / MMP update inline — predicted size
3030                    // is exact for ChaCha20-Poly1305 (tag is
3031                    // constant 16 bytes). When `connected_socket` is
3032                    // `Some`, the worker sends on it without a
3033                    // destination sockaddr — the kernel skips the
3034                    // per-packet sockaddr + route + neighbor resolve.
3035                    if let Some(peer) = self.peers.get_mut(node_addr) {
3036                        peer.link_stats_mut().record_sent(predicted_bytes);
3037                        if let Some(mmp) = peer.mmp_mut() {
3038                            mmp.sender
3039                                .record_sent(reserved_counter, timestamp_ms, predicted_bytes);
3040                        }
3041                    }
3042                    let scheduling_weight = self.send_weight_for_peer(node_addr);
3043                    workers.dispatch(self::encrypt_worker::FmpSendJob {
3044                        cipher: cipher_clone,
3045                        counter: reserved_counter,
3046                        wire_buf,
3047                        fsp_seal: None,
3048                        socket,
3049                        dest_addr: socket_addr,
3050                        #[cfg(any(target_os = "linux", target_os = "macos"))]
3051                        connected_socket,
3052                        drop_on_backpressure: fmp_plaintext_is_bulk_session_datagram(plaintext),
3053                        scheduling_weight,
3054                        queued_at: crate::perf_profile::stamp(),
3055                    });
3056                    return Ok(());
3057                }
3058            }
3059        }
3060
3061        // Inline (legacy) path: encrypt + send on the rx_loop.
3062        // Build the inner plaintext lazily here — the worker path
3063        // above never reaches this point, so the prepend_inner_header
3064        // alloc is avoided in the fast path.
3065        let inner_plaintext = prepend_inner_header(timestamp_ms, plaintext);
3066        // Encrypt with AAD binding to the outer header
3067        let ciphertext = {
3068            let _t = crate::perf_profile::Timer::start(crate::perf_profile::Stage::FmpEncrypt);
3069            session
3070                .encrypt_with_aad(&inner_plaintext, &header)
3071                .map_err(|e| NodeError::SendFailed {
3072                    node_addr: *node_addr,
3073                    reason: format!("encryption failed: {}", e),
3074                })?
3075        };
3076
3077        let wire_packet = build_encrypted(&header, &ciphertext);
3078
3079        // Re-borrow peer for stats update after sending
3080        let send_result = {
3081            let _t = crate::perf_profile::Timer::start(crate::perf_profile::Stage::UdpSend);
3082            let transport = self
3083                .transports
3084                .get(&transport_id)
3085                .ok_or(NodeError::TransportNotFound(transport_id))?;
3086            transport.send(&remote_addr, &wire_packet).await
3087        };
3088        self.note_local_send_outcome(&send_result);
3089        let bytes_sent = send_result.map_err(|e| match e {
3090            TransportError::MtuExceeded { packet_size, mtu } => NodeError::MtuExceeded {
3091                node_addr: *node_addr,
3092                packet_size,
3093                mtu,
3094            },
3095            other => NodeError::SendFailed {
3096                node_addr: *node_addr,
3097                reason: format!("transport send: {}", other),
3098            },
3099        })?;
3100
3101        // Update send statistics
3102        if let Some(peer) = self.peers.get_mut(node_addr) {
3103            peer.link_stats_mut().record_sent(bytes_sent);
3104            // MMP: record sent frame for sender report generation
3105            if let Some(mmp) = peer.mmp_mut() {
3106                mmp.sender.record_sent(counter, timestamp_ms, bytes_sent);
3107            }
3108        }
3109
3110        Ok(())
3111    }
3112}
3113
3114impl fmt::Debug for Node {
3115    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3116        f.debug_struct("Node")
3117            .field("node_addr", self.node_addr())
3118            .field("state", &self.state)
3119            .field("is_leaf_only", &self.is_leaf_only)
3120            .field("connections", &self.connection_count())
3121            .field("peers", &self.peer_count())
3122            .field("links", &self.link_count())
3123            .field("transports", &self.transport_count())
3124            .finish()
3125    }
3126}