NAT traversal has three layers, each solving a different piece of the puzzle.
STUN — discovering your public face
A STUN server (RFC 5389) sits on the open internet. You send it a packet; it replies with the public IP and port your NAT assigned to that packet. Now you know your server-reflexive candidate: the address the outside world sees for you. The trick is that your NAT, having opened that port to contact STUN, will now forward incoming packets from the same remote address — a property called endpoint-dependent filtering that most consumer NATs satisfy.
ICE — finding the best path
Interactive Connectivity Establishment (RFC 8445) is the algorithm that orchestrates everything. Both peers collect candidate addresses: their local LAN address, their STUN-discovered public address, and (if configured) a TURN relay address. They exchange these candidates through a signaling channel (anything out-of-band: a WebSocket, a server, even a phone call). Then ICE runs connectivity checks on every pair — it sends small STUN binding requests from each candidate to the peer's candidates and waits for replies. The pair that succeeds with the lowest priority score wins.
Hole punching — why it works (and when it doesn't)
When peer A sends a packet to peer B's public endpoint, A's NAT opens an outbound mapping. Simultaneously peer B does the same toward A. If both NATs use cone NAT semantics, B's packet arrives just as A's mapping is fresh — the hole is punched. The window is typically a few hundred milliseconds, which is why ICE fires both checks almost simultaneously.
The technique breaks under symmetric NAT, where the NAT assigns a different outbound port for each distinct destination. A's STUN-discovered address is only valid for packets going to the STUN server — peer B sees a completely different port. Here TURN is the only fallback.
TURN — the relay of last resort
A TURN server (RFC 5766) acts as a full relay: both peers connect to it, and all media flows through it. Latency rises and bandwidth costs increase, but the connection is guaranteed. Modern ICE implementations try direct paths first and fall back to TURN only when every direct candidate fails — which is why most WebRTC calls succeed without a relay even on corporate networks.
The overall success rate for direct hole punching across the open internet sits around 85–90 % — the remaining cases rely on TURN. Applications like maximum flow routing or distributed systems use similar "find any working path" strategies, but few are as cleverly adaptive as ICE.
Comments
Loading comments...