ip_unix.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*************************************************************************/
  2. /* ip_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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
  55. #include <netdb.h>
  56. #ifdef ANDROID_ENABLED
  57. #include "platform/android/ifaddrs_android.h"
  58. #else
  59. #ifdef __FreeBSD__
  60. #include <sys/types.h>
  61. #endif
  62. #include <ifaddrs.h>
  63. #endif
  64. #include <arpa/inet.h>
  65. #include <sys/socket.h>
  66. #ifdef __FreeBSD__
  67. #include <netinet/in.h>
  68. #endif
  69. #endif
  70. static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {
  71. IP_Address ip;
  72. if (p_addr->sa_family == AF_INET) {
  73. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  74. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  75. } else if (p_addr->sa_family == AF_INET6) {
  76. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  77. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  78. };
  79. return ip;
  80. };
  81. IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) {
  82. struct addrinfo hints;
  83. struct addrinfo *result;
  84. memset(&hints, 0, sizeof(struct addrinfo));
  85. if (p_type == TYPE_IPV4) {
  86. hints.ai_family = AF_INET;
  87. } else if (p_type == TYPE_IPV6) {
  88. hints.ai_family = AF_INET6;
  89. hints.ai_flags = 0;
  90. } else {
  91. hints.ai_family = AF_UNSPEC;
  92. hints.ai_flags = AI_ADDRCONFIG;
  93. };
  94. hints.ai_flags &= !AI_NUMERICHOST;
  95. int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);
  96. if (s != 0) {
  97. ERR_PRINT("getaddrinfo failed!");
  98. return IP_Address();
  99. };
  100. if (result == NULL || result->ai_addr == NULL) {
  101. ERR_PRINT("Invalid response from getaddrinfo");
  102. return IP_Address();
  103. };
  104. IP_Address ip = _sockaddr2ip(result->ai_addr);
  105. freeaddrinfo(result);
  106. return ip;
  107. }
  108. #if defined(WINDOWS_ENABLED)
  109. #if defined(UWP_ENABLED)
  110. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  111. using namespace Windows::Networking;
  112. using namespace Windows::Networking::Connectivity;
  113. auto hostnames = NetworkInformation::GetHostNames();
  114. for (int i = 0; i < hostnames->Size; i++) {
  115. if (hostnames->GetAt(i)->Type == HostNameType::Ipv4 || hostnames->GetAt(i)->Type == HostNameType::Ipv6 && hostnames->GetAt(i)->IPInformation != nullptr) {
  116. r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data())));
  117. }
  118. }
  119. };
  120. #else
  121. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  122. ULONG buf_size = 1024;
  123. IP_ADAPTER_ADDRESSES *addrs;
  124. while (true) {
  125. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  126. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST |
  127. GAA_FLAG_SKIP_MULTICAST |
  128. GAA_FLAG_SKIP_DNS_SERVER |
  129. GAA_FLAG_SKIP_FRIENDLY_NAME,
  130. NULL, addrs, &buf_size);
  131. if (err == NO_ERROR) {
  132. break;
  133. };
  134. memfree(addrs);
  135. if (err == ERROR_BUFFER_OVERFLOW) {
  136. continue; // will go back and alloc the right size
  137. };
  138. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  139. ERR_FAIL();
  140. return;
  141. };
  142. IP_ADAPTER_ADDRESSES *adapter = addrs;
  143. while (adapter != NULL) {
  144. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  145. while (address != NULL) {
  146. IP_Address ip;
  147. if (address->Address.lpSockaddr->sa_family == AF_INET) {
  148. SOCKADDR_IN *ipv4 = reinterpret_cast<SOCKADDR_IN *>(address->Address.lpSockaddr);
  149. ip.set_ipv4((uint8_t *)&(ipv4->sin_addr));
  150. r_addresses->push_back(ip);
  151. } else if (address->Address.lpSockaddr->sa_family == AF_INET6) { // ipv6
  152. SOCKADDR_IN6 *ipv6 = reinterpret_cast<SOCKADDR_IN6 *>(address->Address.lpSockaddr);
  153. ip.set_ipv6(ipv6->sin6_addr.s6_addr);
  154. r_addresses->push_back(ip);
  155. };
  156. address = address->Next;
  157. };
  158. adapter = adapter->Next;
  159. };
  160. memfree(addrs);
  161. };
  162. #endif
  163. #else
  164. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  165. struct ifaddrs *ifAddrStruct = NULL;
  166. struct ifaddrs *ifa = NULL;
  167. int family;
  168. getifaddrs(&ifAddrStruct);
  169. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  170. if (!ifa->ifa_addr)
  171. continue;
  172. family = ifa->ifa_addr->sa_family;
  173. if (family != AF_INET && family != AF_INET6)
  174. continue;
  175. IP_Address ip = _sockaddr2ip(ifa->ifa_addr);
  176. r_addresses->push_back(ip);
  177. }
  178. if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
  179. }
  180. #endif
  181. void IP_Unix::make_default() {
  182. _create = _create_unix;
  183. }
  184. IP *IP_Unix::_create_unix() {
  185. return memnew(IP_Unix);
  186. }
  187. IP_Unix::IP_Unix() {
  188. }
  189. #endif