ip_unix.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**************************************************************************/
  2. /* ip_unix.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. #include "ip_unix.h"
  31. #if defined(UNIX_ENABLED) || defined(WINDOWS_ENABLED)
  32. #ifdef WINDOWS_ENABLED
  33. #define WIN32_LEAN_AND_MEAN
  34. #include <windows.h>
  35. #include <winsock2.h>
  36. #include <ws2tcpip.h>
  37. #include <iphlpapi.h>
  38. #include <stdio.h>
  39. #else // UNIX
  40. #include <netdb.h>
  41. #ifdef ANDROID_ENABLED
  42. // We could drop this file once we up our API level to 24,
  43. // where the NDK's ifaddrs.h supports to needed getifaddrs.
  44. #include "thirdparty/misc/ifaddrs-android.h"
  45. #else
  46. #ifdef __FreeBSD__
  47. #include <sys/types.h>
  48. #endif
  49. #include <ifaddrs.h>
  50. #endif
  51. #include <arpa/inet.h>
  52. #include <sys/socket.h>
  53. #ifdef __FreeBSD__
  54. #include <netinet/in.h>
  55. #endif
  56. #include <net/if.h> // Order is important on OpenBSD, leave as last.
  57. #endif // UNIX
  58. #include <string.h>
  59. static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
  60. IPAddress ip;
  61. if (p_addr->sa_family == AF_INET) {
  62. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  63. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  64. } else if (p_addr->sa_family == AF_INET6) {
  65. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  66. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  67. }
  68. return ip;
  69. }
  70. void IPUnix::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
  71. struct addrinfo hints;
  72. struct addrinfo *result = nullptr;
  73. memset(&hints, 0, sizeof(struct addrinfo));
  74. if (p_type == TYPE_IPV4) {
  75. hints.ai_family = AF_INET;
  76. } else if (p_type == TYPE_IPV6) {
  77. hints.ai_family = AF_INET6;
  78. hints.ai_flags = 0;
  79. } else {
  80. hints.ai_family = AF_UNSPEC;
  81. hints.ai_flags = AI_ADDRCONFIG;
  82. }
  83. hints.ai_flags &= ~AI_NUMERICHOST;
  84. int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
  85. if (s != 0) {
  86. print_verbose("getaddrinfo failed! Cannot resolve hostname.");
  87. return;
  88. }
  89. if (result == nullptr || result->ai_addr == nullptr) {
  90. print_verbose("Invalid response from getaddrinfo");
  91. if (result) {
  92. freeaddrinfo(result);
  93. }
  94. return;
  95. }
  96. struct addrinfo *next = result;
  97. do {
  98. if (next->ai_addr == nullptr) {
  99. next = next->ai_next;
  100. continue;
  101. }
  102. IPAddress ip = _sockaddr2ip(next->ai_addr);
  103. if (ip.is_valid() && !r_addresses.find(ip)) {
  104. r_addresses.push_back(ip);
  105. }
  106. next = next->ai_next;
  107. } while (next);
  108. freeaddrinfo(result);
  109. }
  110. #if defined(WINDOWS_ENABLED)
  111. void IPUnix::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
  112. ULONG buf_size = 1024;
  113. IP_ADAPTER_ADDRESSES *addrs;
  114. while (true) {
  115. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  116. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  117. nullptr, addrs, &buf_size);
  118. if (err == NO_ERROR) {
  119. break;
  120. }
  121. memfree(addrs);
  122. if (err == ERROR_BUFFER_OVERFLOW) {
  123. continue; // will go back and alloc the right size
  124. }
  125. ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
  126. }
  127. IP_ADAPTER_ADDRESSES *adapter = addrs;
  128. while (adapter != nullptr) {
  129. Interface_Info info;
  130. info.name = adapter->AdapterName;
  131. info.name_friendly = adapter->FriendlyName;
  132. info.index = String::num_uint64(adapter->IfIndex);
  133. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  134. while (address != nullptr) {
  135. int family = address->Address.lpSockaddr->sa_family;
  136. if (family != AF_INET && family != AF_INET6) {
  137. continue;
  138. }
  139. info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
  140. address = address->Next;
  141. }
  142. adapter = adapter->Next;
  143. // Only add interface if it has at least one IP
  144. if (info.ip_addresses.size() > 0) {
  145. r_interfaces->insert(info.name, info);
  146. }
  147. }
  148. memfree(addrs);
  149. }
  150. #else // UNIX
  151. void IPUnix::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
  152. struct ifaddrs *ifAddrStruct = nullptr;
  153. struct ifaddrs *ifa = nullptr;
  154. int family;
  155. getifaddrs(&ifAddrStruct);
  156. for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
  157. if (!ifa->ifa_addr) {
  158. continue;
  159. }
  160. family = ifa->ifa_addr->sa_family;
  161. if (family != AF_INET && family != AF_INET6) {
  162. continue;
  163. }
  164. HashMap<String, Interface_Info>::Iterator E = r_interfaces->find(ifa->ifa_name);
  165. if (!E) {
  166. Interface_Info info;
  167. info.name = ifa->ifa_name;
  168. info.name_friendly = ifa->ifa_name;
  169. info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
  170. E = r_interfaces->insert(ifa->ifa_name, info);
  171. ERR_CONTINUE(!E);
  172. }
  173. Interface_Info &info = E->value;
  174. info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
  175. }
  176. if (ifAddrStruct != nullptr) {
  177. freeifaddrs(ifAddrStruct);
  178. }
  179. }
  180. #endif // UNIX
  181. void IPUnix::make_default() {
  182. _create = _create_unix;
  183. }
  184. IP *IPUnix::_create_unix() {
  185. return memnew(IPUnix);
  186. }
  187. IPUnix::IPUnix() {
  188. }
  189. #endif // UNIX_ENABLED || WINDOWS_ENABLED