TAPDevice.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "util/Bits.h"
  16. #include "exception/Except.h"
  17. #include "exception/WinFail.h"
  18. #include "memory/Allocator.h"
  19. #include "interface/tuntap/windows/TAPDevice.h"
  20. /*
  21. * Portions of this code are copied from QEMU project which is licensed
  22. * under GPLv2 or greater, further contributions are licensed under GPLv3
  23. * or greater.
  24. */
  25. /*
  26. * TAP-Win32 -- A kernel driver to provide virtual tap device functionality
  27. * on win32. Originally derived from the CIPE-Win32
  28. * project by Damion K. Wilson, with extensive modifications by
  29. * James Yonan.
  30. *
  31. * All source code which derives from the CIPE-Win32 project is
  32. * Copyright (C) Damion K. Wilson, 2003, and is released under the
  33. * GPL version 2 (see below).
  34. *
  35. * All other source code is Copyright (C) James Yonan, 2003-2004,
  36. * and is released under the GPL version 2 (see below).
  37. *
  38. * This program is free software; you can redistribute it and/or modify
  39. * it under the terms of the GNU General Public License as published by
  40. * the Free Software Foundation; either version 2 of the License, or
  41. * (at your option) any later version.
  42. *
  43. * This program is distributed in the hope that it will be useful,
  44. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  45. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  46. * GNU General Public License for more details.
  47. *
  48. * You should have received a copy of the GNU General Public License
  49. * along with this program (see the file COPYING included with this
  50. * distribution); if not, write to the Free Software Foundation, Inc.,
  51. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  52. */
  53. #include <stdio.h>
  54. #include <windows.h>
  55. #define NETWORK_ADAPTER_GUID "{4D36E972-E325-11CE-BFC1-08002BE10318}"
  56. #define ADAPTER_KEY \
  57. "SYSTEM\\CurrentControlSet\\Control\\Class\\" NETWORK_ADAPTER_GUID
  58. #define NETWORK_CONNECTIONS_KEY \
  59. "SYSTEM\\CurrentControlSet\\Control\\Network\\" NETWORK_ADAPTER_GUID
  60. #define USERMODEDEVICEDIR "\\\\.\\Global\\"
  61. #define TAPSUFFIX ".tap"
  62. static int is_tap_win32_dev(const char *guid)
  63. {
  64. HKEY netcard_key;
  65. LONG status;
  66. DWORD len;
  67. int i = 0;
  68. status = RegOpenKeyEx(
  69. HKEY_LOCAL_MACHINE,
  70. ADAPTER_KEY,
  71. 0,
  72. KEY_READ,
  73. &netcard_key);
  74. if (status != ERROR_SUCCESS) {
  75. return FALSE;
  76. }
  77. for (;;) {
  78. char enum_name[256];
  79. char unit_string[256];
  80. HKEY unit_key;
  81. char component_id_string[] = "ComponentId";
  82. char component_id[256];
  83. char net_cfg_instance_id_string[] = "NetCfgInstanceId";
  84. char net_cfg_instance_id[256];
  85. DWORD data_type;
  86. len = sizeof (enum_name);
  87. status = RegEnumKeyEx(
  88. netcard_key,
  89. i,
  90. enum_name,
  91. &len,
  92. NULL,
  93. NULL,
  94. NULL,
  95. NULL);
  96. if (status == ERROR_NO_MORE_ITEMS) {
  97. break;
  98. } else if (status != ERROR_SUCCESS) {
  99. return FALSE;
  100. }
  101. snprintf (unit_string, sizeof(unit_string), "%s\\%s",
  102. ADAPTER_KEY, enum_name);
  103. status = RegOpenKeyEx(
  104. HKEY_LOCAL_MACHINE,
  105. unit_string,
  106. 0,
  107. KEY_READ,
  108. &unit_key);
  109. if (status != ERROR_SUCCESS) {
  110. return FALSE;
  111. } else {
  112. len = sizeof (component_id);
  113. status = RegQueryValueEx(
  114. unit_key,
  115. component_id_string,
  116. NULL,
  117. &data_type,
  118. (uint8_t*)component_id,
  119. &len);
  120. if (!(status != ERROR_SUCCESS || data_type != REG_SZ)) {
  121. len = sizeof (net_cfg_instance_id);
  122. status = RegQueryValueEx(
  123. unit_key,
  124. net_cfg_instance_id_string,
  125. NULL,
  126. &data_type,
  127. (uint8_t*)net_cfg_instance_id,
  128. &len);
  129. if (status == ERROR_SUCCESS && data_type == REG_SZ) {
  130. if (!Bits_memcmp(component_id, "tap", CString_strlen("tap")) &&
  131. !strcmp (net_cfg_instance_id, guid)) {
  132. RegCloseKey (unit_key);
  133. RegCloseKey (netcard_key);
  134. return TRUE;
  135. }
  136. }
  137. }
  138. RegCloseKey (unit_key);
  139. }
  140. ++i;
  141. }
  142. RegCloseKey (netcard_key);
  143. return FALSE;
  144. }
  145. static int get_device_guid(
  146. char *name,
  147. int name_size,
  148. char *actual_name,
  149. int actual_name_size,
  150. struct Except* eh)
  151. {
  152. LONG status;
  153. HKEY control_net_key;
  154. DWORD len;
  155. WinFail_check(eh, (
  156. RegOpenKeyEx(HKEY_LOCAL_MACHINE, NETWORK_CONNECTIONS_KEY, 0, KEY_READ, &control_net_key)
  157. ));
  158. int stop = 0;
  159. for (int i = 0; !stop; i++) {
  160. char enum_name[256];
  161. char connection_string[256];
  162. HKEY connKey;
  163. char name_data[256];
  164. DWORD name_type;
  165. const char name_string[] = "Name";
  166. len = sizeof (enum_name);
  167. status = RegEnumKeyEx(control_net_key, i, enum_name, &len, NULL, NULL, NULL, NULL);
  168. if (status == ERROR_NO_MORE_ITEMS) {
  169. break;
  170. } else if (status != ERROR_SUCCESS) {
  171. WinFail_fail(eh, "RegEnumKeyEx() failed", status);
  172. }
  173. if (len != CString_strlen(NETWORK_ADAPTER_GUID)) {
  174. // extranious directory, eg: "Descriptions"
  175. continue;
  176. }
  177. snprintf(connection_string,
  178. sizeof(connection_string),
  179. "%s\\%s\\Connection",
  180. NETWORK_CONNECTIONS_KEY, enum_name);
  181. WinFail_check(eh, (
  182. RegOpenKeyEx(HKEY_LOCAL_MACHINE, connection_string, 0, KEY_READ, &connKey)
  183. ));
  184. len = sizeof (name_data);
  185. WinFail_check(eh, (
  186. RegQueryValueEx(connKey, name_string, NULL, &name_type, (uint8_t*)name_data, &len)
  187. ));
  188. if (name_type != REG_SZ) {
  189. WinFail_fail(eh, "RegQueryValueEx() name_type != REG_SZ", status);
  190. }
  191. if (is_tap_win32_dev(enum_name)) {
  192. snprintf(name, name_size, "%s", enum_name);
  193. if (actual_name) {
  194. if (CString_strcmp(actual_name, "") != 0) {
  195. if (CString_strcmp(name_data, actual_name) != 0) {
  196. RegCloseKey (connKey);
  197. ++i;
  198. continue;
  199. }
  200. }
  201. else {
  202. snprintf(actual_name, actual_name_size, "%s", name_data);
  203. }
  204. }
  205. stop = 1;
  206. }
  207. RegCloseKey(connKey);
  208. }
  209. RegCloseKey (control_net_key);
  210. if (stop == 0) {
  211. return -1;
  212. }
  213. return 0;
  214. }
  215. struct TAPDevice* TAPDevice_find(const char* preferredName,
  216. struct Except* eh,
  217. struct Allocator* alloc)
  218. {
  219. #define BUFF_SZ 0x100
  220. char guid[BUFF_SZ] = {0};
  221. char buff[BUFF_SZ] = {0};
  222. if (preferredName != NULL) {
  223. snprintf(buff, sizeof(buff), "%s", preferredName);
  224. }
  225. if (get_device_guid(guid, sizeof(guid), buff, sizeof(buff), eh)) {
  226. return NULL;
  227. }
  228. struct TAPDevice* out = Allocator_malloc(alloc, sizeof(struct TAPDevice));
  229. out->name = Allocator_malloc(alloc, CString_strlen(buff)+1);
  230. Bits_memcpy(out->name, buff, CString_strlen(buff)+1);
  231. snprintf(buff, sizeof(buff), USERMODEDEVICEDIR "%s" TAPSUFFIX, guid);
  232. out->path = Allocator_malloc(alloc, CString_strlen(buff)+1);
  233. Bits_memcpy(out->path, buff, CString_strlen(buff)+1);
  234. return out;
  235. }