ip_unix.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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-2014 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. #include <ifaddrs.h>
  51. #endif
  52. #include <arpa/inet.h>
  53. #include <sys/socket.h>
  54. #endif
  55. IP_Address IP_Unix::_resolve_hostname(const String& p_hostname) {
  56. struct hostent *he;
  57. if ((he=gethostbyname(p_hostname.utf8().get_data())) == NULL) { // get the host info
  58. ERR_PRINT("gethostbyname failed!");
  59. return IP_Address();
  60. }
  61. IP_Address ip;
  62. ip.host= *((unsigned long*)he->h_addr);
  63. return ip;
  64. }
  65. #if defined(WINDOWS_ENABLED)
  66. #if defined(WINRT_ENABLED)
  67. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  68. };
  69. #else
  70. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  71. ULONG buf_size = 1024;
  72. IP_ADAPTER_ADDRESSES* addrs;
  73. while (true) {
  74. addrs = (IP_ADAPTER_ADDRESSES*)memalloc(buf_size);
  75. int err = GetAdaptersAddresses(AF_INET, GAA_FLAG_SKIP_ANYCAST |
  76. GAA_FLAG_SKIP_MULTICAST |
  77. GAA_FLAG_SKIP_DNS_SERVER |
  78. GAA_FLAG_SKIP_FRIENDLY_NAME,
  79. NULL, addrs, &buf_size);
  80. if (err == NO_ERROR) {
  81. break;
  82. };
  83. memfree(addrs);
  84. if (err == ERROR_BUFFER_OVERFLOW) {
  85. continue; // will go back and alloc the right size
  86. };
  87. ERR_EXPLAIN("Call to GetAdaptersAddresses failed with error " + itos(err));
  88. ERR_FAIL();
  89. return;
  90. };
  91. IP_ADAPTER_ADDRESSES* adapter = addrs;
  92. while (adapter != NULL) {
  93. IP_ADAPTER_UNICAST_ADDRESS* address = adapter->FirstUnicastAddress;
  94. while (address != NULL) {
  95. char addr_chr[INET_ADDRSTRLEN];
  96. SOCKADDR_IN* ipv4 = reinterpret_cast<SOCKADDR_IN*>(address->Address.lpSockaddr);
  97. IP_Address ip;
  98. ip.host= *((unsigned long*)&ipv4->sin_addr);
  99. //inet_ntop(AF_INET, &ipv4->sin_addr, addr_chr, INET_ADDRSTRLEN);
  100. r_addresses->push_back(ip);
  101. address = address->Next;
  102. };
  103. adapter = adapter->Next;
  104. };
  105. memfree(addrs);
  106. };
  107. #endif
  108. #else
  109. void IP_Unix::get_local_addresses(List<IP_Address> *r_addresses) const {
  110. struct ifaddrs * ifAddrStruct=NULL;
  111. struct ifaddrs * ifa=NULL;
  112. getifaddrs(&ifAddrStruct);
  113. for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
  114. if (!ifa->ifa_addr)
  115. continue;
  116. if (ifa ->ifa_addr->sa_family==AF_INET) { // check it is IP4
  117. // is a valid IP4 Address
  118. IP_Address ip;
  119. ip.host= *((unsigned long*)&((struct sockaddr_in *)ifa->ifa_addr)->sin_addr);
  120. r_addresses->push_back(ip);
  121. }/* else if (ifa->ifa_addr->sa_family==AF_INET6) { // check it is IP6
  122. // is a valid IP6 Address
  123. tmpAddrPtr=&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
  124. char addressBuffer[INET6_ADDRSTRLEN];
  125. inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
  126. printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
  127. } */
  128. }
  129. if (ifAddrStruct!=NULL) freeifaddrs(ifAddrStruct);
  130. }
  131. #endif
  132. void IP_Unix::make_default() {
  133. _create=_create_unix;
  134. }
  135. IP* IP_Unix::_create_unix() {
  136. return memnew( IP_Unix );
  137. }
  138. IP_Unix::IP_Unix() {
  139. }
  140. #endif