ip_unix.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*************************************************************************/
  2. /* ip_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 // Re-enable 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. #include <net/if.h> // Order is important on OpenBSD, leave as last
  72. #endif
  73. static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {
  74. IP_Address ip;
  75. if (p_addr->sa_family == AF_INET) {
  76. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  77. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  78. } else if (p_addr->sa_family == AF_INET6) {
  79. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  80. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  81. };
  82. return ip;
  83. };
  84. IP_Address IP_Unix::_resolve_hostname(const String &p_hostname, Type p_type) {
  85. struct addrinfo hints;
  86. struct addrinfo *result;
  87. memset(&hints, 0, sizeof(struct addrinfo));
  88. if (p_type == TYPE_IPV4) {
  89. hints.ai_family = AF_INET;
  90. } else if (p_type == TYPE_IPV6) {
  91. hints.ai_family = AF_INET6;
  92. hints.ai_flags = 0;
  93. } else {
  94. hints.ai_family = AF_UNSPEC;
  95. hints.ai_flags = AI_ADDRCONFIG;
  96. };
  97. hints.ai_flags &= ~AI_NUMERICHOST;
  98. int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);
  99. if (s != 0) {
  100. ERR_PRINT("getaddrinfo failed! Cannot resolve hostname.");
  101. return IP_Address();
  102. };
  103. if (result == NULL || result->ai_addr == NULL) {
  104. ERR_PRINT("Invalid response from getaddrinfo");
  105. if (result)
  106. freeaddrinfo(result);
  107. return IP_Address();
  108. };
  109. IP_Address ip = _sockaddr2ip(result->ai_addr);
  110. freeaddrinfo(result);
  111. return ip;
  112. }
  113. #if defined(WINDOWS_ENABLED)
  114. #if defined(UWP_ENABLED)
  115. void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  116. using namespace Windows::Networking;
  117. using namespace Windows::Networking::Connectivity;
  118. // Returns addresses, not interfaces.
  119. auto hostnames = NetworkInformation::GetHostNames();
  120. for (int i = 0; i < hostnames->Size; i++) {
  121. auto hostname = hostnames->GetAt(i);
  122. if (hostname->Type != HostNameType::Ipv4 && hostname->Type != HostNameType::Ipv6)
  123. continue;
  124. String name = hostname->RawName->Data();
  125. Map<String, Interface_Info>::Element *E = r_interfaces->find(name);
  126. if (!E) {
  127. Interface_Info info;
  128. info.name = name;
  129. info.name_friendly = hostname->DisplayName->Data();
  130. info.index = String::num_uint64(0);
  131. E = r_interfaces->insert(name, info);
  132. ERR_CONTINUE(!E);
  133. }
  134. Interface_Info &info = E->get();
  135. IP_Address ip = IP_Address(hostname->CanonicalName->Data());
  136. info.ip_addresses.push_front(ip);
  137. }
  138. }
  139. #else
  140. void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  141. ULONG buf_size = 1024;
  142. IP_ADAPTER_ADDRESSES *addrs;
  143. while (true) {
  144. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  145. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  146. NULL, addrs, &buf_size);
  147. if (err == NO_ERROR) {
  148. break;
  149. };
  150. memfree(addrs);
  151. if (err == ERROR_BUFFER_OVERFLOW) {
  152. continue; // will go back and alloc the right size
  153. };
  154. ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
  155. };
  156. IP_ADAPTER_ADDRESSES *adapter = addrs;
  157. while (adapter != NULL) {
  158. Interface_Info info;
  159. info.name = adapter->AdapterName;
  160. info.name_friendly = adapter->FriendlyName;
  161. info.index = String::num_uint64(adapter->IfIndex);
  162. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  163. while (address != NULL) {
  164. int family = address->Address.lpSockaddr->sa_family;
  165. if (family != AF_INET && family != AF_INET6)
  166. continue;
  167. info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
  168. address = address->Next;
  169. }
  170. adapter = adapter->Next;
  171. // Only add interface if it has at least one IP
  172. if (info.ip_addresses.size() > 0)
  173. r_interfaces->insert(info.name, info);
  174. };
  175. memfree(addrs);
  176. };
  177. #endif
  178. #else // UNIX
  179. void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  180. struct ifaddrs *ifAddrStruct = NULL;
  181. struct ifaddrs *ifa = NULL;
  182. int family;
  183. getifaddrs(&ifAddrStruct);
  184. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  185. if (!ifa->ifa_addr)
  186. continue;
  187. family = ifa->ifa_addr->sa_family;
  188. if (family != AF_INET && family != AF_INET6)
  189. continue;
  190. Map<String, Interface_Info>::Element *E = r_interfaces->find(ifa->ifa_name);
  191. if (!E) {
  192. Interface_Info info;
  193. info.name = ifa->ifa_name;
  194. info.name_friendly = ifa->ifa_name;
  195. info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
  196. E = r_interfaces->insert(ifa->ifa_name, info);
  197. ERR_CONTINUE(!E);
  198. }
  199. Interface_Info &info = E->get();
  200. info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
  201. }
  202. if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
  203. }
  204. #endif
  205. void IP_Unix::make_default() {
  206. _create = _create_unix;
  207. }
  208. IP *IP_Unix::_create_unix() {
  209. return memnew(IP_Unix);
  210. }
  211. IP_Unix::IP_Unix() {
  212. }
  213. #endif