| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 <queue> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <utility> | 11 #include <utility> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/cancelable_callback.h" | 14 #include "base/cancelable_callback.h" |
| 14 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 15 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 17 #include "net/base/ip_endpoint.h" | 18 #include "net/base/ip_endpoint.h" |
| 18 #include "net/dns/mdns_cache.h" | 19 #include "net/dns/mdns_cache.h" |
| 19 #include "net/dns/mdns_client.h" | 20 #include "net/dns/mdns_client.h" |
| 20 #include "net/udp/datagram_server_socket.h" | 21 #include "net/udp/datagram_server_socket.h" |
| 21 #include "net/udp/udp_server_socket.h" | 22 #include "net/udp/udp_server_socket.h" |
| 22 #include "net/udp/udp_socket.h" | 23 #include "net/udp/udp_socket.h" |
| 23 | 24 |
| 25 namespace base { |
| 26 class Clock; |
| 27 class Timer; |
| 28 } // namespace base |
| 29 |
| 24 namespace net { | 30 namespace net { |
| 25 | 31 |
| 26 class MDnsSocketFactoryImpl : public MDnsSocketFactory { | 32 class MDnsSocketFactoryImpl : public MDnsSocketFactory { |
| 27 public: | 33 public: |
| 28 MDnsSocketFactoryImpl() {}; | 34 MDnsSocketFactoryImpl() {} |
| 29 ~MDnsSocketFactoryImpl() override{}; | 35 ~MDnsSocketFactoryImpl() override{}; |
| 30 | 36 |
| 31 void CreateSockets(ScopedVector<DatagramServerSocket>* sockets) override; | 37 void CreateSockets(ScopedVector<DatagramServerSocket>* sockets) override; |
| 32 | 38 |
| 33 private: | 39 private: |
| 34 DISALLOW_COPY_AND_ASSIGN(MDnsSocketFactoryImpl); | 40 DISALLOW_COPY_AND_ASSIGN(MDnsSocketFactoryImpl); |
| 35 }; | 41 }; |
| 36 | 42 |
| 37 // A connection to the network for multicast DNS clients. It reads data into | 43 // A connection to the network for multicast DNS clients. It reads data into |
| 38 // DnsResponse objects and alerts the delegate that a packet has been received. | 44 // DnsResponse objects and alerts the delegate that a packet has been received. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 class MDnsListenerImpl; | 108 class MDnsListenerImpl; |
| 103 | 109 |
| 104 class NET_EXPORT_PRIVATE MDnsClientImpl : public MDnsClient { | 110 class NET_EXPORT_PRIVATE MDnsClientImpl : public MDnsClient { |
| 105 public: | 111 public: |
| 106 // The core object exists while the MDnsClient is listening, and is deleted | 112 // The core object exists while the MDnsClient is listening, and is deleted |
| 107 // whenever the number of listeners reaches zero. The deletion happens | 113 // whenever the number of listeners reaches zero. The deletion happens |
| 108 // asychronously, so destroying the last listener does not immediately | 114 // asychronously, so destroying the last listener does not immediately |
| 109 // invalidate the core. | 115 // invalidate the core. |
| 110 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { | 116 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
| 111 public: | 117 public: |
| 112 Core(); | 118 Core(base::Clock* clock, base::Timer* timer); |
| 113 ~Core() override; | 119 ~Core() override; |
| 114 | 120 |
| 115 // Initialize the core. Returns true on success. | 121 // Initialize the core. Returns true on success. |
| 116 bool Init(MDnsSocketFactory* socket_factory); | 122 bool Init(MDnsSocketFactory* socket_factory); |
| 117 | 123 |
| 118 // Send a query with a specific rrtype and name. Returns true on success. | 124 // Send a query with a specific rrtype and name. Returns true on success. |
| 119 bool SendQuery(uint16 rrtype, std::string name); | 125 bool SendQuery(uint16 rrtype, std::string name); |
| 120 | 126 |
| 121 // Add/remove a listener to the list of listeners. | 127 // Add/remove a listener to the list of listeners. |
| 122 void AddListener(MDnsListenerImpl* listener); | 128 void AddListener(MDnsListenerImpl* listener); |
| 123 void RemoveListener(MDnsListenerImpl* listener); | 129 void RemoveListener(MDnsListenerImpl* listener); |
| 124 | 130 |
| 125 // Query the cache for records of a specific type and name. | 131 // Query the cache for records of a specific type and name. |
| 126 void QueryCache(uint16 rrtype, const std::string& name, | 132 void QueryCache(uint16 rrtype, const std::string& name, |
| 127 std::vector<const RecordParsed*>* records) const; | 133 std::vector<const RecordParsed*>* records) const; |
| 128 | 134 |
| 129 // Parse the response and alert relevant listeners. | 135 // Parse the response and alert relevant listeners. |
| 130 void HandlePacket(DnsResponse* response, int bytes_read) override; | 136 void HandlePacket(DnsResponse* response, int bytes_read) override; |
| 131 | 137 |
| 132 void OnConnectionError(int error) override; | 138 void OnConnectionError(int error) override; |
| 133 | 139 |
| 134 private: | 140 private: |
| 141 FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); |
| 142 |
| 135 typedef std::pair<std::string, uint16> ListenerKey; | 143 typedef std::pair<std::string, uint16> ListenerKey; |
| 136 typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > | 144 typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > |
| 137 ListenerMap; | 145 ListenerMap; |
| 138 | 146 |
| 139 // Alert listeners of an update to the cache. | 147 // Alert listeners of an update to the cache. |
| 140 void AlertListeners(MDnsCache::UpdateType update_type, | 148 void AlertListeners(MDnsCache::UpdateType update_type, |
| 141 const ListenerKey& key, const RecordParsed* record); | 149 const ListenerKey& key, const RecordParsed* record); |
| 142 | 150 |
| 143 // Schedule a cache cleanup to a specific time, cancelling other cleanups. | 151 // Schedule a cache cleanup to a specific time, cancelling other cleanups. |
| 144 void ScheduleCleanup(base::Time cleanup); | 152 void ScheduleCleanup(base::Time cleanup); |
| 145 | 153 |
| 146 // Clean up the cache and schedule a new cleanup. | 154 // Clean up the cache and schedule a new cleanup. |
| 147 void DoCleanup(); | 155 void DoCleanup(); |
| 148 | 156 |
| 149 // Callback for when a record is removed from the cache. | 157 // Callback for when a record is removed from the cache. |
| 150 void OnRecordRemoved(const RecordParsed* record); | 158 void OnRecordRemoved(const RecordParsed* record); |
| 151 | 159 |
| 152 void NotifyNsecRecord(const RecordParsed* record); | 160 void NotifyNsecRecord(const RecordParsed* record); |
| 153 | 161 |
| 154 // Delete and erase the observer list for |key|. Only deletes the observer | 162 // Delete and erase the observer list for |key|. Only deletes the observer |
| 155 // list if is empty. | 163 // list if is empty. |
| 156 void CleanupObserverList(const ListenerKey& key); | 164 void CleanupObserverList(const ListenerKey& key); |
| 157 | 165 |
| 158 ListenerMap listeners_; | 166 ListenerMap listeners_; |
| 159 | 167 |
| 160 MDnsCache cache_; | 168 MDnsCache cache_; |
| 161 | 169 |
| 162 base::CancelableClosure cleanup_callback_; | 170 base::Clock* clock_; |
| 171 base::Timer* cleanup_timer_; |
| 163 base::Time scheduled_cleanup_; | 172 base::Time scheduled_cleanup_; |
| 164 | 173 |
| 165 scoped_ptr<MDnsConnection> connection_; | 174 scoped_ptr<MDnsConnection> connection_; |
| 166 | 175 |
| 167 DISALLOW_COPY_AND_ASSIGN(Core); | 176 DISALLOW_COPY_AND_ASSIGN(Core); |
| 168 }; | 177 }; |
| 169 | 178 |
| 170 MDnsClientImpl(); | 179 MDnsClientImpl(); |
| 171 ~MDnsClientImpl() override; | 180 ~MDnsClientImpl() override; |
| 172 | 181 |
| 173 // MDnsClient implementation: | 182 // MDnsClient implementation: |
| 174 scoped_ptr<MDnsListener> CreateListener( | 183 scoped_ptr<MDnsListener> CreateListener( |
| 175 uint16 rrtype, | 184 uint16 rrtype, |
| 176 const std::string& name, | 185 const std::string& name, |
| 177 MDnsListener::Delegate* delegate) override; | 186 MDnsListener::Delegate* delegate) override; |
| 178 | 187 |
| 179 scoped_ptr<MDnsTransaction> CreateTransaction( | 188 scoped_ptr<MDnsTransaction> CreateTransaction( |
| 180 uint16 rrtype, | 189 uint16 rrtype, |
| 181 const std::string& name, | 190 const std::string& name, |
| 182 int flags, | 191 int flags, |
| 183 const MDnsTransaction::ResultCallback& callback) override; | 192 const MDnsTransaction::ResultCallback& callback) override; |
| 184 | 193 |
| 185 bool StartListening(MDnsSocketFactory* socket_factory) override; | 194 bool StartListening(MDnsSocketFactory* socket_factory) override; |
| 186 void StopListening() override; | 195 void StopListening() override; |
| 187 bool IsListening() const override; | 196 bool IsListening() const override; |
| 188 | 197 |
| 189 Core* core() { return core_.get(); } | 198 Core* core() { return core_.get(); } |
| 190 | 199 |
| 191 private: | 200 private: |
| 201 FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); |
| 202 |
| 203 // Test constructor, takes a mock clock and mock timer. |
| 204 MDnsClientImpl(scoped_ptr<base::Clock> clock, |
| 205 scoped_ptr<base::Timer> cleanup_timer); |
| 206 |
| 192 scoped_ptr<Core> core_; | 207 scoped_ptr<Core> core_; |
| 208 scoped_ptr<base::Clock> clock_; |
| 209 scoped_ptr<base::Timer> cleanup_timer_; |
| 193 | 210 |
| 194 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); | 211 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); |
| 195 }; | 212 }; |
| 196 | 213 |
| 197 class MDnsListenerImpl : public MDnsListener, | 214 class MDnsListenerImpl : public MDnsListener, |
| 198 public base::SupportsWeakPtr<MDnsListenerImpl> { | 215 public base::SupportsWeakPtr<MDnsListenerImpl> { |
| 199 public: | 216 public: |
| 200 MDnsListenerImpl(uint16 rrtype, | 217 MDnsListenerImpl(uint16 rrtype, |
| 201 const std::string& name, | 218 const std::string& name, |
| 219 base::Clock* clock, |
| 202 MDnsListener::Delegate* delegate, | 220 MDnsListener::Delegate* delegate, |
| 203 MDnsClientImpl* client); | 221 MDnsClientImpl* client); |
| 204 | 222 |
| 205 ~MDnsListenerImpl() override; | 223 ~MDnsListenerImpl() override; |
| 206 | 224 |
| 207 // MDnsListener implementation: | 225 // MDnsListener implementation: |
| 208 bool Start() override; | 226 bool Start() override; |
| 209 | 227 |
| 210 // Actively refresh any received records. | 228 // Actively refresh any received records. |
| 211 void SetActiveRefresh(bool active_refresh) override; | 229 void SetActiveRefresh(bool active_refresh) override; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 222 | 240 |
| 223 // Alert the delegate of the existence of an Nsec record. | 241 // Alert the delegate of the existence of an Nsec record. |
| 224 void AlertNsecRecord(); | 242 void AlertNsecRecord(); |
| 225 | 243 |
| 226 private: | 244 private: |
| 227 void ScheduleNextRefresh(); | 245 void ScheduleNextRefresh(); |
| 228 void DoRefresh(); | 246 void DoRefresh(); |
| 229 | 247 |
| 230 uint16 rrtype_; | 248 uint16 rrtype_; |
| 231 std::string name_; | 249 std::string name_; |
| 250 base::Clock* clock_; |
| 232 MDnsClientImpl* client_; | 251 MDnsClientImpl* client_; |
| 233 MDnsListener::Delegate* delegate_; | 252 MDnsListener::Delegate* delegate_; |
| 234 | 253 |
| 235 base::Time last_update_; | 254 base::Time last_update_; |
| 236 uint32 ttl_; | 255 uint32 ttl_; |
| 237 bool started_; | 256 bool started_; |
| 238 bool active_refresh_; | 257 bool active_refresh_; |
| 239 | 258 |
| 240 base::CancelableClosure next_refresh_; | 259 base::CancelableClosure next_refresh_; |
| 241 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); | 260 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 MDnsClientImpl* client_; | 318 MDnsClientImpl* client_; |
| 300 | 319 |
| 301 bool started_; | 320 bool started_; |
| 302 int flags_; | 321 int flags_; |
| 303 | 322 |
| 304 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); | 323 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
| 305 }; | 324 }; |
| 306 | 325 |
| 307 } // namespace net | 326 } // namespace net |
| 308 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ | 327 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |
| OLD | NEW |