ip_windows.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**************************************************************************/
  2. /* ip_windows.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #if defined(WINDOWS_ENABLED)
  31. #include "ip_windows.h"
  32. #define WIN32_LEAN_AND_MEAN
  33. #include <windows.h>
  34. #include <winsock2.h>
  35. #include <ws2tcpip.h>
  36. #include <iphlpapi.h>
  37. #include <cstdio>
  38. static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
  39. IPAddress ip;
  40. if (p_addr->sa_family == AF_INET) {
  41. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  42. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  43. } else if (p_addr->sa_family == AF_INET6) {
  44. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  45. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  46. }
  47. return ip;
  48. }
  49. void IPWindows::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
  50. struct addrinfo hints;
  51. struct addrinfo *result = nullptr;
  52. memset(&hints, 0, sizeof(struct addrinfo));
  53. if (p_type == TYPE_IPV4) {
  54. hints.ai_family = AF_INET;
  55. } else if (p_type == TYPE_IPV6) {
  56. hints.ai_family = AF_INET6;
  57. hints.ai_flags = 0;
  58. } else {
  59. hints.ai_family = AF_UNSPEC;
  60. hints.ai_flags = AI_ADDRCONFIG;
  61. }
  62. hints.ai_flags &= ~AI_NUMERICHOST;
  63. int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
  64. if (s != 0) {
  65. print_verbose("getaddrinfo failed! Cannot resolve hostname.");
  66. return;
  67. }
  68. if (result == nullptr || result->ai_addr == nullptr) {
  69. print_verbose("Invalid response from getaddrinfo.");
  70. if (result) {
  71. freeaddrinfo(result);
  72. }
  73. return;
  74. }
  75. struct addrinfo *next = result;
  76. do {
  77. if (next->ai_addr == nullptr) {
  78. next = next->ai_next;
  79. continue;
  80. }
  81. IPAddress ip = _sockaddr2ip(next->ai_addr);
  82. if (ip.is_valid() && !r_addresses.find(ip)) {
  83. r_addresses.push_back(ip);
  84. }
  85. next = next->ai_next;
  86. } while (next);
  87. freeaddrinfo(result);
  88. }
  89. void IPWindows::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
  90. ULONG buf_size = 1024;
  91. IP_ADAPTER_ADDRESSES *addrs;
  92. while (true) {
  93. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  94. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  95. nullptr, addrs, &buf_size);
  96. if (err == NO_ERROR) {
  97. break;
  98. }
  99. memfree(addrs);
  100. if (err == ERROR_BUFFER_OVERFLOW) {
  101. continue; // Will go back and alloc the right size.
  102. }
  103. ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
  104. }
  105. IP_ADAPTER_ADDRESSES *adapter = addrs;
  106. while (adapter != nullptr) {
  107. Interface_Info info;
  108. info.name = adapter->AdapterName;
  109. info.name_friendly = adapter->FriendlyName;
  110. info.index = String::num_uint64(adapter->IfIndex);
  111. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  112. while (address != nullptr) {
  113. int family = address->Address.lpSockaddr->sa_family;
  114. if (family != AF_INET && family != AF_INET6) {
  115. continue;
  116. }
  117. info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
  118. address = address->Next;
  119. }
  120. adapter = adapter->Next;
  121. // Only add interface if it has at least one IP.
  122. if (info.ip_addresses.size() > 0) {
  123. r_interfaces->insert(info.name, info);
  124. }
  125. }
  126. memfree(addrs);
  127. }
  128. void IPWindows::make_default() {
  129. _create = _create_unix;
  130. }
  131. IP *IPWindows::_create_unix() {
  132. return memnew(IPWindows);
  133. }
  134. IPWindows::IPWindows() {
  135. }
  136. #endif // WINDOWS_ENABLED