ip_unix.cpp 6.5 KB

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