views:

720

answers:

3

The scenario is the following. I have two machines A and B: A: Client (behind NAT) B: Server (behind NAT)

I want B to be able to listen on any given port, so that A can send packets to B through that specific TCP port and receive any response. If both machines are not behind a NAT it is pretty straight foward process. However how do I make it work so that it works even when B is behind a router, without him having to go change the router configuration enable some port forwarding etc...

For example, how do p2p program like torrent clients work without the user having anything to configure?

Thanks and I hope my question is clear enough.

A: 

You will have to either:

  1. Set up port forwarding from the nat gateway in front the server into the machine your server software is running, and have the client connect to the IP address of that gateway.

  2. Create a proxy server sitting inbetween the 2 nat gatewys so both your server and client can connect to that. Both your server and client have to set up a connection to that proxy which will mediate the data between those 2 connections.

nos
Hi,I have envisaged that solution, the only problem with this solution is that the load will considerable on the proxy, this costly.
Chetane
+1  A: 

To answer the example of Peer to Peer programs, and in general: There is a technology called Universal Plug and Play which NAT routers can use to allow clients behind them to expose ports to the outside. That's what bittorrent clients can use so the other clients can directly connect to them.

Douglas Leeder
but isn't UPnP just a way to automate the port forwarding automation?
Roman Plášil
The key is *automation* - I assume the 'him' in the question is the user. Avoiding manually having to setup the port-forwarding is both the beauty and the danger of UPnP.
Douglas Leeder
A: 

An alternative to a proxy server is a match-making server. Instead of proxying all of the traffic, the match maker just negotiates until the peers can talk to each other. This involves finding the external public IPs of the peers and talking to each one so that the firewall/router knows that the peers wish to communicate.

This is called hole punching and it often has to be done by the match maker rather than the peers themselves. Once the hole are punched though, the match maker can tell the peers about each other and they can communicate directly.

sean riley
AFAIK hole punching like this only works for UDP - and only if the UDP port mapping is consistent on the router.
Douglas Leeder
ah.. he does say TCP in the question..
sean riley
I read about hole punching, would TCP hole punching work as well? It might however be not so easy to implement but I'll give it a try as this seems to be what I am looking for.Thanks.
Chetane