spt-nat
NAT traversal library -- STUN, TURN, UPnP, NAT-PMP, hole punching, and relay management
Overview
spt-nat provides a comprehensive NAT traversal toolkit for establishing peer-to-peer connections across network address translators. It orchestrates multiple traversal strategies (STUN, TURN, UPnP, NAT-PMP, UDP hole punching) through a unified NatTraversalManager that automatically detects NAT type and selects the best strategy. This is the connectivity foundation for spt-p2p.
Key Features
- STUN client/server -- NAT type detection and external address discovery
- TURN client/server -- relay allocation for symmetric NAT fallback
- UPnP -- automatic port mapping via UPnP gateway discovery
- NAT-PMP -- port mapping via NAT-PMP/PCP protocol
- Hole punching -- coordinated UDP hole punching with configurable timeout
- Relay management -- connection relay with configurable pool limits
Dependencies
No internal SPT crate dependencies. Uses tokio for async I/O, dashmap for concurrent port mapping storage, and parking_lot for low-contention locks.
Usage
rust
use spt_nat::{NatTraversalManager, NatTraversalConfig};
let config = NatTraversalConfig::default();
let manager = NatTraversalManager::new(config);
// manager.initialize().await?; // runs detection + gateway discoveryAPI Reference
Core Types
| Type | Description |
|---|---|
NatTraversalManager | Main orchestrator for all traversal strategies |
NatTraversalConfig | Configuration for enabled strategies and timeouts |
StunClient | STUN binding request client |
StunServer | STUN server implementation |
TurnClient | TURN relay allocation client |
TurnServer | TURN server implementation |
TurnAllocation | Active TURN relay allocation |
UpnpManager | UPnP IGD port mapping manager |
NatPmpClient | NAT-PMP/PCP client |
HolePunchCoordinator | Coordinated UDP hole punching |
RelayManager | Connection relay pool manager |
NatInfo | Detected NAT type and external address |
NatType | NAT classification enum |
NatError | Crate error type |