blob: 6a411a6fc55b7cf22534cc8a3fe182fc8f98f7bf [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
14
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#if defined(WEBRTC_WIN)
16#include <ws2spi.h>
17#include <ws2tcpip.h>
18#include "webrtc/base/win32.h"
19#endif
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -070020#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.orgf0488722014-05-13 18:00:26 +000027
nissebc8feda2017-06-29 06:21:20 -070028#include "webrtc/base/bind.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000029#include "webrtc/base/byteorder.h"
nissec80e7412017-01-11 05:56:46 -080030#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031#include "webrtc/base/logging.h"
nissebc8feda2017-06-29 06:21:20 -070032#include "webrtc/base/ptr_util.h"
33#include "webrtc/base/task_queue.h"
34#include "webrtc/base/thread.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035
36namespace rtc {
37
nissebc8feda2017-06-29 06:21:20 -070038namespace {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039int ResolveHostname(const std::string& hostname, int family,
40 std::vector<IPAddress>* addresses) {
41#ifdef __native_client__
nissec80e7412017-01-11 05:56:46 -080042 RTC_NOTREACHED();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 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();
deadbeef37f5ecf2017-02-27 14:06:41 -080050 struct addrinfo* result = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000051 struct addrinfo hints = {0};
Honghai Zhang56c0b202016-06-26 22:11:12 -070052 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.orgf0488722014-05-13 18:00:26 +000070 hints.ai_flags = AI_ADDRCONFIG;
deadbeef37f5ecf2017-02-27 14:06:41 -080071 int ret = getaddrinfo(hostname.c_str(), nullptr, &hints, &result);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072 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}
nissebc8feda2017-06-29 06:21:20 -070088} // namespace
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089
90// AsyncResolver
nissebc8feda2017-06-29 06:21:20 -070091AsyncResolver::AsyncResolver() : construction_thread_(Thread::Current()) {
92 RTC_DCHECK(construction_thread_);
93}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000094
nissebc8feda2017-06-29 06:21:20 -070095AsyncResolver::~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.org67186fe2015-03-09 22:21:53 +0000108
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000109void AsyncResolver::Start(const SocketAddress& addr) {
nissebc8feda2017-06-29 06:21:20 -0700110 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.orgf0488722014-05-13 18:00:26 +0000115 addr_ = addr;
nissebc8feda2017-06-29 06:21:20 -0700116 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.orgf0488722014-05-13 18:00:26 +0000136}
137
138bool 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.org67186fe2015-03-09 22:21:53 +0000152int AsyncResolver::GetError() const {
153 return error_;
154}
155
156void AsyncResolver::Destroy(bool wait) {
nissebc8feda2017-06-29 06:21:20 -0700157 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.org67186fe2015-03-09 22:21:53 +0000168}
169
nissebc8feda2017-06-29 06:21:20 -0700170void 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.orgf0488722014-05-13 18:00:26 +0000179
nissebc8feda2017-06-29 06:21:20 -0700180 // 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.orgf0488722014-05-13 18:00:26 +0000192}
193
194const 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
202int 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
deadbeef9a6f4d42017-05-15 19:43:33 -0700210bool 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.orgf0488722014-05-13 18:00:26 +0000230bool HasIPv6Enabled() {
Taylor Brandstetter2b3bf6b2016-05-19 14:57:31 -0700231#if defined(WEBRTC_WIN)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000232 if (IsWindowsVistaOrLater()) {
233 return true;
234 }
235 if (!IsWindowsXpOrLater()) {
236 return false;
237 }
238 DWORD protbuff_size = 4096;
jbauch555604a2016-04-26 03:13:22 -0700239 std::unique_ptr<char[]> protocols;
deadbeef37f5ecf2017-02-27 14:06:41 -0800240 LPWSAPROTOCOL_INFOW protocol_infos = nullptr;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000241 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 Brandstetter2b3bf6b2016-05-19 14:57:31 -0700267#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.orgf0488722014-05-13 18:00:26 +0000283#endif
284}
285} // namespace rtc