blob: 749993b74ac723b5d3d9ff07ac58db7fc1fbc524 [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"
[email protected]b59ff372009-07-15 22:04:3211#include "net/base/address_list.h"
12#include "net/base/net_errors.h"
[email protected]a540c2d2009-12-12 00:47:3713#include "net/base/sys_addrinfo.h"
tfarina47598f042015-10-07 23:08:3514#include "net/dns/dns_reloader.h"
tfarina77021d62015-10-11 20:19:0315#include "net/dns/dns_util.h"
[email protected]b59ff372009-07-15 22:04:3216
[email protected]bae892f92011-10-20 22:39:2617#if defined(OS_OPENBSD)
18#define AI_ADDRCONFIG 0
[email protected]23f771162011-06-02 18:37:5119#endif
20
[email protected]b59ff372009-07-15 22:04:3221namespace net {
22
[email protected]eaf3a3b2010-09-03 20:34:2723namespace {
24
25bool IsAllLocalhostOfOneFamily(const struct addrinfo* ai) {
26 bool saw_v4_localhost = false;
27 bool saw_v6_localhost = false;
28 for (; ai != NULL; ai = ai->ai_next) {
29 switch (ai->ai_family) {
30 case AF_INET: {
31 const struct sockaddr_in* addr_in =
32 reinterpret_cast<struct sockaddr_in*>(ai->ai_addr);
[email protected]9eb7b11b2012-03-28 20:19:3133 if ((base::NetToHost32(addr_in->sin_addr.s_addr) & 0xff000000) ==
34 0x7f000000)
[email protected]eaf3a3b2010-09-03 20:34:2735 saw_v4_localhost = true;
36 else
37 return false;
38 break;
39 }
40 case AF_INET6: {
41 const struct sockaddr_in6* addr_in6 =
42 reinterpret_cast<struct sockaddr_in6*>(ai->ai_addr);
43 if (IN6_IS_ADDR_LOOPBACK(&addr_in6->sin6_addr))
44 saw_v6_localhost = true;
45 else
46 return false;
47 break;
48 }
49 default:
50 NOTREACHED();
51 return false;
52 }
53 }
54
55 return saw_v4_localhost != saw_v6_localhost;
56}
57
58} // namespace
59
[email protected]b59ff372009-07-15 22:04:3260HostResolverProc* HostResolverProc::default_proc_ = NULL;
61
62HostResolverProc::HostResolverProc(HostResolverProc* previous) {
[email protected]c4ff4952010-01-08 19:12:4763 SetPreviousProc(previous);
[email protected]b59ff372009-07-15 22:04:3264
65 // Implicitly fall-back to the global default procedure.
66 if (!previous)
[email protected]c4ff4952010-01-08 19:12:4767 SetPreviousProc(default_proc_);
68}
69
[email protected]be1a48b2011-01-20 00:12:1370HostResolverProc::~HostResolverProc() {
71}
72
73int HostResolverProc::ResolveUsingPrevious(
74 const std::string& host,
75 AddressFamily address_family,
76 HostResolverFlags host_resolver_flags,
77 AddressList* addrlist,
78 int* os_error) {
[email protected]90499482013-06-01 00:39:5079 if (previous_proc_.get()) {
80 return previous_proc_->Resolve(
81 host, address_family, host_resolver_flags, addrlist, os_error);
[email protected]be1a48b2011-01-20 00:12:1382 }
83
84 // Final fallback is the system resolver.
[email protected]1ee9afa12013-04-16 14:18:0685 return SystemHostResolverCall(host, address_family, host_resolver_flags,
[email protected]be1a48b2011-01-20 00:12:1386 addrlist, os_error);
87}
88
[email protected]c4ff4952010-01-08 19:12:4789void HostResolverProc::SetPreviousProc(HostResolverProc* proc) {
[email protected]90499482013-06-01 00:39:5090 HostResolverProc* current_previous = previous_proc_.get();
[email protected]c4ff4952010-01-08 19:12:4791 previous_proc_ = NULL;
92 // Now that we've guaranteed |this| is the last proc in a chain, we can
93 // detect potential cycles using GetLastProc().
94 previous_proc_ = (GetLastProc(proc) == this) ? current_previous : proc;
95}
96
97void HostResolverProc::SetLastProc(HostResolverProc* proc) {
98 GetLastProc(this)->SetPreviousProc(proc);
99}
100
101// static
102HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) {
103 if (proc == NULL)
104 return NULL;
105 HostResolverProc* last_proc = proc;
[email protected]90499482013-06-01 00:39:50106 while (last_proc->previous_proc_.get() != NULL)
107 last_proc = last_proc->previous_proc_.get();
[email protected]c4ff4952010-01-08 19:12:47108 return last_proc;
[email protected]b59ff372009-07-15 22:04:32109}
110
111// static
112HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) {
113 HostResolverProc* old = default_proc_;
114 default_proc_ = proc;
115 return old;
116}
117
118// static
119HostResolverProc* HostResolverProc::GetDefault() {
120 return default_proc_;
121}
122
[email protected]1ee9afa12013-04-16 14:18:06123int SystemHostResolverCall(const std::string& host,
[email protected]123ab1e32009-10-21 19:12:57124 AddressFamily address_family,
[email protected]5ea28dea2010-04-08 15:35:13125 HostResolverFlags host_resolver_flags,
[email protected]21526002010-05-16 19:42:46126 AddressList* addrlist,
127 int* os_error) {
Matt Menkeb44ea7f2017-07-20 21:35:37128 // |host| should be a valid domain name. HostResolverImpl::Resolve has checks
129 // to fail early if this is not the case.
130 DCHECK(IsValidDNSDomain(host));
ttuttlec7f5ee52015-03-07 01:44:01131
[email protected]21526002010-05-16 19:42:46132 if (os_error)
133 *os_error = 0;
134
[email protected]b59ff372009-07-15 22:04:32135 struct addrinfo* ai = NULL;
136 struct addrinfo hints = {0};
[email protected]123ab1e32009-10-21 19:12:57137
138 switch (address_family) {
[email protected]dd030332009-10-23 04:35:31139 case ADDRESS_FAMILY_IPV4:
[email protected]123ab1e32009-10-21 19:12:57140 hints.ai_family = AF_INET;
141 break;
[email protected]dd030332009-10-23 04:35:31142 case ADDRESS_FAMILY_IPV6:
143 hints.ai_family = AF_INET6;
144 break;
145 case ADDRESS_FAMILY_UNSPECIFIED:
146 hints.ai_family = AF_UNSPEC;
147 break;
[email protected]123ab1e32009-10-21 19:12:57148 default:
[email protected]dd030332009-10-23 04:35:31149 NOTREACHED();
[email protected]123ab1e32009-10-21 19:12:57150 hints.ai_family = AF_UNSPEC;
151 }
[email protected]b59ff372009-07-15 22:04:32152
[email protected]bae892f92011-10-20 22:39:26153#if defined(OS_WIN)
[email protected]b59ff372009-07-15 22:04:32154 // DO NOT USE AI_ADDRCONFIG ON WINDOWS.
155 //
156 // The following comment in <winsock2.h> is the best documentation I found
157 // on AI_ADDRCONFIG for Windows:
158 // Flags used in "hints" argument to getaddrinfo()
159 // - AI_ADDRCONFIG is supported starting with Vista
160 // - default is AI_ADDRCONFIG ON whether the flag is set or not
161 // because the performance penalty in not having ADDRCONFIG in
162 // the multi-protocol stack environment is severe;
163 // this defaulting may be disabled by specifying the AI_ALL flag,
164 // in that case AI_ADDRCONFIG must be EXPLICITLY specified to
165 // enable ADDRCONFIG behavior
166 //
167 // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the
168 // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo
169 // to fail with WSANO_DATA (11004) for "localhost", probably because of the
170 // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page:
171 // The IPv4 or IPv6 loopback address is not considered a valid global
172 // address.
173 // See http://crbug.com/5234.
[email protected]1a157302010-01-29 03:36:45174 //
175 // OpenBSD does not support it, either.
[email protected]b59ff372009-07-15 22:04:32176 hints.ai_flags = 0;
[email protected]b59ff372009-07-15 22:04:32177#else
178 hints.ai_flags = AI_ADDRCONFIG;
179#endif
180
[email protected]2f3bc65c2010-07-23 17:47:10181 // On Linux AI_ADDRCONFIG doesn't consider loopback addreses, even if only
182 // loopback addresses are configured. So don't use it when there are only
183 // loopback addresses.
184 if (host_resolver_flags & HOST_RESOLVER_LOOPBACK_ONLY)
185 hints.ai_flags &= ~AI_ADDRCONFIG;
186
[email protected]5ea28dea2010-04-08 15:35:13187 if (host_resolver_flags & HOST_RESOLVER_CANONNAME)
188 hints.ai_flags |= AI_CANONNAME;
189
[email protected]b59ff372009-07-15 22:04:32190 // Restrict result set to only this socket type to avoid duplicates.
191 hints.ai_socktype = SOCK_STREAM;
192
[email protected]e1439022011-09-05 23:40:51193#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
Sergey Ulanov49085572017-07-10 23:25:46194 !defined(OS_ANDROID) && !defined(OS_FUCHSIA)
[email protected]46018c9d2011-09-06 03:42:34195 DnsReloaderMaybeReload();
[email protected]e1439022011-09-05 23:40:51196#endif
[email protected]46018c9d2011-09-06 03:42:34197 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
198 bool should_retry = false;
[email protected]eaf3a3b2010-09-03 20:34:27199 // If the lookup was restricted (either by address family, or address
200 // detection), and the results where all localhost of a single family,
201 // maybe we should retry. There were several bugs related to these
202 // issues, for example http://crbug.com/42058 and http://crbug.com/49024
203 if ((hints.ai_family != AF_UNSPEC || hints.ai_flags & AI_ADDRCONFIG) &&
204 err == 0 && IsAllLocalhostOfOneFamily(ai)) {
205 if (host_resolver_flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) {
206 hints.ai_family = AF_UNSPEC;
207 should_retry = true;
208 }
209 if (hints.ai_flags & AI_ADDRCONFIG) {
210 hints.ai_flags &= ~AI_ADDRCONFIG;
211 should_retry = true;
212 }
213 }
214 if (should_retry) {
[email protected]ab6d12232010-09-07 19:39:37215 if (ai != NULL) {
216 freeaddrinfo(ai);
217 ai = NULL;
218 }
[email protected]eaf3a3b2010-09-03 20:34:27219 err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
220 }
[email protected]b59ff372009-07-15 22:04:32221
[email protected]21526002010-05-16 19:42:46222 if (err) {
[email protected]21526002010-05-16 19:42:46223#if defined(OS_WIN)
[email protected]8ed0cab62010-10-15 04:12:45224 err = WSAGetLastError();
[email protected]21526002010-05-16 19:42:46225#endif
[email protected]8ed0cab62010-10-15 04:12:45226
227 // Return the OS error to the caller.
228 if (os_error)
229 *os_error = err;
230
231 // If the call to getaddrinfo() failed because of a system error, report
232 // it separately from ERR_NAME_NOT_RESOLVED.
233#if defined(OS_WIN)
234 if (err != WSAHOST_NOT_FOUND && err != WSANO_DATA)
235 return ERR_NAME_RESOLUTION_FAILED;
[email protected]23f771162011-06-02 18:37:51236#elif defined(OS_POSIX) && !defined(OS_FREEBSD)
[email protected]8ed0cab62010-10-15 04:12:45237 if (err != EAI_NONAME && err != EAI_NODATA)
238 return ERR_NAME_RESOLUTION_FAILED;
239#endif
240
[email protected]b59ff372009-07-15 22:04:32241 return ERR_NAME_NOT_RESOLVED;
[email protected]21526002010-05-16 19:42:46242 }
[email protected]b59ff372009-07-15 22:04:32243
[email protected]c33dc2e2012-06-22 21:36:43244#if defined(OS_ANDROID)
245 // Workaround for Android's getaddrinfo leaving ai==NULL without an error.
246 // http://crbug.com/134142
247 if (ai == NULL)
248 return ERR_NAME_NOT_RESOLVED;
249#endif
250
[email protected]7054e78f2012-05-07 21:44:56251 *addrlist = AddressList::CreateFromAddrinfo(ai);
252 freeaddrinfo(ai);
[email protected]b59ff372009-07-15 22:04:32253 return OK;
254}
255
[email protected]1ee9afa12013-04-16 14:18:06256SystemHostResolverProc::SystemHostResolverProc() : HostResolverProc(NULL) {}
257
258int SystemHostResolverProc::Resolve(const std::string& hostname,
259 AddressFamily address_family,
260 HostResolverFlags host_resolver_flags,
261 AddressList* addr_list,
262 int* os_error) {
263 return SystemHostResolverCall(hostname,
264 address_family,
265 host_resolver_flags,
266 addr_list,
267 os_error);
268}
269
270SystemHostResolverProc::~SystemHostResolverProc() {}
271
[email protected]b59ff372009-07-15 22:04:32272} // namespace net