How STUN Assists UDP Applications in NAT Traversal

Session Traversal Utilities for NAT (STUN) is a standardized network protocol designed to help UDP-based applications discover their public network presence and establish direct peer-to-peer connections through Network Address Translators (NATs). This article explores the NAT challenges faced by real-time UDP applications—such as VoIP, video conferencing, and online gaming—and explains the step-by-step mechanism STUN uses to discover public endpoints, classify NAT behavior, and enable direct media streaming.

The NAT Challenge for UDP Applications

Most consumer and enterprise devices reside in private local area networks (LANs) using private IPv4 addresses. When these devices communicate with the internet, a NAT router modifies the IP packet headers, mapping private internal IP addresses and port numbers to a public IP and port.

While NAT conserves IPv4 addresses and provides security, it presents a major obstacle for bidirectional, peer-to-peer UDP applications:

How STUN Solves the Problem

STUN solves these issues by operating as a lightweight client-server protocol (defined in RFC 5389 and RFC 8489) running over UDP.

1. Public Address Discovery (Server-Reflexive Candidate)

The primary function of STUN is reflexive address discovery: * A STUN client located behind a NAT sends a Binding Request over UDP to a STUN server located on the public internet. * As the packet passes through the NAT, the router assigns a public IP address and port to the outgoing UDP flow. * When the STUN server receives the packet, it inspects the source IP and source port in the UDP/IP header. * The STUN server embeds this public information into a Binding Response attribute (known as XOR-MAPPED-ADDRESS) and sends it back to the client. * Upon parsing the response, the client learns its public-facing IP address and port mapping.

2. NAT Behavior and Mapping Characterization

STUN helps applications understand the specific behavior of the NAT device managing their traffic. NAT mapping behaviors typically fall into categories such as: * Endpoint-Independent Mapping (Full Cone): The NAT reuses the same public mapping for all outbound packets from the same internal IP and port, regardless of the destination. * Address-Restricted / Port-Restricted Mapping: The NAT only allows incoming packets from an external endpoint if the internal client has previously sent a packet to that specific external IP address (or IP and port). * Address- and Port-Dependent Mapping (Symmetric NAT): The NAT creates a unique public mapping for every distinct external destination IP and port combination.

By testing connectivity against multiple STUN IP addresses and ports, an application can determine whether direct hole punching will be possible.

Enabling Peer-to-Peer Communication

Once the UDP application obtains its server-reflexive address via STUN, it transmits this endpoint information to the remote peer using an out-of-band signaling channel (such as WebSocket, SIP, or HTTP).

Both peers then simultaneously transmit UDP packets directly to each other’s public IP and port combinations. This process, known as UDP hole punching, creates temporary translation state entries in both NAT routers, allowing subsequent bidirectional UDP media packets (such as SRTP audio/video streams) to flow directly between peers without routing through an intermediate server.

Limitations and the ICE Framework

STUN is lightweight, requires minimal server bandwidth, and introduces negligible latency. However, it cannot facilitate direct connections when one or both endpoints reside behind a Symmetric NAT, because the public port mapping changes when the client attempts to communicate with the peer instead of the STUN server.

To handle these scenarios, STUN is deployed as a foundational component within the Interactive Connectivity Establishment (ICE) framework. ICE uses STUN to discover direct and reflexive paths, and if STUN-assisted direct connection fails, ICE automatically falls back to Traversal Using Relays around NAT (TURN) to relay the UDP traffic.