ip_unix.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*************************************************************************/
  2. /* ip_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #include <string.h>
  33. #ifdef WINDOWS_ENABLED
  34. #include <stdio.h>
  35. #include <winsock2.h>
  36. // Needs to be included after winsocks2.h
  37. #include <windows.h>
  38. #include <ws2tcpip.h>
  39. #ifndef UWP_ENABLED
  40. #if defined(__MINGW32__) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4)
  41. // MinGW-w64 on Ubuntu 12.04 (our Travis build env) has bugs in this code where
  42. // some includes are missing in dependencies of iphlpapi.h for WINVER >= 0x0600 (Vista).
  43. // We don't use this Vista code for now, so working it around by disabling it.
  44. // MinGW-w64 >= 4.0 seems to be better judging by its headers.
  45. #undef _WIN32_WINNT
  46. #define _WIN32_WINNT 0x0501 // Windows XP, disable Vista API
  47. #include <iphlpapi.h>
  48. #undef _WIN32_WINNT
  49. #define _WIN32_WINNT 0x0600 // Reenable Vista API
  50. #else
  51. #include <iphlpapi.h>
  52. #endif // MINGW hack
  53. #endif
  54. #else // UNIX
  55. #include <netdb.h>
  56. #ifdef ANDROID_ENABLED
  57. // We could drop this file once we up our API level to 24,
  58. // where the NDK's ifaddrs.h supports to needed getifaddrs.
  59. #include "thirdparty/misc/ifaddrs-android.h"
  60. #else
  61. #ifdef __FreeBSD__
  62. #include <sys/types.h>
  63. #endif
  64. #include <ifaddrs.h>
  65. #endif
  66. #include <arpa/inet.h>
  67. #include <sys/socket.h>
  68. #ifdef __FreeBSD__
  69. #include <netinet/in.h>
  70. #endif
  71. #endif
  72. static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {
  73. IP_Address ip;
  74. if (p_addr->sa_family == AF_INET) {
  75. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  76. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  77. } else if (p_addr->sa_family == AF_INET6) {
  78. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  79. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  80. };
  81. return ip;
  82. };
  83. IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) {
  84. struct addrinfo hints;
  85. struct addrinfo *result;
  86. memset(&hints, 0, sizeof(struct addrinfo));
  87. if (p_type == TYPE_IPV4) {
  88. hints.ai_family = AF_INET;
  89. } else if (p_type == TYPE_IPV6) {
  90. hints.ai_family = AF_INET6;
  91. hints.ai_flags = 0;
  92. } else {
  93. hints.ai_family = AF_UNSPEC;
  94. hints.ai_flags = AI_ADDRCONFIG;
  95. };
  96. hints.ai_flags &= ~AI_NUMERICHOST;
  97. int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);
  98. if (s != 0) {
  99. ERR_PRINT("getaddrinfo failed! Cannot resolve hostname.");
  100. return IP_Address();
  101. };
  102. if (result == NULL || result->ai_addr == NULL) {
  103. ERR_PRINT("Invalid response from getaddrinfo");
  104. if (result)
  105. freeaddrinfo(result);
  106. return IP_Address();
  107. };
  108. IP_Address ip = _sockaddr2ip(result->ai_addr);
  109. freeaddrinfo(result);
  110. return ip;
  111. }
  112. #if defined(WINDOWS_ENABLED)
  113. #if defined(UWP_ENABLED)
  114. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  115. using namespace Windows::Networking;
  116. using namespace Windows::Networking::Connectivity;
  117. auto hostnames = NetworkInformation::GetHostNames();
  118. for (int i = 0; i < hostnames->Size; i++) {
  119. if (hostnames->GetAt(i)->Type == HostNameType::Ipv4 || hostnames->GetAt(i)->Type == HostNameType::Ipv6 && hostnames->GetAt(i)->IPInformation != nullptr) {
  120. r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data())));
  121. }
  122. }
  123. };
  124. #else
  125. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  126. ULONG buf_size = 1024;
  127. IP_ADAPTER_ADDRESSES *addrs;
  128. while (true) {
  129. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  130. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  131. NULL, addrs, &buf_size);
  132. if (err == NO_ERROR) {
  133. break;
  134. };
  135. memfree(addrs);
  136. if (err == ERROR_BUFFER_OVERFLOW) {
  137. continue; // will go back and alloc the right size
  138. };
  139. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  140. ERR_FAIL();
  141. return;
  142. };
  143. IP_ADAPTER_ADDRESSES *adapter = addrs;
  144. while (adapter != NULL) {
  145. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  146. while (address != NULL) {
  147. IP_Address ip;
  148. if (address->Address.lpSockaddr->sa_family == AF_INET) {
  149. SOCKADDR_IN *ipv4 = reinterpret_cast<SOCKADDR_IN *>(address->Address.lpSockaddr);
  150. ip.set_ipv4((uint8_t *)&(ipv4->sin_addr));
  151. r_addresses->push_back(ip);
  152. } else if (address->Address.lpSockaddr->sa_family == AF_INET6) { // ipv6
  153. SOCKADDR_IN6 *ipv6 = reinterpret_cast<SOCKADDR_IN6 *>(address->Address.lpSockaddr);
  154. ip.set_ipv6(ipv6->sin6_addr.s6_addr);
  155. r_addresses->push_back(ip);
  156. };
  157. address = address->Next;
  158. };
  159. adapter = adapter->Next;
  160. };
  161. memfree(addrs);
  162. };
  163. #endif
  164. #else // UNIX
  165. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  166. struct ifaddrs *ifAddrStruct = NULL;
  167. struct ifaddrs *ifa = NULL;
  168. int family;
  169. getifaddrs(&ifAddrStruct);
  170. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  171. if (!ifa->ifa_addr)
  172. continue;
  173. family = ifa->ifa_addr->sa_family;
  174. if (family != AF_INET && family != AF_INET6)
  175. continue;
  176. IP_Address ip = _sockaddr2ip(ifa->ifa_addr);
  177. r_addresses->push_back(ip);
  178. }
  179. if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
  180. }
  181. #endif
  182. void IP_Unix::make_default() {
  183. _create = _create_unix;
  184. }
  185. IP *IP_Unix::_create_unix() {
  186. return memnew(IP_Unix);
  187. }
  188. IP_Unix::IP_Unix() {
  189. }
  190. #endif