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 | |
| 11 | #include "webrtc/base/nethelpers.h" |
| 12 | |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 13 | #include <memory> |
| 14 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 15 | #if defined(WEBRTC_WIN) |
| 16 | #include <ws2spi.h> |
| 17 | #include <ws2tcpip.h> |
| 18 | #include "webrtc/base/win32.h" |
| 19 | #endif |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 20 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 21 | #if defined(WEBRTC_ANDROID) |
| 22 | #include "webrtc/base/ifaddrs-android.h" |
| 23 | #else |
| 24 | #include <ifaddrs.h> |
| 25 | #endif |
| 26 | #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 27 | |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 28 | #include "webrtc/base/bind.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 29 | #include "webrtc/base/byteorder.h" |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 30 | #include "webrtc/base/checks.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 31 | #include "webrtc/base/logging.h" |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 32 | #include "webrtc/base/ptr_util.h" |
| 33 | #include "webrtc/base/task_queue.h" |
| 34 | #include "webrtc/base/thread.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 35 | |
| 36 | namespace rtc { |
| 37 | |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 38 | namespace { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 39 | int ResolveHostname(const std::string& hostname, int family, |
| 40 | std::vector<IPAddress>* addresses) { |
| 41 | #ifdef __native_client__ |
nisse | c80e741 | 2017-01-11 05:56:46 -0800 | [diff] [blame] | 42 | RTC_NOTREACHED(); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; |
| 44 | return -1; |
| 45 | #else // __native_client__ |
| 46 | if (!addresses) { |
| 47 | return -1; |
| 48 | } |
| 49 | addresses->clear(); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 50 | struct addrinfo* result = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 51 | struct addrinfo hints = {0}; |
Honghai Zhang | 56c0b20 | 2016-06-26 22:11:12 -0700 | [diff] [blame] | 52 | hints.ai_family = family; |
| 53 | // |family| here will almost always be AF_UNSPEC, because |family| comes from |
| 54 | // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed |
| 55 | // with a hostname. When a SocketAddress is constructed with a hostname, its |
| 56 | // family is AF_UNSPEC. However, if someday in the future we construct |
| 57 | // a SocketAddress with both a hostname and a family other than AF_UNSPEC, |
| 58 | // then it would be possible to get a specific family value here. |
| 59 | |
| 60 | // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as |
| 61 | // documented by the various operating systems: |
| 62 | // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html |
| 63 | // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ |
| 64 | // ms738520(v=vs.85).aspx |
| 65 | // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/ |
| 66 | // Reference/ManPages/man3/getaddrinfo.3.html |
| 67 | // Android (source code, not documentation): |
| 68 | // https://android.googlesource.com/platform/bionic/+/ |
| 69 | // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657 |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | hints.ai_flags = AI_ADDRCONFIG; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 71 | int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | if (ret != 0) { |
| 73 | return ret; |
| 74 | } |
| 75 | struct addrinfo* cursor = result; |
| 76 | for (; cursor; cursor = cursor->ai_next) { |
| 77 | if (family == AF_UNSPEC || cursor->ai_family == family) { |
| 78 | IPAddress ip; |
| 79 | if (IPFromAddrInfo(cursor, &ip)) { |
| 80 | addresses->push_back(ip); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | freeaddrinfo(result); |
| 85 | return 0; |
| 86 | #endif // !__native_client__ |
| 87 | } |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 88 | } // namespace |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 89 | |
| 90 | // AsyncResolver |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 91 | AsyncResolver::AsyncResolver() : construction_thread_(Thread::Current()) { |
| 92 | RTC_DCHECK(construction_thread_); |
| 93 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 94 | |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 95 | AsyncResolver::~AsyncResolver() { |
| 96 | RTC_DCHECK(construction_thread_->IsCurrent()); |
| 97 | if (state_) |
| 98 | // It's possible that we have a posted message waiting on the MessageQueue |
| 99 | // refering to this object. Indirection via the ref-counted state_ object |
| 100 | // ensure it doesn't access us after deletion. |
| 101 | |
| 102 | // TODO(nisse): An alternative approach to solve this problem would be to |
| 103 | // extend MessageQueue::Clear in some way to let us selectively cancel posts |
| 104 | // directed to this object. Then we wouldn't need any ref count, but its a |
| 105 | // larger change to the MessageQueue. |
| 106 | state_->resolver = nullptr; |
| 107 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 108 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 109 | void AsyncResolver::Start(const SocketAddress& addr) { |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 110 | RTC_DCHECK_RUN_ON(construction_thread_); |
| 111 | RTC_DCHECK(!resolver_queue_); |
| 112 | RTC_DCHECK(!state_); |
| 113 | // TODO(nisse): Support injection of task queue at construction? |
| 114 | resolver_queue_ = rtc::MakeUnique<TaskQueue>("AsyncResolverQueue"); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 115 | addr_ = addr; |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 116 | state_ = new RefCountedObject<Trampoline>(this); |
| 117 | |
| 118 | // These member variables need to be copied to local variables to make it |
| 119 | // possible to capture them, even for capture-by-copy. |
| 120 | scoped_refptr<Trampoline> state = state_; |
| 121 | rtc::Thread* construction_thread = construction_thread_; |
| 122 | resolver_queue_->PostTask([state, addr, construction_thread]() { |
| 123 | std::vector<IPAddress> addresses; |
| 124 | int error = |
| 125 | ResolveHostname(addr.hostname().c_str(), addr.family(), &addresses); |
| 126 | // Ensure SignalDone is called on the main thread. |
| 127 | // TODO(nisse): Should use move of the address list, but not easy until |
| 128 | // C++17. Since this code isn't performance critical, copy should be fine |
| 129 | // for now. |
| 130 | construction_thread->Post(RTC_FROM_HERE, [state, error, addresses]() { |
| 131 | if (!state->resolver) |
| 132 | return; |
| 133 | state->resolver->ResolveDone(error, std::move(addresses)); |
| 134 | }); |
| 135 | }); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
| 139 | if (error_ != 0 || addresses_.empty()) |
| 140 | return false; |
| 141 | |
| 142 | *addr = addr_; |
| 143 | for (size_t i = 0; i < addresses_.size(); ++i) { |
| 144 | if (family == addresses_[i].family()) { |
| 145 | addr->SetResolvedIP(addresses_[i]); |
| 146 | return true; |
| 147 | } |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 152 | int AsyncResolver::GetError() const { |
| 153 | return error_; |
| 154 | } |
| 155 | |
| 156 | void AsyncResolver::Destroy(bool wait) { |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 157 | RTC_DCHECK_RUN_ON(construction_thread_); |
| 158 | RTC_DCHECK(!state_ || state_->resolver); |
| 159 | // If we don't wait here, we will nevertheless wait in the destructor. |
| 160 | if (wait || !state_) { |
| 161 | // Destroy task queue, blocks on any currently running task. If we have a |
| 162 | // pending task, it will post a call to attempt to call ResolveDone before |
| 163 | // finishing, which we will never handle. |
| 164 | delete this; |
| 165 | } else { |
| 166 | destroyed_ = true; |
| 167 | } |
kwiberg@webrtc.org | 67186fe | 2015-03-09 22:21:53 +0000 | [diff] [blame] | 168 | } |
| 169 | |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 170 | void AsyncResolver::ResolveDone(int error, std::vector<IPAddress> addresses) { |
| 171 | RTC_DCHECK_RUN_ON(construction_thread_); |
| 172 | error_ = error; |
| 173 | addresses_ = std::move(addresses); |
| 174 | if (destroyed_) { |
| 175 | delete this; |
| 176 | return; |
| 177 | } else { |
| 178 | // Beware that SignalDone may call Destroy. |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 179 | |
nisse | bc8feda | 2017-06-29 06:21:20 -0700 | [diff] [blame^] | 180 | // TODO(nisse): Currently allows only Destroy(false) in this case, |
| 181 | // and that's what all webrtc code is using. With Destroy(true), |
| 182 | // this object would be destructed immediately, and the access |
| 183 | // both to |destroyed_| below as well as the sigslot machinery |
| 184 | // involved in SignalDone implies invalid use-after-free. |
| 185 | SignalDone(this); |
| 186 | if (destroyed_) { |
| 187 | delete this; |
| 188 | return; |
| 189 | } |
| 190 | } |
| 191 | state_ = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
| 195 | #if defined(WEBRTC_WIN) |
| 196 | return win32_inet_ntop(af, src, dst, size); |
| 197 | #else |
| 198 | return ::inet_ntop(af, src, dst, size); |
| 199 | #endif |
| 200 | } |
| 201 | |
| 202 | int inet_pton(int af, const char* src, void *dst) { |
| 203 | #if defined(WEBRTC_WIN) |
| 204 | return win32_inet_pton(af, src, dst); |
| 205 | #else |
| 206 | return ::inet_pton(af, src, dst); |
| 207 | #endif |
| 208 | } |
| 209 | |
deadbeef | 9a6f4d4 | 2017-05-15 19:43:33 -0700 | [diff] [blame] | 210 | bool HasIPv4Enabled() { |
| 211 | #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 212 | bool has_ipv4 = false; |
| 213 | struct ifaddrs* ifa; |
| 214 | if (getifaddrs(&ifa) < 0) { |
| 215 | return false; |
| 216 | } |
| 217 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 218 | if (cur->ifa_addr->sa_family == AF_INET) { |
| 219 | has_ipv4 = true; |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | freeifaddrs(ifa); |
| 224 | return has_ipv4; |
| 225 | #else |
| 226 | return true; |
| 227 | #endif |
| 228 | } |
| 229 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 230 | bool HasIPv6Enabled() { |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 231 | #if defined(WEBRTC_WIN) |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 232 | if (IsWindowsVistaOrLater()) { |
| 233 | return true; |
| 234 | } |
| 235 | if (!IsWindowsXpOrLater()) { |
| 236 | return false; |
| 237 | } |
| 238 | DWORD protbuff_size = 4096; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 239 | std::unique_ptr<char[]> protocols; |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 240 | LPWSAPROTOCOL_INFOW protocol_infos = nullptr; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 241 | int requested_protocols[2] = {AF_INET6, 0}; |
| 242 | |
| 243 | int err = 0; |
| 244 | int ret = 0; |
| 245 | // Check for protocols in a do-while loop until we provide a buffer large |
| 246 | // enough. (WSCEnumProtocols sets protbuff_size to its desired value). |
| 247 | // It is extremely unlikely that this will loop more than once. |
| 248 | do { |
| 249 | protocols.reset(new char[protbuff_size]); |
| 250 | protocol_infos = reinterpret_cast<LPWSAPROTOCOL_INFOW>(protocols.get()); |
| 251 | ret = WSCEnumProtocols(requested_protocols, protocol_infos, |
| 252 | &protbuff_size, &err); |
| 253 | } while (ret == SOCKET_ERROR && err == WSAENOBUFS); |
| 254 | |
| 255 | if (ret == SOCKET_ERROR) { |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | // Even if ret is positive, check specifically for IPv6. |
| 260 | // Non-IPv6 enabled WinXP will still return a RAW protocol. |
| 261 | for (int i = 0; i < ret; ++i) { |
| 262 | if (protocol_infos[i].iAddressFamily == AF_INET6) { |
| 263 | return true; |
| 264 | } |
| 265 | } |
| 266 | return false; |
Taylor Brandstetter | 2b3bf6b | 2016-05-19 14:57:31 -0700 | [diff] [blame] | 267 | #elif defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 268 | bool has_ipv6 = false; |
| 269 | struct ifaddrs* ifa; |
| 270 | if (getifaddrs(&ifa) < 0) { |
| 271 | return false; |
| 272 | } |
| 273 | for (struct ifaddrs* cur = ifa; cur != nullptr; cur = cur->ifa_next) { |
| 274 | if (cur->ifa_addr->sa_family == AF_INET6) { |
| 275 | has_ipv6 = true; |
| 276 | break; |
| 277 | } |
| 278 | } |
| 279 | freeifaddrs(ifa); |
| 280 | return has_ipv6; |
| 281 | #else |
| 282 | return true; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 283 | #endif |
| 284 | } |
| 285 | } // namespace rtc |