ip_unix.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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-2015 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) && !defined(WINRT_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. };
  75. #else
  76. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  77. ULONG buf_size = 1024;
  78. IP_ADAPTER_ADDRESSES* addrs;
  79. while (true) {
  80. addrs = (IP_ADAPTER_ADDRESSES*)memalloc(buf_size);
  81. int err = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
  82. GAA_FLAG_SKIP_MULTICAST |
  83. GAA_FLAG_SKIP_DNS_SERVER |
  84. GAA_FLAG_SKIP_FRIENDLY_NAME,
  85. NULL, addrs, &buf_size);
  86. if (err == NO_ERROR) {
  87. break;
  88. };
  89. memfree(addrs);
  90. if (err == ERROR_BUFFER_OVERFLOW) {
  91. continue; // will go back and alloc the right size
  92. };
  93. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  94. ERR_FAIL();
  95. return;
  96. };
  97. IP_ADAPTER_ADDRESSES* adapter = addrs;
  98. while (adapter != NULL) {
  99. IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress;
  100. while (address != NULL) {
  101. char addr_chr[INET_ADDRSTRLEN];
  102. SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
  103. IP_Address ip;
  104. ip.host= *((unsigned long*)&ipv4->sin_addr);
  105. //inet_ntop(AF_INET, &ipv4->sin_addr, addr_chr, INET_ADDRSTRLEN);
  106. r_addresses->push_back(ip);
  107. address = address->Next;
  108. };
  109. adapter = adapter->Next;
  110. };
  111. memfree(addrs);
  112. };
  113. #endif
  114. #else
  115. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  116. struct ifaddrs * ifAddrStruct=NULL;
  117. struct ifaddrs * ifa=NULL;
  118. getifaddrs(&ifAddrStruct);
  119. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  120. if (!ifa->ifa_addr)
  121. continue;
  122. if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
  123. // is a valid IP4 Address
  124. IP_Address ip;
  125. ip.host= *((unsigned long*)&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr);
  126. r_addresses->push_back(ip);
  127. }/* else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
  128. // is a valid IP6 Address
  129. tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
  130. char addressBuffer[INET6_ADDRSTRLEN];
  131. inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
  132. printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
  133. } */
  134. }
  135. if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
  136. }
  137. #endif
  138. void IP_Unix::make_default() {
  139. _create=_create_unix;
  140. }
  141. IP* IP_Unix::_create_unix() {
  142. return memnew( IP_Unix );
  143. }
  144. IP_Unix::IP_Unix() {
  145. }
  146. #endif