WindowsEthernetTap.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. * Copyright (c)2019 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2025-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <string.h>
  17. #include <winsock2.h>
  18. #include <windows.h>
  19. #include <tchar.h>
  20. #include <malloc.h>
  21. #include <winreg.h>
  22. #include <wchar.h>
  23. #include <ws2ipdef.h>
  24. #include <ws2tcpip.h>
  25. #include <IPHlpApi.h>
  26. #include <nldef.h>
  27. #include <netioapi.h>
  28. #include <atlbase.h>
  29. #include <netlistmgr.h>
  30. #include <nldef.h>
  31. #include <SetupAPI.h>
  32. #include <newdev.h>
  33. #include <cfgmgr32.h>
  34. #include <iostream>
  35. #include <set>
  36. #include "../node/Constants.hpp"
  37. #include "../node/Utils.hpp"
  38. #include "../node/Mutex.hpp"
  39. #include "WindowsEthernetTap.hpp"
  40. #include "OSUtils.hpp"
  41. #include "..\windows\TapDriver6\tap-windows.h"
  42. #include "WinDNSHelper.hpp"
  43. #include <netcon.h>
  44. // Create a fake unused default route to force detection of network type on networks without gateways
  45. #define ZT_WINDOWS_CREATE_FAKE_DEFAULT_ROUTE
  46. // Function signatures of dynamically loaded functions, from newdev.h, setupapi.h, and cfgmgr32.h
  47. typedef BOOL (WINAPI *UpdateDriverForPlugAndPlayDevicesA_t)(_In_opt_ HWND hwndParent,_In_ LPCSTR HardwareId,_In_ LPCSTR FullInfPath,_In_ DWORD InstallFlags,_Out_opt_ PBOOL bRebootRequired);
  48. typedef BOOL (WINAPI *SetupDiGetINFClassA_t)(_In_ PCSTR InfName,_Out_ LPGUID ClassGuid,_Out_writes_(ClassNameSize) PSTR ClassName,_In_ DWORD ClassNameSize,_Out_opt_ PDWORD RequiredSize);
  49. typedef HDEVINFO (WINAPI *SetupDiCreateDeviceInfoList_t)(_In_opt_ CONST GUID *ClassGuid,_In_opt_ HWND hwndParent);
  50. typedef BOOL (WINAPI *SetupDiCreateDeviceInfoA_t)(_In_ HDEVINFO DeviceInfoSet,_In_ PCSTR DeviceName,_In_ CONST GUID *ClassGuid,_In_opt_ PCSTR DeviceDescription,_In_opt_ HWND hwndParent,_In_ DWORD CreationFlags,_Out_opt_ PSP_DEVINFO_DATA DeviceInfoData);
  51. typedef BOOL (WINAPI *SetupDiSetDeviceRegistryPropertyA_t)(_In_ HDEVINFO DeviceInfoSet,_Inout_ PSP_DEVINFO_DATA DeviceInfoData,_In_ DWORD Property,_In_reads_bytes_opt_(PropertyBufferSize) CONST BYTE *PropertyBuffer,_In_ DWORD PropertyBufferSize);
  52. typedef BOOL (WINAPI *SetupDiCallClassInstaller_t)(_In_ DI_FUNCTION InstallFunction,_In_ HDEVINFO DeviceInfoSet,_In_opt_ PSP_DEVINFO_DATA DeviceInfoData);
  53. typedef BOOL (WINAPI *SetupDiDestroyDeviceInfoList_t)(_In_ HDEVINFO DeviceInfoSet);
  54. typedef HDEVINFO (WINAPI *SetupDiGetClassDevsExA_t)(_In_opt_ CONST GUID *ClassGuid,_In_opt_ PCSTR Enumerator,_In_opt_ HWND hwndParent,_In_ DWORD Flags,_In_opt_ HDEVINFO DeviceInfoSet,_In_opt_ PCSTR MachineName,_Reserved_ PVOID Reserved);
  55. typedef BOOL (WINAPI *SetupDiOpenDeviceInfoA_t)(_In_ HDEVINFO DeviceInfoSet,_In_ PCSTR DeviceInstanceId,_In_opt_ HWND hwndParent,_In_ DWORD OpenFlags,_Out_opt_ PSP_DEVINFO_DATA DeviceInfoData);
  56. typedef BOOL (WINAPI *SetupDiEnumDeviceInfo_t)(_In_ HDEVINFO DeviceInfoSet,_In_ DWORD MemberIndex,_Out_ PSP_DEVINFO_DATA DeviceInfoData);
  57. typedef BOOL (WINAPI *SetupDiSetClassInstallParamsA_t)(_In_ HDEVINFO DeviceInfoSet,_In_opt_ PSP_DEVINFO_DATA DeviceInfoData,_In_reads_bytes_opt_(ClassInstallParamsSize) PSP_CLASSINSTALL_HEADER ClassInstallParams,_In_ DWORD ClassInstallParamsSize);
  58. typedef CONFIGRET (WINAPI *CM_Get_Device_ID_ExA_t)(_In_ DEVINST dnDevInst,_Out_writes_(BufferLen) PSTR Buffer,_In_ ULONG BufferLen,_In_ ULONG ulFlags,_In_opt_ HMACHINE hMachine);
  59. typedef BOOL (WINAPI *SetupDiGetDeviceInstanceIdA_t)(_In_ HDEVINFO DeviceInfoSet,_In_ PSP_DEVINFO_DATA DeviceInfoData,_Out_writes_opt_(DeviceInstanceIdSize) PSTR DeviceInstanceId,_In_ DWORD DeviceInstanceIdSize,_Out_opt_ PDWORD RequiredSize);
  60. namespace ZeroTier {
  61. namespace {
  62. // Static/singleton class that when initialized loads a bunch of environment information and a few dynamically loaded DLLs
  63. class WindowsEthernetTapEnv
  64. {
  65. public:
  66. WindowsEthernetTapEnv()
  67. {
  68. #ifdef _WIN64
  69. is64Bit = TRUE;
  70. tapDriverPath = "\\tap-windows\\x64\\zttap300.inf";
  71. #else
  72. is64Bit = FALSE;
  73. IsWow64Process(GetCurrentProcess(),&is64Bit);
  74. if (is64Bit) {
  75. fprintf(stderr,"FATAL: you must use the 64-bit ZeroTier One service on 64-bit Windows systems\r\n");
  76. _exit(1);
  77. }
  78. tapDriverPath = "\\tap-windows\\x86\\zttap300.inf";
  79. #endif
  80. tapDriverName = "zttap300";
  81. setupApiMod = LoadLibraryA("setupapi.dll");
  82. if (!setupApiMod) {
  83. fprintf(stderr,"FATAL: unable to dynamically load setupapi.dll\r\n");
  84. _exit(1);
  85. }
  86. if (!(this->SetupDiGetINFClassA = (SetupDiGetINFClassA_t)GetProcAddress(setupApiMod,"SetupDiGetINFClassA"))) {
  87. fprintf(stderr,"FATAL: SetupDiGetINFClassA not found in setupapi.dll\r\n");
  88. _exit(1);
  89. }
  90. if (!(this->SetupDiCreateDeviceInfoList = (SetupDiCreateDeviceInfoList_t)GetProcAddress(setupApiMod,"SetupDiCreateDeviceInfoList"))) {
  91. fprintf(stderr,"FATAL: SetupDiCreateDeviceInfoList not found in setupapi.dll\r\n");
  92. _exit(1);
  93. }
  94. if (!(this->SetupDiCreateDeviceInfoA = (SetupDiCreateDeviceInfoA_t)GetProcAddress(setupApiMod,"SetupDiCreateDeviceInfoA"))) {
  95. fprintf(stderr,"FATAL: SetupDiCreateDeviceInfoA not found in setupapi.dll\r\n");
  96. _exit(1);
  97. }
  98. if (!(this->SetupDiSetDeviceRegistryPropertyA = (SetupDiSetDeviceRegistryPropertyA_t)GetProcAddress(setupApiMod,"SetupDiSetDeviceRegistryPropertyA"))) {
  99. fprintf(stderr,"FATAL: SetupDiSetDeviceRegistryPropertyA not found in setupapi.dll\r\n");
  100. _exit(1);
  101. }
  102. if (!(this->SetupDiCallClassInstaller = (SetupDiCallClassInstaller_t)GetProcAddress(setupApiMod,"SetupDiCallClassInstaller"))) {
  103. fprintf(stderr,"FATAL: SetupDiCallClassInstaller not found in setupapi.dll\r\n");
  104. _exit(1);
  105. }
  106. if (!(this->SetupDiDestroyDeviceInfoList = (SetupDiDestroyDeviceInfoList_t)GetProcAddress(setupApiMod,"SetupDiDestroyDeviceInfoList"))) {
  107. fprintf(stderr,"FATAL: SetupDiDestroyDeviceInfoList not found in setupapi.dll\r\n");
  108. _exit(1);
  109. }
  110. if (!(this->SetupDiGetClassDevsExA = (SetupDiGetClassDevsExA_t)GetProcAddress(setupApiMod,"SetupDiGetClassDevsExA"))) {
  111. fprintf(stderr,"FATAL: SetupDiGetClassDevsExA not found in setupapi.dll\r\n");
  112. _exit(1);
  113. }
  114. if (!(this->SetupDiOpenDeviceInfoA = (SetupDiOpenDeviceInfoA_t)GetProcAddress(setupApiMod,"SetupDiOpenDeviceInfoA"))) {
  115. fprintf(stderr,"FATAL: SetupDiOpenDeviceInfoA not found in setupapi.dll\r\n");
  116. _exit(1);
  117. }
  118. if (!(this->SetupDiEnumDeviceInfo = (SetupDiEnumDeviceInfo_t)GetProcAddress(setupApiMod,"SetupDiEnumDeviceInfo"))) {
  119. fprintf(stderr,"FATAL: SetupDiEnumDeviceInfo not found in setupapi.dll\r\n");
  120. _exit(1);
  121. }
  122. if (!(this->SetupDiSetClassInstallParamsA = (SetupDiSetClassInstallParamsA_t)GetProcAddress(setupApiMod,"SetupDiSetClassInstallParamsA"))) {
  123. fprintf(stderr,"FATAL: SetupDiSetClassInstallParamsA not found in setupapi.dll\r\n");
  124. _exit(1);
  125. }
  126. if (!(this->SetupDiGetDeviceInstanceIdA = (SetupDiGetDeviceInstanceIdA_t)GetProcAddress(setupApiMod,"SetupDiGetDeviceInstanceIdA"))) {
  127. fprintf(stderr,"FATAL: SetupDiGetDeviceInstanceIdA not found in setupapi.dll\r\n");
  128. _exit(1);
  129. }
  130. newDevMod = LoadLibraryA("newdev.dll");
  131. if (!newDevMod) {
  132. fprintf(stderr,"FATAL: unable to dynamically load newdev.dll\r\n");
  133. _exit(1);
  134. }
  135. if (!(this->UpdateDriverForPlugAndPlayDevicesA = (UpdateDriverForPlugAndPlayDevicesA_t)GetProcAddress(newDevMod,"UpdateDriverForPlugAndPlayDevicesA"))) {
  136. fprintf(stderr,"FATAL: UpdateDriverForPlugAndPlayDevicesA not found in newdev.dll\r\n");
  137. _exit(1);
  138. }
  139. cfgMgrMod = LoadLibraryA("cfgmgr32.dll");
  140. if (!cfgMgrMod) {
  141. fprintf(stderr,"FATAL: unable to dynamically load cfgmgr32.dll\r\n");
  142. _exit(1);
  143. }
  144. if (!(this->CM_Get_Device_ID_ExA = (CM_Get_Device_ID_ExA_t)GetProcAddress(cfgMgrMod,"CM_Get_Device_ID_ExA"))) {
  145. fprintf(stderr,"FATAL: CM_Get_Device_ID_ExA not found in cfgmgr32.dll\r\n");
  146. _exit(1);
  147. }
  148. }
  149. BOOL is64Bit; // is the system 64-bit, regardless of whether this binary is or not
  150. std::string tapDriverPath;
  151. std::string tapDriverName;
  152. UpdateDriverForPlugAndPlayDevicesA_t UpdateDriverForPlugAndPlayDevicesA;
  153. SetupDiGetINFClassA_t SetupDiGetINFClassA;
  154. SetupDiCreateDeviceInfoList_t SetupDiCreateDeviceInfoList;
  155. SetupDiCreateDeviceInfoA_t SetupDiCreateDeviceInfoA;
  156. SetupDiSetDeviceRegistryPropertyA_t SetupDiSetDeviceRegistryPropertyA;
  157. SetupDiCallClassInstaller_t SetupDiCallClassInstaller;
  158. SetupDiDestroyDeviceInfoList_t SetupDiDestroyDeviceInfoList;
  159. SetupDiGetClassDevsExA_t SetupDiGetClassDevsExA;
  160. SetupDiOpenDeviceInfoA_t SetupDiOpenDeviceInfoA;
  161. SetupDiEnumDeviceInfo_t SetupDiEnumDeviceInfo;
  162. SetupDiSetClassInstallParamsA_t SetupDiSetClassInstallParamsA;
  163. SetupDiGetDeviceInstanceIdA_t SetupDiGetDeviceInstanceIdA;
  164. CM_Get_Device_ID_ExA_t CM_Get_Device_ID_ExA;
  165. private:
  166. HMODULE setupApiMod;
  167. HMODULE newDevMod;
  168. HMODULE cfgMgrMod;
  169. };
  170. static const WindowsEthernetTapEnv WINENV;
  171. // Only create or delete devices one at a time
  172. static Mutex _systemTapInitLock;
  173. // Only perform installation or uninstallation options one at a time
  174. static Mutex _systemDeviceManagementLock;
  175. } // anonymous namespace
  176. std::string WindowsEthernetTap::addNewPersistentTapDevice(const char *pathToInf,std::string &deviceInstanceId)
  177. {
  178. Mutex::Lock _l(_systemDeviceManagementLock);
  179. GUID classGuid;
  180. char className[1024];
  181. if (!WINENV.SetupDiGetINFClassA(pathToInf,&classGuid,className,sizeof(className),(PDWORD)0)) {
  182. return std::string("SetupDiGetINFClassA() failed -- unable to read zttap driver INF file");
  183. }
  184. HDEVINFO deviceInfoSet = WINENV.SetupDiCreateDeviceInfoList(&classGuid,(HWND)0);
  185. if (deviceInfoSet == INVALID_HANDLE_VALUE) {
  186. return std::string("SetupDiCreateDeviceInfoList() failed");
  187. }
  188. SP_DEVINFO_DATA deviceInfoData;
  189. memset(&deviceInfoData,0,sizeof(deviceInfoData));
  190. deviceInfoData.cbSize = sizeof(deviceInfoData);
  191. if (!WINENV.SetupDiCreateDeviceInfoA(deviceInfoSet,className,&classGuid,(PCSTR)0,(HWND)0,DICD_GENERATE_ID,&deviceInfoData)) {
  192. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  193. return std::string("SetupDiCreateDeviceInfoA() failed");
  194. }
  195. if (!WINENV.SetupDiSetDeviceRegistryPropertyA(deviceInfoSet,&deviceInfoData,SPDRP_HARDWAREID,(const BYTE *)WINENV.tapDriverName.c_str(),(DWORD)(WINENV.tapDriverName.length() + 1))) {
  196. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  197. return std::string("SetupDiSetDeviceRegistryPropertyA() failed");
  198. }
  199. if (!WINENV.SetupDiCallClassInstaller(DIF_REGISTERDEVICE,deviceInfoSet,&deviceInfoData)) {
  200. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  201. return std::string("SetupDiCallClassInstaller(DIF_REGISTERDEVICE) failed");
  202. }
  203. // HACK: During upgrades, this can fail while the installer is still running. So make 60 attempts
  204. // with a 1s delay between each attempt.
  205. bool driverInstalled = false;
  206. for(int retryCounter=0;retryCounter<60;++retryCounter) {
  207. BOOL rebootRequired = FALSE;
  208. if (WINENV.UpdateDriverForPlugAndPlayDevicesA((HWND)0,WINENV.tapDriverName.c_str(),pathToInf,INSTALLFLAG_FORCE|INSTALLFLAG_NONINTERACTIVE,&rebootRequired)) {
  209. driverInstalled = true;
  210. break;
  211. } else Sleep(1000);
  212. }
  213. if (!driverInstalled) {
  214. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  215. return std::string("UpdateDriverForPlugAndPlayDevices() failed (made 60 attempts)");
  216. }
  217. char iidbuf[1024];
  218. DWORD iidReqSize = sizeof(iidbuf);
  219. if (WINENV.SetupDiGetDeviceInstanceIdA(deviceInfoSet,&deviceInfoData,iidbuf,sizeof(iidbuf),&iidReqSize)) {
  220. deviceInstanceId = iidbuf;
  221. } // failure here is not fatal since we only need this on Vista and 2008 -- other versions fill it into the registry automatically
  222. WINENV.SetupDiDestroyDeviceInfoList(deviceInfoSet);
  223. return std::string();
  224. }
  225. std::string WindowsEthernetTap::destroyAllLegacyPersistentTapDevices()
  226. {
  227. char subkeyName[1024];
  228. char subkeyClass[1024];
  229. char data[1024];
  230. std::set<std::string> instanceIdPathsToRemove;
  231. {
  232. HKEY nwAdapters;
  233. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  234. return std::string("Could not open registry key");
  235. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  236. DWORD type;
  237. DWORD dataLen;
  238. DWORD subkeyNameLen = sizeof(subkeyName);
  239. DWORD subkeyClassLen = sizeof(subkeyClass);
  240. FILETIME lastWriteTime;
  241. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  242. type = 0;
  243. dataLen = sizeof(data);
  244. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  245. data[dataLen] = '\0';
  246. if ((!strnicmp(data,"zttap",5))&&(WINENV.tapDriverName != data)) {
  247. std::string instanceIdPath;
  248. type = 0;
  249. dataLen = sizeof(data);
  250. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  251. instanceIdPath.assign(data,dataLen);
  252. if (instanceIdPath.length() != 0)
  253. instanceIdPathsToRemove.insert(instanceIdPath);
  254. }
  255. }
  256. } else break; // end of list or failure
  257. }
  258. RegCloseKey(nwAdapters);
  259. }
  260. std::string errlist;
  261. for(std::set<std::string>::iterator iidp(instanceIdPathsToRemove.begin());iidp!=instanceIdPathsToRemove.end();++iidp) {
  262. std::string err = deletePersistentTapDevice(iidp->c_str());
  263. if (err.length() > 0) {
  264. if (errlist.length() > 0)
  265. errlist.push_back(',');
  266. errlist.append(err);
  267. }
  268. }
  269. return errlist;
  270. }
  271. std::string WindowsEthernetTap::destroyAllPersistentTapDevices()
  272. {
  273. char subkeyName[1024];
  274. char subkeyClass[1024];
  275. char data[1024];
  276. std::set<std::string> instanceIdPathsToRemove;
  277. {
  278. HKEY nwAdapters;
  279. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  280. return std::string("Could not open registry key");
  281. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  282. DWORD type;
  283. DWORD dataLen;
  284. DWORD subkeyNameLen = sizeof(subkeyName);
  285. DWORD subkeyClassLen = sizeof(subkeyClass);
  286. FILETIME lastWriteTime;
  287. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  288. type = 0;
  289. dataLen = sizeof(data);
  290. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  291. data[dataLen] = '\0';
  292. if (!strnicmp(data,"zttap",5)) {
  293. std::string instanceIdPath;
  294. type = 0;
  295. dataLen = sizeof(data);
  296. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  297. instanceIdPath.assign(data,dataLen);
  298. if (instanceIdPath.length() != 0)
  299. instanceIdPathsToRemove.insert(instanceIdPath);
  300. }
  301. }
  302. } else break; // end of list or failure
  303. }
  304. RegCloseKey(nwAdapters);
  305. }
  306. std::string errlist;
  307. for(std::set<std::string>::iterator iidp(instanceIdPathsToRemove.begin());iidp!=instanceIdPathsToRemove.end();++iidp) {
  308. std::string err = deletePersistentTapDevice(iidp->c_str());
  309. if (err.length() > 0) {
  310. if (errlist.length() > 0)
  311. errlist.push_back(',');
  312. errlist.append(err);
  313. }
  314. }
  315. return errlist;
  316. }
  317. std::string WindowsEthernetTap::deletePersistentTapDevice(const char *instanceId)
  318. {
  319. char iid[256];
  320. SP_REMOVEDEVICE_PARAMS rmdParams;
  321. memset(&rmdParams,0,sizeof(rmdParams));
  322. rmdParams.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  323. rmdParams.ClassInstallHeader.InstallFunction = DIF_REMOVE;
  324. rmdParams.Scope = DI_REMOVEDEVICE_GLOBAL;
  325. rmdParams.HwProfile = 0;
  326. Mutex::Lock _l(_systemDeviceManagementLock);
  327. HDEVINFO devInfo = WINENV.SetupDiGetClassDevsExA((const GUID *)0,(PCSTR)0,(HWND)0,DIGCF_ALLCLASSES,(HDEVINFO)0,(PCSTR)0,(PVOID)0);
  328. if (devInfo == INVALID_HANDLE_VALUE)
  329. return std::string("SetupDiGetClassDevsExA() failed");
  330. WINENV.SetupDiOpenDeviceInfoA(devInfo,instanceId,(HWND)0,0,(PSP_DEVINFO_DATA)0);
  331. SP_DEVINFO_DATA devInfoData;
  332. memset(&devInfoData,0,sizeof(devInfoData));
  333. devInfoData.cbSize = sizeof(devInfoData);
  334. for(DWORD devIndex=0;WINENV.SetupDiEnumDeviceInfo(devInfo,devIndex,&devInfoData);devIndex++) {
  335. if ((WINENV.CM_Get_Device_ID_ExA(devInfoData.DevInst,iid,sizeof(iid),0,(HMACHINE)0) == CR_SUCCESS)&&(!strcmp(iid,instanceId))) {
  336. if (!WINENV.SetupDiSetClassInstallParamsA(devInfo,&devInfoData,&rmdParams.ClassInstallHeader,sizeof(rmdParams))) {
  337. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  338. return std::string("SetupDiSetClassInstallParams() failed");
  339. }
  340. if (!WINENV.SetupDiCallClassInstaller(DIF_REMOVE,devInfo,&devInfoData)) {
  341. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  342. return std::string("SetupDiCallClassInstaller(DIF_REMOVE) failed");
  343. }
  344. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  345. return std::string();
  346. }
  347. }
  348. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  349. return std::string("instance ID not found");
  350. }
  351. bool WindowsEthernetTap::setPersistentTapDeviceState(const char *instanceId,bool enabled)
  352. {
  353. char iid[256];
  354. SP_PROPCHANGE_PARAMS params;
  355. Mutex::Lock _l(_systemDeviceManagementLock);
  356. HDEVINFO devInfo = WINENV.SetupDiGetClassDevsExA((const GUID *)0,(PCSTR)0,(HWND)0,DIGCF_ALLCLASSES,(HDEVINFO)0,(PCSTR)0,(PVOID)0);
  357. if (devInfo == INVALID_HANDLE_VALUE)
  358. return false;
  359. WINENV.SetupDiOpenDeviceInfoA(devInfo,instanceId,(HWND)0,0,(PSP_DEVINFO_DATA)0);
  360. SP_DEVINFO_DATA devInfoData;
  361. memset(&devInfoData,0,sizeof(devInfoData));
  362. devInfoData.cbSize = sizeof(devInfoData);
  363. for(DWORD devIndex=0;WINENV.SetupDiEnumDeviceInfo(devInfo,devIndex,&devInfoData);devIndex++) {
  364. if ((WINENV.CM_Get_Device_ID_ExA(devInfoData.DevInst,iid,sizeof(iid),0,(HMACHINE)0) == CR_SUCCESS)&&(!strcmp(iid,instanceId))) {
  365. memset(&params,0,sizeof(params));
  366. params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  367. params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  368. params.StateChange = enabled ? DICS_ENABLE : DICS_DISABLE;
  369. params.Scope = DICS_FLAG_GLOBAL;
  370. params.HwProfile = 0;
  371. WINENV.SetupDiSetClassInstallParamsA(devInfo,&devInfoData,&params.ClassInstallHeader,sizeof(params));
  372. WINENV.SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,devInfo,&devInfoData);
  373. memset(&params,0,sizeof(params));
  374. params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
  375. params.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
  376. params.StateChange = enabled ? DICS_ENABLE : DICS_DISABLE;
  377. params.Scope = DICS_FLAG_CONFIGSPECIFIC;
  378. params.HwProfile = 0;
  379. WINENV.SetupDiSetClassInstallParamsA(devInfo,&devInfoData,&params.ClassInstallHeader,sizeof(params));
  380. WINENV.SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,devInfo,&devInfoData);
  381. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  382. return true;
  383. }
  384. }
  385. WINENV.SetupDiDestroyDeviceInfoList(devInfo);
  386. return false;
  387. }
  388. WindowsEthernetTap::WindowsEthernetTap(
  389. const char *hp,
  390. const MAC &mac,
  391. unsigned int mtu,
  392. unsigned int metric,
  393. uint64_t nwid,
  394. const char *friendlyName,
  395. void (*handler)(void *,void *,uint64_t,const MAC &,const MAC &,unsigned int,unsigned int,const void *,unsigned int),
  396. void *arg) :
  397. _handler(handler),
  398. _arg(arg),
  399. _mac(mac),
  400. _nwid(nwid),
  401. _mtu(mtu),
  402. _tap(INVALID_HANDLE_VALUE),
  403. _friendlyName(friendlyName),
  404. _injectSemaphore(INVALID_HANDLE_VALUE),
  405. _pathToHelpers(hp),
  406. _run(true),
  407. _initialized(false),
  408. _enabled(true),
  409. _lastIfAddrsUpdate(0)
  410. {
  411. char subkeyName[1024];
  412. char subkeyClass[1024];
  413. char data[1024];
  414. char tag[24];
  415. // We "tag" registry entries with the network ID to identify persistent devices
  416. OSUtils::ztsnprintf(tag,sizeof(tag),"%.16llx",(unsigned long long)nwid);
  417. Mutex::Lock _l(_systemTapInitLock);
  418. HKEY nwAdapters;
  419. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  420. throw std::runtime_error("unable to open registry key for network adapter enumeration");
  421. // Look for the tap instance that corresponds with this network
  422. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  423. DWORD type;
  424. DWORD dataLen;
  425. DWORD subkeyNameLen = sizeof(subkeyName);
  426. DWORD subkeyClassLen = sizeof(subkeyClass);
  427. FILETIME lastWriteTime;
  428. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  429. type = 0;
  430. dataLen = sizeof(data);
  431. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  432. data[dataLen] = (char)0;
  433. if (WINENV.tapDriverName == data) {
  434. std::string instanceId;
  435. type = 0;
  436. dataLen = sizeof(data);
  437. if (RegGetValueA(nwAdapters,subkeyName,"NetCfgInstanceId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  438. instanceId.assign(data,dataLen);
  439. std::string instanceIdPath;
  440. type = 0;
  441. dataLen = sizeof(data);
  442. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  443. instanceIdPath.assign(data,dataLen);
  444. if ((_netCfgInstanceId.length() == 0)&&(instanceId.length() != 0)&&(instanceIdPath.length() != 0)) {
  445. type = 0;
  446. dataLen = sizeof(data);
  447. if (RegGetValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  448. data[dataLen] = '\0';
  449. if (!strcmp(data,tag)) {
  450. _netCfgInstanceId = instanceId;
  451. _deviceInstanceId = instanceIdPath;
  452. _mySubkeyName = subkeyName;
  453. break; // found it!
  454. }
  455. }
  456. }
  457. }
  458. }
  459. } else break; // no more subkeys or error occurred enumerating them
  460. }
  461. // If there is no device, try to create one
  462. bool creatingNewDevice = (_netCfgInstanceId.length() == 0);
  463. std::string newDeviceInstanceId;
  464. if (creatingNewDevice) {
  465. for(int getNewAttemptCounter=0;getNewAttemptCounter<2;++getNewAttemptCounter) {
  466. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  467. DWORD type;
  468. DWORD dataLen;
  469. DWORD subkeyNameLen = sizeof(subkeyName);
  470. DWORD subkeyClassLen = sizeof(subkeyClass);
  471. FILETIME lastWriteTime;
  472. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  473. type = 0;
  474. dataLen = sizeof(data);
  475. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  476. data[dataLen] = '\0';
  477. if (WINENV.tapDriverName == data) {
  478. type = 0;
  479. dataLen = sizeof(data);
  480. if ((RegGetValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",RRF_RT_ANY,&type,(PVOID)data,&dataLen) != ERROR_SUCCESS)||(dataLen <= 0)) {
  481. type = 0;
  482. dataLen = sizeof(data);
  483. if (RegGetValueA(nwAdapters,subkeyName,"NetCfgInstanceId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  484. RegSetKeyValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",REG_SZ,tag,(DWORD)(strlen(tag)+1));
  485. _netCfgInstanceId.assign(data,dataLen);
  486. type = 0;
  487. dataLen = sizeof(data);
  488. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  489. _deviceInstanceId.assign(data,dataLen);
  490. _mySubkeyName = subkeyName;
  491. // Disable DHCP by default on new devices
  492. HKEY tcpIpInterfaces;
  493. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ|KEY_WRITE,&tcpIpInterfaces) == ERROR_SUCCESS) {
  494. DWORD enable = 0;
  495. RegSetKeyValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),"EnableDHCP",REG_DWORD,&enable,sizeof(enable));
  496. RegCloseKey(tcpIpInterfaces);
  497. }
  498. break; // found an unused zttap device
  499. }
  500. }
  501. }
  502. }
  503. } else break; // no more keys or error occurred
  504. }
  505. if (_netCfgInstanceId.length() > 0) {
  506. break; // found an unused zttap device
  507. } else {
  508. // no unused zttap devices, so create one
  509. std::string errm = addNewPersistentTapDevice((std::string(_pathToHelpers) + WINENV.tapDriverPath).c_str(),newDeviceInstanceId);
  510. if (errm.length() > 0)
  511. throw std::runtime_error(std::string("unable to create new device instance: ")+errm);
  512. }
  513. }
  514. }
  515. if (_netCfgInstanceId.length() > 0) {
  516. char tmps[64];
  517. unsigned int tmpsl = OSUtils::ztsnprintf(tmps,sizeof(tmps),"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X",(unsigned int)mac[0],(unsigned int)mac[1],(unsigned int)mac[2],(unsigned int)mac[3],(unsigned int)mac[4],(unsigned int)mac[5]) + 1;
  518. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"NetworkAddress",REG_SZ,tmps,tmpsl);
  519. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"MAC",REG_SZ,tmps,tmpsl);
  520. tmpsl = OSUtils::ztsnprintf(tmps, sizeof(tmps), "%d", mtu);
  521. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"MTU",REG_SZ,tmps,tmpsl);
  522. DWORD tmp = 0;
  523. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"*NdisDeviceType",REG_DWORD,(LPCVOID)&tmp,sizeof(tmp));
  524. tmp = IF_TYPE_ETHERNET_CSMACD;
  525. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"*IfType",REG_DWORD,(LPCVOID)&tmp,sizeof(tmp));
  526. if (creatingNewDevice) {
  527. // Vista/2008 does not set this
  528. if (newDeviceInstanceId.length() > 0)
  529. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"DeviceInstanceID",REG_SZ,newDeviceInstanceId.c_str(),(DWORD)newDeviceInstanceId.length());
  530. // Set EnableDHCP to 0 by default on new devices
  531. tmp = 0;
  532. RegSetKeyValueA(nwAdapters,_mySubkeyName.c_str(),"EnableDHCP",REG_DWORD,(LPCVOID)&tmp,sizeof(tmp));
  533. }
  534. RegCloseKey(nwAdapters);
  535. } else {
  536. RegCloseKey(nwAdapters);
  537. throw std::runtime_error("unable to find or create tap adapter");
  538. }
  539. {
  540. char nobraces[128]; // strip braces from GUID before converting it, because Windows
  541. const char *nbtmp1 = _netCfgInstanceId.c_str();
  542. char *nbtmp2 = nobraces;
  543. while (*nbtmp1) {
  544. if ((*nbtmp1 != '{')&&(*nbtmp1 != '}'))
  545. *nbtmp2++ = *nbtmp1;
  546. ++nbtmp1;
  547. }
  548. *nbtmp2 = (char)0;
  549. if (UuidFromStringA((RPC_CSTR)nobraces,&_deviceGuid) != RPC_S_OK)
  550. throw std::runtime_error("unable to convert instance ID GUID to native GUID (invalid NetCfgInstanceId in registry?)");
  551. }
  552. // Get the LUID, which is one of like four fucking ways to refer to a network device in Windows
  553. if (ConvertInterfaceGuidToLuid(&_deviceGuid,&_deviceLuid) != NO_ERROR)
  554. throw std::runtime_error("unable to convert device interface GUID to LUID");
  555. //_initialized = true;
  556. if (friendlyName)
  557. setFriendlyName(friendlyName);
  558. _injectSemaphore = CreateSemaphore(NULL,0,1,NULL);
  559. _thread = Thread::start(this);
  560. }
  561. WindowsEthernetTap::~WindowsEthernetTap()
  562. {
  563. WinDNSHelper::removeDNS(_nwid);
  564. _run = false;
  565. ReleaseSemaphore(_injectSemaphore,1,NULL);
  566. Thread::join(_thread);
  567. CloseHandle(_injectSemaphore);
  568. setPersistentTapDeviceState(_deviceInstanceId.c_str(),false);
  569. }
  570. void WindowsEthernetTap::setEnabled(bool en)
  571. {
  572. _enabled = en;
  573. }
  574. bool WindowsEthernetTap::enabled() const
  575. {
  576. return _enabled;
  577. }
  578. bool WindowsEthernetTap::addIp(const InetAddress &ip)
  579. {
  580. if (!ip.netmaskBits()) // sanity check... netmask of 0.0.0.0 is WUT?
  581. return false;
  582. Mutex::Lock _l(_assignedIps_m);
  583. if (std::find(_assignedIps.begin(),_assignedIps.end(),ip) != _assignedIps.end())
  584. return true;
  585. _assignedIps.push_back(ip);
  586. _syncIps();
  587. return true;
  588. }
  589. bool WindowsEthernetTap::removeIp(const InetAddress &ip)
  590. {
  591. if (ip.isV6())
  592. return true;
  593. {
  594. Mutex::Lock _l(_assignedIps_m);
  595. std::vector<InetAddress>::iterator aip(std::find(_assignedIps.begin(),_assignedIps.end(),ip));
  596. if (aip != _assignedIps.end())
  597. _assignedIps.erase(aip);
  598. }
  599. if (!_initialized)
  600. return false;
  601. try {
  602. MIB_UNICASTIPADDRESS_TABLE *ipt = (MIB_UNICASTIPADDRESS_TABLE *)0;
  603. if (GetUnicastIpAddressTable(AF_UNSPEC,&ipt) == NO_ERROR) {
  604. if ((ipt)&&(ipt->NumEntries > 0)) {
  605. for(DWORD i=0;i<(DWORD)ipt->NumEntries;++i) {
  606. if (ipt->Table[i].InterfaceLuid.Value == _deviceLuid.Value) {
  607. InetAddress addr;
  608. switch(ipt->Table[i].Address.si_family) {
  609. case AF_INET:
  610. addr.set(&(ipt->Table[i].Address.Ipv4.sin_addr.S_un.S_addr),4,ipt->Table[i].OnLinkPrefixLength);
  611. break;
  612. case AF_INET6:
  613. addr.set(ipt->Table[i].Address.Ipv6.sin6_addr.u.Byte,16,ipt->Table[i].OnLinkPrefixLength);
  614. if (addr.ipScope() == InetAddress::IP_SCOPE_LINK_LOCAL)
  615. continue; // can't remove link-local IPv6 addresses
  616. break;
  617. }
  618. if (addr == ip) {
  619. DeleteUnicastIpAddressEntry(&(ipt->Table[i]));
  620. FreeMibTable(ipt);
  621. if (ip.isV4()) {
  622. std::vector<std::string> regIps(_getRegistryIPv4Value("IPAddress"));
  623. std::vector<std::string> regSubnetMasks(_getRegistryIPv4Value("SubnetMask"));
  624. char ipbuf[64];
  625. std::string ipstr(ip.toIpString(ipbuf));
  626. for (std::vector<std::string>::iterator rip(regIps.begin()), rm(regSubnetMasks.begin()); ((rip != regIps.end()) && (rm != regSubnetMasks.end())); ++rip, ++rm) {
  627. if (*rip == ipstr) {
  628. regIps.erase(rip);
  629. regSubnetMasks.erase(rm);
  630. _setRegistryIPv4Value("IPAddress", regIps);
  631. _setRegistryIPv4Value("SubnetMask", regSubnetMasks);
  632. break;
  633. }
  634. }
  635. }
  636. return true;
  637. }
  638. }
  639. }
  640. }
  641. FreeMibTable((PVOID)ipt);
  642. }
  643. } catch ( ... ) {}
  644. return false;
  645. }
  646. std::vector<InetAddress> WindowsEthernetTap::ips() const
  647. {
  648. static const InetAddress linkLocalLoopback("fe80::1/64"); // what is this and why does Windows assign it?
  649. std::vector<InetAddress> addrs;
  650. if (!_initialized)
  651. return addrs;
  652. uint64_t now = OSUtils::now();
  653. if ((now - _lastIfAddrsUpdate) <= GETIFADDRS_CACHE_TIME) {
  654. return _ifaddrs;
  655. }
  656. _lastIfAddrsUpdate = now;
  657. try {
  658. MIB_UNICASTIPADDRESS_TABLE *ipt = (MIB_UNICASTIPADDRESS_TABLE *)0;
  659. if (GetUnicastIpAddressTable(AF_UNSPEC,&ipt) == NO_ERROR) {
  660. if ((ipt)&&(ipt->NumEntries > 0)) {
  661. for(DWORD i=0;i<(DWORD)ipt->NumEntries;++i) {
  662. if (ipt->Table[i].InterfaceLuid.Value == _deviceLuid.Value) {
  663. switch(ipt->Table[i].Address.si_family) {
  664. case AF_INET: {
  665. InetAddress ip(&(ipt->Table[i].Address.Ipv4.sin_addr.S_un.S_addr),4,ipt->Table[i].OnLinkPrefixLength);
  666. if (ip != InetAddress::LO4)
  667. addrs.push_back(ip);
  668. } break;
  669. case AF_INET6: {
  670. InetAddress ip(ipt->Table[i].Address.Ipv6.sin6_addr.u.Byte,16,ipt->Table[i].OnLinkPrefixLength);
  671. if ((ip != linkLocalLoopback)&&(ip != InetAddress::LO6))
  672. addrs.push_back(ip);
  673. } break;
  674. }
  675. }
  676. }
  677. }
  678. FreeMibTable(ipt);
  679. }
  680. } catch ( ... ) {} // sanity check, shouldn't happen unless out of memory
  681. std::sort(addrs.begin(),addrs.end());
  682. addrs.erase(std::unique(addrs.begin(),addrs.end()),addrs.end());
  683. _ifaddrs = addrs;
  684. return addrs;
  685. }
  686. void WindowsEthernetTap::put(const MAC &from,const MAC &to,unsigned int etherType,const void *data,unsigned int len)
  687. {
  688. if ((!_initialized)||(!_enabled)||(_tap == INVALID_HANDLE_VALUE)||(len > _mtu))
  689. return;
  690. Mutex::Lock _l(_injectPending_m);
  691. _injectPending.emplace();
  692. _injectPending.back().len = len + 14;
  693. char *const d = _injectPending.back().data;
  694. to.copyTo(d,6);
  695. from.copyTo(d + 6,6);
  696. d[12] = (char)((etherType >> 8) & 0xff);
  697. d[13] = (char)(etherType & 0xff);
  698. memcpy(d + 14,data,len);
  699. ReleaseSemaphore(_injectSemaphore,1,NULL);
  700. }
  701. std::string WindowsEthernetTap::deviceName() const
  702. {
  703. char tmp[1024];
  704. if (ConvertInterfaceLuidToNameA(&_deviceLuid,tmp,sizeof(tmp)) != NO_ERROR)
  705. return std::string("[ConvertInterfaceLuidToName() failed]");
  706. return std::string(tmp);
  707. }
  708. void WindowsEthernetTap::setFriendlyName(const char *dn)
  709. {
  710. if (!_initialized)
  711. return;
  712. HKEY ifp;
  713. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,(std::string("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\") + _netCfgInstanceId).c_str(),0,KEY_READ|KEY_WRITE,&ifp) == ERROR_SUCCESS) {
  714. RegSetKeyValueA(ifp,"Connection","Name",REG_SZ,(LPCVOID)dn,(DWORD)(strlen(dn)+1));
  715. RegCloseKey(ifp);
  716. }
  717. HRESULT hr = S_OK;
  718. INetSharingManager *nsm;
  719. hr = CoCreateInstance(__uuidof(NetSharingManager), NULL, CLSCTX_ALL, __uuidof(INetSharingManager), (void**)&nsm);
  720. if (hr != S_OK) return;
  721. bool found = false;
  722. INetSharingEveryConnectionCollection *nsecc = nullptr;
  723. hr = nsm->get_EnumEveryConnection(&nsecc);
  724. if (!nsecc) {
  725. fprintf(stderr, "Failed to get NSM connections");
  726. return;
  727. }
  728. IEnumVARIANT *ev = nullptr;
  729. IUnknown *unk = nullptr;
  730. hr = nsecc->get__NewEnum(&unk);
  731. if (unk) {
  732. hr = unk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&ev);
  733. unk->Release();
  734. }
  735. if (ev) {
  736. VARIANT v;
  737. VariantInit(&v);
  738. while ((S_OK == ev->Next(1, &v, NULL)) && found == FALSE) {
  739. if (V_VT(&v) == VT_UNKNOWN) {
  740. INetConnection *nc = nullptr;
  741. V_UNKNOWN(&v)->QueryInterface(__uuidof(INetConnection), (void**)&nc);
  742. if (nc) {
  743. NETCON_PROPERTIES *ncp = nullptr;
  744. nc->GetProperties(&ncp);
  745. if (ncp != nullptr) {
  746. GUID curId = ncp->guidId;
  747. if (curId == _deviceGuid) {
  748. wchar_t wtext[255];
  749. mbstowcs(wtext, dn, strlen(dn)+1);
  750. nc->Rename(wtext);
  751. found = true;
  752. }
  753. }
  754. nc->Release();
  755. }
  756. }
  757. VariantClear(&v);
  758. }
  759. ev->Release();
  760. }
  761. nsecc->Release();
  762. _friendlyName_m.lock();
  763. _friendlyName = dn;
  764. _friendlyName_m.unlock();
  765. }
  766. std::string WindowsEthernetTap::friendlyName() const
  767. {
  768. Mutex::Lock l(_friendlyName_m);
  769. return _friendlyName;
  770. }
  771. void WindowsEthernetTap::scanMulticastGroups(std::vector<MulticastGroup> &added,std::vector<MulticastGroup> &removed)
  772. {
  773. if (!_initialized)
  774. return;
  775. HANDLE t = _tap;
  776. if (t == INVALID_HANDLE_VALUE)
  777. return;
  778. std::vector<MulticastGroup> newGroups;
  779. // The ZT1 tap driver supports an IOCTL to get multicast memberships at the L2
  780. // level... something Windows does not seem to expose ordinarily. This lets
  781. // pretty much anything work... IPv4, IPv6, IPX, oldskool Netbios, who knows...
  782. unsigned char mcastbuf[TAP_WIN_IOCTL_GET_MULTICAST_MEMBERSHIPS_OUTPUT_BUF_SIZE];
  783. DWORD bytesReturned = 0;
  784. if (DeviceIoControl(t,TAP_WIN_IOCTL_GET_MULTICAST_MEMBERSHIPS,(LPVOID)mcastbuf,sizeof(mcastbuf),(LPVOID)mcastbuf,sizeof(mcastbuf),&bytesReturned,NULL)) {
  785. if ((bytesReturned > 0)&&(bytesReturned <= TAP_WIN_IOCTL_GET_MULTICAST_MEMBERSHIPS_OUTPUT_BUF_SIZE)) { // sanity check
  786. MAC mac;
  787. DWORD i = 0;
  788. while ((i + 6) <= bytesReturned) {
  789. mac.setTo(mcastbuf + i,6);
  790. i += 6;
  791. if ((mac.isMulticast())&&(!mac.isBroadcast())) {
  792. // exclude the nulls that may be returned or any other junk Windows puts in there
  793. newGroups.push_back(MulticastGroup(mac,0));
  794. }
  795. }
  796. }
  797. }
  798. std::vector<InetAddress> allIps(ips());
  799. for(std::vector<InetAddress>::iterator ip(allIps.begin());ip!=allIps.end();++ip)
  800. newGroups.push_back(MulticastGroup::deriveMulticastGroupForAddressResolution(*ip));
  801. std::sort(newGroups.begin(),newGroups.end());
  802. newGroups.erase(std::unique(newGroups.begin(),newGroups.end()),newGroups.end());
  803. for(std::vector<MulticastGroup>::iterator m(newGroups.begin());m!=newGroups.end();++m) {
  804. if (!std::binary_search(_multicastGroups.begin(),_multicastGroups.end(),*m))
  805. added.push_back(*m);
  806. }
  807. for(std::vector<MulticastGroup>::iterator m(_multicastGroups.begin());m!=_multicastGroups.end();++m) {
  808. if (!std::binary_search(newGroups.begin(),newGroups.end(),*m))
  809. removed.push_back(*m);
  810. }
  811. _multicastGroups.swap(newGroups);
  812. }
  813. void WindowsEthernetTap::setMtu(unsigned int mtu)
  814. {
  815. if (mtu != _mtu) {
  816. _mtu = mtu;
  817. HKEY nwAdapters;
  818. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}", 0, KEY_READ | KEY_WRITE, &nwAdapters) == ERROR_SUCCESS) {
  819. char tmps[64];
  820. unsigned int tmpsl = OSUtils::ztsnprintf(tmps, sizeof(tmps), "%d", mtu);
  821. RegSetKeyValueA(nwAdapters, _mySubkeyName.c_str(), "MTU", REG_SZ, tmps, tmpsl);
  822. RegCloseKey(nwAdapters);
  823. }
  824. }
  825. }
  826. NET_IFINDEX WindowsEthernetTap::interfaceIndex() const
  827. {
  828. NET_IFINDEX idx = -1;
  829. if (ConvertInterfaceLuidToIndex(&_deviceLuid,&idx) == NO_ERROR)
  830. return idx;
  831. return -1;
  832. }
  833. void WindowsEthernetTap::threadMain()
  834. throw()
  835. {
  836. HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
  837. if (FAILED(hres)) {
  838. fprintf(stderr, "WinEthernetTap: COM initialization failed");
  839. return;
  840. }
  841. char tapReadBuf[ZT_MAX_MTU + 32];
  842. char tapPath[128];
  843. HANDLE wait4[3];
  844. OVERLAPPED tapOvlRead,tapOvlWrite;
  845. OSUtils::ztsnprintf(tapPath,sizeof(tapPath),"\\\\.\\Global\\%s.tap",_netCfgInstanceId.c_str());
  846. try {
  847. while (_run) {
  848. // Because Windows
  849. Sleep(250);
  850. setPersistentTapDeviceState(_deviceInstanceId.c_str(),false);
  851. Sleep(250);
  852. setPersistentTapDeviceState(_deviceInstanceId.c_str(),true);
  853. Sleep(250);
  854. setPersistentTapDeviceState(_deviceInstanceId.c_str(),false);
  855. Sleep(250);
  856. setPersistentTapDeviceState(_deviceInstanceId.c_str(),true);
  857. Sleep(250);
  858. _tap = CreateFileA(tapPath,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_SYSTEM|FILE_FLAG_OVERLAPPED,NULL);
  859. if (_tap == INVALID_HANDLE_VALUE) {
  860. Sleep(250);
  861. continue;
  862. }
  863. {
  864. uint32_t tmpi = 1;
  865. DWORD bytesReturned = 0;
  866. DeviceIoControl(_tap,TAP_WIN_IOCTL_SET_MEDIA_STATUS,&tmpi,sizeof(tmpi),&tmpi,sizeof(tmpi),&bytesReturned,NULL);
  867. }
  868. #ifdef ZT_WINDOWS_CREATE_FAKE_DEFAULT_ROUTE
  869. {
  870. /* This inserts a fake default route and a fake ARP entry, forcing
  871. * Windows to detect this as a "real" network and apply proper
  872. * firewall rules.
  873. *
  874. * This hack is completely stupid, but Windows made me do it
  875. * by being broken and insane.
  876. *
  877. * Background: Windows tries to detect its network location by
  878. * matching it to the ARP address of the default route. Networks
  879. * without default routes are "unidentified networks" and cannot
  880. * have their firewall classification changed by the user (easily).
  881. *
  882. * Yes, you read that right.
  883. *
  884. * The common workaround is to set *NdisDeviceType to 1, which
  885. * totally disables all Windows firewall functionality. This is
  886. * the answer you'll find on most forums for things like OpenVPN.
  887. *
  888. * Yes, you read that right.
  889. *
  890. * The default route workaround is also known, but for this to
  891. * work there must be a known default IP that resolves to a known
  892. * ARP address. This works for an OpenVPN tunnel, but not here
  893. * because this isn't a tunnel. It's a mesh. There is no "other
  894. * end," or any other known always on IP.
  895. *
  896. * So let's make a fake one and shove it in there along with its
  897. * fake static ARP entry. Also makes it instant-on and static.
  898. *
  899. * We'll have to see what DHCP does with this. In the future we
  900. * probably will not want to do this on DHCP-enabled networks, so
  901. * when we enable DHCP we will go in and yank this wacko hacko from
  902. * the routing table before doing so.
  903. *
  904. * Like Jesse Pinkman would say: "YEEEEAAH BITCH!" */
  905. const uint32_t fakeIp = htonl(0x19fffffe); // 25.255.255.254 -- unrouted IPv4 block
  906. for(int i=0;i<8;++i) {
  907. MIB_IPNET_ROW2 ipnr;
  908. memset(&ipnr,0,sizeof(ipnr));
  909. ipnr.Address.si_family = AF_INET;
  910. ipnr.Address.Ipv4.sin_addr.s_addr = fakeIp;
  911. ipnr.InterfaceLuid.Value = _deviceLuid.Value;
  912. ipnr.PhysicalAddress[0] = _mac[0] ^ 0x10; // just make something up that's consistent and not part of this net
  913. ipnr.PhysicalAddress[1] = 0x00;
  914. ipnr.PhysicalAddress[2] = (UCHAR)((_deviceGuid.Data1 >> 24) & 0xff);
  915. ipnr.PhysicalAddress[3] = (UCHAR)((_deviceGuid.Data1 >> 16) & 0xff);
  916. ipnr.PhysicalAddress[4] = (UCHAR)((_deviceGuid.Data1 >> 8) & 0xff);
  917. ipnr.PhysicalAddress[5] = (UCHAR)(_deviceGuid.Data1 & 0xff);
  918. ipnr.PhysicalAddressLength = 6;
  919. ipnr.State = NlnsPermanent;
  920. ipnr.IsRouter = 1;
  921. ipnr.IsUnreachable = 0;
  922. ipnr.ReachabilityTime.LastReachable = 0x0fffffff;
  923. ipnr.ReachabilityTime.LastUnreachable = 1;
  924. DWORD result = CreateIpNetEntry2(&ipnr);
  925. if (result != NO_ERROR)
  926. Sleep(250);
  927. else break;
  928. }
  929. for(int i=0;i<8;++i) {
  930. MIB_IPFORWARD_ROW2 nr;
  931. memset(&nr,0,sizeof(nr));
  932. InitializeIpForwardEntry(&nr);
  933. nr.InterfaceLuid.Value = _deviceLuid.Value;
  934. nr.DestinationPrefix.Prefix.si_family = AF_INET; // rest is left as 0.0.0.0/0
  935. nr.NextHop.si_family = AF_INET;
  936. nr.NextHop.Ipv4.sin_addr.s_addr = fakeIp;
  937. nr.Metric = 9999; // do not use as real default route
  938. nr.Protocol = MIB_IPPROTO_NETMGMT;
  939. DWORD result = CreateIpForwardEntry2(&nr);
  940. if (result != NO_ERROR)
  941. Sleep(250);
  942. else break;
  943. }
  944. }
  945. #endif
  946. // Assign or re-assign any should-be-assigned IPs in case we have restarted
  947. {
  948. Mutex::Lock _l(_assignedIps_m);
  949. _syncIps();
  950. }
  951. memset(&tapOvlRead,0,sizeof(tapOvlRead));
  952. tapOvlRead.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  953. memset(&tapOvlWrite,0,sizeof(tapOvlWrite));
  954. tapOvlWrite.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  955. wait4[0] = _injectSemaphore;
  956. wait4[1] = tapOvlRead.hEvent;
  957. wait4[2] = tapOvlWrite.hEvent; // only included if writeInProgress is true
  958. ReadFile(_tap,tapReadBuf,sizeof(tapReadBuf),NULL,&tapOvlRead);
  959. bool writeInProgress = false;
  960. ULONGLONG timeOfLastBorkCheck = GetTickCount64();
  961. _initialized = true;
  962. unsigned int oldmtu = _mtu;
  963. setFriendlyName(_friendlyName.c_str());
  964. while (_run) {
  965. DWORD waitResult = WaitForMultipleObjectsEx(writeInProgress ? 3 : 2,wait4,FALSE,2500,TRUE);
  966. if (!_run) break; // will also break outer while(_run) since _run is false
  967. // Check for changes in MTU and break to restart tap device to reconfigure in this case
  968. if (_mtu != oldmtu)
  969. break;
  970. // Check for issues with adapter and close/reopen if any are detected. This
  971. // check fixes a while boatload of Windows adapter 'coma' issues after
  972. // sleep/wake and when adapters are added/removed. Basically if the tap
  973. // device is borked, whack it.
  974. {
  975. ULONGLONG tc = GetTickCount64();
  976. if ((tc - timeOfLastBorkCheck) >= 2500) {
  977. timeOfLastBorkCheck = tc;
  978. char aabuf[16384];
  979. ULONG aalen = sizeof(aabuf);
  980. if (GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_SKIP_UNICAST|GAA_FLAG_SKIP_ANYCAST|GAA_FLAG_SKIP_MULTICAST|GAA_FLAG_SKIP_DNS_SERVER|GAA_FLAG_SKIP_FRIENDLY_NAME,(void *)0,reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf),&aalen) == NO_ERROR) {
  981. bool isBorked = false;
  982. PIP_ADAPTER_ADDRESSES aa = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(aabuf);
  983. while (aa) {
  984. if (_deviceLuid.Value == aa->Luid.Value) {
  985. isBorked = (aa->OperStatus != IfOperStatusUp);
  986. break;
  987. }
  988. aa = aa->Next;
  989. }
  990. if (isBorked) {
  991. // Close and reopen tap device if there's an issue (outer loop)
  992. break;
  993. }
  994. }
  995. }
  996. }
  997. if ((waitResult == WAIT_TIMEOUT)||(waitResult == WAIT_FAILED)) {
  998. Sleep(250); // guard against spinning under some conditions
  999. continue;
  1000. }
  1001. if (HasOverlappedIoCompleted(&tapOvlRead)) {
  1002. DWORD bytesRead = 0;
  1003. if (GetOverlappedResult(_tap,&tapOvlRead,&bytesRead,FALSE)) {
  1004. if ((bytesRead > 14)&&(_enabled)) {
  1005. MAC to(tapReadBuf,6);
  1006. MAC from(tapReadBuf + 6,6);
  1007. unsigned int etherType = ((((unsigned int)tapReadBuf[12]) & 0xff) << 8) | (((unsigned int)tapReadBuf[13]) & 0xff);
  1008. try {
  1009. _handler(_arg,(void *)0,_nwid,from,to,etherType,0,tapReadBuf + 14,bytesRead - 14);
  1010. } catch ( ... ) {} // handlers should not throw
  1011. }
  1012. }
  1013. ReadFile(_tap,tapReadBuf,ZT_MAX_MTU + 32,NULL,&tapOvlRead);
  1014. }
  1015. if (writeInProgress) {
  1016. if (HasOverlappedIoCompleted(&tapOvlWrite)) {
  1017. writeInProgress = false;
  1018. _injectPending_m.lock();
  1019. _injectPending.pop();
  1020. } else continue; // still writing, so skip code below and wait
  1021. } else _injectPending_m.lock();
  1022. if (!_injectPending.empty()) {
  1023. WriteFile(_tap,_injectPending.front().data,_injectPending.front().len,NULL,&tapOvlWrite);
  1024. writeInProgress = true;
  1025. }
  1026. _injectPending_m.unlock();
  1027. }
  1028. CancelIo(_tap);
  1029. CloseHandle(tapOvlRead.hEvent);
  1030. CloseHandle(tapOvlWrite.hEvent);
  1031. CloseHandle(_tap);
  1032. _tap = INVALID_HANDLE_VALUE;
  1033. // We will restart and re-open the tap unless _run == false
  1034. }
  1035. } catch ( ... ) {} // catch unexpected exceptions -- this should not happen but would prevent program crash or other weird issues since threads should not throw
  1036. CoUninitialize();
  1037. }
  1038. NET_IFINDEX WindowsEthernetTap::_getDeviceIndex()
  1039. {
  1040. MIB_IF_TABLE2 *ift = (MIB_IF_TABLE2 *)0;
  1041. if (GetIfTable2Ex(MibIfTableRaw,&ift) != NO_ERROR)
  1042. throw std::runtime_error("GetIfTable2Ex() failed");
  1043. if (ift->NumEntries > 0) {
  1044. for(ULONG i=0;i<ift->NumEntries;++i) {
  1045. if (ift->Table[i].InterfaceLuid.Value == _deviceLuid.Value) {
  1046. NET_IFINDEX idx = ift->Table[i].InterfaceIndex;
  1047. FreeMibTable(ift);
  1048. return idx;
  1049. }
  1050. }
  1051. }
  1052. FreeMibTable(&ift);
  1053. throw std::runtime_error("interface not found");
  1054. }
  1055. std::vector<std::string> WindowsEthernetTap::_getRegistryIPv4Value(const char *regKey)
  1056. {
  1057. std::vector<std::string> value;
  1058. HKEY tcpIpInterfaces;
  1059. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ|KEY_WRITE,&tcpIpInterfaces) == ERROR_SUCCESS) {
  1060. char buf[16384];
  1061. DWORD len = sizeof(buf);
  1062. DWORD kt = REG_MULTI_SZ;
  1063. if (RegGetValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),regKey,0,&kt,&buf,&len) == ERROR_SUCCESS) {
  1064. switch(kt) {
  1065. case REG_SZ:
  1066. if (len > 0)
  1067. value.push_back(std::string(buf));
  1068. break;
  1069. case REG_MULTI_SZ: {
  1070. for(DWORD k=0,s=0;k<len;++k) {
  1071. if (!buf[k]) {
  1072. if (s < k) {
  1073. value.push_back(std::string(buf + s));
  1074. s = k + 1;
  1075. } else break;
  1076. }
  1077. }
  1078. } break;
  1079. }
  1080. }
  1081. RegCloseKey(tcpIpInterfaces);
  1082. }
  1083. return value;
  1084. }
  1085. void WindowsEthernetTap::_setRegistryIPv4Value(const char *regKey,const std::vector<std::string> &value)
  1086. {
  1087. std::string regMulti;
  1088. for(std::vector<std::string>::const_iterator s(value.begin());s!=value.end();++s) {
  1089. regMulti.append(*s);
  1090. regMulti.push_back((char)0);
  1091. }
  1092. HKEY tcpIpInterfaces;
  1093. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces",0,KEY_READ|KEY_WRITE,&tcpIpInterfaces) == ERROR_SUCCESS) {
  1094. if (regMulti.length() > 0) {
  1095. regMulti.push_back((char)0);
  1096. RegSetKeyValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),regKey,REG_MULTI_SZ,regMulti.data(),(DWORD)regMulti.length());
  1097. } else {
  1098. RegDeleteKeyValueA(tcpIpInterfaces,_netCfgInstanceId.c_str(),regKey);
  1099. }
  1100. RegCloseKey(tcpIpInterfaces);
  1101. }
  1102. }
  1103. void WindowsEthernetTap::_syncIps()
  1104. {
  1105. // assumes _assignedIps_m is locked
  1106. if (!_initialized)
  1107. return;
  1108. std::vector<InetAddress> haveIps(ips());
  1109. for(std::vector<InetAddress>::const_iterator aip(_assignedIps.begin());aip!=_assignedIps.end();++aip) {
  1110. if (std::find(haveIps.begin(),haveIps.end(),*aip) == haveIps.end()) {
  1111. MIB_UNICASTIPADDRESS_ROW ipr;
  1112. InitializeUnicastIpAddressEntry(&ipr);
  1113. if (aip->isV4()) {
  1114. ipr.Address.Ipv4.sin_family = AF_INET;
  1115. ipr.Address.Ipv4.sin_addr.S_un.S_addr = *((const uint32_t *)aip->rawIpData());
  1116. ipr.OnLinkPrefixLength = aip->netmaskBits();
  1117. if (ipr.OnLinkPrefixLength >= 32)
  1118. continue;
  1119. } else if (aip->isV6()) {
  1120. ipr.Address.Ipv6.sin6_family = AF_INET6;
  1121. memcpy(ipr.Address.Ipv6.sin6_addr.u.Byte,aip->rawIpData(),16);
  1122. ipr.OnLinkPrefixLength = aip->netmaskBits();
  1123. if (ipr.OnLinkPrefixLength >= 128)
  1124. continue;
  1125. } else continue;
  1126. ipr.PrefixOrigin = IpPrefixOriginManual;
  1127. ipr.SuffixOrigin = IpSuffixOriginManual;
  1128. ipr.ValidLifetime = 0xffffffff;
  1129. ipr.PreferredLifetime = 0xffffffff;
  1130. ipr.InterfaceLuid = _deviceLuid;
  1131. ipr.InterfaceIndex = _getDeviceIndex();
  1132. CreateUnicastIpAddressEntry(&ipr);
  1133. }
  1134. if (aip->isV4()) {
  1135. char ipbuf[64];
  1136. std::string ipStr(aip->toIpString(ipbuf));
  1137. std::vector<std::string> regIps(_getRegistryIPv4Value("IPAddress"));
  1138. if (std::find(regIps.begin(), regIps.end(), ipStr) == regIps.end()) {
  1139. std::vector<std::string> regSubnetMasks(_getRegistryIPv4Value("SubnetMask"));
  1140. regIps.push_back(ipStr);
  1141. regSubnetMasks.push_back(aip->netmask().toIpString(ipbuf));
  1142. _setRegistryIPv4Value("IPAddress", regIps);
  1143. _setRegistryIPv4Value("SubnetMask", regSubnetMasks);
  1144. }
  1145. }
  1146. }
  1147. }
  1148. void WindowsEthernetTap::setDns(const char* domain, const std::vector<InetAddress>& servers)
  1149. {
  1150. WinDNSHelper::setDNS(_nwid, domain, servers);
  1151. }
  1152. } // namespace ZeroTier