Search code examples
c++cnetwork-programmingupnp

Peer to peer connection, when one peer behind NAT


Okay, so I need my app in C++ to make peer-to-peer UDP connection. I've considered hole punching and UPNP(using port mapping). Hole punching seems a bit messy: you need server, tons of garbage UDP packets. I really do not want to mess with server, so I've chosen UPNP. I've built miniupnpc http://miniupnp.free.fr/ library, and happily used it at home. But when I've tried my app in another networks, I've got very frustrating results:

  1. Only 1 (my) router does everything I need(local & external ip, port mapping)
  2. 3 routers only show me local ip, and maybe(I've not tested) do port mapping
  3. 1 router supports upnp, but miniupnpc could not find device
  4. 1 router does not support upnp(tested with upnp developer tools)

So, I have 2 questions:

  1. Is UPNP not very common, or am I just unlucky, or using miniupnpc wrong?
  2. Are there any other ways to make peer-to-peer UDP connection, which does not require external server?(So not hole-punching)

Thank you!


Solution

    1. Is UPNP not very common, or am I just unlucky, or using miniupnpc wrong?

    You can't trust on a random router to support UPnP IGD port mapping, and you definitely can't trust on the feature to be enabled.

    Are there any other ways to make peer-to-peer UDP connection, which does not require external server?(So not hole-punching)

    A third party server is not strictly speaking required for hole punching in typical home networks but you do need a way for the endpoints know about each others external IP address and port: At that point both machines can just start sending messages to each other: the first messages will indeed be dropped but when the router sees that the internal IP has already sent packets to to the foreign IP, it will also allow incoming traffic (since the router sees these as responses to the packets from internal IP even though they aren't really).

    See ICE for something resembling a standard in this area.