NAT Traversal Techniques
ICE uses three main techniques to traverse NAT:1
STUN - Server Reflexive Candidates
Discover your public IP and port by querying a STUN server. Works through most NATs but fails with symmetric NAT.
2
TURN - Relay Candidates
Relay all traffic through a TURN server. Works in all NAT scenarios but adds latency and server costs.
3
Address Rewriting
Map known private addresses to public addresses for 1:1 NAT scenarios like AWS EC2 or static NAT.
STUN for Server Reflexive Candidates
STUN (Session Traversal Utilities for NAT) helps discover your public address:How STUN Works
Fromgather.go:734:
STUN Gather Timeout
Configure how long to wait for STUN responses:Increase the timeout if STUN servers are slow to respond or network conditions are poor.
TURN for Relay Candidates
TURN (Traversal Using Relays around NAT) provides relay candidates that work through any NAT:TURN Transport Protocols
TURN supports multiple transport protocols:- UDP
- TCP
- TLS
- DTLS
TURN Allocation Process
Fromgather.go:826:
TLS Certificate Verification
Skip certificate verification for self-signed certificates (development only):Address Rewrite Rules
Address rewrite rules map local private addresses to known public addresses for 1:1 NAT scenarios:Basic Host Rewriting
10.0.1.100 with 203.0.113.10.
Server Reflexive Rewriting
Rewrite Modes
AddressRewriteReplace
AddressRewriteReplace
Replaces the original candidate with the external address(es).Default for host candidates.
AddressRewriteAppend
AddressRewriteAppend
Keeps the original candidate and adds the external address(es).Default for srflx and relay candidates.
Rule Scoping
Limit rules to specific interfaces, CIDR blocks, or network types:Rule Precedence
Fromagent_options.go:36:
Rules are evaluated in order with the following precedence:
- Explicit local match - Rules with
Localfield matching the candidate address - Most specific catch-all - Rules without
Localbut with the most specific scope:- Interface + CIDR (highest)
- Interface only
- CIDR only
- Global (lowest)
Complete NAT Traversal Examples
AWS EC2 Instance
Map EC2 private IP to public IP:Multi-Homed Server
Map multiple network interfaces:STUN + Address Rewriting
Combine STUN with rewrite rules:NAT Type Detection
While Pion ICE doesn’t include built-in NAT type detection, you can infer NAT behavior:Troubleshooting
No Server Reflexive Candidates
- Verify STUN server URLs are correct
- Check firewall allows UDP to STUN port (usually 3478 or 19302)
- Increase STUN gather timeout
- Try multiple STUN servers
TURN Allocation Failures
- Verify TURN credentials are correct
- Check TURN server allows your source IP
- Ensure required transport protocol is supported
- Check firewall allows connections to TURN port
Address Rewriting Not Working
- Verify local address matches exactly
- Check rule precedence (more specific rules first)
- Enable debug logging to see which rules match
- Verify external IPs are valid and reachable
Symmetric NAT Issues
- Use TURN relay candidates
- Try multiple STUN servers to detect symmetric NAT
- Consider deploying TURN servers closer to users
Next Steps
Configuration
Configure ICE agent options
Gathering
Learn about candidate gathering
Multiplexing
Share ports with UDPMux and TCPMux
Examples
See NAT traversal examples