From fe6d5b1402307e1be80e6d78b765cf5175288c1f Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Tue, 28 Jul 2015 14:32:02 -0700 Subject: UPNP/NAT-PMP support with libminiupnpc (if built with it) -- GitHub issue #64 --- service/OneService.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'service') diff --git a/service/OneService.cpp b/service/OneService.cpp index b83cd83f..05fdfb90 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -54,6 +54,7 @@ #include "../osdep/OSUtils.hpp" #include "../osdep/Http.hpp" #include "../osdep/BackgroundResolver.hpp" +#include "../osdep/UPNPClient.hpp" #include "OneService.hpp" #include "ControlPlane.hpp" @@ -415,6 +416,9 @@ public: _tcpFallbackTunnel((TcpConnection *)0), _termReason(ONE_STILL_RUNNING), _port(port), +#ifdef ZT_USE_MINIUPNPC + _upnpClient((int)port), +#endif _run(true) { struct sockaddr_in in4; @@ -511,7 +515,7 @@ public: _lastRestart = clockShouldBe; uint64_t lastTapMulticastGroupCheck = 0; uint64_t lastTcpFallbackResolve = 0; - uint64_t lastLocalInterfaceAddressCheck = 0; + uint64_t lastLocalInterfaceAddressCheck = (OSUtils::now() - ZT1_LOCAL_INTERFACE_CHECK_INTERVAL) + 15000; // do this in 15s to give UPnP time to configure and other things time to settle #ifdef ZT_AUTO_UPDATE uint64_t lastSoftwareUpdateCheck = 0; #endif // ZT_AUTO_UPDATE @@ -576,9 +580,18 @@ public: ztDevices.push_back(t->second->deviceName()); } + _node->clearLocalInterfaceAddresses(); + +#ifdef ZT_USE_MINIUPNPC + std::vector upnpAddresses(_upnpClient.get()); + for(std::vector::const_iterator ext(upnpAddresses.begin());ext!=upnpAddresses.end();++ext) { + printf("Adding UPNP address: %s\n",ext->toString().c_str()); + _node->addLocalInterfaceAddress(reinterpret_cast(&(*ext)),0,ZT1_LOCAL_INTERFACE_ADDRESS_TRUST_NORMAL); + } +#endif + struct ifaddrs *ifatbl = (struct ifaddrs *)0; if ((getifaddrs(&ifatbl) == 0)&&(ifatbl)) { - _node->clearLocalInterfaceAddresses(); struct ifaddrs *ifa = ifatbl; while (ifa) { if ((ifa->ifa_name)&&(ifa->ifa_addr)) { @@ -1242,6 +1255,10 @@ private: unsigned int _port; +#ifdef ZT_USE_MINIUPNPC + UPNPClient _upnpClient; +#endif + bool _run; Mutex _run_m; }; -- cgit v1.2.3