From 7e708ff4aad140d013bb2ba54d9bd09f74baff91 Mon Sep 17 00:00:00 2001 From: Phenom Date: Thu, 24 Sep 2015 00:37:31 +0200 Subject: [PATCH] Fix MiniUPnP IGD with newers versions. Only for windows, as other use UPnPBase. --- libretroshare/src/libretroshare.pro | 1 - .../src/upnp/upnphandler_miniupnp.cc | 100 +++- libretroshare/src/upnp/upnputil.c | 522 ++++++++++++++---- libretroshare/src/upnp/upnputil.h | 14 +- 4 files changed, 516 insertions(+), 121 deletions(-) diff --git a/libretroshare/src/libretroshare.pro b/libretroshare/src/libretroshare.pro index 8cb5e742c..2e53b8884 100644 --- a/libretroshare/src/libretroshare.pro +++ b/libretroshare/src/libretroshare.pro @@ -229,7 +229,6 @@ win32 { OBJECTS_DIR = temp/obj MOC_DIR = temp/moc DEFINES *= WINDOWS_SYS WIN32 STATICLIB MINGW WIN32_LEAN_AND_MEAN _USE_32BIT_TIME_T - DEFINES *= MINIUPNPC_VERSION=13 # This defines the platform to be WinXP or later and is needed for getaddrinfo (_WIN32_WINNT_WINXP) DEFINES *= WINVER=0x0501 diff --git a/libretroshare/src/upnp/upnphandler_miniupnp.cc b/libretroshare/src/upnp/upnphandler_miniupnp.cc index 3c2442527..0b4d32c53 100644 --- a/libretroshare/src/upnp/upnphandler_miniupnp.cc +++ b/libretroshare/src/upnp/upnphandler_miniupnp.cc @@ -30,13 +30,81 @@ bool upnphandler::initUPnPState() { /* allocate memory */ uPnPConfigData *upcd = new uPnPConfigData; - -#if MINIUPNPC_VERSION >= 11 - /* Starting from version 1.1, miniupnpc api has a new parameter (int sameport) */ - upcd->devlist = upnpDiscover(2000, NULL, NULL, 0); +#if MINIUPNPC_API_VERSION >= 14 //1.9 2015/07/23 + /* $Id: miniupnpc.h,v 1.44 2015/07/23 20:40:10 nanard Exp $ */ + //upnpDiscover(int delay, const char * multicastif, + // const char * minissdpdsock, int sameport, + // int ipv6, unsigned char ttl, + // int * error); + unsigned char ttl = 2; /* defaulting to 2 */ + upcd->devlist = upnpDiscover(2000, NULL, + NULL, 0, + 0, ttl, + NULL); #else - upcd->devlist = upnpDiscover(2000, NULL, NULL); -#endif +#if MINIUPNPC_API_VERSION >= 8 //1.5 2011/04/18 + /* $Id: miniupnpc.h,v 1.41 2015/05/22 10:23:48 nanard Exp $ */ + /* $Id: miniupnpc.h,v 1.23 2011/04/11 08:21:46 nanard Exp $ */ + //upnpDiscover(int delay, const char * multicastif, + // const char * minissdpdsock, int sameport, + // int ipv6, + // int * error); + upcd->devlist = upnpDiscover(2000, NULL, + NULL, 0, + 0, + NULL); +#else +#if MINIUPNPC_API_VERSION >= 6//1.5 2011/03/14 + /* $Id: miniupnpc.h,v 1.21 2011/03/14 13:37:12 nanard Exp $ */ + //upnpDiscover(int delay, const char * multicastif, + // const char * minissdpdsock, int sameport, + // int * error); + upcd->devlist = upnpDiscover(2000, NULL, + NULL, 0, + NULL); +#else +#if MINIUPNPC_API_VERSION >= -4//1.1 2008/09/25 + /* $Id: miniupnpc.h,v 1.20 2011/02/07 16:46:05 nanard Exp $ */ + /* $Id: miniupnpc.h,v 1.18 2008/09/25 18:02:50 nanard Exp $ */ + //upnpDiscover(int delay, const char * multicastif, + // const char * minissdpdsock, int sameport); + upcd->devlist = upnpDiscover(2000, NULL, + NULL, 0); +#else +#if MINIUPNPC_API_VERSION >= -5//1.0 2007/12/19 + /* $Id: miniupnpc.h,v 1.17 2007/12/19 14:58:54 nanard Exp $ */ + //upnpDiscover(int delay, const char * multicastif, + // const char * minissdpdsock); + upcd->devlist = upnpDiscover(2000, NULL, + NULL); +#else +#if MINIUPNPC_API_VERSION >= -6//1.0 2007/10/16 + /* $Id: miniupnpc.h,v 1.15 2007/10/16 15:07:32 nanard Exp $ */ + //LIBSPEC struct UPNPDev * upnpDiscover(int delay, const char * multicastif); + upcd->devlist = upnpDiscover(2000, NULL); +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + /* $Id: miniupnpc.h,v 1.14 2007/10/01 13:42:52 nanard Exp $ */ + /* $Id: miniupnpc.h,v 1.9 2006/09/04 09:30:17 nanard Exp $ */ + //struct UPNPDev * upnpDiscover(int); + upcd->devlist = upnpDiscover(2000); +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version + //2006/09/04 to 2007/10/01 => -7//Start return struct UPNPDev * for upnpDiscover + //2007/10/16 => -6 upnpDiscover + //2007/12/19 => -5 upnpDiscover + //2008/09/25 => -4 upnpDiscover + //2009/04/17 => -3 UPNP_AddPortMapping + //2010/12/09 => -2 //struct IGDdatas_service CIF; + //2011/02/15 => -1 UPNP_AddPortMapping + //2011/03/14 => 6 //Start of MINIUPNPC_API_VERSION +#endif//>=-7 +#endif//>=-6 +#endif//>=-5 +#endif//>=-4 +#endif//>=6 +#endif//>=8 +#endif//>=14 if(upcd->devlist) { @@ -305,12 +373,16 @@ bool upnphandler::start_upnp() std::cerr << std::endl; if (!SetRedirectAndTest(&(config -> urls), &(config->data), - in_addr, in_port1, eport1, eprot1)) + in_addr, in_port1, eport1, eprot1, + NULL /*leaseDuration*/, NULL /*description*/, + 0)) { upnpState = RS_UPNP_S_TCP_FAILED; } else if (!SetRedirectAndTest(&(config -> urls), &(config->data), - in_addr, in_port2, eport2, eprot2)) + in_addr, in_port2, eport2, eprot2, + NULL /*leaseDuration*/, NULL /*description*/, + 0)) { upnpState = RS_UPNP_S_UDP_FAILED; } @@ -323,8 +395,16 @@ bool upnphandler::start_upnp() /* now store the external address */ char externalIPAddress[32]; UPNP_GetExternalIPAddress(config -> urls.controlURL, - config->data.servicetype, - externalIPAddress); +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + config->data.first.servicetype, +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + config->data.servicetype, +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif +#endif + externalIPAddress); sockaddr_clear(&upnp_eaddr); diff --git a/libretroshare/src/upnp/upnputil.c b/libretroshare/src/upnp/upnputil.c index cd7f7da20..65d9a2227 100644 --- a/libretroshare/src/upnp/upnputil.c +++ b/libretroshare/src/upnp/upnputil.c @@ -1,8 +1,27 @@ //this file uses miniupnp +//From https://github.com/miniupnp/miniupnp/blob/master/miniupnpc/upnpc.c #include "upnp/upnputil.h" -/* protofix() checks if protocol is "UDP" or "TCP" +//Define this variable follow the date of used MiniUPnP Library +//#define MINIUPNPC_API_VERSION -7 +#ifndef MINIUPNPC_API_VERSION +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version + //2006/09/04 to 2007/10/01 => -7//Start return struct UPNPDev * for upnpDiscover + //2007/10/16 => -6 upnpDiscover + //2007/12/19 => -5 upnpDiscover + //2008/09/25 => -4 upnpDiscover + //2009/04/17 => -3 UPNP_AddPortMapping + //2010/12/09 => -2 //struct IGDdatas_service CIF; + //2011/02/15 => -1 UPNP_AddPortMapping + //2011/03/14 => 6 //Start of MINIUPNPC_API_VERSION +#endif//>=-7 + +#if MINIUPNPC_API_VERSION >= -4//1.0 2008/02/18 +#include +#endif + +/* protofix() checks if protocol is "UDP" or "TCP" * returns NULL if not */ const char * protofix(const char * proto) { @@ -10,13 +29,13 @@ const char * protofix(const char * proto) static const char proto_udp[4] = { 'U', 'D', 'P', 0}; int i, b; for(i=0, b=1; i<4; i++) - b = b && ( (proto[i] == proto_tcp[i]) - || (proto[i] == (proto_tcp[i] | 32)) ); + b = b && ( (proto[i] == proto_tcp[i]) + || (proto[i] == (proto_tcp[i] | 32)) ); if(b) return proto_tcp; for(i=0, b=1; i<4; i++) b = b && ( (proto[i] == proto_udp[i]) - || (proto[i] == (proto_udp[i] | 32)) ); + || (proto[i] == (proto_udp[i] | 32)) ); if(b) return proto_udp; return 0; @@ -25,42 +44,129 @@ const char * protofix(const char * proto) void DisplayInfos(struct UPNPUrls * urls, struct IGDdatas * data) { - char externalIPAddress[16]; + char externalIPAddress[40]; char connectionType[64]; char status[64]; - char lastconnerror[64]; + char lastconnerr[64]; unsigned int uptime; unsigned int brUp, brDown; - UPNP_GetConnectionTypeInfo(urls->controlURL, - data->servicetype, - connectionType); + time_t timenow, timestarted; + int r; +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + const char * servicetype = data->first.servicetype; + const char * servicetype_CIF = data->CIF.servicetype; +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + const char * servicetype = data->servicetype; + const char * servicetype_CIF = data->servicetype_CIF; +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif//>=-7 +#endif//>=-2 + +#if MINIUPNPC_API_VERSION >= -3//1.0 2009/04/17 + if(UPNP_GetConnectionTypeInfo(urls->controlURL, + servicetype, + connectionType) != UPNPCOMMAND_SUCCESS) +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + UPNP_GetConnectionTypeInfo(urls->controlURL, servicetype, + connectionType); if(connectionType[0]) - printf("Connection Type : %s\n", connectionType); - else +#endif//>=-7 +#endif//>=-3 printf("GetConnectionTypeInfo failed.\n"); - UPNP_GetStatusInfo(urls->controlURL, data->servicetype, status, &uptime, lastconnerror); - printf("Status : %s, uptime=%u LastConnError %s\n", status, uptime, lastconnerror); - UPNP_GetLinkLayerMaxBitRates(urls->controlURL_CIF, data->servicetype_CIF, - &brDown, &brUp); + else + printf("Connection Type : %s\n", connectionType); + +#if MINIUPNPC_API_VERSION >= -4//1.0 2008/02/18 + if(UPNP_GetStatusInfo(urls->controlURL, servicetype, + status, &uptime, lastconnerr) != UPNPCOMMAND_SUCCESS) + printf("GetStatusInfo failed.\n"); + else + printf("Status : %s, uptime=%us, LastConnectionError : %s\n", + status, uptime, lastconnerr); + timenow = time(NULL); + timestarted = timenow - uptime; + printf(" Time started : %s", ctime(×tarted)); +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + UPNP_GetStatusInfo(urls->controlURL, servicetype, + status, &uptime, lastconnerr); + printf("Status : %s, uptime=%u, LastConnectionError : %s\n", + status, uptime, lastconnerr); +#endif//>=-7 +#endif//>=-4 + +#if MINIUPNPC_API_VERSION >= -4//1.0 2008/02/18 + if(UPNP_GetLinkLayerMaxBitRates(urls->controlURL_CIF, servicetype_CIF, + &brDown, &brUp) != UPNPCOMMAND_SUCCESS) { + printf("GetLinkLayerMaxBitRates failed.\n"); + } else { + printf("MaxBitRateDown : %u bps", brDown); + if(brDown >= 1000000) { + printf(" (%u.%u Mbps)", brDown / 1000000, (brDown / 100000) % 10); + } else if(brDown >= 1000) { + printf(" (%u Kbps)", brDown / 1000); + } + printf(" MaxBitRateUp %u bps", brUp); + if(brUp >= 1000000) { + printf(" (%u.%u Mbps)", brUp / 1000000, (brUp / 100000) % 10); + } else if(brUp >= 1000) { + printf(" (%u Kbps)", brUp / 1000); + } + printf("\n"); + } +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + UPNP_GetLinkLayerMaxBitRates(urls->controlURL_CIF, + servicetype_CIF, + &brDown, &brUp); printf("MaxBitRateDown : %u bps MaxBitRateUp %u bps\n", brDown, brUp); +#endif//>=-7 +#endif//>=-4 + +#if MINIUPNPC_API_VERSION >= -5//1.0 2007/12/19 + r = UPNP_GetExternalIPAddress(urls->controlURL, + servicetype, + externalIPAddress); + if(r != UPNPCOMMAND_SUCCESS) { + printf("GetExternalIPAddress failed. (errorcode=%d)\n", r); + } else { + printf("ExternalIPAddress = %s\n", externalIPAddress); + } +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 UPNP_GetExternalIPAddress(urls->controlURL, - data->servicetype, - externalIPAddress); + servicetype, + externalIPAddress); if(externalIPAddress[0]) printf("ExternalIPAddress = %s\n", externalIPAddress); else printf("GetExternalIPAddress failed.\n"); +#endif//>=-7 +#endif//>=-4 } void GetConnectionStatus(struct UPNPUrls * urls, - struct IGDdatas * data) + struct IGDdatas * data) { unsigned int bytessent, bytesreceived, packetsreceived, packetssent; +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + const char * servicetype_CIF = data->CIF.servicetype; +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + const char * servicetype_CIF = data->servicetype_CIF; +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif//>=-7 +#endif//>=-2 + DisplayInfos(urls, data); - bytessent = UPNP_GetTotalBytesSent(urls->controlURL_CIF, data->servicetype_CIF); - bytesreceived = UPNP_GetTotalBytesReceived(urls->controlURL_CIF, data->servicetype_CIF); - packetssent = UPNP_GetTotalPacketsSent(urls->controlURL_CIF, data->servicetype_CIF); - packetsreceived = UPNP_GetTotalPacketsReceived(urls->controlURL_CIF, data->servicetype_CIF); + bytessent = UPNP_GetTotalBytesSent(urls->controlURL_CIF, servicetype_CIF); + bytesreceived = UPNP_GetTotalBytesReceived(urls->controlURL_CIF, servicetype_CIF); + packetssent = UPNP_GetTotalPacketsSent(urls->controlURL_CIF, servicetype_CIF); + packetsreceived = UPNP_GetTotalPacketsReceived(urls->controlURL_CIF, servicetype_CIF); printf("Bytes: Sent: %8u\tRecv: %8u\n", bytessent, bytesreceived); printf("Packets: Sent: %8u\tRecv: %8u\n", packetssent, packetsreceived); } @@ -71,7 +177,7 @@ void ListRedirections(struct UPNPUrls * urls, int r; int i = 0; char index[6]; - char intClient[16]; + char intClient[40]; char intPort[6]; char extPort[6]; char protocol[4]; @@ -79,6 +185,16 @@ void ListRedirections(struct UPNPUrls * urls, char enabled[6]; char rHost[64]; char duration[16]; +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + const char * servicetype = data->first.servicetype; +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + const char * servicetype = data->servicetype; +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif//>=-7 +#endif//>=-2 + /*unsigned int num=0; UPNP_GetPortMappingNumberOfEntries(urls->controlURL, data->servicetype, &num); printf("PortMappingNumberOfEntries : %u\n", num);*/ @@ -87,17 +203,22 @@ void ListRedirections(struct UPNPUrls * urls, rHost[0] = '\0'; enabled[0] = '\0'; duration[0] = '\0'; desc[0] = '\0'; extPort[0] = '\0'; intPort[0] = '\0'; intClient[0] = '\0'; - r = UPNP_GetGenericPortMappingEntry(urls->controlURL, data->servicetype, - index, - extPort, intClient, intPort, - protocol, desc, enabled, - rHost, duration); + r = UPNP_GetGenericPortMappingEntry(urls->controlURL, + servicetype, + index, + extPort, intClient, intPort, + protocol, desc, enabled, + rHost, duration); if(r==0) - printf("%02d - %s %s->%s:%s\tenabled=%s leaseDuration=%s\n" + printf("%02d - %s %5s->%s:%-5s" + "\tenabled=%s leaseDuration=%s\n" " desc='%s' rHost='%s'\n", i, protocol, extPort, intClient, intPort, - enabled, duration, - desc, rHost); + enabled, duration, + desc, rHost); + else + printf("GetGenericPortMappingEntry() returned %d (%s)\n", + r, strupnperror(r)); i++; } while(r==0); } @@ -109,17 +230,30 @@ void ListRedirections(struct UPNPUrls * urls, * 4 - get this port mapping from the IGD */ int SetRedirectAndTest(struct UPNPUrls * urls, struct IGDdatas * data, - const char * iaddr, - const char * iport, - const char * eport, - const char * proto) + const char * iaddr, + const char * iport, + const char * eport, + const char * proto, + const char * leaseDuration, + const char * description, + int addAny) { - char externalIPAddress[16]; - char intClient[16]; + char externalIPAddress[40]; + char intClient[40]; char intPort[6]; -// char leaseDuration[] = "3600"; /* 60 mins */ + char reservedPort[6]; + char duration[16]; int r; int ok = 1; +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + const char * servicetype = data->first.servicetype; +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + const char * servicetype = data->servicetype; +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif +#endif if(!iaddr || !iport || !eport || !proto) { @@ -132,56 +266,166 @@ int SetRedirectAndTest(struct UPNPUrls * urls, fprintf(stderr, "invalid protocol\n"); return 0; } - + +#if MINIUPNPC_API_VERSION >= -5//1.0 2007/12/19 + r = UPNP_GetExternalIPAddress(urls->controlURL, + servicetype, + externalIPAddress); + if(r != UPNPCOMMAND_SUCCESS) + printf("GetExternalIPAddress failed. (errorcode=%d)\n", r); + else + printf("ExternalIPAddress = %s\n", externalIPAddress); +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 UPNP_GetExternalIPAddress(urls->controlURL, - data->servicetype, - externalIPAddress); + servicetype, + externalIPAddress); if(externalIPAddress[0]) printf("ExternalIPAddress = %s\n", externalIPAddress); else printf("GetExternalIPAddress failed.\n"); +#endif//>=-7 +#endif//>=-4 -// Unix at the moment! -#if MINIUPNPC_VERSION >= 13 - /* Starting from miniupnpc version 1.2, lease duration parameter is gone */ - r = UPNP_AddPortMapping(urls->controlURL, data->servicetype, - eport, iport, iaddr, 0, proto, NULL); -#else - #if MINIUPNPC_VERSION >= 12 - /* Starting from miniupnpc version 1.2, lease duration parameter is gone */ - r = UPNP_AddPortMapping(urls->controlURL, data->servicetype, - eport, iport, iaddr, 0, proto); - #else - /* The lease parameter is also gone in minupnpc 1.0 */ - r = UPNP_AddPortMapping(urls->controlURL, data->servicetype, - eport, iport, iaddr,0, 0, proto); - #endif +#if MINIUPNPC_API_VERSION >= 11 + if (addAny) { + r = UPNP_AddAnyPortMapping(urls->controlURL, data->first.servicetype, + eport, iport, iaddr, description, + proto, 0, leaseDuration, reservedPort); + if(r==UPNPCOMMAND_SUCCESS) + eport = reservedPort; + else + printf("AddAnyPortMapping(%s, %s, %s) failed with code %d (%s)\n", + eport, iport, iaddr, r, strupnperror(r)); + } else #endif - -// r = UPNP_AddPortMapping(urls->controlURL, data->servicetype, -// eport, iport, iaddr, 0, leaseDuration, proto); - -// r = UPNP_AddPortMapping(urls->controlURL, data->servicetype, -// eport, iport, iaddr, 0, proto); - if(r==0) { - printf("AddPortMapping(%s, %s, %s) failed\n", eport, iport, iaddr); - //this seems to trigger for unknown reasons sometimes. - //rely on Checking it afterwards... - //should check IP address then! - //ok = 0; +#if MINIUPNPC_API_VERSION >= -1 + /* $Id: upnpcommands.h,v 1.30 2015/07/15 12:21:28 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.20 2011/02/15 11:13:22 nanard Exp $ */ + //UPNP_AddPortMapping(const char * controlURL, const char * servicetype, + // const char * extPort, + // const char * inPort, + // const char * inClient, + // const char * desc, + // const char * proto, + // const char * remoteHost, + // const char * leaseDuration); + r = UPNP_AddPortMapping(urls->controlURL, servicetype, + eport, iport, iaddr, NULL, proto, NULL, NULL); +#else +#if MINIUPNPC_API_VERSION >= -3 //1.0 2009/04/17 + /* $Id: upnpcommands.h,v 1.18 2010/06/09 10:59:09 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.17 2009/04/17 21:21:19 nanard Exp $ */ + //UPNP_AddPortMapping(const char * controlURL, const char * servicetype, + // const char * extPort, + // const char * inPort, + // const char * inClient, + // const char * desc, + // const char * proto, + // const char * remoteHost); + r = UPNP_AddPortMapping(urls->controlURL, servicetype, + eport, iport, iaddr, NULL, proto, NULL); +#else +#if MINIUPNPC_API_VERSION >= -7//Before 1.0 + /* $Id: upnpcommands.h,v 1.14 2008/09/25 18:02:50 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.7 2006/07/09 12:00:54 nanard Exp $ */ + //UPNP_AddPortMapping(const char * controlURL, const char * servicetype, + // const char * extPort, + // const char * inPort, + // const char * inClient, + // const char * desc, + // const char * proto); + r = UPNP_AddPortMapping(urls->controlURL, servicetype, + eport, iport, iaddr, NULL, proto); +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif//>=-7 +#endif//>=-3 +#endif//>=-1 + +#if MINIUPNPC_API_VERSION >= -5//2007/12/19 + if(r!=UPNPCOMMAND_SUCCESS){ +#else +#if MINIUPNPC_API_VERSION >= -7//Before 1.0 + if(r==0){ +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif//>=-7 +#endif//>=-5 + printf("AddPortMapping(%s, %s, %s) failed and returns %d\n", eport, iport, iaddr, r); + //this seems to trigger for unknown reasons sometimes. + //rely on Checking it afterwards... + //should check IP address then! + ok = 0; + } } +#if MINIUPNPC_API_VERSION >= 10//1.0 2006/09/04 + /* $Id: upnpcommands.h,v 1.30 2015/07/15 12:21:28 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.26 2014/01/31 13:18:26 nanard Exp $ */ + //UPNP_GetSpecificPortMappingEntry(const char * controlURL, + // const char * servicetype, + // const char * extPort, + // const char * proto, + // const char * remoteHost, + // char * intClient, + // char * intPort, + // char * desc, + // char * enabled, + // char * leaseDuration); + r = UPNP_GetSpecificPortMappingEntry(urls->controlURL, + data->first.servicetype, + eport, proto, NULL/*remoteHost*/, + intClient, intPort, NULL/*desc*/, + NULL/*enabled*/, duration); +#else +#if MINIUPNPC_API_VERSION >= 6//1.0 2006/09/04 + /* $Id: upnpcommands.h,v 1.24 2012/03/05 19:42:47 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.22 2011/03/14 13:36:01 nanard Exp $ */ + //UPNP_GetSpecificPortMappingEntry(const char * controlURL, + // const char * servicetype, + // const char * extPort, + // const char * proto, + // char * intClient, + // char * intPort, + // char * desc, + // char * enabled, + // char * leaseDuration); + r = UPNP_GetSpecificPortMappingEntry(urls->controlURL, + data->first.servicetype, + eport, proto, + intClient, intPort, NULL/*desc*/, + NULL/*enabled*/, duration); +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + /* $Id: upnpcommands.h,v 1.20 2011/02/15 11:13:22 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.7 2006/07/09 12:00:54 nanard Exp $ */ + //UPNP_GetSpecificPortMappingEntry(const char * controlURL, + // const char * servicetype, + // const char * extPort, + // const char * proto, + // char * intClient, + // char * intPort); UPNP_GetSpecificPortMappingEntry(urls->controlURL, - data->servicetype, - eport, proto, - intClient, intPort); - if(intClient[0]) - printf("InternalIP:Port = %s:%s\n", intClient, intPort); - else - { - printf("GetSpecificPortMappingEntry failed.\n"); + servicetype, + eport, + proto, + intClient, + intPort); + if(intClient[0]) r = UPNPCOMMAND_SUCCESS; +#endif//>=-7 +#endif//>=6 +#endif//>=10 + + if(r!=UPNPCOMMAND_SUCCESS) { + printf("GetSpecificPortMappingEntry() failed with code %d (%s)\n", + r, strupnperror(r)); ok = 0; + } else if(intClient[0]) { + printf("InternalIP:Port = %s:%s\n", intClient, intPort); + printf("external %s:%s %s is redirected to internal %s:%s (duration=%s)\n", + externalIPAddress, eport, proto, intClient, intPort, duration); } if ((strcmp(iaddr, intClient) != 0) || (strcmp(iport, intPort) != 0)) @@ -194,9 +438,6 @@ int SetRedirectAndTest(struct UPNPUrls * urls, ok = 0; } - printf("external %s:%s is redirected to internal %s:%s\n", - externalIPAddress, eport, intClient, intPort); - if (ok) { printf("uPnP Forward/Mapping Succeeded\n"); @@ -210,14 +451,17 @@ int SetRedirectAndTest(struct UPNPUrls * urls, } int TestRedirect(struct UPNPUrls * urls, - struct IGDdatas * data, - const char * iaddr, - const char * iport, - const char * eport, - const char * proto) + struct IGDdatas * data, + const char * iaddr, + const char * iport, + const char * eport, + const char * proto) { - char intClient[16]; + char externalIPAddress[40]; + char intClient[40]; char intPort[6]; + char duration[16]; + int r = 0; int ok = 1; if(!iaddr || !iport || !eport || !proto) @@ -231,21 +475,83 @@ int TestRedirect(struct UPNPUrls * urls, fprintf(stderr, "invalid protocol\n"); return 0; } - - UPNP_GetSpecificPortMappingEntry(urls->controlURL, - data->servicetype, - eport, proto, - intClient, intPort); - if(intClient[0]) - printf("uPnP Check: InternalIP:Port = %s:%s\n", intClient, intPort); - else - { - printf("GetSpecificPortMappingEntry failed.\n"); - ok = 0; - } - printf("uPnP Check: External port %s is redirected to internal %s:%s\n", - eport, intClient, intPort); +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + const char * servicetype = data->first.servicetype; +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + const char * servicetype = data->servicetype; +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif +#endif + +#if MINIUPNPC_API_VERSION >= 10//1.0 2006/09/04 + /* $Id: upnpcommands.h,v 1.30 2015/07/15 12:21:28 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.26 2014/01/31 13:18:26 nanard Exp $ */ + //UPNP_GetSpecificPortMappingEntry(const char * controlURL, + // const char * servicetype, + // const char * extPort, + // const char * proto, + // const char * remoteHost, + // char * intClient, + // char * intPort, + // char * desc, + // char * enabled, + // char * leaseDuration); + r = UPNP_GetSpecificPortMappingEntry(urls->controlURL, + servicetype, + eport, proto, NULL/*remoteHost*/, + intClient, intPort, NULL/*desc*/, + NULL/*enabled*/, duration); +#else +#if MINIUPNPC_API_VERSION >= 6//1.0 2006/09/04 + /* $Id: upnpcommands.h,v 1.24 2012/03/05 19:42:47 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.22 2011/03/14 13:36:01 nanard Exp $ */ + //UPNP_GetSpecificPortMappingEntry(const char * controlURL, + // const char * servicetype, + // const char * extPort, + // const char * proto, + // char * intClient, + // char * intPort, + // char * desc, + // char * enabled, + // char * leaseDuration); + r = UPNP_GetSpecificPortMappingEntry(urls->controlURL, + servicetype, + eport, proto, + intClient, intPort, NULL/*desc*/, + NULL/*enabled*/, duration); +#else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 + /* $Id: upnpcommands.h,v 1.20 2011/02/15 11:13:22 nanard Exp $ */ + /* $Id: upnpcommands.h,v 1.7 2006/07/09 12:00:54 nanard Exp $ */ + //UPNP_GetSpecificPortMappingEntry(const char * controlURL, + // const char * servicetype, + // const char * extPort, + // const char * proto, + // char * intClient, + // char * intPort); + UPNP_GetSpecificPortMappingEntry(urls->controlURL, + servicetype, + eport, + proto, + intClient, + intPort); + if(intClient[0]) r = UPNPCOMMAND_SUCCESS; +#endif//>=-7 +#endif//>=6 +#endif//>=10 + + if(r!=UPNPCOMMAND_SUCCESS) { + printf("GetSpecificPortMappingEntry() failed with code %d (%s)\n", + r, strupnperror(r)); + ok = 0; + } else if(intClient[0]) { + printf("uPnP Check: InternalIP:Port = %s:%s\n", intClient, intPort); + printf("external %s:%s %s is redirected to internal %s:%s (duration=%s)\n", + externalIPAddress, eport, proto, intClient, intPort, duration); + } if (ok) { @@ -259,13 +565,11 @@ int TestRedirect(struct UPNPUrls * urls, return ok; } - - int RemoveRedirect(struct UPNPUrls * urls, struct IGDdatas * data, - const char * eport, - const char * proto) + const char * eport, + const char * proto) { if(!proto || !eport) { @@ -278,11 +582,19 @@ RemoveRedirect(struct UPNPUrls * urls, fprintf(stderr, "protocol invalid\n"); return 0; } -#if MINIUPNPC_VERSION >= 13 +#if MINIUPNPC_API_VERSION >= -2//1.4 2010/12/09 + UPNP_DeletePortMapping(urls->controlURL, data->first.servicetype, eport, proto, NULL); +#else +#if MINIUPNPC_API_VERSION >= -3//1.3 2009/04/17 UPNP_DeletePortMapping(urls->controlURL, data->servicetype, eport, proto, NULL); #else +#if MINIUPNPC_API_VERSION >= -7//1.0 2006/09/04 UPNP_DeletePortMapping(urls->controlURL, data->servicetype, eport, proto); -#endif +#else +#error MINIUPNPC_API_VERSION is not defined. You may define one follow miniupnpc library version +#endif//>= -7 +#endif//>= -3 +#endif//>= -2 return 1; } diff --git a/libretroshare/src/upnp/upnputil.h b/libretroshare/src/upnp/upnputil.h index b1bfe6e51..458923f98 100644 --- a/libretroshare/src/upnp/upnputil.h +++ b/libretroshare/src/upnp/upnputil.h @@ -20,6 +20,7 @@ #include #include #include +#include /* Ensure linking names are okay on OSX platform. (C interface) */ @@ -41,11 +42,14 @@ void ListRedirections(struct UPNPUrls * urls, struct IGDdatas * data); int SetRedirectAndTest(struct UPNPUrls * urls, - struct IGDdatas * data, - const char * iaddr, - const char * iport, - const char * eport, - const char * proto); + struct IGDdatas * data, + const char * iaddr, + const char * iport, + const char * eport, + const char * proto, + const char *leaseDuration, + const char *description, + int addAny); int TestRedirect(struct UPNPUrls * urls, struct IGDdatas * data,