WindowsEthernetTapFactory.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. #include "WindowsEthernetTapFactory.hpp"
  28. #include "WindowsEthernetTap.hpp"
  29. namespace ZeroTier {
  30. WindowsEthernetTapFactory::Env::Env()
  31. {
  32. #ifdef _WIN64
  33. is64Bit = TRUE;
  34. devcon = "\\devcon_x64.exe";
  35. tapDriver = "\\tap-windows\\x64\\zttap200.inf";
  36. #else
  37. is64Bit = FALSE;
  38. IsWow64Process(GetCurrentProcess(),&is64Bit);
  39. devcon = ((is64Bit == TRUE) ? "\\devcon_x64.exe" : "\\devcon_x86.exe");
  40. tapDriver = ((is64Bit == TRUE) ? "\\tap-windows\\x64\\zttap200.inf" : "\\tap-windows\\x86\\zttap200.inf");
  41. #endif
  42. }
  43. WindowsEthernetTapFactory::WindowsEthernetTapFactory(const char *pathToHelpers) :
  44. _pathToHelpers(pathToHelpers)
  45. {
  46. }
  47. WindowsEthernetTapFactory::~WindowsEthernetTapFactory()
  48. {
  49. Mutex::Lock _l(_devices_m);
  50. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d)
  51. delete *d;
  52. }
  53. EthernetTap *WindowsEthernetTapFactory::open(
  54. const MAC &mac,
  55. unsigned int mtu,
  56. unsigned int metric,
  57. uint64_t nwid,
  58. const char *desiredDevice,
  59. const char *friendlyName,
  60. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  61. void *arg)
  62. {
  63. Mutex::Lock _l(_devices_m);
  64. EthernetTap *t = new WindowsEthernetTap(_pathToHelpers.c_str(),mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  65. _devices.push_back(t);
  66. return t;
  67. }
  68. void WindowsEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  69. {
  70. std::string instanceId(((WindowsEthernetTap *)tap)->instanceId());
  71. Mutex::Lock _l(_devices_m);
  72. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d) {
  73. if (*d == tap) {
  74. _devices.erase(d);
  75. break;
  76. }
  77. }
  78. delete tap;
  79. if (destroyPersistentDevices)
  80. _deletePersistentTapDevice(_pathToHelpers.c_str(),instanceId.c_str());
  81. }
  82. void WindowsEthernetTapFactory::destroyAllPersistentTapDevices(const char *pathToHelpers)
  83. {
  84. char subkeyName[4096];
  85. char subkeyClass[4096];
  86. char data[4096];
  87. std::set<std::string> instanceIdPathsToRemove;
  88. {
  89. HKEY nwAdapters;
  90. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  91. return;
  92. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  93. DWORD type;
  94. DWORD dataLen;
  95. DWORD subkeyNameLen = sizeof(subkeyName);
  96. DWORD subkeyClassLen = sizeof(subkeyClass);
  97. FILETIME lastWriteTime;
  98. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  99. type = 0;
  100. dataLen = sizeof(data);
  101. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  102. data[dataLen] = '\0';
  103. if (!strnicmp(data,"zttap",5)) {
  104. std::string instanceIdPath;
  105. type = 0;
  106. dataLen = sizeof(data);
  107. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  108. instanceIdPath.assign(data,dataLen);
  109. if (instanceIdPath.length() != 0) {
  110. instanceIdPathsToRemove.insert(instanceIdPath);
  111. /*
  112. type = 0;
  113. dataLen = sizeof(data);
  114. if (RegGetValueA(nwAdapters,subkeyName,"_ZeroTierTapIdentifier",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  115. if (dataLen <= 0) {
  116. instanceIdPathsToRemove.insert(instanceIdPath);
  117. } else {
  118. instanceIdPathsToRemove.insert(instanceIdPath);
  119. }
  120. }
  121. */
  122. }
  123. }
  124. }
  125. } else break; // end of list or failure
  126. }
  127. RegCloseKey(nwAdapters);
  128. }
  129. for(std::set<std::string>::iterator iidp(instanceIdPathsToRemove.begin());iidp!=instanceIdPathsToRemove.end();++iidp)
  130. _deletePersistentTapDevice(pathToHelpers,iidp->c_str());
  131. }
  132. void WindowsEthernetTapFactory::_deletePersistentTapDevice(const char *pathToHelpers,const char *instanceId)
  133. {
  134. HANDLE devconLog = CreateFileA((std::string(pathToHelpers) + "\\devcon.log").c_str(),GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  135. STARTUPINFOA startupInfo;
  136. startupInfo.cb = sizeof(startupInfo);
  137. if (devconLog != INVALID_HANDLE_VALUE) {
  138. SetFilePointer(devconLog,0,0,FILE_END);
  139. startupInfo.hStdOutput = devconLog;
  140. startupInfo.hStdError = devconLog;
  141. }
  142. PROCESS_INFORMATION processInfo;
  143. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  144. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  145. if (CreateProcessA(NULL,(LPSTR)(std::string("\"") + pathToHelpers + WINENV.devcon + "\" remove @" + instanceId).c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
  146. WaitForSingleObject(processInfo.hProcess,INFINITE);
  147. CloseHandle(processInfo.hProcess);
  148. CloseHandle(processInfo.hThread);
  149. }
  150. if (devconLog != INVALID_HANDLE_VALUE)
  151. CloseHandle(devconLog);
  152. }
  153. } // namespace ZeroTier