2
0

ip_unix.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. void IPUnix::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
  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;
  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;
  109. };
  110. struct addrinfo *next = result;
  111. do {
  112. if (next->ai_addr == NULL) {
  113. next = next->ai_next;
  114. continue;
  115. }
  116. IPAddress ip = _sockaddr2ip(next->ai_addr);
  117. if (!r_addresses.find(ip)) {
  118. r_addresses.push_back(ip);
  119. }
  120. next = next->ai_next;
  121. } while (next);
  122. freeaddrinfo(result);
  123. }
  124. #if defined(WINDOWS_ENABLED)
  125. #if defined(UWP_ENABLED)
  126. void IPUnix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  127. using namespace Windows::Networking;
  128. using namespace Windows::Networking::Connectivity;
  129. // Returns addresses, not interfaces.
  130. auto hostnames = NetworkInformation::GetHostNames();
  131. for (int i = 0; i < hostnames->Size; i++) {
  132. auto hostname = hostnames->GetAt(i);
  133. if (hostname->Type != HostNameType::Ipv4 && hostname->Type != HostNameType::Ipv6)
  134. continue;
  135. String name = hostname->RawName->Data();
  136. Map<String, Interface_Info>::Element *E = r_interfaces->find(name);
  137. if (!E) {
  138. Interface_Info info;
  139. info.name = name;
  140. info.name_friendly = hostname->DisplayName->Data();
  141. info.index = String::num_uint64(0);
  142. E = r_interfaces->insert(name, info);
  143. ERR_CONTINUE(!E);
  144. }
  145. Interface_Info &info = E->get();
  146. IPAddress ip = IPAddress(hostname->CanonicalName->Data());
  147. info.ip_addresses.push_front(ip);
  148. }
  149. }
  150. #else
  151. void IPUnix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  152. ULONG buf_size = 1024;
  153. IP_ADAPTER_ADDRESSES *addrs;
  154. while (true) {
  155. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  156. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  157. nullptr, addrs, &buf_size);
  158. if (err == NO_ERROR) {
  159. break;
  160. };
  161. memfree(addrs);
  162. if (err == ERROR_BUFFER_OVERFLOW) {
  163. continue; // will go back and alloc the right size
  164. };
  165. ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
  166. };
  167. IP_ADAPTER_ADDRESSES *adapter = addrs;
  168. while (adapter != nullptr) {
  169. Interface_Info info;
  170. info.name = adapter->AdapterName;
  171. info.name_friendly = adapter->FriendlyName;
  172. info.index = String::num_uint64(adapter->IfIndex);
  173. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  174. while (address != nullptr) {
  175. int family = address->Address.lpSockaddr->sa_family;
  176. if (family != AF_INET && family != AF_INET6)
  177. continue;
  178. info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
  179. address = address->Next;
  180. }
  181. adapter = adapter->Next;
  182. // Only add interface if it has at least one IP
  183. if (info.ip_addresses.size() > 0)
  184. r_interfaces->insert(info.name, info);
  185. };
  186. memfree(addrs);
  187. };
  188. #endif
  189. #else // UNIX
  190. void IPUnix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  191. struct ifaddrs *ifAddrStruct = nullptr;
  192. struct ifaddrs *ifa = nullptr;
  193. int family;
  194. getifaddrs(&ifAddrStruct);
  195. for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
  196. if (!ifa->ifa_addr) {
  197. continue;
  198. }
  199. family = ifa->ifa_addr->sa_family;
  200. if (family != AF_INET && family != AF_INET6) {
  201. continue;
  202. }
  203. Map<String, Interface_Info>::Element *E = r_interfaces->find(ifa->ifa_name);
  204. if (!E) {
  205. Interface_Info info;
  206. info.name = ifa->ifa_name;
  207. info.name_friendly = ifa->ifa_name;
  208. info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
  209. E = r_interfaces->insert(ifa->ifa_name, info);
  210. ERR_CONTINUE(!E);
  211. }
  212. Interface_Info &info = E->get();
  213. info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
  214. }
  215. if (ifAddrStruct != nullptr) {
  216. freeifaddrs(ifAddrStruct);
  217. }
  218. }
  219. #endif
  220. void IPUnix::make_default() {
  221. _create = _create_unix;
  222. }
  223. IP *IPUnix::_create_unix() {
  224. return memnew(IPUnix);
  225. }
  226. IPUnix::IPUnix() {
  227. }
  228. #endif