Author Topic: P2P connections dealing with NAT  (Read 6478 times)

stakka

  • Not-a-newbie
  • *
  • Posts: 1
  • Karma: 0
    • View Profile
P2P connections dealing with NAT
« on: January 20, 2012, 09:12:09 AM »
Hi,

I have a p2p system set up for a game that works in the following fashion :
* one peer initializes himself as the master, and gets a fixed port (say 200)
* all the others initialize themselves with a random port (raknet chooses it) and connect to the master on port 200
* master is just a simple flag, you should compare it to the one opening a game in a lobby (he gets to decide when the game starts)

For the connection I use the following scheme :
* a peer (client) connects to another peer (the master) and the master replies with a host welcome message
* the client replies to the master with a client connect message (adding version, hash, ...)
* the master checks some stuff (version, hash, room free?, ...), and accepts or refuses the connection
* on refusal, the master sends a connection refused message
* on acceptance, the master sends a client joined message to the client which contains a list of the other peers connected to that master,
   the client places those peers in a list of peers from whom it expects a connection
* on acceptance, the master sends a peer joined message to all the other peers connected to that master with the address of the client
* when a peer receives a peer joined message it will try to connect to the client peer mentioned in that message
* when a peer (that is not the master) receives a connection, it will check if it expected a connection from that peer and accepted if it did

So far the summarized (hopefully not too elaborate) setup.
Now all works well when all peers are in the same LAN we have setup here.
But as soon as I place some PCs behind a NAT, the system stops working.

Here is what happens :
* The PC (PC-A) on the LAN has IP 192.168.0.2
* The Router has IP 192.168.0.100 and hands out IPs 10.0.0.x
* I have 2 PCs behind the NAT : PC-B 10.0.0.100 and PC-C 10.0.0.101
* PC-A is set up as the master on port 200
* PC-B is set up with port 1000 and connects to the master, PC-A
* Upon connection, PC-B gets the external IP 192.168.0.100:1000 and that is how the master, PC-A refers to it
* PC-C is set up with port 1001 and connects to the master, PC-A
* Upon connection, PC-C gets the external IP 192.168.0.100:1001 and that is how the master, PC-A refers to it
* Upon connection of PC-C, PC-A sends the peer joined message to PC-B telling it to connect to PC-C

That is where it goes wrong, PC-B is told to connect to PC-C, using the address known to PC-A, which is 192.168.0.100:1001.
When PC-B tries to connect to PC-C using it's external address (192.168.0.100:1001) it doesn't work. However, using it's internal address,
10.0.0.101:1001 the connection does succeed.

Eventually, I will use a lobby system (steamworks) to join the peers and select a master, but that will eventually yield the same problem
as far as i know. The question seems to be, how can i connect 2 peers that are behind the same NAT with each other using their external IP?
Is this impossible? Should I include their internal IP in the messages, so that the master can send both the internal and external IP of the newly
joined peer to the other peers, and they themselves check if the new peer is inside their NAT, and use the internal IP in that case?

Please advice how I should proceed,
thanks in advance!

Rak'kar

  • Administrator
  • Hero Member
  • *****
  • Posts: 6895
  • Karma: 291
    • View Profile
    • RakNet
Re: P2P connections dealing with NAT
« Reply #1 on: January 20, 2012, 10:37:24 PM »
See NAT punchthrough in the manual. Steam does do this automatically though. For the host of a peer to peer game, use FullyConnectedMesh2 which can migrate the host automatically.

kbar

  • Jr. Member
  • **
  • Posts: 91
  • Karma: 3
    • View Profile
    • GameLogicDesign
Re: P2P connections dealing with NAT
« Reply #2 on: January 22, 2012, 11:07:44 PM »
The issue might be that your router doesn't support Nat Loopback

http://opensimulator.org/wiki/NAT_Loopback_Routers

I have the same issue, I can connect using the internal IP addresses but I can't connect using the IP address of the router when I am behind the NAT. If I connect to it from another network then using the IP address of the router does work. So when I am internal on my network (ie behind the NAT) I just use the internal IP, when I am out of the house I can connect using the IP address of the router (which is the IP address assigned to it by my ISP).

You should also give your master server a fixed port (which you are with 200) and make sure you port forward it on your router to the computer its running on, also assign your server a specific IP address (set this up on your router) so that every time you turn on the server it uses the same one. Then you will be able to connect to that Master Computer when you are outside of your network.

A better way would be to use NatPunchThrough (or the Lobby Server since that already contains it). And have that always running on a fixed port on a server with a static IP address (same port forward business as I just said above).
« Last Edit: January 22, 2012, 11:19:52 PM by kbar »