mirror of
https://github.com/ZeroTier/ZeroTierOne
synced 2025-08-21 05:43:59 -07:00
Add stubs to easily build and include all of libnatpmp and miniupnpc in osdep/
This commit is contained in:
parent
c8ca1cfa3c
commit
cfbd7e3bf5
26 changed files with 82 additions and 1931 deletions
|
@ -9,6 +9,8 @@ set(src
|
|||
NeighborDiscovery.cpp
|
||||
OSUtils.cpp
|
||||
PortMapper.cpp
|
||||
PortMapper-miniupnpc.c
|
||||
PortMapper-libnatpmp.c
|
||||
)
|
||||
|
||||
set(headers
|
||||
|
@ -56,5 +58,3 @@ if(APPLE)
|
|||
add_executable(MacEthernetTapAgent MacEthernetTapAgent.c MacEthernetTapAgent.h)
|
||||
target_include_directories(MacEthernetTapAgent PRIVATE ${CMAKE_BINARY_DIR})
|
||||
endif(APPLE)
|
||||
|
||||
|
||||
|
|
14
osdep/PortMapper-libnatpmp.c
Normal file
14
osdep/PortMapper-libnatpmp.c
Normal file
|
@ -0,0 +1,14 @@
|
|||
#define ENABLE_STRNATPMPERR
|
||||
#define _BSD_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _XOPEN_SOURCE 600
|
||||
|
||||
#ifdef __APPLE__
|
||||
#ifndef _DARWIN_C_SOURCE
|
||||
#define _DARWIN_C_SOURCE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "../ext/libnatpmp/getgateway.c"
|
||||
#include "../ext/libnatpmp/wingettimeofday.c"
|
||||
#include "../ext/libnatpmp/natpmp.c"
|
41
osdep/PortMapper-miniupnpc.c
Normal file
41
osdep/PortMapper-miniupnpc.c
Normal file
|
@ -0,0 +1,41 @@
|
|||
#define MINIUPNP_STATICLIB
|
||||
#define MINIUPNPC_SET_SOCKET_TIMEOUT
|
||||
#define MINIUPNPC_GET_SRC_ADDR
|
||||
#define _BSD_SOURCE
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _XOPEN_SOURCE 600
|
||||
#define MINIUPNPC_VERSION_STRING "2.0"
|
||||
#define UPNP_VERSION_STRING "UPnP/1.1"
|
||||
|
||||
#ifdef __LINUX__
|
||||
#define OS_STRING "Linux"
|
||||
#endif
|
||||
#ifdef __APPLE__
|
||||
#define OS_STRING "Darwin"
|
||||
#endif
|
||||
#ifdef __WINDOWS__
|
||||
#define OS_STRING "Windows"
|
||||
#endif
|
||||
#ifndef OS_STRING
|
||||
#define OS_STRING "ZeroTier"
|
||||
#endif
|
||||
|
||||
#ifdef __APPLE__
|
||||
#ifndef _DARWIN_C_SOURCE
|
||||
#define _DARWIN_C_SOURCE
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "../ext/miniupnpc/connecthostport.c"
|
||||
#include "../ext/miniupnpc/igd_desc_parse.c"
|
||||
#include "../ext/miniupnpc/minisoap.c"
|
||||
#include "../ext/miniupnpc/miniupnpc.c"
|
||||
#include "../ext/miniupnpc/miniwget.c"
|
||||
#include "../ext/miniupnpc/minixml.c"
|
||||
#include "../ext/miniupnpc/portlistingparse.c"
|
||||
#include "../ext/miniupnpc/receivedata.c"
|
||||
#include "../ext/miniupnpc/upnpcommands.c"
|
||||
#include "../ext/miniupnpc/upnpdev.c"
|
||||
#include "../ext/miniupnpc/upnperrors.c"
|
||||
#include "../ext/miniupnpc/upnpreplyparse.c"
|
||||
#include "../ext/miniupnpc/minissdpc.c"
|
|
@ -24,8 +24,6 @@
|
|||
* of your own application.
|
||||
*/
|
||||
|
||||
#ifdef ZT_USE_MINIUPNPC
|
||||
|
||||
// Uncomment to dump debug messages
|
||||
//#define ZT_PORTMAPPER_TRACE 1
|
||||
|
||||
|
@ -125,7 +123,7 @@ public:
|
|||
mode = 1;
|
||||
closenatpmp(&natpmp);
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: NAT-PMP: init failed, switching to UPnP mode" ZT_EOL_S);
|
||||
PM_TRACE("PortMapper: NAT-PMP: init failed, switching to UPnP mode" ZT_EOL_S);
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
@ -148,7 +146,7 @@ public:
|
|||
publicAddress = InetAddress((uint32_t)response.pnu.publicaddress.addr.s_addr,0);
|
||||
} else {
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: NAT-PMP: request for external address failed, aborting..." ZT_EOL_S);
|
||||
PM_TRACE("PortMapper: NAT-PMP: request for external address failed, aborting..." ZT_EOL_S);
|
||||
#endif
|
||||
closenatpmp(&natpmp);
|
||||
break;
|
||||
|
@ -170,8 +168,8 @@ public:
|
|||
if (r == 0) {
|
||||
publicAddress.setPort(response.pnu.newportmapping.mappedpublicport);
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
char paddr[128];
|
||||
PM_TRACE("PortMapper: NAT-PMP: mapped %u to %s" ZT_EOL_S,(unsigned int)localPort,publicAddress.toString(paddr));
|
||||
char paddr[128];
|
||||
PM_TRACE("PortMapper: NAT-PMP: mapped %u to %s" ZT_EOL_S,(unsigned int)localPort,publicAddress.toString(paddr));
|
||||
#endif
|
||||
Mutex::Lock sl(surface_l);
|
||||
surface.clear();
|
||||
|
@ -188,7 +186,7 @@ public:
|
|||
if (!natPmpSuccess) {
|
||||
mode = 1;
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: NAT-PMP: request failed, switching to UPnP mode" ZT_EOL_S);
|
||||
PM_TRACE("PortMapper: NAT-PMP: request failed, switching to UPnP mode" ZT_EOL_S);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -213,7 +211,7 @@ public:
|
|||
{
|
||||
UPNPDev *dev = devlist;
|
||||
while (dev) {
|
||||
PM_TRACE("PortMapper: found UPnP device at URL '%s': %s" ZT_EOL_S,dev->descURL,dev->st);
|
||||
PM_TRACE("PortMapper: found UPnP device at URL '%s': %s" ZT_EOL_S,dev->descURL,dev->st);
|
||||
dev = dev->pNext;
|
||||
}
|
||||
}
|
||||
|
@ -227,11 +225,11 @@ public:
|
|||
|
||||
if ((UPNP_GetValidIGD(devlist,&urls,&data,lanaddr,sizeof(lanaddr)))&&(lanaddr[0])) {
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: my LAN IP address: %s" ZT_EOL_S,lanaddr);
|
||||
PM_TRACE("PortMapper: UPnP: my LAN IP address: %s" ZT_EOL_S,lanaddr);
|
||||
#endif
|
||||
if ((UPNP_GetExternalIPAddress(urls.controlURL,data.first.servicetype,externalip) == UPNPCOMMAND_SUCCESS)&&(externalip[0])) {
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: my external IP address: %s" ZT_EOL_S,externalip);
|
||||
PM_TRACE("PortMapper: UPnP: my external IP address: %s" ZT_EOL_S,externalip);
|
||||
#endif
|
||||
|
||||
for(int tries=0;tries<60;++tries) {
|
||||
|
@ -257,7 +255,7 @@ public:
|
|||
memset(haveLeaseDuration,0,sizeof(haveLeaseDuration));
|
||||
if ((UPNP_GetSpecificPortMappingEntry(urls.controlURL,data.first.servicetype,outport,"UDP",(const char *)0,haveIntClient,haveIntPort,haveDesc,haveEnabled,haveLeaseDuration) == UPNPCOMMAND_SUCCESS)&&(uniqueName == haveDesc)) {
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: reusing previously reserved external port: %s" ZT_EOL_S,outport);
|
||||
PM_TRACE("PortMapper: UPnP: reusing previously reserved external port: %s" ZT_EOL_S,outport);
|
||||
#endif
|
||||
Mutex::Lock sl(surface_l);
|
||||
surface.clear();
|
||||
|
@ -272,7 +270,7 @@ public:
|
|||
int mapResult = 0;
|
||||
if ((mapResult = UPNP_AddPortMapping(urls.controlURL,data.first.servicetype,outport,inport,lanaddr,uniqueName.c_str(),"UDP",(const char *)0,"0")) == UPNPCOMMAND_SUCCESS) {
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: reserved external port: %s" ZT_EOL_S,outport);
|
||||
PM_TRACE("PortMapper: UPnP: reserved external port: %s" ZT_EOL_S,outport);
|
||||
#endif
|
||||
Mutex::Lock sl(surface_l);
|
||||
surface.clear();
|
||||
|
@ -282,7 +280,7 @@ public:
|
|||
break;
|
||||
} else {
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: UPNP_AddPortMapping(%s) failed: %d" ZT_EOL_S,outport,mapResult);
|
||||
PM_TRACE("PortMapper: UPnP: UPNP_AddPortMapping(%s) failed: %d" ZT_EOL_S,outport,mapResult);
|
||||
#endif
|
||||
Thread::sleep(1000);
|
||||
}
|
||||
|
@ -291,13 +289,13 @@ public:
|
|||
} else {
|
||||
mode = 0;
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: UPNP_GetExternalIPAddress failed, returning to NAT-PMP mode" ZT_EOL_S);
|
||||
PM_TRACE("PortMapper: UPnP: UPNP_GetExternalIPAddress failed, returning to NAT-PMP mode" ZT_EOL_S);
|
||||
#endif
|
||||
}
|
||||
} else {
|
||||
mode = 0;
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: UPnP: UPNP_GetValidIGD failed, returning to NAT-PMP mode" ZT_EOL_S);
|
||||
PM_TRACE("PortMapper: UPnP: UPNP_GetValidIGD failed, returning to NAT-PMP mode" ZT_EOL_S);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -306,14 +304,14 @@ public:
|
|||
} else {
|
||||
mode = 0;
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("PortMapper: upnpDiscover failed, returning to NAT-PMP mode: %d" ZT_EOL_S,upnpError);
|
||||
PM_TRACE("PortMapper: upnpDiscover failed, returning to NAT-PMP mode: %d" ZT_EOL_S,upnpError);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
#ifdef ZT_PORTMAPPER_TRACE
|
||||
PM_TRACE("UPNPClient: rescanning in %d ms" ZT_EOL_S,ZT_PORTMAPPER_REFRESH_DELAY);
|
||||
PM_TRACE("UPNPClient: rescanning in %d ms" ZT_EOL_S,ZT_PORTMAPPER_REFRESH_DELAY);
|
||||
#endif
|
||||
Thread::sleep(ZT_PORTMAPPER_REFRESH_DELAY);
|
||||
}
|
||||
|
@ -347,5 +345,3 @@ std::vector<InetAddress> PortMapper::get() const
|
|||
}
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif // ZT_USE_MINIUPNPC
|
||||
|
|
|
@ -24,8 +24,6 @@
|
|||
* of your own application.
|
||||
*/
|
||||
|
||||
#ifdef ZT_USE_MINIUPNPC
|
||||
|
||||
#ifndef ZT_PORTMAPPER_HPP
|
||||
#define ZT_PORTMAPPER_HPP
|
||||
|
||||
|
@ -39,7 +37,7 @@
|
|||
/**
|
||||
* How frequently should we refresh our UPNP/NAT-PnP/whatever state?
|
||||
*/
|
||||
#define ZT_PORTMAPPER_REFRESH_DELAY 300000
|
||||
#define ZT_PORTMAPPER_REFRESH_DELAY 120000
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
|
@ -75,5 +73,3 @@ private:
|
|||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
||||
|
||||
#endif // ZT_USE_MINIUPNPC
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue