| 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 #ifndef NET_DNS_HOST_RESOLVER_IMPL_H_ | 5 #ifndef NET_DNS_HOST_RESOLVER_IMPL_H_ |
| 6 #define NET_DNS_HOST_RESOLVER_IMPL_H_ | 6 #define NET_DNS_HOST_RESOLVER_IMPL_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 namespace net { | 25 namespace net { |
| 26 | 26 |
| 27 class AddressList; | 27 class AddressList; |
| 28 class BoundNetLog; | 28 class BoundNetLog; |
| 29 class DnsClient; | 29 class DnsClient; |
| 30 class IPAddress; | 30 class IPAddress; |
| 31 class NetLog; | 31 class NetLog; |
| 32 | 32 |
| 33 // For each hostname that is requested, HostResolver creates a | 33 // For each hostname that is requested, HostResolver creates a |
| 34 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask | 34 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask |
| 35 // which runs the given HostResolverProc on a WorkerPool thread. If requests for | 35 // which runs the given HostResolverProc on a worker thread (a WorkerPool |
| 36 // that same host are made during the job's lifetime, they are attached to the | 36 // thread, in production code.) If requests for that same host are made during |
| 37 // existing job rather than creating a new one. This avoids doing parallel | 37 // the job's lifetime, they are attached to the existing job rather than |
| 38 // resolves for the same host. | 38 // creating a new one. This avoids doing parallel resolves for the same host. |
| 39 // | 39 // |
| 40 // The way these classes fit together is illustrated by: | 40 // The way these classes fit together is illustrated by: |
| 41 // | 41 // |
| 42 // | 42 // |
| 43 // +----------- HostResolverImpl -------------+ | 43 // +----------- HostResolverImpl -------------+ |
| 44 // | | | | 44 // | | | |
| 45 // Job Job Job | 45 // Job Job Job |
| 46 // (for host1, fam1) (for host2, fam2) (for hostx, famx) | 46 // (for host1, fam1) (for host2, fam2) (for hostx, famx) |
| 47 // / | | / | | / | | | 47 // / | | / | | / | | |
| 48 // Request ... Request Request ... Request Request ... Request | 48 // Request ... Request Request ... Request Request ... Request |
| (...skipping 12 matching lines...) Expand all Loading... |
| 61 class NET_EXPORT HostResolverImpl | 61 class NET_EXPORT HostResolverImpl |
| 62 : public HostResolver, | 62 : public HostResolver, |
| 63 NON_EXPORTED_BASE(public base::NonThreadSafe), | 63 NON_EXPORTED_BASE(public base::NonThreadSafe), |
| 64 public NetworkChangeNotifier::IPAddressObserver, | 64 public NetworkChangeNotifier::IPAddressObserver, |
| 65 public NetworkChangeNotifier::ConnectionTypeObserver, | 65 public NetworkChangeNotifier::ConnectionTypeObserver, |
| 66 public NetworkChangeNotifier::DNSObserver { | 66 public NetworkChangeNotifier::DNSObserver { |
| 67 public: | 67 public: |
| 68 // Parameters for ProcTask which resolves hostnames using HostResolveProc. | 68 // Parameters for ProcTask which resolves hostnames using HostResolveProc. |
| 69 // | 69 // |
| 70 // |resolver_proc| is used to perform the actual resolves; it must be | 70 // |resolver_proc| is used to perform the actual resolves; it must be |
| 71 // thread-safe since it is run from multiple worker threads. If | 71 // thread-safe since it may be run from multiple worker threads. If |
| 72 // |resolver_proc| is NULL then the default host resolver procedure is | 72 // |resolver_proc| is NULL then the default host resolver procedure is |
| 73 // used (which is SystemHostResolverProc except if overridden). | 73 // used (which is SystemHostResolverProc except if overridden). |
| 74 // | 74 // |
| 75 // For each attempt, we could start another attempt if host is not resolved | 75 // For each attempt, we could start another attempt if host is not resolved |
| 76 // within |unresponsive_delay| time. We keep attempting to resolve the host | 76 // within |unresponsive_delay| time. We keep attempting to resolve the host |
| 77 // for |max_retry_attempts|. For every retry attempt, we grow the | 77 // for |max_retry_attempts|. For every retry attempt, we grow the |
| 78 // |unresponsive_delay| by the |retry_factor| amount (that is retry interval | 78 // |unresponsive_delay| by the |retry_factor| amount (that is retry interval |
| 79 // is multiplied by the retry factor each time). Once we have retried | 79 // is multiplied by the retry factor each time). Once we have retried |
| 80 // |max_retry_attempts|, we give up on additional attempts. | 80 // |max_retry_attempts|, we give up on additional attempts. |
| 81 // | 81 // |
| (...skipping 14 matching lines...) Expand all Loading... |
| 96 size_t max_retry_attempts; | 96 size_t max_retry_attempts; |
| 97 | 97 |
| 98 // This is the limit after which we make another attempt to resolve the host | 98 // This is the limit after which we make another attempt to resolve the host |
| 99 // if the worker thread has not responded yet. | 99 // if the worker thread has not responded yet. |
| 100 base::TimeDelta unresponsive_delay; | 100 base::TimeDelta unresponsive_delay; |
| 101 | 101 |
| 102 // Factor to grow |unresponsive_delay| when we re-re-try. | 102 // Factor to grow |unresponsive_delay| when we re-re-try. |
| 103 uint32_t retry_factor; | 103 uint32_t retry_factor; |
| 104 }; | 104 }; |
| 105 | 105 |
| 106 // Creates a HostResolver as specified by |options|. | 106 // Creates a HostResolver as specified by |options|. Blocking tasks are run on |
| 107 // the WorkerPool. |
| 107 // | 108 // |
| 108 // If Options.enable_caching is true, a cache is created using | 109 // If Options.enable_caching is true, a cache is created using |
| 109 // HostCache::CreateDefaultCache(). Otherwise no cache is used. | 110 // HostCache::CreateDefaultCache(). Otherwise no cache is used. |
| 110 // | 111 // |
| 111 // Options.GetDispatcherLimits() determines the maximum number of jobs that | 112 // Options.GetDispatcherLimits() determines the maximum number of jobs that |
| 112 // the resolver will run at once. This upper-bounds the total number of | 113 // the resolver will run at once. This upper-bounds the total number of |
| 113 // outstanding DNS transactions (not counting retransmissions and retries). | 114 // outstanding DNS transactions (not counting retransmissions and retries). |
| 114 // | 115 // |
| 115 // |net_log| must remain valid for the life of the HostResolverImpl. | 116 // |net_log| must remain valid for the life of the HostResolverImpl. |
| 116 HostResolverImpl(const Options& options, NetLog* net_log); | 117 HostResolverImpl(const Options& options, NetLog* net_log); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 144 RequestPriority priority) override; | 145 RequestPriority priority) override; |
| 145 void CancelRequest(RequestHandle req) override; | 146 void CancelRequest(RequestHandle req) override; |
| 146 void SetDnsClientEnabled(bool enabled) override; | 147 void SetDnsClientEnabled(bool enabled) override; |
| 147 HostCache* GetHostCache() override; | 148 HostCache* GetHostCache() override; |
| 148 std::unique_ptr<base::Value> GetDnsConfigAsValue() const override; | 149 std::unique_ptr<base::Value> GetDnsConfigAsValue() const override; |
| 149 | 150 |
| 150 void set_proc_params_for_test(const ProcTaskParams& proc_params) { | 151 void set_proc_params_for_test(const ProcTaskParams& proc_params) { |
| 151 proc_params_ = proc_params; | 152 proc_params_ = proc_params; |
| 152 } | 153 } |
| 153 | 154 |
| 155 protected: |
| 156 // Just like the public constructor, but allows the task runner used for |
| 157 // blocking tasks to be specified. Intended for testing only. |
| 158 HostResolverImpl(const Options& options, |
| 159 NetLog* net_log, |
| 160 scoped_refptr<base::TaskRunner> worker_task_runner); |
| 161 |
| 162 // Callback from HaveOnlyLoopbackAddresses probe. |
| 163 void SetHaveOnlyLoopbackAddresses(bool result); |
| 164 |
| 154 private: | 165 private: |
| 155 friend class HostResolverImplTest; | 166 friend class HostResolverImplTest; |
| 156 class Job; | 167 class Job; |
| 157 class ProcTask; | 168 class ProcTask; |
| 158 class LoopbackProbeJob; | 169 class LoopbackProbeJob; |
| 159 class DnsTask; | 170 class DnsTask; |
| 160 class Request; | 171 class Request; |
| 161 typedef HostCache::Key Key; | 172 typedef HostCache::Key Key; |
| 162 typedef std::map<Key, Job*> JobMap; | 173 typedef std::map<Key, Job*> JobMap; |
| 163 | 174 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 bool ServeFromHosts(const Key& key, | 208 bool ServeFromHosts(const Key& key, |
| 198 const RequestInfo& info, | 209 const RequestInfo& info, |
| 199 AddressList* addresses); | 210 AddressList* addresses); |
| 200 | 211 |
| 201 // If |key| is for a localhost name (RFC 6761), returns true and fills | 212 // If |key| is for a localhost name (RFC 6761), returns true and fills |
| 202 // |addresses| with the loopback IP. Otherwise returns false. | 213 // |addresses| with the loopback IP. Otherwise returns false. |
| 203 bool ServeLocalhost(const Key& key, | 214 bool ServeLocalhost(const Key& key, |
| 204 const RequestInfo& info, | 215 const RequestInfo& info, |
| 205 AddressList* addresses); | 216 AddressList* addresses); |
| 206 | 217 |
| 207 // Callback from HaveOnlyLoopbackAddresses probe. | |
| 208 void SetHaveOnlyLoopbackAddresses(bool result); | |
| 209 | |
| 210 // Returns the (hostname, address_family) key to use for |info|, choosing an | 218 // Returns the (hostname, address_family) key to use for |info|, choosing an |
| 211 // "effective" address family by inheriting the resolver's default address | 219 // "effective" address family by inheriting the resolver's default address |
| 212 // family when the request leaves it unspecified. | 220 // family when the request leaves it unspecified. |
| 213 Key GetEffectiveKeyForRequest(const RequestInfo& info, | 221 Key GetEffectiveKeyForRequest(const RequestInfo& info, |
| 214 const IPAddress* ip_address, | 222 const IPAddress* ip_address, |
| 215 const BoundNetLog& net_log); | 223 const BoundNetLog& net_log); |
| 216 | 224 |
| 217 // Probes IPv6 support and returns true if IPv6 support is enabled. | 225 // Probes IPv6 support and returns true if IPv6 support is enabled. |
| 218 // Results are cached, i.e. when called repeatedly this method returns result | 226 // Results are cached, i.e. when called repeatedly this method returns result |
| 219 // from the first probe for some time before probing again. | 227 // from the first probe for some time before probing again. |
| 220 virtual bool IsIPv6Reachable(const BoundNetLog& net_log); | 228 virtual bool IsIPv6Reachable(const BoundNetLog& net_log); |
| 221 | 229 |
| 230 // Asynchronously checks if only loopback IPs are available. |
| 231 virtual void RunLoopbackProbeJob(); |
| 232 |
| 222 // Records the result in cache if cache is present. | 233 // Records the result in cache if cache is present. |
| 223 void CacheResult(const Key& key, | 234 void CacheResult(const Key& key, |
| 224 const HostCache::Entry& entry, | 235 const HostCache::Entry& entry, |
| 225 base::TimeDelta ttl); | 236 base::TimeDelta ttl); |
| 226 | 237 |
| 227 // Removes |job| from |jobs_|, only if it exists. | 238 // Removes |job| from |jobs_|, only if it exists. |
| 228 void RemoveJob(Job* job); | 239 void RemoveJob(Job* job); |
| 229 | 240 |
| 230 // Aborts all in progress jobs with ERR_NETWORK_CHANGED and notifies their | 241 // Aborts all in progress jobs with ERR_NETWORK_CHANGED and notifies their |
| 231 // requests. Might start new jobs. | 242 // requests. Might start new jobs. |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // True iff ProcTask has successfully resolved a hostname known to have IPv6 | 315 // True iff ProcTask has successfully resolved a hostname known to have IPv6 |
| 305 // addresses using ADDRESS_FAMILY_UNSPECIFIED. Reset on IP address change. | 316 // addresses using ADDRESS_FAMILY_UNSPECIFIED. Reset on IP address change. |
| 306 bool resolved_known_ipv6_hostname_; | 317 bool resolved_known_ipv6_hostname_; |
| 307 | 318 |
| 308 // Any resolver flags that should be added to a request by default. | 319 // Any resolver flags that should be added to a request by default. |
| 309 HostResolverFlags additional_resolver_flags_; | 320 HostResolverFlags additional_resolver_flags_; |
| 310 | 321 |
| 311 // Allow fallback to ProcTask if DnsTask fails. | 322 // Allow fallback to ProcTask if DnsTask fails. |
| 312 bool fallback_to_proctask_; | 323 bool fallback_to_proctask_; |
| 313 | 324 |
| 325 // Task runner used for DNS lookups using the platform resolver, and other |
| 326 // blocking operations. Usually just the WorkerPool's task runner for slow |
| 327 // tasks, but can be overridden for tests. |
| 328 scoped_refptr<base::TaskRunner> worker_task_runner_; |
| 329 |
| 314 base::WeakPtrFactory<HostResolverImpl> weak_ptr_factory_; | 330 base::WeakPtrFactory<HostResolverImpl> weak_ptr_factory_; |
| 315 | 331 |
| 316 base::WeakPtrFactory<HostResolverImpl> probe_weak_ptr_factory_; | 332 base::WeakPtrFactory<HostResolverImpl> probe_weak_ptr_factory_; |
| 317 | 333 |
| 318 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); | 334 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); |
| 319 }; | 335 }; |
| 320 | 336 |
| 321 // Resolves a local hostname (such as "localhost" or "localhost6") into | 337 // Resolves a local hostname (such as "localhost" or "localhost6") into |
| 322 // IP endpoints with the given port. Returns true if |host| is a local | 338 // IP endpoints with the given port. Returns true if |host| is a local |
| 323 // hostname and false otherwise. Special IPv6 names (e.g. "localhost6") | 339 // hostname and false otherwise. Special IPv6 names (e.g. "localhost6") |
| 324 // will resolve to an IPv6 address only, whereas other names will | 340 // will resolve to an IPv6 address only, whereas other names will |
| 325 // resolve to both IPv4 and IPv6. | 341 // resolve to both IPv4 and IPv6. |
| 326 // This function is only exposed so it can be unit-tested. | 342 // This function is only exposed so it can be unit-tested. |
| 327 // TODO(tfarina): It would be better to change the tests so this function | 343 // TODO(tfarina): It would be better to change the tests so this function |
| 328 // gets exercised indirectly through HostResolverImpl. | 344 // gets exercised indirectly through HostResolverImpl. |
| 329 NET_EXPORT_PRIVATE bool ResolveLocalHostname(base::StringPiece host, | 345 NET_EXPORT_PRIVATE bool ResolveLocalHostname(base::StringPiece host, |
| 330 uint16_t port, | 346 uint16_t port, |
| 331 AddressList* address_list); | 347 AddressList* address_list); |
| 332 | 348 |
| 333 } // namespace net | 349 } // namespace net |
| 334 | 350 |
| 335 #endif // NET_DNS_HOST_RESOLVER_IMPL_H_ | 351 #endif // NET_DNS_HOST_RESOLVER_IMPL_H_ |
| OLD | NEW |