You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
10 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/win32.h"
  11. #include <winsock2.h>
  12. #include <ws2tcpip.h>
  13. #include <algorithm>
  14. #include "rtc_base/arraysize.h"
  15. #include "rtc_base/byte_order.h"
  16. #include "rtc_base/checks.h"
  17. #include "rtc_base/logging.h"
  18. #include "rtc_base/string_utils.h"
  19. namespace rtc {
  20. // Helper function declarations for inet_ntop/inet_pton.
  21. static const char* inet_ntop_v4(const void* src, char* dst, socklen_t size);
  22. static const char* inet_ntop_v6(const void* src, char* dst, socklen_t size);
  23. static int inet_pton_v4(const char* src, void* dst);
  24. static int inet_pton_v6(const char* src, void* dst);
  25. // Implementation of inet_ntop (create a printable representation of an
  26. // ip address). XP doesn't have its own inet_ntop, and
  27. // WSAAddressToString requires both IPv6 to be installed and for Winsock
  28. // to be initialized.
  29. const char* win32_inet_ntop(int af,
  30. const void* src,
  31. char* dst,
  32. socklen_t size) {
  33. if (!src || !dst) {
  34. return nullptr;
  35. }
  36. switch (af) {
  37. case AF_INET: {
  38. return inet_ntop_v4(src, dst, size);
  39. }
  40. case AF_INET6: {
  41. return inet_ntop_v6(src, dst, size);
  42. }
  43. }
  44. return nullptr;
  45. }
  46. // As above, but for inet_pton. Implements inet_pton for v4 and v6.
  47. // Note that our inet_ntop will output normal 'dotted' v4 addresses only.
  48. int win32_inet_pton(int af, const char* src, void* dst) {
  49. if (!src || !dst) {
  50. return 0;
  51. }
  52. if (af == AF_INET) {
  53. return inet_pton_v4(src, dst);
  54. } else if (af == AF_INET6) {
  55. return inet_pton_v6(src, dst);
  56. }
  57. return -1;
  58. }
  59. // Helper function for inet_ntop for IPv4 addresses.
  60. // Outputs "dotted-quad" decimal notation.
  61. const char* inet_ntop_v4(const void* src, char* dst, socklen_t size) {
  62. if (size < INET_ADDRSTRLEN) {
  63. return nullptr;
  64. }
  65. const struct in_addr* as_in_addr =
  66. reinterpret_cast<const struct in_addr*>(src);
  67. snprintf(dst, size, "%d.%d.%d.%d", as_in_addr->S_un.S_un_b.s_b1,
  68. as_in_addr->S_un.S_un_b.s_b2, as_in_addr->S_un.S_un_b.s_b3,
  69. as_in_addr->S_un.S_un_b.s_b4);
  70. return dst;
  71. }
  72. // Helper function for inet_ntop for IPv6 addresses.
  73. const char* inet_ntop_v6(const void* src, char* dst, socklen_t size) {
  74. if (size < INET6_ADDRSTRLEN) {
  75. return nullptr;
  76. }
  77. const uint16_t* as_shorts = reinterpret_cast<const uint16_t*>(src);
  78. int runpos[8];
  79. int current = 1;
  80. int max = 0;
  81. int maxpos = -1;
  82. int run_array_size = arraysize(runpos);
  83. // Run over the address marking runs of 0s.
  84. for (int i = 0; i < run_array_size; ++i) {
  85. if (as_shorts[i] == 0) {
  86. runpos[i] = current;
  87. if (current > max) {
  88. maxpos = i;
  89. max = current;
  90. }
  91. ++current;
  92. } else {
  93. runpos[i] = -1;
  94. current = 1;
  95. }
  96. }
  97. if (max > 0) {
  98. int tmpmax = maxpos;
  99. // Run back through, setting -1 for all but the longest run.
  100. for (int i = run_array_size - 1; i >= 0; i--) {
  101. if (i > tmpmax) {
  102. runpos[i] = -1;
  103. } else if (runpos[i] == -1) {
  104. // We're less than maxpos, we hit a -1, so the 'good' run is done.
  105. // Setting tmpmax -1 means all remaining positions get set to -1.
  106. tmpmax = -1;
  107. }
  108. }
  109. }
  110. char* cursor = dst;
  111. // Print IPv4 compatible and IPv4 mapped addresses using the IPv4 helper.
  112. // These addresses have an initial run of either eight zero-bytes followed
  113. // by 0xFFFF, or an initial run of ten zero-bytes.
  114. if (runpos[0] == 1 &&
  115. (maxpos == 5 || (maxpos == 4 && as_shorts[5] == 0xFFFF))) {
  116. *cursor++ = ':';
  117. *cursor++ = ':';
  118. if (maxpos == 4) {
  119. cursor += snprintf(cursor, INET6_ADDRSTRLEN - 2, "ffff:");
  120. }
  121. const struct in_addr* as_v4 =
  122. reinterpret_cast<const struct in_addr*>(&(as_shorts[6]));
  123. inet_ntop_v4(as_v4, cursor,
  124. static_cast<socklen_t>(INET6_ADDRSTRLEN - (cursor - dst)));
  125. } else {
  126. for (int i = 0; i < run_array_size; ++i) {
  127. if (runpos[i] == -1) {
  128. cursor += snprintf(cursor, INET6_ADDRSTRLEN - (cursor - dst), "%x",
  129. NetworkToHost16(as_shorts[i]));
  130. if (i != 7 && runpos[i + 1] != 1) {
  131. *cursor++ = ':';
  132. }
  133. } else if (runpos[i] == 1) {
  134. // Entered the run; print the colons and skip the run.
  135. *cursor++ = ':';
  136. *cursor++ = ':';
  137. i += (max - 1);
  138. }
  139. }
  140. }
  141. return dst;
  142. }
  143. // Helper function for inet_pton for IPv4 addresses.
  144. // |src| points to a character string containing an IPv4 network address in
  145. // dotted-decimal format, "ddd.ddd.ddd.ddd", where ddd is a decimal number
  146. // of up to three digits in the range 0 to 255.
  147. // The address is converted and copied to dst,
  148. // which must be sizeof(struct in_addr) (4) bytes (32 bits) long.
  149. int inet_pton_v4(const char* src, void* dst) {
  150. const int kIpv4AddressSize = 4;
  151. int found = 0;
  152. const char* src_pos = src;
  153. unsigned char result[kIpv4AddressSize] = {0};
  154. while (*src_pos != '\0') {
  155. // strtol won't treat whitespace characters in the begining as an error,
  156. // so check to ensure this is started with digit before passing to strtol.
  157. if (!isdigit(*src_pos)) {
  158. return 0;
  159. }
  160. char* end_pos;
  161. long value = strtol(src_pos, &end_pos, 10);
  162. if (value < 0 || value > 255 || src_pos == end_pos) {
  163. return 0;
  164. }
  165. ++found;
  166. if (found > kIpv4AddressSize) {
  167. return 0;
  168. }
  169. result[found - 1] = static_cast<unsigned char>(value);
  170. src_pos = end_pos;
  171. if (*src_pos == '.') {
  172. // There's more.
  173. ++src_pos;
  174. } else if (*src_pos != '\0') {
  175. // If it's neither '.' nor '\0' then return fail.
  176. return 0;
  177. }
  178. }
  179. if (found != kIpv4AddressSize) {
  180. return 0;
  181. }
  182. memcpy(dst, result, sizeof(result));
  183. return 1;
  184. }
  185. // Helper function for inet_pton for IPv6 addresses.
  186. int inet_pton_v6(const char* src, void* dst) {
  187. // sscanf will pick any other invalid chars up, but it parses 0xnnnn as hex.
  188. // Check for literal x in the input string.
  189. const char* readcursor = src;
  190. char c = *readcursor++;
  191. while (c) {
  192. if (c == 'x') {
  193. return 0;
  194. }
  195. c = *readcursor++;
  196. }
  197. readcursor = src;
  198. struct in6_addr an_addr;
  199. memset(&an_addr, 0, sizeof(an_addr));
  200. uint16_t* addr_cursor = reinterpret_cast<uint16_t*>(&an_addr.s6_addr[0]);
  201. uint16_t* addr_end = reinterpret_cast<uint16_t*>(&an_addr.s6_addr[16]);
  202. bool seencompressed = false;
  203. // Addresses that start with "::" (i.e., a run of initial zeros) or
  204. // "::ffff:" can potentially be IPv4 mapped or compatibility addresses.
  205. // These have dotted-style IPv4 addresses on the end (e.g. "::192.168.7.1").
  206. if (*readcursor == ':' && *(readcursor + 1) == ':' &&
  207. *(readcursor + 2) != 0) {
  208. // Check for periods, which we'll take as a sign of v4 addresses.
  209. const char* addrstart = readcursor + 2;
  210. if (strchr(addrstart, '.')) {
  211. const char* colon = strchr(addrstart, ':');
  212. if (colon) {
  213. uint16_t a_short;
  214. int bytesread = 0;
  215. if (sscanf(addrstart, "%hx%n", &a_short, &bytesread) != 1 ||
  216. a_short != 0xFFFF || bytesread != 4) {
  217. // Colons + periods means has to be ::ffff:a.b.c.d. But it wasn't.
  218. return 0;
  219. } else {
  220. an_addr.s6_addr[10] = 0xFF;
  221. an_addr.s6_addr[11] = 0xFF;
  222. addrstart = colon + 1;
  223. }
  224. }
  225. struct in_addr v4;
  226. if (inet_pton_v4(addrstart, &v4.s_addr)) {
  227. memcpy(&an_addr.s6_addr[12], &v4, sizeof(v4));
  228. memcpy(dst, &an_addr, sizeof(an_addr));
  229. return 1;
  230. } else {
  231. // Invalid v4 address.
  232. return 0;
  233. }
  234. }
  235. }
  236. // For addresses without a trailing IPv4 component ('normal' IPv6 addresses).
  237. while (*readcursor != 0 && addr_cursor < addr_end) {
  238. if (*readcursor == ':') {
  239. if (*(readcursor + 1) == ':') {
  240. if (seencompressed) {
  241. // Can only have one compressed run of zeroes ("::") per address.
  242. return 0;
  243. }
  244. // Hit a compressed run. Count colons to figure out how much of the
  245. // address is skipped.
  246. readcursor += 2;
  247. const char* coloncounter = readcursor;
  248. int coloncount = 0;
  249. if (*coloncounter == 0) {
  250. // Special case - trailing ::.
  251. addr_cursor = addr_end;
  252. } else {
  253. while (*coloncounter) {
  254. if (*coloncounter == ':') {
  255. ++coloncount;
  256. }
  257. ++coloncounter;
  258. }
  259. // (coloncount + 1) is the number of shorts left in the address.
  260. // If this number is greater than the number of available shorts, the
  261. // address is malformed.
  262. if (coloncount + 1 > addr_end - addr_cursor) {
  263. return 0;
  264. }
  265. addr_cursor = addr_end - (coloncount + 1);
  266. seencompressed = true;
  267. }
  268. } else {
  269. ++readcursor;
  270. }
  271. } else {
  272. uint16_t word;
  273. int bytesread = 0;
  274. if (sscanf(readcursor, "%4hx%n", &word, &bytesread) != 1) {
  275. return 0;
  276. } else {
  277. *addr_cursor = HostToNetwork16(word);
  278. ++addr_cursor;
  279. readcursor += bytesread;
  280. if (*readcursor != ':' && *readcursor != '\0') {
  281. return 0;
  282. }
  283. }
  284. }
  285. }
  286. if (*readcursor != '\0' || addr_cursor < addr_end) {
  287. // Catches addresses too short or too long.
  288. return 0;
  289. }
  290. memcpy(dst, &an_addr, sizeof(an_addr));
  291. return 1;
  292. }
  293. // Windows UWP applications cannot obtain versioning information from
  294. // the sandbox with intention (as behehaviour based on OS versioning rather
  295. // than feature discovery / compilation flags is discoraged and Windows
  296. // 10 is living continously updated version unlike previous versions
  297. // of Windows).
  298. #if !defined(WINUWP)
  299. bool GetOsVersion(int* major, int* minor, int* build) {
  300. OSVERSIONINFO info = {0};
  301. info.dwOSVersionInfoSize = sizeof(info);
  302. if (GetVersionEx(&info)) {
  303. if (major)
  304. *major = info.dwMajorVersion;
  305. if (minor)
  306. *minor = info.dwMinorVersion;
  307. if (build)
  308. *build = info.dwBuildNumber;
  309. return true;
  310. }
  311. return false;
  312. }
  313. #endif // !defined(WINUWP)
  314. } // namespace rtc