1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2024 Saorsa Labs Ltd.
//
// This Saorsa Network Software is licensed under the General Public License (GPL), version 3.
// Please see the file LICENSE-GPL, or visit <http://www.gnu.org/licenses/> for the full text.
//
// Full details available at https://saorsalabs.com/licenses
//! Consolidated node status for observability
//!
//! This module provides [`NodeStatus`] - a single snapshot of everything
//! about a node's current state, including NAT type, connectivity,
//! relay status, and performance metrics.
//!
//! # Example
//!
//! ```rust,ignore
//! use ant_quic::Node;
//!
//! let node = Node::new().await?;
//! let status = node.status();
//!
//! println!("NAT type: {:?}", status.nat_type);
//! println!("Can receive direct: {}", status.can_receive_direct);
//! println!("Acting as relay: {}", status.is_relaying);
//! println!("Relay sessions: {}", status.relay_sessions);
//! ```
use std::net::SocketAddr;
use std::time::Duration;
use crate::nat_traversal_api::PeerId;
/// Detected NAT type for the node
///
/// NAT type affects connectivity - some types are easier to traverse than others.
/// The node automatically detects its NAT type and adjusts traversal strategies.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum NatType {
/// No NAT detected - direct public connectivity
///
/// The node has a public IP address and can accept connections directly.
None,
/// Full cone NAT - easiest to traverse
///
/// Any external host can send packets to the internal IP:port once
/// the internal host has sent a packet to any external host.
FullCone,
/// Address-restricted cone NAT
///
/// External hosts can send packets only if the internal host
/// has previously sent to that specific external IP.
AddressRestricted,
/// Port-restricted cone NAT
///
/// External hosts can send packets only if the internal host
/// has previously sent to that specific external IP:port.
PortRestricted,
/// Symmetric NAT - hardest to traverse
///
/// Each outgoing connection gets a different external port.
/// Requires prediction algorithms or relay fallback.
Symmetric,
/// NAT type not yet determined
///
/// The node hasn't completed NAT detection yet.
#[default]
Unknown,
}
impl std::fmt::Display for NatType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::None => write!(f, "None (Public IP)"),
Self::FullCone => write!(f, "Full Cone"),
Self::AddressRestricted => write!(f, "Address Restricted"),
Self::PortRestricted => write!(f, "Port Restricted"),
Self::Symmetric => write!(f, "Symmetric"),
Self::Unknown => write!(f, "Unknown"),
}
}
}
/// Comprehensive node status snapshot
///
/// This struct provides a complete view of the node's current state,
/// including identity, connectivity, NAT status, relay status, and performance.
///
/// # Status Categories
///
/// - **Identity**: peer_id, local_addr, external_addrs
/// - **NAT Status**: nat_type, can_receive_direct, has_public_ip
/// - **Connections**: connected_peers, active_connections, pending_connections
/// - **NAT Traversal**: direct_connections, relayed_connections, hole_punch_success_rate
/// - **Relay**: is_relaying, relay_sessions, relay_bytes_forwarded
/// - **Coordinator**: is_coordinating, coordination_sessions
/// - **Performance**: avg_rtt, uptime
#[derive(Debug, Clone)]
pub struct NodeStatus {
// --- Identity ---
/// This node's peer ID (derived from public key)
pub peer_id: PeerId,
/// Local bind address
pub local_addr: SocketAddr,
/// All discovered external addresses
///
/// These are addresses as seen by other peers. Multiple addresses
/// may be discovered when behind NAT or with multiple interfaces.
pub external_addrs: Vec<SocketAddr>,
// --- NAT Status ---
/// Detected NAT type
pub nat_type: NatType,
/// Whether this node can receive direct connections
///
/// `true` if the node has a public IP or is behind a traversable NAT.
pub can_receive_direct: bool,
/// Whether this node has a public IP
///
/// `true` if local_addr matches an external_addr (no NAT).
pub has_public_ip: bool,
// --- Connections ---
/// Number of connected peers
pub connected_peers: usize,
/// Number of active connections (may differ from peers if multiplexed)
pub active_connections: usize,
/// Number of pending connection attempts
pub pending_connections: usize,
// --- NAT Traversal Stats ---
/// Total successful direct connections (no relay)
pub direct_connections: u64,
/// Total connections that required relay
pub relayed_connections: u64,
/// Hole punch success rate (0.0 - 1.0)
///
/// Calculated from NAT traversal attempts vs successes.
pub hole_punch_success_rate: f64,
// --- Relay Status (NEW - key visibility) ---
/// Whether this node is currently acting as a relay for others
///
/// `true` if this node has public connectivity and is forwarding
/// traffic for peers behind restrictive NATs.
pub is_relaying: bool,
/// Number of active relay sessions
pub relay_sessions: usize,
/// Total bytes forwarded as relay
pub relay_bytes_forwarded: u64,
// --- Coordinator Status (NEW - key visibility) ---
/// Whether this node is coordinating NAT traversal
///
/// `true` if this node is helping peers coordinate hole punching.
/// All nodes with public connectivity act as coordinators.
pub is_coordinating: bool,
/// Number of active coordination sessions
pub coordination_sessions: usize,
// --- Performance ---
/// Average round-trip time across all connections
pub avg_rtt: Duration,
/// Time since node started
pub uptime: Duration,
}
impl Default for NodeStatus {
fn default() -> Self {
Self {
peer_id: PeerId([0u8; 32]),
local_addr: "0.0.0.0:0".parse().unwrap_or_else(|_| {
SocketAddr::new(std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED), 0)
}),
external_addrs: Vec::new(),
nat_type: NatType::Unknown,
can_receive_direct: false,
has_public_ip: false,
connected_peers: 0,
active_connections: 0,
pending_connections: 0,
direct_connections: 0,
relayed_connections: 0,
hole_punch_success_rate: 0.0,
is_relaying: false,
relay_sessions: 0,
relay_bytes_forwarded: 0,
is_coordinating: false,
coordination_sessions: 0,
avg_rtt: Duration::ZERO,
uptime: Duration::ZERO,
}
}
}
impl NodeStatus {
/// Check if node has any connectivity
pub fn is_connected(&self) -> bool {
self.connected_peers > 0
}
/// Check if node can help with NAT traversal
///
/// Returns true if the node has public connectivity and can
/// act as coordinator/relay for other peers.
pub fn can_help_traversal(&self) -> bool {
self.has_public_ip || self.can_receive_direct
}
/// Get the total number of connections (direct + relayed)
pub fn total_connections(&self) -> u64 {
self.direct_connections + self.relayed_connections
}
/// Get the direct connection rate (0.0 - 1.0)
///
/// Higher is better - indicates more direct connections vs relayed.
pub fn direct_rate(&self) -> f64 {
let total = self.total_connections();
if total == 0 {
0.0
} else {
self.direct_connections as f64 / total as f64
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_nat_type_display() {
assert_eq!(format!("{}", NatType::None), "None (Public IP)");
assert_eq!(format!("{}", NatType::FullCone), "Full Cone");
assert_eq!(
format!("{}", NatType::AddressRestricted),
"Address Restricted"
);
assert_eq!(format!("{}", NatType::PortRestricted), "Port Restricted");
assert_eq!(format!("{}", NatType::Symmetric), "Symmetric");
assert_eq!(format!("{}", NatType::Unknown), "Unknown");
}
#[test]
fn test_nat_type_default() {
assert_eq!(NatType::default(), NatType::Unknown);
}
#[test]
fn test_node_status_default() {
let status = NodeStatus::default();
assert_eq!(status.nat_type, NatType::Unknown);
assert!(!status.can_receive_direct);
assert!(!status.has_public_ip);
assert_eq!(status.connected_peers, 0);
assert!(!status.is_relaying);
assert!(!status.is_coordinating);
}
#[test]
fn test_is_connected() {
let mut status = NodeStatus::default();
assert!(!status.is_connected());
status.connected_peers = 1;
assert!(status.is_connected());
}
#[test]
fn test_can_help_traversal() {
let mut status = NodeStatus::default();
assert!(!status.can_help_traversal());
status.has_public_ip = true;
assert!(status.can_help_traversal());
status.has_public_ip = false;
status.can_receive_direct = true;
assert!(status.can_help_traversal());
}
#[test]
fn test_total_connections() {
let mut status = NodeStatus::default();
status.direct_connections = 5;
status.relayed_connections = 3;
assert_eq!(status.total_connections(), 8);
}
#[test]
fn test_direct_rate() {
let mut status = NodeStatus::default();
assert_eq!(status.direct_rate(), 0.0);
status.direct_connections = 8;
status.relayed_connections = 2;
assert!((status.direct_rate() - 0.8).abs() < 0.001);
}
#[test]
fn test_status_is_debug() {
let status = NodeStatus::default();
let debug_str = format!("{:?}", status);
assert!(debug_str.contains("NodeStatus"));
assert!(debug_str.contains("nat_type"));
assert!(debug_str.contains("is_relaying"));
}
#[test]
fn test_status_is_clone() {
let mut status = NodeStatus::default();
status.connected_peers = 5;
status.is_relaying = true;
let cloned = status.clone();
assert_eq!(status.connected_peers, cloned.connected_peers);
assert_eq!(status.is_relaying, cloned.is_relaying);
}
#[test]
fn test_nat_type_equality() {
assert_eq!(NatType::FullCone, NatType::FullCone);
assert_ne!(NatType::FullCone, NatType::Symmetric);
}
#[test]
fn test_status_with_relay() {
let mut status = NodeStatus::default();
status.is_relaying = true;
status.relay_sessions = 3;
status.relay_bytes_forwarded = 1024 * 1024; // 1 MB
assert!(status.is_relaying);
assert_eq!(status.relay_sessions, 3);
assert_eq!(status.relay_bytes_forwarded, 1024 * 1024);
}
#[test]
fn test_status_with_coordinator() {
let mut status = NodeStatus::default();
status.is_coordinating = true;
status.coordination_sessions = 5;
assert!(status.is_coordinating);
assert_eq!(status.coordination_sessions, 5);
}
#[test]
fn test_external_addrs() {
let mut status = NodeStatus::default();
let addr1: SocketAddr = "1.2.3.4:9000".parse().unwrap();
let addr2: SocketAddr = "5.6.7.8:9001".parse().unwrap();
status.external_addrs.push(addr1);
status.external_addrs.push(addr2);
assert_eq!(status.external_addrs.len(), 2);
assert!(status.external_addrs.contains(&addr1));
assert!(status.external_addrs.contains(&addr2));
}
}