| 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 1863 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1874 DCHECK(CalledOnValidThread()); | 1874 DCHECK(CalledOnValidThread()); |
| 1875 DCHECK_EQ(false, callback.is_null()); | 1875 DCHECK_EQ(false, callback.is_null()); |
| 1876 | 1876 |
| 1877 // Check that the caller supplied a valid hostname to resolve. | 1877 // Check that the caller supplied a valid hostname to resolve. |
| 1878 std::string labeled_hostname; | 1878 std::string labeled_hostname; |
| 1879 if (!DNSDomainFromDot(info.hostname(), &labeled_hostname)) | 1879 if (!DNSDomainFromDot(info.hostname(), &labeled_hostname)) |
| 1880 return ERR_NAME_NOT_RESOLVED; | 1880 return ERR_NAME_NOT_RESOLVED; |
| 1881 | 1881 |
| 1882 LogStartRequest(source_net_log, info); | 1882 LogStartRequest(source_net_log, info); |
| 1883 | 1883 |
| 1884 IPAddressNumber ip_number; |
| 1885 IPAddressNumber* ip_number_ptr = nullptr; |
| 1886 if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) |
| 1887 ip_number_ptr = &ip_number; |
| 1888 |
| 1884 // Build a key that identifies the request in the cache and in the | 1889 // Build a key that identifies the request in the cache and in the |
| 1885 // outstanding jobs map. | 1890 // outstanding jobs map. |
| 1886 Key key = GetEffectiveKeyForRequest(info, source_net_log); | 1891 Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log); |
| 1887 | 1892 |
| 1888 int rv = ResolveHelper(key, info, addresses, source_net_log); | 1893 int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log); |
| 1889 if (rv != ERR_DNS_CACHE_MISS) { | 1894 if (rv != ERR_DNS_CACHE_MISS) { |
| 1890 LogFinishRequest(source_net_log, info, rv); | 1895 LogFinishRequest(source_net_log, info, rv); |
| 1891 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); | 1896 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); |
| 1892 return rv; | 1897 return rv; |
| 1893 } | 1898 } |
| 1894 | 1899 |
| 1895 // Next we need to attach our request to a "job". This job is responsible for | 1900 // Next we need to attach our request to a "job". This job is responsible for |
| 1896 // calling "getaddrinfo(hostname)" on a worker thread. | 1901 // calling "getaddrinfo(hostname)" on a worker thread. |
| 1897 | 1902 |
| 1898 JobMap::iterator jobit = jobs_.find(key); | 1903 JobMap::iterator jobit = jobs_.find(key); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 1924 if (out_req) | 1929 if (out_req) |
| 1925 *out_req = reinterpret_cast<RequestHandle>(req.get()); | 1930 *out_req = reinterpret_cast<RequestHandle>(req.get()); |
| 1926 | 1931 |
| 1927 job->AddRequest(req.Pass()); | 1932 job->AddRequest(req.Pass()); |
| 1928 // Completion happens during Job::CompleteRequests(). | 1933 // Completion happens during Job::CompleteRequests(). |
| 1929 return ERR_IO_PENDING; | 1934 return ERR_IO_PENDING; |
| 1930 } | 1935 } |
| 1931 | 1936 |
| 1932 int HostResolverImpl::ResolveHelper(const Key& key, | 1937 int HostResolverImpl::ResolveHelper(const Key& key, |
| 1933 const RequestInfo& info, | 1938 const RequestInfo& info, |
| 1939 const IPAddressNumber* ip_number, |
| 1934 AddressList* addresses, | 1940 AddressList* addresses, |
| 1935 const BoundNetLog& source_net_log) { | 1941 const BoundNetLog& source_net_log) { |
| 1936 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. | 1942 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. |
| 1937 // On Windows it gives the default interface's address, whereas on Linux it | 1943 // On Windows it gives the default interface's address, whereas on Linux it |
| 1938 // gives an error. We will make it fail on all platforms for consistency. | 1944 // gives an error. We will make it fail on all platforms for consistency. |
| 1939 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) | 1945 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) |
| 1940 return ERR_NAME_NOT_RESOLVED; | 1946 return ERR_NAME_NOT_RESOLVED; |
| 1941 | 1947 |
| 1942 int net_error = ERR_UNEXPECTED; | 1948 int net_error = ERR_UNEXPECTED; |
| 1943 if (ResolveAsIP(key, info, &net_error, addresses)) | 1949 if (ResolveAsIP(key, info, ip_number, &net_error, addresses)) |
| 1944 return net_error; | 1950 return net_error; |
| 1945 if (ServeFromCache(key, info, &net_error, addresses)) { | 1951 if (ServeFromCache(key, info, &net_error, addresses)) { |
| 1946 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); | 1952 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); |
| 1947 return net_error; | 1953 return net_error; |
| 1948 } | 1954 } |
| 1949 // TODO(szym): Do not do this if nsswitch.conf instructs not to. | 1955 // TODO(szym): Do not do this if nsswitch.conf instructs not to. |
| 1950 // http://crbug.com/117655 | 1956 // http://crbug.com/117655 |
| 1951 if (ServeFromHosts(key, info, addresses)) { | 1957 if (ServeFromHosts(key, info, addresses)) { |
| 1952 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); | 1958 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); |
| 1953 return OK; | 1959 return OK; |
| 1954 } | 1960 } |
| 1955 return ERR_DNS_CACHE_MISS; | 1961 return ERR_DNS_CACHE_MISS; |
| 1956 } | 1962 } |
| 1957 | 1963 |
| 1958 int HostResolverImpl::ResolveFromCache(const RequestInfo& info, | 1964 int HostResolverImpl::ResolveFromCache(const RequestInfo& info, |
| 1959 AddressList* addresses, | 1965 AddressList* addresses, |
| 1960 const BoundNetLog& source_net_log) { | 1966 const BoundNetLog& source_net_log) { |
| 1961 DCHECK(CalledOnValidThread()); | 1967 DCHECK(CalledOnValidThread()); |
| 1962 DCHECK(addresses); | 1968 DCHECK(addresses); |
| 1963 | 1969 |
| 1964 // Update the net log and notify registered observers. | 1970 // Update the net log and notify registered observers. |
| 1965 LogStartRequest(source_net_log, info); | 1971 LogStartRequest(source_net_log, info); |
| 1966 | 1972 |
| 1967 Key key = GetEffectiveKeyForRequest(info, source_net_log); | 1973 IPAddressNumber ip_number; |
| 1974 IPAddressNumber* ip_number_ptr = nullptr; |
| 1975 if (ParseIPLiteralToNumber(info.hostname(), &ip_number)) |
| 1976 ip_number_ptr = &ip_number; |
| 1968 | 1977 |
| 1969 int rv = ResolveHelper(key, info, addresses, source_net_log); | 1978 Key key = GetEffectiveKeyForRequest(info, ip_number_ptr, source_net_log); |
| 1979 |
| 1980 int rv = ResolveHelper(key, info, ip_number_ptr, addresses, source_net_log); |
| 1970 LogFinishRequest(source_net_log, info, rv); | 1981 LogFinishRequest(source_net_log, info, rv); |
| 1971 return rv; | 1982 return rv; |
| 1972 } | 1983 } |
| 1973 | 1984 |
| 1974 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { | 1985 void HostResolverImpl::CancelRequest(RequestHandle req_handle) { |
| 1975 DCHECK(CalledOnValidThread()); | 1986 DCHECK(CalledOnValidThread()); |
| 1976 Request* req = reinterpret_cast<Request*>(req_handle); | 1987 Request* req = reinterpret_cast<Request*>(req_handle); |
| 1977 DCHECK(req); | 1988 DCHECK(req); |
| 1978 Job* job = req->job(); | 1989 Job* job = req->job(); |
| 1979 DCHECK(job); | 1990 DCHECK(job); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2014 // for it. | 2025 // for it. |
| 2015 const DnsConfig* dns_config = dns_client_->GetConfig(); | 2026 const DnsConfig* dns_config = dns_client_->GetConfig(); |
| 2016 if (dns_config == NULL) | 2027 if (dns_config == NULL) |
| 2017 return new base::DictionaryValue(); | 2028 return new base::DictionaryValue(); |
| 2018 | 2029 |
| 2019 return dns_config->ToValue(); | 2030 return dns_config->ToValue(); |
| 2020 } | 2031 } |
| 2021 | 2032 |
| 2022 bool HostResolverImpl::ResolveAsIP(const Key& key, | 2033 bool HostResolverImpl::ResolveAsIP(const Key& key, |
| 2023 const RequestInfo& info, | 2034 const RequestInfo& info, |
| 2035 const IPAddressNumber* ip_number, |
| 2024 int* net_error, | 2036 int* net_error, |
| 2025 AddressList* addresses) { | 2037 AddressList* addresses) { |
| 2026 DCHECK(addresses); | 2038 DCHECK(addresses); |
| 2027 DCHECK(net_error); | 2039 DCHECK(net_error); |
| 2028 IPAddressNumber ip_number; | 2040 if (ip_number == nullptr) |
| 2029 if (!ParseIPLiteralToNumber(key.hostname, &ip_number)) | |
| 2030 return false; | 2041 return false; |
| 2031 | 2042 |
| 2032 DCHECK_EQ(key.host_resolver_flags & | 2043 DCHECK_EQ(key.host_resolver_flags & |
| 2033 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | | 2044 ~(HOST_RESOLVER_CANONNAME | HOST_RESOLVER_LOOPBACK_ONLY | |
| 2034 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), | 2045 HOST_RESOLVER_DEFAULT_FAMILY_SET_DUE_TO_NO_IPV6), |
| 2035 0) << " Unhandled flag"; | 2046 0) << " Unhandled flag"; |
| 2036 | 2047 |
| 2037 *net_error = OK; | 2048 *net_error = OK; |
| 2038 AddressFamily family = GetAddressFamily(ip_number); | 2049 AddressFamily family = GetAddressFamily(*ip_number); |
| 2039 if (family == ADDRESS_FAMILY_IPV6 && | 2050 if (family == ADDRESS_FAMILY_IPV6 && |
| 2040 !probe_ipv6_support_ && | 2051 !probe_ipv6_support_ && |
| 2041 default_address_family_ == ADDRESS_FAMILY_IPV4) { | 2052 default_address_family_ == ADDRESS_FAMILY_IPV4) { |
| 2042 // Don't return IPv6 addresses if default address family is set to IPv4, | 2053 // Don't return IPv6 addresses if default address family is set to IPv4, |
| 2043 // and probes are disabled. | 2054 // and probes are disabled. |
| 2044 *net_error = ERR_NAME_NOT_RESOLVED; | 2055 *net_error = ERR_NAME_NOT_RESOLVED; |
| 2045 } else if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED && | 2056 } else if (key.address_family != ADDRESS_FAMILY_UNSPECIFIED && |
| 2046 key.address_family != family) { | 2057 key.address_family != family) { |
| 2047 // Don't return IPv6 addresses for IPv4 queries, and vice versa. | 2058 // Don't return IPv6 addresses for IPv4 queries, and vice versa. |
| 2048 *net_error = ERR_NAME_NOT_RESOLVED; | 2059 *net_error = ERR_NAME_NOT_RESOLVED; |
| 2049 } else { | 2060 } else { |
| 2050 *addresses = AddressList::CreateFromIPAddress(ip_number, info.port()); | 2061 *addresses = AddressList::CreateFromIPAddress(*ip_number, info.port()); |
| 2051 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) | 2062 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) |
| 2052 addresses->SetDefaultCanonicalName(); | 2063 addresses->SetDefaultCanonicalName(); |
| 2053 } | 2064 } |
| 2054 return true; | 2065 return true; |
| 2055 } | 2066 } |
| 2056 | 2067 |
| 2057 bool HostResolverImpl::ServeFromCache(const Key& key, | 2068 bool HostResolverImpl::ServeFromCache(const Key& key, |
| 2058 const RequestInfo& info, | 2069 const RequestInfo& info, |
| 2059 int* net_error, | 2070 int* net_error, |
| 2060 AddressList* addresses) { | 2071 AddressList* addresses) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2141 | 2152 |
| 2142 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { | 2153 void HostResolverImpl::SetHaveOnlyLoopbackAddresses(bool result) { |
| 2143 if (result) { | 2154 if (result) { |
| 2144 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; | 2155 additional_resolver_flags_ |= HOST_RESOLVER_LOOPBACK_ONLY; |
| 2145 } else { | 2156 } else { |
| 2146 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; | 2157 additional_resolver_flags_ &= ~HOST_RESOLVER_LOOPBACK_ONLY; |
| 2147 } | 2158 } |
| 2148 } | 2159 } |
| 2149 | 2160 |
| 2150 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( | 2161 HostResolverImpl::Key HostResolverImpl::GetEffectiveKeyForRequest( |
| 2151 const RequestInfo& info, const BoundNetLog& net_log) const { | 2162 const RequestInfo& info, |
| 2163 const IPAddressNumber* ip_number, |
| 2164 const BoundNetLog& net_log) const { |
| 2152 HostResolverFlags effective_flags = | 2165 HostResolverFlags effective_flags = |
| 2153 info.host_resolver_flags() | additional_resolver_flags_; | 2166 info.host_resolver_flags() | additional_resolver_flags_; |
| 2154 AddressFamily effective_address_family = info.address_family(); | 2167 AddressFamily effective_address_family = info.address_family(); |
| 2155 | 2168 |
| 2156 if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) { | 2169 if (info.address_family() == ADDRESS_FAMILY_UNSPECIFIED) { |
| 2157 unsigned char ip_number[4]; | |
| 2158 url::Component host_comp(0, info.hostname().size()); | |
| 2159 int num_components; | |
| 2160 if (probe_ipv6_support_ && !use_local_ipv6_ && | 2170 if (probe_ipv6_support_ && !use_local_ipv6_ && |
| 2161 // Don't bother IPv6 probing when resolving IPv4 literals. | 2171 // When resolving IPv4 literals, there's no need to probe for IPv6. |
| 2162 url::IPv4AddressToNumber(info.hostname().c_str(), host_comp, ip_number, | 2172 // When resolving IPv6 literals, there's no benefit to artificially |
| 2163 &num_components) != url::CanonHostInfo::IPV4) { | 2173 // limiting our resolution based on a probe. Prior logic ensures |
| 2174 // that this query is UNSPECIFIED (see info.address_family() |
| 2175 // check above) and that |default_address_family_| is UNSPECIFIED |
| 2176 // (|prove_ipv6_support_| is false if |default_address_family_| is |
| 2177 // set) so the code requesting the resolution should be amenable to |
| 2178 // receiving a IPv6 resolution. |
| 2179 ip_number == nullptr) { |
| 2164 // Google DNS address. | 2180 // Google DNS address. |
| 2165 const uint8 kIPv6Address[] = | 2181 const uint8 kIPv6Address[] = |
| 2166 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, | 2182 { 0x20, 0x01, 0x48, 0x60, 0x48, 0x60, 0x00, 0x00, |
| 2167 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; | 2183 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x88, 0x88 }; |
| 2168 IPAddressNumber address(kIPv6Address, | 2184 IPAddressNumber address(kIPv6Address, |
| 2169 kIPv6Address + arraysize(kIPv6Address)); | 2185 kIPv6Address + arraysize(kIPv6Address)); |
| 2170 BoundNetLog probe_net_log = BoundNetLog::Make( | 2186 BoundNetLog probe_net_log = BoundNetLog::Make( |
| 2171 net_log.net_log(), NetLog::SOURCE_IPV6_REACHABILITY_CHECK); | 2187 net_log.net_log(), NetLog::SOURCE_IPV6_REACHABILITY_CHECK); |
| 2172 probe_net_log.BeginEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK, | 2188 probe_net_log.BeginEvent(NetLog::TYPE_IPV6_REACHABILITY_CHECK, |
| 2173 net_log.source().ToEventParametersCallback()); | 2189 net_log.source().ToEventParametersCallback()); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2361 dns_client_->SetConfig(dns_config); | 2377 dns_client_->SetConfig(dns_config); |
| 2362 num_dns_failures_ = 0; | 2378 num_dns_failures_ = 0; |
| 2363 if (dns_client_->GetConfig()) | 2379 if (dns_client_->GetConfig()) |
| 2364 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); | 2380 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); |
| 2365 } | 2381 } |
| 2366 | 2382 |
| 2367 AbortDnsTasks(); | 2383 AbortDnsTasks(); |
| 2368 } | 2384 } |
| 2369 | 2385 |
| 2370 } // namespace net | 2386 } // namespace net |
| OLD | NEW |