2
0

ip_unix.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. #ifdef WINDOWS_ENABLED
  32. #ifdef WINRT_ENABLED
  33. #include <ws2tcpip.h>
  34. #include <winsock2.h>
  35. #include <windows.h>
  36. #include <stdio.h>
  37. #else
  38. #define WINVER 0x0600
  39. #include <ws2tcpip.h>
  40. #include <winsock2.h>
  41. #include <windows.h>
  42. #include <stdio.h>
  43. #include <iphlpapi.h>
  44. #endif
  45. #else
  46. #include <netdb.h>
  47. #ifdef ANDROID_ENABLED
  48. #include "platform/android/ifaddrs_android.h"
  49. #else
  50. #ifdef __FreeBSD__
  51. #include <sys/types.h>
  52. #endif
  53. #include <ifaddrs.h>
  54. #endif
  55. #include <arpa/inet.h>
  56. #include <sys/socket.h>
  57. #ifdef __FreeBSD__
  58. #include <netinet/in.h>
  59. #endif
  60. #endif
  61. IP_Address IP_Unix::_resolve_hostname(const String& p_hostname) {
  62. struct hostent *he;
  63. if ((he=gethostbyname(p_hostname.utf8().get_data())) == NULL) { // get the host info
  64. ERR_PRINT("gethostbyname failed!");
  65. return IP_Address();
  66. }
  67. IP_Address ip;
  68. ip.host= *((unsigned long*)he->h_addr);
  69. return ip;
  70. }
  71. #if defined(WINDOWS_ENABLED)
  72. #if defined(WINRT_ENABLED)
  73. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  74. using namespace Windows::Networking;
  75. using namespace Windows::Networking::Connectivity;
  76. auto hostnames = NetworkInformation::GetHostNames();
  77. for (int i = 0; i < hostnames->Size; i++) {
  78. if (hostnames->GetAt(i)->Type == HostNameType::Ipv4 && hostnames->GetAt(i)->IPInformation != nullptr) {
  79. r_addresses->push_back(IP_Address(String(hostnames->GetAt(i)->CanonicalName->Data())));
  80. }
  81. }
  82. };
  83. #else
  84. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  85. ULONG buf_size = 1024;
  86. IP_ADAPTER_ADDRESSES* addrs;
  87. while (true) {
  88. addrs = (IP_ADAPTER_ADDRESSES*)memalloc(buf_size);
  89. int err = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
  90. GAA_FLAG_SKIP_MULTICAST |
  91. GAA_FLAG_SKIP_DNS_SERVER |
  92. GAA_FLAG_SKIP_FRIENDLY_NAME,
  93. NULL, addrs, &buf_size);
  94. if (err == NO_ERROR) {
  95. break;
  96. };
  97. memfree(addrs);
  98. if (err == ERROR_BUFFER_OVERFLOW) {
  99. continue; // will go back and alloc the right size
  100. };
  101. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  102. ERR_FAIL();
  103. return;
  104. };
  105. IP_ADAPTER_ADDRESSES* adapter = addrs;
  106. while (adapter != NULL) {
  107. IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress;
  108. while (address != NULL) {
  109. char addr_chr[INET_ADDRSTRLEN];
  110. SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
  111. IP_Address ip;
  112. ip.host= *((unsigned long*)&ipv4->sin_addr);
  113. //inet_ntop(AF_INET, &ipv4->sin_addr, addr_chr, INET_ADDRSTRLEN);
  114. r_addresses->push_back(ip);
  115. address = address->Next;
  116. };
  117. adapter = adapter->Next;
  118. };
  119. memfree(addrs);
  120. };
  121. #endif
  122. #else
  123. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  124. struct ifaddrs * ifAddrStruct=NULL;
  125. struct ifaddrs * ifa=NULL;
  126. getifaddrs(&ifAddrStruct);
  127. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  128. if (!ifa->ifa_addr)
  129. continue;
  130. if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
  131. // is a valid IP4 Address
  132. IP_Address ip;
  133. ip.host= *((unsigned long*)&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr);
  134. r_addresses->push_back(ip);
  135. }/* else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
  136. // is a valid IP6 Address
  137. tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
  138. char addressBuffer[INET6_ADDRSTRLEN];
  139. inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
  140. printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
  141. } */
  142. }
  143. if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
  144. }
  145. #endif
  146. void IP_Unix::make_default() {
  147. _create=_create_unix;
  148. }
  149. IP* IP_Unix::_create_unix() {
  150. return memnew( IP_Unix );
  151. }
  152. IP_Unix::IP_Unix() {
  153. }
  154. #endif