OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_DNS_HOST_RESOLVER_H_ | |
6 #define NET_DNS_HOST_RESOLVER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "base/basictypes.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "net/base/address_family.h" | |
13 #include "net/base/completion_callback.h" | |
14 #include "net/base/host_port_pair.h" | |
15 #include "net/base/net_export.h" | |
16 #include "net/base/net_util.h" | |
17 #include "net/base/prioritized_dispatcher.h" | |
18 #include "net/base/request_priority.h" | |
19 | |
20 namespace base { | |
21 class Value; | |
22 } | |
23 | |
24 namespace net { | |
25 | |
26 class AddressList; | |
27 class BoundNetLog; | |
28 class HostCache; | |
29 class HostResolverProc; | |
30 class NetLog; | |
31 | |
32 // This class represents the task of resolving hostnames (or IP address | |
33 // literal) to an AddressList object. | |
34 // | |
35 // HostResolver can handle multiple requests at a time, so when cancelling a | |
36 // request the RequestHandle that was returned by Resolve() needs to be | |
37 // given. A simpler alternative for consumers that only have 1 outstanding | |
38 // request at a time is to create a SingleRequestHostResolver wrapper around | |
39 // HostResolver (which will automatically cancel the single request when it | |
40 // goes out of scope). | |
41 class NET_EXPORT HostResolver { | |
42 public: | |
43 // |max_concurrent_resolves| is how many resolve requests will be allowed to | |
44 // run in parallel. Pass HostResolver::kDefaultParallelism to choose a | |
45 // default value. | |
46 // |max_retry_attempts| is the maximum number of times we will retry for host | |
47 // resolution. Pass HostResolver::kDefaultRetryAttempts to choose a default | |
48 // value. | |
49 // |enable_caching| controls whether a HostCache is used. | |
50 struct NET_EXPORT Options { | |
51 Options(); | |
52 | |
53 PrioritizedDispatcher::Limits GetDispatcherLimits() const; | |
54 | |
55 size_t max_concurrent_resolves; | |
56 size_t max_retry_attempts; | |
57 bool enable_caching; | |
58 }; | |
59 | |
60 // The parameters for doing a Resolve(). A hostname and port are | |
61 // required; the rest are optional (and have reasonable defaults). | |
62 class NET_EXPORT RequestInfo { | |
63 public: | |
64 explicit RequestInfo(const HostPortPair& host_port_pair); | |
65 | |
66 const HostPortPair& host_port_pair() const { return host_port_pair_; } | |
67 void set_host_port_pair(const HostPortPair& host_port_pair) { | |
68 host_port_pair_ = host_port_pair; | |
69 } | |
70 | |
71 uint16 port() const { return host_port_pair_.port(); } | |
72 const std::string& hostname() const { return host_port_pair_.host(); } | |
73 | |
74 AddressFamily address_family() const { return address_family_; } | |
75 void set_address_family(AddressFamily address_family) { | |
76 address_family_ = address_family; | |
77 } | |
78 | |
79 HostResolverFlags host_resolver_flags() const { | |
80 return host_resolver_flags_; | |
81 } | |
82 void set_host_resolver_flags(HostResolverFlags host_resolver_flags) { | |
83 host_resolver_flags_ = host_resolver_flags; | |
84 } | |
85 | |
86 bool allow_cached_response() const { return allow_cached_response_; } | |
87 void set_allow_cached_response(bool b) { allow_cached_response_ = b; } | |
88 | |
89 bool is_speculative() const { return is_speculative_; } | |
90 void set_is_speculative(bool b) { is_speculative_ = b; } | |
91 | |
92 bool is_my_ip_address() const { return is_my_ip_address_; } | |
93 void set_is_my_ip_address(bool b) { is_my_ip_address_ = b; } | |
94 | |
95 private: | |
96 // The hostname to resolve, and the port to use in resulting sockaddrs. | |
97 HostPortPair host_port_pair_; | |
98 | |
99 // The address family to restrict results to. | |
100 AddressFamily address_family_; | |
101 | |
102 // Flags to use when resolving this request. | |
103 HostResolverFlags host_resolver_flags_; | |
104 | |
105 // Whether it is ok to return a result from the host cache. | |
106 bool allow_cached_response_; | |
107 | |
108 // Whether this request was started by the DNS prefetcher. | |
109 bool is_speculative_; | |
110 | |
111 // Indicates a request for myIpAddress (to differentiate from other requests | |
112 // for localhost, currently used by Chrome OS). | |
113 bool is_my_ip_address_; | |
114 }; | |
115 | |
116 // Opaque type used to cancel a request. | |
117 typedef void* RequestHandle; | |
118 | |
119 // Set Options.max_concurrent_resolves to this to select a default level | |
120 // of concurrency. | |
121 static const size_t kDefaultParallelism = 0; | |
122 | |
123 // Set Options.max_retry_attempts to this to select a default retry value. | |
124 static const size_t kDefaultRetryAttempts = static_cast<size_t>(-1); | |
125 | |
126 // If any completion callbacks are pending when the resolver is destroyed, | |
127 // the host resolutions are cancelled, and the completion callbacks will not | |
128 // be called. | |
129 virtual ~HostResolver(); | |
130 | |
131 // Resolves the given hostname (or IP address literal), filling out the | |
132 // |addresses| object upon success. The |info.port| parameter will be set as | |
133 // the sin(6)_port field of the sockaddr_in{6} struct. Returns OK if | |
134 // successful or an error code upon failure. Returns | |
135 // ERR_NAME_NOT_RESOLVED if hostname is invalid, or if it is an | |
136 // incompatible IP literal (e.g. IPv6 is disabled and it is an IPv6 | |
137 // literal). | |
138 // | |
139 // If the operation cannot be completed synchronously, ERR_IO_PENDING will | |
140 // be returned and the real result code will be passed to the completion | |
141 // callback. Otherwise the result code is returned immediately from this | |
142 // call. | |
143 // | |
144 // If |out_req| is non-NULL, then |*out_req| will be filled with a handle to | |
145 // the async request. This handle is not valid after the request has | |
146 // completed. | |
147 // | |
148 // Profiling information for the request is saved to |net_log| if non-NULL. | |
149 virtual int Resolve(const RequestInfo& info, | |
150 RequestPriority priority, | |
151 AddressList* addresses, | |
152 const CompletionCallback& callback, | |
153 RequestHandle* out_req, | |
154 const BoundNetLog& net_log) = 0; | |
155 | |
156 // Resolves the given hostname (or IP address literal) out of cache or HOSTS | |
157 // file (if enabled) only. This is guaranteed to complete synchronously. | |
158 // This acts like |Resolve()| if the hostname is IP literal, or cached value | |
159 // or HOSTS entry exists. Otherwise, ERR_DNS_CACHE_MISS is returned. | |
160 virtual int ResolveFromCache(const RequestInfo& info, | |
161 AddressList* addresses, | |
162 const BoundNetLog& net_log) = 0; | |
163 | |
164 // Cancels the specified request. |req| is the handle returned by Resolve(). | |
165 // After a request is canceled, its completion callback will not be called. | |
166 // CancelRequest must NOT be called after the request's completion callback | |
167 // has already run or the request was canceled. | |
168 virtual void CancelRequest(RequestHandle req) = 0; | |
169 | |
170 // Sets the default AddressFamily to use when requests have left it | |
171 // unspecified. For example, this could be used to restrict resolution | |
172 // results to AF_INET by passing in ADDRESS_FAMILY_IPV4, or to | |
173 // AF_INET6 by passing in ADDRESS_FAMILY_IPV6. | |
174 virtual void SetDefaultAddressFamily(AddressFamily address_family) {} | |
175 virtual AddressFamily GetDefaultAddressFamily() const; | |
176 | |
177 // Enable or disable the built-in asynchronous DnsClient. | |
178 virtual void SetDnsClientEnabled(bool enabled); | |
179 | |
180 // Returns the HostResolverCache |this| uses, or NULL if there isn't one. | |
181 // Used primarily to clear the cache and for getting debug information. | |
182 virtual HostCache* GetHostCache(); | |
183 | |
184 // Returns the current DNS configuration |this| is using, as a Value, or NULL | |
185 // if it's configured to always use the system host resolver. Caller takes | |
186 // ownership of the returned Value. | |
187 virtual base::Value* GetDnsConfigAsValue() const; | |
188 | |
189 // Creates a HostResolver implementation that queries the underlying system. | |
190 // (Except if a unit-test has changed the global HostResolverProc using | |
191 // ScopedHostResolverProc to intercept requests to the system). | |
192 static scoped_ptr<HostResolver> CreateSystemResolver( | |
193 const Options& options, | |
194 NetLog* net_log); | |
195 | |
196 // As above, but uses default parameters. | |
197 static scoped_ptr<HostResolver> CreateDefaultResolver(NetLog* net_log); | |
198 | |
199 protected: | |
200 HostResolver(); | |
201 | |
202 private: | |
203 DISALLOW_COPY_AND_ASSIGN(HostResolver); | |
204 }; | |
205 | |
206 } // namespace net | |
207 | |
208 #endif // NET_DNS_HOST_RESOLVER_H_ | |
OLD | NEW |