Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(709)

Side by Side Diff: net/dns/host_resolver_impl.cc

Issue 14208010: [net/dns] Add test DualFamilyLocalhost and fix it when DnsClient enabled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Implement the needed logic in ServeFromHosts Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 bool IsAllLocalhostOfOneFamily(const AddressList& list) {
mmenke 2013/04/17 15:42:32 Think this function is worth a short description.
mmenke 2013/04/17 15:42:32 This function name seems to be inaccurate. We che
mmenke 2013/04/17 15:42:32 optional nit: Suggest using "addresses" rather th
szym 2013/04/17 17:09:16 Done.
szym 2013/04/17 17:09:16 Done. Also changed "localhost" to "loopback" which
szym 2013/04/17 17:09:16 Done.
254 const uint8 kIPv6Loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0,
255 0, 0, 0, 0, 0, 0, 0, 1 };
256 for (unsigned i = 0; i < list.size(); ++i) {
257 const IPAddressNumber& address = list[i].address();
258 switch (list[i].GetFamily()) {
mmenke 2013/04/17 15:42:32 nit: address.GetFamily()
szym 2013/04/17 17:09:16 GetFamily is a function of IPEndPoint. IPAddressNu
259 case ADDRESS_FAMILY_IPV4:
260 if (address[0] != 127) return false;
mmenke 2013/04/17 15:42:32 nit: Line break before "return".
szym 2013/04/17 17:09:16 Done.
261 break;
262 case ADDRESS_FAMILY_IPV6:
mmenke 2013/04/17 15:42:32 Do we even need to check for IPv6? I'm not partic
szym 2013/04/17 17:09:16 We don't, and this code path will not be exercised
mmenke 2013/04/17 17:35:00 I think we should go ahead and do that. If we nee
263 if (!std::equal(address.begin(), address.end(), kIPv6Loopback))
264 return false;
265 break;
266 default:
267 NOTREACHED();
268 }
269 }
270 return true;
271 }
272
253 // Creates NetLog parameters when the resolve failed. 273 // Creates NetLog parameters when the resolve failed.
254 base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number, 274 base::Value* NetLogProcTaskFailedCallback(uint32 attempt_number,
255 int net_error, 275 int net_error,
256 int os_error, 276 int os_error,
257 NetLog::LogLevel /* log_level */) { 277 NetLog::LogLevel /* log_level */) {
258 DictionaryValue* dict = new DictionaryValue(); 278 DictionaryValue* dict = new DictionaryValue();
259 if (attempt_number) 279 if (attempt_number)
260 dict->SetInteger("attempt_number", attempt_number); 280 dict->SetInteger("attempt_number", attempt_number);
261 281
262 dict->SetInteger("net_error", net_error); 282 dict->SetInteger("net_error", net_error);
(...skipping 1680 matching lines...) Expand 10 before | Expand all | Expand 10 after
1943 } 1963 }
1944 return true; 1964 return true;
1945 } 1965 }
1946 1966
1947 bool HostResolverImpl::ServeFromHosts(const Key& key, 1967 bool HostResolverImpl::ServeFromHosts(const Key& key,
1948 const RequestInfo& info, 1968 const RequestInfo& info,
1949 AddressList* addresses) { 1969 AddressList* addresses) {
1950 DCHECK(addresses); 1970 DCHECK(addresses);
1951 if (!HaveDnsConfig()) 1971 if (!HaveDnsConfig())
1952 return false; 1972 return false;
1953
1954 addresses->clear(); 1973 addresses->clear();
1955 1974
1956 // HOSTS lookups are case-insensitive. 1975 // HOSTS lookups are case-insensitive.
1957 std::string hostname = StringToLowerASCII(key.hostname); 1976 std::string hostname = StringToLowerASCII(key.hostname);
1958 1977
1959 const DnsHosts& hosts = dns_client_->GetConfig()->hosts; 1978 const DnsHosts& hosts = dns_client_->GetConfig()->hosts;
1960 1979
1961 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations 1980 // If |address_family| is ADDRESS_FAMILY_UNSPECIFIED other implementations
1962 // (glibc and c-ares) return the first matching line. We have more 1981 // (glibc and c-ares) return the first matching line. We have more
1963 // flexibility, but lose implicit ordering. 1982 // flexibility, but lose implicit ordering.
1964 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if 1983 // We prefer IPv6 because "happy eyeballs" will fall back to IPv4 if
1965 // necessary. 1984 // necessary.
1966 if (key.address_family == ADDRESS_FAMILY_IPV6 || 1985 if (key.address_family == ADDRESS_FAMILY_IPV6 ||
1967 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { 1986 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
1968 DnsHosts::const_iterator it = hosts.find( 1987 DnsHosts::const_iterator it = hosts.find(
1969 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6)); 1988 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV6));
1970 if (it != hosts.end()) 1989 if (it != hosts.end())
1971 addresses->push_back(IPEndPoint(it->second, info.port())); 1990 addresses->push_back(IPEndPoint(it->second, info.port()));
1972 } 1991 }
1973 1992
1974 if (key.address_family == ADDRESS_FAMILY_IPV4 || 1993 if (key.address_family == ADDRESS_FAMILY_IPV4 ||
1975 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) { 1994 key.address_family == ADDRESS_FAMILY_UNSPECIFIED) {
1976 DnsHosts::const_iterator it = hosts.find( 1995 DnsHosts::const_iterator it = hosts.find(
1977 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4)); 1996 DnsHostsKey(hostname, ADDRESS_FAMILY_IPV4));
1978 if (it != hosts.end()) 1997 if (it != hosts.end())
1979 addresses->push_back(IPEndPoint(it->second, info.port())); 1998 addresses->push_back(IPEndPoint(it->second, info.port()));
1980 } 1999 }
1981 2000
2001 // If got only loopback addresses and the family was restricted, resolve
2002 // again, without restrictions. See SystemHostResolverCall for rationale.
2003 if ((key.host_resolver_flags &
2004 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6) &&
2005 IsAllLocalhostOfOneFamily(*addresses)) {
2006 Key new_key(key);
2007 new_key.address_family = ADDRESS_FAMILY_UNSPECIFIED;
2008 new_key.host_resolver_flags &=
2009 ~HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6;
2010 return ServeFromHosts(new_key, info, addresses);
2011 }
1982 return !addresses->empty(); 2012 return !addresses->empty();
1983 } 2013 }
1984 2014
1985 void HostResolverImpl::CacheResult(const Key& key, 2015 void HostResolverImpl::CacheResult(const Key& key,
1986 const HostCache::Entry& entry, 2016 const HostCache::Entry& entry,
1987 base::TimeDelta ttl) { 2017 base::TimeDelta ttl) {
1988 if (cache_.get()) 2018 if (cache_.get())
1989 cache_->Set(key, entry, base::TimeTicks::Now(), ttl); 2019 cache_->Set(key, entry, base::TimeTicks::Now(), ttl);
1990 } 2020 }
1991 2021
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
2111 #if defined(OS_POSIX) && !defined(OS_MACOSX) 2141 #if defined(OS_POSIX) && !defined(OS_MACOSX)
2112 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr()); 2142 new LoopbackProbeJob(probe_weak_ptr_factory_.GetWeakPtr());
2113 #endif 2143 #endif
2114 AbortAllInProgressJobs(); 2144 AbortAllInProgressJobs();
2115 // |this| may be deleted inside AbortAllInProgressJobs(). 2145 // |this| may be deleted inside AbortAllInProgressJobs().
2116 } 2146 }
2117 2147
2118 void HostResolverImpl::OnDNSChanged() { 2148 void HostResolverImpl::OnDNSChanged() {
2119 DnsConfig dns_config; 2149 DnsConfig dns_config;
2120 NetworkChangeNotifier::GetDnsConfig(&dns_config); 2150 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2151
2121 if (net_log_) { 2152 if (net_log_) {
2122 net_log_->AddGlobalEntry( 2153 net_log_->AddGlobalEntry(
2123 NetLog::TYPE_DNS_CONFIG_CHANGED, 2154 NetLog::TYPE_DNS_CONFIG_CHANGED,
2124 base::Bind(&NetLogDnsConfigCallback, &dns_config)); 2155 base::Bind(&NetLogDnsConfigCallback, &dns_config));
2125 } 2156 }
2126 2157
2127 // TODO(szym): Remove once http://crbug.com/137914 is resolved. 2158 // TODO(szym): Remove once http://crbug.com/137914 is resolved.
2128 received_dns_config_ = dns_config.IsValid(); 2159 received_dns_config_ = dns_config.IsValid();
2129 2160
2130 num_dns_failures_ = 0; 2161 num_dns_failures_ = 0;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
2198 } 2229 }
2199 DnsConfig dns_config; 2230 DnsConfig dns_config;
2200 NetworkChangeNotifier::GetDnsConfig(&dns_config); 2231 NetworkChangeNotifier::GetDnsConfig(&dns_config);
2201 dns_client_->SetConfig(dns_config); 2232 dns_client_->SetConfig(dns_config);
2202 num_dns_failures_ = 0; 2233 num_dns_failures_ = 0;
2203 if (dns_config.IsValid()) 2234 if (dns_config.IsValid())
2204 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); 2235 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2205 } 2236 }
2206 2237
2207 } // namespace net 2238 } // namespace net
OLDNEW
« no previous file with comments | « no previous file | net/dns/host_resolver_impl_unittest.cc » ('j') | net/dns/host_resolver_impl_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698