1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/dns/host_resolver_proc.h"
7 #include "build/build_config.h"
9 #include "base/logging.h"
10 #include "base/sys_byteorder.h"
11 #include "net/base/address_list.h"
12 #include "net/base/dns_reloader.h"
13 #include "net/base/dns_util.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/sys_addrinfo.h"
17 #if defined(OS_OPENBSD)
18 #define AI_ADDRCONFIG 0
25 bool 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
) {
31 const struct sockaddr_in
* addr_in
=
32 reinterpret_cast<struct sockaddr_in
*>(ai
->ai_addr
);
33 if ((base::NetToHost32(addr_in
->sin_addr
.s_addr
) & 0xff000000) ==
35 saw_v4_localhost
= true;
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;
55 return saw_v4_localhost
!= saw_v6_localhost
;
60 HostResolverProc
* HostResolverProc::default_proc_
= NULL
;
62 HostResolverProc::HostResolverProc(HostResolverProc
* previous
) {
63 SetPreviousProc(previous
);
65 // Implicitly fall-back to the global default procedure.
67 SetPreviousProc(default_proc_
);
70 HostResolverProc::~HostResolverProc() {
73 int HostResolverProc::ResolveUsingPrevious(
74 const std::string
& host
,
75 AddressFamily address_family
,
76 HostResolverFlags host_resolver_flags
,
77 AddressList
* addrlist
,
79 if (previous_proc_
.get()) {
80 return previous_proc_
->Resolve(
81 host
, address_family
, host_resolver_flags
, addrlist
, os_error
);
84 // Final fallback is the system resolver.
85 return SystemHostResolverCall(host
, address_family
, host_resolver_flags
,
89 void HostResolverProc::SetPreviousProc(HostResolverProc
* proc
) {
90 HostResolverProc
* current_previous
= previous_proc_
.get();
91 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
;
97 void HostResolverProc::SetLastProc(HostResolverProc
* proc
) {
98 GetLastProc(this)->SetPreviousProc(proc
);
102 HostResolverProc
* HostResolverProc::GetLastProc(HostResolverProc
* proc
) {
105 HostResolverProc
* last_proc
= proc
;
106 while (last_proc
->previous_proc_
.get() != NULL
)
107 last_proc
= last_proc
->previous_proc_
.get();
112 HostResolverProc
* HostResolverProc::SetDefault(HostResolverProc
* proc
) {
113 HostResolverProc
* old
= default_proc_
;
114 default_proc_
= proc
;
119 HostResolverProc
* HostResolverProc::GetDefault() {
120 return default_proc_
;
123 int SystemHostResolverCall(const std::string
& host
,
124 AddressFamily address_family
,
125 HostResolverFlags host_resolver_flags
,
126 AddressList
* addrlist
,
128 // Make sure |host| is properly formed.
130 std::string out_ignored
;
131 if (!DNSDomainFromDot(host
, &out_ignored
))
132 return ERR_NAME_NOT_RESOLVED
;
138 struct addrinfo
* ai
= NULL
;
139 struct addrinfo hints
= {0};
141 switch (address_family
) {
142 case ADDRESS_FAMILY_IPV4
:
143 hints
.ai_family
= AF_INET
;
145 case ADDRESS_FAMILY_IPV6
:
146 hints
.ai_family
= AF_INET6
;
148 case ADDRESS_FAMILY_UNSPECIFIED
:
149 hints
.ai_family
= AF_UNSPEC
;
153 hints
.ai_family
= AF_UNSPEC
;
157 // DO NOT USE AI_ADDRCONFIG ON WINDOWS.
159 // The following comment in <winsock2.h> is the best documentation I found
160 // on AI_ADDRCONFIG for Windows:
161 // Flags used in "hints" argument to getaddrinfo()
162 // - AI_ADDRCONFIG is supported starting with Vista
163 // - default is AI_ADDRCONFIG ON whether the flag is set or not
164 // because the performance penalty in not having ADDRCONFIG in
165 // the multi-protocol stack environment is severe;
166 // this defaulting may be disabled by specifying the AI_ALL flag,
167 // in that case AI_ADDRCONFIG must be EXPLICITLY specified to
168 // enable ADDRCONFIG behavior
170 // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the
171 // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo
172 // to fail with WSANO_DATA (11004) for "localhost", probably because of the
173 // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page:
174 // The IPv4 or IPv6 loopback address is not considered a valid global
176 // See http://crbug.com/5234.
178 // OpenBSD does not support it, either.
181 hints
.ai_flags
= AI_ADDRCONFIG
;
184 // On Linux AI_ADDRCONFIG doesn't consider loopback addreses, even if only
185 // loopback addresses are configured. So don't use it when there are only
186 // loopback addresses.
187 if (host_resolver_flags
& HOST_RESOLVER_LOOPBACK_ONLY
)
188 hints
.ai_flags
&= ~AI_ADDRCONFIG
;
190 if (host_resolver_flags
& HOST_RESOLVER_CANONNAME
)
191 hints
.ai_flags
|= AI_CANONNAME
;
193 // Restrict result set to only this socket type to avoid duplicates.
194 hints
.ai_socktype
= SOCK_STREAM
;
196 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \
198 DnsReloaderMaybeReload();
200 int err
= getaddrinfo(host
.c_str(), NULL
, &hints
, &ai
);
201 bool should_retry
= false;
202 // If the lookup was restricted (either by address family, or address
203 // detection), and the results where all localhost of a single family,
204 // maybe we should retry. There were several bugs related to these
205 // issues, for example http://crbug.com/42058 and http://crbug.com/49024
206 if ((hints
.ai_family
!= AF_UNSPEC
|| hints
.ai_flags
& AI_ADDRCONFIG
) &&
207 err
== 0 && IsAllLocalhostOfOneFamily(ai
)) {
208 if (host_resolver_flags
& HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6
) {
209 hints
.ai_family
= AF_UNSPEC
;
212 if (hints
.ai_flags
& AI_ADDRCONFIG
) {
213 hints
.ai_flags
&= ~AI_ADDRCONFIG
;
222 err
= getaddrinfo(host
.c_str(), NULL
, &hints
, &ai
);
227 err
= WSAGetLastError();
230 // Return the OS error to the caller.
234 // If the call to getaddrinfo() failed because of a system error, report
235 // it separately from ERR_NAME_NOT_RESOLVED.
237 if (err
!= WSAHOST_NOT_FOUND
&& err
!= WSANO_DATA
)
238 return ERR_NAME_RESOLUTION_FAILED
;
239 #elif defined(OS_POSIX) && !defined(OS_FREEBSD)
240 if (err
!= EAI_NONAME
&& err
!= EAI_NODATA
)
241 return ERR_NAME_RESOLUTION_FAILED
;
244 return ERR_NAME_NOT_RESOLVED
;
247 #if defined(OS_ANDROID)
248 // Workaround for Android's getaddrinfo leaving ai==NULL without an error.
249 // http://crbug.com/134142
251 return ERR_NAME_NOT_RESOLVED
;
254 *addrlist
= AddressList::CreateFromAddrinfo(ai
);
259 SystemHostResolverProc::SystemHostResolverProc() : HostResolverProc(NULL
) {}
261 int SystemHostResolverProc::Resolve(const std::string
& hostname
,
262 AddressFamily address_family
,
263 HostResolverFlags host_resolver_flags
,
264 AddressList
* addr_list
,
266 return SystemHostResolverCall(hostname
,
273 SystemHostResolverProc::~SystemHostResolverProc() {}