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/cancelable_callback.h" |
| 14 #include "base/observer_list.h" |
| 15 #include "base/task_runner.h" |
| 16 #include "base/time/clock.h" |
| 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/ip_endpoint.h" |
| 19 #include "net/dns/mdns_cache.h" |
| 20 #include "net/dns/mdns_listener.h" |
| 21 #include "net/udp/udp_socket.h" |
| 22 |
| 23 namespace net { |
| 24 |
| 25 class MDnsListenerImpl; |
| 26 |
| 27 class MDnsConnection { |
| 28 public: |
| 29 class Delegate { |
| 30 public: |
| 31 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; |
| 32 virtual ~Delegate() {} |
| 33 }; |
| 34 |
| 35 virtual bool Init() = 0; |
| 36 virtual bool Send(IOBuffer* buffer, unsigned size) = 0; |
| 37 virtual ~MDnsConnection() {} |
| 38 }; |
| 39 |
| 40 class MDnsConnectionFactory { |
| 41 public: |
| 42 virtual scoped_ptr<MDnsConnection> CreateConnection( |
| 43 MDnsConnection::Delegate* delegate, |
| 44 base::TaskRunner* task_runner) = 0; |
| 45 |
| 46 virtual ~MDnsConnectionFactory() {} |
| 47 }; |
| 48 |
| 49 class MDnsClientImpl : public MDnsClient { |
| 50 public: |
| 51 // The core object exists while the MDnsClient is listening for MDnsPackets, |
| 52 // and is deleted whenever the number of references for listening reachers |
| 53 // zero. The message loop may hold weak pointers to it in order to ensure |
| 54 // tasks scheduled on it are not delivered after it is destroyed. |
| 55 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
| 56 public: |
| 57 Core(MDnsClientImpl* client, |
| 58 MDnsConnectionFactory* connection_factory, |
| 59 base::TaskRunner* task_runner, |
| 60 base::Clock* clock); |
| 61 virtual ~Core(); |
| 62 |
| 63 // Initialize the core. Returns true on success. |
| 64 bool Init(); |
| 65 |
| 66 // Send a query with a specific rrtype and name. Returns true on success. |
| 67 bool SendQuery(uint16 rrtype, std::string name); |
| 68 |
| 69 // Add/remove a listener to the list of listener. May cause network traffic |
| 70 // if listener is active. |
| 71 void AddListener(MDnsListenerImpl* listener, bool alert_existing_records); |
| 72 void RemoveListener(MDnsListenerImpl* listener); |
| 73 |
| 74 // Query the cache for records of a specific type and name. |
| 75 void QueryCache(uint16 rrtype, const std::string& name, |
| 76 std::vector<const RecordParsed*>* records) const; |
| 77 |
| 78 // Parse the response and alert relevant listeners. |
| 79 virtual void HandlePacket(DnsResponse* response, int bytes_read) OVERRIDE; |
| 80 |
| 81 private: |
| 82 typedef std::pair<uint16, std::string> ListenerKey; |
| 83 typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > |
| 84 ListenerMap; |
| 85 |
| 86 // Alert listeners of an update to the cache. |
| 87 void AlertListeners(MDnsUpdateType update_type, |
| 88 const ListenerKey& key, const RecordParsed* record); |
| 89 |
| 90 // Schedule a cleanup to a specific time, cancelling other cleanups. |
| 91 void ScheduleCleanup(base::Time cleanup); |
| 92 |
| 93 // Clean up the cache and schedule a new cleanup. |
| 94 void DoCleanup(); |
| 95 |
| 96 // Callback for when a record is removed from the cache. |
| 97 void OnRecordRemoved(const RecordParsed* record); |
| 98 |
| 99 ListenerMap listeners_; |
| 100 |
| 101 MDnsClientImpl* client_; |
| 102 MDnsCache cache_; |
| 103 |
| 104 base::CancelableCallback<void()> cleanup_callback_; |
| 105 base::Time scheduled_cleanup_; |
| 106 |
| 107 scoped_refptr<base::TaskRunner> task_runner_; |
| 108 base::Clock* clock_; |
| 109 scoped_ptr<MDnsConnection> connection_; |
| 110 }; |
| 111 |
| 112 MDnsClientImpl(); |
| 113 |
| 114 // Used only for testing. Lets users of this class decouple it from time |
| 115 // dependence and network dependence. |
| 116 MDnsClientImpl(base::Clock* clock, base::TaskRunner* task_runner, |
| 117 MDnsConnectionFactory* connection_factory); |
| 118 virtual ~MDnsClientImpl(); |
| 119 |
| 120 // Add delegate for RRType |rrtype| and name |name|. |
| 121 // If |name| is an empty string, listen to all notification of type |
| 122 // |rrtype|. |
| 123 virtual scoped_ptr<MDnsListener> CreateListener( |
| 124 uint16 rrtype, |
| 125 const std::string& name, |
| 126 bool active, |
| 127 bool alert_existing_records, |
| 128 MDnsListener::Delegate* delegate) OVERRIDE; |
| 129 |
| 130 // Create a transaction to Query MDNS for a single-value query |
| 131 // (A, AAAA, TXT, and SRV) asynchronously. May defer to cache. |
| 132 virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
| 133 uint16 rrtype, |
| 134 const std::string& name, |
| 135 const MDnsTransaction::ResultCallback& callback) OVERRIDE; |
| 136 |
| 137 // Functions for testing only. |
| 138 bool IsListeningForTests(); |
| 139 |
| 140 private: |
| 141 bool AddListenRef(); |
| 142 void SubtractListenRef(); |
| 143 void Shutdown(); |
| 144 |
| 145 scoped_ptr<Core> core_; |
| 146 int listen_refs_; |
| 147 |
| 148 // Since we can either own or not own clock, use both a scoped_ptr and a ptr. |
| 149 scoped_ptr<base::Clock> clock_owned_; |
| 150 base::Clock* clock_; |
| 151 |
| 152 // Since we can either own or not own the connection_factory, use a scoped_ptr |
| 153 // and a ptr. |
| 154 scoped_ptr<MDnsConnectionFactory> connection_factory_owned_; |
| 155 MDnsConnectionFactory* connection_factory_; |
| 156 |
| 157 scoped_refptr<base::TaskRunner> task_runner_; |
| 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); |
| 160 }; |
| 161 |
| 162 class MDnsListenerImpl : public MDnsListener, |
| 163 public base::SupportsWeakPtr<MDnsListenerImpl> { |
| 164 public: |
| 165 MDnsListenerImpl(uint16 rrtype, |
| 166 const std::string& name, |
| 167 bool active, |
| 168 bool alert_existing_records, |
| 169 MDnsListener::Delegate* delegate, |
| 170 MDnsClientImpl::Core* core); |
| 171 |
| 172 // Destroying the listener stops listening. |
| 173 virtual ~MDnsListenerImpl(); |
| 174 |
| 175 // Get the host or service name for this query. |
| 176 // Return an empty string for no name. |
| 177 virtual const std::string& GetName() const OVERRIDE; |
| 178 |
| 179 // Get the type for this query (SRV, TXT, A, AAA, etc) |
| 180 virtual uint16 GetType() const OVERRIDE; |
| 181 |
| 182 virtual bool IsActive() const OVERRIDE; |
| 183 |
| 184 // Applies only to listeners with names. Will send out a query for new |
| 185 // information. |force_refresh_cache| will force a refresh of all cached |
| 186 // entities. |
| 187 virtual bool SendQuery(bool force_refresh_cache) OVERRIDE; |
| 188 |
| 189 // Applies only to listeners with names. Query mDNS cache synchronously for |
| 190 // either single- or multi- valued records. |
| 191 virtual bool QueryCache( |
| 192 std::vector<const RecordParsed*>* records) const OVERRIDE; |
| 193 |
| 194 MDnsListener::Delegate* delegate() { return delegate_; } |
| 195 |
| 196 void AlertDelegate(MDnsUpdateType update_type, |
| 197 const RecordParsed* record_parsed); |
| 198 private: |
| 199 uint16 rrtype_; |
| 200 std::string name_; |
| 201 bool active_; |
| 202 MDnsClientImpl::Core* parent_; |
| 203 MDnsListener::Delegate* delegate_; |
| 204 |
| 205 DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); |
| 206 }; |
| 207 |
| 208 class MDnsTransactionImpl : public MDnsTransaction, |
| 209 public base::SupportsWeakPtr<MDnsTransactionImpl>, |
| 210 public MDnsListener::Delegate { |
| 211 public: |
| 212 MDnsTransactionImpl(uint16 rrtype, |
| 213 const std::string& name, |
| 214 const MDnsTransaction::ResultCallback& callback, |
| 215 base::TaskRunner* task_runner); |
| 216 virtual ~MDnsTransactionImpl(); |
| 217 |
| 218 // Start the transaction. Returns true on success. |
| 219 bool Init(MDnsClientImpl* client, |
| 220 MDnsClientImpl::Core* core); |
| 221 |
| 222 // MDnsListener::Delegate implementation |
| 223 virtual void OnRecordUpdate(MDnsUpdateType update, |
| 224 const RecordParsed* record) OVERRIDE; |
| 225 virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; |
| 226 |
| 227 virtual const std::string& GetName() const OVERRIDE; |
| 228 virtual uint16 GetType() const OVERRIDE; |
| 229 |
| 230 private: |
| 231 // Trigger the callback and reset all related variables. |
| 232 void TriggerCallback(MDnsTransactionResult result, |
| 233 const RecordParsed* record); |
| 234 |
| 235 // Internal callback for when a cache record is found. |
| 236 void CacheRecordFound(const RecordParsed* record); |
| 237 |
| 238 // Callback for when the transaction times out. |
| 239 void OnTimedOut(); |
| 240 |
| 241 uint16 rrtype_; |
| 242 std::string name_; |
| 243 MDnsTransaction::ResultCallback callback_; |
| 244 |
| 245 bool triggered_; |
| 246 scoped_ptr<MDnsListener> listener_; |
| 247 base::CancelableCallback<void()> timeout_; |
| 248 scoped_refptr<base::TaskRunner> task_runner_; |
| 249 |
| 250 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
| 251 }; |
| 252 |
| 253 class MDnsConnectionImpl : public base::SupportsWeakPtr<MDnsConnectionImpl>, |
| 254 public MDnsConnection { |
| 255 public: |
| 256 MDnsConnectionImpl(MDnsConnection::Delegate* delegate, |
| 257 base::TaskRunner* task_runner); |
| 258 virtual ~MDnsConnectionImpl(); |
| 259 |
| 260 virtual bool Init() OVERRIDE; |
| 261 virtual bool Send(IOBuffer* buffer, unsigned size) OVERRIDE; |
| 262 |
| 263 private: |
| 264 // Bind a socket with a specific address size to a specific multicast group |
| 265 // and port 5353. |
| 266 bool BindSocket(UDPSocket* socket, |
| 267 int addr_size, |
| 268 const char* multicast_group); |
| 269 |
| 270 // Callback for handling a datagram being recieved on either ipv4 or ipv6. |
| 271 // Responsible for ensuring we request another packet from the network. |
| 272 void OnDatagramReceived(UDPSocket* socket, |
| 273 DnsResponse* response, |
| 274 IPEndPoint* recv_addr, |
| 275 int bytes_read); |
| 276 |
| 277 // Request another packet from the network. |
| 278 bool ReceiveNextPacket(UDPSocket* socket, |
| 279 DnsResponse* response, |
| 280 IPEndPoint* recv_addr); |
| 281 |
| 282 // Callback for when sending a query has finished. |
| 283 void SendDone(int sent); |
| 284 |
| 285 IPEndPoint GetIPv4SendEndpoint(); |
| 286 IPEndPoint GetIPv6SendEndpoint(); |
| 287 |
| 288 scoped_ptr<UDPSocket> socket_ipv4_; |
| 289 scoped_ptr<UDPSocket> socket_ipv6_; |
| 290 |
| 291 scoped_ptr<DnsResponse> response_ipv4_; |
| 292 scoped_ptr<DnsResponse> response_ipv6_; |
| 293 |
| 294 // The address from which the last packet was recieved. |
| 295 IPEndPoint recv_addr_ipv4_; |
| 296 IPEndPoint recv_addr_ipv6_; |
| 297 |
| 298 MDnsConnection::Delegate* delegate_; |
| 299 |
| 300 scoped_refptr<base::TaskRunner> task_runner_; |
| 301 }; |
| 302 |
| 303 class MDnsConnectionImplFactory : public MDnsConnectionFactory { |
| 304 public: |
| 305 MDnsConnectionImplFactory(); |
| 306 virtual ~MDnsConnectionImplFactory(); |
| 307 |
| 308 virtual scoped_ptr<MDnsConnection> CreateConnection( |
| 309 MDnsConnection::Delegate* delegate, |
| 310 base::TaskRunner* task_runner) OVERRIDE; |
| 311 }; |
| 312 |
| 313 } |
| 314 #endif // NET_DNS_MDNS_LISTENER_IMPL_H_ |
OLD | NEW |