ip_unix.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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-2016 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. // Workaround mingw missing flags!
  34. #ifndef AI_ADDRCONFIG
  35. #define AI_ADDRCONFIG 0x00000400
  36. #endif
  37. #ifndef AI_V4MAPPED
  38. #define AI_V4MAPPED 0x00000800
  39. #endif
  40. #ifdef WINRT_ENABLED
  41. #include <ws2tcpip.h>
  42. #include <winsock2.h>
  43. #include <windows.h>
  44. #include <stdio.h>
  45. #else
  46. #define WINVER 0x0600
  47. #include <ws2tcpip.h>
  48. #include <winsock2.h>
  49. #include <windows.h>
  50. #include <stdio.h>
  51. #include <iphlpapi.h>
  52. #endif
  53. #else
  54. #include <netdb.h>
  55. #ifdef ANDROID_ENABLED
  56. #include "platform/android/ifaddrs_android.h"
  57. #else
  58. #ifdef __FreeBSD__
  59. #include <sys/types.h>
  60. #endif
  61. #include <ifaddrs.h>
  62. #endif
  63. #include <arpa/inet.h>
  64. #include <sys/socket.h>
  65. #ifdef __FreeBSD__
  66. #include <netinet/in.h>
  67. #endif
  68. #endif
  69. static IP_Address _sockaddr2ip(struct sockaddr* p_addr) {
  70. IP_Address ip;
  71. if (p_addr->sa_family == AF_INET) {
  72. struct sockaddr_in* addr = (struct sockaddr_in*)p_addr;
  73. ip.field32[0] = *((unsigned long*)&addr->sin_addr);
  74. ip.type = IP_Address::TYPE_IPV4;
  75. } else {
  76. struct sockaddr_in6* addr6 = (struct sockaddr_in6*)p_addr;
  77. for (int i=0; i<16; i++)
  78. ip.field8[i] = addr6->sin6_addr.s6_addr[i];
  79. ip.type = IP_Address::TYPE_IPV6;
  80. };
  81. return ip;
  82. };
  83. IP_Address IP_Unix::_resolve_hostname(const String& p_hostname, Type p_type) {
  84. struct addrinfo hints;
  85. struct addrinfo* result;
  86. memset(&hints, 0, sizeof(struct addrinfo));
  87. if (p_type == TYPE_IPV4) {
  88. hints.ai_family = AF_INET;
  89. } else if (p_type == TYPE_IPV6) {
  90. hints.ai_family = AF_INET6;
  91. hints.ai_flags = 0;
  92. } else {
  93. hints.ai_family = AF_UNSPEC;
  94. #ifdef ANDROID_ENABLED
  95. // AI_V4MAPPED is not supported by android getaadrinfo
  96. hints.ai_flags = AI_ADDRCONFIG;
  97. #else
  98. hints.ai_flags = (AI_V4MAPPED | AI_ADDRCONFIG);
  99. #endif
  100. };
  101. int s = getaddrinfo(p_hostname.utf8().get_data(), NULL, &hints, &result);
  102. if (s != 0) {
  103. ERR_PRINT("getaddrinfo failed!");
  104. return IP_Address();
  105. };
  106. if (result == NULL || result->ai_addr == NULL) {
  107. ERR_PRINT("Invalid response from getaddrinfo");
  108. return IP_Address();
  109. };
  110. IP_Address ip = _sockaddr2ip(result->ai_addr);
  111. freeaddrinfo(result);
  112. return ip;
  113. }
  114. #if defined(WINDOWS_ENABLED)
  115. #if defined(WINRT_ENABLED)
  116. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  117. using namespace Windows::Networking;
  118. using namespace Windows::Networking::Connectivity;
  119. auto hostnames = NetworkInformation::GetHostNames();
  120. for (int i = 0; i < hostnames->Size; i++) {
  121. if (hostnames->GetAt(i)->Type == HostNameType::Ipv4 || hostnames->GetAt(i)->Type == HostNameType::Ipv6 && hostnames->GetAt(i)->IPInformation != nullptr) {
  122. r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data())));
  123. }
  124. }
  125. };
  126. #else
  127. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  128. ULONG buf_size = 1024;
  129. IP_ADAPTER_ADDRESSES* addrs;
  130. while (true) {
  131. addrs = (IP_ADAPTER_ADDRESSES*)memalloc(buf_size);
  132. int err = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_SKIP_ANYCAST |
  133. GAA_FLAG_SKIP_MULTICAST |
  134. GAA_FLAG_SKIP_DNS_SERVER |
  135. GAA_FLAG_SKIP_FRIENDLY_NAME,
  136. NULL, addrs, &buf_size);
  137. if (err == NO_ERROR) {
  138. break;
  139. };
  140. memfree(addrs);
  141. if (err == ERROR_BUFFER_OVERFLOW) {
  142. continue; // will go back and alloc the right size
  143. };
  144. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  145. ERR_FAIL();
  146. return;
  147. };
  148. IP_ADAPTER_ADDRESSES* adapter = addrs;
  149. while (adapter != NULL) {
  150. IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress;
  151. while (address != NULL) {
  152. IP_Address ip;
  153. if (address->Address.lpSockaddr->sa_family == AF_INET) {
  154. SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
  155. ip.field32[0] = *((unsigned long*)&ipv4->sin_addr);
  156. ip.type = IP_Address::TYPE_IPV4;
  157. } else { // ipv6
  158. SOCKADDR_IN6* ipv6 = reinterpret_cast<SOCKADDR_IN6*>(address->Address.lpSockaddr);
  159. for (int i=0; i<16; i++) {
  160. ip.field8[i] = ipv6->sin6_addr.s6_addr[i];
  161. };
  162. ip.type = IP_Address::TYPE_IPV6;
  163. };
  164. r_addresses->push_back(ip);
  165. address = address->Next;
  166. };
  167. adapter = adapter->Next;
  168. };
  169. memfree(addrs);
  170. };
  171. #endif
  172. #else
  173. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  174. struct ifaddrs * ifAddrStruct=NULL;
  175. struct ifaddrs * ifa=NULL;
  176. getifaddrs(&ifAddrStruct);
  177. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  178. if (!ifa->ifa_addr)
  179. continue;
  180. IP_Address ip = _sockaddr2ip(ifa->ifa_addr);
  181. r_addresses->push_back(ip);
  182. }
  183. if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
  184. }
  185. #endif
  186. void IP_Unix::make_default() {
  187. _create=_create_unix;
  188. }
  189. IP* IP_Unix::_create_unix() {
  190. return memnew( IP_Unix );
  191. }
  192. IP_Unix::IP_Unix() {
  193. }
  194. #endif