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_LISTENER_IMPL_H_ | |
6 #define NET_DNS_MDNS_LISTENER_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/time/clock.h" | |
14 #include "base/task_runner.h" | |
15 #include "base/cancelable_callback.h" | |
16 #include "net/base/io_buffer.h" | |
17 #include "net/base/ip_endpoint.h" | |
18 #include "net/dns/mdns_cache.h" | |
19 #include "net/dns/mdns_listener.h" | |
20 #include "net/udp/udp_socket.h" | |
21 | |
22 namespace net { | |
23 | |
24 class MDnsListenerImpl; | |
25 | |
26 class MDnsListenerFactoryImpl : public MDnsListenerFactory { | |
27 public: | |
28 class Core : public base::SupportsWeakPtr<Core> { | |
szym
2013/05/24 15:32:22
Needs comment regarding lifetime. When is Core cre
Noam Samuel
2013/05/24 19:00:49
The weak pointer-ability of these classes is mostl
szym
2013/05/25 02:51:01
Still needs a comment in the code.
On 2013/05/24
Noam Samuel
2013/05/29 21:25:16
Done.
| |
29 public: | |
30 explicit Core(MDnsListenerFactoryImpl* factory); | |
31 ~Core(); | |
32 | |
33 // Initialize the core. Returns true on success. | |
34 bool Init(); | |
35 | |
36 // Send a query with a specific rrtype and name. Returns true on success. | |
37 bool SendQuery(uint16 rrtype, std::string name); | |
38 | |
39 // Add/remove a listener to the list of listener. May cause network traffic | |
40 // if listener is active. | |
41 void AddListener(MDnsListenerImpl* listener, bool alert_existing_records); | |
42 void RemoveListener(MDnsListenerImpl* listener); | |
43 | |
44 // Query the cache for records of a specific type and name. | |
45 void QueryCache(uint16 rrtype, const std::string& name, | |
46 std::vector<const RecordParsed*>* records) const; | |
47 | |
48 private: | |
49 typedef std::pair<uint16, std::string> ListenerKey; | |
50 | |
51 typedef std::multimap<ListenerKey, MDnsListenerImpl*>::iterator | |
52 ListenersIterator; | |
53 | |
54 // Bind a socket with a specific address size to a specific multicast group | |
55 // and port 5353. | |
56 bool BindSocket(UDPSocket* socket, | |
57 int addr_size, | |
58 const char* multicast_group); | |
59 | |
60 // Callback for handling a datagram being recieved on either ipv4 or ipv6. | |
61 // Responsible for ensuring we request another packet from the network. | |
62 void OnDatagramRecieved(UDPSocket* socket, | |
szym
2013/05/24 15:32:22
Throughout the file: s/Recieve/Receive/
Noam Samuel
2013/05/29 21:25:16
Done.
| |
63 DnsResponse* response, | |
64 IPEndPoint* recv_addr, | |
65 int bytes_read); | |
66 | |
67 // Request another packet from the network. | |
szym
2013/05/24 15:32:22
The implementation shows that this calls RecvFrom
Noam Samuel
2013/05/24 19:00:49
It does wait indefinitely. It does so in an asynch
| |
68 bool RecieveOnePacket(UDPSocket* socket, | |
69 DnsResponse* response, | |
70 IPEndPoint* recv_addr); | |
71 | |
72 // Parse the response and alert relevant listeners. | |
73 void HandlePacket(DnsResponse* response, int bytes_read); | |
74 | |
75 // Alert listeners of an update to the cache. | |
76 void AlertListeners(MDnsUpdateType update_type, | |
77 const ListenerKey& key, const RecordParsed* record); | |
78 | |
79 // Schedule a cleanup to a specific time, cancelling other cleanups. | |
80 void ScheduleCleanup(base::Time cleanup); | |
81 | |
82 // Clean up the cache and schedule a new cleanup. | |
83 void DoCleanup(); | |
84 | |
85 // Callback for when sending a query has finished. | |
86 void SendDone(int sent); | |
87 | |
88 // Callback for when a record is removed from the cache. | |
89 void OnRecordRemoved(const RecordParsed* record); | |
90 | |
91 std::multimap<ListenerKey, MDnsListenerImpl*> listeners_; | |
92 | |
93 scoped_ptr<UDPSocket> socket_ipv4_; | |
94 scoped_ptr<UDPSocket> socket_ipv6_; | |
95 | |
96 scoped_ptr<DnsResponse> response_ipv4_; | |
97 scoped_ptr<DnsResponse> response_ipv6_; | |
98 | |
99 // The address from which the last packet was recieved. | |
100 IPEndPoint recv_addr_ipv4_; | |
101 IPEndPoint recv_addr_ipv6_; | |
102 | |
103 MDnsListenerFactoryImpl* factory_; | |
104 MDnsCache cache_; | |
105 | |
106 base::CancelableCallback<void()> cleanup_callback_; | |
107 base::Time scheduled_cleanup_; | |
szym
2013/05/24 15:32:22
DISALLOW_COPY_AND_ASSIGN.
Noam Samuel
2013/05/29 21:25:16
Done.
| |
108 }; | |
109 | |
110 MDnsListenerFactoryImpl(); | |
111 | |
112 // Used only for testing. Lets users of this class decouple it from time | |
113 // dependence. | |
114 MDnsListenerFactoryImpl(base::Clock* clock, base::TaskRunner* task_runner); | |
115 virtual ~MDnsListenerFactoryImpl(); | |
116 | |
117 // Add delegate for RRType |rrtype| and name |name|. | |
118 // If |name| is an empty string, listen to all notification of type | |
119 // |rrtype|. | |
120 virtual scoped_ptr<MDnsListener> CreateListener( | |
121 uint16 rrtype, | |
122 const std::string& name, | |
123 bool active, | |
124 bool alert_existing_records, | |
125 MDnsListenerFactory::Delegate* delegate) OVERRIDE; | |
szym
2013/05/24 15:32:22
You don't need MDnsListenerFactory:: here and else
Noam Samuel
2013/05/29 21:25:16
Moved all the delegates and callbacks to their res
| |
126 | |
127 // Create a transaction to Query MDNS for a single-value query | |
128 // (A, AAAA, TXT, and SRV) asynchronously. May defer to cache. | |
129 virtual scoped_ptr<MDnsTransaction> CreateTransaction( | |
130 uint16 rrtype, | |
131 const std::string& name, | |
132 const MDnsListenerFactory::QueryCallback& callback) OVERRIDE; | |
133 | |
134 // Functions for testing only. | |
135 bool IsListeningForTests(); | |
136 | |
137 protected: | |
138 // These are couplings to multicast networking that tests will need to | |
139 // override in order to ensure the tests run only on the loopback network. | |
140 | |
141 // Tests will need to redirect multicast queries to a unicast destination. | |
142 virtual IPEndPoint GetIPv4SendEndpoint(); | |
143 virtual IPEndPoint GetIPv6SendEndpoint(); | |
144 | |
145 // Tests will need to control the binding behavior of sockets | |
146 // (e.g. not use REUSEADDR and not join the multicast group). | |
147 virtual bool BindToAddressAndMulticastGroup(UDPSocket* socket, | |
148 const IPEndPoint& address, | |
149 const IPAddressNumber& group); | |
150 | |
151 private: | |
152 bool AddListenRef(); | |
153 void SubtractListenRef(); | |
154 | |
155 scoped_ptr<Core> core_; | |
156 int listen_refs_; | |
157 | |
158 // Since we can either own or not own clock, use both a scoped_ptr and a ptr. | |
159 scoped_ptr<base::Clock> clock_owned_; | |
160 base::Clock* clock_; | |
161 | |
162 scoped_refptr<base::TaskRunner> task_runner_; | |
163 }; | |
164 | |
165 class MDnsListenerImpl : public MDnsListener, | |
166 public base::SupportsWeakPtr<MDnsListenerImpl> { | |
167 public: | |
168 MDnsListenerImpl(uint16 rrtype, | |
169 const std::string& name, | |
170 bool active, | |
171 bool alert_existing_records, | |
172 MDnsListenerFactory::Delegate* delegate, | |
173 MDnsListenerFactoryImpl::Core* core); | |
174 | |
175 // Destroying the listener stops listening. | |
176 virtual ~MDnsListenerImpl(); | |
177 | |
178 // Get the host or service name for this query. | |
179 // Return an empty string for no name. | |
180 virtual const std::string& GetName() const OVERRIDE; | |
181 | |
182 // Get the type for this query (SRV, TXT, A, AAA, etc) | |
183 virtual uint16 GetType() const OVERRIDE; | |
184 | |
185 virtual bool IsActive() const OVERRIDE; | |
186 | |
187 // Applies only to listeners with names. Will send out a query for new | |
188 // information. |force_refresh_cache| will force a refresh of all cached | |
189 // entities. | |
190 virtual bool SendQuery(bool force_refresh_cache) OVERRIDE; | |
191 | |
192 // Applies only to listeners with names. Query mDNS cache synchronously for | |
193 // either single- or multi- valued records. | |
194 virtual bool QueryCache( | |
195 std::vector<const RecordParsed*>* records) const OVERRIDE; | |
196 | |
197 MDnsListenerFactory::Delegate* delegate() { return delegate_; } | |
198 | |
199 void AlertDelegate(MDnsUpdateType update_type, | |
200 const RecordParsed* record_parsed); | |
201 private: | |
202 uint16 rrtype_; | |
203 std::string name_; | |
204 bool active_; | |
205 MDnsListenerFactoryImpl::Core* parent_; | |
206 MDnsListenerFactory::Delegate* delegate_; | |
207 }; | |
208 | |
209 class MDnsTransactionImpl : public MDnsTransaction, | |
210 public base::SupportsWeakPtr<MDnsTransactionImpl>, | |
211 public MDnsListenerFactoryImpl::Delegate { | |
212 public: | |
213 MDnsTransactionImpl(uint16 rrtype, | |
214 const std::string& name, | |
215 const MDnsListenerFactory::QueryCallback& callback, | |
216 base::TaskRunner* task_runner); | |
217 virtual ~MDnsTransactionImpl(); | |
218 | |
219 // Start the transaction. Returns true on success. | |
220 bool Init(MDnsListenerFactoryImpl* factory, | |
221 MDnsListenerFactoryImpl::Core* core); | |
222 | |
223 // MDnsListenerFactoryImpl::Delegate implementation | |
224 virtual void OnRecordUpdate(MDnsUpdateType update, | |
225 const RecordParsed* record) OVERRIDE; | |
226 virtual void OnNSecRecord(const std::string& name, unsigned type) OVERRIDE; | |
227 | |
228 | |
229 virtual const std::string& GetName() const OVERRIDE; | |
230 virtual uint16 GetType() const OVERRIDE; | |
231 | |
232 private: | |
233 // Trigger the callback and reset all related variables. | |
234 void TriggerCallback(MDnsTransactionResult result, | |
235 const RecordParsed* record); | |
236 | |
237 // Internal callback for when a cache record is found. | |
238 void CacheRecordFound(const RecordParsed* record); | |
239 | |
240 // Callback for when the transaction times out. | |
241 void OnTimedOut(); | |
242 | |
243 uint16 rrtype_; | |
244 std::string name_; | |
245 MDnsListenerFactory::QueryCallback callback_; | |
246 | |
247 bool triggered_; | |
248 scoped_ptr<MDnsListener> listener_; | |
249 base::CancelableCallback<void()> timeout_; | |
250 scoped_refptr<base::TaskRunner> task_runner_; | |
251 }; | |
252 } | |
253 #endif // NET_DNS_MDNS_LISTENER_IMPL_H_ | |
OLD | NEW |