1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker * Copyright 2008 The WebRTC Project Authors. All rights reserved.
3*d9f75844SAndroid Build Coastguard Worker *
4*d9f75844SAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker * that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker * tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker * in the file PATENTS. All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker * be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker */
10*d9f75844SAndroid Build Coastguard Worker
11*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/async_resolver.h"
12*d9f75844SAndroid Build Coastguard Worker
13*d9f75844SAndroid Build Coastguard Worker #include <memory>
14*d9f75844SAndroid Build Coastguard Worker #include <string>
15*d9f75844SAndroid Build Coastguard Worker #include <utility>
16*d9f75844SAndroid Build Coastguard Worker
17*d9f75844SAndroid Build Coastguard Worker #include "absl/strings/string_view.h"
18*d9f75844SAndroid Build Coastguard Worker #include "api/ref_counted_base.h"
19*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
20*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
21*d9f75844SAndroid Build Coastguard Worker
22*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
23*d9f75844SAndroid Build Coastguard Worker #include <ws2spi.h>
24*d9f75844SAndroid Build Coastguard Worker #include <ws2tcpip.h>
25*d9f75844SAndroid Build Coastguard Worker
26*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/win32.h"
27*d9f75844SAndroid Build Coastguard Worker #endif
28*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_POSIX) && !defined(__native_client__)
29*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_ANDROID)
30*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ifaddrs_android.h"
31*d9f75844SAndroid Build Coastguard Worker #else
32*d9f75844SAndroid Build Coastguard Worker #include <ifaddrs.h>
33*d9f75844SAndroid Build Coastguard Worker #endif
34*d9f75844SAndroid Build Coastguard Worker #endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
35*d9f75844SAndroid Build Coastguard Worker
36*d9f75844SAndroid Build Coastguard Worker #include "api/task_queue/task_queue_base.h"
37*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/ip_address.h"
38*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/logging.h"
39*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/platform_thread.h"
40*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/task_queue.h"
41*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread...
42*d9f75844SAndroid Build Coastguard Worker
43*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
44*d9f75844SAndroid Build Coastguard Worker #include <dispatch/dispatch.h>
45*d9f75844SAndroid Build Coastguard Worker #endif
46*d9f75844SAndroid Build Coastguard Worker
47*d9f75844SAndroid Build Coastguard Worker namespace rtc {
48*d9f75844SAndroid Build Coastguard Worker
49*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
50*d9f75844SAndroid Build Coastguard Worker namespace {
51*d9f75844SAndroid Build Coastguard Worker
GlobalGcdRunTask(void * context)52*d9f75844SAndroid Build Coastguard Worker void GlobalGcdRunTask(void* context) {
53*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<absl::AnyInvocable<void() &&>> task(
54*d9f75844SAndroid Build Coastguard Worker static_cast<absl::AnyInvocable<void() &&>*>(context));
55*d9f75844SAndroid Build Coastguard Worker std::move (*task)();
56*d9f75844SAndroid Build Coastguard Worker }
57*d9f75844SAndroid Build Coastguard Worker
58*d9f75844SAndroid Build Coastguard Worker // Post a task into the system-defined global concurrent queue.
PostTaskToGlobalQueue(std::unique_ptr<absl::AnyInvocable<void ()&&>> task)59*d9f75844SAndroid Build Coastguard Worker void PostTaskToGlobalQueue(
60*d9f75844SAndroid Build Coastguard Worker std::unique_ptr<absl::AnyInvocable<void() &&>> task) {
61*d9f75844SAndroid Build Coastguard Worker dispatch_queue_global_t global_queue =
62*d9f75844SAndroid Build Coastguard Worker dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
63*d9f75844SAndroid Build Coastguard Worker dispatch_async_f(global_queue, task.release(), &GlobalGcdRunTask);
64*d9f75844SAndroid Build Coastguard Worker }
65*d9f75844SAndroid Build Coastguard Worker
66*d9f75844SAndroid Build Coastguard Worker } // namespace
67*d9f75844SAndroid Build Coastguard Worker #endif
68*d9f75844SAndroid Build Coastguard Worker
ResolveHostname(absl::string_view hostname,int family,std::vector<IPAddress> * addresses)69*d9f75844SAndroid Build Coastguard Worker int ResolveHostname(absl::string_view hostname,
70*d9f75844SAndroid Build Coastguard Worker int family,
71*d9f75844SAndroid Build Coastguard Worker std::vector<IPAddress>* addresses) {
72*d9f75844SAndroid Build Coastguard Worker #ifdef __native_client__
73*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_NOTREACHED();
74*d9f75844SAndroid Build Coastguard Worker RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
75*d9f75844SAndroid Build Coastguard Worker return -1;
76*d9f75844SAndroid Build Coastguard Worker #else // __native_client__
77*d9f75844SAndroid Build Coastguard Worker if (!addresses) {
78*d9f75844SAndroid Build Coastguard Worker return -1;
79*d9f75844SAndroid Build Coastguard Worker }
80*d9f75844SAndroid Build Coastguard Worker addresses->clear();
81*d9f75844SAndroid Build Coastguard Worker struct addrinfo* result = nullptr;
82*d9f75844SAndroid Build Coastguard Worker struct addrinfo hints = {0};
83*d9f75844SAndroid Build Coastguard Worker hints.ai_family = family;
84*d9f75844SAndroid Build Coastguard Worker // `family` here will almost always be AF_UNSPEC, because `family` comes from
85*d9f75844SAndroid Build Coastguard Worker // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed
86*d9f75844SAndroid Build Coastguard Worker // with a hostname. When a SocketAddress is constructed with a hostname, its
87*d9f75844SAndroid Build Coastguard Worker // family is AF_UNSPEC. However, if someday in the future we construct
88*d9f75844SAndroid Build Coastguard Worker // a SocketAddress with both a hostname and a family other than AF_UNSPEC,
89*d9f75844SAndroid Build Coastguard Worker // then it would be possible to get a specific family value here.
90*d9f75844SAndroid Build Coastguard Worker
91*d9f75844SAndroid Build Coastguard Worker // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as
92*d9f75844SAndroid Build Coastguard Worker // documented by the various operating systems:
93*d9f75844SAndroid Build Coastguard Worker // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
94*d9f75844SAndroid Build Coastguard Worker // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/
95*d9f75844SAndroid Build Coastguard Worker // ms738520(v=vs.85).aspx
96*d9f75844SAndroid Build Coastguard Worker // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/
97*d9f75844SAndroid Build Coastguard Worker // Reference/ManPages/man3/getaddrinfo.3.html
98*d9f75844SAndroid Build Coastguard Worker // Android (source code, not documentation):
99*d9f75844SAndroid Build Coastguard Worker // https://android.googlesource.com/platform/bionic/+/
100*d9f75844SAndroid Build Coastguard Worker // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657
101*d9f75844SAndroid Build Coastguard Worker hints.ai_flags = AI_ADDRCONFIG;
102*d9f75844SAndroid Build Coastguard Worker int ret =
103*d9f75844SAndroid Build Coastguard Worker getaddrinfo(std::string(hostname).c_str(), nullptr, &hints, &result);
104*d9f75844SAndroid Build Coastguard Worker if (ret != 0) {
105*d9f75844SAndroid Build Coastguard Worker return ret;
106*d9f75844SAndroid Build Coastguard Worker }
107*d9f75844SAndroid Build Coastguard Worker struct addrinfo* cursor = result;
108*d9f75844SAndroid Build Coastguard Worker for (; cursor; cursor = cursor->ai_next) {
109*d9f75844SAndroid Build Coastguard Worker if (family == AF_UNSPEC || cursor->ai_family == family) {
110*d9f75844SAndroid Build Coastguard Worker IPAddress ip;
111*d9f75844SAndroid Build Coastguard Worker if (IPFromAddrInfo(cursor, &ip)) {
112*d9f75844SAndroid Build Coastguard Worker addresses->push_back(ip);
113*d9f75844SAndroid Build Coastguard Worker }
114*d9f75844SAndroid Build Coastguard Worker }
115*d9f75844SAndroid Build Coastguard Worker }
116*d9f75844SAndroid Build Coastguard Worker freeaddrinfo(result);
117*d9f75844SAndroid Build Coastguard Worker return 0;
118*d9f75844SAndroid Build Coastguard Worker #endif // !__native_client__
119*d9f75844SAndroid Build Coastguard Worker }
120*d9f75844SAndroid Build Coastguard Worker
121*d9f75844SAndroid Build Coastguard Worker struct AsyncResolver::State : public RefCountedBase {
122*d9f75844SAndroid Build Coastguard Worker webrtc::Mutex mutex;
123*d9f75844SAndroid Build Coastguard Worker enum class Status {
124*d9f75844SAndroid Build Coastguard Worker kLive,
125*d9f75844SAndroid Build Coastguard Worker kDead
126*d9f75844SAndroid Build Coastguard Worker } status RTC_GUARDED_BY(mutex) = Status::kLive;
127*d9f75844SAndroid Build Coastguard Worker };
128*d9f75844SAndroid Build Coastguard Worker
AsyncResolver()129*d9f75844SAndroid Build Coastguard Worker AsyncResolver::AsyncResolver() : error_(-1), state_(new State) {}
130*d9f75844SAndroid Build Coastguard Worker
~AsyncResolver()131*d9f75844SAndroid Build Coastguard Worker AsyncResolver::~AsyncResolver() {
132*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&sequence_checker_);
133*d9f75844SAndroid Build Coastguard Worker
134*d9f75844SAndroid Build Coastguard Worker // Ensure the thread isn't using a stale reference to the current task queue,
135*d9f75844SAndroid Build Coastguard Worker // or calling into ResolveDone post destruction.
136*d9f75844SAndroid Build Coastguard Worker webrtc::MutexLock lock(&state_->mutex);
137*d9f75844SAndroid Build Coastguard Worker state_->status = State::Status::kDead;
138*d9f75844SAndroid Build Coastguard Worker }
139*d9f75844SAndroid Build Coastguard Worker
RunResolution(void * obj)140*d9f75844SAndroid Build Coastguard Worker void RunResolution(void* obj) {
141*d9f75844SAndroid Build Coastguard Worker std::function<void()>* function_ptr =
142*d9f75844SAndroid Build Coastguard Worker static_cast<std::function<void()>*>(obj);
143*d9f75844SAndroid Build Coastguard Worker (*function_ptr)();
144*d9f75844SAndroid Build Coastguard Worker delete function_ptr;
145*d9f75844SAndroid Build Coastguard Worker }
146*d9f75844SAndroid Build Coastguard Worker
Start(const SocketAddress & addr)147*d9f75844SAndroid Build Coastguard Worker void AsyncResolver::Start(const SocketAddress& addr) {
148*d9f75844SAndroid Build Coastguard Worker Start(addr, addr.family());
149*d9f75844SAndroid Build Coastguard Worker }
150*d9f75844SAndroid Build Coastguard Worker
Start(const SocketAddress & addr,int family)151*d9f75844SAndroid Build Coastguard Worker void AsyncResolver::Start(const SocketAddress& addr, int family) {
152*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&sequence_checker_);
153*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(!destroy_called_);
154*d9f75844SAndroid Build Coastguard Worker addr_ = addr;
155*d9f75844SAndroid Build Coastguard Worker auto thread_function =
156*d9f75844SAndroid Build Coastguard Worker [this, addr, family, caller_task_queue = webrtc::TaskQueueBase::Current(),
157*d9f75844SAndroid Build Coastguard Worker state = state_] {
158*d9f75844SAndroid Build Coastguard Worker std::vector<IPAddress> addresses;
159*d9f75844SAndroid Build Coastguard Worker int error = ResolveHostname(addr.hostname(), family, &addresses);
160*d9f75844SAndroid Build Coastguard Worker webrtc::MutexLock lock(&state->mutex);
161*d9f75844SAndroid Build Coastguard Worker if (state->status == State::Status::kLive) {
162*d9f75844SAndroid Build Coastguard Worker caller_task_queue->PostTask(
163*d9f75844SAndroid Build Coastguard Worker [this, error, addresses = std::move(addresses), state] {
164*d9f75844SAndroid Build Coastguard Worker bool live;
165*d9f75844SAndroid Build Coastguard Worker {
166*d9f75844SAndroid Build Coastguard Worker // ResolveDone can lead to instance destruction, so make sure
167*d9f75844SAndroid Build Coastguard Worker // we don't deadlock.
168*d9f75844SAndroid Build Coastguard Worker webrtc::MutexLock lock(&state->mutex);
169*d9f75844SAndroid Build Coastguard Worker live = state->status == State::Status::kLive;
170*d9f75844SAndroid Build Coastguard Worker }
171*d9f75844SAndroid Build Coastguard Worker if (live) {
172*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&sequence_checker_);
173*d9f75844SAndroid Build Coastguard Worker ResolveDone(std::move(addresses), error);
174*d9f75844SAndroid Build Coastguard Worker }
175*d9f75844SAndroid Build Coastguard Worker });
176*d9f75844SAndroid Build Coastguard Worker }
177*d9f75844SAndroid Build Coastguard Worker };
178*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
179*d9f75844SAndroid Build Coastguard Worker PostTaskToGlobalQueue(
180*d9f75844SAndroid Build Coastguard Worker std::make_unique<absl::AnyInvocable<void() &&>>(thread_function));
181*d9f75844SAndroid Build Coastguard Worker #else
182*d9f75844SAndroid Build Coastguard Worker PlatformThread::SpawnDetached(std::move(thread_function), "AsyncResolver");
183*d9f75844SAndroid Build Coastguard Worker #endif
184*d9f75844SAndroid Build Coastguard Worker }
185*d9f75844SAndroid Build Coastguard Worker
GetResolvedAddress(int family,SocketAddress * addr) const186*d9f75844SAndroid Build Coastguard Worker bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
187*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&sequence_checker_);
188*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(!destroy_called_);
189*d9f75844SAndroid Build Coastguard Worker if (error_ != 0 || addresses_.empty())
190*d9f75844SAndroid Build Coastguard Worker return false;
191*d9f75844SAndroid Build Coastguard Worker
192*d9f75844SAndroid Build Coastguard Worker *addr = addr_;
193*d9f75844SAndroid Build Coastguard Worker for (size_t i = 0; i < addresses_.size(); ++i) {
194*d9f75844SAndroid Build Coastguard Worker if (family == addresses_[i].family()) {
195*d9f75844SAndroid Build Coastguard Worker addr->SetResolvedIP(addresses_[i]);
196*d9f75844SAndroid Build Coastguard Worker return true;
197*d9f75844SAndroid Build Coastguard Worker }
198*d9f75844SAndroid Build Coastguard Worker }
199*d9f75844SAndroid Build Coastguard Worker return false;
200*d9f75844SAndroid Build Coastguard Worker }
201*d9f75844SAndroid Build Coastguard Worker
GetError() const202*d9f75844SAndroid Build Coastguard Worker int AsyncResolver::GetError() const {
203*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&sequence_checker_);
204*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(!destroy_called_);
205*d9f75844SAndroid Build Coastguard Worker return error_;
206*d9f75844SAndroid Build Coastguard Worker }
207*d9f75844SAndroid Build Coastguard Worker
Destroy(bool wait)208*d9f75844SAndroid Build Coastguard Worker void AsyncResolver::Destroy(bool wait) {
209*d9f75844SAndroid Build Coastguard Worker // Some callers have trouble guaranteeing that Destroy is called on the
210*d9f75844SAndroid Build Coastguard Worker // sequence guarded by `sequence_checker_`.
211*d9f75844SAndroid Build Coastguard Worker // RTC_DCHECK_RUN_ON(&sequence_checker_);
212*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(!destroy_called_);
213*d9f75844SAndroid Build Coastguard Worker destroy_called_ = true;
214*d9f75844SAndroid Build Coastguard Worker MaybeSelfDestruct();
215*d9f75844SAndroid Build Coastguard Worker }
216*d9f75844SAndroid Build Coastguard Worker
addresses() const217*d9f75844SAndroid Build Coastguard Worker const std::vector<IPAddress>& AsyncResolver::addresses() const {
218*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK_RUN_ON(&sequence_checker_);
219*d9f75844SAndroid Build Coastguard Worker RTC_DCHECK(!destroy_called_);
220*d9f75844SAndroid Build Coastguard Worker return addresses_;
221*d9f75844SAndroid Build Coastguard Worker }
222*d9f75844SAndroid Build Coastguard Worker
ResolveDone(std::vector<IPAddress> addresses,int error)223*d9f75844SAndroid Build Coastguard Worker void AsyncResolver::ResolveDone(std::vector<IPAddress> addresses, int error) {
224*d9f75844SAndroid Build Coastguard Worker addresses_ = addresses;
225*d9f75844SAndroid Build Coastguard Worker error_ = error;
226*d9f75844SAndroid Build Coastguard Worker recursion_check_ = true;
227*d9f75844SAndroid Build Coastguard Worker SignalDone(this);
228*d9f75844SAndroid Build Coastguard Worker MaybeSelfDestruct();
229*d9f75844SAndroid Build Coastguard Worker }
230*d9f75844SAndroid Build Coastguard Worker
MaybeSelfDestruct()231*d9f75844SAndroid Build Coastguard Worker void AsyncResolver::MaybeSelfDestruct() {
232*d9f75844SAndroid Build Coastguard Worker if (!recursion_check_) {
233*d9f75844SAndroid Build Coastguard Worker delete this;
234*d9f75844SAndroid Build Coastguard Worker } else {
235*d9f75844SAndroid Build Coastguard Worker recursion_check_ = false;
236*d9f75844SAndroid Build Coastguard Worker }
237*d9f75844SAndroid Build Coastguard Worker }
238*d9f75844SAndroid Build Coastguard Worker
239*d9f75844SAndroid Build Coastguard Worker } // namespace rtc
240