tapwin32-funcs.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @file tapwin32-funcs.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <misc/debug.h>
  27. #include "wintap-common.h"
  28. #include <tuntap/tapwin32-funcs.h>
  29. static int split_spec (char *name, char *sep, char *out_fields[], int num_fields)
  30. {
  31. ASSERT(num_fields > 0)
  32. ASSERT(strlen(sep) > 0)
  33. size_t seplen = strlen(sep);
  34. int i;
  35. for (i = 0; i < num_fields - 1; i++) {
  36. char *s = strstr(name, sep);
  37. if (!s) {
  38. DEBUG("missing separator number %d", (i + 1));
  39. return 0;
  40. }
  41. int flen = s - name;
  42. memcpy(out_fields[i], name, flen);
  43. out_fields[i][flen] = '\0';
  44. name = s + seplen;
  45. }
  46. int flen = strlen(name);
  47. memcpy(out_fields[i], name, flen);
  48. out_fields[i][flen] = '\0';
  49. return 1;
  50. }
  51. static int parse_ipv4_addr (char *name, uint8_t out_addr[4])
  52. {
  53. if (strlen(name) > 15) {
  54. return 0;
  55. }
  56. char (nums[4])[16];
  57. char *out_fields[] = { nums[0], nums[1], nums[2], nums[3] };
  58. if (!split_spec(name, ".", out_fields, 4)) {
  59. return 0;
  60. }
  61. for (int i = 0; i < 4; i++) {
  62. if (strlen(nums[i]) > 3) {
  63. return 0;
  64. }
  65. int num = atoi(nums[i]);
  66. if (!(num >= 0 && num < 256)) {
  67. return 0;
  68. }
  69. out_addr[i] = num;
  70. }
  71. return 1;
  72. }
  73. int tapwin32_parse_tap_spec (char *name, char *out_component_id, char *out_human_name)
  74. {
  75. char *out_fields[] = { out_component_id, out_human_name };
  76. return split_spec(name, ":", out_fields, 2);
  77. }
  78. int tapwin32_parse_tun_spec (char *name, char *out_component_id, char *out_human_name, uint32_t out_addrs[3])
  79. {
  80. int namelen = strlen(name);
  81. char (addr_strs[3])[namelen + 1];
  82. char *out_fields[] = { out_component_id, out_human_name, addr_strs[0], addr_strs[1], addr_strs[2] };
  83. if (!split_spec(name, ":", out_fields, 5)) {
  84. return 0;
  85. }
  86. for (int i = 0; i < 3; i++) {
  87. if (!parse_ipv4_addr(addr_strs[i], (uint8_t *)(out_addrs + i))) {
  88. return 0;
  89. }
  90. }
  91. return 1;
  92. }
  93. int tapwin32_find_device (char *device_component_id, char *device_name, char (*device_path)[TAPWIN32_MAX_REG_SIZE])
  94. {
  95. // open adapter key
  96. // used to find all devices with the given ComponentId
  97. HKEY adapter_key;
  98. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, ADAPTER_KEY, 0, KEY_READ, &adapter_key) != ERROR_SUCCESS) {
  99. DEBUG("Error opening adapter key");
  100. return 0;
  101. }
  102. char net_cfg_instance_id[TAPWIN32_MAX_REG_SIZE];
  103. int found = 0;
  104. DWORD i;
  105. for (i = 0;; i++) {
  106. DWORD len;
  107. DWORD type;
  108. char key_name[TAPWIN32_MAX_REG_SIZE];
  109. len = sizeof(key_name);
  110. if (RegEnumKeyEx(adapter_key, i, key_name, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) {
  111. break;
  112. }
  113. char unit_string[TAPWIN32_MAX_REG_SIZE];
  114. snprintf(unit_string, sizeof(unit_string), "%s\\%s", ADAPTER_KEY, key_name);
  115. HKEY unit_key;
  116. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, unit_string, 0, KEY_READ, &unit_key) != ERROR_SUCCESS) {
  117. continue;
  118. }
  119. char component_id[TAPWIN32_MAX_REG_SIZE];
  120. len = sizeof(component_id);
  121. if (RegQueryValueEx(unit_key, "ComponentId", NULL, &type, component_id, &len) != ERROR_SUCCESS || type != REG_SZ) {
  122. ASSERT_FORCE(RegCloseKey(unit_key) == ERROR_SUCCESS)
  123. continue;
  124. }
  125. len = sizeof(net_cfg_instance_id);
  126. if (RegQueryValueEx(unit_key, "NetCfgInstanceId", NULL, &type, net_cfg_instance_id, &len) != ERROR_SUCCESS || type != REG_SZ) {
  127. ASSERT_FORCE(RegCloseKey(unit_key) == ERROR_SUCCESS)
  128. continue;
  129. }
  130. RegCloseKey(unit_key);
  131. // check if ComponentId matches
  132. if (!strcmp(component_id, device_component_id)) {
  133. // if no name was given, use the first device with the given ComponentId
  134. if (!device_name) {
  135. found = 1;
  136. break;
  137. }
  138. // open connection key
  139. char conn_string[TAPWIN32_MAX_REG_SIZE];
  140. snprintf(conn_string, sizeof(conn_string), "%s\\%s\\Connection", NETWORK_CONNECTIONS_KEY, net_cfg_instance_id);
  141. HKEY conn_key;
  142. if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, conn_string, 0, KEY_READ, &conn_key) != ERROR_SUCCESS) {
  143. continue;
  144. }
  145. // read name
  146. char name[TAPWIN32_MAX_REG_SIZE];
  147. len = sizeof(name);
  148. if (RegQueryValueEx(conn_key, "Name", NULL, &type, name, &len) != ERROR_SUCCESS || type != REG_SZ) {
  149. ASSERT_FORCE(RegCloseKey(conn_key) == ERROR_SUCCESS)
  150. continue;
  151. }
  152. ASSERT_FORCE(RegCloseKey(conn_key) == ERROR_SUCCESS)
  153. // check name
  154. if (!strcmp(name, device_name)) {
  155. found = 1;
  156. break;
  157. }
  158. }
  159. }
  160. ASSERT_FORCE(RegCloseKey(adapter_key) == ERROR_SUCCESS)
  161. if (!found) {
  162. return 0;
  163. }
  164. snprintf(*device_path, sizeof(*device_path), "%s%s%s", USERMODEDEVICEDIR, net_cfg_instance_id, TAPSUFFIX);
  165. return 1;
  166. }