[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2 | // 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] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 9 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 10 | #include <resolv.h> |
| 11 | #endif |
| 12 | |
| 13 | #include "base/logging.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 14 | #include "net/base/address_list.h" |
[email protected] | 37e658b | 2010-07-28 17:23:04 | [diff] [blame] | 15 | #include "net/base/dns_reload_timer.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 16 | #include "net/base/net_errors.h" |
[email protected] | a540c2d | 2009-12-12 00:47:37 | [diff] [blame] | 17 | #include "net/base/sys_addrinfo.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 18 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 19 | namespace net { |
| 20 | |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | bool IsAllLocalhostOfOneFamily(const struct addrinfo* ai) { |
| 24 | bool saw_v4_localhost = false; |
| 25 | bool saw_v6_localhost = false; |
| 26 | for (; ai != NULL; ai = ai->ai_next) { |
| 27 | switch (ai->ai_family) { |
| 28 | case AF_INET: { |
| 29 | const struct sockaddr_in* addr_in = |
| 30 | reinterpret_cast<struct sockaddr_in*>(ai->ai_addr); |
| 31 | if ((ntohl(addr_in->sin_addr.s_addr) & 0xff000000) == 0x7f000000) |
| 32 | saw_v4_localhost = true; |
| 33 | else |
| 34 | return false; |
| 35 | break; |
| 36 | } |
| 37 | case AF_INET6: { |
| 38 | const struct sockaddr_in6* addr_in6 = |
| 39 | reinterpret_cast<struct sockaddr_in6*>(ai->ai_addr); |
| 40 | if (IN6_IS_ADDR_LOOPBACK(&addr_in6->sin6_addr)) |
| 41 | saw_v6_localhost = true; |
| 42 | else |
| 43 | return false; |
| 44 | break; |
| 45 | } |
| 46 | default: |
| 47 | NOTREACHED(); |
| 48 | return false; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return saw_v4_localhost != saw_v6_localhost; |
| 53 | } |
| 54 | |
| 55 | } // namespace |
| 56 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 57 | HostResolverProc* HostResolverProc::default_proc_ = NULL; |
| 58 | |
| 59 | HostResolverProc::HostResolverProc(HostResolverProc* previous) { |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 60 | SetPreviousProc(previous); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 61 | |
| 62 | // Implicitly fall-back to the global default procedure. |
| 63 | if (!previous) |
[email protected] | c4ff495 | 2010-01-08 19:12:47 | [diff] [blame] | 64 | SetPreviousProc(default_proc_); |
| 65 | } |
| 66 | |
| 67 | void HostResolverProc::SetPreviousProc(HostResolverProc* proc) { |
| 68 | HostResolverProc* current_previous = previous_proc_; |
| 69 | previous_proc_ = NULL; |
| 70 | // Now that we've guaranteed |this| is the last proc in a chain, we can |
| 71 | // detect potential cycles using GetLastProc(). |
| 72 | previous_proc_ = (GetLastProc(proc) == this) ? current_previous : proc; |
| 73 | } |
| 74 | |
| 75 | void HostResolverProc::SetLastProc(HostResolverProc* proc) { |
| 76 | GetLastProc(this)->SetPreviousProc(proc); |
| 77 | } |
| 78 | |
| 79 | // static |
| 80 | HostResolverProc* HostResolverProc::GetLastProc(HostResolverProc* proc) { |
| 81 | if (proc == NULL) |
| 82 | return NULL; |
| 83 | HostResolverProc* last_proc = proc; |
| 84 | while (last_proc->previous_proc_ != NULL) |
| 85 | last_proc = last_proc->previous_proc_; |
| 86 | return last_proc; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // static |
| 90 | HostResolverProc* HostResolverProc::SetDefault(HostResolverProc* proc) { |
| 91 | HostResolverProc* old = default_proc_; |
| 92 | default_proc_ = proc; |
| 93 | return old; |
| 94 | } |
| 95 | |
| 96 | // static |
| 97 | HostResolverProc* HostResolverProc::GetDefault() { |
| 98 | return default_proc_; |
| 99 | } |
| 100 | |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 101 | HostResolverProc::~HostResolverProc() { |
| 102 | } |
| 103 | |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 104 | int HostResolverProc::ResolveUsingPrevious( |
| 105 | const std::string& host, |
| 106 | AddressFamily address_family, |
| 107 | HostResolverFlags host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 108 | AddressList* addrlist, |
| 109 | int* os_error) { |
| 110 | if (previous_proc_) { |
| 111 | return previous_proc_->Resolve(host, address_family, host_resolver_flags, |
| 112 | addrlist, os_error); |
| 113 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 114 | |
| 115 | // Final fallback is the system resolver. |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 116 | return SystemHostResolverProc(host, address_family, host_resolver_flags, |
| 117 | addrlist, os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 118 | } |
| 119 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 120 | int SystemHostResolverProc(const std::string& host, |
| 121 | AddressFamily address_family, |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 122 | HostResolverFlags host_resolver_flags, |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 123 | AddressList* addrlist, |
| 124 | int* os_error) { |
[email protected] | 3eac24a2 | 2010-08-07 01:13:28 | [diff] [blame] | 125 | static const size_t kMaxHostLength = 4096; |
| 126 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 127 | if (os_error) |
| 128 | *os_error = 0; |
| 129 | |
[email protected] | 4154786 | 2009-08-17 20:47:02 | [diff] [blame] | 130 | // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 131 | // On Windows it gives the default interface's address, whereas on Linux it |
| 132 | // gives an error. We will make it fail on all platforms for consistency. |
| 133 | if (host.empty()) |
| 134 | return ERR_NAME_NOT_RESOLVED; |
| 135 | |
[email protected] | 3eac24a2 | 2010-08-07 01:13:28 | [diff] [blame] | 136 | // Limit the size of hostnames that will be resolved to combat issues in some |
| 137 | // platform's resolvers. |
| 138 | if (host.size() > kMaxHostLength) |
| 139 | return ERR_NAME_NOT_RESOLVED; |
| 140 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 141 | struct addrinfo* ai = NULL; |
| 142 | struct addrinfo hints = {0}; |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 143 | |
| 144 | switch (address_family) { |
[email protected] | dd03033 | 2009-10-23 04:35:31 | [diff] [blame] | 145 | case ADDRESS_FAMILY_IPV4: |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 146 | hints.ai_family = AF_INET; |
| 147 | break; |
[email protected] | dd03033 | 2009-10-23 04:35:31 | [diff] [blame] | 148 | case ADDRESS_FAMILY_IPV6: |
| 149 | hints.ai_family = AF_INET6; |
| 150 | break; |
| 151 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 152 | hints.ai_family = AF_UNSPEC; |
| 153 | break; |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 154 | default: |
[email protected] | dd03033 | 2009-10-23 04:35:31 | [diff] [blame] | 155 | NOTREACHED(); |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 156 | hints.ai_family = AF_UNSPEC; |
| 157 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 158 | |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 159 | #if defined(OS_WIN) || defined(OS_OPENBSD) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 160 | // DO NOT USE AI_ADDRCONFIG ON WINDOWS. |
| 161 | // |
| 162 | // The following comment in <winsock2.h> is the best documentation I found |
| 163 | // on AI_ADDRCONFIG for Windows: |
| 164 | // Flags used in "hints" argument to getaddrinfo() |
| 165 | // - AI_ADDRCONFIG is supported starting with Vista |
| 166 | // - default is AI_ADDRCONFIG ON whether the flag is set or not |
| 167 | // because the performance penalty in not having ADDRCONFIG in |
| 168 | // the multi-protocol stack environment is severe; |
| 169 | // this defaulting may be disabled by specifying the AI_ALL flag, |
| 170 | // in that case AI_ADDRCONFIG must be EXPLICITLY specified to |
| 171 | // enable ADDRCONFIG behavior |
| 172 | // |
| 173 | // Not only is AI_ADDRCONFIG unnecessary, but it can be harmful. If the |
| 174 | // computer is not connected to a network, AI_ADDRCONFIG causes getaddrinfo |
| 175 | // to fail with WSANO_DATA (11004) for "localhost", probably because of the |
| 176 | // following note on AI_ADDRCONFIG in the MSDN getaddrinfo page: |
| 177 | // The IPv4 or IPv6 loopback address is not considered a valid global |
| 178 | // address. |
| 179 | // See http://crbug.com/5234. |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 180 | // |
| 181 | // OpenBSD does not support it, either. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 182 | hints.ai_flags = 0; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 183 | #else |
| 184 | hints.ai_flags = AI_ADDRCONFIG; |
| 185 | #endif |
| 186 | |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 187 | // On Linux AI_ADDRCONFIG doesn't consider loopback addreses, even if only |
| 188 | // loopback addresses are configured. So don't use it when there are only |
| 189 | // loopback addresses. |
| 190 | if (host_resolver_flags & HOST_RESOLVER_LOOPBACK_ONLY) |
| 191 | hints.ai_flags &= ~AI_ADDRCONFIG; |
| 192 | |
[email protected] | 5ea28dea | 2010-04-08 15:35:13 | [diff] [blame] | 193 | if (host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 194 | hints.ai_flags |= AI_CANONNAME; |
| 195 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 196 | // Restrict result set to only this socket type to avoid duplicates. |
| 197 | hints.ai_socktype = SOCK_STREAM; |
| 198 | |
[email protected] | f820596a | 2009-11-25 00:15:38 | [diff] [blame] | 199 | int err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 200 | bool should_retry = false; |
[email protected] | 1a15730 | 2010-01-29 03:36:45 | [diff] [blame] | 201 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 202 | // If we fail, re-initialise the resolver just in case there have been any |
| 203 | // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. |
[email protected] | 37e658b | 2010-07-28 17:23:04 | [diff] [blame] | 204 | if (err && DnsReloadTimerHasExpired()) { |
[email protected] | 8ec8c299 | 2010-10-12 16:55:31 | [diff] [blame] | 205 | // When there's no network connection, _res may not be initialized by |
| 206 | // getaddrinfo. Therefore, we call res_nclose only when there are ns |
| 207 | // entries. |
| 208 | if (_res.nscount > 0) |
| 209 | res_nclose(&_res); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 210 | if (!res_ninit(&_res)) |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 211 | should_retry = true; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 212 | } |
| 213 | #endif |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 214 | // If the lookup was restricted (either by address family, or address |
| 215 | // detection), and the results where all localhost of a single family, |
| 216 | // maybe we should retry. There were several bugs related to these |
| 217 | // issues, for example http://crbug.com/42058 and http://crbug.com/49024 |
| 218 | if ((hints.ai_family != AF_UNSPEC || hints.ai_flags & AI_ADDRCONFIG) && |
| 219 | err == 0 && IsAllLocalhostOfOneFamily(ai)) { |
| 220 | if (host_resolver_flags & HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) { |
| 221 | hints.ai_family = AF_UNSPEC; |
| 222 | should_retry = true; |
| 223 | } |
| 224 | if (hints.ai_flags & AI_ADDRCONFIG) { |
| 225 | hints.ai_flags &= ~AI_ADDRCONFIG; |
| 226 | should_retry = true; |
| 227 | } |
| 228 | } |
| 229 | if (should_retry) { |
[email protected] | ab6d1223 | 2010-09-07 19:39:37 | [diff] [blame] | 230 | if (ai != NULL) { |
| 231 | freeaddrinfo(ai); |
| 232 | ai = NULL; |
| 233 | } |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 234 | err = getaddrinfo(host.c_str(), NULL, &hints, &ai); |
| 235 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 236 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 237 | if (err) { |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 238 | #if defined(OS_WIN) |
[email protected] | 8ed0cab6 | 2010-10-15 04:12:45 | [diff] [blame^] | 239 | err = WSAGetLastError(); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 240 | #endif |
[email protected] | 8ed0cab6 | 2010-10-15 04:12:45 | [diff] [blame^] | 241 | |
| 242 | // Return the OS error to the caller. |
| 243 | if (os_error) |
| 244 | *os_error = err; |
| 245 | |
| 246 | // If the call to getaddrinfo() failed because of a system error, report |
| 247 | // it separately from ERR_NAME_NOT_RESOLVED. |
| 248 | #if defined(OS_WIN) |
| 249 | if (err != WSAHOST_NOT_FOUND && err != WSANO_DATA) |
| 250 | return ERR_NAME_RESOLUTION_FAILED; |
| 251 | #elif defined(OS_POSIX) |
| 252 | if (err != EAI_NONAME && err != EAI_NODATA) |
| 253 | return ERR_NAME_RESOLUTION_FAILED; |
| 254 | #endif |
| 255 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 256 | return ERR_NAME_NOT_RESOLVED; |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 257 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 258 | |
| 259 | addrlist->Adopt(ai); |
| 260 | return OK; |
| 261 | } |
| 262 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 263 | } // namespace net |