Auto port forwarding (UPnP ?) C++
Asked Answered
N

1

15

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.

Nofretete answered 14/11, 2012 at 14:32 Comment(3)
Skype avoids the need for UPnP by using a trick called NAT hole punching which requires the co-operation of a public server. This means the initial connection setup phase is not P2P, though subsequent communication is.Fosque
@Rook, thank you for letting me know about HolePunching. I've read few articles and it looks like exactly what I need. Sadly, can't mark your comment as "accepted answer", since it's posted as comment, but it's totally worth it! Thanks :)Nofretete
Well, I wasn't going to make it an answer because it doesn't talk about uPnP at all. Pavel's answer is rather more useful in that respect!Fosque
S
18

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.

Superable answered 14/11, 2012 at 22:35 Comment(1)
thank you very much. (sorry, 11 reputation does not allows to vote you up, but your comment is very halpful, thanks)Nofretete

© 2022 - 2024 — McMap. All rights reserved.