NAT traversal: How does ICE work?
Translation of the original in Catalan. Read it in: CatalàCastellano
Part 3 of 3NAT traversal
- NAT traversal: Introduction
- NAT traversal: How does STUN work?
- NAT traversal: How does ICE work?
In order to understand how
ICE
works the best thing is to explain it together with another protocol; since as was mentioned in the introduction to NAT traversal the
ICE
protocol uses a combination of the TURN and STUN protocols depending on the case, that's very simple to understand if we talk about a practical case of use. That is, when we try for example, for SIP to cross a firewall/gateway/router to deliver a call to an IP phone.
What are candidates?
The Caller sends a
SIP INVITE
message with SDP information that describes over which IP and over which PORTS the application can receive audio and/or video. These addresses and ports are known as
candidates
. These
candidates
are obtained from the
ICE
server (it's often found inside the firewall/router/gateway).
We could describe a
candidate
as an IP and a port through which a
peer
can receive data from another
peer
. There are three types of
candidates
:
- Local candidate: local IP of the client.
- Reflexive or STUN candidates: a public IP address of the NAT server.
- Relay or TURN candidate: an address or a relay server that has been reserved by the client.
Traffic can always be sent successfully through the
relay candidates
, unless a firewall blocks the link, in which case there's no possible NAT traversal technique to apply. The problem of using
relay candidates
is that it requires using a server's resources, that makes the traffic arriving at the peer have delays, losses and
problems.
Process of establishing a SIP call via ICE
1. The Caller gets its candidates
In the following graph you can see how the Caller determines the
reflexive i relay candidates
for a connection. The client sends an
ALLOCATE
request to the server, then the server locates an IP/port pair (the
relay candidate
). This information is sent back to the Caller (
reflexive candidate
), this message contains the public IP the Caller will use when it emits a message to the internet, often that is the IP through which the NAT will be applied.
2. The Caller sends a SIP INVITE
The Caller builds a
SIP INVITE
message using the information of the candidates it has obtained from its
ICE
server, then sends it to its destination.
3. The Callee gets the candidates
When the
SIP INVITE
message is received, the Callee gets the
candidates
in the same way the Caller has done in point (1).
4. The Callee sends a 1xx response
Continuing with the SIP protocol, the Callee has to generate a response message to the
SIP INVITE
, for example, a
SIP 183
(Session Progress). When this message is built inside the SDP information the Callee will also encapsulate its
candidates
. Until this message hasn't been received and confirmed by the Caller (
200 OK
) the negotiation won't be considered successful.
5. ICE connectivity checks
Having reached this point both the Caller and the Callee know each other's candidates and it's the moment to use what is called
ICE
connectivity checks. This is done by sending a STUN message from the local candidate towards the remote candidate, this can be done for as many
candidates
as there are from the one with the highest priority to the one with the lowest. Both sides of the link will do this if both are behind a gateway with
ICE
. When it has been determined which of the
candidates
is the best to establish each of the channels then this candidate will be chosen to start sending and receiving media through it.
Below you can see three examples of how these tests of the
candidates
are done. The first diagram would be the case in which both hosts are directly routable because, for example, they're on the same LAN. The second diagram would achieve the link via the STUN methodology and in the third via the TURN methodology.
6. The Callee sends SIP 180 (ringing)
At this point the Callee sends the
SIP 180
(ringing) message to the Caller to inform it that the Callee's phone is ringing. Having previously negotiated the data channel through
ICE
when the Callee picks up the phone it will be able to talk without delays, if this were negotiated afterwards there would be an unpleasant delay effect for the
peers
.
7. Accepts the call and they talk
So then, when the Callee picks up a
200 OK
message will be sent and via RTP (for example) and using the channels negotiated via
ICE
the multimedia data flow (in this case audio) will start crossing transparently the two gateways that give internet access to the two
peers
.
Some notes to clarify the subject
In this example the aim is to demonstrate that we can establish an
link through two
peers
that are behind NAT thanks to the
ICE
protocol. As a protocol to control the RTP session SIP is used, that makes both peers need a DNAT redirection on their gateway towards the TCP/UPD 5060 ports. In case we want to avoid that what should be used is another signalling protocol different from SIP. For example, if we don't want/can't redirect any port on the gateway XMPP could be used.
Glossary
- Caller: who makes the call
- Calee: who receives the call
- SDP: Session Description Protocol
- SIP: Session Initiation Protocol
- Peer: one end of the connection

