STUN: Definition and explanation
Tsahi Levent-Levi
WebRTC & Programmable Communications Expert | Your WebRTC problem solver
STUN stands for?Session Traversal Utilities for NAT.
Session Traversal Utilities for NAT is a standard method of NAT traversal used in WebRTC. The definition is in IETF RFC 5389.
It is one of two NAT traversal servers that are used in WebRTC (the other one is TURN).
At its core, it’s purpose is to answer the question “what is my IP address?” It does that by using a STUN server.
What is a STUN server?
It is a client-server protocol.
A STUN server is a server that runs on the public network and replies to incoming requests. The responses sent out include the public IP address the request was sent to him from. This effectively answers the question “what is my IP address?”
In most cases, the servers aren’t installed and managed as separate entities but are rather installed together with TURN servers.
While there are free public servers that can be used (such as the one Google operates), it isn’t advisable to use them in commercial services as your main NAT traversal mechanism.
Using a STUN server
A WebRTC client has to know its public IP address in order to get more sessions connected successfully. For that purpose, WebRTC sends a request to a STUN server asking for its public IP address. The server replies back to the WebRTC client with the public IP address the request came from. This way, the WebRTC client learns what its public IP address is.
The WebRTC client then shares the public IP address it received from the server with its peer.
领英推荐
It might not always work. With some network architecture and NAT device types, the public IP address obtained via STUN will not work. This is why we use it in conjunction with TURN and ICE.
As a protocol, it is lightweight and simple, making the availability of public, free and open servers possible.
Free and public Session Traversal Utilities for NAT server
There are various free STUN servers available out there.
It is lightweight and carry no real authentication mechanism to it. This makes it simple for developers to connect to freeservers without any technical hurdles. It is not advisable to do so.
Best practices
See also the episode of “WebRTC Insights”: