UPNP/NAT-PMP support with libminiupnpc (if built with it) -- GitHub issue #64

This commit is contained in:
Adam Ierymenko 2015-07-28 14:32:02 -07:00
parent d2bfdfa6e7
commit fe6d5b1402
4 changed files with 308 additions and 8 deletions

View file

@ -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<InetAddress> upnpAddresses(_upnpClient.get());
for(std::vector<InetAddress>::const_iterator ext(upnpAddresses.begin());ext!=upnpAddresses.end();++ext) {
printf("Adding UPNP address: %s\n",ext->toString().c_str());
_node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&(*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;
};