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 904 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 915 | 915 |
| 916 AddressList results_; | 916 AddressList results_; |
| 917 | 917 |
| 918 BoundNetLog net_log_; | 918 BoundNetLog net_log_; |
| 919 | 919 |
| 920 DISALLOW_COPY_AND_ASSIGN(ProcTask); | 920 DISALLOW_COPY_AND_ASSIGN(ProcTask); |
| 921 }; | 921 }; |
| 922 | 922 |
| 923 //----------------------------------------------------------------------------- | 923 //----------------------------------------------------------------------------- |
| 924 | 924 |
| 925 // Wraps a call to TestIPv6Support to be executed on the WorkerPool as it takes | |
| 926 // 40-100ms. | |
| 927 // TODO(szym): Remove altogether, if IPv6ActiveProbe works. | |
| 928 class HostResolverImpl::IPv6ProbeJob { | |
| 929 public: | |
| 930 IPv6ProbeJob(const base::WeakPtr<HostResolverImpl>& resolver, NetLog* net_log) | |
| 931 : resolver_(resolver), | |
| 932 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_IPV6_PROBE_JOB)), | |
|
mmenke
2013/07/16 20:17:21
Should remove this source type.
| |
| 933 result_(false, IPV6_SUPPORT_MAX, OK) { | |
| 934 DCHECK(resolver.get()); | |
| 935 net_log_.BeginEvent(NetLog::TYPE_IPV6_PROBE_RUNNING); | |
|
mmenke
2013/07/16 20:17:21
And this event type.
| |
| 936 const bool kIsSlow = true; | |
| 937 base::WorkerPool::PostTaskAndReply( | |
| 938 FROM_HERE, | |
| 939 base::Bind(&IPv6ProbeJob::DoProbe, base::Unretained(this)), | |
| 940 base::Bind(&IPv6ProbeJob::OnProbeComplete, base::Owned(this)), | |
| 941 kIsSlow); | |
| 942 } | |
| 943 | |
| 944 virtual ~IPv6ProbeJob() {} | |
| 945 | |
| 946 private: | |
| 947 // Runs on worker thread. | |
| 948 void DoProbe() { | |
| 949 result_ = TestIPv6Support(); | |
|
mmenke
2013/07/16 20:17:21
Think we should remove TestIPv6Support and IPv6Sup
| |
| 950 } | |
| 951 | |
| 952 void OnProbeComplete() { | |
| 953 net_log_.EndEvent(NetLog::TYPE_IPV6_PROBE_RUNNING, | |
| 954 base::Bind(&IPv6SupportResult::ToNetLogValue, | |
| 955 base::Unretained(&result_))); | |
| 956 if (!resolver_.get()) | |
| 957 return; | |
| 958 resolver_->IPv6ProbeSetDefaultAddressFamily( | |
| 959 result_.ipv6_supported ? ADDRESS_FAMILY_UNSPECIFIED | |
| 960 : ADDRESS_FAMILY_IPV4); | |
| 961 } | |
| 962 | |
| 963 // Used/set only on origin thread. | |
| 964 base::WeakPtr<HostResolverImpl> resolver_; | |
| 965 | |
| 966 BoundNetLog net_log_; | |
| 967 | |
| 968 IPv6SupportResult result_; | |
| 969 | |
| 970 DISALLOW_COPY_AND_ASSIGN(IPv6ProbeJob); | |
| 971 }; | |
| 972 | |
| 973 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as | 925 // Wraps a call to HaveOnlyLoopbackAddresses to be executed on the WorkerPool as |
| 974 // it takes 40-100ms and should not block initialization. | 926 // it takes 40-100ms and should not block initialization. |
| 975 class HostResolverImpl::LoopbackProbeJob { | 927 class HostResolverImpl::LoopbackProbeJob { |
| 976 public: | 928 public: |
| 977 explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver) | 929 explicit LoopbackProbeJob(const base::WeakPtr<HostResolverImpl>& resolver) |
| 978 : resolver_(resolver), | 930 : resolver_(resolver), |
| 979 result_(false) { | 931 result_(false) { |
| 980 DCHECK(resolver.get()); | 932 DCHECK(resolver.get()); |
| 981 const bool kIsSlow = true; | 933 const bool kIsSlow = true; |
| 982 base::WorkerPool::PostTaskAndReply( | 934 base::WorkerPool::PostTaskAndReply( |
| (...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1740 : cache_(cache.Pass()), | 1692 : cache_(cache.Pass()), |
| 1741 dispatcher_(job_limits), | 1693 dispatcher_(job_limits), |
| 1742 max_queued_jobs_(job_limits.total_jobs * 100u), | 1694 max_queued_jobs_(job_limits.total_jobs * 100u), |
| 1743 proc_params_(proc_params), | 1695 proc_params_(proc_params), |
| 1744 net_log_(net_log), | 1696 net_log_(net_log), |
| 1745 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), | 1697 default_address_family_(ADDRESS_FAMILY_UNSPECIFIED), |
| 1746 weak_ptr_factory_(this), | 1698 weak_ptr_factory_(this), |
| 1747 probe_weak_ptr_factory_(this), | 1699 probe_weak_ptr_factory_(this), |
| 1748 received_dns_config_(false), | 1700 received_dns_config_(false), |
| 1749 num_dns_failures_(0), | 1701 num_dns_failures_(0), |
| 1750 ipv6_probe_monitoring_(false), | 1702 probe_ipv6_support_(true), |
| 1751 resolved_known_ipv6_hostname_(false), | 1703 resolved_known_ipv6_hostname_(false), |
| 1752 additional_resolver_flags_(0), | 1704 additional_resolver_flags_(0), |
| 1753 fallback_to_proctask_(true) { | 1705 fallback_to_proctask_(true) { |
| 1754 | 1706 |
| 1755 DCHECK_GE(dispatcher_.num_priorities(), static_cast<size_t>(NUM_PRIORITIES)); | 1707 DCHECK_GE(dispatcher_.num_priorities(), static_cast<size_t>(NUM_PRIORITIES)); |
| 1756 | 1708 |
| 1757 // Maximum of 4 retry attempts for host resolution. | 1709 // Maximum of 4 retry attempts for host resolution. |
| 1758 static const size_t kDefaultMaxRetryAttempts = 4u; | 1710 static const size_t kDefaultMaxRetryAttempts = 4u; |
| 1759 | 1711 |
| 1760 if (proc_params_.max_retry_attempts == HostResolver::kDefaultRetryAttempts) | 1712 if (proc_params_.max_retry_attempts == HostResolver::kDefaultRetryAttempts) |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1920 Request* req = reinterpret_cast<Request*>(req_handle); | 1872 Request* req = reinterpret_cast<Request*>(req_handle); |
| 1921 DCHECK(req); | 1873 DCHECK(req); |
| 1922 Job* job = req->job(); | 1874 Job* job = req->job(); |
| 1923 DCHECK(job); | 1875 DCHECK(job); |
| 1924 job->CancelRequest(req); | 1876 job->CancelRequest(req); |
| 1925 } | 1877 } |
| 1926 | 1878 |
| 1927 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { | 1879 void HostResolverImpl::SetDefaultAddressFamily(AddressFamily address_family) { |
| 1928 DCHECK(CalledOnValidThread()); | 1880 DCHECK(CalledOnValidThread()); |
| 1929 default_address_family_ = address_family; | 1881 default_address_family_ = address_family; |
| 1930 ipv6_probe_monitoring_ = false; | 1882 probe_ipv6_support_ = false; |
| 1931 } | 1883 } |
| 1932 | 1884 |
| 1933 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { | 1885 AddressFamily HostResolverImpl::GetDefaultAddressFamily() const { |
| 1934 return default_address_family_; | 1886 return default_address_family_; |
| 1935 } | 1887 } |
| 1936 | 1888 |
| 1937 // TODO(szym): Remove this API altogether if IPv6ActiveProbe works. | |
| 1938 void HostResolverImpl::ProbeIPv6Support() { | |
| 1939 DCHECK(CalledOnValidThread()); | |
| 1940 DCHECK(!ipv6_probe_monitoring_); | |
| 1941 ipv6_probe_monitoring_ = true; | |
| 1942 OnIPAddressChanged(); | |
| 1943 } | |
| 1944 | |
| 1945 void HostResolverImpl::SetDnsClientEnabled(bool enabled) { | 1889 void HostResolverImpl::SetDnsClientEnabled(bool enabled) { |
| 1946 DCHECK(CalledOnValidThread()); | 1890 DCHECK(CalledOnValidThread()); |
| 1947 #if defined(ENABLE_BUILT_IN_DNS) | 1891 #if defined(ENABLE_BUILT_IN_DNS) |
| 1948 if (enabled && !dns_client_) { | 1892 if (enabled && !dns_client_) { |
| 1949 SetDnsClient(DnsClient::CreateClient(net_log_)); | 1893 SetDnsClient(DnsClient::CreateClient(net_log_)); |
| 1950 } else if (!enabled && dns_client_) { | 1894 } else if (!enabled && dns_client_) { |
| 1951 SetDnsClient(scoped_ptr<DnsClient>()); | 1895 SetDnsClient(scoped_ptr<DnsClient>()); |
| 1952 } | 1896 } |
| 1953 #endif | 1897 #endif |
| 1954 } | 1898 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 1979 DCHECK(net_error); | 1923 DCHECK(net_error); |
| 1980 IPAddressNumber ip_number; | 1924 IPAddressNumber ip_number; |
| 1981 if (!ParseIPLiteralToNumber(key.hostname, &ip_number)) | 1925 if (!ParseIPLiteralToNumber(key.hostname, &ip_number)) |
| 1982 return false; | 1926 return false; |
| 1983 | 1927 |
| 1984 DCHECK_EQ(key.host_resolver_flags & | 1928 DCHECK_EQ(key.host_resolver_flags & |
| 1985 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | | 1929 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | |
| 1986 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), | 1930 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), |
| 1987 0) << " Unhandled flag"; | 1931 0) << " Unhandled flag"; |
| 1988 bool ipv6_disabled = (default_address_family_ == ADDRESS_FAMILY_IPV4) && | 1932 bool ipv6_disabled = (default_address_family_ == ADDRESS_FAMILY_IPV4) && |
| 1989 !ipv6_probe_monitoring_; | 1933 !probe_ipv6_support_; |
| 1990 *net_error = OK; | 1934 *net_error = OK; |
| 1991 if ((ip_number.size() == kIPv6AddressSize) && ipv6_disabled) { | 1935 if ((ip_number.size() == kIPv6AddressSize) && ipv6_disabled) { |
| 1992 *net_error = ERR_NAME_NOT_RESOLVED; | 1936 *net_error = ERR_NAME_NOT_RESOLVED; |
| 1993 } else { | 1937 } else { |
| 1994 *addresses = AddressList::CreateFromIPAddress(ip_number, info.port()); | 1938 *addresses = AddressList::CreateFromIPAddress(ip_number, info.port()); |
| 1995 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) | 1939 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 1996 addresses->SetDefaultCanonicalName(); | 1940 addresses->SetDefaultCanonicalName(); |
| 1997 } | 1941 } |
| 1998 return true; | 1942 return true; |
| 1999 } | 1943 } |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2076 cache_->Set(key, entry, base::TimeTicks::Now(), ttl); | 2020 cache_->Set(key, entry, base::TimeTicks::Now(), ttl); |
| 2077 } | 2021 } |
| 2078 | 2022 |
| 2079 void HostResolverImpl::RemoveJob(Job* job) { | 2023 void HostResolverImpl::RemoveJob(Job* job) { |
| 2080 DCHECK(job); | 2024 DCHECK(job); |
| 2081 JobMap::iterator it = jobs_.find(job->key()); | 2025 JobMap::iterator it = jobs_.find(job->key()); |
| 2082 if (it != jobs_.end() && it->second == job) | 2026 if (it != jobs_.end() && it->second == job) |
| 2083 jobs_.erase(it); | 2027 jobs_.erase(it); |
| 2084 } | 2028 } |
| 2085 | 2029 |
| 2086 void HostResolverImpl::IPv6ProbeSetDefaultAddressFamily( | |
| 2087 AddressFamily address_family) { | |
| 2088 DCHECK(address_family == ADDRESS_FAMILY_UNSPECIFIED || | |
| 2089 address_family == ADDRESS_FAMILY_IPV4); | |
| 2090 if (!ipv6_probe_monitoring_) | |
| 2091 return; | |
| 2092 if (default_address_family_ != address_family) { | |
| 2093 VLOG(1) << "IPv6Probe forced AddressFamily setting to " | |
| 2094 << ((address_family == ADDRESS_FAMILY_UNSPECIFIED) ? | |
| 2095 "ADDRESS_FAMILY_UNSPECIFIED" : "ADDRESS_FAMILY_IPV4"); | |
| 2096 } | |
| 2097 default_address_family_ = address_family; | |
| 2098 } | |
| 2099 | |
| 2100 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { | 2030 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { |
| 2101 if (result) { | 2031 if (result) { |
| 2102 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; | 2032 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 2103 } else { | 2033 } else { |
| 2104 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; | 2034 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 2105 } | 2035 } |
| 2106 } | 2036 } |
| 2107 | 2037 |
| 2108 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( | 2038 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
| 2109 const RequestInfo& info) const { | 2039 const RequestInfo& info) const { |
| 2110 HostResolverFlags effective_flags = | 2040 HostResolverFlags effective_flags = |
| 2111 info.host_resolver_flags() | additional_resolver_flags_; | 2041 info.host_resolver_flags() | additional_resolver_flags_; |
| 2112 AddressFamily effective_address_family = info.address_family(); | 2042 AddressFamily effective_address_family = info.address_family(); |
| 2113 | 2043 |
| 2114 if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) { | 2044 if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2115 if (ipv6_probe_monitoring_) { | 2045 if (probe_ipv6_support_) { |
| 2116 base::TimeTicks start_time = base::TimeTicks::Now(); | 2046 base::TimeTicks start_time = base::TimeTicks::Now(); |
| 2117 // Google DNS address. | 2047 // Google DNS address. |
| 2118 const uint8 kIPv6Address[] = | 2048 const uint8 kIPv6Address[] = |
| 2119 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, | 2049 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, |
| 2120 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; | 2050 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; |
| 2121 IPAddressNumber address(kIPv6Address, | 2051 IPAddressNumber address(kIPv6Address, |
| 2122 kIPv6Address + arraysize(kIPv6Address)); | 2052 kIPv6Address + arraysize(kIPv6Address)); |
| 2123 bool rv6 = IsGloballyReachable(address); | 2053 bool rv6 = IsGloballyReachable(address); |
| 2124 | 2054 |
| 2125 UMA_HISTOGRAM_TIMES("Net.IPv6ConnectDuration", | 2055 UMA_HISTOGRAM_TIMES("Net.IPv6ConnectDuration", |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2187 job->ServeFromHosts(); | 2117 job->ServeFromHosts(); |
| 2188 } | 2118 } |
| 2189 } | 2119 } |
| 2190 | 2120 |
| 2191 void HostResolverImpl::OnIPAddressChanged() { | 2121 void HostResolverImpl::OnIPAddressChanged() { |
| 2192 resolved_known_ipv6_hostname_ = false; | 2122 resolved_known_ipv6_hostname_ = false; |
| 2193 // Abandon all ProbeJobs. | 2123 // Abandon all ProbeJobs. |
| 2194 probe_weak_ptr_factory_.InvalidateWeakPtrs(); | 2124 probe_weak_ptr_factory_.InvalidateWeakPtrs(); |
| 2195 if (cache_.get()) | 2125 if (cache_.get()) |
| 2196 cache_->clear(); | 2126 cache_->clear(); |
| 2197 if (ipv6_probe_monitoring_) | |
| 2198 new IPv6ProbeJob(probe_weak_ptr_factory_.GetWeakPtr(), net_log_); | |
| 2199 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 2127 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
| 2200 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); | 2128 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); |
| 2201 #endif | 2129 #endif |
| 2202 AbortAllInProgressJobs(); | 2130 AbortAllInProgressJobs(); |
| 2203 // |this| may be deleted inside AbortAllInProgressJobs(). | 2131 // |this| may be deleted inside AbortAllInProgressJobs(). |
| 2204 } | 2132 } |
| 2205 | 2133 |
| 2206 void HostResolverImpl::OnDNSChanged() { | 2134 void HostResolverImpl::OnDNSChanged() { |
| 2207 DnsConfig dns_config; | 2135 DnsConfig dns_config; |
| 2208 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2136 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2286 } | 2214 } |
| 2287 DnsConfig dns_config; | 2215 DnsConfig dns_config; |
| 2288 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2216 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2289 dns_client_->SetConfig(dns_config); | 2217 dns_client_->SetConfig(dns_config); |
| 2290 num_dns_failures_ = 0; | 2218 num_dns_failures_ = 0; |
| 2291 if (dns_config.IsValid()) | 2219 if (dns_config.IsValid()) |
| 2292 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2220 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
| 2293 } | 2221 } |
| 2294 | 2222 |
| 2295 } // namespace net | 2223 } // namespace net |
| OLD | NEW |