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