ip_unix.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <string.h>
  50. static IPAddress _sockaddr2ip(struct sockaddr *p_addr) {
  51. IPAddress ip;
  52. if (p_addr->sa_family == AF_INET) {
  53. struct sockaddr_in *addr = (struct sockaddr_in *)p_addr;
  54. ip.set_ipv4((uint8_t *)&(addr->sin_addr));
  55. } else if (p_addr->sa_family == AF_INET6) {
  56. struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)p_addr;
  57. ip.set_ipv6(addr6->sin6_addr.s6_addr);
  58. }
  59. return ip;
  60. }
  61. void IPUnix::_resolve_hostname(List<IPAddress> &r_addresses, const String &p_hostname, Type p_type) const {
  62. struct addrinfo hints;
  63. struct addrinfo *result = nullptr;
  64. memset(&hints, 0, sizeof(struct addrinfo));
  65. if (p_type == TYPE_IPV4) {
  66. hints.ai_family = AF_INET;
  67. } else if (p_type == TYPE_IPV6) {
  68. hints.ai_family = AF_INET6;
  69. hints.ai_flags = 0;
  70. } else {
  71. hints.ai_family = AF_UNSPEC;
  72. hints.ai_flags = AI_ADDRCONFIG;
  73. }
  74. hints.ai_flags &= ~AI_NUMERICHOST;
  75. int s = getaddrinfo(p_hostname.utf8().get_data(), nullptr, &hints, &result);
  76. if (s != 0) {
  77. print_verbose("getaddrinfo failed! Cannot resolve hostname.");
  78. return;
  79. }
  80. if (result == nullptr || result->ai_addr == nullptr) {
  81. print_verbose("Invalid response from getaddrinfo.");
  82. if (result) {
  83. freeaddrinfo(result);
  84. }
  85. return;
  86. }
  87. struct addrinfo *next = result;
  88. do {
  89. if (next->ai_addr == nullptr) {
  90. next = next->ai_next;
  91. continue;
  92. }
  93. IPAddress ip = _sockaddr2ip(next->ai_addr);
  94. if (ip.is_valid() && !r_addresses.find(ip)) {
  95. r_addresses.push_back(ip);
  96. }
  97. next = next->ai_next;
  98. } while (next);
  99. freeaddrinfo(result);
  100. }
  101. void IPUnix::get_local_interfaces(HashMap<String, Interface_Info> *r_interfaces) const {
  102. struct ifaddrs *ifAddrStruct = nullptr;
  103. struct ifaddrs *ifa = nullptr;
  104. int family;
  105. getifaddrs(&ifAddrStruct);
  106. for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next) {
  107. if (!ifa->ifa_addr) {
  108. continue;
  109. }
  110. family = ifa->ifa_addr->sa_family;
  111. if (family != AF_INET && family != AF_INET6) {
  112. continue;
  113. }
  114. HashMap<String, Interface_Info>::Iterator E = r_interfaces->find(ifa->ifa_name);
  115. if (!E) {
  116. Interface_Info info;
  117. info.name = ifa->ifa_name;
  118. info.name_friendly = ifa->ifa_name;
  119. info.index = String::num_uint64(if_nametoindex(ifa->ifa_name));
  120. E = r_interfaces->insert(ifa->ifa_name, info);
  121. ERR_CONTINUE(!E);
  122. }
  123. Interface_Info &info = E->value;
  124. info.ip_addresses.push_front(_sockaddr2ip(ifa->ifa_addr));
  125. }
  126. if (ifAddrStruct != nullptr) {
  127. freeifaddrs(ifAddrStruct);
  128. }
  129. }
  130. void IPUnix::make_default() {
  131. _create = _create_unix;
  132. }
  133. IP *IPUnix::_create_unix() {
  134. return memnew(IPUnix);
  135. }
  136. IPUnix::IPUnix() {
  137. }
  138. #endif // UNIX_ENABLED