blob: 198b4984e51579b1d9d5af2bf43abcb176f5f622 [file] [log] [blame]
Mirko Bonadeie5f4c6b2021-01-15 10:41:01 +01001/*
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 "rtc_base/async_resolver.h"
12
13#include <string>
14#include <utility>
15
16#if defined(WEBRTC_WIN)
17#include <ws2spi.h>
18#include <ws2tcpip.h>
19
20#include "rtc_base/win32.h"
21#endif
22#if defined(WEBRTC_POSIX) && !defined(__native_client__)
23#if defined(WEBRTC_ANDROID)
24#include "rtc_base/ifaddrs_android.h"
25#else
26#include <ifaddrs.h>
27#endif
28#endif // defined(WEBRTC_POSIX) && !defined(__native_client__)
29
30#include "api/task_queue/task_queue_base.h"
31#include "rtc_base/ip_address.h"
32#include "rtc_base/logging.h"
33#include "rtc_base/task_queue.h"
34#include "rtc_base/task_utils/to_queued_task.h"
35#include "rtc_base/third_party/sigslot/sigslot.h" // for signal_with_thread...
36
37namespace rtc {
38
39int ResolveHostname(const std::string& hostname,
40 int family,
41 std::vector<IPAddress>* addresses) {
42#ifdef __native_client__
43 RTC_NOTREACHED();
44 RTC_LOG(LS_WARNING) << "ResolveHostname() is not implemented for NaCl";
45 return -1;
46#else // __native_client__
47 if (!addresses) {
48 return -1;
49 }
50 addresses->clear();
51 struct addrinfo* result = nullptr;
52 struct addrinfo hints = {0};
53 hints.ai_family = family;
54 // |family| here will almost always be AF_UNSPEC, because |family| comes from
55 // AsyncResolver::addr_.family(), which comes from a SocketAddress constructed
56 // with a hostname. When a SocketAddress is constructed with a hostname, its
57 // family is AF_UNSPEC. However, if someday in the future we construct
58 // a SocketAddress with both a hostname and a family other than AF_UNSPEC,
59 // then it would be possible to get a specific family value here.
60
61 // The behavior of AF_UNSPEC is roughly "get both ipv4 and ipv6", as
62 // documented by the various operating systems:
63 // Linux: http://man7.org/linux/man-pages/man3/getaddrinfo.3.html
64 // Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/
65 // ms738520(v=vs.85).aspx
66 // Mac: https://developer.apple.com/legacy/library/documentation/Darwin/
67 // Reference/ManPages/man3/getaddrinfo.3.html
68 // Android (source code, not documentation):
69 // https://android.googlesource.com/platform/bionic/+/
70 // 7e0bfb511e85834d7c6cb9631206b62f82701d60/libc/netbsd/net/getaddrinfo.c#1657
71 hints.ai_flags = AI_ADDRCONFIG;
72 int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
73 if (ret != 0) {
74 return ret;
75 }
76 struct addrinfo* cursor = result;
77 for (; cursor; cursor = cursor->ai_next) {
78 if (family == AF_UNSPEC || cursor->ai_family == family) {
79 IPAddress ip;
80 if (IPFromAddrInfo(cursor, &ip)) {
81 addresses->push_back(ip);
82 }
83 }
84 }
85 freeaddrinfo(result);
86 return 0;
87#endif // !__native_client__
88}
89
90AsyncResolver::AsyncResolver() : error_(-1) {}
91
92AsyncResolver::~AsyncResolver() {
93 RTC_DCHECK_RUN_ON(&sequence_checker_);
94}
95
96void AsyncResolver::Start(const SocketAddress& addr) {
97 RTC_DCHECK_RUN_ON(&sequence_checker_);
98 RTC_DCHECK(!destroy_called_);
99 addr_ = addr;
100 webrtc::TaskQueueBase* current_task_queue = webrtc::TaskQueueBase::Current();
101 popup_thread_ = Thread::Create();
102 popup_thread_->Start();
103 popup_thread_->PostTask(webrtc::ToQueuedTask(
104 [this, flag = safety_.flag(), addr, current_task_queue] {
105 std::vector<IPAddress> addresses;
106 int error =
107 ResolveHostname(addr.hostname().c_str(), addr.family(), &addresses);
108 current_task_queue->PostTask(webrtc::ToQueuedTask(
109 std::move(flag), [this, error, addresses = std::move(addresses)] {
110 RTC_DCHECK_RUN_ON(&sequence_checker_);
111 ResolveDone(std::move(addresses), error);
112 }));
113 }));
114}
115
116bool AsyncResolver::GetResolvedAddress(int family, SocketAddress* addr) const {
117 RTC_DCHECK_RUN_ON(&sequence_checker_);
118 RTC_DCHECK(!destroy_called_);
119 if (error_ != 0 || addresses_.empty())
120 return false;
121
122 *addr = addr_;
123 for (size_t i = 0; i < addresses_.size(); ++i) {
124 if (family == addresses_[i].family()) {
125 addr->SetResolvedIP(addresses_[i]);
126 return true;
127 }
128 }
129 return false;
130}
131
132int AsyncResolver::GetError() const {
133 RTC_DCHECK_RUN_ON(&sequence_checker_);
134 RTC_DCHECK(!destroy_called_);
135 return error_;
136}
137
138void AsyncResolver::Destroy(bool wait) {
139 // Some callers have trouble guaranteeing that Destroy is called on the
140 // sequence guarded by |sequence_checker_|.
141 // RTC_DCHECK_RUN_ON(&sequence_checker_);
142 RTC_DCHECK(!destroy_called_);
143 destroy_called_ = true;
144 MaybeSelfDestruct();
145}
146
147const std::vector<IPAddress>& AsyncResolver::addresses() const {
148 RTC_DCHECK_RUN_ON(&sequence_checker_);
149 RTC_DCHECK(!destroy_called_);
150 return addresses_;
151}
152
153void AsyncResolver::ResolveDone(std::vector<IPAddress> addresses, int error) {
154 addresses_ = addresses;
155 error_ = error;
156 recursion_check_ = true;
157 SignalDone(this);
158 MaybeSelfDestruct();
159}
160
161void AsyncResolver::MaybeSelfDestruct() {
162 if (!recursion_check_) {
163 delete this;
164 } else {
165 recursion_check_ = false;
166 }
167}
168
169} // namespace rtc