ip_unix.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************/
  2. /* ip_unix.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. #define WIN32_LEAN_AND_MEAN
  36. #include <windows.h>
  37. #include <winsock2.h>
  38. #include <ws2tcpip.h>
  39. #ifndef UWP_ENABLED
  40. #include <iphlpapi.h>
  41. #endif
  42. #else // UNIX
  43. #include <netdb.h>
  44. #ifdef ANDROID_ENABLED
  45. // We could drop this file once we up our API level to 24,
  46. // where the NDK's ifaddrs.h supports to needed getifaddrs.
  47. #include "thirdparty/misc/ifaddrs-android.h"
  48. #else
  49. #ifdef __FreeBSD__
  50. #include <sys/types.h>
  51. #endif
  52. #include <ifaddrs.h>
  53. #endif
  54. #include <arpa/inet.h>
  55. #include <sys/socket.h>
  56. #ifdef __FreeBSD__
  57. #include <netinet/in.h>
  58. #endif
  59. #include <net/if.h> // Order is important on OpenBSD, leave as last
  60. #endif
  61. static IP_Address _sockaddr2ip(struct sockaddr *p_addr) {
  62. IP_Address ip;
  63. if (p_addr->sa_family == AF_INET) {
  64. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  65. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  66. } else if (p_addr->sa_family == AF_INET6) {
  67. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  68. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  69. };
  70. return ip;
  71. };
  72. void IP_Unix::_resolve_hostname(List<IP_Address> &r_addresses, const String &p_hostname, Type p_type) const {
  73. struct addrinfo hints;
  74. struct addrinfo *result;
  75. memset(&hints, 0, sizeof(struct addrinfo));
  76. if (p_type == TYPE_IPV4) {
  77. hints.ai_family = AF_INET;
  78. } else if (p_type == TYPE_IPV6) {
  79. hints.ai_family = AF_INET6;
  80. hints.ai_flags = 0;
  81. } else {
  82. hints.ai_family = AF_UNSPEC;
  83. hints.ai_flags = AI_ADDRCONFIG;
  84. };
  85. hints.ai_flags &= ~AI_NUMERICHOST;
  86. int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
  87. if (s != 0) {
  88. ERR_PRINT("getaddrinfo failed! Cannot resolve hostname.");
  89. return;
  90. };
  91. if (result == nullptr || result->ai_addr == nullptr) {
  92. ERR_PRINT("Invalid response from getaddrinfo");
  93. if (result) {
  94. freeaddrinfo(result);
  95. }
  96. return;
  97. };
  98. struct addrinfo *next = result;
  99. do {
  100. if (next->ai_addr == NULL) {
  101. next = next->ai_next;
  102. continue;
  103. }
  104. IP_Address ip = _sockaddr2ip(next->ai_addr);
  105. if (ip.is_valid() && !r_addresses.find(ip)) {
  106. r_addresses.push_back(ip);
  107. }
  108. next = next->ai_next;
  109. } while (next);
  110. freeaddrinfo(result);
  111. }
  112. #if defined(WINDOWS_ENABLED)
  113. #if defined(UWP_ENABLED)
  114. void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  115. using namespace Windows::Networking;
  116. using namespace Windows::Networking::Connectivity;
  117. // Returns addresses, not interfaces.
  118. auto hostnames = NetworkInformation::GetHostNames();
  119. for (int i = 0; i < hostnames->Size; i++) {
  120. auto hostname = hostnames->GetAt(i);
  121. if (hostname->Type != HostNameType::Ipv4 && hostname->Type != HostNameType::Ipv6)
  122. continue;
  123. String name = hostname->RawName->Data();
  124. Map<String, Interface_Info>::Element *E = r_interfaces->find(name);
  125. if (!E) {
  126. Interface_Info info;
  127. info.name = name;
  128. info.name_friendly = hostname->DisplayName->Data();
  129. info.index = String::num_uint64(0);
  130. E = r_interfaces->insert(name, info);
  131. ERR_CONTINUE(!E);
  132. }
  133. Interface_Info &info = E->get();
  134. IP_Address ip = IP_Address(hostname->CanonicalName->Data());
  135. info.ip_addresses.push_front(ip);
  136. }
  137. }
  138. #else
  139. void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  140. ULONG buf_size = 1024;
  141. IP_ADAPTER_ADDRESSES *addrs;
  142. while (true) {
  143. addrs = (IP_ADAPTER_ADDRESSES *)memalloc(buf_size);
  144. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
  145. NULL, addrs, &buf_size);
  146. if (err == NO_ERROR) {
  147. break;
  148. };
  149. memfree(addrs);
  150. if (err == ERROR_BUFFER_OVERFLOW) {
  151. continue; // will go back and alloc the right size
  152. };
  153. ERR_FAIL_MSG("Call to GetAdaptersAddresses failed with error " + itos(err) + ".");
  154. };
  155. IP_ADAPTER_ADDRESSES *adapter = addrs;
  156. while (adapter != NULL) {
  157. Interface_Info info;
  158. info.name = adapter->AdapterName;
  159. info.name_friendly = adapter->FriendlyName;
  160. info.index = String::num_uint64(adapter->IfIndex);
  161. IP_ADAPTER_UNICAST_ADDRESS *address = adapter->FirstUnicastAddress;
  162. while (address != NULL) {
  163. int family = address->Address.lpSockaddr->sa_family;
  164. if (family != AF_INET && family != AF_INET6)
  165. continue;
  166. info.ip_addresses.push_front(_sockaddr2ip(address->Address.lpSockaddr));
  167. address = address->Next;
  168. }
  169. adapter = adapter->Next;
  170. // Only add interface if it has at least one IP
  171. if (info.ip_addresses.size() > 0)
  172. r_interfaces->insert(info.name, info);
  173. };
  174. memfree(addrs);
  175. };
  176. #endif
  177. #else // UNIX
  178. void IP_Unix::get_local_interfaces(Map<String, Interface_Info> *r_interfaces) const {
  179. struct ifaddrs *ifAddrStruct = nullptr;
  180. struct ifaddrs *ifa = nullptr;
  181. int family;
  182. getifaddrs(&ifAddrStruct);
  183. for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
  184. if (!ifa->ifa_addr) {
  185. continue;
  186. }
  187. family = ifa->ifa_addr->sa_family;
  188. if (family != AF_INET && family != AF_INET6) {
  189. continue;
  190. }
  191. Map<String, Interface_Info>::Element *E = r_interfaces->find(ifa->ifa_name);
  192. if (!E) {
  193. Interface_Info info;
  194. info.name = ifa->ifa_name;
  195. info.name_friendly = ifa->ifa_name;
  196. info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
  197. E = r_interfaces->insert(ifa->ifa_name, info);
  198. ERR_CONTINUE(!E);
  199. }
  200. Interface_Info &info = E->get();
  201. info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
  202. }
  203. if (ifAddrStruct != nullptr) {
  204. freeifaddrs(ifAddrStruct);
  205. }
  206. }
  207. #endif
  208. void IP_Unix::make_default() {
  209. _create = _create_unix;
  210. }
  211. IP *IP_Unix::_create_unix() {
  212. return memnew(IP_Unix);
  213. }
  214. IP_Unix::IP_Unix() {
  215. }
  216. #endif