Skip to main content

fips_core/node/
session_access_impl.rs

1use super::*;
2
3impl Node {
4    // === End-to-End Sessions ===
5
6    /// Get a session by remote NodeAddr.
7    /// Disable the discovery forward rate limiter (for tests).
8    #[cfg(test)]
9    pub(crate) fn disable_discovery_forward_rate_limit(&mut self) {
10        self.discovery_forward_limiter
11            .set_interval(std::time::Duration::ZERO);
12    }
13
14    #[cfg(test)]
15    pub(crate) fn get_session(&self, remote: &NodeAddr) -> Option<&SessionEntry> {
16        self.sessions.get(remote)
17    }
18
19    /// Get a mutable session by remote NodeAddr.
20    #[cfg(test)]
21    pub(crate) fn get_session_mut(&mut self, remote: &NodeAddr) -> Option<&mut SessionEntry> {
22        self.sessions.get_mut(remote)
23    }
24
25    /// Remove a session.
26    #[cfg(test)]
27    pub(crate) fn remove_session(&mut self, remote: &NodeAddr) -> Option<SessionEntry> {
28        self.sessions.remove(remote)
29    }
30
31    /// Read the path_mtu_lookup entry for a destination FipsAddress.
32    #[cfg(test)]
33    pub(crate) fn path_mtu_lookup_get(&self, fips_addr: &crate::FipsAddress) -> Option<u16> {
34        self.path_mtu_lookup
35            .read()
36            .ok()
37            .and_then(|map| map.get(fips_addr).copied())
38    }
39
40    /// Write a path_mtu_lookup entry directly (for tests that pre-seed the map).
41    #[cfg(test)]
42    pub(crate) fn path_mtu_lookup_insert(&self, fips_addr: crate::FipsAddress, mtu: u16) {
43        if let Ok(mut map) = self.path_mtu_lookup.write() {
44            map.insert(fips_addr, mtu);
45        }
46    }
47
48    /// Number of end-to-end sessions.
49    pub fn session_count(&self) -> usize {
50        self.sessions.len()
51    }
52
53    /// Iterate over all session entries (for control queries).
54    pub(crate) fn session_entries(&self) -> impl Iterator<Item = (&NodeAddr, &SessionEntry)> {
55        self.sessions.iter()
56    }
57
58    // === Identity Cache ===
59
60    /// Register a node in the identity cache for FipsAddress → NodeAddr lookup.
61    pub(crate) fn register_identity(
62        &mut self,
63        node_addr: NodeAddr,
64        pubkey: secp256k1::PublicKey,
65    ) -> bool {
66        // Endpoint sends pass the same PeerIdentity on every packet. Once
67        // validated, avoid re-deriving NodeAddr from the public key in the
68        // data path; that hash showed up in macOS sender profiles.
69        self.identity_cache.register(
70            node_addr,
71            pubkey,
72            Self::now_ms(),
73            self.config.node.cache.identity_size,
74        )
75    }
76
77    /// Look up a destination by FipsAddress prefix (bytes 1-15 of the IPv6 address).
78    pub(crate) fn lookup_by_fips_prefix(
79        &mut self,
80        prefix: &[u8; 15],
81    ) -> Option<(NodeAddr, secp256k1::PublicKey)> {
82        self.identity_cache.lookup_by_prefix(prefix, Self::now_ms())
83    }
84
85    /// Check if a node's identity is in the cache (without LRU touch).
86    pub(crate) fn has_cached_identity(&self, addr: &NodeAddr) -> bool {
87        self.identity_cache.has_prefix_for(addr)
88    }
89
90    /// Number of identity cache entries.
91    pub fn identity_cache_len(&self) -> usize {
92        self.identity_cache.len()
93    }
94
95    /// Iterate over identity cache entries.
96    ///
97    /// Returns `(NodeAddr, PublicKey, last_seen_ms)` for each cached identity.
98    /// Used by the `show_identity_cache` control query.
99    pub fn identity_cache_iter(
100        &self,
101    ) -> impl Iterator<Item = (&NodeAddr, &secp256k1::PublicKey, u64)> {
102        self.identity_cache.iter()
103    }
104
105    /// Configured maximum identity cache size.
106    pub fn identity_cache_max(&self) -> usize {
107        self.config.node.cache.identity_size
108    }
109
110    /// Number of pending discovery lookups.
111    pub fn pending_lookup_count(&self) -> usize {
112        self.pending_lookups.len()
113    }
114
115    /// Iterate over pending discovery lookups for diagnostics.
116    pub fn pending_lookups_iter(
117        &self,
118    ) -> impl Iterator<Item = (&NodeAddr, &handlers::discovery::PendingLookup)> {
119        self.pending_lookups.iter()
120    }
121
122    /// Number of recent discovery requests tracked.
123    pub fn recent_request_count(&self) -> usize {
124        self.recent_requests.len()
125    }
126
127    /// Count of destinations with queued TUN packets awaiting session setup.
128    pub fn pending_tun_destinations(&self) -> usize {
129        self.pending_session_traffic.tun_destination_count()
130    }
131
132    /// Total TUN packets queued across all destinations.
133    pub fn pending_tun_total_packets(&self) -> usize {
134        self.pending_session_traffic.tun_packet_count()
135    }
136
137    /// Iterate over retry state for diagnostics.
138    pub fn retry_state_iter(&self) -> impl Iterator<Item = (&NodeAddr, &retry::RetryState)> {
139        self.retry_pending.iter()
140    }
141}