[email protected] | a273088 | 2012-01-21 00:56:27 | [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_impl.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 6 | |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 7 | #if defined(OS_WIN) |
| 8 | #include <Winsock2.h> |
| 9 | #elif defined(OS_POSIX) |
| 10 | #include <netdb.h> |
| 11 | #endif |
| 12 | |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 13 | #include <cmath> |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 15 | #include <vector> |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 16 | |
| 17 | #include "base/basictypes.h" |
[email protected] | 33152acc | 2011-10-20 23:37:12 | [diff] [blame] | 18 | #include "base/bind.h" |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 19 | #include "base/bind_helpers.h" |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 20 | #include "base/callback.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 21 | #include "base/compiler_specific.h" |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 22 | #include "base/debug/debugger.h" |
| 23 | #include "base/debug/stack_trace.h" |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 24 | #include "base/metrics/field_trial.h" |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 25 | #include "base/metrics/histogram_macros.h" |
| 26 | #include "base/metrics/sparse_histogram.h" |
eroman | e186724 | 2015-04-23 23:07:44 | [diff] [blame] | 27 | #include "base/profiler/scoped_tracker.h" |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 28 | #include "base/single_thread_task_runner.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 29 | #include "base/stl_util.h" |
[email protected] | be528af | 2013-06-11 07:39:48 | [diff] [blame] | 30 | #include "base/strings/string_util.h" |
[email protected] | 750b2f3c | 2013-06-07 18:41:05 | [diff] [blame] | 31 | #include "base/strings/utf_string_conversions.h" |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 32 | #include "base/thread_task_runner_handle.h" |
[email protected] | ac9ba8fe | 2010-12-30 18:08:36 | [diff] [blame] | 33 | #include "base/threading/worker_pool.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 34 | #include "base/time/time.h" |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 35 | #include "base/values.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 36 | #include "net/base/address_family.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 37 | #include "net/base/address_list.h" |
[email protected] | 46018c9d | 2011-09-06 03:42:34 | [diff] [blame] | 38 | #include "net/base/dns_reloader.h" |
[email protected] | e806cd7 | 2013-05-17 02:08:43 | [diff] [blame] | 39 | #include "net/base/dns_util.h" |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 40 | #include "net/base/host_port_pair.h" |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 41 | #include "net/base/ip_endpoint.h" |
[email protected] | 2bb0444 | 2010-08-18 18:01:15 | [diff] [blame] | 42 | #include "net/base/net_errors.h" |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 43 | #include "net/base/net_util.h" |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 44 | #include "net/dns/address_sorter.h" |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 45 | #include "net/dns/dns_client.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 46 | #include "net/dns/dns_config_service.h" |
| 47 | #include "net/dns/dns_protocol.h" |
| 48 | #include "net/dns/dns_response.h" |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 49 | #include "net/dns/dns_transaction.h" |
[email protected] | f2cb3cf | 2013-03-21 01:40:53 | [diff] [blame] | 50 | #include "net/dns/host_resolver_proc.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 51 | #include "net/log/net_log.h" |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 52 | #include "net/socket/client_socket_factory.h" |
| 53 | #include "net/udp/datagram_client_socket.h" |
pauljensen | 370f1c7 | 2015-02-17 16:59:14 | [diff] [blame] | 54 | #include "url/url_canon_ip.h" |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 55 | |
| 56 | #if defined(OS_WIN) |
| 57 | #include "net/base/winsock_init.h" |
| 58 | #endif |
| 59 | |
| 60 | namespace net { |
| 61 | |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 62 | namespace { |
| 63 | |
[email protected] | 6e78dfb | 2011-07-28 21:34:47 | [diff] [blame] | 64 | // Limit the size of hostnames that will be resolved to combat issues in |
| 65 | // some platform's resolvers. |
| 66 | const size_t kMaxHostLength = 4096; |
| 67 | |
[email protected] | a273088 | 2012-01-21 00:56:27 | [diff] [blame] | 68 | // Default TTL for successful resolutions with ProcTask. |
| 69 | const unsigned kCacheEntryTTLSeconds = 60; |
| 70 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 71 | // Default TTL for unsuccessful resolutions with ProcTask. |
| 72 | const unsigned kNegativeCacheEntryTTLSeconds = 0; |
| 73 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 74 | // Minimum TTL for successful resolutions with DnsTask. |
| 75 | const unsigned kMinimumTTLSeconds = kCacheEntryTTLSeconds; |
| 76 | |
estark | 5d131a1 | 2015-03-26 19:52:19 | [diff] [blame] | 77 | const char kLocalhost[] = "localhost."; |
| 78 | |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 79 | // Time between IPv6 probes, i.e. for how long results of each IPv6 probe are |
| 80 | // cached. |
| 81 | const int kIPv6ProbePeriodMs = 1000; |
| 82 | |
| 83 | // Google DNS address used for IPv6 probes. |
| 84 | const uint8_t kIPv6ProbeAddress[] = |
| 85 | { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, |
| 86 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; |
| 87 | |
[email protected] | 24f4bab | 2010-10-15 01:27:11 | [diff] [blame] | 88 | // We use a separate histogram name for each platform to facilitate the |
| 89 | // display of error codes by their symbolic name (since each platform has |
| 90 | // different mappings). |
| 91 | const char kOSErrorsForGetAddrinfoHistogramName[] = |
| 92 | #if defined(OS_WIN) |
| 93 | "Net.OSErrorsForGetAddrinfo_Win"; |
| 94 | #elif defined(OS_MACOSX) |
| 95 | "Net.OSErrorsForGetAddrinfo_Mac"; |
| 96 | #elif defined(OS_LINUX) |
| 97 | "Net.OSErrorsForGetAddrinfo_Linux"; |
| 98 | #else |
| 99 | "Net.OSErrorsForGetAddrinfo"; |
| 100 | #endif |
| 101 | |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 102 | // Gets a list of the likely error codes that getaddrinfo() can return |
| 103 | // (non-exhaustive). These are the error codes that we will track via |
| 104 | // a histogram. |
| 105 | std::vector<int> GetAllGetAddrinfoOSErrors() { |
| 106 | int os_errors[] = { |
| 107 | #if defined(OS_POSIX) |
[email protected] | 23f77116 | 2011-06-02 18:37:51 | [diff] [blame] | 108 | #if !defined(OS_FREEBSD) |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 109 | #if !defined(OS_ANDROID) |
[email protected] | c48aef9 | 2011-11-22 23:41:45 | [diff] [blame] | 110 | // EAI_ADDRFAMILY has been declared obsolete in Android's and |
| 111 | // FreeBSD's netdb.h. |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 112 | EAI_ADDRFAMILY, |
[email protected] | 3958899 | 2011-07-11 19:54:37 | [diff] [blame] | 113 | #endif |
[email protected] | c48aef9 | 2011-11-22 23:41:45 | [diff] [blame] | 114 | // EAI_NODATA has been declared obsolete in FreeBSD's netdb.h. |
[email protected] | 23f77116 | 2011-06-02 18:37:51 | [diff] [blame] | 115 | EAI_NODATA, |
| 116 | #endif |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 117 | EAI_AGAIN, |
| 118 | EAI_BADFLAGS, |
| 119 | EAI_FAIL, |
| 120 | EAI_FAMILY, |
| 121 | EAI_MEMORY, |
[email protected] | c89b244 | 2011-05-26 14:28:27 | [diff] [blame] | 122 | EAI_NONAME, |
| 123 | EAI_SERVICE, |
| 124 | EAI_SOCKTYPE, |
| 125 | EAI_SYSTEM, |
| 126 | #elif defined(OS_WIN) |
| 127 | // See: http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx |
| 128 | WSA_NOT_ENOUGH_MEMORY, |
| 129 | WSAEAFNOSUPPORT, |
| 130 | WSAEINVAL, |
| 131 | WSAESOCKTNOSUPPORT, |
| 132 | WSAHOST_NOT_FOUND, |
| 133 | WSANO_DATA, |
| 134 | WSANO_RECOVERY, |
| 135 | WSANOTINITIALISED, |
| 136 | WSATRY_AGAIN, |
| 137 | WSATYPE_NOT_FOUND, |
| 138 | // The following are not in doc, but might be to appearing in results :-(. |
| 139 | WSA_INVALID_HANDLE, |
| 140 | #endif |
| 141 | }; |
| 142 | |
| 143 | // Ensure all errors are positive, as histogram only tracks positive values. |
| 144 | for (size_t i = 0; i < arraysize(os_errors); ++i) { |
| 145 | os_errors[i] = std::abs(os_errors[i]); |
| 146 | } |
| 147 | |
| 148 | return base::CustomHistogram::ArrayToCustomRanges(os_errors, |
| 149 | arraysize(os_errors)); |
| 150 | } |
| 151 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 152 | enum DnsResolveStatus { |
| 153 | RESOLVE_STATUS_DNS_SUCCESS = 0, |
| 154 | RESOLVE_STATUS_PROC_SUCCESS, |
| 155 | RESOLVE_STATUS_FAIL, |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 156 | RESOLVE_STATUS_SUSPECT_NETBIOS, |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 157 | RESOLVE_STATUS_MAX |
| 158 | }; |
| 159 | |
eroman | 91dd360 | 2015-03-26 03:46:33 | [diff] [blame] | 160 | // ICANN uses this localhost address to indicate a name collision. |
| 161 | // |
| 162 | // The policy in Chromium is to fail host resolving if it resolves to |
| 163 | // this special address. |
| 164 | // |
| 165 | // Not however that IP literals are exempt from this policy, so it is still |
| 166 | // possible to navigate to http://127.0.53.53/ directly. |
| 167 | // |
| 168 | // For more details: https://www.icann.org/news/announcement-2-2014-08-01-en |
| 169 | const unsigned char kIcanNameCollisionIp[] = {127, 0, 53, 53}; |
| 170 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 171 | void UmaAsyncDnsResolveStatus(DnsResolveStatus result) { |
| 172 | UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ResolveStatus", |
| 173 | result, |
| 174 | RESOLVE_STATUS_MAX); |
| 175 | } |
| 176 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 177 | bool ResemblesNetBIOSName(const std::string& hostname) { |
| 178 | return (hostname.size() < 16) && (hostname.find('.') == std::string::npos); |
| 179 | } |
| 180 | |
| 181 | // True if |hostname| ends with either ".local" or ".local.". |
| 182 | bool ResemblesMulticastDNSName(const std::string& hostname) { |
| 183 | DCHECK(!hostname.empty()); |
| 184 | const char kSuffix[] = ".local."; |
| 185 | const size_t kSuffixLen = sizeof(kSuffix) - 1; |
| 186 | const size_t kSuffixLenTrimmed = kSuffixLen - 1; |
| 187 | if (hostname[hostname.size() - 1] == '.') { |
| 188 | return hostname.size() > kSuffixLen && |
| 189 | !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix); |
| 190 | } |
| 191 | return hostname.size() > kSuffixLenTrimmed && |
| 192 | !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed, |
| 193 | kSuffix, kSuffixLenTrimmed); |
| 194 | } |
| 195 | |
[email protected] | 34e6136 | 2013-07-24 20:41:56 | [diff] [blame] | 196 | // Attempts to connect a UDP socket to |dest|:53. |
[email protected] | 2b74a2f | 2013-07-23 19:37:38 | [diff] [blame] | 197 | bool IsGloballyReachable(const IPAddressNumber& dest, |
| 198 | const BoundNetLog& net_log) { |
eroman | 0eb394e | 2015-04-30 23:25:46 | [diff] [blame] | 199 | // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed. |
| 200 | tracked_objects::ScopedTracker tracking_profile_1( |
eroman | 2734a9d | 2015-05-05 19:24:38 | [diff] [blame] | 201 | FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable")); |
eroman | 0eb394e | 2015-04-30 23:25:46 | [diff] [blame] | 202 | |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 203 | scoped_ptr<DatagramClientSocket> socket( |
| 204 | ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( |
| 205 | DatagramSocket::DEFAULT_BIND, |
| 206 | RandIntCallback(), |
[email protected] | 2b74a2f | 2013-07-23 19:37:38 | [diff] [blame] | 207 | net_log.net_log(), |
| 208 | net_log.source())); |
[email protected] | 34e6136 | 2013-07-24 20:41:56 | [diff] [blame] | 209 | int rv = socket->Connect(IPEndPoint(dest, 53)); |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 210 | if (rv != OK) |
| 211 | return false; |
| 212 | IPEndPoint endpoint; |
| 213 | rv = socket->GetLocalAddress(&endpoint); |
| 214 | if (rv != OK) |
| 215 | return false; |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 216 | DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily()); |
[email protected] | e905172 | 2013-04-12 21:58:18 | [diff] [blame] | 217 | const IPAddressNumber& address = endpoint.address(); |
| 218 | bool is_link_local = (address[0] == 0xFE) && ((address[1] & 0xC0) == 0x80); |
| 219 | if (is_link_local) |
| 220 | return false; |
| 221 | const uint8 kTeredoPrefix[] = { 0x20, 0x01, 0, 0 }; |
| 222 | bool is_teredo = std::equal(kTeredoPrefix, |
| 223 | kTeredoPrefix + arraysize(kTeredoPrefix), |
| 224 | address.begin()); |
| 225 | if (is_teredo) |
| 226 | return false; |
| 227 | return true; |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 230 | // Provide a common macro to simplify code and readability. We must use a |
| 231 | // macro as the underlying HISTOGRAM macro creates static variables. |
| 232 | #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ |
| 233 | base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100) |
| 234 | |
| 235 | // A macro to simplify code and readability. |
| 236 | #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \ |
| 237 | do { \ |
| 238 | switch (priority) { \ |
| 239 | case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \ |
| 240 | case MEDIUM: DNS_HISTOGRAM(basename "_MEDIUM", time); break; \ |
| 241 | case LOW: DNS_HISTOGRAM(basename "_LOW", time); break; \ |
| 242 | case LOWEST: DNS_HISTOGRAM(basename "_LOWEST", time); break; \ |
| 243 | case IDLE: DNS_HISTOGRAM(basename "_IDLE", time); break; \ |
| 244 | default: NOTREACHED(); break; \ |
| 245 | } \ |
| 246 | DNS_HISTOGRAM(basename, time); \ |
| 247 | } while (0) |
| 248 | |
| 249 | // Record time from Request creation until a valid DNS response. |
| 250 | void RecordTotalTime(bool had_dns_config, |
| 251 | bool speculative, |
| 252 | base::TimeDelta duration) { |
| 253 | if (had_dns_config) { |
| 254 | if (speculative) { |
| 255 | DNS_HISTOGRAM("AsyncDNS.TotalTime_speculative", duration); |
| 256 | } else { |
| 257 | DNS_HISTOGRAM("AsyncDNS.TotalTime", duration); |
| 258 | } |
| 259 | } else { |
| 260 | if (speculative) { |
| 261 | DNS_HISTOGRAM("DNS.TotalTime_speculative", duration); |
| 262 | } else { |
| 263 | DNS_HISTOGRAM("DNS.TotalTime", duration); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 268 | void RecordTTL(base::TimeDelta ttl) { |
| 269 | UMA_HISTOGRAM_CUSTOM_TIMES("AsyncDNS.TTL", ttl, |
| 270 | base::TimeDelta::FromSeconds(1), |
| 271 | base::TimeDelta::FromDays(1), 100); |
| 272 | } |
| 273 | |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 274 | bool ConfigureAsyncDnsNoFallbackFieldTrial() { |
| 275 | const bool kDefault = false; |
| 276 | |
| 277 | // Configure the AsyncDns field trial as follows: |
| 278 | // groups AsyncDnsNoFallbackA and AsyncDnsNoFallbackB: return true, |
| 279 | // groups AsyncDnsA and AsyncDnsB: return false, |
| 280 | // groups SystemDnsA and SystemDnsB: return false, |
| 281 | // otherwise (trial absent): return default. |
| 282 | std::string group_name = base::FieldTrialList::FindFullName("AsyncDns"); |
| 283 | if (!group_name.empty()) |
| 284 | return StartsWithASCII(group_name, "AsyncDnsNoFallback", false); |
| 285 | return kDefault; |
| 286 | } |
| 287 | |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 288 | //----------------------------------------------------------------------------- |
| 289 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 290 | AddressList EnsurePortOnAddressList(const AddressList& list, uint16 port) { |
| 291 | if (list.empty() || list.front().port() == port) |
| 292 | return list; |
| 293 | return AddressList::CopyWithPort(list, port); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 296 | // Returns true if |addresses| contains only IPv4 loopback addresses. |
| 297 | bool IsAllIPv4Loopback(const AddressList& addresses) { |
| 298 | for (unsigned i = 0; i < addresses.size(); ++i) { |
| 299 | const IPAddressNumber& address = addresses[i].address(); |
| 300 | switch (addresses[i].GetFamily()) { |
| 301 | case ADDRESS_FAMILY_IPV4: |
| 302 | if (address[0] != 127) |
| 303 | return false; |
| 304 | break; |
| 305 | case ADDRESS_FAMILY_IPV6: |
| 306 | return false; |
| 307 | default: |
| 308 | NOTREACHED(); |
| 309 | return false; |
| 310 | } |
| 311 | } |
| 312 | return true; |
| 313 | } |
| 314 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 315 | // Creates NetLog parameters when the resolve failed. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 316 | scoped_ptr<base::Value> NetLogProcTaskFailedCallback( |
eroman | 001c374 | 2015-04-23 03:11:17 | [diff] [blame] | 317 | uint32 attempt_number, |
| 318 | int net_error, |
| 319 | int os_error, |
| 320 | NetLogCaptureMode /* capture_mode */) { |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 321 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 322 | if (attempt_number) |
| 323 | dict->SetInteger("attempt_number", attempt_number); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 324 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 325 | dict->SetInteger("net_error", net_error); |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 326 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 327 | if (os_error) { |
| 328 | dict->SetInteger("os_error", os_error); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 329 | #if defined(OS_POSIX) |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 330 | dict->SetString("os_error_string", gai_strerror(os_error)); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 331 | #elif defined(OS_WIN) |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 332 | // Map the error code to a human-readable string. |
| 333 | LPWSTR error_string = NULL; |
Peter Kasting | be940e9 | 2014-11-20 23:14:08 | [diff] [blame] | 334 | FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 335 | 0, // Use the internal message table. |
| 336 | os_error, |
| 337 | 0, // Use default language. |
| 338 | (LPWSTR)&error_string, |
| 339 | 0, // Buffer size. |
| 340 | 0); // Arguments (unused). |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 341 | dict->SetString("os_error_string", base::WideToUTF8(error_string)); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 342 | LocalFree(error_string); |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 343 | #endif |
[email protected] | 2152600 | 2010-05-16 19:42:46 | [diff] [blame] | 344 | } |
| 345 | |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 346 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 347 | } |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 348 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 349 | // Creates NetLog parameters when the DnsTask failed. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 350 | scoped_ptr<base::Value> NetLogDnsTaskFailedCallback( |
| 351 | int net_error, |
| 352 | int dns_error, |
| 353 | NetLogCaptureMode /* capture_mode */) { |
| 354 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 355 | dict->SetInteger("net_error", net_error); |
| 356 | if (dns_error) |
| 357 | dict->SetInteger("dns_error", dns_error); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 358 | return dict.Pass(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 359 | }; |
| 360 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 361 | // Creates NetLog parameters containing the information in a RequestInfo object, |
| 362 | // along with the associated NetLog::Source. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 363 | scoped_ptr<base::Value> NetLogRequestInfoCallback( |
| 364 | const HostResolver::RequestInfo* info, |
| 365 | NetLogCaptureMode /* capture_mode */) { |
| 366 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 367 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 368 | dict->SetString("host", info->host_port_pair().ToString()); |
| 369 | dict->SetInteger("address_family", |
| 370 | static_cast<int>(info->address_family())); |
| 371 | dict->SetBoolean("allow_cached_response", info->allow_cached_response()); |
| 372 | dict->SetBoolean("is_speculative", info->is_speculative()); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 373 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 374 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 375 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 376 | // Creates NetLog parameters for the creation of a HostResolverImpl::Job. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 377 | scoped_ptr<base::Value> NetLogJobCreationCallback( |
| 378 | const NetLog::Source& source, |
| 379 | const std::string* host, |
| 380 | NetLogCaptureMode /* capture_mode */) { |
| 381 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 382 | source.AddToEventParameters(dict.get()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 383 | dict->SetString("host", *host); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 384 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 385 | } |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 386 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 387 | // Creates NetLog parameters for HOST_RESOLVER_IMPL_JOB_ATTACH/DETACH events. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 388 | scoped_ptr<base::Value> NetLogJobAttachCallback( |
| 389 | const NetLog::Source& source, |
| 390 | RequestPriority priority, |
| 391 | NetLogCaptureMode /* capture_mode */) { |
| 392 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 393 | source.AddToEventParameters(dict.get()); |
[email protected] | 3b04d1f2 | 2013-10-16 00:23:56 | [diff] [blame] | 394 | dict->SetString("priority", RequestPriorityToString(priority)); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 395 | return dict.Pass(); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 396 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 397 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 398 | // Creates NetLog parameters for the DNS_CONFIG_CHANGED event. |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 399 | scoped_ptr<base::Value> NetLogDnsConfigCallback( |
| 400 | const DnsConfig* config, |
| 401 | NetLogCaptureMode /* capture_mode */) { |
| 402 | return make_scoped_ptr(config->ToValue()); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 403 | } |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 404 | |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 405 | scoped_ptr<base::Value> NetLogIPv6AvailableCallback( |
| 406 | bool ipv6_available, |
| 407 | bool cached, |
| 408 | NetLogCaptureMode /* capture_mode */) { |
| 409 | scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 410 | dict->SetBoolean("ipv6_available", ipv6_available); |
| 411 | dict->SetBoolean("cached", cached); |
estade | 5e5529d | 2015-05-21 20:59:11 | [diff] [blame] | 412 | return dict.Pass(); |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 413 | } |
| 414 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 415 | // The logging routines are defined here because some requests are resolved |
| 416 | // without a Request object. |
| 417 | |
| 418 | // Logs when a request has just been started. |
| 419 | void LogStartRequest(const BoundNetLog& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 420 | const HostResolver::RequestInfo& info) { |
| 421 | source_net_log.BeginEvent( |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 422 | NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 423 | base::Bind(&NetLogRequestInfoCallback, &info)); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Logs when a request has just completed (before its callback is run). |
| 427 | void LogFinishRequest(const BoundNetLog& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 428 | const HostResolver::RequestInfo& info, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 429 | int net_error) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 430 | source_net_log.EndEventWithNetErrorCode( |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 431 | NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, net_error); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // Logs when a request has been cancelled. |
| 435 | void LogCancelRequest(const BoundNetLog& source_net_log, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 436 | const HostResolverImpl::RequestInfo& info) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 437 | source_net_log.AddEvent(NetLog::TYPE_CANCELLED); |
| 438 | source_net_log.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 441 | //----------------------------------------------------------------------------- |
| 442 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 443 | // Keeps track of the highest priority. |
| 444 | class PriorityTracker { |
| 445 | public: |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 446 | explicit PriorityTracker(RequestPriority initial_priority) |
| 447 | : highest_priority_(initial_priority), total_count_(0) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 448 | memset(counts_, 0, sizeof(counts_)); |
| 449 | } |
| 450 | |
| 451 | RequestPriority highest_priority() const { |
| 452 | return highest_priority_; |
| 453 | } |
| 454 | |
| 455 | size_t total_count() const { |
| 456 | return total_count_; |
| 457 | } |
| 458 | |
| 459 | void Add(RequestPriority req_priority) { |
| 460 | ++total_count_; |
| 461 | ++counts_[req_priority]; |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 462 | if (highest_priority_ < req_priority) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 463 | highest_priority_ = req_priority; |
| 464 | } |
| 465 | |
| 466 | void Remove(RequestPriority req_priority) { |
| 467 | DCHECK_GT(total_count_, 0u); |
| 468 | DCHECK_GT(counts_[req_priority], 0u); |
| 469 | --total_count_; |
| 470 | --counts_[req_priority]; |
| 471 | size_t i; |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 472 | for (i = highest_priority_; i > MINIMUM_PRIORITY && !counts_[i]; --i); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 473 | highest_priority_ = static_cast<RequestPriority>(i); |
| 474 | |
[email protected] | 31ae7ab | 2012-04-24 21:09:05 | [diff] [blame] | 475 | // In absence of requests, default to MINIMUM_PRIORITY. |
| 476 | if (total_count_ == 0) |
| 477 | DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | private: |
| 481 | RequestPriority highest_priority_; |
| 482 | size_t total_count_; |
| 483 | size_t counts_[NUM_PRIORITIES]; |
| 484 | }; |
| 485 | |
[email protected] | c54a891 | 2012-10-22 22:09:43 | [diff] [blame] | 486 | } // namespace |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 487 | |
| 488 | //----------------------------------------------------------------------------- |
| 489 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 490 | const unsigned HostResolverImpl::kMaximumDnsFailures = 16; |
| 491 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 492 | // Holds the data for a request that could not be completed synchronously. |
| 493 | // It is owned by a Job. Canceled Requests are only marked as canceled rather |
| 494 | // than removed from the Job's |requests_| list. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 495 | class HostResolverImpl::Request { |
| 496 | public: |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 497 | Request(const BoundNetLog& source_net_log, |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 498 | const RequestInfo& info, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 499 | RequestPriority priority, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 500 | const CompletionCallback& callback, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 501 | AddressList* addresses) |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 502 | : source_net_log_(source_net_log), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 503 | info_(info), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 504 | priority_(priority), |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 505 | job_(NULL), |
| 506 | callback_(callback), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 507 | addresses_(addresses), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 508 | request_time_(base::TimeTicks::Now()) {} |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 509 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 510 | // Mark the request as canceled. |
| 511 | void MarkAsCanceled() { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 512 | job_ = NULL; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 513 | addresses_ = NULL; |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 514 | callback_.Reset(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 515 | } |
| 516 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 517 | bool was_canceled() const { |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 518 | return callback_.is_null(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | void set_job(Job* job) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 522 | DCHECK(job); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 523 | // Identify which job the request is waiting on. |
| 524 | job_ = job; |
| 525 | } |
| 526 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 527 | // Prepare final AddressList and call completion callback. |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 528 | void OnComplete(int error, const AddressList& addr_list) { |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 529 | DCHECK(!was_canceled()); |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 530 | if (error == OK) |
| 531 | *addresses_ = EnsurePortOnAddressList(addr_list, info_.port()); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 532 | CompletionCallback callback = callback_; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 533 | MarkAsCanceled(); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 534 | callback.Run(error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 535 | } |
| 536 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 537 | Job* job() const { |
| 538 | return job_; |
| 539 | } |
| 540 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 541 | // NetLog for the source, passed in HostResolver::Resolve. |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 542 | const BoundNetLog& source_net_log() { |
| 543 | return source_net_log_; |
| 544 | } |
| 545 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 546 | const RequestInfo& info() const { |
| 547 | return info_; |
| 548 | } |
| 549 | |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 550 | RequestPriority priority() const { return priority_; } |
| 551 | |
| 552 | base::TimeTicks request_time() const { return request_time_; } |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 553 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 554 | private: |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 555 | const BoundNetLog source_net_log_; |
[email protected] | 54e1377 | 2009-08-14 03:01:09 | [diff] [blame] | 556 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 557 | // The request info that started the request. |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 558 | const RequestInfo info_; |
| 559 | |
| 560 | // TODO(akalin): Support reprioritization. |
| 561 | const RequestPriority priority_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 562 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 563 | // The resolve job that this request is dependent on. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 564 | Job* job_; |
| 565 | |
| 566 | // The user's callback to invoke when the request completes. |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 567 | CompletionCallback callback_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 568 | |
| 569 | // The address list to save result into. |
| 570 | AddressList* addresses_; |
| 571 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 572 | const base::TimeTicks request_time_; |
| 573 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 574 | DISALLOW_COPY_AND_ASSIGN(Request); |
| 575 | }; |
| 576 | |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 577 | //------------------------------------------------------------------------------ |
| 578 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 579 | // Calls HostResolverProc on the WorkerPool. Performs retries if necessary. |
| 580 | // |
| 581 | // Whenever we try to resolve the host, we post a delayed task to check if host |
| 582 | // resolution (OnLookupComplete) is completed or not. If the original attempt |
| 583 | // hasn't completed, then we start another attempt for host resolution. We take |
| 584 | // the results from the first attempt that finishes and ignore the results from |
| 585 | // all other attempts. |
| 586 | // |
| 587 | // TODO(szym): Move to separate source file for testing and mocking. |
| 588 | // |
| 589 | class HostResolverImpl::ProcTask |
| 590 | : public base::RefCountedThreadSafe<HostResolverImpl::ProcTask> { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 591 | public: |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 592 | typedef base::Callback<void(int net_error, |
| 593 | const AddressList& addr_list)> Callback; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 594 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 595 | ProcTask(const Key& key, |
| 596 | const ProcTaskParams& params, |
| 597 | const Callback& callback, |
| 598 | const BoundNetLog& job_net_log) |
| 599 | : key_(key), |
| 600 | params_(params), |
| 601 | callback_(callback), |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 602 | task_runner_(base::ThreadTaskRunnerHandle::Get()), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 603 | attempt_number_(0), |
| 604 | completed_attempt_number_(0), |
| 605 | completed_attempt_error_(ERR_UNEXPECTED), |
| 606 | had_non_speculative_request_(false), |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 607 | net_log_(job_net_log) { |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 608 | if (!params_.resolver_proc.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 609 | params_.resolver_proc = HostResolverProc::GetDefault(); |
| 610 | // If default is unset, use the system proc. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 611 | if (!params_.resolver_proc.get()) |
[email protected] | 1ee9afa1 | 2013-04-16 14:18:06 | [diff] [blame] | 612 | params_.resolver_proc = new SystemHostResolverProc(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 615 | void Start() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 616 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 617 | net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 618 | StartLookupAttempt(); |
| 619 | } |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 620 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 621 | // Cancels this ProcTask. It will be orphaned. Any outstanding resolve |
| 622 | // attempts running on worker threads will continue running. Only once all the |
| 623 | // attempts complete will the final reference to this ProcTask be released. |
| 624 | void Cancel() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 625 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 626 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 627 | if (was_canceled() || was_completed()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 628 | return; |
| 629 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 630 | callback_.Reset(); |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 631 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | void set_had_non_speculative_request() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 635 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 636 | had_non_speculative_request_ = true; |
| 637 | } |
| 638 | |
| 639 | bool was_canceled() const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 640 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 641 | return callback_.is_null(); |
| 642 | } |
| 643 | |
| 644 | bool was_completed() const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 645 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 646 | return completed_attempt_number_ > 0; |
| 647 | } |
| 648 | |
| 649 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 650 | friend class base::RefCountedThreadSafe<ProcTask>; |
| 651 | ~ProcTask() {} |
| 652 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 653 | void StartLookupAttempt() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 654 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 655 | base::TimeTicks start_time = base::TimeTicks::Now(); |
| 656 | ++attempt_number_; |
| 657 | // Dispatch the lookup attempt to a worker thread. |
| 658 | if (!base::WorkerPool::PostTask( |
| 659 | FROM_HERE, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 660 | base::Bind(&ProcTask::DoLookup, this, start_time, attempt_number_), |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 661 | true)) { |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 662 | NOTREACHED(); |
| 663 | |
| 664 | // Since we could be running within Resolve() right now, we can't just |
| 665 | // call OnLookupComplete(). Instead we must wait until Resolve() has |
| 666 | // returned (IO_PENDING). |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 667 | task_runner_->PostTask(FROM_HERE, |
| 668 | base::Bind(&ProcTask::OnLookupComplete, |
| 669 | this, |
| 670 | AddressList(), |
| 671 | start_time, |
| 672 | attempt_number_, |
| 673 | ERR_UNEXPECTED, |
| 674 | 0)); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 675 | return; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 676 | } |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 677 | |
| 678 | net_log_.AddEvent( |
| 679 | NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_STARTED, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 680 | NetLog::IntegerCallback("attempt_number", attempt_number_)); |
[email protected] | 1302488 | 2011-05-18 23:19:16 | [diff] [blame] | 681 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 682 | // If we don't get the results within a given time, RetryIfNotComplete |
| 683 | // will start a new attempt on a different worker thread if none of our |
| 684 | // outstanding attempts have completed yet. |
| 685 | if (attempt_number_ <= params_.max_retry_attempts) { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 686 | task_runner_->PostDelayedTask( |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 687 | FROM_HERE, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 688 | base::Bind(&ProcTask::RetryIfNotComplete, this), |
[email protected] | 7e56010 | 2012-03-08 20:58:42 | [diff] [blame] | 689 | params_.unresponsive_delay); |
[email protected] | 06ef6d9 | 2011-05-19 04:24:58 | [diff] [blame] | 690 | } |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 691 | } |
| 692 | |
[email protected] | 6c710ee | 2010-05-07 07:51:16 | [diff] [blame] | 693 | // WARNING: This code runs inside a worker pool. The shutdown code cannot |
| 694 | // wait for it to finish, so we must be very careful here about using other |
| 695 | // objects (like MessageLoops, Singletons, etc). During shutdown these objects |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 696 | // may no longer exist. Multiple DoLookups() could be running in parallel, so |
| 697 | // any state inside of |this| must not mutate . |
| 698 | void DoLookup(const base::TimeTicks& start_time, |
| 699 | const uint32 attempt_number) { |
| 700 | AddressList results; |
| 701 | int os_error = 0; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 702 | // Running on the worker thread |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 703 | int error = params_.resolver_proc->Resolve(key_.hostname, |
| 704 | key_.address_family, |
| 705 | key_.host_resolver_flags, |
| 706 | &results, |
| 707 | &os_error); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 708 | |
eroman | 91dd360 | 2015-03-26 03:46:33 | [diff] [blame] | 709 | // Fail the resolution if the result contains 127.0.53.53. See the comment |
| 710 | // block of kIcanNameCollisionIp for details on why. |
| 711 | for (const auto& it : results) { |
| 712 | const IPAddressNumber& cur = it.address(); |
| 713 | if (cur.size() == arraysize(kIcanNameCollisionIp) && |
| 714 | 0 == memcmp(&cur.front(), kIcanNameCollisionIp, cur.size())) { |
| 715 | error = ERR_ICANN_NAME_COLLISION; |
| 716 | break; |
| 717 | } |
| 718 | } |
| 719 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 720 | task_runner_->PostTask(FROM_HERE, |
| 721 | base::Bind(&ProcTask::OnLookupComplete, |
| 722 | this, |
| 723 | results, |
| 724 | start_time, |
| 725 | attempt_number, |
| 726 | error, |
| 727 | os_error)); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 728 | } |
| 729 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 730 | // Makes next attempt if DoLookup() has not finished (runs on task runner |
| 731 | // thread). |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 732 | void RetryIfNotComplete() { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 733 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 734 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 735 | if (was_completed() || was_canceled()) |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 736 | return; |
| 737 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 738 | params_.unresponsive_delay *= params_.retry_factor; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 739 | StartLookupAttempt(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 740 | } |
| 741 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 742 | // Callback for when DoLookup() completes (runs on task runner thread). |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 743 | void OnLookupComplete(const AddressList& results, |
| 744 | const base::TimeTicks& start_time, |
| 745 | const uint32 attempt_number, |
| 746 | int error, |
| 747 | const int os_error) { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 748 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 49b70b22 | 2013-05-07 21:24:23 | [diff] [blame] | 749 | // If results are empty, we should return an error. |
| 750 | bool empty_list_on_ok = (error == OK && results.empty()); |
| 751 | UMA_HISTOGRAM_BOOLEAN("DNS.EmptyAddressListAndNoError", empty_list_on_ok); |
| 752 | if (empty_list_on_ok) |
| 753 | error = ERR_NAME_NOT_RESOLVED; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 754 | |
| 755 | bool was_retry_attempt = attempt_number > 1; |
| 756 | |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 757 | // Ideally the following code would be part of host_resolver_proc.cc, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 758 | // however it isn't safe to call NetworkChangeNotifier from worker threads. |
| 759 | // So we do it here on the IO thread instead. |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 760 | if (error != OK && NetworkChangeNotifier::IsOffline()) |
| 761 | error = ERR_INTERNET_DISCONNECTED; |
[email protected] | 2d3b776 | 2010-10-09 00:35:47 | [diff] [blame] | 762 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 763 | // If this is the first attempt that is finishing later, then record data |
| 764 | // for the first attempt. Won't contaminate with retry attempt's data. |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 765 | if (!was_retry_attempt) |
| 766 | RecordPerformanceHistograms(start_time, error, os_error); |
| 767 | |
| 768 | RecordAttemptHistograms(start_time, attempt_number, error, os_error); |
[email protected] | f2d8c421 | 2010-02-02 00:56:35 | [diff] [blame] | 769 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 770 | if (was_canceled()) |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 771 | return; |
| 772 | |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 773 | NetLog::ParametersCallback net_log_callback; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 774 | if (error != OK) { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 775 | net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, |
| 776 | attempt_number, |
| 777 | error, |
| 778 | os_error); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 779 | } else { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 780 | net_log_callback = NetLog::IntegerCallback("attempt_number", |
| 781 | attempt_number); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 782 | } |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 783 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_ATTEMPT_FINISHED, |
| 784 | net_log_callback); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 785 | |
| 786 | if (was_completed()) |
| 787 | return; |
| 788 | |
| 789 | // Copy the results from the first worker thread that resolves the host. |
| 790 | results_ = results; |
| 791 | completed_attempt_number_ = attempt_number; |
| 792 | completed_attempt_error_ = error; |
| 793 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 794 | if (was_retry_attempt) { |
| 795 | // If retry attempt finishes before 1st attempt, then get stats on how |
| 796 | // much time is saved by having spawned an extra attempt. |
| 797 | retry_attempt_finished_time_ = base::TimeTicks::Now(); |
| 798 | } |
| 799 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 800 | if (error != OK) { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 801 | net_log_callback = base::Bind(&NetLogProcTaskFailedCallback, |
| 802 | 0, error, os_error); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 803 | } else { |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 804 | net_log_callback = results_.CreateNetLogCallback(); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 805 | } |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 806 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_PROC_TASK, |
| 807 | net_log_callback); |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 808 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 809 | callback_.Run(error, results_); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 810 | } |
| 811 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 812 | void RecordPerformanceHistograms(const base::TimeTicks& start_time, |
| 813 | const int error, |
| 814 | const int os_error) const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 815 | DCHECK(task_runner_->BelongsToCurrentThread()); |
asvitkine | c0fb802 | 2014-08-26 04:39:35 | [diff] [blame] | 816 | enum Category { // Used in UMA_HISTOGRAM_ENUMERATION. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 817 | RESOLVE_SUCCESS, |
| 818 | RESOLVE_FAIL, |
| 819 | RESOLVE_SPECULATIVE_SUCCESS, |
| 820 | RESOLVE_SPECULATIVE_FAIL, |
| 821 | RESOLVE_MAX, // Bounding value. |
| 822 | }; |
| 823 | int category = RESOLVE_MAX; // Illegal value for later DCHECK only. |
| 824 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 825 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
| 826 | if (error == OK) { |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 827 | if (had_non_speculative_request_) { |
| 828 | category = RESOLVE_SUCCESS; |
| 829 | DNS_HISTOGRAM("DNS.ResolveSuccess", duration); |
| 830 | } else { |
| 831 | category = RESOLVE_SPECULATIVE_SUCCESS; |
| 832 | DNS_HISTOGRAM("DNS.ResolveSpeculativeSuccess", duration); |
| 833 | } |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 834 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 835 | // Log DNS lookups based on |address_family|. This will help us determine |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 836 | // if IPv4 or IPv4/6 lookups are faster or slower. |
| 837 | switch(key_.address_family) { |
| 838 | case ADDRESS_FAMILY_IPV4: |
| 839 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV4", duration); |
| 840 | break; |
| 841 | case ADDRESS_FAMILY_IPV6: |
| 842 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_IPV6", duration); |
| 843 | break; |
| 844 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 845 | DNS_HISTOGRAM("DNS.ResolveSuccess_FAMILY_UNSPEC", duration); |
| 846 | break; |
| 847 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 848 | } else { |
| 849 | if (had_non_speculative_request_) { |
| 850 | category = RESOLVE_FAIL; |
| 851 | DNS_HISTOGRAM("DNS.ResolveFail", duration); |
| 852 | } else { |
| 853 | category = RESOLVE_SPECULATIVE_FAIL; |
| 854 | DNS_HISTOGRAM("DNS.ResolveSpeculativeFail", duration); |
| 855 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 856 | // Log DNS lookups based on |address_family|. This will help us determine |
[email protected] | 7e96d79 | 2011-06-10 17:08:23 | [diff] [blame] | 857 | // if IPv4 or IPv4/6 lookups are faster or slower. |
| 858 | switch(key_.address_family) { |
| 859 | case ADDRESS_FAMILY_IPV4: |
| 860 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV4", duration); |
| 861 | break; |
| 862 | case ADDRESS_FAMILY_IPV6: |
| 863 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_IPV6", duration); |
| 864 | break; |
| 865 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 866 | DNS_HISTOGRAM("DNS.ResolveFail_FAMILY_UNSPEC", duration); |
| 867 | break; |
| 868 | } |
[email protected] | c833e32 | 2010-10-16 23:51:36 | [diff] [blame] | 869 | UMA_HISTOGRAM_CUSTOM_ENUMERATION(kOSErrorsForGetAddrinfoHistogramName, |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 870 | std::abs(os_error), |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 871 | GetAllGetAddrinfoOSErrors()); |
| 872 | } |
[email protected] | 051b6ab | 2010-10-18 16:50:46 | [diff] [blame] | 873 | DCHECK_LT(category, static_cast<int>(RESOLVE_MAX)); // Be sure it was set. |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 874 | |
| 875 | UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCategory", category, RESOLVE_MAX); |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 876 | } |
| 877 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 878 | void RecordAttemptHistograms(const base::TimeTicks& start_time, |
| 879 | const uint32 attempt_number, |
| 880 | const int error, |
| 881 | const int os_error) const { |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 882 | DCHECK(task_runner_->BelongsToCurrentThread()); |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 883 | bool first_attempt_to_complete = |
| 884 | completed_attempt_number_ == attempt_number; |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 885 | bool is_first_attempt = (attempt_number == 1); |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 886 | |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 887 | if (first_attempt_to_complete) { |
| 888 | // If this was first attempt to complete, then record the resolution |
| 889 | // status of the attempt. |
| 890 | if (completed_attempt_error_ == OK) { |
| 891 | UMA_HISTOGRAM_ENUMERATION( |
| 892 | "DNS.AttemptFirstSuccess", attempt_number, 100); |
| 893 | } else { |
| 894 | UMA_HISTOGRAM_ENUMERATION( |
| 895 | "DNS.AttemptFirstFailure", attempt_number, 100); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (error == OK) |
| 900 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptSuccess", attempt_number, 100); |
| 901 | else |
| 902 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptFailure", attempt_number, 100); |
| 903 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 904 | // If first attempt didn't finish before retry attempt, then calculate stats |
| 905 | // on how much time is saved by having spawned an extra attempt. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 906 | if (!first_attempt_to_complete && is_first_attempt && !was_canceled()) { |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 907 | DNS_HISTOGRAM("DNS.AttemptTimeSavedByRetry", |
| 908 | base::TimeTicks::Now() - retry_attempt_finished_time_); |
| 909 | } |
| 910 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 911 | if (was_canceled() || !first_attempt_to_complete) { |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 912 | // Count those attempts which completed after the job was already canceled |
| 913 | // OR after the job was already completed by an earlier attempt (so in |
| 914 | // effect). |
| 915 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptDiscarded", attempt_number, 100); |
| 916 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 917 | // Record if job is canceled. |
| 918 | if (was_canceled()) |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 919 | UMA_HISTOGRAM_ENUMERATION("DNS.AttemptCancelled", attempt_number, 100); |
| 920 | } |
| 921 | |
| 922 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
| 923 | if (error == OK) |
| 924 | DNS_HISTOGRAM("DNS.AttemptSuccessDuration", duration); |
| 925 | else |
| 926 | DNS_HISTOGRAM("DNS.AttemptFailDuration", duration); |
| 927 | } |
[email protected] | 1e9bbd2 | 2010-10-15 16:42:45 | [diff] [blame] | 928 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 929 | // Set on the task runner thread, read on the worker thread. |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 930 | Key key_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 931 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 932 | // Holds an owning reference to the HostResolverProc that we are going to use. |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 933 | // This may not be the current resolver procedure by the time we call |
| 934 | // ResolveAddrInfo, but that's OK... we'll use it anyways, and the owning |
| 935 | // reference ensures that it remains valid until we are done. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 936 | ProcTaskParams params_; |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 937 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 938 | // The listener to the results of this ProcTask. |
| 939 | Callback callback_; |
| 940 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 941 | // Used to post ourselves onto the task runner thread. |
| 942 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
[email protected] | 189163e | 2011-05-11 01:48:54 | [diff] [blame] | 943 | |
| 944 | // Keeps track of the number of attempts we have made so far to resolve the |
| 945 | // host. Whenever we start an attempt to resolve the host, we increase this |
| 946 | // number. |
| 947 | uint32 attempt_number_; |
| 948 | |
| 949 | // The index of the attempt which finished first (or 0 if the job is still in |
| 950 | // progress). |
| 951 | uint32 completed_attempt_number_; |
| 952 | |
| 953 | // The result (a net error code) from the first attempt to complete. |
| 954 | int completed_attempt_error_; |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 955 | |
[email protected] | e87b8b51 | 2011-06-14 22:12:52 | [diff] [blame] | 956 | // The time when retry attempt was finished. |
| 957 | base::TimeTicks retry_attempt_finished_time_; |
| 958 | |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 959 | // True if a non-speculative request was ever attached to this job |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 960 | // (regardless of whether or not it was later canceled. |
[email protected] | 252b699b | 2010-02-05 21:38:06 | [diff] [blame] | 961 | // This boolean is used for histogramming the duration of jobs used to |
| 962 | // service non-speculative requests. |
| 963 | bool had_non_speculative_request_; |
| 964 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 965 | AddressList results_; |
| 966 | |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 967 | BoundNetLog net_log_; |
| 968 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 969 | DISALLOW_COPY_AND_ASSIGN(ProcTask); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 970 | }; |
| 971 | |
| 972 | //----------------------------------------------------------------------------- |
| 973 | |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 974 | // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as |
| 975 | // it takes 40-100ms and should not block initialization. |
| 976 | class HostResolverImpl::LoopbackProbeJob { |
| 977 | public: |
| 978 | explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver) |
| 979 | : resolver_(resolver), |
| 980 | result_(false) { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 981 | DCHECK(resolver.get()); |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 982 | const bool kIsSlow = true; |
| 983 | base::WorkerPool::PostTaskAndReply( |
| 984 | FROM_HERE, |
| 985 | base::Bind(&LoopbackProbeJob::DoProbe, base::Unretained(this)), |
| 986 | base::Bind(&LoopbackProbeJob::OnProbeComplete, base::Owned(this)), |
| 987 | kIsSlow); |
| 988 | } |
| 989 | |
| 990 | virtual ~LoopbackProbeJob() {} |
| 991 | |
| 992 | private: |
| 993 | // Runs on worker thread. |
| 994 | void DoProbe() { |
| 995 | result_ = HaveOnlyLoopbackAddresses(); |
| 996 | } |
| 997 | |
| 998 | void OnProbeComplete() { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 999 | if (!resolver_.get()) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1000 | return; |
| 1001 | resolver_->SetHaveOnlyLoopbackAddresses(result_); |
| 1002 | } |
| 1003 | |
anujk.sharma | 7c907f0 | 2015-05-29 02:55:44 | [diff] [blame^] | 1004 | // Used/set only on task runner thread. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1005 | base::WeakPtr<HostResolverImpl> resolver_; |
| 1006 | |
| 1007 | bool result_; |
| 1008 | |
| 1009 | DISALLOW_COPY_AND_ASSIGN(LoopbackProbeJob); |
| 1010 | }; |
| 1011 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 1012 | //----------------------------------------------------------------------------- |
| 1013 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1014 | // Resolves the hostname using DnsTransaction. |
| 1015 | // TODO(szym): This could be moved to separate source file as well. |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1016 | class HostResolverImpl::DnsTask : public base::SupportsWeakPtr<DnsTask> { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1017 | public: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1018 | class Delegate { |
| 1019 | public: |
| 1020 | virtual void OnDnsTaskComplete(base::TimeTicks start_time, |
| 1021 | int net_error, |
| 1022 | const AddressList& addr_list, |
| 1023 | base::TimeDelta ttl) = 0; |
| 1024 | |
| 1025 | // Called when the first of two jobs succeeds. If the first completed |
| 1026 | // transaction fails, this is not called. Also not called when the DnsTask |
| 1027 | // only needs to run one transaction. |
| 1028 | virtual void OnFirstDnsTransactionComplete() = 0; |
| 1029 | |
| 1030 | protected: |
| 1031 | Delegate() {} |
| 1032 | virtual ~Delegate() {} |
| 1033 | }; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1034 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1035 | DnsTask(DnsClient* client, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1036 | const Key& key, |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1037 | Delegate* delegate, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1038 | const BoundNetLog& job_net_log) |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1039 | : client_(client), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1040 | key_(key), |
| 1041 | delegate_(delegate), |
| 1042 | net_log_(job_net_log), |
| 1043 | num_completed_transactions_(0), |
| 1044 | task_start_time_(base::TimeTicks::Now()) { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1045 | DCHECK(client); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1046 | DCHECK(delegate_); |
[email protected] | 1affed6 | 2013-08-21 03:24:50 | [diff] [blame] | 1047 | } |
| 1048 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1049 | bool needs_two_transactions() const { |
| 1050 | return key_.address_family == ADDRESS_FAMILY_UNSPECIFIED; |
| 1051 | } |
| 1052 | |
| 1053 | bool needs_another_transaction() const { |
| 1054 | return needs_two_transactions() && !transaction_aaaa_; |
| 1055 | } |
| 1056 | |
| 1057 | void StartFirstTransaction() { |
| 1058 | DCHECK_EQ(0u, num_completed_transactions_); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1059 | net_log_.BeginEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1060 | if (key_.address_family == ADDRESS_FAMILY_IPV6) { |
| 1061 | StartAAAA(); |
| 1062 | } else { |
| 1063 | StartA(); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | void StartSecondTransaction() { |
| 1068 | DCHECK(needs_two_transactions()); |
| 1069 | StartAAAA(); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1073 | void StartA() { |
| 1074 | DCHECK(!transaction_a_); |
| 1075 | DCHECK_NE(ADDRESS_FAMILY_IPV6, key_.address_family); |
| 1076 | transaction_a_ = CreateTransaction(ADDRESS_FAMILY_IPV4); |
| 1077 | transaction_a_->Start(); |
| 1078 | } |
| 1079 | |
| 1080 | void StartAAAA() { |
| 1081 | DCHECK(!transaction_aaaa_); |
| 1082 | DCHECK_NE(ADDRESS_FAMILY_IPV4, key_.address_family); |
| 1083 | transaction_aaaa_ = CreateTransaction(ADDRESS_FAMILY_IPV6); |
| 1084 | transaction_aaaa_->Start(); |
| 1085 | } |
| 1086 | |
| 1087 | scoped_ptr<DnsTransaction> CreateTransaction(AddressFamily family) { |
| 1088 | DCHECK_NE(ADDRESS_FAMILY_UNSPECIFIED, family); |
| 1089 | return client_->GetTransactionFactory()->CreateTransaction( |
| 1090 | key_.hostname, |
| 1091 | family == ADDRESS_FAMILY_IPV6 ? dns_protocol::kTypeAAAA : |
| 1092 | dns_protocol::kTypeA, |
| 1093 | base::Bind(&DnsTask::OnTransactionComplete, base::Unretained(this), |
| 1094 | base::TimeTicks::Now()), |
| 1095 | net_log_); |
| 1096 | } |
| 1097 | |
| 1098 | void OnTransactionComplete(const base::TimeTicks& start_time, |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1099 | DnsTransaction* transaction, |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1100 | int net_error, |
| 1101 | const DnsResponse* response) { |
[email protected] | add7653 | 2012-03-30 14:47:47 | [diff] [blame] | 1102 | DCHECK(transaction); |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1103 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1104 | if (net_error != OK) { |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1105 | DNS_HISTOGRAM("AsyncDNS.TransactionFailure", duration); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1106 | OnFailure(net_error, DnsResponse::DNS_PARSE_OK); |
| 1107 | return; |
[email protected] | 6c41190 | 2012-08-14 22:36:36 | [diff] [blame] | 1108 | } |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1109 | |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1110 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess", duration); |
| 1111 | switch (transaction->GetType()) { |
| 1112 | case dns_protocol::kTypeA: |
| 1113 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_A", duration); |
| 1114 | break; |
| 1115 | case dns_protocol::kTypeAAAA: |
| 1116 | DNS_HISTOGRAM("AsyncDNS.TransactionSuccess_AAAA", duration); |
| 1117 | break; |
| 1118 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1119 | |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1120 | AddressList addr_list; |
| 1121 | base::TimeDelta ttl; |
| 1122 | DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl); |
| 1123 | UMA_HISTOGRAM_ENUMERATION("AsyncDNS.ParseToAddressList", |
| 1124 | result, |
| 1125 | DnsResponse::DNS_PARSE_RESULT_MAX); |
| 1126 | if (result != DnsResponse::DNS_PARSE_OK) { |
| 1127 | // Fail even if the other query succeeds. |
| 1128 | OnFailure(ERR_DNS_MALFORMED_RESPONSE, result); |
| 1129 | return; |
| 1130 | } |
| 1131 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1132 | ++num_completed_transactions_; |
| 1133 | if (num_completed_transactions_ == 1) { |
| 1134 | ttl_ = ttl; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1135 | } else { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1136 | ttl_ = std::min(ttl_, ttl); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1137 | } |
| 1138 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1139 | if (transaction->GetType() == dns_protocol::kTypeA) { |
| 1140 | DCHECK_EQ(transaction_a_.get(), transaction); |
| 1141 | // Place IPv4 addresses after IPv6. |
| 1142 | addr_list_.insert(addr_list_.end(), addr_list.begin(), addr_list.end()); |
| 1143 | } else { |
| 1144 | DCHECK_EQ(transaction_aaaa_.get(), transaction); |
| 1145 | // Place IPv6 addresses before IPv4. |
| 1146 | addr_list_.insert(addr_list_.begin(), addr_list.begin(), addr_list.end()); |
| 1147 | } |
| 1148 | |
| 1149 | if (needs_two_transactions() && num_completed_transactions_ == 1) { |
| 1150 | // No need to repeat the suffix search. |
| 1151 | key_.hostname = transaction->GetHostname(); |
| 1152 | delegate_->OnFirstDnsTransactionComplete(); |
| 1153 | return; |
| 1154 | } |
| 1155 | |
| 1156 | if (addr_list_.empty()) { |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1157 | // TODO(szym): Don't fallback to ProcTask in this case. |
| 1158 | OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1159 | return; |
| 1160 | } |
| 1161 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1162 | // If there are multiple addresses, and at least one is IPv6, need to sort |
| 1163 | // them. Note that IPv6 addresses are always put before IPv4 ones, so it's |
| 1164 | // sufficient to just check the family of the first address. |
| 1165 | if (addr_list_.size() > 1 && |
| 1166 | addr_list_[0].GetFamily() == ADDRESS_FAMILY_IPV6) { |
| 1167 | // Sort addresses if needed. Sort could complete synchronously. |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1168 | client_->GetAddressSorter()->Sort( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1169 | addr_list_, |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 1170 | base::Bind(&DnsTask::OnSortComplete, |
| 1171 | AsWeakPtr(), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1172 | base::TimeTicks::Now())); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1173 | } else { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1174 | OnSuccess(addr_list_); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | void OnSortComplete(base::TimeTicks start_time, |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1179 | bool success, |
| 1180 | const AddressList& addr_list) { |
| 1181 | if (!success) { |
| 1182 | DNS_HISTOGRAM("AsyncDNS.SortFailure", |
| 1183 | base::TimeTicks::Now() - start_time); |
| 1184 | OnFailure(ERR_DNS_SORT_ERROR, DnsResponse::DNS_PARSE_OK); |
| 1185 | return; |
| 1186 | } |
| 1187 | |
| 1188 | DNS_HISTOGRAM("AsyncDNS.SortSuccess", |
| 1189 | base::TimeTicks::Now() - start_time); |
| 1190 | |
| 1191 | // AddressSorter prunes unusable destinations. |
| 1192 | if (addr_list.empty()) { |
| 1193 | LOG(WARNING) << "Address list empty after RFC3484 sort"; |
| 1194 | OnFailure(ERR_NAME_NOT_RESOLVED, DnsResponse::DNS_PARSE_OK); |
| 1195 | return; |
| 1196 | } |
| 1197 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1198 | OnSuccess(addr_list); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | void OnFailure(int net_error, DnsResponse::Result result) { |
| 1202 | DCHECK_NE(OK, net_error); |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1203 | net_log_.EndEvent( |
| 1204 | NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, |
| 1205 | base::Bind(&NetLogDnsTaskFailedCallback, net_error, result)); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1206 | delegate_->OnDnsTaskComplete(task_start_time_, net_error, AddressList(), |
| 1207 | base::TimeDelta()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1208 | } |
| 1209 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1210 | void OnSuccess(const AddressList& addr_list) { |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1211 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_DNS_TASK, |
| 1212 | addr_list.CreateNetLogCallback()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1213 | delegate_->OnDnsTaskComplete(task_start_time_, OK, addr_list, ttl_); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | DnsClient* client_; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1217 | Key key_; |
| 1218 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1219 | // The listener to the results of this DnsTask. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1220 | Delegate* delegate_; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1221 | const BoundNetLog net_log_; |
| 1222 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1223 | scoped_ptr<DnsTransaction> transaction_a_; |
| 1224 | scoped_ptr<DnsTransaction> transaction_aaaa_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1225 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1226 | unsigned num_completed_transactions_; |
| 1227 | |
| 1228 | // These are updated as each transaction completes. |
| 1229 | base::TimeDelta ttl_; |
| 1230 | // IPv6 addresses must appear first in the list. |
| 1231 | AddressList addr_list_; |
| 1232 | |
| 1233 | base::TimeTicks task_start_time_; |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1234 | |
| 1235 | DISALLOW_COPY_AND_ASSIGN(DnsTask); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1236 | }; |
| 1237 | |
| 1238 | //----------------------------------------------------------------------------- |
| 1239 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1240 | // Aggregates all Requests for the same Key. Dispatched via PriorityDispatch. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1241 | class HostResolverImpl::Job : public PrioritizedDispatcher::Job, |
| 1242 | public HostResolverImpl::DnsTask::Delegate { |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1243 | public: |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1244 | // Creates new job for |key| where |request_net_log| is bound to the |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1245 | // request that spawned it. |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1246 | Job(const base::WeakPtr<HostResolverImpl>& resolver, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1247 | const Key& key, |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 1248 | RequestPriority priority, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1249 | const BoundNetLog& source_net_log) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1250 | : resolver_(resolver), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1251 | key_(key), |
[email protected] | 8c98d00 | 2012-07-18 19:02:27 | [diff] [blame] | 1252 | priority_tracker_(priority), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1253 | had_non_speculative_request_(false), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1254 | had_dns_config_(false), |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1255 | num_occupied_job_slots_(0), |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1256 | dns_task_error_(OK), |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1257 | creation_time_(base::TimeTicks::Now()), |
| 1258 | priority_change_time_(creation_time_), |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1259 | net_log_(BoundNetLog::Make(source_net_log.net_log(), |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1260 | NetLog::SOURCE_HOST_RESOLVER_IMPL_JOB)) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1261 | source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CREATE_JOB); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1262 | |
| 1263 | net_log_.BeginEvent( |
| 1264 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1265 | base::Bind(&NetLogJobCreationCallback, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1266 | source_net_log.source(), |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1267 | &key_.hostname)); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1268 | } |
| 1269 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1270 | ~Job() override { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1271 | if (is_running()) { |
| 1272 | // |resolver_| was destroyed with this Job still in flight. |
| 1273 | // Clean-up, record in the log, but don't run any callbacks. |
| 1274 | if (is_proc_running()) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1275 | proc_task_->Cancel(); |
| 1276 | proc_task_ = NULL; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1277 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1278 | // Clean up now for nice NetLog. |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1279 | KillDnsTask(); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1280 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1281 | ERR_ABORTED); |
| 1282 | } else if (is_queued()) { |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 1283 | // |resolver_| was destroyed without running this Job. |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1284 | // TODO(szym): is there any benefit in having this distinction? |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1285 | net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 1286 | net_log_.EndEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1287 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1288 | // else CompleteRequests logged EndEvent. |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1289 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1290 | // Log any remaining Requests as cancelled. |
| 1291 | for (RequestsList::const_iterator it = requests_.begin(); |
| 1292 | it != requests_.end(); ++it) { |
| 1293 | Request* req = *it; |
| 1294 | if (req->was_canceled()) |
| 1295 | continue; |
| 1296 | DCHECK_EQ(this, req->job()); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1297 | LogCancelRequest(req->source_net_log(), req->info()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1298 | } |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1299 | } |
| 1300 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1301 | // Add this job to the dispatcher. If "at_head" is true, adds at the front |
| 1302 | // of the queue. |
| 1303 | void Schedule(bool at_head) { |
| 1304 | DCHECK(!is_queued()); |
| 1305 | PrioritizedDispatcher::Handle handle; |
| 1306 | if (!at_head) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1307 | handle = resolver_->dispatcher_->Add(this, priority()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1308 | } else { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1309 | handle = resolver_->dispatcher_->AddAtHead(this, priority()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1310 | } |
| 1311 | // The dispatcher could have started |this| in the above call to Add, which |
| 1312 | // could have called Schedule again. In that case |handle| will be null, |
| 1313 | // but |handle_| may have been set by the other nested call to Schedule. |
| 1314 | if (!handle.is_null()) { |
| 1315 | DCHECK(handle_.is_null()); |
| 1316 | handle_ = handle; |
| 1317 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1318 | } |
| 1319 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1320 | void AddRequest(scoped_ptr<Request> req) { |
estark | 5d131a1 | 2015-03-26 19:52:19 | [diff] [blame] | 1321 | // .localhost queries are redirected to "localhost." to make sure |
| 1322 | // that they are never sent out on the network, per RFC 6761. |
| 1323 | if (IsLocalhostTLD(req->info().hostname())) { |
| 1324 | DCHECK_EQ(key_.hostname, kLocalhost); |
| 1325 | } else { |
| 1326 | DCHECK_EQ(key_.hostname, req->info().hostname()); |
| 1327 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1328 | |
| 1329 | req->set_job(this); |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1330 | priority_tracker_.Add(req->priority()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1331 | |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1332 | req->source_net_log().AddEvent( |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1333 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_ATTACH, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1334 | net_log_.source().ToEventParametersCallback()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1335 | |
| 1336 | net_log_.AddEvent( |
| 1337 | NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_ATTACH, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1338 | base::Bind(&NetLogJobAttachCallback, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1339 | req->source_net_log().source(), |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 1340 | priority())); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1341 | |
| 1342 | // TODO(szym): Check if this is still needed. |
| 1343 | if (!req->info().is_speculative()) { |
| 1344 | had_non_speculative_request_ = true; |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 1345 | if (proc_task_.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1346 | proc_task_->set_had_non_speculative_request(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1347 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1348 | |
hari.singh1 | bc615d387 | 2015-05-27 04:26:45 | [diff] [blame] | 1349 | requests_.push_back(req.Pass()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1350 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1351 | UpdatePriority(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1352 | } |
| 1353 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1354 | // Marks |req| as cancelled. If it was the last active Request, also finishes |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1355 | // this Job, marking it as cancelled, and deletes it. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1356 | void CancelRequest(Request* req) { |
| 1357 | DCHECK_EQ(key_.hostname, req->info().hostname()); |
| 1358 | DCHECK(!req->was_canceled()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1359 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1360 | // Don't remove it from |requests_| just mark it canceled. |
| 1361 | req->MarkAsCanceled(); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1362 | LogCancelRequest(req->source_net_log(), req->info()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1363 | |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1364 | priority_tracker_.Remove(req->priority()); |
| 1365 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_REQUEST_DETACH, |
| 1366 | base::Bind(&NetLogJobAttachCallback, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1367 | req->source_net_log().source(), |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1368 | priority())); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1369 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1370 | if (num_active_requests() > 0) { |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1371 | UpdatePriority(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1372 | } else { |
| 1373 | // If we were called from a Request's callback within CompleteRequests, |
| 1374 | // that Request could not have been cancelled, so num_active_requests() |
| 1375 | // could not be 0. Therefore, we are not in CompleteRequests(). |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1376 | CompleteRequestsWithError(OK /* cancelled */); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1377 | } |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1378 | } |
| 1379 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1380 | // Called from AbortAllInProgressJobs. Completes all requests and destroys |
| 1381 | // the job. This currently assumes the abort is due to a network change. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1382 | void Abort() { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1383 | DCHECK(is_running()); |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1384 | CompleteRequestsWithError(ERR_NETWORK_CHANGED); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1385 | } |
| 1386 | |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1387 | // If DnsTask present, abort it and fall back to ProcTask. |
| 1388 | void AbortDnsTask() { |
| 1389 | if (dns_task_) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1390 | KillDnsTask(); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1391 | dns_task_error_ = OK; |
| 1392 | StartProcTask(); |
| 1393 | } |
| 1394 | } |
| 1395 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1396 | // Called by HostResolverImpl when this job is evicted due to queue overflow. |
| 1397 | // Completes all requests and destroys the job. |
| 1398 | void OnEvicted() { |
| 1399 | DCHECK(!is_running()); |
| 1400 | DCHECK(is_queued()); |
| 1401 | handle_.Reset(); |
| 1402 | |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1403 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_EVICTED); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1404 | |
| 1405 | // This signals to CompleteRequests that this job never ran. |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1406 | CompleteRequestsWithError(ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1407 | } |
| 1408 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1409 | // Attempts to serve the job from HOSTS. Returns true if succeeded and |
| 1410 | // this Job was destroyed. |
| 1411 | bool ServeFromHosts() { |
| 1412 | DCHECK_GT(num_active_requests(), 0u); |
| 1413 | AddressList addr_list; |
| 1414 | if (resolver_->ServeFromHosts(key(), |
[email protected] | 3cb676a1 | 2012-06-30 15:46:03 | [diff] [blame] | 1415 | requests_.front()->info(), |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1416 | &addr_list)) { |
| 1417 | // This will destroy the Job. |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1418 | CompleteRequests( |
| 1419 | HostCache::Entry(OK, MakeAddressListForRequest(addr_list)), |
| 1420 | base::TimeDelta()); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1421 | return true; |
| 1422 | } |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1426 | const Key key() const { |
| 1427 | return key_; |
| 1428 | } |
| 1429 | |
| 1430 | bool is_queued() const { |
| 1431 | return !handle_.is_null(); |
| 1432 | } |
| 1433 | |
| 1434 | bool is_running() const { |
| 1435 | return is_dns_running() || is_proc_running(); |
| 1436 | } |
| 1437 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1438 | private: |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1439 | void KillDnsTask() { |
| 1440 | if (dns_task_) { |
| 1441 | ReduceToOneJobSlot(); |
| 1442 | dns_task_.reset(); |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | // Reduce the number of job slots occupied and queued in the dispatcher |
| 1447 | // to one. If the second Job slot is queued in the dispatcher, cancels the |
| 1448 | // queued job. Otherwise, the second Job has been started by the |
| 1449 | // PrioritizedDispatcher, so signals it is complete. |
| 1450 | void ReduceToOneJobSlot() { |
| 1451 | DCHECK_GE(num_occupied_job_slots_, 1u); |
| 1452 | if (is_queued()) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1453 | resolver_->dispatcher_->Cancel(handle_); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1454 | handle_.Reset(); |
| 1455 | } else if (num_occupied_job_slots_ > 1) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1456 | resolver_->dispatcher_->OnJobFinished(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1457 | --num_occupied_job_slots_; |
| 1458 | } |
| 1459 | DCHECK_EQ(1u, num_occupied_job_slots_); |
| 1460 | } |
| 1461 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1462 | void UpdatePriority() { |
| 1463 | if (is_queued()) { |
| 1464 | if (priority() != static_cast<RequestPriority>(handle_.priority())) |
| 1465 | priority_change_time_ = base::TimeTicks::Now(); |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1466 | handle_ = resolver_->dispatcher_->ChangePriority(handle_, priority()); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1470 | AddressList MakeAddressListForRequest(const AddressList& list) const { |
| 1471 | if (requests_.empty()) |
| 1472 | return list; |
| 1473 | return AddressList::CopyWithPort(list, requests_.front()->info().port()); |
| 1474 | } |
| 1475 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1476 | // PriorityDispatch::Job: |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1477 | void Start() override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1478 | DCHECK_LE(num_occupied_job_slots_, 1u); |
| 1479 | |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 1480 | handle_.Reset(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1481 | ++num_occupied_job_slots_; |
| 1482 | |
| 1483 | if (num_occupied_job_slots_ == 2) { |
| 1484 | StartSecondDnsTransaction(); |
| 1485 | return; |
| 1486 | } |
| 1487 | |
| 1488 | DCHECK(!is_running()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1489 | |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1490 | net_log_.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB_STARTED); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1491 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1492 | had_dns_config_ = resolver_->HaveDnsConfig(); |
| 1493 | |
| 1494 | base::TimeTicks now = base::TimeTicks::Now(); |
| 1495 | base::TimeDelta queue_time = now - creation_time_; |
| 1496 | base::TimeDelta queue_time_after_change = now - priority_change_time_; |
| 1497 | |
| 1498 | if (had_dns_config_) { |
| 1499 | DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTime", priority(), |
| 1500 | queue_time); |
| 1501 | DNS_HISTOGRAM_BY_PRIORITY("AsyncDNS.JobQueueTimeAfterChange", priority(), |
| 1502 | queue_time_after_change); |
| 1503 | } else { |
| 1504 | DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTime", priority(), queue_time); |
| 1505 | DNS_HISTOGRAM_BY_PRIORITY("DNS.JobQueueTimeAfterChange", priority(), |
| 1506 | queue_time_after_change); |
| 1507 | } |
| 1508 | |
[email protected] | 443714fad | 2013-09-19 04:52:01 | [diff] [blame] | 1509 | bool system_only = |
| 1510 | (key_.host_resolver_flags & HOST_RESOLVER_SYSTEM_ONLY) != 0; |
| 1511 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1512 | // Caution: Job::Start must not complete synchronously. |
[email protected] | 443714fad | 2013-09-19 04:52:01 | [diff] [blame] | 1513 | if (!system_only && had_dns_config_ && |
| 1514 | !ResemblesMulticastDNSName(key_.hostname)) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1515 | StartDnsTask(); |
| 1516 | } else { |
| 1517 | StartProcTask(); |
| 1518 | } |
| 1519 | } |
| 1520 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1521 | // TODO(szym): Since DnsTransaction does not consume threads, we can increase |
| 1522 | // the limits on |dispatcher_|. But in order to keep the number of WorkerPool |
| 1523 | // threads low, we will need to use an "inner" PrioritizedDispatcher with |
| 1524 | // tighter limits. |
| 1525 | void StartProcTask() { |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1526 | DCHECK(!is_dns_running()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1527 | proc_task_ = new ProcTask( |
| 1528 | key_, |
| 1529 | resolver_->proc_params_, |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1530 | base::Bind(&Job::OnProcTaskComplete, base::Unretained(this), |
| 1531 | base::TimeTicks::Now()), |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1532 | net_log_); |
| 1533 | |
| 1534 | if (had_non_speculative_request_) |
| 1535 | proc_task_->set_had_non_speculative_request(); |
| 1536 | // Start() could be called from within Resolve(), hence it must NOT directly |
| 1537 | // call OnProcTaskComplete, for example, on synchronous failure. |
| 1538 | proc_task_->Start(); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1539 | } |
| 1540 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1541 | // Called by ProcTask when it completes. |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1542 | void OnProcTaskComplete(base::TimeTicks start_time, |
| 1543 | int net_error, |
| 1544 | const AddressList& addr_list) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1545 | DCHECK(is_proc_running()); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1546 | |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1547 | if (!resolver_->resolved_known_ipv6_hostname_ && |
| 1548 | net_error == OK && |
| 1549 | key_.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 1550 | if (key_.hostname == "www.google.com") { |
| 1551 | resolver_->resolved_known_ipv6_hostname_ = true; |
| 1552 | bool got_ipv6_address = false; |
| 1553 | for (size_t i = 0; i < addr_list.size(); ++i) { |
[email protected] | 5134c22a | 2013-08-06 18:09:02 | [diff] [blame] | 1554 | if (addr_list[i].GetFamily() == ADDRESS_FAMILY_IPV6) { |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1555 | got_ipv6_address = true; |
[email protected] | 5134c22a | 2013-08-06 18:09:02 | [diff] [blame] | 1556 | break; |
| 1557 | } |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1558 | } |
| 1559 | UMA_HISTOGRAM_BOOLEAN("Net.UnspecResolvedIPv6", got_ipv6_address); |
| 1560 | } |
| 1561 | } |
| 1562 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1563 | if (dns_task_error_ != OK) { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1564 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1565 | if (net_error == OK) { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1566 | DNS_HISTOGRAM("AsyncDNS.FallbackSuccess", duration); |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1567 | if ((dns_task_error_ == ERR_NAME_NOT_RESOLVED) && |
| 1568 | ResemblesNetBIOSName(key_.hostname)) { |
| 1569 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_SUSPECT_NETBIOS); |
| 1570 | } else { |
| 1571 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_PROC_SUCCESS); |
| 1572 | } |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 1573 | UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.ResolveError", |
| 1574 | std::abs(dns_task_error_)); |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 1575 | resolver_->OnDnsTaskResolve(dns_task_error_); |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1576 | } else { |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1577 | DNS_HISTOGRAM("AsyncDNS.FallbackFail", duration); |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1578 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL); |
| 1579 | } |
| 1580 | } |
| 1581 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1582 | base::TimeDelta ttl = |
| 1583 | base::TimeDelta::FromSeconds(kNegativeCacheEntryTTLSeconds); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1584 | if (net_error == OK) |
| 1585 | ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1586 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1587 | // Don't store the |ttl| in cache since it's not obtained from the server. |
| 1588 | CompleteRequests( |
| 1589 | HostCache::Entry(net_error, MakeAddressListForRequest(addr_list)), |
| 1590 | ttl); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1591 | } |
| 1592 | |
| 1593 | void StartDnsTask() { |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1594 | DCHECK(resolver_->HaveDnsConfig()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1595 | dns_task_.reset(new DnsTask(resolver_->dns_client_.get(), key_, this, |
| 1596 | net_log_)); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1597 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1598 | dns_task_->StartFirstTransaction(); |
| 1599 | // Schedule a second transaction, if needed. |
| 1600 | if (dns_task_->needs_two_transactions()) |
| 1601 | Schedule(true); |
| 1602 | } |
| 1603 | |
| 1604 | void StartSecondDnsTransaction() { |
| 1605 | DCHECK(dns_task_->needs_two_transactions()); |
| 1606 | dns_task_->StartSecondTransaction(); |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | // Called if DnsTask fails. It is posted from StartDnsTask, so Job may be |
| 1610 | // deleted before this callback. In this case dns_task is deleted as well, |
| 1611 | // so we use it as indicator whether Job is still valid. |
| 1612 | void OnDnsTaskFailure(const base::WeakPtr<DnsTask>& dns_task, |
| 1613 | base::TimeDelta duration, |
| 1614 | int net_error) { |
| 1615 | DNS_HISTOGRAM("AsyncDNS.ResolveFail", duration); |
| 1616 | |
| 1617 | if (dns_task == NULL) |
| 1618 | return; |
| 1619 | |
| 1620 | dns_task_error_ = net_error; |
| 1621 | |
| 1622 | // TODO(szym): Run ServeFromHosts now if nsswitch.conf says so. |
| 1623 | // http://crbug.com/117655 |
| 1624 | |
| 1625 | // TODO(szym): Some net errors indicate lack of connectivity. Starting |
| 1626 | // ProcTask in that case is a waste of time. |
| 1627 | if (resolver_->fallback_to_proctask_) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1628 | KillDnsTask(); |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1629 | StartProcTask(); |
| 1630 | } else { |
| 1631 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_FAIL); |
| 1632 | CompleteRequestsWithError(net_error); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1636 | |
| 1637 | // HostResolverImpl::DnsTask::Delegate implementation: |
| 1638 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1639 | void OnDnsTaskComplete(base::TimeTicks start_time, |
| 1640 | int net_error, |
| 1641 | const AddressList& addr_list, |
| 1642 | base::TimeDelta ttl) override { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1643 | DCHECK(is_dns_running()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1644 | |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1645 | base::TimeDelta duration = base::TimeTicks::Now() - start_time; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1646 | if (net_error != OK) { |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1647 | OnDnsTaskFailure(dns_task_->AsWeakPtr(), duration, net_error); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1648 | return; |
| 1649 | } |
[email protected] | e3bd482 | 2012-10-23 18:01:37 | [diff] [blame] | 1650 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess", duration); |
[email protected] | 02cd698 | 2013-01-10 20:12:51 | [diff] [blame] | 1651 | // Log DNS lookups based on |address_family|. |
| 1652 | switch(key_.address_family) { |
| 1653 | case ADDRESS_FAMILY_IPV4: |
| 1654 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV4", duration); |
| 1655 | break; |
| 1656 | case ADDRESS_FAMILY_IPV6: |
| 1657 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_IPV6", duration); |
| 1658 | break; |
| 1659 | case ADDRESS_FAMILY_UNSPECIFIED: |
| 1660 | DNS_HISTOGRAM("AsyncDNS.ResolveSuccess_FAMILY_UNSPEC", duration); |
| 1661 | break; |
| 1662 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1663 | |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1664 | UmaAsyncDnsResolveStatus(RESOLVE_STATUS_DNS_SUCCESS); |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1665 | RecordTTL(ttl); |
[email protected] | 0adcb2b | 2012-08-15 21:30:46 | [diff] [blame] | 1666 | |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 1667 | resolver_->OnDnsTaskResolve(OK); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1668 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1669 | base::TimeDelta bounded_ttl = |
| 1670 | std::max(ttl, base::TimeDelta::FromSeconds(kMinimumTTLSeconds)); |
| 1671 | |
| 1672 | CompleteRequests( |
| 1673 | HostCache::Entry(net_error, MakeAddressListForRequest(addr_list), ttl), |
| 1674 | bounded_ttl); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1675 | } |
| 1676 | |
dcheng | b03027d | 2014-10-21 12:00:20 | [diff] [blame] | 1677 | void OnFirstDnsTransactionComplete() override { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1678 | DCHECK(dns_task_->needs_two_transactions()); |
| 1679 | DCHECK_EQ(dns_task_->needs_another_transaction(), is_queued()); |
| 1680 | // No longer need to occupy two dispatcher slots. |
| 1681 | ReduceToOneJobSlot(); |
| 1682 | |
| 1683 | // We already have a job slot at the dispatcher, so if the second |
| 1684 | // transaction hasn't started, reuse it now instead of waiting in the queue |
| 1685 | // for the second slot. |
| 1686 | if (dns_task_->needs_another_transaction()) |
| 1687 | dns_task_->StartSecondTransaction(); |
| 1688 | } |
| 1689 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1690 | // Performs Job's last rites. Completes all Requests. Deletes this. |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1691 | void CompleteRequests(const HostCache::Entry& entry, |
| 1692 | base::TimeDelta ttl) { |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1693 | CHECK(resolver_.get()); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1694 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1695 | // This job must be removed from resolver's |jobs_| now to make room for a |
| 1696 | // new job with the same key in case one of the OnComplete callbacks decides |
| 1697 | // to spawn one. Consequently, the job deletes itself when CompleteRequests |
| 1698 | // is done. |
| 1699 | scoped_ptr<Job> self_deleter(this); |
| 1700 | |
| 1701 | resolver_->RemoveJob(this); |
| 1702 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1703 | if (is_running()) { |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1704 | if (is_proc_running()) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1705 | DCHECK(!is_queued()); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1706 | proc_task_->Cancel(); |
| 1707 | proc_task_ = NULL; |
| 1708 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1709 | KillDnsTask(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1710 | |
| 1711 | // Signal dispatcher that a slot has opened. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1712 | resolver_->dispatcher_->OnJobFinished(); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1713 | } else if (is_queued()) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1714 | resolver_->dispatcher_->Cancel(handle_); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1715 | handle_.Reset(); |
| 1716 | } |
| 1717 | |
| 1718 | if (num_active_requests() == 0) { |
[email protected] | 4da911f | 2012-06-14 19:45:20 | [diff] [blame] | 1719 | net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1720 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
| 1721 | OK); |
| 1722 | return; |
| 1723 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1724 | |
| 1725 | net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1726 | entry.error); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1727 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1728 | DCHECK(!requests_.empty()); |
| 1729 | |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1730 | if (entry.error == OK) { |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1731 | // Record this histogram here, when we know the system has a valid DNS |
| 1732 | // configuration. |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 1733 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.HaveDnsConfig", |
| 1734 | resolver_->received_dns_config_); |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1735 | } |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1736 | |
[email protected] | 7af985a | 2012-12-14 22:40:42 | [diff] [blame] | 1737 | bool did_complete = (entry.error != ERR_NETWORK_CHANGED) && |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1738 | (entry.error != ERR_HOST_RESOLVER_QUEUE_TOO_LARGE); |
| 1739 | if (did_complete) |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1740 | resolver_->CacheResult(key_, entry, ttl); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1741 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1742 | // Complete all of the requests that were attached to the job. |
| 1743 | for (RequestsList::const_iterator it = requests_.begin(); |
| 1744 | it != requests_.end(); ++it) { |
| 1745 | Request* req = *it; |
| 1746 | |
| 1747 | if (req->was_canceled()) |
| 1748 | continue; |
| 1749 | |
| 1750 | DCHECK_EQ(this, req->job()); |
| 1751 | // Update the net log and notify registered observers. |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1752 | LogFinishRequest(req->source_net_log(), req->info(), entry.error); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1753 | if (did_complete) { |
| 1754 | // Record effective total time from creation to completion. |
| 1755 | RecordTotalTime(had_dns_config_, req->info().is_speculative(), |
| 1756 | base::TimeTicks::Now() - req->request_time()); |
| 1757 | } |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1758 | req->OnComplete(entry.error, entry.addrlist); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1759 | |
| 1760 | // Check if the resolver was destroyed as a result of running the |
| 1761 | // callback. If it was, we could continue, but we choose to bail. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 1762 | if (!resolver_.get()) |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1763 | return; |
| 1764 | } |
| 1765 | } |
| 1766 | |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1767 | // Convenience wrapper for CompleteRequests in case of failure. |
| 1768 | void CompleteRequestsWithError(int net_error) { |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 1769 | CompleteRequests(HostCache::Entry(net_error, AddressList()), |
| 1770 | base::TimeDelta()); |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 1771 | } |
| 1772 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 1773 | RequestPriority priority() const { |
| 1774 | return priority_tracker_.highest_priority(); |
| 1775 | } |
| 1776 | |
| 1777 | // Number of non-canceled requests in |requests_|. |
| 1778 | size_t num_active_requests() const { |
| 1779 | return priority_tracker_.total_count(); |
| 1780 | } |
| 1781 | |
| 1782 | bool is_dns_running() const { |
| 1783 | return dns_task_.get() != NULL; |
| 1784 | } |
| 1785 | |
| 1786 | bool is_proc_running() const { |
| 1787 | return proc_task_.get() != NULL; |
| 1788 | } |
| 1789 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1790 | base::WeakPtr<HostResolverImpl> resolver_; |
| 1791 | |
| 1792 | Key key_; |
| 1793 | |
| 1794 | // Tracks the highest priority across |requests_|. |
| 1795 | PriorityTracker priority_tracker_; |
| 1796 | |
| 1797 | bool had_non_speculative_request_; |
| 1798 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1799 | // Distinguishes measurements taken while DnsClient was fully configured. |
| 1800 | bool had_dns_config_; |
| 1801 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1802 | // Number of slots occupied by this Job in resolver's PrioritizedDispatcher. |
| 1803 | unsigned num_occupied_job_slots_; |
| 1804 | |
[email protected] | 1d93285 | 2012-06-19 19:40:33 | [diff] [blame] | 1805 | // Result of DnsTask. |
| 1806 | int dns_task_error_; |
[email protected] | 1def74c | 2012-03-22 20:07:00 | [diff] [blame] | 1807 | |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1808 | const base::TimeTicks creation_time_; |
| 1809 | base::TimeTicks priority_change_time_; |
| 1810 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1811 | BoundNetLog net_log_; |
| 1812 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1813 | // Resolves the host using a HostResolverProc. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1814 | scoped_refptr<ProcTask> proc_task_; |
| 1815 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1816 | // Resolves the host using a DnsTransaction. |
| 1817 | scoped_ptr<DnsTask> dns_task_; |
| 1818 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1819 | // All Requests waiting for the result of this Job. Some can be canceled. |
| 1820 | RequestsList requests_; |
| 1821 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1822 | // A handle used in |HostResolverImpl::dispatcher_|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1823 | PrioritizedDispatcher::Handle handle_; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1824 | }; |
| 1825 | |
| 1826 | //----------------------------------------------------------------------------- |
| 1827 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1828 | HostResolverImpl::ProcTaskParams::ProcTaskParams( |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1829 | HostResolverProc* resolver_proc, |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1830 | size_t max_retry_attempts) |
| 1831 | : resolver_proc(resolver_proc), |
| 1832 | max_retry_attempts(max_retry_attempts), |
| 1833 | unresponsive_delay(base::TimeDelta::FromMilliseconds(6000)), |
| 1834 | retry_factor(2) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1835 | // Maximum of 4 retry attempts for host resolution. |
| 1836 | static const size_t kDefaultMaxRetryAttempts = 4u; |
| 1837 | if (max_retry_attempts == HostResolver::kDefaultRetryAttempts) |
| 1838 | max_retry_attempts = kDefaultMaxRetryAttempts; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | HostResolverImpl::ProcTaskParams::~ProcTaskParams() {} |
| 1842 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1843 | HostResolverImpl::HostResolverImpl(const Options& options, NetLog* net_log) |
| 1844 | : max_queued_jobs_(0), |
| 1845 | proc_params_(NULL, options.max_retry_attempts), |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1846 | net_log_(net_log), |
[email protected] | 0c779845 | 2009-10-26 17:59:51 | [diff] [blame] | 1847 | default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1848 | received_dns_config_(false), |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 1849 | num_dns_failures_(0), |
[email protected] | 23330db7 | 2013-07-18 03:32:11 | [diff] [blame] | 1850 | probe_ipv6_support_(true), |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 1851 | use_local_ipv6_(false), |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 1852 | last_ipv6_probe_result_(true), |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 1853 | resolved_known_ipv6_hostname_(false), |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1854 | additional_resolver_flags_(0), |
[email protected] | 0a30cf51 | 2014-05-27 20:55:18 | [diff] [blame] | 1855 | fallback_to_proctask_(true), |
| 1856 | weak_ptr_factory_(this), |
| 1857 | probe_weak_ptr_factory_(this) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1858 | if (options.enable_caching) |
| 1859 | cache_ = HostCache::CreateDefaultCache(); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1860 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1861 | PrioritizedDispatcher::Limits job_limits = options.GetDispatcherLimits(); |
| 1862 | dispatcher_.reset(new PrioritizedDispatcher(job_limits)); |
| 1863 | max_queued_jobs_ = job_limits.total_jobs * 100u; |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1864 | |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1865 | DCHECK_GE(dispatcher_->num_priorities(), static_cast<size_t>(NUM_PRIORITIES)); |
[email protected] | 68ad3ee | 2010-01-30 03:45:39 | [diff] [blame] | 1866 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1867 | #if defined(OS_WIN) |
| 1868 | EnsureWinsockInit(); |
| 1869 | #endif |
[email protected] | 7c466e9 | 2013-07-20 01:44:48 | [diff] [blame] | 1870 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 1871 | new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr()); |
[email protected] | 2f3bc65c | 2010-07-23 17:47:10 | [diff] [blame] | 1872 | #endif |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 1873 | NetworkChangeNotifier::AddIPAddressObserver(this); |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 1874 | NetworkChangeNotifier::AddDNSObserver(this); |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1875 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_OPENBSD) && \ |
| 1876 | !defined(OS_ANDROID) |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 1877 | EnsureDnsReloaderInit(); |
[email protected] | 46018c9d | 2011-09-06 03:42:34 | [diff] [blame] | 1878 | #endif |
[email protected] | 2ac22db | 2012-11-28 19:50:04 | [diff] [blame] | 1879 | |
[email protected] | 2ac22db | 2012-11-28 19:50:04 | [diff] [blame] | 1880 | { |
| 1881 | DnsConfig dns_config; |
| 1882 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 1883 | received_dns_config_ = dns_config.IsValid(); |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 1884 | // Conservatively assume local IPv6 is needed when DnsConfig is not valid. |
| 1885 | use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; |
[email protected] | 2ac22db | 2012-11-28 19:50:04 | [diff] [blame] | 1886 | } |
[email protected] | 16c2bd7 | 2013-06-28 01:19:22 | [diff] [blame] | 1887 | |
| 1888 | fallback_to_proctask_ = !ConfigureAsyncDnsNoFallbackFieldTrial(); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | HostResolverImpl::~HostResolverImpl() { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1892 | // Prevent the dispatcher from starting new jobs. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1893 | dispatcher_->SetLimitsToZero(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1894 | // It's now safe for Jobs to call KillDsnTask on destruction, because |
| 1895 | // OnJobComplete will not start any new jobs. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1896 | STLDeleteValues(&jobs_); |
[email protected] | e95d3aca | 2010-01-11 22:47:43 | [diff] [blame] | 1897 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 1898 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 1899 | NetworkChangeNotifier::RemoveDNSObserver(this); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1900 | } |
| 1901 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1902 | void HostResolverImpl::SetMaxQueuedJobs(size_t value) { |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1903 | DCHECK_EQ(0u, dispatcher_->num_queued_jobs()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1904 | DCHECK_GT(value, 0u); |
| 1905 | max_queued_jobs_ = value; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 1906 | } |
| 1907 | |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1908 | int HostResolverImpl::Resolve(const RequestInfo& info, |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1909 | RequestPriority priority, |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1910 | AddressList* addresses, |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 1911 | const CompletionCallback& callback, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 1912 | RequestHandle* out_req, |
[email protected] | ee094b8 | 2010-08-24 15:55:51 | [diff] [blame] | 1913 | const BoundNetLog& source_net_log) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1914 | DCHECK(addresses); |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1915 | DCHECK(CalledOnValidThread()); |
[email protected] | aa22b24 | 2011-11-16 18:58:29 | [diff] [blame] | 1916 | DCHECK_EQ(false, callback.is_null()); |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 1917 | |
[email protected] | e806cd7 | 2013-05-17 02:08:43 | [diff] [blame] | 1918 | // Check that the caller supplied a valid hostname to resolve. |
| 1919 | std::string labeled_hostname; |
| 1920 | if (!DNSDomainFromDot(info.hostname(), &labeled_hostname)) |
| 1921 | return ERR_NAME_NOT_RESOLVED; |
| 1922 | |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1923 | LogStartRequest(source_net_log, info); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1924 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1925 | IPAddressNumber ip_number; |
| 1926 | IPAddressNumber* ip_number_ptr = nullptr; |
| 1927 | if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) |
| 1928 | ip_number_ptr = &ip_number; |
| 1929 | |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1930 | // Build a key that identifies the request in the cache and in the |
| 1931 | // outstanding jobs map. |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1932 | Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log); |
[email protected] | 123ab1e3 | 2009-10-21 19:12:57 | [diff] [blame] | 1933 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1934 | int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1935 | if (rv != ERR_DNS_CACHE_MISS) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1936 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 51b9a6b | 2012-06-25 21:50:29 | [diff] [blame] | 1937 | RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1938 | return rv; |
[email protected] | 3836871 | 2011-03-02 08:09:40 | [diff] [blame] | 1939 | } |
| 1940 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1941 | // Next we need to attach our request to a "job". This job is responsible for |
| 1942 | // calling "getaddrinfo(hostname)" on a worker thread. |
| 1943 | |
| 1944 | JobMap::iterator jobit = jobs_.find(key); |
| 1945 | Job* job; |
| 1946 | if (jobit == jobs_.end()) { |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1947 | job = |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1948 | new Job(weak_ptr_factory_.GetWeakPtr(), key, priority, source_net_log); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 1949 | job->Schedule(false); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1950 | |
| 1951 | // Check for queue overflow. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 1952 | if (dispatcher_->num_queued_jobs() > max_queued_jobs_) { |
| 1953 | Job* evicted = static_cast<Job*>(dispatcher_->EvictOldestLowest()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1954 | DCHECK(evicted); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 1955 | evicted->OnEvicted(); // Deletes |evicted|. |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1956 | if (evicted == job) { |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1957 | rv = ERR_HOST_RESOLVER_QUEUE_TOO_LARGE; |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1958 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1959 | return rv; |
| 1960 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1961 | } |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1962 | jobs_.insert(jobit, std::make_pair(key, job)); |
| 1963 | } else { |
| 1964 | job = jobit->second; |
| 1965 | } |
| 1966 | |
| 1967 | // Can't complete synchronously. Create and attach request. |
[email protected] | 5109c195 | 2013-08-20 18:44:10 | [diff] [blame] | 1968 | scoped_ptr<Request> req(new Request( |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1969 | source_net_log, info, priority, callback, addresses)); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1970 | if (out_req) |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1971 | *out_req = reinterpret_cast<RequestHandle>(req.get()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1972 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 1973 | job->AddRequest(req.Pass()); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 1974 | // Completion happens during Job::CompleteRequests(). |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 1975 | return ERR_IO_PENDING; |
| 1976 | } |
| 1977 | |
[email protected] | 287d7c2 | 2011-11-15 17:34:25 | [diff] [blame] | 1978 | int HostResolverImpl::ResolveHelper(const Key& key, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1979 | const RequestInfo& info, |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1980 | const IPAddressNumber* ip_number, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1981 | AddressList* addresses, |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1982 | const BoundNetLog& source_net_log) { |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1983 | // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 1984 | // On Windows it gives the default interface's address, whereas on Linux it |
| 1985 | // gives an error. We will make it fail on all platforms for consistency. |
| 1986 | if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) |
| 1987 | return ERR_NAME_NOT_RESOLVED; |
| 1988 | |
| 1989 | int net_error = ERR_UNEXPECTED; |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 1990 | if (ResolveAsIP(key, info, ip_number, &net_error, addresses)) |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 1991 | return net_error; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1992 | if (ServeFromCache(key, info, &net_error, addresses)) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1993 | source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 1994 | return net_error; |
| 1995 | } |
| 1996 | // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 1997 | // http://crbug.com/117655 |
| 1998 | if (ServeFromHosts(key, info, addresses)) { |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 1999 | source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2000 | return OK; |
| 2001 | } |
| 2002 | return ERR_DNS_CACHE_MISS; |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | int HostResolverImpl::ResolveFromCache(const RequestInfo& info, |
| 2006 | AddressList* addresses, |
| 2007 | const BoundNetLog& source_net_log) { |
| 2008 | DCHECK(CalledOnValidThread()); |
| 2009 | DCHECK(addresses); |
| 2010 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2011 | // Update the net log and notify registered observers. |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2012 | LogStartRequest(source_net_log, info); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2013 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2014 | IPAddressNumber ip_number; |
| 2015 | IPAddressNumber* ip_number_ptr = nullptr; |
| 2016 | if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) |
| 2017 | ip_number_ptr = &ip_number; |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2018 | |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2019 | Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log); |
| 2020 | |
| 2021 | int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log); |
xunjieli | 26f9045 | 2014-11-10 16:23:02 | [diff] [blame] | 2022 | LogFinishRequest(source_net_log, info, rv); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2023 | return rv; |
| 2024 | } |
| 2025 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2026 | void HostResolverImpl::CancelRequest(RequestHandle req_handle) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 2027 | DCHECK(CalledOnValidThread()); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2028 | Request* req = reinterpret_cast<Request*>(req_handle); |
| 2029 | DCHECK(req); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2030 | Job* job = req->job(); |
| 2031 | DCHECK(job); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2032 | job->CancelRequest(req); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2033 | } |
| 2034 | |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 2035 | void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { |
[email protected] | 1ac6af9 | 2010-06-03 21:00:14 | [diff] [blame] | 2036 | DCHECK(CalledOnValidThread()); |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 2037 | default_address_family_ = address_family; |
[email protected] | 23330db7 | 2013-07-18 03:32:11 | [diff] [blame] | 2038 | probe_ipv6_support_ = false; |
[email protected] | 0f8f1b43 | 2010-03-16 19:06:03 | [diff] [blame] | 2039 | } |
| 2040 | |
[email protected] | f7d310e | 2010-10-07 16:25:11 | [diff] [blame] | 2041 | AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { |
| 2042 | return default_address_family_; |
| 2043 | } |
| 2044 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2045 | void HostResolverImpl::SetDnsClientEnabled(bool enabled) { |
| 2046 | DCHECK(CalledOnValidThread()); |
| 2047 | #if defined(ENABLE_BUILT_IN_DNS) |
| 2048 | if (enabled && !dns_client_) { |
| 2049 | SetDnsClient(DnsClient::CreateClient(net_log_)); |
| 2050 | } else if (!enabled && dns_client_) { |
| 2051 | SetDnsClient(scoped_ptr<DnsClient>()); |
| 2052 | } |
| 2053 | #endif |
| 2054 | } |
| 2055 | |
[email protected] | 489d1a8 | 2011-10-12 03:09:11 | [diff] [blame] | 2056 | HostCache* HostResolverImpl::GetHostCache() { |
| 2057 | return cache_.get(); |
| 2058 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2059 | |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2060 | base::Value* HostResolverImpl::GetDnsConfigAsValue() const { |
| 2061 | // Check if async DNS is disabled. |
| 2062 | if (!dns_client_.get()) |
| 2063 | return NULL; |
| 2064 | |
| 2065 | // Check if async DNS is enabled, but we currently have no configuration |
| 2066 | // for it. |
| 2067 | const DnsConfig* dns_config = dns_client_->GetConfig(); |
| 2068 | if (dns_config == NULL) |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 2069 | return new base::DictionaryValue(); |
[email protected] | 17e9203 | 2012-03-29 00:56:24 | [diff] [blame] | 2070 | |
| 2071 | return dns_config->ToValue(); |
| 2072 | } |
| 2073 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2074 | bool HostResolverImpl::ResolveAsIP(const Key& key, |
| 2075 | const RequestInfo& info, |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2076 | const IPAddressNumber* ip_number, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2077 | int* net_error, |
| 2078 | AddressList* addresses) { |
| 2079 | DCHECK(addresses); |
| 2080 | DCHECK(net_error); |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2081 | if (ip_number == nullptr) |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2082 | return false; |
| 2083 | |
| 2084 | DCHECK_EQ(key.host_resolver_flags & |
| 2085 | ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | |
| 2086 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), |
| 2087 | 0) << " Unhandled flag"; |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 2088 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2089 | *net_error = OK; |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2090 | AddressFamily family = GetAddressFamily(*ip_number); |
[email protected] | 1c7cf3f8 | 2014-08-07 21:33:48 | [diff] [blame] | 2091 | if (family == ADDRESS_FAMILY_IPV6 && |
| 2092 | !probe_ipv6_support_ && |
| 2093 | default_address_family_ == ADDRESS_FAMILY_IPV4) { |
| 2094 | // Don't return IPv6 addresses if default address family is set to IPv4, |
| 2095 | // and probes are disabled. |
| 2096 | *net_error = ERR_NAME_NOT_RESOLVED; |
| 2097 | } else if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED && |
| 2098 | key.address_family != family) { |
| 2099 | // Don't return IPv6 addresses for IPv4 queries, and vice versa. |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2100 | *net_error = ERR_NAME_NOT_RESOLVED; |
| 2101 | } else { |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2102 | *addresses = AddressList::CreateFromIPAddress(*ip_number, info.port()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2103 | if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 2104 | addresses->SetDefaultCanonicalName(); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2105 | } |
| 2106 | return true; |
| 2107 | } |
| 2108 | |
| 2109 | bool HostResolverImpl::ServeFromCache(const Key& key, |
| 2110 | const RequestInfo& info, |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2111 | int* net_error, |
| 2112 | AddressList* addresses) { |
| 2113 | DCHECK(addresses); |
| 2114 | DCHECK(net_error); |
| 2115 | if (!info.allow_cached_response() || !cache_.get()) |
| 2116 | return false; |
| 2117 | |
[email protected] | 407a30ab | 2012-08-15 17:16:10 | [diff] [blame] | 2118 | const HostCache::Entry* cache_entry = cache_->Lookup( |
| 2119 | key, base::TimeTicks::Now()); |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2120 | if (!cache_entry) |
| 2121 | return false; |
| 2122 | |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2123 | *net_error = cache_entry->error; |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2124 | if (*net_error == OK) { |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2125 | if (cache_entry->has_ttl()) |
| 2126 | RecordTTL(cache_entry->ttl); |
[email protected] | 89512322 | 2012-10-25 15:21:17 | [diff] [blame] | 2127 | *addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port()); |
[email protected] | 7054e78f | 2012-05-07 21:44:56 | [diff] [blame] | 2128 | } |
[email protected] | 95a214c | 2011-08-04 21:50:40 | [diff] [blame] | 2129 | return true; |
| 2130 | } |
| 2131 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2132 | bool HostResolverImpl::ServeFromHosts(const Key& key, |
| 2133 | const RequestInfo& info, |
| 2134 | AddressList* addresses) { |
| 2135 | DCHECK(addresses); |
| 2136 | if (!HaveDnsConfig()) |
| 2137 | return false; |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2138 | addresses->clear(); |
| 2139 | |
[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 2140 | // HOSTS lookups are case-insensitive. |
[email protected] | cb1f4ac | 2014-08-07 16:55:42 | [diff] [blame] | 2141 | std::string hostname = base::StringToLowerASCII(key.hostname); |
[email protected] | cb50762 | 2012-03-23 16:17:06 | [diff] [blame] | 2142 | |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2143 | const DnsHosts& hosts = dns_client_->GetConfig()->hosts; |
| 2144 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2145 | // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations |
| 2146 | // (glibc and c-ares) return the first matching line. We have more |
| 2147 | // flexibility, but lose implicit ordering. |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2148 | // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if |
| 2149 | // necessary. |
| 2150 | if (key.address_family == ADDRESS_FAMILY_IPV6 || |
| 2151 | key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2152 | DnsHosts::const_iterator it = hosts.find( |
| 2153 | DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6)); |
| 2154 | if (it != hosts.end()) |
| 2155 | addresses->push_back(IPEndPoint(it->second, info.port())); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2156 | } |
| 2157 | |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2158 | if (key.address_family == ADDRESS_FAMILY_IPV4 || |
| 2159 | key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2160 | DnsHosts::const_iterator it = hosts.find( |
| 2161 | DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4)); |
| 2162 | if (it != hosts.end()) |
| 2163 | addresses->push_back(IPEndPoint(it->second, info.port())); |
| 2164 | } |
| 2165 | |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 2166 | // If got only loopback addresses and the family was restricted, resolve |
| 2167 | // again, without restrictions. See SystemHostResolverCall for rationale. |
| 2168 | if ((key.host_resolver_flags & |
| 2169 | HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) && |
| 2170 | IsAllIPv4Loopback(*addresses)) { |
| 2171 | Key new_key(key); |
| 2172 | new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED; |
| 2173 | new_key.host_resolver_flags &= |
| 2174 | ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2175 | return ServeFromHosts(new_key, info, addresses); |
| 2176 | } |
[email protected] | 05a79d4 | 2013-03-28 07:30:09 | [diff] [blame] | 2177 | return !addresses->empty(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2178 | } |
| 2179 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2180 | void HostResolverImpl::CacheResult(const Key& key, |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2181 | const HostCache::Entry& entry, |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2182 | base::TimeDelta ttl) { |
| 2183 | if (cache_.get()) |
[email protected] | 1339a2a2 | 2012-10-17 08:39:43 | [diff] [blame] | 2184 | cache_->Set(key, entry, base::TimeTicks::Now(), ttl); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2185 | } |
| 2186 | |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2187 | void HostResolverImpl::RemoveJob(Job* job) { |
| 2188 | DCHECK(job); |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2189 | JobMap::iterator it = jobs_.find(job->key()); |
| 2190 | if (it != jobs_.end() && it->second == job) |
| 2191 | jobs_.erase(it); |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2192 | } |
| 2193 | |
[email protected] | 9936a786 | 2012-10-26 04:44:02 | [diff] [blame] | 2194 | void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { |
| 2195 | if (result) { |
| 2196 | additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 2197 | } else { |
| 2198 | additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 2199 | } |
| 2200 | } |
| 2201 | |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2202 | HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2203 | const RequestInfo& info, |
| 2204 | const IPAddressNumber* ip_number, |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2205 | const BoundNetLog& net_log) { |
[email protected] | eaf3a3b | 2010-09-03 20:34:27 | [diff] [blame] | 2206 | HostResolverFlags effective_flags = |
| 2207 | info.host_resolver_flags() | additional_resolver_flags_; |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2208 | AddressFamily effective_address_family = info.address_family(); |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2209 | |
| 2210 | if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) { |
pauljensen | 370f1c7 | 2015-02-17 16:59:14 | [diff] [blame] | 2211 | if (probe_ipv6_support_ && !use_local_ipv6_ && |
pauljensen | 19eb7342 | 2015-04-15 23:20:06 | [diff] [blame] | 2212 | // When resolving IPv4 literals, there's no need to probe for IPv6. |
| 2213 | // When resolving IPv6 literals, there's no benefit to artificially |
| 2214 | // limiting our resolution based on a probe. Prior logic ensures |
| 2215 | // that this query is UNSPECIFIED (see info.address_family() |
| 2216 | // check above) and that |default_address_family_| is UNSPECIFIED |
| 2217 | // (|prove_ipv6_support_| is false if |default_address_family_| is |
| 2218 | // set) so the code requesting the resolution should be amenable to |
| 2219 | // receiving a IPv6 resolution. |
| 2220 | ip_number == nullptr) { |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2221 | if (!IsIPv6Reachable(net_log)) { |
[email protected] | ac0b52e | 2013-04-21 01:26:16 | [diff] [blame] | 2222 | effective_address_family = ADDRESS_FAMILY_IPV4; |
| 2223 | effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2224 | } |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2225 | } else { |
[email protected] | ac0b52e | 2013-04-21 01:26:16 | [diff] [blame] | 2226 | effective_address_family = default_address_family_; |
[email protected] | 9db6f70 | 2013-04-10 18:10:51 | [diff] [blame] | 2227 | } |
| 2228 | } |
| 2229 | |
estark | 5d131a1 | 2015-03-26 19:52:19 | [diff] [blame] | 2230 | std::string hostname = info.hostname(); |
| 2231 | // Redirect .localhost queries to "localhost." to make sure that they |
| 2232 | // are never sent out on the network, per RFC 6761. |
| 2233 | if (IsLocalhostTLD(info.hostname())) |
| 2234 | hostname = kLocalhost; |
| 2235 | |
| 2236 | return Key(hostname, effective_address_family, effective_flags); |
[email protected] | 137af62 | 2010-02-05 02:14:35 | [diff] [blame] | 2237 | } |
| 2238 | |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2239 | bool HostResolverImpl::IsIPv6Reachable(const BoundNetLog& net_log) { |
| 2240 | base::TimeTicks now = base::TimeTicks::Now(); |
| 2241 | bool cached = true; |
| 2242 | if ((now - last_ipv6_probe_time_).InMilliseconds() > kIPv6ProbePeriodMs) { |
| 2243 | IPAddressNumber address(kIPv6ProbeAddress, |
| 2244 | kIPv6ProbeAddress + arraysize(kIPv6ProbeAddress)); |
| 2245 | last_ipv6_probe_result_ = IsGloballyReachable(address, net_log); |
| 2246 | last_ipv6_probe_time_ = now; |
| 2247 | cached = false; |
| 2248 | } |
| 2249 | net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK, |
| 2250 | base::Bind(&NetLogIPv6AvailableCallback, |
| 2251 | last_ipv6_probe_result_, cached)); |
| 2252 | return last_ipv6_probe_result_; |
| 2253 | } |
| 2254 | |
[email protected] | 35ddc28 | 2010-09-21 23:42:06 | [diff] [blame] | 2255 | void HostResolverImpl::AbortAllInProgressJobs() { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2256 | // In Abort, a Request callback could spawn new Jobs with matching keys, so |
| 2257 | // first collect and remove all running jobs from |jobs_|. |
[email protected] | c143d89 | 2012-04-06 07:56:54 | [diff] [blame] | 2258 | ScopedVector<Job> jobs_to_abort; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2259 | for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ) { |
| 2260 | Job* job = it->second; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2261 | if (job->is_running()) { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2262 | jobs_to_abort.push_back(job); |
| 2263 | jobs_.erase(it++); |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2264 | } else { |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2265 | DCHECK(job->is_queued()); |
| 2266 | ++it; |
[email protected] | 0f292de0 | 2012-02-01 22:28:20 | [diff] [blame] | 2267 | } |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2268 | } |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2269 | |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2270 | // Pause the dispatcher so it won't start any new dispatcher jobs while |
| 2271 | // aborting the old ones. This is needed so that it won't start the second |
| 2272 | // DnsTransaction for a job in |jobs_to_abort| if the DnsConfig just became |
| 2273 | // invalid. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2274 | PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits(); |
| 2275 | dispatcher_->SetLimits( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2276 | PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); |
[email protected] | 70c04ab | 2013-08-22 16:05:12 | [diff] [blame] | 2277 | |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2278 | // Life check to bail once |this| is deleted. |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 2279 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2280 | |
[email protected] | 16ee26d | 2012-03-08 03:34:35 | [diff] [blame] | 2281 | // Then Abort them. |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 2282 | for (size_t i = 0; self.get() && i < jobs_to_abort.size(); ++i) { |
[email protected] | 57a48d3 | 2012-03-03 00:04:55 | [diff] [blame] | 2283 | jobs_to_abort[i]->Abort(); |
[email protected] | c143d89 | 2012-04-06 07:56:54 | [diff] [blame] | 2284 | jobs_to_abort[i] = NULL; |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2285 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2286 | |
| 2287 | if (self) |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2288 | dispatcher_->SetLimits(limits); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | void HostResolverImpl::AbortDnsTasks() { |
| 2292 | // Pause the dispatcher so it won't start any new dispatcher jobs while |
| 2293 | // aborting the old ones. This is needed so that it won't start the second |
| 2294 | // DnsTransaction for a job if the DnsConfig just changed. |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2295 | PrioritizedDispatcher::Limits limits = dispatcher_->GetLimits(); |
| 2296 | dispatcher_->SetLimits( |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2297 | PrioritizedDispatcher::Limits(limits.reserved_slots.size(), 0)); |
| 2298 | |
| 2299 | for (JobMap::iterator it = jobs_.begin(); it != jobs_.end(); ++it) |
| 2300 | it->second->AbortDnsTask(); |
[email protected] | 106ccd2c | 2014-06-17 09:21:00 | [diff] [blame] | 2301 | dispatcher_->SetLimits(limits); |
[email protected] | ef4c40c | 2010-09-01 14:42:03 | [diff] [blame] | 2302 | } |
| 2303 | |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2304 | void HostResolverImpl::TryServingAllJobsFromHosts() { |
| 2305 | if (!HaveDnsConfig()) |
| 2306 | return; |
| 2307 | |
| 2308 | // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 2309 | // http://crbug.com/117655 |
| 2310 | |
| 2311 | // Life check to bail once |this| is deleted. |
[email protected] | 4589a3a | 2012-09-20 20:57:07 | [diff] [blame] | 2312 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2313 | |
[email protected] | 11fbca0b | 2013-06-02 23:37:21 | [diff] [blame] | 2314 | for (JobMap::iterator it = jobs_.begin(); self.get() && it != jobs_.end();) { |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2315 | Job* job = it->second; |
| 2316 | ++it; |
| 2317 | // This could remove |job| from |jobs_|, but iterator will remain valid. |
| 2318 | job->ServeFromHosts(); |
| 2319 | } |
| 2320 | } |
| 2321 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2322 | void HostResolverImpl::OnIPAddressChanged() { |
[email protected] | 62e86ba | 2013-01-29 18:59:16 | [diff] [blame] | 2323 | resolved_known_ipv6_hostname_ = false; |
sergeyu | b8cdc21 | 2015-05-14 18:50:37 | [diff] [blame] | 2324 | last_ipv6_probe_time_ = base::TimeTicks(); |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 2325 | // Abandon all ProbeJobs. |
| 2326 | probe_weak_ptr_factory_.InvalidateWeakPtrs(); |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2327 | if (cache_.get()) |
| 2328 | cache_->clear(); |
[email protected] | 7c466e9 | 2013-07-20 01:44:48 | [diff] [blame] | 2329 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | 12faa4c | 2012-11-06 04:44:18 | [diff] [blame] | 2330 | new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 2331 | #endif |
| 2332 | AbortAllInProgressJobs(); |
| 2333 | // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2334 | } |
| 2335 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2336 | void HostResolverImpl::OnInitialDNSConfigRead() { |
| 2337 | UpdateDNSConfig(false); |
| 2338 | } |
| 2339 | |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 2340 | void HostResolverImpl::OnDNSChanged() { |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2341 | UpdateDNSConfig(true); |
| 2342 | } |
| 2343 | |
| 2344 | void HostResolverImpl::UpdateDNSConfig(bool config_changed) { |
[email protected] | bb0e3454 | 2012-08-31 19:52:40 | [diff] [blame] | 2345 | DnsConfig dns_config; |
| 2346 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
[email protected] | ec666ab2 | 2013-04-17 20:05:59 | [diff] [blame] | 2347 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 2348 | if (net_log_) { |
| 2349 | net_log_->AddGlobalEntry( |
| 2350 | NetLog::TYPE_DNS_CONFIG_CHANGED, |
[email protected] | cd56514 | 2012-06-12 16:21:45 | [diff] [blame] | 2351 | base::Bind(&NetLogDnsConfigCallback, &dns_config)); |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 2352 | } |
| 2353 | |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2354 | // TODO(szym): Remove once http://crbug.com/137914 is resolved. |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 2355 | received_dns_config_ = dns_config.IsValid(); |
[email protected] | c9fa8f31 | 2013-09-17 12:24:52 | [diff] [blame] | 2356 | // Conservatively assume local IPv6 is needed when DnsConfig is not valid. |
| 2357 | use_local_ipv6_ = !dns_config.IsValid() || dns_config.use_local_ipv6; |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2358 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2359 | num_dns_failures_ = 0; |
| 2360 | |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2361 | // We want a new DnsSession in place, before we Abort running Jobs, so that |
| 2362 | // the newly started jobs use the new config. |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2363 | if (dns_client_.get()) { |
[email protected] | d7b9a2b | 2012-05-31 22:31:19 | [diff] [blame] | 2364 | dns_client_->SetConfig(dns_config); |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2365 | if (dns_client_->GetConfig()) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2366 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2367 | // If we just switched DnsClients, restart jobs using new resolver. |
| 2368 | // TODO(pauljensen): Is this necessary? |
| 2369 | config_changed = true; |
| 2370 | } |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2371 | } |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2372 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2373 | if (config_changed) { |
| 2374 | // If the DNS server has changed, existing cached info could be wrong so we |
| 2375 | // have to drop our internal cache :( Note that OS level DNS caches, such |
| 2376 | // as NSCD's cache should be dropped automatically by the OS when |
| 2377 | // resolv.conf changes so we don't need to do anything to clear that cache. |
| 2378 | if (cache_.get()) |
| 2379 | cache_->clear(); |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2380 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2381 | // Life check to bail once |this| is deleted. |
| 2382 | base::WeakPtr<HostResolverImpl> self = weak_ptr_factory_.GetWeakPtr(); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2383 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2384 | // Existing jobs will have been sent to the original server so they need to |
| 2385 | // be aborted. |
| 2386 | AbortAllInProgressJobs(); |
[email protected] | 01b3b9d | 2012-08-13 16:18:14 | [diff] [blame] | 2387 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 2388 | // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2389 | if (self.get()) |
| 2390 | TryServingAllJobsFromHosts(); |
| 2391 | } |
[email protected] | 78eac2a | 2012-03-14 19:09:27 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | bool HostResolverImpl::HaveDnsConfig() const { |
[email protected] | 32b1dbcf | 2013-01-26 03:48:25 | [diff] [blame] | 2395 | // Use DnsClient only if it's fully configured and there is no override by |
| 2396 | // ScopedDefaultHostResolverProc. |
| 2397 | // The alternative is to use NetworkChangeNotifier to override DnsConfig, |
| 2398 | // but that would introduce construction order requirements for NCN and SDHRP. |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 2399 | return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL) && |
| 2400 | !(proc_params_.resolver_proc.get() == NULL && |
[email protected] | 32b1dbcf | 2013-01-26 03:48:25 | [diff] [blame] | 2401 | HostResolverProc::GetDefault() != NULL); |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 2402 | } |
| 2403 | |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 2404 | void HostResolverImpl::OnDnsTaskResolve(int net_error) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2405 | DCHECK(dns_client_); |
[email protected] | 1ffdda8 | 2012-12-12 23:04:22 | [diff] [blame] | 2406 | if (net_error == OK) { |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2407 | num_dns_failures_ = 0; |
| 2408 | return; |
| 2409 | } |
| 2410 | ++num_dns_failures_; |
| 2411 | if (num_dns_failures_ < kMaximumDnsFailures) |
| 2412 | return; |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2413 | |
| 2414 | // Disable DnsClient until the next DNS change. Must be done before aborting |
| 2415 | // DnsTasks, since doing so may start new jobs. |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2416 | dns_client_->SetConfig(DnsConfig()); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2417 | |
| 2418 | // Switch jobs with active DnsTasks over to using ProcTasks. |
| 2419 | AbortDnsTasks(); |
| 2420 | |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2421 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", false); |
davidben | ad6fc44 | 2015-05-18 20:59:13 | [diff] [blame] | 2422 | UMA_HISTOGRAM_SPARSE_SLOWLY("AsyncDNS.DnsClientDisabledReason", |
| 2423 | std::abs(net_error)); |
[email protected] | f0f602bd | 2012-11-15 18:01:02 | [diff] [blame] | 2424 | } |
| 2425 | |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2426 | void HostResolverImpl::SetDnsClient(scoped_ptr<DnsClient> dns_client) { |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2427 | // DnsClient and config must be updated before aborting DnsTasks, since doing |
| 2428 | // so may start new jobs. |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2429 | dns_client_ = dns_client.Pass(); |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2430 | if (dns_client_ && !dns_client_->GetConfig() && |
| 2431 | num_dns_failures_ < kMaximumDnsFailures) { |
| 2432 | DnsConfig dns_config; |
| 2433 | NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2434 | dns_client_->SetConfig(dns_config); |
| 2435 | num_dns_failures_ = 0; |
| 2436 | if (dns_client_->GetConfig()) |
| 2437 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2438 | } |
[email protected] | daae132 | 2013-09-05 18:26:50 | [diff] [blame] | 2439 | |
| 2440 | AbortDnsTasks(); |
[email protected] | a8883e45 | 2012-11-17 05:58:06 | [diff] [blame] | 2441 | } |
| 2442 | |
[email protected] | b59ff37 | 2009-07-15 22:04:32 | [diff] [blame] | 2443 | } // namespace net |