Table of Contents
- NAT Traversal Testing Guide
- Prerequisites
- Build
- Quick Start (2 Servers)
- Port Usage
- Testing Scenarios
- Scenario 1: Both Servers Have Public IPs (No NAT)
- Scenario 2: One Peer Behind NAT (Home Router)
- Scenario 3: Both Peers Behind NAT
- Scenario 4: Three-Node DHT Network
- Troubleshooting
- "Waiting for peer publication..." hangs
- "Hole punch failed" / timeout after 30s
- Wrong address published
- Connection established but no hole punch
- "DHT Node ID not found"
- Verifying the Connection
- How It Works (Summary)
NAT Traversal Testing Guide
How to test NAT traversal across real servers using the MWP demo.
Prerequisites
- Two or more servers with public IPs (or behind different NATs)
- Zig 0.15+ installed on each, or cross-compile from your machine
- Firewall rules allowing:
- TCP on your chosen port (P2P messaging + DHT)
- UDP on port + 1 (MWP hole punch packets)
Build
Build on each server directly:
zig build -Doptimize=ReleaseFast
Or cross-compile and copy the binary:
# For Linux x86_64
zig build -Doptimize=ReleaseFast -Dtarget=x86_64-linux
# For Linux aarch64
zig build -Doptimize=ReleaseFast -Dtarget=aarch64-linux
scp zig-out/bin/mwp-demo user@server:/path/
Quick Start (2 Servers)
Server A (Bootstrap + Alice)
./mwp-demo --name alice --port 9000 --deterministic --connect-to bob
Output will show:
=== MWP Demo ===
Name: alice
Port: 9000
DHT Node ID: a1b2c3d4... <-- copy this
MWP Public Key: e5f6a7b8...
Listening on 0.0.0.0:9000
=== Address Discovery ===
Trying NAT-PMP...
Trying UPnP...
Using fallback address: 127.0.0.1:9001 <-- will be wrong, see below
Since this is the first node (no DHT peers for IP reflection), it will fall back to 127.0.0.1. Use --public-ip to tell it the real address:
./mwp-demo --name alice --port 9000 --deterministic --connect-to bob \
--public-ip <SERVER_A_PUBLIC_IP>
Server B (Bob connects to Alice)
./mwp-demo --name bob --port 9000 --deterministic --connect-to alice \
--bootstrap <SERVER_A_IP>:9000 \
--dht-id <ALICE_DHT_NODE_ID> \
--public-ip <SERVER_B_PUBLIC_IP>
Bob will:
- Connect to Alice via TCP on port 9000
- Register Alice's DHT node ID for routing
- Use IP reflection from Alice's DHT response to discover its own address (or use
--public-ip) - Publish its address to the DHT
- Find Alice's publication in the DHT
- Compute symmetric rendezvous time from both nonces
- Perform UDP hole punch on port 9001 (port + 1)
Expected Output (Success)
=== MWP Connection ===
Publishing our address to DHT...
Waiting for peer publication...
Found peer publication!
Peer addresses: [203.0.113.10:9001]
Peer nonce: f1e2d3c4...
Computing rendezvous...
Rendezvous in 2847ms
Hole punching to 203.0.113.10:9001...
Sending hole punch packet (attempt 1)...
Sending hole punch packet (attempt 2)...
Received valid hole punch from peer!
SUCCESS: Connected to peer
Remote address: 203.0.113.10:9001
Establishment time: 3215ms
Port Usage
| Port | Protocol | Purpose |
|---|---|---|
--port (e.g. 9000) |
TCP | P2P connections, DHT messages |
--port + 1 (e.g. 9001) |
UDP | MWP hole punch packets |
Both must be reachable. If either server is behind a firewall:
# Example: ufw
sudo ufw allow 9000/tcp
sudo ufw allow 9001/udp
# Example: iptables
sudo iptables -A INPUT -p tcp --dport 9000 -j ACCEPT
sudo iptables -A INPUT -p udp --dport 9001 -j ACCEPT
Testing Scenarios
Scenario 1: Both Servers Have Public IPs (No NAT)
Simplest case. Both peers are directly reachable.
# Server A (203.0.113.10)
./mwp-demo --name alice --port 9000 --deterministic --connect-to bob \
--public-ip 203.0.113.10
# Server B (198.51.100.20)
./mwp-demo --name bob --port 9000 --deterministic --connect-to alice \
--bootstrap 203.0.113.10:9000 --dht-id <ALICE_DHT_ID> \
--public-ip 198.51.100.20
Expected: Direct UDP hole punch succeeds immediately.
Scenario 2: One Peer Behind NAT (Home Router)
Server A has a public IP. Server B is behind a home router.
# Server A (public, 203.0.113.10)
./mwp-demo --name alice --port 9000 --deterministic --connect-to bob \
--public-ip 203.0.113.10
# Server B (behind NAT, router supports NAT-PMP or UPnP)
./mwp-demo --name bob --port 9000 --deterministic --connect-to alice \
--bootstrap 203.0.113.10:9000 --dht-id <ALICE_DHT_ID>
The demo will automatically try:
- NAT-PMP - requests port mapping from the gateway
- UPnP - discovers IGD device and requests mapping via SOAP
- IP Reflection - uses Alice's DHT response to learn the NAT'd address
If NAT-PMP/UPnP succeed, Bob's external address is discovered automatically.
If neither works, IP reflection from Alice's DHT response gives Bob's observed IP:port.
Scenario 3: Both Peers Behind NAT
This is the hard case - where MWP earns its keep.
# Peer A (behind NAT A, knows a bootstrap server at 203.0.113.10:9000)
./mwp-demo --name alice --port 9000 --deterministic --connect-to bob \
--bootstrap 203.0.113.10:9000 --dht-id <BOOTSTRAP_DHT_ID>
# Peer B (behind NAT B, same bootstrap)
./mwp-demo --name bob --port 9000 --deterministic --connect-to alice \
--bootstrap 203.0.113.10:9000 --dht-id <BOOTSTRAP_DHT_ID>
Both peers discover their external addresses via IP reflection from the bootstrap node, then publish to the DHT. The hole punch works because:
- Both peers bind to
0.0.0.0:port+1(creates a NAT mapping) - They learn each other's external
IP:portfrom DHT publications - They both send UDP packets to each other's external address at the rendezvous time
- The outgoing packets create NAT pinholes that allow the other peer's packets through
NAT type compatibility:
| NAT A \ NAT B | Full Cone | Restricted | Port Restricted | Symmetric |
|---|---|---|---|---|
| Full Cone | Yes | Yes | Yes | Yes |
| Restricted | Yes | Yes | Yes | Maybe |
| Port Restricted | Yes | Yes | Maybe | No |
| Symmetric | Yes | Maybe | No | No |
Scenario 4: Three-Node DHT Network
For a more realistic setup, run a dedicated bootstrap node:
# Bootstrap node (always-on server, 203.0.113.10)
./mwp-demo --name bootstrap --port 9000 --public-ip 203.0.113.10
# Peer A (any network)
./mwp-demo --name alice --port 9000 --deterministic --connect-to bob \
--bootstrap 203.0.113.10:9000 --dht-id <BOOTSTRAP_DHT_ID>
# Peer B (any network)
./mwp-demo --name bob --port 9000 --deterministic --connect-to alice \
--bootstrap 203.0.113.10:9000 --dht-id <BOOTSTRAP_DHT_ID>
The bootstrap node doesn't need --connect-to anyone. It just runs the DHT.
Alice and Bob both join via bootstrap, discover each other through DHT publications, and hole punch directly.
Troubleshooting
"Waiting for peer publication..." hangs
- The other peer hasn't started yet, or hasn't published to the DHT
- Check that
--dht-idmatches the bootstrap node's actual DHT Node ID - Check that both peers use matching
--connect-to/--namepairs (deterministic keys are derived from the name)
"Hole punch failed" / timeout after 30s
- Firewall blocking UDP on port+1
- Both peers behind symmetric NAT (won't work without TURN relay)
- Check with
sudo tcpdump -i any udp port 9001to see if packets arrive
Wrong address published
- First node has no DHT peers, so IP reflection can't work. Always use
--public-ipfor the bootstrap node - NAT-PMP/UPnP may return internal gateway IP if misconfigured
- Check the "Address Discovery" output to see which method was used
Connection established but no hole punch
- TCP connection on port 9000 works (that's how DHT messages flow)
- UDP hole punch uses port 9001 separately
- These are independent: TCP succeeding doesn't mean UDP will
"DHT Node ID not found"
- The
--dht-idvalue must be the exact 64-character hex string from the other peer's startup output - Copy-paste carefully, no spaces or newlines
Verifying the Connection
When hole punch succeeds, the demo prints:
SUCCESS: Connected to peer
Remote address: <IP>:<port>
Establishment time: <ms>ms
To verify the UDP path is truly direct (not relayed), check the remote address:
- Should be the other peer's public IP, not the bootstrap node's IP
- Establishment time under 1 second typically means direct connectivity
- Over 5 seconds may indicate packet loss / retransmission
How It Works (Summary)
Alice DHT (Bootstrap) Bob
| | |
|-- TCP connect ---------->| |
| |<-------- TCP connect ----|
| | |
|-- STORE(alice_pub) ----->| |
| |<--- STORE(bob_pub) ------|
| | |
|-- FIND_VALUE(bob) ------>| |
|<-- bob's addresses ------| |
| |<-- FIND_VALUE(alice) ----|
| |--- alice's addresses --->|
| | |
| [compute rendezvous time from both nonces] |
| | |
|========== UDP hole punch (port+1) =================>|
|<========= UDP hole punch (port+1) ==================|
| | |
| [SUCCESS: direct P2P UDP connection established] |
The key insight: MWP uses only the DHT for signaling. No STUN servers, no TURN relays, no signaling servers. The bootstrap node is just a regular DHT peer.