Browse Source

Bring Windows service code up to date and into sync.

Adam Ierymenko 10 years ago
parent
commit
e2c65bf16a

+ 6 - 5
one.cpp

@@ -32,6 +32,8 @@
 #include <time.h>
 #include <errno.h>
 
+#include "node/Constants.hpp"
+
 #ifdef __WINDOWS__
 #include <WinSock2.h>
 #include <Windows.h>
@@ -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;

+ 13 - 1
windows/ZeroTierOne/ZeroTierOne.vcxproj

@@ -43,12 +43,21 @@
     <ClCompile Include="..\..\node\Switch.cpp" />
     <ClCompile Include="..\..\node\Topology.cpp" />
     <ClCompile Include="..\..\node\Utils.cpp" />
+    <ClCompile Include="..\..\one.cpp" />
     <ClCompile Include="..\..\osdep\Http.cpp" />
     <ClCompile Include="..\..\osdep\OSUtils.cpp" />
     <ClCompile Include="..\..\osdep\WindowsEthernetTap.cpp" />
-    <ClCompile Include="..\..\selftest.cpp" />
+    <ClCompile Include="..\..\selftest.cpp">
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
+      <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
+    </ClCompile>
     <ClCompile Include="..\..\service\ControlPlane.cpp" />
     <ClCompile Include="..\..\service\OneService.cpp" />
+    <ClCompile Include="ServiceBase.cpp" />
+    <ClCompile Include="ServiceInstaller.cpp" />
+    <ClCompile Include="ZeroTierOneService.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\ext\http-parser\http_parser.h" />
@@ -102,6 +111,9 @@
     <ClInclude Include="..\..\service\OneService.hpp" />
     <ClInclude Include="..\..\version.h" />
     <ClInclude Include="resource.h" />
+    <ClInclude Include="ServiceBase.h" />
+    <ClInclude Include="ServiceInstaller.h" />
+    <ClInclude Include="ZeroTierOneService.h" />
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="ZeroTierOne.rc" />

+ 33 - 0
windows/ZeroTierOne/ZeroTierOne.vcxproj.filters

@@ -58,6 +58,18 @@
     <Filter Include="Source Files\ext\json-parser">
       <UniqueIdentifier>{da28e961-1761-41d8-9a59-65b00dfb1302}</UniqueIdentifier>
     </Filter>
+    <Filter Include="Source Files\windows">
+      <UniqueIdentifier>{43f75f84-c70d-4d44-a0ef-28a7a399abd4}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Source Files\windows\ZeroTierOne">
+      <UniqueIdentifier>{0da07a2f-8922-4827-ac51-29ca3f30f881}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Header Files\windows">
+      <UniqueIdentifier>{b74916eb-bb6c-4449-a2a2-fa0b17f60121}</UniqueIdentifier>
+    </Filter>
+    <Filter Include="Header Files\windows\ZeroTierOne">
+      <UniqueIdentifier>{bf604491-14c4-4a74-81a6-6105d07c5c7c}</UniqueIdentifier>
+    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\service\ControlPlane.cpp">
@@ -150,6 +162,18 @@
     <ClCompile Include="..\..\ext\json-parser\json.c">
       <Filter>Source Files\ext\json-parser</Filter>
     </ClCompile>
+    <ClCompile Include="..\..\one.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="ServiceBase.cpp">
+      <Filter>Source Files\windows\ZeroTierOne</Filter>
+    </ClCompile>
+    <ClCompile Include="ServiceInstaller.cpp">
+      <Filter>Source Files\windows\ZeroTierOne</Filter>
+    </ClCompile>
+    <ClCompile Include="ZeroTierOneService.cpp">
+      <Filter>Source Files\windows\ZeroTierOne</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="resource.h">
@@ -305,6 +329,15 @@
     <ClInclude Include="..\..\ext\http-parser\http_parser.h">
       <Filter>Header Files\ext\http-parser</Filter>
     </ClInclude>
+    <ClInclude Include="ServiceBase.h">
+      <Filter>Header Files\windows\ZeroTierOne</Filter>
+    </ClInclude>
+    <ClInclude Include="ServiceInstaller.h">
+      <Filter>Header Files\windows\ZeroTierOne</Filter>
+    </ClInclude>
+    <ClInclude Include="ZeroTierOneService.h">
+      <Filter>Header Files\windows\ZeroTierOne</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ResourceCompile Include="ZeroTierOne.rc">

+ 54 - 17
windows/ZeroTierOne/ZeroTierOneService.cpp

@@ -34,15 +34,13 @@
 
 #include "ZeroTierOneService.h"
 
-#include "../../node/Defaults.hpp"
-#include "../../node/Utils.hpp"
-
-#include "../../control/NodeControlClient.hpp"
-#include "../../control/NodeControlService.hpp"
+#include "../../version.h"
+#include "../../include/ZeroTierOne.h"
 
-#include "../../osdep/WindowsEthernetTapFactory.hpp"
-#include "../../osdep/WindowsRoutingTable.hpp"
-#include "../../osdep/NativeSocketManager.hpp"
+#include "../../node/Constants.hpp"
+#include "../../node/Utils.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 <PSTR>(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);
 	}
 }

+ 3 - 5
windows/ZeroTierOne/ZeroTierOneService.h

@@ -33,11 +33,9 @@
 
 #include <string>
 
-#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;
 };