diff options
author | Grant Limberg <glimberg@gmail.com> | 2015-07-07 19:24:02 -0700 |
---|---|---|
committer | Grant Limberg <glimberg@gmail.com> | 2015-07-07 19:24:02 -0700 |
commit | 1ad2cfeedfa2a9f4fc1f512e1009e5bb1b0630cb (patch) | |
tree | d8264c9bd4f6b9dce3f5237b94fe1f2fc521cf19 /service | |
parent | 6d398beefddb48d91f27e5f41bf39f40eb77222f (diff) | |
parent | 412389ec755528108e0254e75a9cf43fc53e331a (diff) | |
download | infinitytier-1ad2cfeedfa2a9f4fc1f512e1009e5bb1b0630cb.tar.gz infinitytier-1ad2cfeedfa2a9f4fc1f512e1009e5bb1b0630cb.zip |
Merge branch 'adamierymenko-dev' into android-jni
Diffstat (limited to 'service')
-rw-r--r-- | service/OneService.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/service/OneService.cpp b/service/OneService.cpp index 7532bf75..bde59d56 100644 --- a/service/OneService.cpp +++ b/service/OneService.cpp @@ -75,11 +75,17 @@ class SqliteNetworkController; #endif // ZT_ENABLE_NETWORK_CONTROLLER #ifdef __WINDOWS__ +#include <WinSock2.h> +#include <Windows.h> #include <ShlObj.h> +#include <netioapi.h> +#include <iphlpapi.h> #else #include <sys/types.h> +#include <sys/socket.h> #include <sys/wait.h> #include <unistd.h> +#include <ifaddrs.h> #endif // Include the right tap device driver for this platform -- add new platforms here @@ -123,6 +129,9 @@ namespace ZeroTier { typedef BSDEthernetTap EthernetTap; } // Attempt to engage TCP fallback after this many ms of no reply to packets sent to global-scope IPs #define ZT1_TCP_FALLBACK_AFTER 60000 +// How often to check for local interface addresses +#define ZT1_LOCAL_INTERFACE_CHECK_INTERVAL 300000 + namespace ZeroTier { namespace { @@ -405,6 +414,7 @@ public: _nextBackgroundTaskDeadline(0), _tcpFallbackTunnel((TcpConnection *)0), _termReason(ONE_STILL_RUNNING), + _port(port), _run(true) { struct sockaddr_in in4; @@ -501,6 +511,7 @@ public: _lastRestart = clockShouldBe; uint64_t lastTapMulticastGroupCheck = 0; uint64_t lastTcpFallbackResolve = 0; + uint64_t lastLocalInterfaceAddressCheck = 0; #ifdef ZT_AUTO_UPDATE uint64_t lastSoftwareUpdateCheck = 0; #endif // ZT_AUTO_UPDATE @@ -554,6 +565,77 @@ public: } } + if ((now - lastLocalInterfaceAddressCheck) >= ZT1_LOCAL_INTERFACE_CHECK_INTERVAL) { + lastLocalInterfaceAddressCheck = now; + +#ifdef __UNIX_LIKE__ + std::vector<std::string> ztDevices; + { + Mutex::Lock _l(_taps_m); + for(std::map< uint64_t,EthernetTap *>::const_iterator t(_taps.begin());t!=_taps.end();++t) + ztDevices.push_back(t->second->deviceName()); + } + + 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)) { + bool isZT = false; + for(std::vector<std::string>::const_iterator d(ztDevices.begin());d!=ztDevices.end();++d) { + if (*d == ifa->ifa_name) { + isZT = true; + break; + } + } + if (!isZT) { + InetAddress ip(ifa->ifa_addr); + ip.setPort(_port); + _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&ip),0,ZT1_LOCAL_INTERFACE_ADDRESS_TRUST_NORMAL,0); + } + } + ifa = ifa->ifa_next; + } + freeifaddrs(ifatbl); + } +#endif // __UNIX_LIKE__ + +#ifdef __WINDOWS__ + std::vector<NET_LUID> ztDevices; + { + Mutex::Lock _l(_taps_m); + for(std::map< uint64_t,EthernetTap *>::const_iterator t(_taps.begin());t!=_taps.end();++t) + ztDevices.push_back(t->second->luid()); + } + + char aabuf[16384]; + ULONG aalen = sizeof(aabuf); + if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) { + PIP_ADAPTER_ADDRESSES a = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf); + while (a) { + bool isZT = false; + for(std::vector<NET_LUID>::const_iterator d(ztDevices.begin());d!=ztDevices.end();++d) { + if (a->Luid.Value == d->Value) { + isZT = true; + break; + } + } + if (!isZT) { + PIP_ADAPTER_UNICAST_ADDRESS ua = a->FirstUnicastAddress; + while (ua) { + InetAddress ip(ua->Address.lpSockaddr); + ip.setPort(_port); + _node->addLocalInterfaceAddress(reinterpret_cast<const struct sockaddr_storage *>(&ip),0,ZT1_LOCAL_INTERFACE_ADDRESS_TRUST_NORMAL,0); + ua = ua->Next; + } + } + a = a->Next; + } + } +#endif // __WINDOWS__ + } + const unsigned long delay = (dl > now) ? (unsigned long)(dl - now) : 100; clockShouldBe = now + (uint64_t)delay; _phy.poll(delay); @@ -1158,6 +1240,8 @@ private: std::string _fatalErrorMessage; Mutex _termReason_m; + unsigned int _port; + bool _run; Mutex _run_m; }; |