diff --git a/one.cpp b/one.cpp index c75542e4c..54145d037 100644 --- a/one.cpp +++ b/one.cpp @@ -32,6 +32,8 @@ #include #include +#include "node/Constants.hpp" + #ifdef __WINDOWS__ #include #include @@ -60,7 +62,6 @@ #include "ext/json-parser/json.h" -#include "node/Constants.hpp" #include "node/Identity.hpp" #include "node/CertificateOfMembership.hpp" #include "node/Utils.hpp" @@ -499,7 +500,7 @@ static int cli(int argc,char **argv) cliPrintHelp(argv[0],stderr); return 2; } - unsigned int scode = Http::DELETE( + unsigned int scode = Http::DEL( 1024 * 1024 * 16, 60000, (const struct sockaddr *)&addr, @@ -731,9 +732,9 @@ static BOOL WINAPI _winConsoleCtrlHandler(DWORD dwCtrlType) case CTRL_BREAK_EVENT: case CTRL_CLOSE_EVENT: case CTRL_SHUTDOWN_EVENT: - Node *n = node; - if (n) - n->terminate(Node::NODE_NORMAL_TERMINATION,"terminated by signal"); + OneService *s = zt1Service; + if (s) + s->terminate(); return TRUE; } return FALSE; diff --git a/windows/ZeroTierOne/ZeroTierOne.vcxproj b/windows/ZeroTierOne/ZeroTierOne.vcxproj index 2dfc4433e..796c30d94 100644 --- a/windows/ZeroTierOne/ZeroTierOne.vcxproj +++ b/windows/ZeroTierOne/ZeroTierOne.vcxproj @@ -43,12 +43,21 @@ + - + + true + true + true + true + + + + @@ -102,6 +111,9 @@ + + + diff --git a/windows/ZeroTierOne/ZeroTierOne.vcxproj.filters b/windows/ZeroTierOne/ZeroTierOne.vcxproj.filters index be0997454..7ed585ad5 100644 --- a/windows/ZeroTierOne/ZeroTierOne.vcxproj.filters +++ b/windows/ZeroTierOne/ZeroTierOne.vcxproj.filters @@ -58,6 +58,18 @@ {da28e961-1761-41d8-9a59-65b00dfb1302} + + {43f75f84-c70d-4d44-a0ef-28a7a399abd4} + + + {0da07a2f-8922-4827-ac51-29ca3f30f881} + + + {b74916eb-bb6c-4449-a2a2-fa0b17f60121} + + + {bf604491-14c4-4a74-81a6-6105d07c5c7c} + @@ -150,6 +162,18 @@ Source Files\ext\json-parser + + Source Files + + + Source Files\windows\ZeroTierOne + + + Source Files\windows\ZeroTierOne + + + Source Files\windows\ZeroTierOne + @@ -305,6 +329,15 @@ Header Files\ext\http-parser + + Header Files\windows\ZeroTierOne + + + Header Files\windows\ZeroTierOne + + + Header Files\windows\ZeroTierOne + diff --git a/windows/ZeroTierOne/ZeroTierOneService.cpp b/windows/ZeroTierOne/ZeroTierOneService.cpp index abae3affc..e5426dc27 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.cpp +++ b/windows/ZeroTierOne/ZeroTierOneService.cpp @@ -34,15 +34,13 @@ #include "ZeroTierOneService.h" -#include "../../node/Defaults.hpp" +#include "../../version.h" +#include "../../include/ZeroTierOne.h" + +#include "../../node/Constants.hpp" #include "../../node/Utils.hpp" - -#include "../../control/NodeControlClient.hpp" -#include "../../control/NodeControlService.hpp" - -#include "../../osdep/WindowsEthernetTapFactory.hpp" -#include "../../osdep/WindowsRoutingTable.hpp" -#include "../../osdep/NativeSocketManager.hpp" +#include "../../osdep/OSUtils.hpp" +#include "../../service/OneService.hpp" #pragma endregion // Includes @@ -53,7 +51,7 @@ ZeroTier::Mutex SVCDBGfile_m; ZeroTierOneService::ZeroTierOneService() : CServiceBase(ZT_SERVICE_NAME,TRUE,TRUE,FALSE), - _node((ZeroTier::Node *)0) + _service((ZeroTier::OneService *)0) { #ifdef ZT_DEBUG_SERVICE SVCDBGfile_m.lock(); @@ -86,6 +84,41 @@ void ZeroTierOneService::threadMain() restart_node: try { + { + ZeroTier::Mutex::Lock _l(_lock); + delete _service; + _service = (ZeroTier::OneService *)0; // in case newInstance() fails + _service = ZeroTier::OneService::newInstance( + ZeroTier::OneService::platformDefaultHomePath().c_str(), + ZT1_DEFAULT_PORT); + } + switch(_service->run()) { + case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR: { + std::string err("ZeroTier One encountered an unrecoverable error: "); + err.append(_service->fatalErrorMessage()); + err.append(" (restarting in 5 seconds)"); + WriteEventLogEntry(const_cast (err.c_str()),EVENTLOG_ERROR_TYPE); + Sleep(5000); + } goto restart_node; + + case ZeroTier::OneService::ONE_IDENTITY_COLLISION: { + std::string homeDir(ZeroTier::OneService::platformDefaultHomePath()); + delete _service; + _service = (ZeroTier::OneService *)0; + std::string oldid; + ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid); + if (oldid.length()) { + ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid); + ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str()); + ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str()); + } + } goto restart_node; + + default: // normal termination + break; + } + +#if 0 std::string authToken(ZeroTier::NodeControlClient::getAuthToken((ZeroTier::ZT_DEFAULTS.defaultHomePath + ZT_PATH_SEPARATOR_S + "authtoken.secret").c_str(),true)); ZeroTier::WindowsEthernetTapFactory tapFactory(ZeroTier::ZT_DEFAULTS.defaultHomePath.c_str()); @@ -154,6 +187,7 @@ restart_node: break; } +#endif } catch ( ... ) { // sanity check, shouldn't happen since Node::run() should catch all its own errors // could also happen if we're out of memory though! @@ -164,17 +198,19 @@ restart_node: { ZeroTier::Mutex::Lock _l(_lock); - delete _node; - _node = (ZeroTier::Node *)0; + delete _service; + _service = (ZeroTier::OneService *)0; } } bool ZeroTierOneService::doStartUpgrade(const std::string &msiPath) { - std::string msiLog(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\LastUpdateLog.txt"); - ZeroTier::Utils::rm(msiLog); + std::string homePath(ZeroTier::OneService::platformDefaultHomePath()); - std::string bat(ZeroTier::ZT_DEFAULTS.defaultHomePath + "\\InstallAndRestartService.bat"); + std::string msiLog(homePath + "\\LastUpdateLog.txt"); + ZeroTier::OSUtils::rm(msiLog); + + std::string bat(homePath + "\\InstallAndRestartService.bat"); FILE *batf = fopen(bat.c_str(),"wb"); if (!batf) return false; @@ -210,10 +246,11 @@ void ZeroTierOneService::OnStop() ZT_SVCDBG("ZeroTierOneService::OnStop()\r\n"); _lock.lock(); - ZeroTier::Node *n = _node; + ZeroTier::OneService *s = _service; _lock.unlock(); - if (n) { - n->terminate(ZeroTier::Node::NODE_NORMAL_TERMINATION,"Windows service stopped"); + + if (s) { + s->terminate(); ZeroTier::Thread::join(_thread); } } diff --git a/windows/ZeroTierOne/ZeroTierOneService.h b/windows/ZeroTierOne/ZeroTierOneService.h index bfc835f27..1b97fd46e 100644 --- a/windows/ZeroTierOne/ZeroTierOneService.h +++ b/windows/ZeroTierOne/ZeroTierOneService.h @@ -33,11 +33,9 @@ #include -#include "../../node/Node.hpp" -#include "../../node/Defaults.hpp" -#include "../../node/Thread.hpp" #include "../../node/Mutex.hpp" -#include "../../node/Utils.hpp" +#include "../../osdep/Thread.hpp" +#include "../../service/OneService.hpp" // Uncomment to make debugging Windows services suck slightly less hard. //#define ZT_DEBUG_SERVICE "C:\\ZeroTierOneServiceDebugLog.txt" @@ -79,7 +77,7 @@ protected: virtual void OnShutdown(); private: - ZeroTier::Node *volatile _node; + ZeroTier::OneService *volatile _service; ZeroTier::Mutex _lock; ZeroTier::Thread _thread; };