WindowsEthernetTapFactory.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. const WindowsEthernetTapFactory::Env WindowsEthernetTapFactory::WINENV;
  44. WindowsEthernetTapFactory::WindowsEthernetTapFactory(const char *pathToHelpers) :
  45. _pathToHelpers(pathToHelpers)
  46. {
  47. }
  48. WindowsEthernetTapFactory::~WindowsEthernetTapFactory()
  49. {
  50. Mutex::Lock _l(_devices_m);
  51. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d)
  52. delete *d;
  53. }
  54. EthernetTap *WindowsEthernetTapFactory::open(
  55. const MAC &mac,
  56. unsigned int mtu,
  57. unsigned int metric,
  58. uint64_t nwid,
  59. const char *desiredDevice,
  60. const char *friendlyName,
  61. void (*handler)(void *,const MAC &,const MAC &,unsigned int,const Buffer<4096> &),
  62. void *arg)
  63. {
  64. Mutex::Lock _l(_devices_m);
  65. EthernetTap *t = new WindowsEthernetTap(_pathToHelpers.c_str(),mac,mtu,metric,nwid,desiredDevice,friendlyName,handler,arg);
  66. _devices.push_back(t);
  67. return t;
  68. }
  69. void WindowsEthernetTapFactory::close(EthernetTap *tap,bool destroyPersistentDevices)
  70. {
  71. if (!tap)
  72. return;
  73. std::string instanceId(((WindowsEthernetTap *)tap)->instanceId());
  74. Mutex::Lock _l(_devices_m);
  75. for(std::vector<EthernetTap *>::iterator d(_devices.begin());d!=_devices.end();++d) {
  76. if (*d == tap) {
  77. _devices.erase(d);
  78. break;
  79. }
  80. }
  81. delete tap;
  82. if (destroyPersistentDevices)
  83. _deletePersistentTapDevice(_pathToHelpers.c_str(),instanceId.c_str());
  84. }
  85. void WindowsEthernetTapFactory::destroyAllPersistentTapDevices(const char *pathToHelpers)
  86. {
  87. char subkeyName[4096];
  88. char subkeyClass[4096];
  89. char data[4096];
  90. std::set<std::string> instanceIdPathsToRemove;
  91. {
  92. HKEY nwAdapters;
  93. if (RegOpenKeyExA(HKEY_LOCAL_MACHINE,"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002BE10318}",0,KEY_READ|KEY_WRITE,&nwAdapters) != ERROR_SUCCESS)
  94. return;
  95. for(DWORD subkeyIndex=0;;++subkeyIndex) {
  96. DWORD type;
  97. DWORD dataLen;
  98. DWORD subkeyNameLen = sizeof(subkeyName);
  99. DWORD subkeyClassLen = sizeof(subkeyClass);
  100. FILETIME lastWriteTime;
  101. if (RegEnumKeyExA(nwAdapters,subkeyIndex,subkeyName,&subkeyNameLen,(DWORD *)0,subkeyClass,&subkeyClassLen,&lastWriteTime) == ERROR_SUCCESS) {
  102. type = 0;
  103. dataLen = sizeof(data);
  104. if (RegGetValueA(nwAdapters,subkeyName,"ComponentId",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS) {
  105. data[dataLen] = '\0';
  106. if (!strnicmp(data,"zttap",5)) {
  107. std::string instanceIdPath;
  108. type = 0;
  109. dataLen = sizeof(data);
  110. if (RegGetValueA(nwAdapters,subkeyName,"DeviceInstanceID",RRF_RT_ANY,&type,(PVOID)data,&dataLen) == ERROR_SUCCESS)
  111. instanceIdPath.assign(data,dataLen);
  112. if (instanceIdPath.length() != 0)
  113. instanceIdPathsToRemove.insert(instanceIdPath);
  114. }
  115. }
  116. } else break; // end of list or failure
  117. }
  118. RegCloseKey(nwAdapters);
  119. }
  120. for(std::set<std::string>::iterator iidp(instanceIdPathsToRemove.begin());iidp!=instanceIdPathsToRemove.end();++iidp)
  121. _deletePersistentTapDevice(pathToHelpers,iidp->c_str());
  122. }
  123. void WindowsEthernetTapFactory::_deletePersistentTapDevice(const char *pathToHelpers,const char *instanceId)
  124. {
  125. 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);
  126. STARTUPINFOA startupInfo;
  127. startupInfo.cb = sizeof(startupInfo);
  128. if (devconLog != INVALID_HANDLE_VALUE) {
  129. SetFilePointer(devconLog,0,0,FILE_END);
  130. startupInfo.hStdOutput = devconLog;
  131. startupInfo.hStdError = devconLog;
  132. }
  133. PROCESS_INFORMATION processInfo;
  134. memset(&startupInfo,0,sizeof(STARTUPINFOA));
  135. memset(&processInfo,0,sizeof(PROCESS_INFORMATION));
  136. if (CreateProcessA(NULL,(LPSTR)(std::string("\"") + pathToHelpers + WINENV.devcon + "\" remove @" + instanceId).c_str(),NULL,NULL,FALSE,0,NULL,NULL,&startupInfo,&processInfo)) {
  137. WaitForSingleObject(processInfo.hProcess,INFINITE);
  138. CloseHandle(processInfo.hProcess);
  139. CloseHandle(processInfo.hThread);
  140. }
  141. if (devconLog != INVALID_HANDLE_VALUE)
  142. CloseHandle(devconLog);
  143. }
  144. } // namespace ZeroTier