Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / dns / host_resolver_proc.cc
blob98b98125c1b1e2e71f12508681fa218dc10158d6
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
19 #endif
21 namespace net {
23 namespace {
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) {
30 case AF_INET: {
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) ==
34 0x7f000000)
35 saw_v4_localhost = true;
36 else
37 return false;
38 break;
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;
49 default:
50 NOTREACHED();
51 return false;
55 return saw_v4_localhost != saw_v6_localhost;
58 } // namespace
60 HostResolverProc* HostResolverProc::default_proc_ = NULL;
62 HostResolverProc::HostResolverProc(HostResolverProc* previous) {
63 SetPreviousProc(previous);
65 // Implicitly fall-back to the global default procedure.
66 if (!previous)
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,
78 int* os_error) {
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,
86 addrlist, os_error);
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);
101 // static
102 HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) {
103 if (proc == NULL)
104 return NULL;
105 HostResolverProc* last_proc = proc;
106 while (last_proc->previous_proc_.get() != NULL)
107 last_proc = last_proc->previous_proc_.get();
108 return last_proc;
111 // static
112 HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) {
113 HostResolverProc* old = default_proc_;
114 default_proc_ = proc;
115 return old;
118 // static
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,
127 int* os_error) {
128 // Make sure |host| is properly formed.
130 std::string out_ignored;
131 if (!DNSDomainFromDot(host, &out_ignored))
132 return ERR_NAME_NOT_RESOLVED;
135 if (os_error)
136 *os_error = 0;
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;
144 break;
145 case ADDRESS_FAMILY_IPV6:
146 hints.ai_family = AF_INET6;
147 break;
148 case ADDRESS_FAMILY_UNSPECIFIED:
149 hints.ai_family = AF_UNSPEC;
150 break;
151 default:
152 NOTREACHED();
153 hints.ai_family = AF_UNSPEC;
156 #if defined(OS_WIN)
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
175 // address.
176 // See http://crbug.com/5234.
178 // OpenBSD does not support it, either.
179 hints.ai_flags = 0;
180 #else
181 hints.ai_flags = AI_ADDRCONFIG;
182 #endif
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) && \
197 !defined(OS_ANDROID)
198 DnsReloaderMaybeReload();
199 #endif
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;
210 should_retry = true;
212 if (hints.ai_flags & AI_ADDRCONFIG) {
213 hints.ai_flags &= ~AI_ADDRCONFIG;
214 should_retry = true;
217 if (should_retry) {
218 if (ai != NULL) {
219 freeaddrinfo(ai);
220 ai = NULL;
222 err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
225 if (err) {
226 #if defined(OS_WIN)
227 err = WSAGetLastError();
228 #endif
230 // Return the OS error to the caller.
231 if (os_error)
232 *os_error = err;
234 // If the call to getaddrinfo() failed because of a system error, report
235 // it separately from ERR_NAME_NOT_RESOLVED.
236 #if defined(OS_WIN)
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;
242 #endif
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
250 if (ai == NULL)
251 return ERR_NAME_NOT_RESOLVED;
252 #endif
254 *addrlist = AddressList::CreateFromAddrinfo(ai);
255 freeaddrinfo(ai);
256 return OK;
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,
265 int* os_error) {
266 return SystemHostResolverCall(hostname,
267 address_family,
268 host_resolver_flags,
269 addr_list,
270 os_error);
273 SystemHostResolverProc::~SystemHostResolverProc() {}
275 } // namespace net