ip_windows.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <stdio.h>
  38. #include <string.h>
  39. static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
  40. IPAddress ip;
  41. if (p_addr->sa_family == AF_INET) {
  42. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  43. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  44. } else if (p_addr->sa_family == AF_INET6) {
  45. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  46. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  47. }
  48. return ip;
  49. }
  50. void IPWindows::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
  51. struct addrinfo hints;
  52. struct addrinfo *result = nullptr;
  53. memset(&hints, 0, sizeof(struct addrinfo));
  54. if (p_type == TYPE_IPV4) {
  55. hints.ai_family = AF_INET;
  56. } else if (p_type == TYPE_IPV6) {
  57. hints.ai_family = AF_INET6;
  58. hints.ai_flags = 0;
  59. } else {
  60. hints.ai_family = AF_UNSPEC;
  61. hints.ai_flags = AI_ADDRCONFIG;
  62. }
  63. hints.ai_flags &= ~AI_NUMERICHOST;
  64. int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
  65. if (s != 0) {
  66. print_verbose("getaddrinfo failed! Cannot resolve hostname.");
  67. return;
  68. }
  69. if (result == nullptr || result->ai_addr == nullptr) {
  70. print_verbose("Invalid response from getaddrinfo.");
  71. if (result) {
  72. freeaddrinfo(result);
  73. }
  74. return;
  75. }
  76. struct addrinfo *next = result;
  77. do {
  78. if (next->ai_addr == nullptr) {
  79. next = next->ai_next;
  80. continue;
  81. }
  82. IPAddress ip = _sockaddr2ip(next->ai_addr);
  83. if (ip.is_valid() && !r_addresses.find(ip)) {
  84. r_addresses.push_back(ip);
  85. }
  86. next = next->ai_next;
  87. } while (next);
  88. freeaddrinfo(result);
  89. }
  90. void IPWindows::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
  91. ULONG buf_size = 1024;
  92. IP_ADAPTER_ADDRESSES *addrs;
  93. while (true) {
  94. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  95. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  96. nullptr, addrs, &buf_size);
  97. if (err == NO_ERROR) {
  98. break;
  99. }
  100. memfree(addrs);
  101. if (err == ERROR_BUFFER_OVERFLOW) {
  102. continue; // Will go back and alloc the right size.
  103. }
  104. ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
  105. }
  106. IP_ADAPTER_ADDRESSES *adapter = addrs;
  107. while (adapter != nullptr) {
  108. Interface_Info info;
  109. info.name = adapter->AdapterName;
  110. info.name_friendly = adapter->FriendlyName;
  111. info.index = String::num_uint64(adapter->IfIndex);
  112. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  113. while (address != nullptr) {
  114. int family = address->Address.lpSockaddr->sa_family;
  115. if (family != AF_INET && family != AF_INET6) {
  116. continue;
  117. }
  118. info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
  119. address = address->Next;
  120. }
  121. adapter = adapter->Next;
  122. // Only add interface if it has at least one IP.
  123. if (info.ip_addresses.size() > 0) {
  124. r_interfaces->insert(info.name, info);
  125. }
  126. }
  127. memfree(addrs);
  128. }
  129. void IPWindows::make_default() {
  130. _create = _create_unix;
  131. }
  132. IP *IPWindows::_create_unix() {
  133. return memnew(IPWindows);
  134. }
  135. IPWindows::IPWindows() {
  136. }
  137. #endif // WINDOWS_ENABLED