Traversal Using Relay NAT (TURN)
May 20, 2023
Traversal Using Relay NAT (TURN) is a protocol used in Web Real-Time Communication (WebRTC) to help establish a connection between two devices that are behind Network Address Translation (NAT). NAT is a common technology used in routers that allows multiple devices on a network to access the internet using a single public IP address. However, this technology can make it difficult for devices behind NAT to establish a direct connection with each other, which is required for peer-to-peer communication. TURN is designed to solve this problem by relaying data between the two devices through a third-party server.
Purpose
The purpose of TURN is to provide a way for WebRTC applications to establish a successful connection between devices that are behind NAT. When two devices try to communicate through WebRTC, they exchange network information such as IP addresses and ports. However, in many cases, this information is not enough to establish a direct connection between the devices. This is where TURN comes in. TURN provides a relay server that can be used to relay data between the two devices, allowing them to communicate even if they are behind NAT.
Usage
TURN is used primarily in WebRTC applications to establish a connection between two devices that are behind NAT. When a WebRTC application detects that a direct connection is not possible between two devices, it will attempt to use TURN to relay data between them. This process is transparent to the user and happens automatically in the background.
TURN is also used in other applications that require peer-to-peer communication between devices that are behind NAT. For example, online gaming platforms may use TURN to facilitate communication between players who are behind NAT.
TURN and WebRTC
WebRTC is a technology that enables real-time communication between web browsers and mobile applications. It is commonly used for video and audio communication, as well as for data sharing between devices. WebRTC uses a set of protocols to establish a direct connection between two devices. However, when devices are behind NAT, these protocols may not be enough to establish a direct connection. This is where TURN comes in.
TURN is one of the three protocols used in WebRTC to establish a connection between devices. The other two protocols are Interactive Connectivity Establishment (ICE) and Session Traversal Utilities for NAT (STUN). ICE is used to discover the best possible path for data transmission between devices, while STUN is used to obtain the public IP address of a device. Together, these protocols work to establish a direct connection between two devices. However, when a direct connection is not possible, TURN is used to relay data between the devices.
How TURN Works
TURN works by creating a relay server that sits between two devices that are behind NAT. When two devices attempt to communicate through WebRTC, they exchange network information such as IP addresses and ports. If a direct connection cannot be established, the devices will attempt to use TURN to relay data between them.
To use TURN, the devices must first obtain the address of the TURN server. This address is typically provided by the WebRTC application. Once the devices have the address of the TURN server, they can send data to each other through the server. The server acts as a relay by accepting data from one device and forwarding it to the other device. This allows the two devices to communicate even if they are behind NAT.
TURN can be used in two different modes: relay-only mode and relay-and-turn mode. In relay-only mode, the TURN server acts only as a relay and does not modify the data being transmitted between the two devices. In relay-and-turn mode, the TURN server not only relays data, but also modifies the data to optimize it for transmission over the network.
TURN vs. STUN
STUN and TURN are both protocols used in WebRTC to establish a connection between devices that are behind NAT. However, they serve different purposes.
STUN is used to obtain the public IP address of a device that is behind NAT. When a device connects to the internet through a router that uses NAT, it is assigned a private IP address that cannot be accessed from outside the network. To establish a direct connection between two devices, WebRTC needs to know the public IP address of each device. STUN is used to obtain this information.
TURN, on the other hand, is used to relay data between two devices that are behind NAT. If a direct connection cannot be established between the two devices, TURN provides a relay server that can be used to transmit data between them.