ZeroTierOneService.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  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. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #pragma region Includes
  28. #include <WinSock2.h>
  29. #include <Windows.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "ZeroTierOneService.h"
  33. #include "../../version.h"
  34. #include "../../include/ZeroTierOne.h"
  35. #include "../../node/Constants.hpp"
  36. #include "../../node/Utils.hpp"
  37. #include "../../osdep/OSUtils.hpp"
  38. #include "../../service/OneService.hpp"
  39. #pragma endregion // Includes
  40. #ifdef ZT_DEBUG_SERVICE
  41. FILE *SVCDBGfile = (FILE *)0;
  42. ZeroTier::Mutex SVCDBGfile_m;
  43. #endif
  44. ZeroTierOneService::ZeroTierOneService() :
  45. CServiceBase(ZT_SERVICE_NAME,TRUE,TRUE,FALSE),
  46. _service((ZeroTier::OneService *)0)
  47. {
  48. #ifdef ZT_DEBUG_SERVICE
  49. SVCDBGfile_m.lock();
  50. if (!SVCDBGfile)
  51. SVCDBGfile = fopen(ZT_DEBUG_SERVICE,"a");
  52. SVCDBGfile_m.unlock();
  53. #endif
  54. ZT_SVCDBG("ZeroTierOneService::ZeroTierOneService()\r\n");
  55. }
  56. ZeroTierOneService::~ZeroTierOneService(void)
  57. {
  58. ZT_SVCDBG("ZeroTierOneService::~ZeroTierOneService()\r\n");
  59. #ifdef ZT_DEBUG_SERVICE
  60. SVCDBGfile_m.lock();
  61. if (SVCDBGfile) {
  62. fclose(SVCDBGfile);
  63. SVCDBGfile = (FILE *)0;
  64. }
  65. SVCDBGfile_m.unlock();
  66. #endif
  67. }
  68. void ZeroTierOneService::threadMain()
  69. throw()
  70. {
  71. ZT_SVCDBG("ZeroTierOneService::threadMain()\r\n");
  72. restart_node:
  73. try {
  74. {
  75. ZeroTier::Mutex::Lock _l(_lock);
  76. delete _service;
  77. _service = (ZeroTier::OneService *)0; // in case newInstance() fails
  78. _service = ZeroTier::OneService::newInstance(
  79. ZeroTier::OneService::platformDefaultHomePath().c_str(),
  80. ZT1_DEFAULT_PORT);
  81. }
  82. switch(_service->run()) {
  83. case ZeroTier::OneService::ONE_UNRECOVERABLE_ERROR: {
  84. std::string err("ZeroTier One encountered an unrecoverable error: ");
  85. err.append(_service->fatalErrorMessage());
  86. err.append(" (restarting in 5 seconds)");
  87. WriteEventLogEntry(const_cast <PSTR>(err.c_str()),EVENTLOG_ERROR_TYPE);
  88. Sleep(5000);
  89. } goto restart_node;
  90. case ZeroTier::OneService::ONE_IDENTITY_COLLISION: {
  91. std::string homeDir(ZeroTier::OneService::platformDefaultHomePath());
  92. delete _service;
  93. _service = (ZeroTier::OneService *)0;
  94. std::string oldid;
  95. ZeroTier::OSUtils::readFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str(),oldid);
  96. if (oldid.length()) {
  97. ZeroTier::OSUtils::writeFile((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret.saved_after_collision").c_str(),oldid);
  98. ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.secret").c_str());
  99. ZeroTier::OSUtils::rm((homeDir + ZT_PATH_SEPARATOR_S + "identity.public").c_str());
  100. }
  101. } goto restart_node;
  102. default: // normal termination
  103. break;
  104. }
  105. } catch ( ... ) {
  106. // sanity check, shouldn't happen since Node::run() should catch all its own errors
  107. // could also happen if we're out of memory though!
  108. WriteEventLogEntry("unexpected exception (out of memory?) (trying again in 5 seconds)",EVENTLOG_ERROR_TYPE);
  109. Sleep(5000);
  110. goto restart_node;
  111. }
  112. {
  113. ZeroTier::Mutex::Lock _l(_lock);
  114. delete _service;
  115. _service = (ZeroTier::OneService *)0;
  116. }
  117. }
  118. void ZeroTierOneService::OnStart(DWORD dwArgc, LPSTR *lpszArgv)
  119. {
  120. ZT_SVCDBG("ZeroTierOneService::OnStart()\r\n");
  121. try {
  122. _thread = ZeroTier::Thread::start(this);
  123. } catch ( ... ) {
  124. throw (DWORD)ERROR_EXCEPTION_IN_SERVICE;
  125. }
  126. }
  127. void ZeroTierOneService::OnStop()
  128. {
  129. ZT_SVCDBG("ZeroTierOneService::OnStop()\r\n");
  130. _lock.lock();
  131. ZeroTier::OneService *s = _service;
  132. _lock.unlock();
  133. if (s) {
  134. s->terminate();
  135. ZeroTier::Thread::join(_thread);
  136. }
  137. }
  138. void ZeroTierOneService::OnShutdown()
  139. {
  140. ZT_SVCDBG("ZeroTierOneService::OnShutdown()\r\n");
  141. // stop thread on system shutdown (if it hasn't happened already)
  142. OnStop();
  143. }