ip_unix.cpp 8.1 KB

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