You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
6.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2008 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/net_helpers.h"
  11. #include <memory>
  12. #if defined(WEBRTC_WIN)
  13. #include <ws2spi.h>
  14. #include <ws2tcpip.h>
  15. #include "rtc_base/win32.h"
  16. #endif
  17. #if defined(WEBRTC_POSIX) && !defined(__native_client__)
  18. #if defined(WEBRTC_ANDROID)
  19. #include "rtc_base/ifaddrs_android.h"
  20. #else
  21. #include <ifaddrs.h>
  22. #endif
  23. #endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
  24. #include "rtc_base/logging.h"
  25. #include "rtc_base/signal_thread.h"
  26. #include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread...
  27. namespace rtc {
  28. int ResolveHostname(const std::string& hostname,
  29. int family,
  30. std::vector<IPAddress>* addresses) {
  31. #ifdef __native_client__
  32. RTC_NOTREACHED();
  33. RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
  34. return -1;
  35. #else // __native_client__
  36. if (!addresses) {
  37. return -1;
  38. }
  39. addresses->clear();
  40. struct addrinfo* result = nullptr;
  41. struct addrinfo hints = {0};
  42. hints.ai_family = family;
  43. // |family| here will almost always be AF_UNSPEC, because |family| comes from
  44. // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed
  45. // with a hostname. When a SocketAddress is constructed with a hostname, its
  46. // family is AF_UNSPEC. However, if someday in the future we construct
  47. // a SocketAddress with both a hostname and a family other than AF_UNSPEC,
  48. // then it would be possible to get a specific family value here.
  49. // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as
  50. // documented by the various operating systems:
  51. // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
  52. // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/
  53. // ms738520(v=vs.85).aspx
  54. // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/
  55. // Reference/ManPages/man3/getaddrinfo.3.html
  56. // Android (source code, not documentation):
  57. // https://android.googlesource.com/platform/bionic/+/
  58. // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657
  59. hints.ai_flags = AI_ADDRCONFIG;
  60. int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
  61. if (ret != 0) {
  62. return ret;
  63. }
  64. struct addrinfo* cursor = result;
  65. for (; cursor; cursor = cursor->ai_next) {
  66. if (family == AF_UNSPEC || cursor->ai_family == family) {
  67. IPAddress ip;
  68. if (IPFromAddrInfo(cursor, &ip)) {
  69. addresses->push_back(ip);
  70. }
  71. }
  72. }
  73. freeaddrinfo(result);
  74. return 0;
  75. #endif // !__native_client__
  76. }
  77. // AsyncResolver
  78. AsyncResolver::AsyncResolver() : SignalThread(), error_(-1) {}
  79. AsyncResolver::~AsyncResolver() = default;
  80. void AsyncResolver::Start(const SocketAddress& addr) {
  81. addr_ = addr;
  82. // SignalThred Start will kickoff the resolve process.
  83. SignalThread::Start();
  84. }
  85. bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
  86. if (error_ != 0 || addresses_.empty())
  87. return false;
  88. *addr = addr_;
  89. for (size_t i = 0; i < addresses_.size(); ++i) {
  90. if (family == addresses_[i].family()) {
  91. addr->SetResolvedIP(addresses_[i]);
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. int AsyncResolver::GetError() const {
  98. return error_;
  99. }
  100. void AsyncResolver::Destroy(bool wait) {
  101. SignalThread::Destroy(wait);
  102. }
  103. void AsyncResolver::DoWork() {
  104. error_ =
  105. ResolveHostname(addr_.hostname().c_str(), addr_.family(), &addresses_);
  106. }
  107. void AsyncResolver::OnWorkDone() {
  108. SignalDone(this);
  109. }
  110. const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) {
  111. #if defined(WEBRTC_WIN)
  112. return win32_inet_ntop(af, src, dst, size);
  113. #else
  114. return ::inet_ntop(af, src, dst, size);
  115. #endif
  116. }
  117. int inet_pton(int af, const char* src, void* dst) {
  118. #if defined(WEBRTC_WIN)
  119. return win32_inet_pton(af, src, dst);
  120. #else
  121. return ::inet_pton(af, src, dst);
  122. #endif
  123. }
  124. bool HasIPv4Enabled() {
  125. #if defined(WEBRTC_POSIX) && !defined(__native_client__)
  126. bool has_ipv4 = false;
  127. struct ifaddrs* ifa;
  128. if (getifaddrs(&ifa) < 0) {
  129. return false;
  130. }
  131. for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
  132. if (cur->ifa_addr->sa_family == AF_INET) {
  133. has_ipv4 = true;
  134. break;
  135. }
  136. }
  137. freeifaddrs(ifa);
  138. return has_ipv4;
  139. #else
  140. return true;
  141. #endif
  142. }
  143. bool HasIPv6Enabled() {
  144. #if defined(WINUWP)
  145. // WinUWP always has IPv6 capability.
  146. return true;
  147. #elif defined(WEBRTC_WIN)
  148. if (IsWindowsVistaOrLater()) {
  149. return true;
  150. }
  151. if (!IsWindowsXpOrLater()) {
  152. return false;
  153. }
  154. DWORD protbuff_size = 4096;
  155. std::unique_ptr<char[]> protocols;
  156. LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
  157. int requested_protocols[2] = {AF_INET6, 0};
  158. int err = 0;
  159. int ret = 0;
  160. // Check for protocols in a do-while loop until we provide a buffer large
  161. // enough. (WSCEnumProtocols sets protbuff_size to its desired value).
  162. // It is extremely unlikely that this will loop more than once.
  163. do {
  164. protocols.reset(new char[protbuff_size]);
  165. protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get());
  166. ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size,
  167. &err);
  168. } while (ret == SOCKET_ERROR && err == WSAENOBUFS);
  169. if (ret == SOCKET_ERROR) {
  170. return false;
  171. }
  172. // Even if ret is positive, check specifically for IPv6.
  173. // Non-IPv6 enabled WinXP will still return a RAW protocol.
  174. for (int i = 0; i < ret; ++i) {
  175. if (protocol_infos[i].iAddressFamily == AF_INET6) {
  176. return true;
  177. }
  178. }
  179. return false;
  180. #elif defined(WEBRTC_POSIX) && !defined(__native_client__)
  181. bool has_ipv6 = false;
  182. struct ifaddrs* ifa;
  183. if (getifaddrs(&ifa) < 0) {
  184. return false;
  185. }
  186. for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) {
  187. if (cur->ifa_addr->sa_family == AF_INET6) {
  188. has_ipv6 = true;
  189. break;
  190. }
  191. }
  192. freeifaddrs(ifa);
  193. return has_ipv6;
  194. #else
  195. return true;
  196. #endif
  197. }
  198. } // namespace rtc