36

I am designing a p2p application which works on port 30000. My router is not UPnP so I required to forward a port to router. But Skype a another p2p application works without port forwarding on my pc. When I analyzed it with wireshark I found its using UDP port 48980, 58544. I am using c++.

There is a library in python here which does it for Nat PnP routers. Is it possible to programmatically forward port to router irrespective of type of router and operating system. What should be the approach to do it in c++ or any other language.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Xinus
  • 29,617
  • 32
  • 119
  • 165

4 Answers4

22

Skype works in a very interesting way. From what I've read (and this is a while ago) it works as follows:

  • Skype server maintains a list of all users and IPs.
  • Skype user A wants to speak to user B
  • Skype user A sends a network packet to user B's IP address and waits for a response. (user B never gets this packet).
  • Skype user A notifies server of IP/port combination on which it is waiting for a response
  • Server notifies user B to send a response to user A on the specified port
  • A connection is made.

This is probably over simplified but last I checked, this is how it works. (Someone correct me if I'm wrong).

Edit: fixed bullet issue

Mike R
  • 329
  • 2
  • 11
rein
  • 32,967
  • 23
  • 82
  • 106
  • 2
    but question is that are IP of clients PUBLIC? see for example A needs to communicate to B and server knows the IP of B but what ip it uses? if this is the case you can ping anyones ip? sorry if it is stupid – Afnan Bashir Apr 02 '12 at 00:02
  • 6
    This answer merely describes A and failing to communicate to B and the server sending the right IP/Port combination to user B. It's missing the main point, which is called hole punching and it very well described in the link provided by @MartinBeckett. I would just add that it is also possible to forward the port irrespectively of the type of router and the operating system, provided the router supports UPnP. – Igor Jan 27 '13 at 15:15
  • 1
    @rein so what about symmetric NAT ? – onmyway133 Jul 21 '13 at 04:09
20

UDP hole punching is (one) of ways how to traverse through the NAT.

Dustin Getz
  • 21,282
  • 15
  • 82
  • 131
nothrow
  • 15,882
  • 9
  • 57
  • 104
  • I am not able to understand difference between router and NAT ? Do we forward port to router or NAT? Isn't hole punching equivalent to sending data from both sides at the same time which does not work when we work on different port. Thanks – Xinus Oct 08 '09 at 18:04
  • 1
    A router routes information from one network to another. NAT is the process of having a private address range on the internal network and only having the router have a valid address on the other network. Router is a physical device, NAT is something you do. – rein Oct 08 '09 at 18:09
3

You have to use an intermediary server, and initiate communication from the client side. As Yossarian mentioned, from that point one possibility is UDP hole punching. Depending on the type of application (and whether it's truly P2P or not) you might keep the go-between server in place the entire time.

Joe
  • 41,484
  • 20
  • 104
  • 125
0

Skype uses another peers as intermediate point when direct connection is not possible.

i.e. Peer A wants to connect to peer B, but peer B has all ports closed, then communication is initiated through peer C which has open ports.