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

Side by Side Diff: net/dns/mdns_client_impl.h

Issue 15733008: Multicast DNS implementation (initial) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mdns_implementation2
Patch Set: Created 7 years, 6 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
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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_MDNS_CLIENT_IMPL_H_ 5 #ifndef NET_DNS_MDNS_CLIENT_IMPL_H_
6 #define NET_DNS_MDNS_CLIENT_IMPL_H_ 6 #define NET_DNS_MDNS_CLIENT_IMPL_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <utility> 10 #include <utility>
11 #include <vector> 11 #include <vector>
12 12
13 #include "base/cancelable_callback.h" 13 #include "base/cancelable_callback.h"
14 #include "base/observer_list.h" 14 #include "base/observer_list.h"
15 #include "net/base/io_buffer.h" 15 #include "net/base/io_buffer.h"
16 #include "net/base/ip_endpoint.h" 16 #include "net/base/ip_endpoint.h"
17 #include "net/dns/mdns_cache.h" 17 #include "net/dns/mdns_cache.h"
18 #include "net/dns/mdns_client.h" 18 #include "net/dns/mdns_client.h"
19 #include "net/udp/datagram_server_socket.h" 19 #include "net/udp/datagram_server_socket.h"
20 #include "net/udp/udp_server_socket.h" 20 #include "net/udp/udp_server_socket.h"
21 #include "net/udp/udp_socket.h" 21 #include "net/udp/udp_socket.h"
22 22
23 23
24 namespace net { 24 namespace net {
25 25
26 class MDnsListenerImpl; 26 class MDnsListenerImpl;
27 27
28 // This class represents a connection to the network for multicast DNS clients. 28 // A connection to the network for multicast DNS clients. It reads data into
29 // It reads data into DNSResponse objects and alerts the delegate that a packet 29 // DnsResponse objects and alerts the delegate that a packet has been received.
30 // has been received.
31 class MDnsConnection { 30 class MDnsConnection {
32 public: 31 public:
33 class Delegate { 32 class Delegate {
34 public: 33 public:
35 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. 34 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|.
36 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; 35 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0;
36 virtual void OnConnectionError(int error) = 0;
37 virtual ~Delegate() {} 37 virtual ~Delegate() {}
38 }; 38 };
39 39
40 // Start the connection. Will begin alerting the delegate of packets. 40 // Start the connection. Will begin alerting the delegate of packets.
41 virtual bool Init() = 0; 41 // Returns a net error code.
42 virtual int Init() = 0;
42 43
43 // Send a packet on both ipv4 and ipv6. 44 // Send a packet on both ipv4 and ipv6. Returns a net error code.
44 virtual bool Send(IOBuffer* buffer, unsigned size) = 0; 45 virtual int Send(IOBuffer* buffer, unsigned size) = 0;
45 virtual ~MDnsConnection() {} 46 virtual ~MDnsConnection() {}
46 }; 47 };
47 48
48 class MDnsConnectionFactory { 49 class MDnsConnectionFactory {
49 public: 50 public:
50 virtual scoped_ptr<MDnsConnection> CreateConnection( 51 virtual scoped_ptr<MDnsConnection> CreateConnection(
51 MDnsConnection::Delegate* delegate) = 0; 52 MDnsConnection::Delegate* delegate) = 0;
52 53
53 virtual ~MDnsConnectionFactory() {} 54 virtual ~MDnsConnectionFactory() {}
54 }; 55 };
(...skipping 11 matching lines...) Expand all
66 virtual ~Core(); 67 virtual ~Core();
67 68
68 // Initialize the core. Returns true on success. 69 // Initialize the core. Returns true on success.
69 bool Init(); 70 bool Init();
70 71
71 // Send a query with a specific rrtype and name. Returns true on success. 72 // Send a query with a specific rrtype and name. Returns true on success.
72 bool SendQuery(uint16 rrtype, std::string name); 73 bool SendQuery(uint16 rrtype, std::string name);
73 74
74 // Add/remove a listener to the list of listener. May cause network traffic 75 // Add/remove a listener to the list of listener. May cause network traffic
75 // if listener is active. 76 // if listener is active.
76 void AddListener(MDnsListenerImpl* listener, bool alert_existing_records); 77 void AddListener(MDnsListenerImpl* listener);
77 void RemoveListener(MDnsListenerImpl* listener); 78 void RemoveListener(MDnsListenerImpl* listener);
78 79
79 // Query the cache for records of a specific type and name. 80 // Query the cache for records of a specific type and name.
80 void QueryCache(uint16 rrtype, const std::string& name, 81 void QueryCache(uint16 rrtype, const std::string& name,
81 std::vector<const RecordParsed*>* records) const; 82 std::vector<const RecordParsed*>* records) const;
82 83
83 // Parse the response and alert relevant listeners. 84 // Parse the response and alert relevant listeners.
84 virtual void HandlePacket(DnsResponse* response, int bytes_read) OVERRIDE; 85 virtual void HandlePacket(DnsResponse* response, int bytes_read) OVERRIDE;
85 86
87 virtual void OnConnectionError(int error) OVERRIDE;
88
86 private: 89 private:
87 typedef std::pair<uint16, std::string> ListenerKey; 90 typedef std::pair<uint16, std::string> ListenerKey;
88 typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > 91 typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* >
89 ListenerMap; 92 ListenerMap;
90 93
91 // Alert listeners of an update to the cache. 94 // Alert listeners of an update to the cache.
92 void AlertListeners(MDnsUpdateType update_type, 95 void AlertListeners(MDnsUpdateType update_type,
93 const ListenerKey& key, const RecordParsed* record); 96 const ListenerKey& key, const RecordParsed* record);
94 97
95 // Schedule a cleanup to a specific time, cancelling other cleanups. 98 // Schedule a cleanup to a specific time, cancelling other cleanups.
(...skipping 18 matching lines...) Expand all
114 DISALLOW_COPY_AND_ASSIGN(Core); 117 DISALLOW_COPY_AND_ASSIGN(Core);
115 }; 118 };
116 119
117 MDnsClientImpl(); 120 MDnsClientImpl();
118 121
119 // Used only for testing. Lets users of this class decouple it from time 122 // Used only for testing. Lets users of this class decouple it from time
120 // dependence and network dependence. 123 // dependence and network dependence.
121 explicit MDnsClientImpl(MDnsConnectionFactory* connection_factory); 124 explicit MDnsClientImpl(MDnsConnectionFactory* connection_factory);
122 virtual ~MDnsClientImpl(); 125 virtual ~MDnsClientImpl();
123 126
124 // Add delegate for RRType |rrtype| and name |name|. 127 // MDnsClient implementation:
125 // If |name| is an empty string, listen to all notification of type
126 // |rrtype|.
127 virtual scoped_ptr<MDnsListener> CreateListener( 128 virtual scoped_ptr<MDnsListener> CreateListener(
128 uint16 rrtype, 129 uint16 rrtype,
129 const std::string& name, 130 const std::string& name,
130 bool active,
131 bool alert_existing_records,
132 MDnsListener::Delegate* delegate) OVERRIDE; 131 MDnsListener::Delegate* delegate) OVERRIDE;
133 132
134 // Create a transaction to Query MDNS for a single-value query
135 // (A, AAAA, TXT, and SRV) asynchronously. May defer to cache.
136 virtual scoped_ptr<MDnsTransaction> CreateTransaction( 133 virtual scoped_ptr<MDnsTransaction> CreateTransaction(
137 uint16 rrtype, 134 uint16 rrtype,
138 const std::string& name, 135 const std::string& name,
136 int flags,
139 const MDnsTransaction::ResultCallback& callback) OVERRIDE; 137 const MDnsTransaction::ResultCallback& callback) OVERRIDE;
140 138
139
141 // Functions for testing only. 140 // Functions for testing only.
142 bool IsListeningForTests(); 141 bool IsListeningForTests();
143 142
144 bool AddListenRef(); 143 bool AddListenRef();
145 void SubtractListenRef(); 144 void SubtractListenRef();
146 145
147 Core* core() { return core_.get(); } 146 Core* core() { return core_.get(); }
148 147
149 private: 148 private:
150 void Shutdown(); 149 void Shutdown();
151 150
152 scoped_ptr<Core> core_; 151 scoped_ptr<Core> core_;
153 int listen_refs_; 152 int listen_refs_;
154 153
155 // Since we can either own or not own the connection_factory, use a scoped_ptr 154 // Since we can either own or not own the connection_factory, use a scoped_ptr
156 // and a ptr. 155 // and a ptr.
157 scoped_ptr<MDnsConnectionFactory> connection_factory_owned_; 156 scoped_ptr<MDnsConnectionFactory> connection_factory_owned_;
158 MDnsConnectionFactory* connection_factory_; 157 MDnsConnectionFactory* connection_factory_;
159 158
160 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); 159 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl);
161 }; 160 };
162 161
163 class MDnsListenerImpl : public MDnsListener, 162 class MDnsListenerImpl : public MDnsListener,
164 public base::SupportsWeakPtr<MDnsListenerImpl> { 163 public base::SupportsWeakPtr<MDnsListenerImpl> {
165 public: 164 public:
166 MDnsListenerImpl(uint16 rrtype, 165 MDnsListenerImpl(uint16 rrtype,
167 const std::string& name, 166 const std::string& name,
168 bool active,
169 bool alert_existing_records,
170 MDnsListener::Delegate* delegate, 167 MDnsListener::Delegate* delegate,
171 MDnsClientImpl* client); 168 MDnsClientImpl* client);
172 169
173 // Destroying the listener stops listening.
174 virtual ~MDnsListenerImpl(); 170 virtual ~MDnsListenerImpl();
175 171
176 // Start the listener. Returns true on success. 172 // MDnsListener implementation:
177 virtual bool Start() OVERRIDE; 173 virtual bool Start() OVERRIDE;
178 174
179 // Get the host or service name for this query.
180 // Return an empty string for no name.
181 virtual const std::string& GetName() const OVERRIDE; 175 virtual const std::string& GetName() const OVERRIDE;
182 176
183 // Get the type for this query (SRV, TXT, A, AAA, etc)
184 virtual uint16 GetType() const OVERRIDE; 177 virtual uint16 GetType() const OVERRIDE;
185 178
186 virtual bool IsActive() const OVERRIDE;
187
188 // Applies only to listeners with names. Will send out a query for new
189 // information. |force_refresh_cache| will force a refresh of all cached
190 // entities.
191 virtual bool SendQuery(bool force_refresh_cache) OVERRIDE;
192
193 // Applies only to listeners with names. Query mDNS cache synchronously for
194 // either single- or multi- valued records.
195 virtual bool QueryCache(
196 std::vector<const RecordParsed*>* records) const OVERRIDE;
197
198 MDnsListener::Delegate* delegate() { return delegate_; } 179 MDnsListener::Delegate* delegate() { return delegate_; }
199 180
181 // Alert the delegate of a record update.
200 void AlertDelegate(MDnsUpdateType update_type, 182 void AlertDelegate(MDnsUpdateType update_type,
201 const RecordParsed* record_parsed); 183 const RecordParsed* record_parsed);
202 private: 184 private:
203 uint16 rrtype_; 185 uint16 rrtype_;
204 std::string name_; 186 std::string name_;
205 bool active_;
206 bool alert_existing_records_;
207 MDnsClientImpl* client_; 187 MDnsClientImpl* client_;
208 MDnsListener::Delegate* delegate_; 188 MDnsListener::Delegate* delegate_;
209 189
210 bool started_; 190 bool started_;
211 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); 191 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl);
212 }; 192 };
213 193
214 class MDnsTransactionImpl : public MDnsTransaction, 194 class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>,
215 public base::SupportsWeakPtr<MDnsTransactionImpl>, 195 public MDnsTransaction,
216 public MDnsListener::Delegate { 196 public MDnsListener::Delegate {
217 public: 197 public:
218 MDnsTransactionImpl(uint16 rrtype, 198 MDnsTransactionImpl(uint16 rrtype,
219 const std::string& name, 199 const std::string& name,
200 int flags,
220 const MDnsTransaction::ResultCallback& callback, 201 const MDnsTransaction::ResultCallback& callback,
221 MDnsClientImpl* client); 202 MDnsClientImpl* client);
222 virtual ~MDnsTransactionImpl(); 203 virtual ~MDnsTransactionImpl();
223 204
224 // Start the transaction. Returns true on success. 205 // MDnsTransaction implementation:
225 virtual bool Start() OVERRIDE; 206 virtual bool Start() OVERRIDE;
226 207
227 // MDnsListener::Delegate implementation 208 virtual const std::string& GetName() const OVERRIDE;
209 virtual uint16 GetType() const OVERRIDE;
210
211 // MDnsListener::Delegate implementation:
228 virtual void OnRecordUpdate(MDnsUpdateType update, 212 virtual void OnRecordUpdate(MDnsUpdateType update,
229 const RecordParsed* record) OVERRIDE; 213 const RecordParsed* record) OVERRIDE;
230 virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; 214 virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE;
231 215
232 virtual const std::string& GetName() const OVERRIDE; 216 virtual void OnCachePurged() OVERRIDE;
233 virtual uint16 GetType() const OVERRIDE; 217
218 bool is_active() { return !callback_.is_null(); }
219
220 void Reset();
szym 2013/06/10 21:58:28 Make it private.
Noam Samuel 2013/06/11 20:35:03 Done.
234 221
235 private: 222 private:
236 // Trigger the callback and reset all related variables. 223 // Trigger the callback and reset all related variables.
237 void TriggerCallback(MDnsTransactionResult result, 224 void TriggerCallback(MDnsTransactionResult result,
238 const RecordParsed* record); 225 const RecordParsed* record);
239 226
240 // Internal callback for when a cache record is found. 227 // Internal callback for when a cache record is found.
241 void CacheRecordFound(const RecordParsed* record); 228 void CacheRecordFound(const RecordParsed* record);
242 229
243 // Callback for when the transaction times out. 230 // Signal the transactionis over and release all related resources.
244 void OnTimedOut(); 231 void SignalTransactionOver();
245 232
246 uint16 rrtype_; 233 uint16 rrtype_;
247 std::string name_; 234 std::string name_;
248 MDnsTransaction::ResultCallback callback_; 235 MDnsTransaction::ResultCallback callback_;
249 236
250 scoped_ptr<MDnsListener> listener_; 237 scoped_ptr<MDnsListener> listener_;
251 base::CancelableCallback<void()> timeout_; 238 base::CancelableCallback<void()> timeout_;
252 239
253 MDnsClientImpl* client_; 240 MDnsClientImpl* client_;
254 241
255 bool started_; 242 bool started_;
243 int flags_;
256 244
257 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); 245 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl);
258 }; 246 };
259 247
260 // This implementation of MDnsConnection handles connecting to both IPv4 and 248 // This implementation of MDnsConnection handles connecting to both IPv4 and
261 // IPv6. 249 // IPv6.
262 class MDnsConnectionImpl : public MDnsConnection { 250 class MDnsConnectionImpl : public MDnsConnection {
263 public: 251 public:
264 explicit MDnsConnectionImpl(MDnsConnection::Delegate* delegate); 252 explicit MDnsConnectionImpl(MDnsConnection::Delegate* delegate);
253 MDnsConnectionImpl(DatagramServerSocket* socket_ipv4,
254 DatagramServerSocket* socket_ipv6,
255 MDnsConnection::Delegate* delegate);
256
265 virtual ~MDnsConnectionImpl(); 257 virtual ~MDnsConnectionImpl();
266 258
267 virtual bool Init() OVERRIDE; 259 virtual int Init() OVERRIDE;
268 virtual bool Send(IOBuffer* buffer, unsigned size) OVERRIDE; 260 virtual int Send(IOBuffer* buffer, unsigned size) OVERRIDE;
269 261
270 private: 262 private:
271 class RecvLoop { 263 class SocketHandler {
272 public: 264 public:
273 RecvLoop(DatagramServerSocket* socket, MDnsConnectionImpl* connection); 265 // Pass the socket in. Used for testing.
274 ~RecvLoop(); 266 SocketHandler(DatagramServerSocket* socket, MDnsConnectionImpl* connection,
275 void DoLoop(int rv); 267 const IPEndPoint& multicast_addr);
268 SocketHandler(MDnsConnectionImpl* connection,
269 const IPEndPoint& multicast_addr);
270 ~SocketHandler();
271 int DoLoop(int rv);
272 int Start();
273
274 int Send(IOBuffer* buffer, unsigned size);
276 275
277 private: 276 private:
277 int BindSocket();
278 void OnDatagramReceived(int rv); 278 void OnDatagramReceived(int rv);
279 279
280 // Callback for when sending a query has finished.
281 void SendDone(int sent);
282
283 scoped_ptr<DatagramServerSocket> socket_owned_;
280 DatagramServerSocket* socket_; 284 DatagramServerSocket* socket_;
285
281 MDnsConnectionImpl* connection_; 286 MDnsConnectionImpl* connection_;
282 IPEndPoint recv_addr_; 287 IPEndPoint recv_addr_;
283 scoped_ptr<DnsResponse> response_; 288 scoped_ptr<DnsResponse> response_;
289 IPEndPoint multicast_addr_;
284 }; 290 };
285 // Bind a socket with a specific address size to a specific multicast group
286 // and port 5353.
287 bool BindSocket(DatagramServerSocket* socket,
288 int addr_size,
289 const char* multicast_group);
290 291
291 // Callback for handling a datagram being recieved on either ipv4 or ipv6. 292 // Callback for handling a datagram being recieved on either ipv4 or ipv6.
292 // Responsible for ensuring we request another packet from the network. 293 // Responsible for ensuring we request another packet from the network.
293 void OnDatagramReceived(DatagramServerSocket* socket, 294 void OnDatagramReceived(DatagramServerSocket* socket,
294 DnsResponse* response, 295 DnsResponse* response,
295 IPEndPoint* recv_addr, 296 IPEndPoint* recv_addr,
296 int bytes_read); 297 int bytes_read);
297 298
298 void OnError(RecvLoop* loop, DatagramServerSocket* socket, int error); 299 void OnError(SocketHandler* loop, int error);
299 300
300 // Callback for when sending a query has finished. 301 IPEndPoint GetIPEndPoint(const char* address, int port);
301 void SendDone(int sent);
302 302
303 // The IPEndPoints for sending/recieving packets on IPv4 and IPv6. 303 SocketHandler socket_handler_ipv4_;
304 IPEndPoint GetIPv4SendEndpoint(); 304 SocketHandler socket_handler_ipv6_;
305 IPEndPoint GetIPv6SendEndpoint();
306
307 scoped_ptr<DatagramServerSocket> socket_ipv4_;
308 scoped_ptr<DatagramServerSocket> socket_ipv6_;
309
310 RecvLoop loop_ipv4_;
311 RecvLoop loop_ipv6_;
312 305
313 MDnsConnection::Delegate* delegate_; 306 MDnsConnection::Delegate* delegate_;
314 307
315 DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImpl); 308 DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImpl);
316 }; 309 };
317 310
318 class MDnsConnectionImplFactory : public MDnsConnectionFactory { 311 class MDnsConnectionImplFactory : public MDnsConnectionFactory {
319 public: 312 public:
320 MDnsConnectionImplFactory(); 313 MDnsConnectionImplFactory();
321 virtual ~MDnsConnectionImplFactory(); 314 virtual ~MDnsConnectionImplFactory();
322 315
323 virtual scoped_ptr<MDnsConnection> CreateConnection( 316 virtual scoped_ptr<MDnsConnection> CreateConnection(
324 MDnsConnection::Delegate* delegate) OVERRIDE; 317 MDnsConnection::Delegate* delegate) OVERRIDE;
325 318
326 private: 319 private:
327 DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImplFactory); 320 DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImplFactory);
328 }; 321 };
329 322
330 } 323 }
331 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ 324 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698