| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2008 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2008 The WebRTC Project Authors. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/rtc_base/nethelpers.h" | 11 #include "webrtc/rtc_base/nethelpers.h" |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 | 14 |
| 15 #if defined(WEBRTC_WIN) | 15 #if defined(WEBRTC_WIN) |
| 16 #include <ws2spi.h> | 16 #include <ws2spi.h> |
| 17 #include <ws2tcpip.h> | 17 #include <ws2tcpip.h> |
| 18 #include "webrtc/rtc_base/win32.h" | 18 #include "webrtc/rtc_base/win32.h" |
| 19 #endif | 19 #endif |
| 20 #if defined(WEBRTC_POSIX) && !defined(__native_client__) | 20 #if defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 21 #if defined(WEBRTC_ANDROID) | 21 #if defined(WEBRTC_ANDROID) |
| 22 #include "webrtc/rtc_base/ifaddrs-android.h" | 22 #include "webrtc/rtc_base/ifaddrs-android.h" |
| 23 #else | 23 #else |
| 24 #include <ifaddrs.h> | 24 #include <ifaddrs.h> |
| 25 #endif | 25 #endif |
| 26 #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) | 26 #endif // defined(WEBRTC_POSIX) && !defined(__native_client__) |
| 27 | 27 |
| 28 #include "webrtc/rtc_base/bind.h" | |
| 29 #include "webrtc/rtc_base/byteorder.h" | 28 #include "webrtc/rtc_base/byteorder.h" |
| 30 #include "webrtc/rtc_base/checks.h" | 29 #include "webrtc/rtc_base/checks.h" |
| 31 #include "webrtc/rtc_base/logging.h" | 30 #include "webrtc/rtc_base/logging.h" |
| 32 #include "webrtc/rtc_base/ptr_util.h" | 31 #include "webrtc/rtc_base/signalthread.h" |
| 33 #include "webrtc/rtc_base/task_queue.h" | |
| 34 #include "webrtc/rtc_base/thread.h" | |
| 35 | 32 |
| 36 namespace rtc { | 33 namespace rtc { |
| 37 | 34 |
| 38 namespace { | |
| 39 int ResolveHostname(const std::string& hostname, int family, | 35 int ResolveHostname(const std::string& hostname, int family, |
| 40 std::vector<IPAddress>* addresses) { | 36 std::vector<IPAddress>* addresses) { |
| 41 #ifdef __native_client__ | 37 #ifdef __native_client__ |
| 42 RTC_NOTREACHED(); | 38 RTC_NOTREACHED(); |
| 43 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; | 39 LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl"; |
| 44 return -1; | 40 return -1; |
| 45 #else // __native_client__ | 41 #else // __native_client__ |
| 46 if (!addresses) { | 42 if (!addresses) { |
| 47 return -1; | 43 return -1; |
| 48 } | 44 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 78 IPAddress ip; | 74 IPAddress ip; |
| 79 if (IPFromAddrInfo(cursor, &ip)) { | 75 if (IPFromAddrInfo(cursor, &ip)) { |
| 80 addresses->push_back(ip); | 76 addresses->push_back(ip); |
| 81 } | 77 } |
| 82 } | 78 } |
| 83 } | 79 } |
| 84 freeaddrinfo(result); | 80 freeaddrinfo(result); |
| 85 return 0; | 81 return 0; |
| 86 #endif // !__native_client__ | 82 #endif // !__native_client__ |
| 87 } | 83 } |
| 88 } // namespace | |
| 89 | 84 |
| 90 // AsyncResolver | 85 // AsyncResolver |
| 91 AsyncResolver::AsyncResolver() : construction_thread_(Thread::Current()) { | 86 AsyncResolver::AsyncResolver() |
| 92 RTC_DCHECK(construction_thread_); | 87 : SignalThread(false /* use_socket_server */), error_(-1) {} |
| 93 } | |
| 94 | 88 |
| 95 AsyncResolver::~AsyncResolver() { | 89 AsyncResolver::~AsyncResolver() = default; |
| 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 } | |
| 108 | 90 |
| 109 void AsyncResolver::Start(const SocketAddress& addr) { | 91 void AsyncResolver::Start(const SocketAddress& addr) { |
| 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"); | |
| 115 addr_ = addr; | 92 addr_ = addr; |
| 116 state_ = new RefCountedObject<Trampoline>(this); | 93 // SignalThred Start will kickoff the resolve process. |
| 117 | 94 SignalThread::Start(); |
| 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 }); | |
| 136 } | 95 } |
| 137 | 96 |
| 138 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { | 97 bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const { |
| 139 if (error_ != 0 || addresses_.empty()) | 98 if (error_ != 0 || addresses_.empty()) |
| 140 return false; | 99 return false; |
| 141 | 100 |
| 142 *addr = addr_; | 101 *addr = addr_; |
| 143 for (size_t i = 0; i < addresses_.size(); ++i) { | 102 for (size_t i = 0; i < addresses_.size(); ++i) { |
| 144 if (family == addresses_[i].family()) { | 103 if (family == addresses_[i].family()) { |
| 145 addr->SetResolvedIP(addresses_[i]); | 104 addr->SetResolvedIP(addresses_[i]); |
| 146 return true; | 105 return true; |
| 147 } | 106 } |
| 148 } | 107 } |
| 149 return false; | 108 return false; |
| 150 } | 109 } |
| 151 | 110 |
| 152 int AsyncResolver::GetError() const { | 111 int AsyncResolver::GetError() const { |
| 153 return error_; | 112 return error_; |
| 154 } | 113 } |
| 155 | 114 |
| 156 void AsyncResolver::Destroy(bool wait) { | 115 void AsyncResolver::Destroy(bool wait) { |
| 157 RTC_DCHECK_RUN_ON(construction_thread_); | 116 SignalThread::Destroy(wait); |
| 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 } | |
| 168 } | 117 } |
| 169 | 118 |
| 170 void AsyncResolver::ResolveDone(int error, std::vector<IPAddress> addresses) { | 119 void AsyncResolver::DoWork() { |
| 171 RTC_DCHECK_RUN_ON(construction_thread_); | 120 error_ = ResolveHostname(addr_.hostname().c_str(), addr_.family(), |
| 172 error_ = error; | 121 &addresses_); |
| 173 addresses_ = std::move(addresses); | 122 } |
| 174 if (destroyed_) { | |
| 175 delete this; | |
| 176 return; | |
| 177 } else { | |
| 178 // Beware that SignalDone may call Destroy. | |
| 179 | 123 |
| 180 // TODO(nisse): Currently allows only Destroy(false) in this case, | 124 void AsyncResolver::OnWorkDone() { |
| 181 // and that's what all webrtc code is using. With Destroy(true), | 125 SignalDone(this); |
| 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; | |
| 192 } | 126 } |
| 193 | 127 |
| 194 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { | 128 const char* inet_ntop(int af, const void *src, char* dst, socklen_t size) { |
| 195 #if defined(WEBRTC_WIN) | 129 #if defined(WEBRTC_WIN) |
| 196 return win32_inet_ntop(af, src, dst, size); | 130 return win32_inet_ntop(af, src, dst, size); |
| 197 #else | 131 #else |
| 198 return ::inet_ntop(af, src, dst, size); | 132 return ::inet_ntop(af, src, dst, size); |
| 199 #endif | 133 #endif |
| 200 } | 134 } |
| 201 | 135 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 break; | 210 break; |
| 277 } | 211 } |
| 278 } | 212 } |
| 279 freeifaddrs(ifa); | 213 freeifaddrs(ifa); |
| 280 return has_ipv6; | 214 return has_ipv6; |
| 281 #else | 215 #else |
| 282 return true; | 216 return true; |
| 283 #endif | 217 #endif |
| 284 } | 218 } |
| 285 } // namespace rtc | 219 } // namespace rtc |
| OLD | NEW |