henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 11 | #include "rtc_base/net_helpers.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 12 | |
| 13 | #if defined(WEBRTC_WIN) |
| 14 | #include <ws2spi.h> |
| 15 | #include <ws2tcpip.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "rtc_base/win32.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 18 | #endif |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 19 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 20 | #if defined(WEBRTC_ANDROID) |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 21 | #include "rtc_base/ifaddrs_android.h" |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 22 | #else |
| 23 | #include <ifaddrs.h> |
| 24 | #endif |
| 25 | #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 26 | |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 27 | #include "api/task_queue/task_queue_base.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 29 | #include "rtc_base/signal_thread.h" |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 30 | #include "rtc_base/task_queue.h" |
| 31 | #include "rtc_base/task_utils/to_queued_task.h" |
Yves Gerey | 2e00abc | 2018-10-05 15:39:24 +0200 | [diff] [blame] | 32 | #include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread... |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 33 | |
| 34 | namespace rtc { |
| 35 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 36 | int ResolveHostname(const std::string& hostname, |
| 37 | int family, |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 38 | std::vector<IPAddress>* addresses) { |
| 39 | #ifdef __native_client__ |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 40 | RTC_NOTREACHED(); |
Mirko Bonadei | 675513b | 2017-11-09 11:09:25 +0100 | [diff] [blame] | 41 | RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 42 | return -1; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 43 | #else // __native_client__ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 44 | if (!addresses) { |
| 45 | return -1; |
| 46 | } |
| 47 | addresses->clear(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 48 | struct addrinfo* result = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 49 | struct addrinfo hints = {0}; |
Honghai Zhang | 56c0b20 | 2016-06-26 22:11:12 -0700 | [diff] [blame] | 50 | hints.ai_family = family; |
| 51 | // |family| here will almost always be AF_UNSPEC, because |family| comes from |
| 52 | // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed |
| 53 | // with a hostname. When a SocketAddress is constructed with a hostname, its |
| 54 | // family is AF_UNSPEC. However, if someday in the future we construct |
| 55 | // a SocketAddress with both a hostname and a family other than AF_UNSPEC, |
| 56 | // then it would be possible to get a specific family value here. |
| 57 | |
| 58 | // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as |
| 59 | // documented by the various operating systems: |
| 60 | // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html |
| 61 | // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ |
| 62 | // ms738520(v=vs.85).aspx |
| 63 | // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/ |
| 64 | // Reference/ManPages/man3/getaddrinfo.3.html |
| 65 | // Android (source code, not documentation): |
| 66 | // https://android.googlesource.com/platform/bionic/+/ |
| 67 | // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 68 | hints.ai_flags = AI_ADDRCONFIG; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 69 | int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | if (ret != 0) { |
| 71 | return ret; |
| 72 | } |
| 73 | struct addrinfo* cursor = result; |
| 74 | for (; cursor; cursor = cursor->ai_next) { |
| 75 | if (family == AF_UNSPEC || cursor->ai_family == family) { |
| 76 | IPAddress ip; |
| 77 | if (IPFromAddrInfo(cursor, &ip)) { |
| 78 | addresses->push_back(ip); |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | freeaddrinfo(result); |
| 83 | return 0; |
| 84 | #endif // !__native_client__ |
| 85 | } |
| 86 | |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 87 | AsyncResolver::AsyncResolver() : error_(-1) {} |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 89 | AsyncResolver::~AsyncResolver() { |
| 90 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 91 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 92 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 93 | void AsyncResolver::Start(const SocketAddress& addr) { |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 94 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 95 | RTC_DCHECK(!destroy_called_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 96 | addr_ = addr; |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 97 | webrtc::TaskQueueBase* current_task_queue = webrtc::TaskQueueBase::Current(); |
| 98 | popup_thread_ = Thread::Create(); |
| 99 | popup_thread_->Start(); |
| 100 | popup_thread_->PostTask(webrtc::ToQueuedTask( |
| 101 | [this, flag = safety_.flag(), addr, current_task_queue] { |
| 102 | std::vector<IPAddress> addresses; |
| 103 | int error = |
| 104 | ResolveHostname(addr.hostname().c_str(), addr.family(), &addresses); |
| 105 | current_task_queue->PostTask(webrtc::ToQueuedTask( |
| 106 | std::move(flag), [this, error, addresses = std::move(addresses)] { |
| 107 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 108 | ResolveDone(std::move(addresses), error); |
| 109 | })); |
| 110 | })); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 114 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 115 | RTC_DCHECK(!destroy_called_); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 116 | if (error_ != 0 || addresses_.empty()) |
| 117 | return false; |
| 118 | |
| 119 | *addr = addr_; |
| 120 | for (size_t i = 0; i < addresses_.size(); ++i) { |
| 121 | if (family == addresses_[i].family()) { |
| 122 | addr->SetResolvedIP(addresses_[i]); |
| 123 | return true; |
| 124 | } |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 129 | int AsyncResolver::GetError() const { |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 130 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 131 | RTC_DCHECK(!destroy_called_); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 132 | return error_; |
| 133 | } |
| 134 | |
| 135 | void AsyncResolver::Destroy(bool wait) { |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 136 | // Some callers have trouble guaranteeing that Destroy is called on the |
| 137 | // sequence guarded by |sequence_checker_|. |
| 138 | // RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 139 | RTC_DCHECK(!destroy_called_); |
| 140 | destroy_called_ = true; |
| 141 | MaybeSelfDestruct(); |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 144 | const std::vector<IPAddress>& AsyncResolver::addresses() const { |
| 145 | RTC_DCHECK_RUN_ON(&sequence_checker_); |
| 146 | RTC_DCHECK(!destroy_called_); |
| 147 | return addresses_; |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 148 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 149 | |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 150 | void AsyncResolver::ResolveDone(std::vector<IPAddress> addresses, int error) { |
| 151 | addresses_ = addresses; |
| 152 | error_ = error; |
| 153 | recursion_check_ = true; |
deadbeef | 8290ddf | 2017-07-11 16:56:05 -0700 | [diff] [blame] | 154 | SignalDone(this); |
Markus Handell | fbf4ad2 | 2020-05-26 18:43:55 +0200 | [diff] [blame] | 155 | MaybeSelfDestruct(); |
| 156 | } |
| 157 | |
| 158 | void AsyncResolver::MaybeSelfDestruct() { |
| 159 | if (!recursion_check_) { |
| 160 | delete this; |
| 161 | } else { |
| 162 | recursion_check_ = false; |
| 163 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 166 | const char* inet_ntop(int af, const void* src, char* dst, socklen_t size) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 167 | #if defined(WEBRTC_WIN) |
| 168 | return win32_inet_ntop(af, src, dst, size); |
| 169 | #else |
| 170 | return ::inet_ntop(af, src, dst, size); |
| 171 | #endif |
| 172 | } |
| 173 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 174 | int inet_pton(int af, const char* src, void* dst) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 175 | #if defined(WEBRTC_WIN) |
| 176 | return win32_inet_pton(af, src, dst); |
| 177 | #else |
| 178 | return ::inet_pton(af, src, dst); |
| 179 | #endif |
| 180 | } |
| 181 | |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 182 | bool HasIPv4Enabled() { |
| 183 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 184 | bool has_ipv4 = false; |
| 185 | struct ifaddrs* ifa; |
| 186 | if (getifaddrs(&ifa) < 0) { |
| 187 | return false; |
| 188 | } |
| 189 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 190 | if (cur->ifa_addr->sa_family == AF_INET) { |
| 191 | has_ipv4 = true; |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | freeifaddrs(ifa); |
| 196 | return has_ipv4; |
| 197 | #else |
| 198 | return true; |
| 199 | #endif |
| 200 | } |
| 201 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 202 | bool HasIPv6Enabled() { |
Robin Raymond | ce1b140 | 2018-11-22 20:10:11 -0500 | [diff] [blame] | 203 | #if defined(WINUWP) |
| 204 | // WinUWP always has IPv6 capability. |
| 205 | return true; |
| 206 | #elif defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 207 | if (IsWindowsVistaOrLater()) { |
| 208 | return true; |
| 209 | } |
| 210 | if (!IsWindowsXpOrLater()) { |
| 211 | return false; |
| 212 | } |
| 213 | DWORD protbuff_size = 4096; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 214 | std::unique_ptr<char[]> protocols; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 215 | LPWSAPROTOCOL_INFOW protocol_infos = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 216 | int requested_protocols[2] = {AF_INET6, 0}; |
| 217 | |
| 218 | int err = 0; |
| 219 | int ret = 0; |
| 220 | // Check for protocols in a do-while loop until we provide a buffer large |
| 221 | // enough. (WSCEnumProtocols sets protbuff_size to its desired value). |
| 222 | // It is extremely unlikely that this will loop more than once. |
| 223 | do { |
| 224 | protocols.reset(new char[protbuff_size]); |
| 225 | protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get()); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 226 | ret = WSCEnumProtocols(requested_protocols, protocol_infos, &protbuff_size, |
| 227 | &err); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 228 | } while (ret == SOCKET_ERROR && err == WSAENOBUFS); |
| 229 | |
| 230 | if (ret == SOCKET_ERROR) { |
| 231 | return false; |
| 232 | } |
| 233 | |
| 234 | // Even if ret is positive, check specifically for IPv6. |
| 235 | // Non-IPv6 enabled WinXP will still return a RAW protocol. |
| 236 | for (int i = 0; i < ret; ++i) { |
| 237 | if (protocol_infos[i].iAddressFamily == AF_INET6) { |
| 238 | return true; |
| 239 | } |
| 240 | } |
| 241 | return false; |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 242 | #elif defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 243 | bool has_ipv6 = false; |
| 244 | struct ifaddrs* ifa; |
| 245 | if (getifaddrs(&ifa) < 0) { |
| 246 | return false; |
| 247 | } |
| 248 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 249 | if (cur->ifa_addr->sa_family == AF_INET6) { |
| 250 | has_ipv6 = true; |
| 251 | break; |
| 252 | } |
| 253 | } |
| 254 | freeifaddrs(ifa); |
| 255 | return has_ipv6; |
| 256 | #else |
| 257 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 258 | #endif |
| 259 | } |
| 260 | } // namespace rtc |