diff options
-rw-r--r-- | main.cpp | 4 | ||||
-rw-r--r-- | node/Switch.cpp | 2 | ||||
-rw-r--r-- | windows/ZeroTierOne.sln | 90 | ||||
-rw-r--r-- | windows/ZeroTierOne/ServiceBase.h | 2 | ||||
-rw-r--r-- | windows/ZeroTierOne/ZeroTierOneService.cpp | 83 | ||||
-rw-r--r-- | windows/ZeroTierOne/ZeroTierOneService.h | 14 |
6 files changed, 54 insertions, 141 deletions
@@ -628,6 +628,7 @@ int main(int argc,char **argv) if (!winRunFromCommandLine) { ZeroTierOneService zt1Service; if (CServiceBase::Run(zt1Service) == TRUE) { + // Normal termination of service process return 0; } else { fprintf(stderr,"%s: unable to start service (try -h for help)"ZT_EOL_S,argv[0]); @@ -637,7 +638,6 @@ int main(int argc,char **argv) #endif { int exitCode = 0; - try { node = new Node(homeDir,port,controlPort); switch(node->run()) { @@ -669,11 +669,9 @@ int main(int argc,char **argv) fprintf(stderr,"%s: unexpected exception!"ZT_EOL_S,argv[0]); exitCode = 3; } - #ifdef __UNIX_LIKE__ Utils::rm((std::string(homeDir)+"/zerotier-one.pid").c_str()); #endif - return exitCode; } } diff --git a/node/Switch.cpp b/node/Switch.cpp index a759a01b..e6a8e6fd 100644 --- a/node/Switch.cpp +++ b/node/Switch.cpp @@ -218,7 +218,7 @@ bool Switch::sendHELLO(const SharedPtr<Peer> &dest,Demarc::Port localPort,const outp.append(now); _r->identity.serialize(outp,false); outp.armor(dest->key(),false); - return _r->demarc->send(localPort,remoteAddr,outp.data(),outp.size(),-1); + return (_r->demarc->send(localPort,remoteAddr,outp.data(),outp.size(),-1) != Demarc::NULL_PORT); } bool Switch::unite(const Address &p1,const Address &p2,bool force) diff --git a/windows/ZeroTierOne.sln b/windows/ZeroTierOne.sln index 7bf5ecc2..9e3710fe 100644 --- a/windows/ZeroTierOne.sln +++ b/windows/ZeroTierOne.sln @@ -12,8 +12,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TapDriver Package", "TapDri EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZeroTierOne", "ZeroTierOne\ZeroTierOne.vcxproj", "{B00A4957-5977-4AC1-9EF4-571DC27EADA2}" EndProject -Project("{6141683F-8A12-4E36-9623-2EB02B2C2303}") = "ZeroTierOneInstaller", "ZeroTierOneInstaller\ZeroTierOneInstaller.isproj", "{C4424647-0581-47D1-918B-575BFF735CAA}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution CD_ROM|Any CPU = CD_ROM|Any CPU @@ -418,94 +416,6 @@ Global {B00A4957-5977-4AC1-9EF4-571DC27EADA2}.Win8 Release|Win32.Build.0 = Release|Win32 {B00A4957-5977-4AC1-9EF4-571DC27EADA2}.Win8 Release|Win32.Deploy.0 = Release|Win32 {B00A4957-5977-4AC1-9EF4-571DC27EADA2}.Win8 Release|x64.ActiveCfg = Release|Win32 - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|Any CPU.ActiveCfg = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|Any CPU.Build.0 = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|Mixed Platforms.ActiveCfg = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|Mixed Platforms.Build.0 = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|Win32.ActiveCfg = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|Win32.Build.0 = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|x64.ActiveCfg = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.CD_ROM|x64.Build.0 = CD_ROM - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|Any CPU.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|Any CPU.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|Mixed Platforms.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|Mixed Platforms.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|Win32.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|Win32.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|x64.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Debug|x64.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|Any CPU.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|Any CPU.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|Mixed Platforms.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|Mixed Platforms.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|Win32.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|Win32.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|x64.ActiveCfg = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.DVD-5|x64.Build.0 = DVD-5 - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Release|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.SingleImage|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Debug|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Vista Release|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Debug|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win7 Release|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Debug|x64.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|Any CPU.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|Any CPU.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|Mixed Platforms.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|Mixed Platforms.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|Win32.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|Win32.Build.0 = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|x64.ActiveCfg = SingleImage - {C4424647-0581-47D1-918B-575BFF735CAA}.Win8 Release|x64.Build.0 = SingleImage EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/windows/ZeroTierOne/ServiceBase.h b/windows/ZeroTierOne/ServiceBase.h index 5288194f..6d62b1f8 100644 --- a/windows/ZeroTierOne/ServiceBase.h +++ b/windows/ZeroTierOne/ServiceBase.h @@ -18,9 +18,9 @@ #pragma once +#include <WinSock2.h> #include <windows.h> - class CServiceBase { public: diff --git a/windows/ZeroTierOne/ZeroTierOneService.cpp b/windows/ZeroTierOne/ZeroTierOneService.cpp index b8277436..6ed95fb5 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.cpp +++ b/windows/ZeroTierOne/ZeroTierOneService.cpp @@ -27,81 +27,84 @@ #pragma region Includes #include "ZeroTierOneService.h" -#include "../../node/Node.hpp" -#include "../../node/Defaults.hpp" -#include "../../node/Thread.hpp" #pragma endregion -using namespace ZeroTier; - ZeroTierOneService::ZeroTierOneService() : CServiceBase(ZT_SERVICE_NAME,TRUE,TRUE,FALSE), - _thread(new Thread()), - _node((Node *)0) + _node((ZeroTier::Node *)0) { } ZeroTierOneService::~ZeroTierOneService(void) { - delete _thread; - delete _node; } void ZeroTierOneService::threadMain() throw() { +restart_node: try { - // Since Windows doesn't auto-restart services, we'll restart the node - // on normal termination. - for(;;) { - switch(_node->run()) { - case Node::NODE_NORMAL_TERMINATION: - delete _node; - _node = new Node(ZT_DEFAULTS.defaultHomePath.c_str(),0,0); - break; // restart - case Node::NODE_RESTART_FOR_UPGRADE: { - } return; // terminate thread - case Node::NODE_UNRECOVERABLE_ERROR: { - std::string err("unrecoverable error: "); - const char *r = _node->reasonForTermination; - if (r) - err.append(r); - else err.append("(unknown error)"); - WriteEventLogEntry(const_cast <PSTR>(err.c_str()),EVENTLOG_ERROR_TYPE); - } return; // terminate thread - default: - break; - } + { + // start or restart + ZeroTier::Mutex::Lock _l(_lock); + delete _node; + _node = new ZeroTier::Node(ZeroTier::ZT_DEFAULTS.defaultHomePath.c_str(),0,0); + } + switch(_node->run()) { + case ZeroTier::Node::NODE_RESTART_FOR_UPGRADE: { + } break; + case ZeroTier::Node::NODE_UNRECOVERABLE_ERROR: { + std::string err("ZeroTier node encountered an unrecoverable error: "); + const char *r = _node->reasonForTermination(); + if (r) + err.append(r); + else err.append("(unknown error)"); + err.append(" (restarting in 5 seconds)"); + WriteEventLogEntry(const_cast <PSTR>(err.c_str()),EVENTLOG_ERROR_TYPE); + Sleep(5000); + goto restart_node; + } break; + default: // includes normal termination, which will terminate thread + break; } } catch ( ... ) { - WriteEventLogEntry("unexpected exception in Node::run() or during restart",EVENTLOG_ERROR_TYPE); + // sanity check, shouldn't happen since Node::run() should catch all its own errors + // could also happen if we're out of memory though! + WriteEventLogEntry("unexpected exception (out of memory?) (trying again in 5 seconds)",EVENTLOG_ERROR_TYPE); + Sleep(5000); + goto restart_node; } + + _lock.lock(); + delete _node; + _node = (ZeroTier::Node *)0; + _lock.unlock(); } void ZeroTierOneService::OnStart(DWORD dwArgc, LPSTR *lpszArgv) { + if (_node) + return; // sanity check try { - _node = new Node(ZT_DEFAULTS.defaultHomePath.c_str(),0,0); - *_thread = Thread::start(this); + _thread = ZeroTier::Thread::start(this); } catch ( ... ) { - // shouldn't happen unless something weird occurs like out of memory... throw (DWORD)ERROR_EXCEPTION_IN_SERVICE; } } void ZeroTierOneService::OnStop() { - Node *n = _node; - _node = (Node *)0; + _lock.lock(); + ZeroTier::Node *n = _node; + _lock.unlock(); if (n) { - n->terminate(Node::NODE_NORMAL_TERMINATION,"Service Shutdown"); - Thread::join(*_thread); - delete n; + n->terminate(ZeroTier::Node::NODE_NORMAL_TERMINATION,"Windows service stopped"); + ZeroTier::Thread::join(_thread); } } void ZeroTierOneService::OnShutdown() { - // make sure it's stopped + // stop thread on system shutdown (if it hasn't happened already) OnStop(); } diff --git a/windows/ZeroTierOne/ZeroTierOneService.h b/windows/ZeroTierOne/ZeroTierOneService.h index 776446b0..3c1f8d08 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.h +++ b/windows/ZeroTierOne/ZeroTierOneService.h @@ -29,6 +29,12 @@ #include "ServiceBase.h" +#include "../../node/Node.hpp" +#include "../../node/Defaults.hpp" +#include "../../node/Thread.hpp" +#include "../../node/Mutex.hpp" +#include "../../node/Utils.hpp" + #define ZT_SERVICE_NAME "ZeroTierOneService" #define ZT_SERVICE_DISPLAY_NAME "ZeroTier One" #define ZT_SERVICE_START_TYPE SERVICE_AUTO_START @@ -36,11 +42,6 @@ #define ZT_SERVICE_ACCOUNT "NT AUTHORITY\\LocalService" #define ZT_SERVICE_PASSWORD NULL -namespace ZeroTier { -class Node; -class Thread; -} // namespace ZeroTier - class ZeroTierOneService : public CServiceBase { public: @@ -60,5 +61,6 @@ protected: private: ZeroTier::Node *volatile _node; - ZeroTier::Thread *volatile _thread; + ZeroTier::Mutex _lock; + ZeroTier::Thread _thread; }; |