| 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 <stdint.h> |
| 9 |
| 8 #include <map> | 10 #include <map> |
| 9 #include <queue> | 11 #include <queue> |
| 10 #include <string> | 12 #include <string> |
| 11 #include <utility> | 13 #include <utility> |
| 12 #include <vector> | 14 #include <vector> |
| 13 | 15 |
| 14 #include "base/cancelable_callback.h" | 16 #include "base/cancelable_callback.h" |
| 15 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
| 18 #include "base/macros.h" |
| 16 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
| 18 #include "net/base/io_buffer.h" | 21 #include "net/base/io_buffer.h" |
| 19 #include "net/base/ip_endpoint.h" | 22 #include "net/base/ip_endpoint.h" |
| 20 #include "net/dns/mdns_cache.h" | 23 #include "net/dns/mdns_cache.h" |
| 21 #include "net/dns/mdns_client.h" | 24 #include "net/dns/mdns_client.h" |
| 22 #include "net/udp/datagram_server_socket.h" | 25 #include "net/udp/datagram_server_socket.h" |
| 23 #include "net/udp/udp_server_socket.h" | 26 #include "net/udp/udp_server_socket.h" |
| 24 #include "net/udp/udp_socket.h" | 27 #include "net/udp/udp_socket.h" |
| 25 | 28 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 // invalidate the core. | 120 // invalidate the core. |
| 118 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { | 121 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
| 119 public: | 122 public: |
| 120 Core(base::Clock* clock, base::Timer* timer); | 123 Core(base::Clock* clock, base::Timer* timer); |
| 121 ~Core() override; | 124 ~Core() override; |
| 122 | 125 |
| 123 // Initialize the core. Returns true on success. | 126 // Initialize the core. Returns true on success. |
| 124 bool Init(MDnsSocketFactory* socket_factory); | 127 bool Init(MDnsSocketFactory* socket_factory); |
| 125 | 128 |
| 126 // Send a query with a specific rrtype and name. Returns true on success. | 129 // Send a query with a specific rrtype and name. Returns true on success. |
| 127 bool SendQuery(uint16 rrtype, const std::string& name); | 130 bool SendQuery(uint16_t rrtype, const std::string& name); |
| 128 | 131 |
| 129 // Add/remove a listener to the list of listeners. | 132 // Add/remove a listener to the list of listeners. |
| 130 void AddListener(MDnsListenerImpl* listener); | 133 void AddListener(MDnsListenerImpl* listener); |
| 131 void RemoveListener(MDnsListenerImpl* listener); | 134 void RemoveListener(MDnsListenerImpl* listener); |
| 132 | 135 |
| 133 // Query the cache for records of a specific type and name. | 136 // Query the cache for records of a specific type and name. |
| 134 void QueryCache(uint16 rrtype, const std::string& name, | 137 void QueryCache(uint16_t rrtype, |
| 138 const std::string& name, |
| 135 std::vector<const RecordParsed*>* records) const; | 139 std::vector<const RecordParsed*>* records) const; |
| 136 | 140 |
| 137 // Parse the response and alert relevant listeners. | 141 // Parse the response and alert relevant listeners. |
| 138 void HandlePacket(DnsResponse* response, int bytes_read) override; | 142 void HandlePacket(DnsResponse* response, int bytes_read) override; |
| 139 | 143 |
| 140 void OnConnectionError(int error) override; | 144 void OnConnectionError(int error) override; |
| 141 | 145 |
| 142 private: | 146 private: |
| 143 FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); | 147 FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); |
| 144 | 148 |
| 145 typedef std::pair<std::string, uint16> ListenerKey; | 149 typedef std::pair<std::string, uint16_t> ListenerKey; |
| 146 typedef std::map<ListenerKey, base::ObserverList<MDnsListenerImpl>*> | 150 typedef std::map<ListenerKey, base::ObserverList<MDnsListenerImpl>*> |
| 147 ListenerMap; | 151 ListenerMap; |
| 148 | 152 |
| 149 // Alert listeners of an update to the cache. | 153 // Alert listeners of an update to the cache. |
| 150 void AlertListeners(MDnsCache::UpdateType update_type, | 154 void AlertListeners(MDnsCache::UpdateType update_type, |
| 151 const ListenerKey& key, const RecordParsed* record); | 155 const ListenerKey& key, const RecordParsed* record); |
| 152 | 156 |
| 153 // Schedule a cache cleanup to a specific time, cancelling other cleanups. | 157 // Schedule a cache cleanup to a specific time, cancelling other cleanups. |
| 154 void ScheduleCleanup(base::Time cleanup); | 158 void ScheduleCleanup(base::Time cleanup); |
| 155 | 159 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 176 scoped_ptr<MDnsConnection> connection_; | 180 scoped_ptr<MDnsConnection> connection_; |
| 177 | 181 |
| 178 DISALLOW_COPY_AND_ASSIGN(Core); | 182 DISALLOW_COPY_AND_ASSIGN(Core); |
| 179 }; | 183 }; |
| 180 | 184 |
| 181 MDnsClientImpl(); | 185 MDnsClientImpl(); |
| 182 ~MDnsClientImpl() override; | 186 ~MDnsClientImpl() override; |
| 183 | 187 |
| 184 // MDnsClient implementation: | 188 // MDnsClient implementation: |
| 185 scoped_ptr<MDnsListener> CreateListener( | 189 scoped_ptr<MDnsListener> CreateListener( |
| 186 uint16 rrtype, | 190 uint16_t rrtype, |
| 187 const std::string& name, | 191 const std::string& name, |
| 188 MDnsListener::Delegate* delegate) override; | 192 MDnsListener::Delegate* delegate) override; |
| 189 | 193 |
| 190 scoped_ptr<MDnsTransaction> CreateTransaction( | 194 scoped_ptr<MDnsTransaction> CreateTransaction( |
| 191 uint16 rrtype, | 195 uint16_t rrtype, |
| 192 const std::string& name, | 196 const std::string& name, |
| 193 int flags, | 197 int flags, |
| 194 const MDnsTransaction::ResultCallback& callback) override; | 198 const MDnsTransaction::ResultCallback& callback) override; |
| 195 | 199 |
| 196 bool StartListening(MDnsSocketFactory* socket_factory) override; | 200 bool StartListening(MDnsSocketFactory* socket_factory) override; |
| 197 void StopListening() override; | 201 void StopListening() override; |
| 198 bool IsListening() const override; | 202 bool IsListening() const override; |
| 199 | 203 |
| 200 Core* core() { return core_.get(); } | 204 Core* core() { return core_.get(); } |
| 201 | 205 |
| 202 private: | 206 private: |
| 203 FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); | 207 FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); |
| 204 | 208 |
| 205 // Test constructor, takes a mock clock and mock timer. | 209 // Test constructor, takes a mock clock and mock timer. |
| 206 MDnsClientImpl(scoped_ptr<base::Clock> clock, | 210 MDnsClientImpl(scoped_ptr<base::Clock> clock, |
| 207 scoped_ptr<base::Timer> cleanup_timer); | 211 scoped_ptr<base::Timer> cleanup_timer); |
| 208 | 212 |
| 209 scoped_ptr<Core> core_; | 213 scoped_ptr<Core> core_; |
| 210 scoped_ptr<base::Clock> clock_; | 214 scoped_ptr<base::Clock> clock_; |
| 211 scoped_ptr<base::Timer> cleanup_timer_; | 215 scoped_ptr<base::Timer> cleanup_timer_; |
| 212 | 216 |
| 213 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); | 217 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); |
| 214 }; | 218 }; |
| 215 | 219 |
| 216 class MDnsListenerImpl : public MDnsListener, | 220 class MDnsListenerImpl : public MDnsListener, |
| 217 public base::SupportsWeakPtr<MDnsListenerImpl> { | 221 public base::SupportsWeakPtr<MDnsListenerImpl> { |
| 218 public: | 222 public: |
| 219 MDnsListenerImpl(uint16 rrtype, | 223 MDnsListenerImpl(uint16_t rrtype, |
| 220 const std::string& name, | 224 const std::string& name, |
| 221 base::Clock* clock, | 225 base::Clock* clock, |
| 222 MDnsListener::Delegate* delegate, | 226 MDnsListener::Delegate* delegate, |
| 223 MDnsClientImpl* client); | 227 MDnsClientImpl* client); |
| 224 | 228 |
| 225 ~MDnsListenerImpl() override; | 229 ~MDnsListenerImpl() override; |
| 226 | 230 |
| 227 // MDnsListener implementation: | 231 // MDnsListener implementation: |
| 228 bool Start() override; | 232 bool Start() override; |
| 229 | 233 |
| 230 // Actively refresh any received records. | 234 // Actively refresh any received records. |
| 231 void SetActiveRefresh(bool active_refresh) override; | 235 void SetActiveRefresh(bool active_refresh) override; |
| 232 | 236 |
| 233 const std::string& GetName() const override; | 237 const std::string& GetName() const override; |
| 234 | 238 |
| 235 uint16 GetType() const override; | 239 uint16_t GetType() const override; |
| 236 | 240 |
| 237 MDnsListener::Delegate* delegate() { return delegate_; } | 241 MDnsListener::Delegate* delegate() { return delegate_; } |
| 238 | 242 |
| 239 // Alert the delegate of a record update. | 243 // Alert the delegate of a record update. |
| 240 void HandleRecordUpdate(MDnsCache::UpdateType update_type, | 244 void HandleRecordUpdate(MDnsCache::UpdateType update_type, |
| 241 const RecordParsed* record_parsed); | 245 const RecordParsed* record_parsed); |
| 242 | 246 |
| 243 // Alert the delegate of the existence of an Nsec record. | 247 // Alert the delegate of the existence of an Nsec record. |
| 244 void AlertNsecRecord(); | 248 void AlertNsecRecord(); |
| 245 | 249 |
| 246 private: | 250 private: |
| 247 void ScheduleNextRefresh(); | 251 void ScheduleNextRefresh(); |
| 248 void DoRefresh(); | 252 void DoRefresh(); |
| 249 | 253 |
| 250 uint16 rrtype_; | 254 uint16_t rrtype_; |
| 251 std::string name_; | 255 std::string name_; |
| 252 base::Clock* clock_; | 256 base::Clock* clock_; |
| 253 MDnsClientImpl* client_; | 257 MDnsClientImpl* client_; |
| 254 MDnsListener::Delegate* delegate_; | 258 MDnsListener::Delegate* delegate_; |
| 255 | 259 |
| 256 base::Time last_update_; | 260 base::Time last_update_; |
| 257 uint32 ttl_; | 261 uint32_t ttl_; |
| 258 bool started_; | 262 bool started_; |
| 259 bool active_refresh_; | 263 bool active_refresh_; |
| 260 | 264 |
| 261 base::CancelableClosure next_refresh_; | 265 base::CancelableClosure next_refresh_; |
| 262 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); | 266 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); |
| 263 }; | 267 }; |
| 264 | 268 |
| 265 class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>, | 269 class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>, |
| 266 public MDnsTransaction, | 270 public MDnsTransaction, |
| 267 public MDnsListener::Delegate { | 271 public MDnsListener::Delegate { |
| 268 public: | 272 public: |
| 269 MDnsTransactionImpl(uint16 rrtype, | 273 MDnsTransactionImpl(uint16_t rrtype, |
| 270 const std::string& name, | 274 const std::string& name, |
| 271 int flags, | 275 int flags, |
| 272 const MDnsTransaction::ResultCallback& callback, | 276 const MDnsTransaction::ResultCallback& callback, |
| 273 MDnsClientImpl* client); | 277 MDnsClientImpl* client); |
| 274 ~MDnsTransactionImpl() override; | 278 ~MDnsTransactionImpl() override; |
| 275 | 279 |
| 276 // MDnsTransaction implementation: | 280 // MDnsTransaction implementation: |
| 277 bool Start() override; | 281 bool Start() override; |
| 278 | 282 |
| 279 const std::string& GetName() const override; | 283 const std::string& GetName() const override; |
| 280 uint16 GetType() const override; | 284 uint16_t GetType() const override; |
| 281 | 285 |
| 282 // MDnsListener::Delegate implementation: | 286 // MDnsListener::Delegate implementation: |
| 283 void OnRecordUpdate(MDnsListener::UpdateType update, | 287 void OnRecordUpdate(MDnsListener::UpdateType update, |
| 284 const RecordParsed* record) override; | 288 const RecordParsed* record) override; |
| 285 void OnNsecRecord(const std::string& name, unsigned type) override; | 289 void OnNsecRecord(const std::string& name, unsigned type) override; |
| 286 | 290 |
| 287 void OnCachePurged() override; | 291 void OnCachePurged() override; |
| 288 | 292 |
| 289 private: | 293 private: |
| 290 bool is_active() { return !callback_.is_null(); } | 294 bool is_active() { return !callback_.is_null(); } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 303 | 307 |
| 304 // Reads records from the cache and calls the callback for every | 308 // Reads records from the cache and calls the callback for every |
| 305 // record read. | 309 // record read. |
| 306 void ServeRecordsFromCache(); | 310 void ServeRecordsFromCache(); |
| 307 | 311 |
| 308 // Send a query to the network and set up a timeout to time out the | 312 // Send a query to the network and set up a timeout to time out the |
| 309 // transaction. Returns false if it fails to start listening on the network | 313 // transaction. Returns false if it fails to start listening on the network |
| 310 // or if it fails to send a query. | 314 // or if it fails to send a query. |
| 311 bool QueryAndListen(); | 315 bool QueryAndListen(); |
| 312 | 316 |
| 313 uint16 rrtype_; | 317 uint16_t rrtype_; |
| 314 std::string name_; | 318 std::string name_; |
| 315 MDnsTransaction::ResultCallback callback_; | 319 MDnsTransaction::ResultCallback callback_; |
| 316 | 320 |
| 317 scoped_ptr<MDnsListener> listener_; | 321 scoped_ptr<MDnsListener> listener_; |
| 318 base::CancelableCallback<void()> timeout_; | 322 base::CancelableCallback<void()> timeout_; |
| 319 | 323 |
| 320 MDnsClientImpl* client_; | 324 MDnsClientImpl* client_; |
| 321 | 325 |
| 322 bool started_; | 326 bool started_; |
| 323 int flags_; | 327 int flags_; |
| 324 | 328 |
| 325 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); | 329 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
| 326 }; | 330 }; |
| 327 | 331 |
| 328 } // namespace net | 332 } // namespace net |
| 329 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ | 333 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |
| OLD | NEW |