ZeroTierOneService.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2016 ZeroTier, Inc. https://www.zerotier.com/
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #pragma region Includes
  19. #include <WinSock2.h>
  20. #include <Windows.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "ZeroTierOneService.h"
  24. #include "../../version.h"
  25. #include "../../include/ZeroTierOne.h"
  26. #include "../../node/Constants.hpp"
  27. #include "../../node/Utils.hpp"
  28. #include "../../osdep/OSUtils.hpp"
  29. #include "../../service/OneService.hpp"
  30. #pragma endregion // Includes
  31. #ifdef ZT_DEBUG_SERVICE
  32. FILE *SVCDBGfile = (FILE *)0;
  33. ZeroTier::Mutex SVCDBGfile_m;
  34. #endif
  35. ZeroTierOneService::ZeroTierOneService() :
  36. CServiceBase(ZT_SERVICE_NAME,TRUE,TRUE,FALSE),
  37. _service((ZeroTier::OneService *)0)
  38. {
  39. #ifdef ZT_DEBUG_SERVICE
  40. SVCDBGfile_m.lock();
  41. if (!SVCDBGfile)
  42. SVCDBGfile = fopen(ZT_DEBUG_SERVICE,"a");
  43. SVCDBGfile_m.unlock();
  44. #endif
  45. ZT_SVCDBG("ZeroTierOneService::ZeroTierOneService()\r\n");
  46. }
  47. ZeroTierOneService::~ZeroTierOneService(void)
  48. {
  49. ZT_SVCDBG("ZeroTierOneService::~ZeroTierOneService()\r\n");
  50. #ifdef ZT_DEBUG_SERVICE
  51. SVCDBGfile_m.lock();
  52. if (SVCDBGfile) {
  53. fclose(SVCDBGfile);
  54. SVCDBGfile = (FILE *)0;
  55. }
  56. SVCDBGfile_m.unlock();
  57. #endif
  58. }
  59. void ZeroTierOneService::threadMain()
  60. throw()
  61. {
  62. ZT_SVCDBG("ZeroTierOneService::threadMain()\r\n");
  63. restart_node:
  64. try {
  65. {
  66. ZeroTier::Mutex::Lock _l(_lock);
  67. delete _service;
  68. _service = (ZeroTier::OneService *)0; // in case newInstance() fails
  69. _service = ZeroTier::OneService::newInstance(
  70. ZeroTier::OneService::platformDefaultHomePath().c_str(),
  71. ZT_DEFAULT_PORT);
  72. }
  73. switch(_service->run()) {
  74. case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR: {
  75. std::string err("ZeroTier One encountered an unrecoverable error: ");
  76. err.append(_service->fatalErrorMessage());
  77. err.append(" (restarting in 5 seconds)");
  78. WriteEventLogEntry(const_cast <PSTR>(err.c_str()),EVENTLOG_ERROR_TYPE);
  79. Sleep(5000);
  80. } goto restart_node;
  81. case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
  82. std::string homeDir(ZeroTier::OneService::platformDefaultHomePath());
  83. delete _service;
  84. _service = (ZeroTier::OneService *)0;
  85. std::string oldid;
  86. ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
  87. if (oldid.length()) {
  88. ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
  89. ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
  90. ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
  91. }
  92. } goto restart_node;
  93. default: // normal termination
  94. break;
  95. }
  96. } catch ( ... ) {
  97. // sanity check, shouldn't happen since Node::run() should catch all its own errors
  98. // could also happen if we're out of memory though!
  99. WriteEventLogEntry("unexpected exception (out of memory?) (trying again in 5 seconds)",EVENTLOG_ERROR_TYPE);
  100. Sleep(5000);
  101. goto restart_node;
  102. }
  103. {
  104. ZeroTier::Mutex::Lock _l(_lock);
  105. delete _service;
  106. _service = (ZeroTier::OneService *)0;
  107. }
  108. }
  109. void ZeroTierOneService::OnStart(DWORD dwArgc, LPSTR *lpszArgv)
  110. {
  111. ZT_SVCDBG("ZeroTierOneService::OnStart()\r\n");
  112. try {
  113. _thread = ZeroTier::Thread::start(this);
  114. } catch ( ... ) {
  115. throw (DWORD)ERROR_EXCEPTION_IN_SERVICE;
  116. }
  117. }
  118. void ZeroTierOneService::OnStop()
  119. {
  120. ZT_SVCDBG("ZeroTierOneService::OnStop()\r\n");
  121. _lock.lock();
  122. ZeroTier::OneService *s = _service;
  123. _lock.unlock();
  124. if (s) {
  125. s->terminate();
  126. ZeroTier::Thread::join(_thread);
  127. }
  128. }
  129. void ZeroTierOneService::OnShutdown()
  130. {
  131. ZT_SVCDBG("ZeroTierOneService::OnShutdown()\r\n");
  132. // stop thread on system shutdown (if it hasn't happened already)
  133. OnStop();
  134. }