12

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 peer-to-peer programs like torrent clients work without the user having anything to configure?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Chetane
  • 240
  • 1
  • 3
  • 10

4 Answers4

8

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
  • 52,368
  • 9
  • 94
  • 137
1

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
  • 223,662
  • 58
  • 417
  • 506
  • Hi, I have envisaged that solution, the only problem with this solution is that the load will considerable on the proxy, this costly. – Chetane Oct 03 '09 at 00:15
1

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
  • 2,633
  • 1
  • 22
  • 22
  • 1
    AFAIK hole punching like this only works for UDP - and only if the UDP port mapping is consistent on the router. – Douglas Leeder Oct 02 '09 at 22:31
  • 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 Oct 03 '09 at 00:16
  • you can have a Look on STUNT. But I would Not recommend TCP hole punching as it will Not work reliably – tobias Jul 28 '13 at 00:50
1

Hole punching is moderately well-understood for UDP communication, but it can be reliably used to set up peer-to-peer TCP streams as well. Here is the well detailed article on both TCP and UDP:

http://www.brynosaurus.com/pub/net/p2pnat/

Humoyun Ahmad
  • 2,875
  • 4
  • 28
  • 46