May 20, 2023

STUN stands for Session Traversal Utilities for NAT, which is a protocol designed to allow devices behind a network address translator (NAT) to discover and communicate with other devices through the Internet. The NAT is a device that translates IP addresses between a private network and the public Internet. The purpose of the STUN protocol is to enable real-time communication between devices, such as voice and video calls or online gaming, which require low latency and high bandwidth.


Before the invention of NAT, all devices on the Internet had a unique public IP address that could be used for communication with other devices. However, as the number of devices on the Internet increased, the limited number of available IP addresses became a problem. NAT was developed as a solution to this problem, by allowing multiple devices to share a single public IP address.

However, NAT also introduced a new problem for real-time communication. When a device behind a NAT wants to initiate communication with another device, it needs to know its public IP address and the port that the device is listening on. However, the NAT device modifies the IP address and port number of outgoing packets, so the recipient device cannot simply reply to the original IP address and port number.

The STUN protocol solves this problem by allowing a device behind a NAT to discover its public IP address and the port number that the NAT has assigned to it. This information can then be shared with other devices, so they can communicate with the device behind the NAT.


The STUN protocol is used in real-time communication applications, such as Voice over IP (VoIP), video conferencing, and online gaming. These applications require low latency and high bandwidth, which can be achieved by using direct connections between devices, rather than routing traffic through a central server.

When a device behind a NAT wants to communicate with another device, it first sends a STUN request to a STUN server. The STUN server responds with a STUN response that contains the public IP address and port number that the NAT device has assigned to the device behind the NAT. The device behind the NAT can then use this information to communicate directly with the other device.

If the STUN protocol is not successful in establishing a direct connection between the devices, other techniques such as TURN (Traversal Using Relays around NAT) or ICE (Interactive Connectivity Establishment) can be used to establish a connection through a relay server or a combination of direct and relayed connections.

STUN Protocol Components

The STUN protocol consists of three main components: the STUN client, the STUN server, and the STUN message format.

STUN Client

The STUN client is a software component that runs on a device behind a NAT and wants to establish a direct connection with another device. The STUN client sends STUN requests to a STUN server and receives STUN responses from the server. The client also sends the public IP address and port number that it has learned from the STUN server to other devices that it wants to communicate with.

STUN Server

The STUN server is a software component that runs on a server on the public Internet. The server listens for STUN requests from STUN clients and responds with STUN responses that contain the public IP address and port number of the client. The server may also use other techniques, such as TURN or ICE, to establish a direct or relayed connection between clients that cannot establish a direct connection.

STUN Message Format

The STUN message format defines the structure of STUN requests and responses. A STUN message consists of a STUN header and one or more STUN attributes.

The STUN header contains a message type, a message length, a transaction ID, and a magic cookie. The message type indicates whether the message is a request or a response, and what type of request or response it is. The message length indicates the length of the message body. The transaction ID is a unique identifier that is used to match requests and responses. The magic cookie is a fixed value that identifies the message as a STUN message.

The STUN attributes contain additional information that is sent along with the request or response. Some common STUN attributes include the MAPPED-ADDRESS attribute, which contains the public IP address and port number of the client, and the XOR-MAPPED-ADDRESS attribute, which is used to obscure the public IP address and port number for security purposes.

STUN Security

The STUN protocol does not provide any encryption or authentication mechanisms, so it is vulnerable to attacks such as eavesdropping, man-in-the-middle attacks, and denial-of-service attacks. Therefore, it is important to use additional security mechanisms, such as Transport Layer Security (TLS), to protect STUN traffic.

In addition, the STUN protocol can leak information about the network topology, which can be used by attackers to map the network and identify potential targets. Therefore, it is important to configure NAT devices to restrict access to the STUN server and to use secure transport protocols such as TLS.