Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/dns/host_resolver_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <Winsock2.h> | 8 #include <Winsock2.h> |
| 9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 const size_t kSuffixLenTrimmed = kSuffixLen - 1; | 224 const size_t kSuffixLenTrimmed = kSuffixLen - 1; |
| 225 if (hostname.back() == '.') { | 225 if (hostname.back() == '.') { |
| 226 return hostname.size() > kSuffixLen && | 226 return hostname.size() > kSuffixLen && |
| 227 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix); | 227 !hostname.compare(hostname.size() - kSuffixLen, kSuffixLen, kSuffix); |
| 228 } | 228 } |
| 229 return hostname.size() > kSuffixLenTrimmed && | 229 return hostname.size() > kSuffixLenTrimmed && |
| 230 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed, | 230 !hostname.compare(hostname.size() - kSuffixLenTrimmed, kSuffixLenTrimmed, |
| 231 kSuffix, kSuffixLenTrimmed); | 231 kSuffix, kSuffixLenTrimmed); |
| 232 } | 232 } |
| 233 | 233 |
| 234 // Attempts to connect a UDP socket to |dest|:53. | |
| 235 bool IsGloballyReachable(const IPAddress& dest, | |
| 236 const NetLogWithSource& net_log) { | |
| 237 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed. | |
| 238 tracked_objects::ScopedTracker tracking_profile_1( | |
| 239 FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable")); | |
| 240 | |
| 241 std::unique_ptr<DatagramClientSocket> socket( | |
| 242 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( | |
| 243 DatagramSocket::DEFAULT_BIND, RandIntCallback(), net_log.net_log(), | |
| 244 net_log.source())); | |
| 245 int rv = socket->Connect(IPEndPoint(dest, 53)); | |
| 246 if (rv != OK) | |
| 247 return false; | |
| 248 IPEndPoint endpoint; | |
| 249 rv = socket->GetLocalAddress(&endpoint); | |
| 250 if (rv != OK) | |
| 251 return false; | |
| 252 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily()); | |
| 253 const IPAddress& address = endpoint.address(); | |
| 254 | |
| 255 bool is_link_local = | |
| 256 (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80); | |
| 257 if (is_link_local) | |
| 258 return false; | |
| 259 | |
| 260 const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0}; | |
| 261 if (IPAddressStartsWith(address, kTeredoPrefix)) | |
| 262 return false; | |
| 263 | |
| 264 return true; | |
| 265 } | |
| 266 | |
| 267 // Provide a common macro to simplify code and readability. We must use a | 234 // Provide a common macro to simplify code and readability. We must use a |
| 268 // macro as the underlying HISTOGRAM macro creates static variables. | 235 // macro as the underlying HISTOGRAM macro creates static variables. |
| 269 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ | 236 #define DNS_HISTOGRAM(name, time) UMA_HISTOGRAM_CUSTOM_TIMES(name, time, \ |
| 270 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100) | 237 base::TimeDelta::FromMilliseconds(1), base::TimeDelta::FromHours(1), 100) |
| 271 | 238 |
| 272 // A macro to simplify code and readability. | 239 // A macro to simplify code and readability. |
| 273 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \ | 240 #define DNS_HISTOGRAM_BY_PRIORITY(basename, priority, time) \ |
| 274 do { \ | 241 do { \ |
| 275 switch (priority) { \ | 242 switch (priority) { \ |
| 276 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \ | 243 case HIGHEST: DNS_HISTOGRAM(basename "_HIGHEST", time); break; \ |
| (...skipping 1796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2073 HostResolverImpl::HostResolverImpl( | 2040 HostResolverImpl::HostResolverImpl( |
| 2074 const Options& options, | 2041 const Options& options, |
| 2075 NetLog* net_log, | 2042 NetLog* net_log, |
| 2076 scoped_refptr<base::TaskRunner> worker_task_runner) | 2043 scoped_refptr<base::TaskRunner> worker_task_runner) |
| 2077 : max_queued_jobs_(0), | 2044 : max_queued_jobs_(0), |
| 2078 proc_params_(NULL, options.max_retry_attempts), | 2045 proc_params_(NULL, options.max_retry_attempts), |
| 2079 net_log_(net_log), | 2046 net_log_(net_log), |
| 2080 received_dns_config_(false), | 2047 received_dns_config_(false), |
| 2081 num_dns_failures_(0), | 2048 num_dns_failures_(0), |
| 2082 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), | 2049 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
| 2050 assume_ipv6_failure_on_wifi_(false), | |
| 2083 use_local_ipv6_(false), | 2051 use_local_ipv6_(false), |
| 2084 last_ipv6_probe_result_(true), | 2052 last_ipv6_probe_result_(true), |
| 2085 resolved_known_ipv6_hostname_(false), | 2053 resolved_known_ipv6_hostname_(false), |
| 2086 additional_resolver_flags_(0), | 2054 additional_resolver_flags_(0), |
| 2087 fallback_to_proctask_(true), | 2055 fallback_to_proctask_(true), |
| 2088 worker_task_runner_(std::move(worker_task_runner)), | 2056 worker_task_runner_(std::move(worker_task_runner)), |
| 2089 persist_initialized_(false), | 2057 persist_initialized_(false), |
| 2090 weak_ptr_factory_(this), | 2058 weak_ptr_factory_(this), |
| 2091 probe_weak_ptr_factory_(this) { | 2059 probe_weak_ptr_factory_(this) { |
| 2092 if (options.enable_caching) | 2060 if (options.enable_caching) |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2257 | 2225 |
| 2258 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { | 2226 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { |
| 2259 DCHECK(CalledOnValidThread()); | 2227 DCHECK(CalledOnValidThread()); |
| 2260 default_address_family_ = address_family; | 2228 default_address_family_ = address_family; |
| 2261 } | 2229 } |
| 2262 | 2230 |
| 2263 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { | 2231 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { |
| 2264 return default_address_family_; | 2232 return default_address_family_; |
| 2265 } | 2233 } |
| 2266 | 2234 |
| 2235 void HostResolverImpl::SetNoIPv6OnWifi() { | |
| 2236 DCHECK(CalledOnValidThread()); | |
| 2237 assume_ipv6_failure_on_wifi_ = true; | |
| 2238 } | |
| 2239 | |
| 2240 bool HostResolverImpl::GetNoIPv6OnWifi() { | |
| 2241 return assume_ipv6_failure_on_wifi_; | |
| 2242 } | |
| 2243 | |
| 2267 bool HostResolverImpl::ResolveAsIP(const Key& key, | 2244 bool HostResolverImpl::ResolveAsIP(const Key& key, |
| 2268 const RequestInfo& info, | 2245 const RequestInfo& info, |
| 2269 const IPAddress* ip_address, | 2246 const IPAddress* ip_address, |
| 2270 int* net_error, | 2247 int* net_error, |
| 2271 AddressList* addresses) { | 2248 AddressList* addresses) { |
| 2272 DCHECK(addresses); | 2249 DCHECK(addresses); |
| 2273 DCHECK(net_error); | 2250 DCHECK(net_error); |
| 2274 if (ip_address == nullptr) | 2251 if (ip_address == nullptr) |
| 2275 return false; | 2252 return false; |
| 2276 | 2253 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2435 // to receiving a IPv6 resolution. | 2412 // to receiving a IPv6 resolution. |
| 2436 !use_local_ipv6_ && ip_address == nullptr && !IsIPv6Reachable(net_log)) { | 2413 !use_local_ipv6_ && ip_address == nullptr && !IsIPv6Reachable(net_log)) { |
| 2437 effective_address_family = ADDRESS_FAMILY_IPV4; | 2414 effective_address_family = ADDRESS_FAMILY_IPV4; |
| 2438 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; | 2415 effective_flags |= HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2439 } | 2416 } |
| 2440 | 2417 |
| 2441 return Key(info.hostname(), effective_address_family, effective_flags); | 2418 return Key(info.hostname(), effective_address_family, effective_flags); |
| 2442 } | 2419 } |
| 2443 | 2420 |
| 2444 bool HostResolverImpl::IsIPv6Reachable(const NetLogWithSource& net_log) { | 2421 bool HostResolverImpl::IsIPv6Reachable(const NetLogWithSource& net_log) { |
| 2422 // Don't bother checking if we're on wifi and we're assuming that IPv6 doesn't | |
| 2423 // work on wifi. | |
| 2424 if (assume_ipv6_failure_on_wifi_ && | |
| 2425 current_connection_type_ == NetworkChangeNotifier::CONNECTION_WIFI) { | |
| 2426 return false; | |
| 2427 } | |
| 2428 | |
| 2445 // Cache the result for kIPv6ProbePeriodMs (measured from after | 2429 // Cache the result for kIPv6ProbePeriodMs (measured from after |
| 2446 // IsGloballyReachable() completes). | 2430 // IsGloballyReachable() completes). |
| 2447 bool cached = true; | 2431 bool cached = true; |
| 2448 if ((base::TimeTicks::Now() - last_ipv6_probe_time_).InMilliseconds() > | 2432 if ((base::TimeTicks::Now() - last_ipv6_probe_time_).InMilliseconds() > |
| 2449 kIPv6ProbePeriodMs) { | 2433 kIPv6ProbePeriodMs) { |
| 2450 last_ipv6_probe_result_ = | 2434 last_ipv6_probe_result_ = |
| 2451 IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log); | 2435 IsGloballyReachable(IPAddress(kIPv6ProbeAddress), net_log); |
| 2452 last_ipv6_probe_time_ = base::TimeTicks::Now(); | 2436 last_ipv6_probe_time_ = base::TimeTicks::Now(); |
| 2453 cached = false; | 2437 cached = false; |
| 2454 } | 2438 } |
| 2455 net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK, | 2439 net_log.AddEvent(NetLogEventType::HOST_RESOLVER_IMPL_IPV6_REACHABILITY_CHECK, |
| 2456 base::Bind(&NetLogIPv6AvailableCallback, | 2440 base::Bind(&NetLogIPv6AvailableCallback, |
| 2457 last_ipv6_probe_result_, cached)); | 2441 last_ipv6_probe_result_, cached)); |
| 2458 return last_ipv6_probe_result_; | 2442 return last_ipv6_probe_result_; |
| 2459 } | 2443 } |
| 2460 | 2444 |
| 2445 bool HostResolverImpl::IsGloballyReachable(const IPAddress& dest, | |
|
pauljensen
2017/04/12 18:16:40
why is this moved?
mgersh
2017/04/12 18:26:42
As I said in the CL description, I wanted tests to
pauljensen
2017/04/13 17:17:01
Ah, okay, sorry I didn't notice the "HostResolverI
| |
| 2446 const NetLogWithSource& net_log) { | |
| 2447 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed. | |
| 2448 tracked_objects::ScopedTracker tracking_profile_1( | |
| 2449 FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 IsGloballyReachable")); | |
| 2450 | |
| 2451 std::unique_ptr<DatagramClientSocket> socket( | |
| 2452 ClientSocketFactory::GetDefaultFactory()->CreateDatagramClientSocket( | |
| 2453 DatagramSocket::DEFAULT_BIND, RandIntCallback(), net_log.net_log(), | |
| 2454 net_log.source())); | |
| 2455 int rv = socket->Connect(IPEndPoint(dest, 53)); | |
| 2456 if (rv != OK) | |
| 2457 return false; | |
| 2458 IPEndPoint endpoint; | |
| 2459 rv = socket->GetLocalAddress(&endpoint); | |
| 2460 if (rv != OK) | |
| 2461 return false; | |
| 2462 DCHECK_EQ(ADDRESS_FAMILY_IPV6, endpoint.GetFamily()); | |
| 2463 const IPAddress& address = endpoint.address(); | |
| 2464 | |
| 2465 bool is_link_local = | |
| 2466 (address.bytes()[0] == 0xFE) && ((address.bytes()[1] & 0xC0) == 0x80); | |
| 2467 if (is_link_local) | |
| 2468 return false; | |
| 2469 | |
| 2470 const uint8_t kTeredoPrefix[] = {0x20, 0x01, 0, 0}; | |
| 2471 if (IPAddressStartsWith(address, kTeredoPrefix)) | |
| 2472 return false; | |
| 2473 | |
| 2474 return true; | |
| 2475 } | |
| 2476 | |
| 2461 void HostResolverImpl::RunLoopbackProbeJob() { | 2477 void HostResolverImpl::RunLoopbackProbeJob() { |
| 2462 new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr(), | 2478 new LoopbackProbeJob(weak_ptr_factory_.GetWeakPtr(), |
| 2463 worker_task_runner_.get()); | 2479 worker_task_runner_.get()); |
| 2464 } | 2480 } |
| 2465 | 2481 |
| 2466 void HostResolverImpl::AbortAllInProgressJobs() { | 2482 void HostResolverImpl::AbortAllInProgressJobs() { |
| 2467 // In Abort, a Request callback could spawn new Jobs with matching keys, so | 2483 // In Abort, a Request callback could spawn new Jobs with matching keys, so |
| 2468 // first collect and remove all running jobs from |jobs_|. | 2484 // first collect and remove all running jobs from |jobs_|. |
| 2469 std::vector<std::unique_ptr<Job>> jobs_to_abort; | 2485 std::vector<std::unique_ptr<Job>> jobs_to_abort; |
| 2470 for (auto it = jobs_.begin(); it != jobs_.end();) { | 2486 for (auto it = jobs_.begin(); it != jobs_.end();) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2546 // |this| may be deleted inside AbortAllInProgressJobs(). | 2562 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2547 } | 2563 } |
| 2548 | 2564 |
| 2549 void HostResolverImpl::OnConnectionTypeChanged( | 2565 void HostResolverImpl::OnConnectionTypeChanged( |
| 2550 NetworkChangeNotifier::ConnectionType type) { | 2566 NetworkChangeNotifier::ConnectionType type) { |
| 2551 proc_params_.unresponsive_delay = | 2567 proc_params_.unresponsive_delay = |
| 2552 GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( | 2568 GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 2553 "DnsUnresponsiveDelayMsByConnectionType", | 2569 "DnsUnresponsiveDelayMsByConnectionType", |
| 2554 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs), | 2570 base::TimeDelta::FromMilliseconds(kDnsDefaultUnresponsiveDelayMs), |
| 2555 type); | 2571 type); |
| 2572 current_connection_type_ = type; | |
| 2556 } | 2573 } |
| 2557 | 2574 |
| 2558 void HostResolverImpl::OnInitialDNSConfigRead() { | 2575 void HostResolverImpl::OnInitialDNSConfigRead() { |
| 2559 UpdateDNSConfig(false); | 2576 UpdateDNSConfig(false); |
| 2560 } | 2577 } |
| 2561 | 2578 |
| 2562 void HostResolverImpl::OnDNSChanged() { | 2579 void HostResolverImpl::OnDNSChanged() { |
| 2563 UpdateDNSConfig(true); | 2580 UpdateDNSConfig(true); |
| 2564 } | 2581 } |
| 2565 | 2582 |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2718 if (job_) | 2735 if (job_) |
| 2719 job_->CancelRequest(this); | 2736 job_->CancelRequest(this); |
| 2720 } | 2737 } |
| 2721 | 2738 |
| 2722 void HostResolverImpl::RequestImpl::ChangeRequestPriority( | 2739 void HostResolverImpl::RequestImpl::ChangeRequestPriority( |
| 2723 RequestPriority priority) { | 2740 RequestPriority priority) { |
| 2724 job_->ChangeRequestPriority(this, priority); | 2741 job_->ChangeRequestPriority(this, priority); |
| 2725 } | 2742 } |
| 2726 | 2743 |
| 2727 } // namespace net | 2744 } // namespace net |
| OLD | NEW |