ip_unix.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**************************************************************************/
  2. /* ip_unix.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #if defined(UNIX_ENABLED) && !defined(UNIX_SOCKET_UNAVAILABLE)
  31. #include "ip_unix.h"
  32. #include <netdb.h>
  33. #ifdef ANDROID_ENABLED
  34. // We could drop this file once we up our API level to 24,
  35. // where the NDK's ifaddrs.h supports to needed getifaddrs.
  36. #include "thirdparty/misc/ifaddrs-android.h"
  37. #else
  38. #ifdef __FreeBSD__
  39. #include <sys/types.h>
  40. #endif
  41. #include <ifaddrs.h>
  42. #endif
  43. #include <arpa/inet.h>
  44. #include <sys/socket.h>
  45. #ifdef __FreeBSD__
  46. #include <netinet/in.h>
  47. #endif
  48. #include <net/if.h> // Order is important on OpenBSD, leave as last.
  49. static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
  50. IPAddress ip;
  51. if (p_addr->sa_family == AF_INET) {
  52. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  53. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  54. } else if (p_addr->sa_family == AF_INET6) {
  55. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  56. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  57. }
  58. return ip;
  59. }
  60. void IPUnix::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
  61. struct addrinfo hints;
  62. struct addrinfo *result = nullptr;
  63. memset(&hints, 0, sizeof(struct addrinfo));
  64. if (p_type == TYPE_IPV4) {
  65. hints.ai_family = AF_INET;
  66. } else if (p_type == TYPE_IPV6) {
  67. hints.ai_family = AF_INET6;
  68. hints.ai_flags = 0;
  69. } else {
  70. hints.ai_family = AF_UNSPEC;
  71. hints.ai_flags = AI_ADDRCONFIG;
  72. }
  73. hints.ai_flags &= ~AI_NUMERICHOST;
  74. int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
  75. if (s != 0) {
  76. print_verbose("getaddrinfo failed! Cannot resolve hostname.");
  77. return;
  78. }
  79. if (result == nullptr || result->ai_addr == nullptr) {
  80. print_verbose("Invalid response from getaddrinfo.");
  81. if (result) {
  82. freeaddrinfo(result);
  83. }
  84. return;
  85. }
  86. struct addrinfo *next = result;
  87. do {
  88. if (next->ai_addr == nullptr) {
  89. next = next->ai_next;
  90. continue;
  91. }
  92. IPAddress ip = _sockaddr2ip(next->ai_addr);
  93. if (ip.is_valid() && !r_addresses.find(ip)) {
  94. r_addresses.push_back(ip);
  95. }
  96. next = next->ai_next;
  97. } while (next);
  98. freeaddrinfo(result);
  99. }
  100. void IPUnix::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
  101. struct ifaddrs *ifAddrStruct = nullptr;
  102. struct ifaddrs *ifa = nullptr;
  103. int family;
  104. getifaddrs(&ifAddrStruct);
  105. for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
  106. if (!ifa->ifa_addr) {
  107. continue;
  108. }
  109. family = ifa->ifa_addr->sa_family;
  110. if (family != AF_INET && family != AF_INET6) {
  111. continue;
  112. }
  113. HashMap<String, Interface_Info>::Iterator E = r_interfaces->find(ifa->ifa_name);
  114. if (!E) {
  115. Interface_Info info;
  116. info.name = ifa->ifa_name;
  117. info.name_friendly = ifa->ifa_name;
  118. info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
  119. E = r_interfaces->insert(ifa->ifa_name, info);
  120. ERR_CONTINUE(!E);
  121. }
  122. Interface_Info &info = E->value;
  123. info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
  124. }
  125. if (ifAddrStruct != nullptr) {
  126. freeifaddrs(ifAddrStruct);
  127. }
  128. }
  129. void IPUnix::make_default() {
  130. _create = _create_unix;
  131. }
  132. IP *IPUnix::_create_unix() {
  133. return memnew(IPUnix);
  134. }
  135. IPUnix::IPUnix() {
  136. }
  137. #endif // UNIX_ENABLED