Practical NAT traversal for reliable network connections

Your simple problem, as you've discovered, isn't simple at all. NAT traversal is hard. What makes ICE unsuitable? Its use of SDP? Something else?

Commented Sep 1, 2010 at 12:07

Maybe I had trouble understanding anything from the huge pile of obscure terminology, but how would ICE help me here? From what I understood, in simple words ICE just figures out if it can make a direct connection, and if it can't it uses an intermediary server to tunnel the data.

Commented Sep 1, 2010 at 14:37

Also, I kinda refuse to believe that no one has solved this problem yet. I mean, it has to be incredibly common! Mostly in video games (some can deal with unreliable connections but not all). Does everyone just write their own "reliable UDP" protocols?

Commented Sep 1, 2010 at 14:41

2 Answers 2

ICE collects a list of candidate IP/port targets to which to connect. Each peer collects these, and then each runs a connectivity check on each of the candidates in order, until either a check passes or a check fails.

When Alice tries to connect to Bob, she somehow gets a list of possible ways - determined by Bob - she may connect to Bob. ICE calls these candidates. Bob might say, for example: "my local socket's 192.168.1.1:1024/udp, my external NAT binding (found through STUN) is 196.25.1.1:4454/udp, and you can invoke a media relay (a middlebox) at 1.2.3.4:6675/udp". Bob puts that in an SDP packet (a description of these various candidates), and sends that to Alice in some way. (In SIP, the original use case for ICE, the SDP's carried in a SIP INVITE/200/ACK exchange, setting up a SIP session.)

ICE is pluggable, and you can configure the precise nature/number of candidates. You could try a direct link, followed by asking a STUN server for a binding (this punches a hole in your NAT, and tells you the external IP/port of that hole, which you put into your session description), and falling back on asking a TURN server to relay your data.

One downside to ICE is that your peers exchange SDP descriptions, which you may or may not like. Another is that TCP support's still in draft form, which may or may not be a problem for you. [UPDATE: ICE is now officially RFC 6544.]

Games often use UDP, because old data is useless. (This is why RTP usually runs over UDP.) Some P2P applications often use middleboxes or networks of middleboxes.

IRC uses a network of middleboxes: IRC servers form networks, and clients connect to a near server. Messages from one client to another may travel through the network of servers.

Failing all that, you could take a look at BitTorrent's architecture and see how they handle the NAT problem. As CodeShadow points out in the comments below, BitTorrent relies on reachable peers in the network: in a sense some peers form a network of middleboxes. If those middleboxes could act as relays, you'd have an IRC-like architecture, but one that's set up dynamically.