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..975db9f6cb1788fae826c5ac8dabfec1b291ad41 |
--- /dev/null |
+++ b/net/dns/mdns_client_impl.h |
@@ -0,0 +1,324 @@ |
+// 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 "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/datagram_server_socket.h" |
+#include "net/udp/udp_server_socket.h" |
+#include "net/udp/udp_socket.h" |
+ |
+ |
+namespace net { |
+ |
+class MDnsListenerImpl; |
+ |
+// 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 { |
+ 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 void OnConnectionError(int error) = 0; |
+ virtual ~Delegate() {} |
+ }; |
+ |
+ // Start the connection. Will begin alerting the delegate of packets. |
+ // Returns a net error code. |
+ virtual int Init() = 0; |
+ |
+ // Send a packet on both ipv4 and ipv6. Returns a net error code. |
+ virtual int Send(IOBuffer* buffer, unsigned size) = 0; |
+ virtual ~MDnsConnection() {} |
+}; |
+ |
+class MDnsConnectionFactory { |
+ public: |
+ virtual scoped_ptr<MDnsConnection> CreateConnection( |
+ MDnsConnection::Delegate* delegate) = 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 |
szym
2013/06/10 21:58:29
nit: "reaches"
Suggest: "whenever the number of l
Noam Samuel
2013/06/11 20:35:03
Done.
|
+ // 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. |
szym
2013/06/10 21:58:29
The last sentence does not seem to be adding much.
Noam Samuel
2013/06/11 20:35:03
Done.
|
+ class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
+ public: |
+ Core(MDnsClientImpl* client, |
+ MDnsConnectionFactory* connection_factory); |
+ 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); |
+ 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; |
+ |
+ virtual void OnConnectionError(int error) 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_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. |
+ explicit MDnsClientImpl(MDnsConnectionFactory* connection_factory); |
szym
2013/06/10 21:58:29
I suggest always passing the factory into the cons
Noam Samuel
2013/06/11 20:35:03
Done.
|
+ virtual ~MDnsClientImpl(); |
+ |
+ // MDnsClient implementation: |
+ virtual scoped_ptr<MDnsListener> CreateListener( |
+ uint16 rrtype, |
+ const std::string& name, |
+ MDnsListener::Delegate* delegate) OVERRIDE; |
+ |
+ virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
+ uint16 rrtype, |
+ const std::string& name, |
+ int flags, |
+ const MDnsTransaction::ResultCallback& callback) OVERRIDE; |
+ |
+ |
+ // Functions for testing only. |
+ bool IsListeningForTests(); |
+ |
+ bool AddListenRef(); |
+ void SubtractListenRef(); |
+ |
+ Core* core() { return core_.get(); } |
+ |
+ private: |
+ void Shutdown(); |
+ |
+ scoped_ptr<Core> core_; |
+ int listen_refs_; |
+ |
+ // 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_; |
szym
2013/06/10 21:58:29
I suggest it should always own it. Whenever a situ
Noam Samuel
2013/06/11 20:35:03
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); |
+}; |
+ |
+class MDnsListenerImpl : public MDnsListener, |
+ public base::SupportsWeakPtr<MDnsListenerImpl> { |
+ public: |
+ MDnsListenerImpl(uint16 rrtype, |
+ const std::string& name, |
+ MDnsListener::Delegate* delegate, |
+ MDnsClientImpl* client); |
+ |
+ virtual ~MDnsListenerImpl(); |
+ |
+ // MDnsListener implementation: |
+ virtual bool Start() OVERRIDE; |
+ |
+ virtual const std::string& GetName() const OVERRIDE; |
+ |
+ virtual uint16 GetType() const OVERRIDE; |
+ |
+ MDnsListener::Delegate* delegate() { return delegate_; } |
+ |
+ // Alert the delegate of a record update. |
+ void AlertDelegate(MDnsUpdateType update_type, |
+ const RecordParsed* record_parsed); |
+ private: |
+ uint16 rrtype_; |
+ std::string name_; |
+ MDnsClientImpl* client_; |
+ MDnsListener::Delegate* delegate_; |
+ |
+ bool started_; |
+ DISALLOW_COPY_AND_ASSIGN(MDnsListenerImpl); |
+}; |
+ |
+class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>, |
+ public MDnsTransaction, |
+ public MDnsListener::Delegate { |
+ public: |
+ MDnsTransactionImpl(uint16 rrtype, |
+ const std::string& name, |
+ int flags, |
+ const MDnsTransaction::ResultCallback& callback, |
+ MDnsClientImpl* client); |
+ virtual ~MDnsTransactionImpl(); |
+ |
+ // MDnsTransaction implementation: |
+ virtual bool Start() OVERRIDE; |
+ |
+ virtual const std::string& GetName() const OVERRIDE; |
+ virtual uint16 GetType() const OVERRIDE; |
+ |
+ // MDnsListener::Delegate implementation: |
+ virtual void OnRecordUpdate(MDnsUpdateType update, |
+ const RecordParsed* record) OVERRIDE; |
+ virtual void OnNsecRecord(const std::string& name, unsigned type) OVERRIDE; |
+ |
+ virtual void OnCachePurged() OVERRIDE; |
+ |
+ bool is_active() { return !callback_.is_null(); } |
+ |
+ void Reset(); |
+ |
+ 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); |
+ |
+ // Signal the transactionis over and release all related resources. |
+ void SignalTransactionOver(); |
+ |
+ uint16 rrtype_; |
+ std::string name_; |
+ MDnsTransaction::ResultCallback callback_; |
+ |
+ scoped_ptr<MDnsListener> listener_; |
+ base::CancelableCallback<void()> timeout_; |
+ |
+ MDnsClientImpl* client_; |
+ |
+ bool started_; |
+ int flags_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
+}; |
+ |
+// This implementation of MDnsConnection handles connecting to both IPv4 and |
+// IPv6. |
+class MDnsConnectionImpl : public MDnsConnection { |
+ public: |
+ explicit MDnsConnectionImpl(MDnsConnection::Delegate* delegate); |
+ MDnsConnectionImpl(DatagramServerSocket* socket_ipv4, |
+ DatagramServerSocket* socket_ipv6, |
+ MDnsConnection::Delegate* delegate); |
+ |
+ virtual ~MDnsConnectionImpl(); |
+ |
+ virtual int Init() OVERRIDE; |
+ virtual int Send(IOBuffer* buffer, unsigned size) OVERRIDE; |
+ |
+ private: |
+ class SocketHandler { |
+ public: |
+ // Pass the socket in. Used for testing. |
+ SocketHandler(DatagramServerSocket* socket, MDnsConnectionImpl* connection, |
+ const IPEndPoint& multicast_addr); |
+ SocketHandler(MDnsConnectionImpl* connection, |
+ const IPEndPoint& multicast_addr); |
+ ~SocketHandler(); |
+ int DoLoop(int rv); |
+ int Start(); |
+ |
+ int Send(IOBuffer* buffer, unsigned size); |
+ |
+ private: |
+ int BindSocket(); |
+ void OnDatagramReceived(int rv); |
+ |
+ // Callback for when sending a query has finished. |
+ void SendDone(int sent); |
+ |
+ scoped_ptr<DatagramServerSocket> socket_owned_; |
+ DatagramServerSocket* socket_; |
+ |
+ MDnsConnectionImpl* connection_; |
+ IPEndPoint recv_addr_; |
+ scoped_ptr<DnsResponse> response_; |
+ IPEndPoint multicast_addr_; |
+ }; |
+ |
+ // Callback for handling a datagram being recieved on either ipv4 or ipv6. |
szym
2013/06/10 21:58:29
nit: "received"
Noam Samuel
2013/06/11 20:35:03
Done.
|
+ // Responsible for ensuring we request another packet from the network. |
szym
2013/06/10 21:58:29
This sentence is obsolete.
Noam Samuel
2013/06/11 20:35:03
Done.
|
+ void OnDatagramReceived(DatagramServerSocket* socket, |
+ DnsResponse* response, |
+ IPEndPoint* recv_addr, |
+ int bytes_read); |
+ |
+ void OnError(SocketHandler* loop, int error); |
+ |
+ IPEndPoint GetIPEndPoint(const char* address, int port); |
+ |
+ SocketHandler socket_handler_ipv4_; |
+ SocketHandler socket_handler_ipv6_; |
+ |
+ MDnsConnection::Delegate* delegate_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImpl); |
+}; |
+ |
+class MDnsConnectionImplFactory : public MDnsConnectionFactory { |
+ public: |
+ MDnsConnectionImplFactory(); |
+ virtual ~MDnsConnectionImplFactory(); |
+ |
+ virtual scoped_ptr<MDnsConnection> CreateConnection( |
+ MDnsConnection::Delegate* delegate) OVERRIDE; |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(MDnsConnectionImplFactory); |
+}; |
+ |
+} |
+#endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |