Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto port forwarding (UPnP ?) C++

Disclaimer: I've carefully read every similar topic here and did a google search. Non of those answered my questions, so I would like to accumulate information in this topic. P.S. I might not be so good in English, sorry about that.

I want to make a p2p application (a game). Obviously lots of people will run this program on a machine which is a part of a local network with one internet access point (router) and app simply will not work without port forwarding. So, I did a research and found out that I can do automatic port forwarding with UPnP. Micro test-code were written to access port forwarding:

bool CPortController::Init() {


  HRESULT result = CoInitialize(NULL); // Must be NULL
  if (FAILED(result)) return false;

  result = CoCreateInstance(__uuidof(UPnPNAT), NULL, CLSCTX_ALL, __uuidof(IUPnPNAT), (void **)&Nat);
  if (FAILED(result) || !Nat) return false;

  result = Nat->get_StaticPortMappingCollection(&Collection); 
  if (FAILED(result) || !Collection) return false; // Here I'm getting S_OK as result, but Collection is always == 0

  return true;
}

Searched for reason of that(see comment in code) happening I've found: 1. Old system (nope, I have Win7 with updates) 2. Router might not have UPnP capability or it might not be enabled 3. Firewall might be blocking TCP port 2869 or UDP port 1900 which are both needed for UPnP

Sadly, I don't have access to router to check last 2, but it's not that important since Skype and uTorrent (both p2p) works perfectly. I need to find the way to do the same trick and forward port automatically with my app without asking user to do anything.

About UPnP libraries: I've found few UPnP libraries (PlatinumUPnP, miniUPnP), but they seems to have so much code oriented on finding smart devices and putting them together ... uhm ... I'm not sure I need this to complete my task and I can't find a piece of code (which not using those WinUPnPAPI, I listed above) for auto-port-forwarding.

I'm a bit lost now. Could anyone show me a direction to continue research ? Maybe there is something else than UPnP for that ? Maybe there is a lib (which I missed) to use ? Spread some light, please. Thank you in advance.

like image 319
JacksonRR Avatar asked Nov 14 '12 14:11

JacksonRR


People also ask

What is UPnP port forwarding?

Universal Plug and Play (UPnP) is a network protocol that allows compliant devices to automatically set port forwarding rules for themselves. These devices can be personal computers, printers, security cameras, game consoles or mobile devices that communicate with each other and share data over your network.

Should UPnP be enabled for port forwarding?

Should UPnP be enabled? It depends on what you want: convenience or security. UPnP has its security risks, but it also lets devices connect and send port forwarding requests automatically, saving time. If you're a heavy UPnP user and don't want to set up manual ports on your router, you can keep UPnP enabled.

Should UPnP be enabled?

Should You Disable UPnP? Ultimately, it is a matter of opinion. UPnP is convenient but does bring with it some quite serious security flaws, some of which cannot be mitigated by security solutions. It's our recommendation that if you don't use port forwarding at all, then you should disable UPnP.

What is automatic port forwarding?

Port forwarding is a process on local network routers that forwards connection attempts from online devices to specific devices on a local network. This is thanks to the port forwarding rules on your network router that match the connection attempts made to the correct port and IP address of a device on your network.


1 Answers

Run DeviceSpy from UPnP Developer Tools and search your network for InternetGatewayDevice. If there is none, your router is not UPnP capable or has it disabled. If there is one, check WANIPConnection service. Be aware that as of InternetGatewayDevice v2 (since 2010), you are not allowed to set static port mapping (lease time 0) over UPnP.

As @Rook pointed out, there are other methods for establishing P2P connections, so the fact that some other random P2P software works, doesn't necessarily mean that the software knows how to configure your port mapping. Skype in particular is not really P2P, it depends on huge number of dedicated supernodes.

like image 187
Pavel Zdenek Avatar answered Oct 09 '22 10:10

Pavel Zdenek