blob: ab5e16f4894f75b9ed53127ea675f2b53ceb9b90 [file] [log] [blame]
[email protected]9eb7b11b2012-03-28 20:19:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]b59ff372009-07-15 22:04:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]f2cb3cf2013-03-21 01:40:535#include "net/dns/host_resolver_proc.h"
[email protected]b59ff372009-07-15 22:04:326
7#include "build/build_config.h"
8
[email protected]b59ff372009-07-15 22:04:329#include "base/logging.h"
[email protected]9eb7b11b2012-03-28 20:19:3110#include "base/sys_byteorder.h"
Francois Doraya2d01ba2017-09-25 19:17:4011#include "base/threading/scoped_blocking_call.h"
[email protected]b59ff372009-07-15 22:04:3212#include "net/base/address_list.h"
13#include "net/base/net_errors.h"
[email protected]a540c2d2009-12-12 00:47:3714#include "net/base/sys_addrinfo.h"
tfarina47598f042015-10-07 23:08:3515#include "net/dns/dns_reloader.h"
tfarina77021d62015-10-11 20:19:0316#include "net/dns/dns_util.h"
Eric Orth59066222019-03-07 23:52:2717#include "net/dns/host_resolver.h"
[email protected]b59ff372009-07-15 22:04:3218
[email protected]bae892f92011-10-20 22:39:2619#if defined(OS_OPENBSD)
20#define AI_ADDRCONFIG 0
[email protected]23f771162011-06-02 18:37:5121#endif
22
[email protected]b59ff372009-07-15 22:04:3223namespace net {
24
[email protected]eaf3a3b2010-09-03 20:34:2725namespace {
26
27bool IsAllLocalhostOfOneFamily(const struct addrinfo* ai) {
28 bool saw_v4_localhost = false;
29 bool saw_v6_localhost = false;
30 for (; ai != NULL; ai = ai->ai_next) {
31 switch (ai->ai_family) {
32 case AF_INET: {
33 const struct sockaddr_in* addr_in =
34 reinterpret_cast<struct sockaddr_in*>(ai->ai_addr);
[email protected]9eb7b11b2012-03-28 20:19:3135 if ((base::NetToHost32(addr_in->sin_addr.s_addr) & 0xff000000) ==
36 0x7f000000)
[email protected]eaf3a3b2010-09-03 20:34:2737 saw_v4_localhost = true;
38 else
39 return false;
40 break;
41 }
42 case AF_INET6: {
43 const struct sockaddr_in6* addr_in6 =
44 reinterpret_cast<struct sockaddr_in6*>(ai->ai_addr);
45 if (IN6_IS_ADDR_LOOPBACK(&addr_in6->sin6_addr))
46 saw_v6_localhost = true;
47 else
48 return false;
49 break;
50 }
51 default:
52 NOTREACHED();
53 return false;
54 }
55 }
56
57 return saw_v4_localhost != saw_v6_localhost;
58}
59
60} // namespace
61
[email protected]b59ff372009-07-15 22:04:3262HostResolverProc* HostResolverProc::default_proc_ = NULL;
63
64HostResolverProc::HostResolverProc(HostResolverProc* previous) {
[email protected]c4ff4952010-01-08 19:12:4765 SetPreviousProc(previous);
[email protected]b59ff372009-07-15 22:04:3266
67 // Implicitly fall-back to the global default procedure.
68 if (!previous)
[email protected]c4ff4952010-01-08 19:12:4769 SetPreviousProc(default_proc_);
70}
71
Chris Watkins68b15032017-12-01 03:07:1372HostResolverProc::~HostResolverProc() = default;
[email protected]be1a48b2011-01-20 00:12:1373
74int HostResolverProc::ResolveUsingPrevious(
75 const std::string& host,
76 AddressFamily address_family,
77 HostResolverFlags host_resolver_flags,
78 AddressList* addrlist,
79 int* os_error) {
[email protected]90499482013-06-01 00:39:5080 if (previous_proc_.get()) {
81 return previous_proc_->Resolve(
82 host, address_family, host_resolver_flags, addrlist, os_error);
[email protected]be1a48b2011-01-20 00:12:1383 }
84
85 // Final fallback is the system resolver.
[email protected]1ee9afa12013-04-16 14:18:0686 return SystemHostResolverCall(host, address_family, host_resolver_flags,
[email protected]be1a48b2011-01-20 00:12:1387 addrlist, os_error);
88}
89
[email protected]c4ff4952010-01-08 19:12:4790void HostResolverProc::SetPreviousProc(HostResolverProc* proc) {
[email protected]90499482013-06-01 00:39:5091 HostResolverProc* current_previous = previous_proc_.get();
[email protected]c4ff4952010-01-08 19:12:4792 previous_proc_ = NULL;
93 // Now that we've guaranteed |this| is the last proc in a chain, we can
94 // detect potential cycles using GetLastProc().
95 previous_proc_ = (GetLastProc(proc) == this) ? current_previous : proc;
96}
97
98void HostResolverProc::SetLastProc(HostResolverProc* proc) {
99 GetLastProc(this)->SetPreviousProc(proc);
100}
101
102// static
103HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) {
104 if (proc == NULL)
105 return NULL;
106 HostResolverProc* last_proc = proc;
[email protected]90499482013-06-01 00:39:50107 while (last_proc->previous_proc_.get() != NULL)
108 last_proc = last_proc->previous_proc_.get();
[email protected]c4ff4952010-01-08 19:12:47109 return last_proc;
[email protected]b59ff372009-07-15 22:04:32110}
111
112// static
113HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) {
114 HostResolverProc* old = default_proc_;
115 default_proc_ = proc;
116 return old;
117}
118
119// static
120HostResolverProc* HostResolverProc::GetDefault() {
121 return default_proc_;
122}
123
[email protected]1ee9afa12013-04-16 14:18:06124int SystemHostResolverCall(const std::string& host,
[email protected]123ab1e32009-10-21 19:12:57125 AddressFamily address_family,
[email protected]5ea28dea2010-04-08 15:35:13126 HostResolverFlags host_resolver_flags,
[email protected]21526002010-05-16 19:42:46127 AddressList* addrlist,
128 int* os_error) {
Matt Menkeb44ea7f2017-07-20 21:35:37129 // |host| should be a valid domain name. HostResolverImpl::Resolve has checks
130 // to fail early if this is not the case.
131 DCHECK(IsValidDNSDomain(host));
ttuttlec7f5ee52015-03-07 01:44:01132
[email protected]21526002010-05-16 19:42:46133 if (os_error)
134 *os_error = 0;
135
[email protected]b59ff372009-07-15 22:04:32136 struct addrinfo* ai = NULL;
137 struct addrinfo hints = {0};
[email protected]123ab1e32009-10-21 19:12:57138
139 switch (address_family) {
[email protected]dd030332009-10-23 04:35:31140 case ADDRESS_FAMILY_IPV4:
[email protected]123ab1e32009-10-21 19:12:57141 hints.ai_family = AF_INET;
142 break;
[email protected]dd030332009-10-23 04:35:31143 case ADDRESS_FAMILY_IPV6:
144 hints.ai_family = AF_INET6;
145 break;
146 case ADDRESS_FAMILY_UNSPECIFIED:
147 hints.ai_family = AF_UNSPEC;
148 break;
[email protected]123ab1e32009-10-21 19:12:57149 default:
[email protected]dd030332009-10-23 04:35:31150 NOTREACHED();
[email protected]123ab1e32009-10-21 19:12:57151 hints.ai_family = AF_UNSPEC;
152 }
[email protected]b59ff372009-07-15 22:04:32153
[email protected]bae892f92011-10-20 22:39:26154#if defined(OS_WIN)
[email protected]b59ff372009-07-15 22:04:32155 // DO NOT USE AI_ADDRCONFIG ON WINDOWS.
156 //
157 // The following comment in <winsock2.h> is the best documentation I found
158 // on AI_ADDRCONFIG for Windows:
159 // Flags used in "hints" argument to getaddrinfo()
160 // - AI_ADDRCONFIG is supported starting with Vista
161 // - default is AI_ADDRCONFIG ON whether the flag is set or not
162 // because the performance penalty in not having ADDRCONFIG in
163 // the multi-protocol stack environment is severe;
164 // this defaulting may be disabled by specifying the AI_ALL flag,
165 // in that case AI_ADDRCONFIG must be EXPLICITLY specified to
166 // enable ADDRCONFIG behavior
167 //
168 // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the
169 // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo
170 // to fail with WSANO_DATA (11004) for "localhost", probably because of the
171 // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page:
172 // The IPv4 or IPv6 loopback address is not considered a valid global
173 // address.
174 // See http://crbug.com/5234.
[email protected]1a157302010-01-29 03:36:45175 //
176 // OpenBSD does not support it, either.
[email protected]b59ff372009-07-15 22:04:32177 hints.ai_flags = 0;
[email protected]b59ff372009-07-15 22:04:32178#else
179 hints.ai_flags = AI_ADDRCONFIG;
180#endif
181
[email protected]2f3bc65c2010-07-23 17:47:10182 // On Linux AI_ADDRCONFIG doesn't consider loopback addreses, even if only
183 // loopback addresses are configured. So don't use it when there are only
184 // loopback addresses.
185 if (host_resolver_flags & HOST_RESOLVER_LOOPBACK_ONLY)
186 hints.ai_flags &= ~AI_ADDRCONFIG;
187
[email protected]5ea28dea2010-04-08 15:35:13188 if (host_resolver_flags & HOST_RESOLVER_CANONNAME)
189 hints.ai_flags |= AI_CANONNAME;
190
[email protected]b59ff372009-07-15 22:04:32191 // Restrict result set to only this socket type to avoid duplicates.
192 hints.ai_socktype = SOCK_STREAM;
193
Francois Doraya2d01ba2017-09-25 19:17:40194 // This function can block for a long time. Use ScopedBlockingCall to increase
195 // the current thread pool's capacity and thus avoid reducing CPU usage by the
196 // current process during that time.
Etienne Bergeron436d42212019-02-26 17:15:12197 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
198 base::BlockingType::WILL_BLOCK);
Francois Doraya2d01ba2017-09-25 19:17:40199
[email protected]e1439022011-09-05 23:40:51200#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
Sergey Ulanov49085572017-07-10 23:25:46201 !defined(OS_ANDROID) && !defined(OS_FUCHSIA)
[email protected]46018c9d2011-09-06 03:42:34202 DnsReloaderMaybeReload();
[email protected]e1439022011-09-05 23:40:51203#endif
[email protected]46018c9d2011-09-06 03:42:34204 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
205 bool should_retry = false;
[email protected]eaf3a3b2010-09-03 20:34:27206 // If the lookup was restricted (either by address family, or address
207 // detection), and the results where all localhost of a single family,
208 // maybe we should retry. There were several bugs related to these
209 // issues, for example http://crbug.com/42058 and http://crbug.com/49024
210 if ((hints.ai_family != AF_UNSPEC || hints.ai_flags & AI_ADDRCONFIG) &&
211 err == 0 && IsAllLocalhostOfOneFamily(ai)) {
212 if (host_resolver_flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) {
213 hints.ai_family = AF_UNSPEC;
214 should_retry = true;
215 }
216 if (hints.ai_flags & AI_ADDRCONFIG) {
217 hints.ai_flags &= ~AI_ADDRCONFIG;
218 should_retry = true;
219 }
220 }
221 if (should_retry) {
[email protected]ab6d12232010-09-07 19:39:37222 if (ai != NULL) {
223 freeaddrinfo(ai);
224 ai = NULL;
225 }
[email protected]eaf3a3b2010-09-03 20:34:27226 err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
227 }
[email protected]b59ff372009-07-15 22:04:32228
[email protected]21526002010-05-16 19:42:46229 if (err) {
[email protected]21526002010-05-16 19:42:46230#if defined(OS_WIN)
[email protected]8ed0cab62010-10-15 04:12:45231 err = WSAGetLastError();
[email protected]21526002010-05-16 19:42:46232#endif
[email protected]8ed0cab62010-10-15 04:12:45233
234 // Return the OS error to the caller.
235 if (os_error)
236 *os_error = err;
237
238 // If the call to getaddrinfo() failed because of a system error, report
239 // it separately from ERR_NAME_NOT_RESOLVED.
240#if defined(OS_WIN)
241 if (err != WSAHOST_NOT_FOUND && err != WSANO_DATA)
242 return ERR_NAME_RESOLUTION_FAILED;
[email protected]23f771162011-06-02 18:37:51243#elif defined(OS_POSIX) && !defined(OS_FREEBSD)
[email protected]8ed0cab62010-10-15 04:12:45244 if (err != EAI_NONAME && err != EAI_NODATA)
245 return ERR_NAME_RESOLUTION_FAILED;
246#endif
247
[email protected]b59ff372009-07-15 22:04:32248 return ERR_NAME_NOT_RESOLVED;
[email protected]21526002010-05-16 19:42:46249 }
[email protected]b59ff372009-07-15 22:04:32250
[email protected]c33dc2e2012-06-22 21:36:43251#if defined(OS_ANDROID)
252 // Workaround for Android's getaddrinfo leaving ai==NULL without an error.
253 // http://crbug.com/134142
254 if (ai == NULL)
255 return ERR_NAME_NOT_RESOLVED;
256#endif
257
[email protected]7054e78f2012-05-07 21:44:56258 *addrlist = AddressList::CreateFromAddrinfo(ai);
259 freeaddrinfo(ai);
[email protected]b59ff372009-07-15 22:04:32260 return OK;
261}
262
[email protected]1ee9afa12013-04-16 14:18:06263SystemHostResolverProc::SystemHostResolverProc() : HostResolverProc(NULL) {}
264
265int SystemHostResolverProc::Resolve(const std::string& hostname,
266 AddressFamily address_family,
267 HostResolverFlags host_resolver_flags,
268 AddressList* addr_list,
269 int* os_error) {
270 return SystemHostResolverCall(hostname,
271 address_family,
272 host_resolver_flags,
273 addr_list,
274 os_error);
275}
276
Chris Watkins68b15032017-12-01 03:07:13277SystemHostResolverProc::~SystemHostResolverProc() = default;
[email protected]1ee9afa12013-04-16 14:18:06278
Eric Orth59066222019-03-07 23:52:27279const base::TimeDelta ProcTaskParams::kDnsDefaultUnresponsiveDelay =
280 base::TimeDelta::FromSeconds(6);
281
282ProcTaskParams::ProcTaskParams(HostResolverProc* resolver_proc,
283 size_t max_retry_attempts)
284 : resolver_proc(resolver_proc),
285 max_retry_attempts(max_retry_attempts),
286 unresponsive_delay(kDnsDefaultUnresponsiveDelay),
287 retry_factor(2) {
288 // Maximum of 4 retry attempts for host resolution.
289 static const size_t kDefaultMaxRetryAttempts = 4u;
290 if (max_retry_attempts == HostResolver::kDefaultRetryAttempts)
291 max_retry_attempts = kDefaultMaxRetryAttempts;
292}
293
294ProcTaskParams::ProcTaskParams(const ProcTaskParams& other) = default;
295
296ProcTaskParams::~ProcTaskParams() = default;
297
[email protected]b59ff372009-07-15 22:04:32298} // namespace net