blob: 9ae40208ea69fa880ba536a6bb68c27185e75434 [file] [log] [blame]
[email protected]c4ff4952010-01-08 19:12:471// Copyright (c) 2010 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
5#include "net/base/host_resolver_proc.h"
6
7#include "build/build_config.h"
8
[email protected]1a157302010-01-29 03:36:459#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]b59ff372009-07-15 22:04:3210#include <resolv.h>
11#endif
12
13#include "base/logging.h"
[email protected]b59ff372009-07-15 22:04:3214#include "net/base/address_list.h"
[email protected]37e658b2010-07-28 17:23:0415#include "net/base/dns_reload_timer.h"
[email protected]b59ff372009-07-15 22:04:3216#include "net/base/net_errors.h"
[email protected]a540c2d2009-12-12 00:47:3717#include "net/base/sys_addrinfo.h"
[email protected]b59ff372009-07-15 22:04:3218
[email protected]b59ff372009-07-15 22:04:3219namespace net {
20
21HostResolverProc* HostResolverProc::default_proc_ = NULL;
22
23HostResolverProc::HostResolverProc(HostResolverProc* previous) {
[email protected]c4ff4952010-01-08 19:12:4724 SetPreviousProc(previous);
[email protected]b59ff372009-07-15 22:04:3225
26 // Implicitly fall-back to the global default procedure.
27 if (!previous)
[email protected]c4ff4952010-01-08 19:12:4728 SetPreviousProc(default_proc_);
29}
30
31void HostResolverProc::SetPreviousProc(HostResolverProc* proc) {
32 HostResolverProc* current_previous = previous_proc_;
33 previous_proc_ = NULL;
34 // Now that we've guaranteed |this| is the last proc in a chain, we can
35 // detect potential cycles using GetLastProc().
36 previous_proc_ = (GetLastProc(proc) == this) ? current_previous : proc;
37}
38
39void HostResolverProc::SetLastProc(HostResolverProc* proc) {
40 GetLastProc(this)->SetPreviousProc(proc);
41}
42
43// static
44HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) {
45 if (proc == NULL)
46 return NULL;
47 HostResolverProc* last_proc = proc;
48 while (last_proc->previous_proc_ != NULL)
49 last_proc = last_proc->previous_proc_;
50 return last_proc;
[email protected]b59ff372009-07-15 22:04:3251}
52
53// static
54HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) {
55 HostResolverProc* old = default_proc_;
56 default_proc_ = proc;
57 return old;
58}
59
60// static
61HostResolverProc* HostResolverProc::GetDefault() {
62 return default_proc_;
63}
64
[email protected]5ea28dea2010-04-08 15:35:1365int HostResolverProc::ResolveUsingPrevious(
66 const std::string& host,
67 AddressFamily address_family,
68 HostResolverFlags host_resolver_flags,
[email protected]21526002010-05-16 19:42:4669 AddressList* addrlist,
70 int* os_error) {
71 if (previous_proc_) {
72 return previous_proc_->Resolve(host, address_family, host_resolver_flags,
73 addrlist, os_error);
74 }
[email protected]b59ff372009-07-15 22:04:3275
76 // Final fallback is the system resolver.
[email protected]21526002010-05-16 19:42:4677 return SystemHostResolverProc(host, address_family, host_resolver_flags,
78 addrlist, os_error);
[email protected]b59ff372009-07-15 22:04:3279}
80
[email protected]123ab1e32009-10-21 19:12:5781int SystemHostResolverProc(const std::string& host,
82 AddressFamily address_family,
[email protected]5ea28dea2010-04-08 15:35:1383 HostResolverFlags host_resolver_flags,
[email protected]21526002010-05-16 19:42:4684 AddressList* addrlist,
85 int* os_error) {
[email protected]3eac24a22010-08-07 01:13:2886 static const size_t kMaxHostLength = 4096;
87
[email protected]21526002010-05-16 19:42:4688 if (os_error)
89 *os_error = 0;
90
[email protected]41547862009-08-17 20:47:0291 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
92 // On Windows it gives the default interface's address, whereas on Linux it
93 // gives an error. We will make it fail on all platforms for consistency.
94 if (host.empty())
95 return ERR_NAME_NOT_RESOLVED;
96
[email protected]3eac24a22010-08-07 01:13:2897 // Limit the size of hostnames that will be resolved to combat issues in some
98 // platform's resolvers.
99 if (host.size() > kMaxHostLength)
100 return ERR_NAME_NOT_RESOLVED;
101
[email protected]b59ff372009-07-15 22:04:32102 struct addrinfo* ai = NULL;
103 struct addrinfo hints = {0};
[email protected]123ab1e32009-10-21 19:12:57104
105 switch (address_family) {
[email protected]dd030332009-10-23 04:35:31106 case ADDRESS_FAMILY_IPV4:
[email protected]123ab1e32009-10-21 19:12:57107 hints.ai_family = AF_INET;
108 break;
[email protected]dd030332009-10-23 04:35:31109 case ADDRESS_FAMILY_IPV6:
110 hints.ai_family = AF_INET6;
111 break;
112 case ADDRESS_FAMILY_UNSPECIFIED:
113 hints.ai_family = AF_UNSPEC;
114 break;
[email protected]123ab1e32009-10-21 19:12:57115 default:
[email protected]dd030332009-10-23 04:35:31116 NOTREACHED();
[email protected]123ab1e32009-10-21 19:12:57117 hints.ai_family = AF_UNSPEC;
118 }
[email protected]b59ff372009-07-15 22:04:32119
[email protected]1a157302010-01-29 03:36:45120#if defined(OS_WIN) || defined(OS_OPENBSD)
[email protected]b59ff372009-07-15 22:04:32121 // DO NOT USE AI_ADDRCONFIG ON WINDOWS.
122 //
123 // The following comment in <winsock2.h> is the best documentation I found
124 // on AI_ADDRCONFIG for Windows:
125 // Flags used in "hints" argument to getaddrinfo()
126 // - AI_ADDRCONFIG is supported starting with Vista
127 // - default is AI_ADDRCONFIG ON whether the flag is set or not
128 // because the performance penalty in not having ADDRCONFIG in
129 // the multi-protocol stack environment is severe;
130 // this defaulting may be disabled by specifying the AI_ALL flag,
131 // in that case AI_ADDRCONFIG must be EXPLICITLY specified to
132 // enable ADDRCONFIG behavior
133 //
134 // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the
135 // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo
136 // to fail with WSANO_DATA (11004) for "localhost", probably because of the
137 // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page:
138 // The IPv4 or IPv6 loopback address is not considered a valid global
139 // address.
140 // See http://crbug.com/5234.
[email protected]1a157302010-01-29 03:36:45141 //
142 // OpenBSD does not support it, either.
[email protected]b59ff372009-07-15 22:04:32143 hints.ai_flags = 0;
[email protected]b59ff372009-07-15 22:04:32144#else
145 hints.ai_flags = AI_ADDRCONFIG;
146#endif
147
[email protected]2f3bc65c2010-07-23 17:47:10148 // On Linux AI_ADDRCONFIG doesn't consider loopback addreses, even if only
149 // loopback addresses are configured. So don't use it when there are only
150 // loopback addresses.
151 if (host_resolver_flags & HOST_RESOLVER_LOOPBACK_ONLY)
152 hints.ai_flags &= ~AI_ADDRCONFIG;
153
[email protected]5ea28dea2010-04-08 15:35:13154 if (host_resolver_flags & HOST_RESOLVER_CANONNAME)
155 hints.ai_flags |= AI_CANONNAME;
156
[email protected]b59ff372009-07-15 22:04:32157 // Restrict result set to only this socket type to avoid duplicates.
158 hints.ai_socktype = SOCK_STREAM;
159
[email protected]f820596a2009-11-25 00:15:38160 int err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
[email protected]1a157302010-01-29 03:36:45161#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD)
[email protected]b59ff372009-07-15 22:04:32162 // If we fail, re-initialise the resolver just in case there have been any
163 // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info.
[email protected]37e658b2010-07-28 17:23:04164 if (err && DnsReloadTimerHasExpired()) {
[email protected]b59ff372009-07-15 22:04:32165 res_nclose(&_res);
166 if (!res_ninit(&_res))
167 err = getaddrinfo(host.c_str(), NULL, &hints, &ai);
168 }
169#endif
170
[email protected]21526002010-05-16 19:42:46171 if (err) {
172 if (os_error) {
173#if defined(OS_WIN)
174 *os_error = WSAGetLastError();
175#else
176 *os_error = err;
177#endif
178 }
[email protected]b59ff372009-07-15 22:04:32179 return ERR_NAME_NOT_RESOLVED;
[email protected]21526002010-05-16 19:42:46180 }
[email protected]b59ff372009-07-15 22:04:32181
182 addrlist->Adopt(ai);
183 return OK;
184}
185
[email protected]b59ff372009-07-15 22:04:32186} // namespace net