OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_DNS_MDNS_CLIENT_IMPL_H_ | |
6 #define NET_DNS_MDNS_CLIENT_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/cancelable_callback.h" | |
14 #include "base/observer_list.h" | |
15 #include "net/base/io_buffer.h" | |
16 #include "net/base/ip_endpoint.h" | |
17 #include "net/dns/mdns_cache.h" | |
18 #include "net/dns/mdns_client.h" | |
19 #include "net/udp/datagram_server_socket.h" | |
20 #include "net/udp/udp_server_socket.h" | |
21 #include "net/udp/udp_socket.h" | |
22 | |
23 namespace net { | |
24 | |
25 class MDnsDatagramServerSocketFactory; | |
26 | |
27 // A connection to the network for multicast DNS clients. It reads data into | |
28 // DnsResponse objects and alerts the delegate that a packet has been received. | |
29 class MDnsConnection { | |
30 public: | |
31 class Delegate { | |
32 public: | |
33 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. | |
34 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; | |
35 virtual void OnConnectionError(int error) = 0; | |
36 virtual ~Delegate() {} | |
37 }; | |
38 | |
39 explicit MDnsConnection(MDnsDatagramServerSocketFactory* socket_factory, | |
40 MDnsConnection::Delegate* delegate); | |
41 | |
42 virtual ~MDnsConnection(); | |
43 | |
44 int Init(); | |
45 int Send(IOBuffer* buffer, unsigned size); | |
46 | |
47 private: | |
48 class SocketHandler { | |
49 public: | |
50 SocketHandler(MDnsConnection* connection, | |
51 const IPEndPoint& multicast_addr, | |
52 MDnsDatagramServerSocketFactory* socket_factory); | |
53 ~SocketHandler(); | |
54 int DoLoop(int rv); | |
55 int Start(); | |
56 | |
57 int Send(IOBuffer* buffer, unsigned size); | |
58 | |
59 private: | |
60 int BindSocket(); | |
61 void OnDatagramReceived(int rv); | |
62 | |
63 // Callback for when sending a query has finished. | |
64 void SendDone(int sent); | |
szym
2013/06/12 21:35:41
nit: |rv| would be better instead of |sent|.
Noam Samuel
2013/06/13 01:08:40
Done.
| |
65 | |
66 scoped_ptr<DatagramServerSocket> socket_; | |
67 | |
68 MDnsConnection* connection_; | |
69 IPEndPoint recv_addr_; | |
70 scoped_ptr<DnsResponse> response_; | |
71 IPEndPoint multicast_addr_; | |
72 }; | |
73 | |
74 // Callback for handling a datagram being received on either ipv4 or ipv6. | |
75 void OnDatagramReceived(DatagramServerSocket* socket, | |
76 DnsResponse* response, | |
77 IPEndPoint* recv_addr, | |
szym
2013/06/12 21:35:41
I suggest you pass IPEndPoint by const&. Why do yo
Noam Samuel
2013/06/13 01:08:40
Done.
| |
78 int bytes_read); | |
79 | |
80 void OnError(SocketHandler* loop, int error); | |
81 | |
82 IPEndPoint GetMDnsIPEndPoint(const char* address); | |
83 | |
84 SocketHandler socket_handler_ipv4_; | |
85 SocketHandler socket_handler_ipv6_; | |
86 | |
87 Delegate* delegate_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(MDnsConnection); | |
90 }; | |
91 | |
92 class MDnsListenerImpl; | |
93 | |
94 class MDnsClientImpl : public MDnsClient { | |
95 public: | |
96 // The core object exists while the MDnsClient is listening for MDnsPackets, | |
szym
2013/06/12 21:35:41
nit: No need for "MDnsPackets". It's also not clea
Noam Samuel
2013/06/13 01:08:40
Done.
| |
97 // and is deleted whenever the number of listeners reaches zero. | |
98 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { | |
99 public: | |
100 Core(MDnsClientImpl* client, | |
101 MDnsDatagramServerSocketFactory* socket_factory); | |
102 virtual ~Core(); | |
103 | |
104 // Initialize the core. Returns true on success. | |
105 bool Init(); | |
106 | |
107 // Send a query with a specific rrtype and name. Returns true on success. | |
108 bool SendQuery(uint16 rrtype, std::string name); | |
109 | |
110 // Add/remove a listener to the list of listener. May cause network traffic | |
111 // if listener is active. | |
112 void AddListener(MDnsListenerImpl* listener); | |
113 void RemoveListener(MDnsListenerImpl* listener); | |
114 | |
115 // Query the cache for records of a specific type and name. | |
116 void QueryCache(uint16 rrtype, const std::string& name, | |
117 std::vector<const RecordParsed*>* records) const; | |
118 | |
119 // Parse the response and alert relevant listeners. | |
120 virtual void HandlePacket(DnsResponse* response, int bytes_read) OVERRIDE; | |
121 | |
122 virtual void OnConnectionError(int error) OVERRIDE; | |
123 | |
124 private: | |
125 typedef std::pair<uint16, std::string> ListenerKey; | |
126 typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > | |
127 ListenerMap; | |
128 | |
129 // Alert listeners of an update to the cache. | |
130 void AlertListeners(MDnsUpdateType update_type, | |
131 const ListenerKey& key, const RecordParsed* record); | |
132 | |
133 // Schedule a cleanup to a specific time, cancelling other cleanups. | |
szym
2013/06/12 21:35:41
nit: add "cache"
Noam Samuel
2013/06/13 01:08:40
Done.
| |
134 void ScheduleCleanup(base::Time cleanup); | |
135 | |
136 // Clean up the cache and schedule a new cleanup. | |
137 void DoCleanup(); | |
138 | |
139 // Callback for when a record is removed from the cache. | |
140 void OnRecordRemoved(const RecordParsed* record); | |
141 | |
142 ListenerMap listeners_; | |
143 | |
144 MDnsClientImpl* client_; | |
145 MDnsCache cache_; | |
146 | |
147 base::CancelableCallback<void()> cleanup_callback_; | |
148 base::Time scheduled_cleanup_; | |
149 | |
150 scoped_ptr<MDnsConnection> connection_; | |
151 | |
152 DISALLOW_COPY_AND_ASSIGN(Core); | |
153 }; | |
154 | |
155 explicit MDnsClientImpl(MDnsDatagramServerSocketFactory* socket_factory_); | |
szym
2013/06/12 21:35:41
should be scoped_ptr
Noam Samuel
2013/06/13 01:08:40
Done.
| |
156 virtual ~MDnsClientImpl(); | |
157 | |
158 // MDnsClient implementation: | |
159 virtual scoped_ptr<MDnsListener> CreateListener( | |
160 uint16 rrtype, | |
161 const std::string& name, | |
162 MDnsListener::Delegate* delegate) OVERRIDE; | |
163 | |
164 virtual scoped_ptr<MDnsTransaction> CreateTransaction( | |
165 uint16 rrtype, | |
166 const std::string& name, | |
167 int flags, | |
168 const MDnsTransaction::ResultCallback& callback) OVERRIDE; | |
169 | |
170 // Functions for testing only. | |
szym
2013/06/12 21:35:41
nit: unnecessary comment. Better to explain what "
Noam Samuel
2013/06/13 01:08:40
Done.
| |
171 bool IsListeningForTests(); | |
172 | |
173 bool AddListenRef(); | |
174 void SubtractListenRef(); | |
175 | |
176 Core* core() { return core_.get(); } | |
177 | |
178 private: | |
179 void Shutdown(); | |
szym
2013/06/12 21:35:41
Add a comment that this is deferred shutdown after
Noam Samuel
2013/06/13 01:08:40
Done.
| |
180 | |
181 scoped_ptr<Core> core_; | |
182 int listen_refs_; | |
183 | |
184 scoped_ptr<MDnsDatagramServerSocketFactory> socket_factory_; | |
185 | |
186 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); | |
187 }; | |
188 | |
189 class MDnsListenerImpl : public MDnsListener, | |
190 public base::SupportsWeakPtr<MDnsListenerImpl> { | |
191 public: | |
192 MDnsListenerImpl(uint16 rrtype, | |
193 const std::string& name, | |
194 MDnsListener::Delegate* delegate, | |
195 MDnsClientImpl* client); | |
196 | |
197 virtual ~MDnsListenerImpl(); | |
198 | |
199 // MDnsListener implementation: | |
200 virtual bool Start() OVERRIDE; | |
201 | |
202 virtual const std::string& GetName() const OVERRIDE; | |
203 | |
204 virtual uint16 GetType() const OVERRIDE; | |
205 | |
206 MDnsListener::Delegate* delegate() { return delegate_; } | |
207 | |
208 // Alert the delegate of a record update. | |
209 void AlertDelegate(MDnsUpdateType update_type, | |
210 const RecordParsed* record_parsed); | |
211 private: | |
212 uint16 rrtype_; | |
213 std::string name_; | |
214 MDnsClientImpl* client_; | |
215 MDnsListener::Delegate* delegate_; | |
216 | |
217 bool started_; | |
218 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); | |
219 }; | |
220 | |
221 class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>, | |
222 public MDnsTransaction, | |
223 public MDnsListener::Delegate { | |
224 public: | |
225 MDnsTransactionImpl(uint16 rrtype, | |
226 const std::string& name, | |
227 int flags, | |
228 const MDnsTransaction::ResultCallback& callback, | |
229 MDnsClientImpl* client); | |
230 virtual ~MDnsTransactionImpl(); | |
231 | |
232 // MDnsTransaction implementation: | |
233 virtual bool Start() OVERRIDE; | |
234 | |
235 virtual const std::string& GetName() const OVERRIDE; | |
236 virtual uint16 GetType() const OVERRIDE; | |
237 | |
238 // MDnsListener::Delegate implementation: | |
239 virtual void OnRecordUpdate(MDnsUpdateType update, | |
240 const RecordParsed* record) OVERRIDE; | |
241 virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; | |
242 | |
243 virtual void OnCachePurged() OVERRIDE; | |
244 | |
245 private: | |
246 bool is_active() { return !callback_.is_null(); } | |
247 | |
248 void Reset(); | |
249 | |
250 // Trigger the callback and reset all related variables. | |
251 void TriggerCallback(MDnsTransactionResult result, | |
252 const RecordParsed* record); | |
253 | |
254 // Internal callback for when a cache record is found. | |
255 void CacheRecordFound(const RecordParsed* record); | |
256 | |
257 // Signal the transactionis over and release all related resources. | |
258 void SignalTransactionOver(); | |
259 | |
260 uint16 rrtype_; | |
261 std::string name_; | |
262 MDnsTransaction::ResultCallback callback_; | |
263 | |
264 scoped_ptr<MDnsListener> listener_; | |
265 base::CancelableCallback<void()> timeout_; | |
266 | |
267 MDnsClientImpl* client_; | |
268 | |
269 bool started_; | |
270 int flags_; | |
271 | |
272 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); | |
273 }; | |
274 | |
275 class MDnsDatagramServerSocketFactory { | |
szym
2013/06/12 21:35:41
Why not declare it first so that there's no need t
Noam Samuel
2013/06/13 01:08:40
Done.
| |
276 public: | |
277 virtual ~MDnsDatagramServerSocketFactory() {} | |
278 | |
279 virtual scoped_ptr<DatagramServerSocket> CreateSocket() = 0; | |
280 }; | |
281 | |
282 class MDnsDatagramServerSocketFactoryImpl | |
szym
2013/06/12 21:35:41
I'd suggest hiding this from the header and only e
Noam Samuel
2013/06/13 01:08:40
Done.
| |
283 : public MDnsDatagramServerSocketFactory { | |
284 public: | |
285 MDnsDatagramServerSocketFactoryImpl(); | |
286 virtual ~MDnsDatagramServerSocketFactoryImpl(); | |
287 | |
288 virtual scoped_ptr<DatagramServerSocket> CreateSocket() OVERRIDE; | |
289 }; | |
290 | |
291 } // namespace net | |
292 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ | |
OLD | NEW |