Sign in to follow this  
krozen

NAT hold punching with TCP (Game Prog. Gems 6 article)

Recommended Posts

krozen    122
Hi guys, I'm just working through "Reliable Peer-2-Peer Gaming Connections Using TCP Penetrating NATs" from Game Programming Gems 6. I fully understand the basics behind the gem. However, the authoer mentions that, in order for hole punching to work, it is necessary to bind new TCP connections to the existing connection used to connect to the connection broker using SO_REUSEADDR. My issues are as follows: 1. If 2 sockets are bound to the same port, but sending to different destinations, how does each socket know which data is meant for it? Is something done using the addresses of the sender? Its hard to find out, as, from what I've read, the behaviour of sockets in such a scenario is "undefined". 2. Does this mean that every single connection has to use the same port, but different destination ip addresses as the connections are brokered. Would anyone be able to clarify these issues for me? Thanks for your help!

Share this post


Link to post
Share on other sites
hplus0603    11356
Quote:
how does each socket know which data is meant for it?


The definition of a TCP connection is the four-tuple:
1) source ip address
2) source TCP port number
3) destination ip address
4) destination TCP port number

When that four-tuple is the same, then the connection is the same. To have multiple connections between two separate hosts, at least one of the hosts must use a separate port. For punch-through, you'll end up with only a single connection available between the hosts per port number you use.

Share this post


Link to post
Share on other sites
peter_b    200
hplus: Do you know if its possible to bind a tcp socket using SO_REUSEADDR to a port that is already in use, but is bound by another application(on win32)?

For instance if you are running a server of some application you have not written yourself behind a NAT, and you would like to be able to connect to it.

Then you could write an application that at some fixed time would try to punch a hole to the computer you would connect from(static address).

Could this work?

Share this post


Link to post
Share on other sites
krozen    122
Thanks for that Hplus. That's what I was thinking, but wasn't sure. Your own article in the previous Gems book was great by the way - loads of great code samples, and very detailed (which I sometimes find lacking in the Gems series). So thanks for that!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this