Chromium Code Reviews| Index: net/dns/mdns_client_impl.h |
| diff --git a/net/dns/mdns_client_impl.h b/net/dns/mdns_client_impl.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..00a7bf45f75b600fa53a62f8110350ba9815b444 |
| --- /dev/null |
| +++ b/net/dns/mdns_client_impl.h |
| @@ -0,0 +1,331 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef NET_DNS_MDNS_CLIENT_IMPL_H_ |
| +#define NET_DNS_MDNS_CLIENT_IMPL_H_ |
| + |
| +#include <map> |
| +#include <string> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/cancelable_callback.h" |
| +#include "base/observer_list.h" |
| +#include "base/task_runner.h" |
| +#include "base/time/clock.h" |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/ip_endpoint.h" |
| +#include "net/dns/mdns_cache.h" |
| +#include "net/dns/mdns_client.h" |
| +#include "net/udp/udp_socket.h" |
| + |
| +namespace net { |
| + |
| +class MDnsListenerImpl; |
| + |
| +// This class represents a connection to the network for multicast DNS clients. |
| +// It reads data into DNSResponse objects and alerts the delegate that a packet |
| +// has been received. |
| +class MDnsConnection { |
|
szym
2013/06/02 19:01:23
Since this is internal to mdns_client_impl, I don'
Noam Samuel
2013/06/03 17:57:55
This class is mocked in the tests to allow separat
szym
2013/06/03 18:38:30
Whelp! I didn't notice it. This CL is a bit large.
|
| + public: |
| + class Delegate { |
| + public: |
| + // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. |
| + virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; |
| + virtual ~Delegate() {} |
| + }; |
| + |
| + // Start the connection. Will begin alerting the delegate of packets. |
| + virtual bool Init() = 0; |
| + |
| + // Send a packet on both ipv4 and ipv6. |
| + virtual bool Send(IOBuffer* buffer, unsigned size) = 0; |
| + virtual ~MDnsConnection() {} |
| +}; |
| + |
| +class MDnsConnectionFactory { |
| + public: |
| + virtual scoped_ptr<MDnsConnection> CreateConnection( |
| + MDnsConnection::Delegate* delegate, |
| + base::TaskRunner* task_runner) = 0; |
| + |
| + virtual ~MDnsConnectionFactory() {} |
| +}; |
| + |
| +class MDnsClientImpl : public MDnsClient { |
| + public: |
| + // The core object exists while the MDnsClient is listening for MDnsPackets, |
| + // and is deleted whenever the number of references for listening reachers |
| + // zero. The message loop may hold weak pointers to it in order to ensure |
| + // tasks scheduled on it are not delivered after it is destroyed. |
| + class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
| + public: |
| + Core(MDnsClientImpl* client, |
| + MDnsConnectionFactory* connection_factory, |
| + base::TaskRunner* task_runner, |
| + base::Clock* clock); |
| + virtual ~Core(); |
| + |
| + // Initialize the core. Returns true on success. |
| + bool Init(); |
| + |
| + // Send a query with a specific rrtype and name. Returns true on success. |
| + bool SendQuery(uint16 rrtype, std::string name); |
| + |
| + // Add/remove a listener to the list of listener. May cause network traffic |
| + // if listener is active. |
| + void AddListener(MDnsListenerImpl* listener, bool alert_existing_records); |
| + void RemoveListener(MDnsListenerImpl* listener); |
| + |
| + // Query the cache for records of a specific type and name. |
| + void QueryCache(uint16 rrtype, const std::string& name, |
| + std::vector<const RecordParsed*>* records) const; |
| + |
| + // Parse the response and alert relevant listeners. |
| + virtual void HandlePacket(DnsResponse* response, int bytes_read) OVERRIDE; |
| + |
| + private: |
| + typedef std::pair<uint16, std::string> ListenerKey; |
| + typedef std::map<ListenerKey, ObserverList<MDnsListenerImpl>* > |
| + ListenerMap; |
| + |
| + // Alert listeners of an update to the cache. |
| + void AlertListeners(MDnsUpdateType update_type, |
| + const ListenerKey& key, const RecordParsed* record); |
| + |
| + // Schedule a cleanup to a specific time, cancelling other cleanups. |
| + void ScheduleCleanup(base::Time cleanup); |
| + |
| + // Clean up the cache and schedule a new cleanup. |
| + void DoCleanup(); |
| + |
| + // Callback for when a record is removed from the cache. |
| + void OnRecordRemoved(const RecordParsed* record); |
| + |
| + ListenerMap listeners_; |
| + |
| + MDnsClientImpl* client_; |
| + MDnsCache cache_; |
| + |
| + base::CancelableCallback<void()> cleanup_callback_; |
| + base::Time scheduled_cleanup_; |
| + |
| + scoped_refptr<base::TaskRunner> task_runner_; |
| + base::Clock* clock_; |
| + scoped_ptr<MDnsConnection> connection_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Core); |
| + }; |
| + |
| + MDnsClientImpl(); |
| + |
| + // Used only for testing. Lets users of this class decouple it from time |
| + // dependence and network dependence. |
| + MDnsClientImpl(base::Clock* clock, base::TaskRunner* task_runner, |
| + MDnsConnectionFactory* connection_factory); |
| + virtual ~MDnsClientImpl(); |
| + |
| + // Add delegate for RRType |rrtype| and name |name|. |
| + // If |name| is an empty string, listen to all notification of type |
| + // |rrtype|. |
| + virtual scoped_ptr<MDnsListener> CreateListener( |
| + uint16 rrtype, |
| + const std::string& name, |
| + bool active, |
| + bool alert_existing_records, |
| + MDnsListener::Delegate* delegate) OVERRIDE; |
| + |
| + // Create a transaction to Query MDNS for a single-value query |
| + // (A, AAAA, TXT, and SRV) asynchronously. May defer to cache. |
| + virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
| + uint16 rrtype, |
| + const std::string& name, |
| + const MDnsTransaction::ResultCallback& callback) OVERRIDE; |
| + |
| + // Functions for testing only. |
| + bool IsListeningForTests(); |
| + |
| + private: |
| + bool AddListenRef(); |
| + void SubtractListenRef(); |
| + void Shutdown(); |
| + |
| + scoped_ptr<Core> core_; |
| + int listen_refs_; |
| + |
| + // Since we can either own or not own clock, use both a scoped_ptr and a ptr. |
| + scoped_ptr<base::Clock> clock_owned_; |
| + base::Clock* clock_; |
| + |
| + // Since we can either own or not own the connection_factory, use a scoped_ptr |
| + // and a ptr. |
| + scoped_ptr<MDnsConnectionFactory> connection_factory_owned_; |
| + MDnsConnectionFactory* connection_factory_; |
| + |
| + scoped_refptr<base::TaskRunner> task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); |
| +}; |
| + |
| +class MDnsListenerImpl : public MDnsListener, |
| + public base::SupportsWeakPtr<MDnsListenerImpl> { |
| + public: |
| + MDnsListenerImpl(uint16 rrtype, |
| + const std::string& name, |
| + bool active, |
| + bool alert_existing_records, |
| + MDnsListener::Delegate* delegate, |
| + MDnsClientImpl::Core* core); |
| + |
| + // Destroying the listener stops listening. |
| + virtual ~MDnsListenerImpl(); |
| + |
| + // Get the host or service name for this query. |
| + // Return an empty string for no name. |
| + virtual const std::string& GetName() const OVERRIDE; |
| + |
| + // Get the type for this query (SRV, TXT, A, AAA, etc) |
| + virtual uint16 GetType() const OVERRIDE; |
| + |
| + virtual bool IsActive() const OVERRIDE; |
| + |
| + // Applies only to listeners with names. Will send out a query for new |
| + // information. |force_refresh_cache| will force a refresh of all cached |
| + // entities. |
| + virtual bool SendQuery(bool force_refresh_cache) OVERRIDE; |
| + |
| + // Applies only to listeners with names. Query mDNS cache synchronously for |
| + // either single- or multi- valued records. |
| + virtual bool QueryCache( |
| + std::vector<const RecordParsed*>* records) const OVERRIDE; |
| + |
| + MDnsListener::Delegate* delegate() { return delegate_; } |
| + |
| + void AlertDelegate(MDnsUpdateType update_type, |
| + const RecordParsed* record_parsed); |
| + private: |
| + uint16 rrtype_; |
| + std::string name_; |
| + bool active_; |
| + MDnsClientImpl::Core* parent_; |
| + MDnsListener::Delegate* delegate_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); |
| +}; |
| + |
| +class MDnsTransactionImpl : public MDnsTransaction, |
| + public base::SupportsWeakPtr<MDnsTransactionImpl>, |
| + public MDnsListener::Delegate { |
| + public: |
| + MDnsTransactionImpl(uint16 rrtype, |
| + const std::string& name, |
| + const MDnsTransaction::ResultCallback& callback, |
| + base::TaskRunner* task_runner); |
| + virtual ~MDnsTransactionImpl(); |
| + |
| + // Start the transaction. Returns true on success. |
| + bool Init(MDnsClientImpl* client, |
| + MDnsClientImpl::Core* core); |
| + |
| + // MDnsListener::Delegate implementation |
| + virtual void OnRecordUpdate(MDnsUpdateType update, |
| + const RecordParsed* record) OVERRIDE; |
| + virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; |
| + |
| + virtual const std::string& GetName() const OVERRIDE; |
| + virtual uint16 GetType() const OVERRIDE; |
| + |
| + private: |
| + // Trigger the callback and reset all related variables. |
| + void TriggerCallback(MDnsTransactionResult result, |
| + const RecordParsed* record); |
| + |
| + // Internal callback for when a cache record is found. |
| + void CacheRecordFound(const RecordParsed* record); |
| + |
| + // Callback for when the transaction times out. |
| + void OnTimedOut(); |
| + |
| + uint16 rrtype_; |
| + std::string name_; |
| + MDnsTransaction::ResultCallback callback_; |
| + |
| + bool triggered_; |
| + scoped_ptr<MDnsListener> listener_; |
| + base::CancelableCallback<void()> timeout_; |
| + scoped_refptr<base::TaskRunner> task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
| +}; |
| + |
| +// This implementation of MDnsConnection handles connecting to both IPv4 and |
| +// IPv6. |
| +class MDnsConnectionImpl : public base::SupportsWeakPtr<MDnsConnectionImpl>, |
| + public MDnsConnection { |
| + public: |
| + MDnsConnectionImpl(MDnsConnection::Delegate* delegate, |
| + base::TaskRunner* task_runner); |
| + virtual ~MDnsConnectionImpl(); |
| + |
| + virtual bool Init() OVERRIDE; |
| + virtual bool Send(IOBuffer* buffer, unsigned size) OVERRIDE; |
| + |
| + private: |
| + // Bind a socket with a specific address size to a specific multicast group |
| + // and port 5353. |
| + bool BindSocket(UDPSocket* socket, |
| + int addr_size, |
| + const char* multicast_group); |
| + |
| + // Callback for handling a datagram being recieved on either ipv4 or ipv6. |
| + // Responsible for ensuring we request another packet from the network. |
| + void OnDatagramReceived(UDPSocket* socket, |
| + DnsResponse* response, |
| + IPEndPoint* recv_addr, |
| + int bytes_read); |
| + |
| + // Request another packet from the network. |
| + bool ReceiveNextPacket(UDPSocket* socket, |
| + DnsResponse* response, |
| + IPEndPoint* recv_addr); |
| + |
| + // Callback for when sending a query has finished. |
| + void SendDone(int sent); |
| + |
| + // The IPEndPoints for sending/recieving packets on IPv4 and IPv6. |
| + IPEndPoint GetIPv4SendEndpoint(); |
| + IPEndPoint GetIPv6SendEndpoint(); |
| + |
| + scoped_ptr<UDPSocket> socket_ipv4_; |
| + scoped_ptr<UDPSocket> socket_ipv6_; |
| + |
| + scoped_ptr<DnsResponse> response_ipv4_; |
| + scoped_ptr<DnsResponse> response_ipv6_; |
| + |
| + // The address from which the last packet was recieved. |
| + IPEndPoint recv_addr_ipv4_; |
| + IPEndPoint recv_addr_ipv6_; |
| + |
| + MDnsConnection::Delegate* delegate_; |
| + |
| + scoped_refptr<base::TaskRunner> task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImpl); |
| +}; |
| + |
| +class MDnsConnectionImplFactory : public MDnsConnectionFactory { |
| + public: |
| + MDnsConnectionImplFactory(); |
| + virtual ~MDnsConnectionImplFactory(); |
| + |
| + virtual scoped_ptr<MDnsConnection> CreateConnection( |
| + MDnsConnection::Delegate* delegate, |
| + base::TaskRunner* task_runner) OVERRIDE; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImplFactory); |
| +}; |
| + |
| +} |
| +#endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |