Official eMule-Board: Emule Upnp Port Forwarding - Official eMule-Board

Jump to content


Page 1 of 1

Emule Upnp Port Forwarding

#1 User is offline   Aureliano82 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 05-January 12

Posted 06 January 2012 - 12:00 AM

Would somebody be so kind to tell me how eMule implements its UPnP support? I'm trying to use the UPnP port forwarding in Windows with the IUPnPNAT interface of Microsoft without any success. But eMule works fine and open the ports in my router, what library is using? Is it an own implementation of the protocol?

My code looks like this:

WORD abre_puerto() {
WORD puerto, i; wchar_t buf_port[12], puerto_s[6]; char nombre[256], ip[16]; wchar_t ipw[16], descripcion[100];
struct addrinfo *resultado = NULL; struct addrinfo *ptr = NULL; struct addrinfo hints; struct sockaddr_in *sockaddr_ipv4;
IUPnPNAT *nat; IStaticPortMappingCollection *coleccion; IStaticPortMapping *mapeado; BSTR protocolo, ipb;

swprintf(buf_port, sizeof(buf_port), L"%d", time(NULL));
swprintf(puerto_s, sizeof(puerto_s), L"151%c%c", buf_port[wcslen(buf_port) - 2], buf_port[wcslen(buf_port) - 1]);
puerto = _wtoi(puerto_s);

if(gethostname(nombre, sizeof(nombre)) == SOCKET_ERROR) {return 0;}
ZeroMemory(&hints, sizeof(hints)); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP;
if(getaddrinfo(nombre, NULL, &hints, &resultado) != 0) {return 0;}

sockaddr_ipv4 = (struct sockaddr_in *)resultado->ai_addr;
strcpy(ip, inet_ntoa(sockaddr_ipv4->sin_addr));
MultiByteToWideChar(CP_UTF8, 0, ip, -1, ipw, sizeof(ipw));
ipb = SysAllocString(ipw);

nat = NULL; coleccion = NULL; mapeado = NULL;
if(CoInitialize(NULL) != S_OK) {return 0;}
if(CoCreateInstance(__uuidof(UPnPNAT), NULL, CLSCTX_ALL, __uuidof(IUPnPNAT), (void **)&nat) != S_OK) {return 0;}
else if(nat == NULL) {return 0;}
if(nat->get_StaticPortMappingCollection(&coleccion) != S_OK) {return 0;}
else if(coleccion == NULL) {return 0;} //fails here: coleccion is allways NULL!
protocolo = SysAllocString(L"TCP");
wcscpy(descripcion, TEXT("PROOF"));

for(i = 0; i < 250; i++) {
if(coleccion->get_Item(puerto, protocolo, &mapeado) != S_OK || mapeado == NULL) {break;}
puerto++; mapeado->Release(); mapeado = NULL;
}
if(i == 250) {return 0;}
if(coleccion->Add(puerto, protocolo, puerto, ipb, TRUE, descripcion, &mapeado) != S_OK) {return 0;}
else if(mapeado == NULL) {return 0;}
nat->Release(); coleccion->Release(); mapeado->Release();
CoUninitialize();

return puerto;
}

Thank you very much.

This post has been edited by Aureliano82: 06 January 2012 - 10:01 AM

0

#2 User is offline   Some Support 

  • Last eMule
  • PipPipPipPipPipPipPip
  • Group: Yes
  • Posts: 3631
  • Joined: 27-June 03

Posted 06 January 2012 - 07:20 PM

Since you do understand C/C++, why not take a look into the source code. Relevent classes contain "UPnP".

In short eMule uses two different approaches, one via the Windows API and one with a third party library.

  • Member Options

Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users