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 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 1931 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1942 | 1942 |
1943 IPAddress ip_address; | 1943 IPAddress ip_address; |
1944 IPAddress* ip_address_ptr = nullptr; | 1944 IPAddress* ip_address_ptr = nullptr; |
1945 if (ip_address.AssignFromIPLiteral(info.hostname())) | 1945 if (ip_address.AssignFromIPLiteral(info.hostname())) |
1946 ip_address_ptr = &ip_address; | 1946 ip_address_ptr = &ip_address; |
1947 | 1947 |
1948 // Build a key that identifies the request in the cache and in the | 1948 // Build a key that identifies the request in the cache and in the |
1949 // outstanding jobs map. | 1949 // outstanding jobs map. |
1950 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); | 1950 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); |
1951 | 1951 |
1952 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, source_net_log); | 1952 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, nullptr, |
| 1953 source_net_log); |
1953 if (rv != ERR_DNS_CACHE_MISS) { | 1954 if (rv != ERR_DNS_CACHE_MISS) { |
1954 LogFinishRequest(source_net_log, info, rv); | 1955 LogFinishRequest(source_net_log, info, rv); |
1955 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); | 1956 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); |
1956 return rv; | 1957 return rv; |
1957 } | 1958 } |
1958 | 1959 |
1959 // Next we need to attach our request to a "job". This job is responsible for | 1960 // Next we need to attach our request to a "job". This job is responsible for |
1960 // calling "getaddrinfo(hostname)" on a worker thread. | 1961 // calling "getaddrinfo(hostname)" on a worker thread. |
1961 | 1962 |
1962 JobMap::iterator jobit = jobs_.find(key); | 1963 JobMap::iterator jobit = jobs_.find(key); |
(...skipping 27 matching lines...) Expand all Loading... |
1990 | 1991 |
1991 job->AddRequest(std::move(req)); | 1992 job->AddRequest(std::move(req)); |
1992 // Completion happens during Job::CompleteRequests(). | 1993 // Completion happens during Job::CompleteRequests(). |
1993 return ERR_IO_PENDING; | 1994 return ERR_IO_PENDING; |
1994 } | 1995 } |
1995 | 1996 |
1996 int HostResolverImpl::ResolveHelper(const Key& key, | 1997 int HostResolverImpl::ResolveHelper(const Key& key, |
1997 const RequestInfo& info, | 1998 const RequestInfo& info, |
1998 const IPAddress* ip_address, | 1999 const IPAddress* ip_address, |
1999 AddressList* addresses, | 2000 AddressList* addresses, |
| 2001 HostCache::EntryStaleness* stale_info, |
2000 const BoundNetLog& source_net_log) { | 2002 const BoundNetLog& source_net_log) { |
2001 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. | 2003 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
2002 // On Windows it gives the default interface's address, whereas on Linux it | 2004 // On Windows it gives the default interface's address, whereas on Linux it |
2003 // gives an error. We will make it fail on all platforms for consistency. | 2005 // gives an error. We will make it fail on all platforms for consistency. |
2004 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) | 2006 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) |
2005 return ERR_NAME_NOT_RESOLVED; | 2007 return ERR_NAME_NOT_RESOLVED; |
2006 | 2008 |
2007 int net_error = ERR_UNEXPECTED; | 2009 int net_error = ERR_UNEXPECTED; |
2008 if (ResolveAsIP(key, info, ip_address, &net_error, addresses)) | 2010 if (ResolveAsIP(key, info, ip_address, &net_error, addresses)) |
2009 return net_error; | 2011 return net_error; |
2010 if (ServeFromCache(key, info, &net_error, addresses)) { | 2012 if (ServeFromCache(key, info, &net_error, addresses, stale_info)) { |
2011 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); | 2013 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); |
2012 return net_error; | 2014 return net_error; |
2013 } | 2015 } |
2014 // TODO(szym): Do not do this if nsswitch.conf instructs not to. | 2016 // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
2015 // http://crbug.com/117655 | 2017 // http://crbug.com/117655 |
2016 if (ServeFromHosts(key, info, addresses)) { | 2018 if (ServeFromHosts(key, info, addresses)) { |
2017 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); | 2019 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); |
2018 return OK; | 2020 return OK; |
2019 } | 2021 } |
2020 | 2022 |
2021 if (ServeLocalhost(key, info, addresses)) | 2023 if (ServeLocalhost(key, info, addresses)) |
2022 return OK; | 2024 return OK; |
2023 | 2025 |
2024 return ERR_DNS_CACHE_MISS; | 2026 return ERR_DNS_CACHE_MISS; |
2025 } | 2027 } |
2026 | 2028 |
2027 int HostResolverImpl::ResolveFromCache(const RequestInfo& info, | 2029 int HostResolverImpl::ResolveFromCache(const RequestInfo& info, |
2028 AddressList* addresses, | 2030 AddressList* addresses, |
2029 const BoundNetLog& source_net_log) { | 2031 const BoundNetLog& source_net_log) { |
| 2032 return ResolveStaleFromCache(info, addresses, nullptr, source_net_log); |
| 2033 } |
| 2034 |
| 2035 int HostResolverImpl::ResolveStaleFromCache( |
| 2036 const RequestInfo& info, |
| 2037 AddressList* addresses, |
| 2038 HostCache::EntryStaleness* stale_info, |
| 2039 const BoundNetLog& source_net_log) { |
2030 DCHECK(CalledOnValidThread()); | 2040 DCHECK(CalledOnValidThread()); |
2031 DCHECK(addresses); | 2041 DCHECK(addresses); |
2032 | 2042 |
2033 // Update the net log and notify registered observers. | 2043 // Update the net log and notify registered observers. |
2034 LogStartRequest(source_net_log, info); | 2044 LogStartRequest(source_net_log, info); |
2035 | 2045 |
2036 IPAddress ip_address; | 2046 IPAddress ip_address; |
2037 IPAddress* ip_address_ptr = nullptr; | 2047 IPAddress* ip_address_ptr = nullptr; |
2038 if (ip_address.AssignFromIPLiteral(info.hostname())) | 2048 if (ip_address.AssignFromIPLiteral(info.hostname())) |
2039 ip_address_ptr = &ip_address; | 2049 ip_address_ptr = &ip_address; |
2040 | 2050 |
2041 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); | 2051 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); |
2042 | 2052 |
2043 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, source_net_log); | 2053 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, stale_info, |
| 2054 source_net_log); |
2044 LogFinishRequest(source_net_log, info, rv); | 2055 LogFinishRequest(source_net_log, info, rv); |
2045 return rv; | 2056 return rv; |
2046 } | 2057 } |
2047 | 2058 |
2048 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { | 2059 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { |
2049 DCHECK(CalledOnValidThread()); | 2060 DCHECK(CalledOnValidThread()); |
2050 Request* req = reinterpret_cast<Request*>(req_handle); | 2061 Request* req = reinterpret_cast<Request*>(req_handle); |
2051 DCHECK(req); | 2062 DCHECK(req); |
2052 Job* job = req->job(); | 2063 Job* job = req->job(); |
2053 DCHECK(job); | 2064 DCHECK(job); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2103 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port()); | 2114 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port()); |
2104 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) | 2115 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) |
2105 addresses->SetDefaultCanonicalName(); | 2116 addresses->SetDefaultCanonicalName(); |
2106 } | 2117 } |
2107 return true; | 2118 return true; |
2108 } | 2119 } |
2109 | 2120 |
2110 bool HostResolverImpl::ServeFromCache(const Key& key, | 2121 bool HostResolverImpl::ServeFromCache(const Key& key, |
2111 const RequestInfo& info, | 2122 const RequestInfo& info, |
2112 int* net_error, | 2123 int* net_error, |
2113 AddressList* addresses) { | 2124 AddressList* addresses, |
| 2125 HostCache::EntryStaleness* stale_info) { |
2114 DCHECK(addresses); | 2126 DCHECK(addresses); |
2115 DCHECK(net_error); | 2127 DCHECK(net_error); |
2116 if (!info.allow_cached_response() || !cache_.get()) | 2128 if (!info.allow_cached_response() || !cache_.get()) |
2117 return false; | 2129 return false; |
2118 | 2130 |
2119 const HostCache::Entry* cache_entry = cache_->Lookup( | 2131 const HostCache::Entry* cache_entry; |
2120 key, base::TimeTicks::Now()); | 2132 if (stale_info) |
| 2133 cache_entry = cache_->LookupStale(key, base::TimeTicks::Now(), stale_info); |
| 2134 else |
| 2135 cache_entry = cache_->Lookup(key, base::TimeTicks::Now()); |
2121 if (!cache_entry) | 2136 if (!cache_entry) |
2122 return false; | 2137 return false; |
2123 | 2138 |
2124 *net_error = cache_entry->error; | 2139 *net_error = cache_entry->error; |
2125 if (*net_error == OK) { | 2140 if (*net_error == OK) { |
2126 if (cache_entry->has_ttl()) | 2141 if (cache_entry->has_ttl()) |
2127 RecordTTL(cache_entry->ttl); | 2142 RecordTTL(cache_entry->ttl); |
2128 *addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port()); | 2143 *addresses = EnsurePortOnAddressList(cache_entry->addrlist, info.port()); |
2129 } | 2144 } |
2130 return true; | 2145 return true; |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2462 dns_client_->SetConfig(dns_config); | 2477 dns_client_->SetConfig(dns_config); |
2463 num_dns_failures_ = 0; | 2478 num_dns_failures_ = 0; |
2464 if (dns_client_->GetConfig()) | 2479 if (dns_client_->GetConfig()) |
2465 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2480 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
2466 } | 2481 } |
2467 | 2482 |
2468 AbortDnsTasks(); | 2483 AbortDnsTasks(); |
2469 } | 2484 } |
2470 | 2485 |
2471 } // namespace net | 2486 } // namespace net |
OLD | NEW |