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 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 } | 243 } |
244 | 244 |
245 //----------------------------------------------------------------------------- | 245 //----------------------------------------------------------------------------- |
246 | 246 |
247 AddressList EnsurePortOnAddressList(const AddressList& list, uint16 port) { | 247 AddressList EnsurePortOnAddressList(const AddressList& list, uint16 port) { |
248 if (list.empty() || list.front().port() == port) | 248 if (list.empty() || list.front().port() == port) |
249 return list; | 249 return list; |
250 return AddressList::CopyWithPort(list, port); | 250 return AddressList::CopyWithPort(list, port); |
251 } | 251 } |
252 | 252 |
| 253 // Returns true if |addresses| contains only IPv4 loopback addresses. |
| 254 bool IsAllIPv4Loopback(const AddressList& addresses) { |
| 255 for (unsigned i = 0; i < addresses.size(); ++i) { |
| 256 const IPAddressNumber& address = addresses[i].address(); |
| 257 switch (addresses[i].GetFamily()) { |
| 258 case ADDRESS_FAMILY_IPV4: |
| 259 if (address[0] != 127) |
| 260 return false; |
| 261 break; |
| 262 case ADDRESS_FAMILY_IPV6: |
| 263 return false; |
| 264 default: |
| 265 NOTREACHED(); |
| 266 return false; |
| 267 } |
| 268 } |
| 269 return true; |
| 270 } |
| 271 |
253 // Creates NetLog parameters when the resolve failed. | 272 // Creates NetLog parameters when the resolve failed. |
254 base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number, | 273 base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number, |
255 int net_error, | 274 int net_error, |
256 int os_error, | 275 int os_error, |
257 NetLog::LogLevel /* log_level */) { | 276 NetLog::LogLevel /* log_level */) { |
258 DictionaryValue* dict = new DictionaryValue(); | 277 DictionaryValue* dict = new DictionaryValue(); |
259 if (attempt_number) | 278 if (attempt_number) |
260 dict->SetInteger("attempt_number", attempt_number); | 279 dict->SetInteger("attempt_number", attempt_number); |
261 | 280 |
262 dict->SetInteger("net_error", net_error); | 281 dict->SetInteger("net_error", net_error); |
(...skipping 1680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1943 } | 1962 } |
1944 return true; | 1963 return true; |
1945 } | 1964 } |
1946 | 1965 |
1947 bool HostResolverImpl::ServeFromHosts(const Key& key, | 1966 bool HostResolverImpl::ServeFromHosts(const Key& key, |
1948 const RequestInfo& info, | 1967 const RequestInfo& info, |
1949 AddressList* addresses) { | 1968 AddressList* addresses) { |
1950 DCHECK(addresses); | 1969 DCHECK(addresses); |
1951 if (!HaveDnsConfig()) | 1970 if (!HaveDnsConfig()) |
1952 return false; | 1971 return false; |
1953 | |
1954 addresses->clear(); | 1972 addresses->clear(); |
1955 | 1973 |
1956 // HOSTS lookups are case-insensitive. | 1974 // HOSTS lookups are case-insensitive. |
1957 std::string hostname = StringToLowerASCII(key.hostname); | 1975 std::string hostname = StringToLowerASCII(key.hostname); |
1958 | 1976 |
1959 const DnsHosts& hosts = dns_client_->GetConfig()->hosts; | 1977 const DnsHosts& hosts = dns_client_->GetConfig()->hosts; |
1960 | 1978 |
1961 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations | 1979 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations |
1962 // (glibc and c-ares) return the first matching line. We have more | 1980 // (glibc and c-ares) return the first matching line. We have more |
1963 // flexibility, but lose implicit ordering. | 1981 // flexibility, but lose implicit ordering. |
1964 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if | 1982 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if |
1965 // necessary. | 1983 // necessary. |
1966 if (key.address_family == ADDRESS_FAMILY_IPV6 || | 1984 if (key.address_family == ADDRESS_FAMILY_IPV6 || |
1967 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { | 1985 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
1968 DnsHosts::const_iterator it = hosts.find( | 1986 DnsHosts::const_iterator it = hosts.find( |
1969 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6)); | 1987 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6)); |
1970 if (it != hosts.end()) | 1988 if (it != hosts.end()) |
1971 addresses->push_back(IPEndPoint(it->second, info.port())); | 1989 addresses->push_back(IPEndPoint(it->second, info.port())); |
1972 } | 1990 } |
1973 | 1991 |
1974 if (key.address_family == ADDRESS_FAMILY_IPV4 || | 1992 if (key.address_family == ADDRESS_FAMILY_IPV4 || |
1975 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { | 1993 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { |
1976 DnsHosts::const_iterator it = hosts.find( | 1994 DnsHosts::const_iterator it = hosts.find( |
1977 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4)); | 1995 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4)); |
1978 if (it != hosts.end()) | 1996 if (it != hosts.end()) |
1979 addresses->push_back(IPEndPoint(it->second, info.port())); | 1997 addresses->push_back(IPEndPoint(it->second, info.port())); |
1980 } | 1998 } |
1981 | 1999 |
| 2000 // If got only loopback addresses and the family was restricted, resolve |
| 2001 // again, without restrictions. See SystemHostResolverCall for rationale. |
| 2002 if ((key.host_resolver_flags & |
| 2003 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) && |
| 2004 IsAllIPv4Loopback(*addresses)) { |
| 2005 Key new_key(key); |
| 2006 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED; |
| 2007 new_key.host_resolver_flags &= |
| 2008 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6; |
| 2009 return ServeFromHosts(new_key, info, addresses); |
| 2010 } |
1982 return !addresses->empty(); | 2011 return !addresses->empty(); |
1983 } | 2012 } |
1984 | 2013 |
1985 void HostResolverImpl::CacheResult(const Key& key, | 2014 void HostResolverImpl::CacheResult(const Key& key, |
1986 const HostCache::Entry& entry, | 2015 const HostCache::Entry& entry, |
1987 base::TimeDelta ttl) { | 2016 base::TimeDelta ttl) { |
1988 if (cache_.get()) | 2017 if (cache_.get()) |
1989 cache_->Set(key, entry, base::TimeTicks::Now(), ttl); | 2018 cache_->Set(key, entry, base::TimeTicks::Now(), ttl); |
1990 } | 2019 } |
1991 | 2020 |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2111 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 2140 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
2112 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); | 2141 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); |
2113 #endif | 2142 #endif |
2114 AbortAllInProgressJobs(); | 2143 AbortAllInProgressJobs(); |
2115 // |this| may be deleted inside AbortAllInProgressJobs(). | 2144 // |this| may be deleted inside AbortAllInProgressJobs(). |
2116 } | 2145 } |
2117 | 2146 |
2118 void HostResolverImpl::OnDNSChanged() { | 2147 void HostResolverImpl::OnDNSChanged() { |
2119 DnsConfig dns_config; | 2148 DnsConfig dns_config; |
2120 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2149 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
| 2150 |
2121 if (net_log_) { | 2151 if (net_log_) { |
2122 net_log_->AddGlobalEntry( | 2152 net_log_->AddGlobalEntry( |
2123 NetLog::TYPE_DNS_CONFIG_CHANGED, | 2153 NetLog::TYPE_DNS_CONFIG_CHANGED, |
2124 base::Bind(&NetLogDnsConfigCallback, &dns_config)); | 2154 base::Bind(&NetLogDnsConfigCallback, &dns_config)); |
2125 } | 2155 } |
2126 | 2156 |
2127 // TODO(szym): Remove once http://crbug.com/137914 is resolved. | 2157 // TODO(szym): Remove once http://crbug.com/137914 is resolved. |
2128 received_dns_config_ = dns_config.IsValid(); | 2158 received_dns_config_ = dns_config.IsValid(); |
2129 | 2159 |
2130 num_dns_failures_ = 0; | 2160 num_dns_failures_ = 0; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2198 } | 2228 } |
2199 DnsConfig dns_config; | 2229 DnsConfig dns_config; |
2200 NetworkChangeNotifier::GetDnsConfig(&dns_config); | 2230 NetworkChangeNotifier::GetDnsConfig(&dns_config); |
2201 dns_client_->SetConfig(dns_config); | 2231 dns_client_->SetConfig(dns_config); |
2202 num_dns_failures_ = 0; | 2232 num_dns_failures_ = 0; |
2203 if (dns_config.IsValid()) | 2233 if (dns_config.IsValid()) |
2204 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2234 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
2205 } | 2235 } |
2206 | 2236 |
2207 } // namespace net | 2237 } // namespace net |
OLD | NEW |