NAT traversal: How does STUN work?
Translation of the original in Catalan. Read it in: CatalàCastellano
Part 2 of 3NAT traversal
- NAT traversal: Introduction
- NAT traversal: How does STUN work?
- NAT traversal: How does ICE work?
The STUN server has to publish its IP (3478TCP/UPD or 5349TLS) on a gateway that has an internal and an external IP (private and public). The communication starts when the client that is on the masqueraded network launches a series of
requests
against the public IP of the STUN server. The STUN server will answer the client informing it of the pairs of ports used outside the NAT, with that the client learns what type of NAT it has and what the TTL of the bindings made on the outside is to re-send its requests.
A client application in order to know who its STUN server is does is look up in its DNS domain the resolution of the name _stun_.domini_exemple.com or _stun._udp.domini_exemple.com for the SRV record types. After knowing who its STUN server is what the application does is look up the public address of the STUN server. In order to discover what the source of its packets will have to be on the internet. So when it builds the packets with the application data it can warn what the public address of its STUN server is.
Having reached this point often we have enough to start the communication with the final destination of our link, since this one will only have to answer the packets through the same ports that the gateway is doing
binding
at that moment. This is where the STUN server does its job keeping the ports open. One of the problems we can run into is that the destination node can't attack those ports because of some restriction of its firewall or similar. Which complicates the possibility of making the link. If the two hosts we want to communicate are both behind the NAT-masquerade the thing becomes very difficult and often a proxy is needed between the two STUN servers for the thing to work, in other words, it gets very complicated.
Protocol flow diagram:
3 comments
Comments from the old blog, as they were written. Comments are closed.

pau
Oriol he llegit i rellegit el teu post i la info de la wikipedia, hi ha un dubte que no aconsegueixo aclarir : en casos de restricted cone i port restricted cone diuen que només funciona si els dos host inicien les transmisions al mateix moment ? Saps que cony vol dir això, perque jo no veig solució si no es amb full conne !!!
També hauria sgiut bo que en le protocol de stun s'utilitzecin les paruales source i destination !! m'he fet un lio que thi cagues ;)
Oriol Rius
Em sap greu respondre amb una altre pregunta, però no veig on diu que han de començar les connexions al mateix moment...
Bàsicament perquè l'enllaç funcioni amb restricted cone i port restricted el que cal és usar aquests petits rangs permesos i el que cal és aprofitar el rang de temps que queda quan es fa binding en el NAT per passar els paquets de forma que mai es compleixi el TTL i sempre quedin oberts els ports al NAT. No sé si m'he explicat.
pau
En parlem el dissabte despres del partit de tenis ;)