ip_unix.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*************************************************************************/
  2. /* ip_unix.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "ip_unix.h"
  30. #if defined(UNIX_ENABLED) || defined(WINDOWS_ENABLED)
  31. #include <string.h>
  32. #ifdef WINDOWS_ENABLED
  33. #include <ws2tcpip.h>
  34. #include <winsock2.h>
  35. #include <windows.h>
  36. #include <stdio.h>
  37. #ifndef UWP_ENABLED
  38. #if defined(__MINGW32__ ) && (!defined(__MINGW64_VERSION_MAJOR) || __MINGW64_VERSION_MAJOR < 4)
  39. // MinGW-w64 on Ubuntu 12.04 (our Travis build env) has bugs in this code where
  40. // some includes are missing in dependencies of iphlpapi.h for WINVER >= 0x0600 (Vista).
  41. // We don't use this Vista code for now, so working it around by disabling it.
  42. // MinGW-w64 >= 4.0 seems to be better judging by its headers.
  43. #undef _WIN32_WINNT
  44. #define _WIN32_WINNT 0x0501 // Windows XP, disable Vista API
  45. #include <iphlpapi.h>
  46. #undef _WIN32_WINNT
  47. #define _WIN32_WINNT 0x0600 // Reenable Vista API
  48. #else
  49. #include <iphlpapi.h>
  50. #endif // MINGW hack
  51. #endif
  52. #else
  53. #include <netdb.h>
  54. #ifdef ANDROID_ENABLED
  55. #include "platform/android/ifaddrs_android.h"
  56. #else
  57. #ifdef __FreeBSD__
  58. #include <sys/types.h>
  59. #endif
  60. #include <ifaddrs.h>
  61. #endif
  62. #include <arpa/inet.h>
  63. #include <sys/socket.h>
  64. #ifdef __FreeBSD__
  65. #include <netinet/in.h>
  66. #endif
  67. #endif
  68. static IP_Address _sockaddr2ip(struct sockaddr* p_addr) {
  69. IP_Address ip;
  70. if (p_addr->sa_family == AF_INET) {
  71. struct sockaddr_in* addr = (struct sockaddr_in*)p_addr;
  72. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  73. } else {
  74. struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
  75. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  76. };
  77. return ip;
  78. };
  79. IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, Type p_type) {
  80. struct addrinfo hints;
  81. struct addrinfo* result;
  82. memset(&hints, 0, sizeof(struct addrinfo));
  83. if (p_type == TYPE_IPV4) {
  84. hints.ai_family = AF_INET;
  85. } else if (p_type == TYPE_IPV6) {
  86. hints.ai_family = AF_INET6;
  87. hints.ai_flags = 0;
  88. } else {
  89. hints.ai_family = AF_UNSPEC;
  90. hints.ai_flags = AI_ADDRCONFIG;
  91. };
  92. int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);
  93. if (s != 0) {
  94. ERR_PRINT("getaddrinfo failed!");
  95. return IP_Address();
  96. };
  97. if (result == NULL || result->ai_addr == NULL) {
  98. ERR_PRINT("Invalid response from getaddrinfo");
  99. return IP_Address();
  100. };
  101. IP_Address ip = _sockaddr2ip(result->ai_addr);
  102. freeaddrinfo(result);
  103. return ip;
  104. }
  105. #if defined(WINDOWS_ENABLED)
  106. #if defined(UWP_ENABLED)
  107. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  108. using namespace Windows::Networking;
  109. using namespace Windows::Networking::Connectivity;
  110. auto hostnames = NetworkInformation::GetHostNames();
  111. for (int i = 0; i < hostnames->Size; i++) {
  112. if (hostnames->GetAt(i)->Type == HostNameType::Ipv4 || hostnames->GetAt(i)->Type == HostNameType::Ipv6 && hostnames->GetAt(i)->IPInformation != nullptr) {
  113. r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data())));
  114. }
  115. }
  116. };
  117. #else
  118. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  119. ULONG buf_size = 1024;
  120. IP_ADAPTER_ADDRESSES* addrs;
  121. while (true) {
  122. addrs = (IP_ADAPTER_ADDRESSES*)memalloc(buf_size);
  123. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST |
  124. GAA_FLAG_SKIP_MULTICAST |
  125. GAA_FLAG_SKIP_DNS_SERVER |
  126. GAA_FLAG_SKIP_FRIENDLY_NAME,
  127. NULL, addrs, &buf_size);
  128. if (err == NO_ERROR) {
  129. break;
  130. };
  131. memfree(addrs);
  132. if (err == ERROR_BUFFER_OVERFLOW) {
  133. continue; // will go back and alloc the right size
  134. };
  135. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  136. ERR_FAIL();
  137. return;
  138. };
  139. IP_ADAPTER_ADDRESSES* adapter = addrs;
  140. while (adapter != NULL) {
  141. IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress;
  142. while (address != NULL) {
  143. IP_Address ip;
  144. if (address->Address.lpSockaddr->sa_family == AF_INET) {
  145. SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
  146. ip.set_ipv4((uint8_t *)&(ipv4->sin_addr));
  147. } else { // ipv6
  148. SOCKADDR_IN6* ipv6 = reinterpret_cast<SOCKADDR_IN6*>(address->Address.lpSockaddr);
  149. ip.set_ipv6(ipv6->sin6_addr.s6_addr);
  150. };
  151. r_addresses->push_back(ip);
  152. address = address->Next;
  153. };
  154. adapter = adapter->Next;
  155. };
  156. memfree(addrs);
  157. };
  158. #endif
  159. #else
  160. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  161. struct ifaddrs * ifAddrStruct=NULL;
  162. struct ifaddrs * ifa=NULL;
  163. getifaddrs(&ifAddrStruct);
  164. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  165. if (!ifa->ifa_addr)
  166. continue;
  167. IP_Address ip = _sockaddr2ip(ifa->ifa_addr);
  168. r_addresses->push_back(ip);
  169. }
  170. if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
  171. }
  172. #endif
  173. void IP_Unix::make_default() {
  174. _create=_create_unix;
  175. }
  176. IP* IP_Unix::_create_unix() {
  177. return memnew( IP_Unix );
  178. }
  179. IP_Unix::IP_Unix() {
  180. }
  181. #endif