Index: net/dns/mdns_listener_impl.h |
diff --git a/net/dns/mdns_listener_impl.h b/net/dns/mdns_listener_impl.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..128e69e9066061ff3bef7563af642a2d2f5656bf |
--- /dev/null |
+++ b/net/dns/mdns_listener_impl.h |
@@ -0,0 +1,253 @@ |
+// 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_LISTENER_IMPL_H_ |
+#define NET_DNS_MDNS_LISTENER_IMPL_H_ |
+ |
+#include <map> |
+#include <string> |
+#include <utility> |
+#include <vector> |
+ |
+#include "base/time/clock.h" |
+#include "base/task_runner.h" |
+#include "base/cancelable_callback.h" |
+#include "net/base/io_buffer.h" |
+#include "net/base/ip_endpoint.h" |
+#include "net/dns/mdns_cache.h" |
+#include "net/dns/mdns_listener.h" |
+#include "net/udp/udp_socket.h" |
+ |
+namespace net { |
+ |
+class MDnsListenerImpl; |
+ |
+class MDnsListenerFactoryImpl : public MDnsListenerFactory { |
+ public: |
+ class Core : public base::SupportsWeakPtr<Core> { |
szym
2013/05/24 15:32:22
Needs comment regarding lifetime. When is Core cre
Noam Samuel
2013/05/24 19:00:49
The weak pointer-ability of these classes is mostl
szym
2013/05/25 02:51:01
Still needs a comment in the code.
On 2013/05/24
Noam Samuel
2013/05/29 21:25:16
Done.
|
+ public: |
+ explicit Core(MDnsListenerFactoryImpl* factory); |
+ ~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; |
+ |
+ private: |
+ typedef std::pair<uint16, std::string> ListenerKey; |
+ |
+ typedef std::multimap<ListenerKey, MDnsListenerImpl*>::iterator |
+ ListenersIterator; |
+ |
+ // 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 OnDatagramRecieved(UDPSocket* socket, |
szym
2013/05/24 15:32:22
Throughout the file: s/Recieve/Receive/
Noam Samuel
2013/05/29 21:25:16
Done.
|
+ DnsResponse* response, |
+ IPEndPoint* recv_addr, |
+ int bytes_read); |
+ |
+ // Request another packet from the network. |
szym
2013/05/24 15:32:22
The implementation shows that this calls RecvFrom
Noam Samuel
2013/05/24 19:00:49
It does wait indefinitely. It does so in an asynch
|
+ bool RecieveOnePacket(UDPSocket* socket, |
+ DnsResponse* response, |
+ IPEndPoint* recv_addr); |
+ |
+ // Parse the response and alert relevant listeners. |
+ void HandlePacket(DnsResponse* response, int bytes_read); |
+ |
+ // 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 sending a query has finished. |
+ void SendDone(int sent); |
+ |
+ // Callback for when a record is removed from the cache. |
+ void OnRecordRemoved(const RecordParsed* record); |
+ |
+ std::multimap<ListenerKey, MDnsListenerImpl*> listeners_; |
+ |
+ 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_; |
+ |
+ MDnsListenerFactoryImpl* factory_; |
+ MDnsCache cache_; |
+ |
+ base::CancelableCallback<void()> cleanup_callback_; |
+ base::Time scheduled_cleanup_; |
szym
2013/05/24 15:32:22
DISALLOW_COPY_AND_ASSIGN.
Noam Samuel
2013/05/29 21:25:16
Done.
|
+ }; |
+ |
+ MDnsListenerFactoryImpl(); |
+ |
+ // Used only for testing. Lets users of this class decouple it from time |
+ // dependence. |
+ MDnsListenerFactoryImpl(base::Clock* clock, base::TaskRunner* task_runner); |
+ virtual ~MDnsListenerFactoryImpl(); |
+ |
+ // 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, |
+ MDnsListenerFactory::Delegate* delegate) OVERRIDE; |
szym
2013/05/24 15:32:22
You don't need MDnsListenerFactory:: here and else
Noam Samuel
2013/05/29 21:25:16
Moved all the delegates and callbacks to their res
|
+ |
+ // 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 MDnsListenerFactory::QueryCallback& callback) OVERRIDE; |
+ |
+ // Functions for testing only. |
+ bool IsListeningForTests(); |
+ |
+ protected: |
+ // These are couplings to multicast networking that tests will need to |
+ // override in order to ensure the tests run only on the loopback network. |
+ |
+ // Tests will need to redirect multicast queries to a unicast destination. |
+ virtual IPEndPoint GetIPv4SendEndpoint(); |
+ virtual IPEndPoint GetIPv6SendEndpoint(); |
+ |
+ // Tests will need to control the binding behavior of sockets |
+ // (e.g. not use REUSEADDR and not join the multicast group). |
+ virtual bool BindToAddressAndMulticastGroup(UDPSocket* socket, |
+ const IPEndPoint& address, |
+ const IPAddressNumber& group); |
+ |
+ private: |
+ bool AddListenRef(); |
+ void SubtractListenRef(); |
+ |
+ 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_; |
+ |
+ scoped_refptr<base::TaskRunner> task_runner_; |
+}; |
+ |
+class MDnsListenerImpl : public MDnsListener, |
+ public base::SupportsWeakPtr<MDnsListenerImpl> { |
+ public: |
+ MDnsListenerImpl(uint16 rrtype, |
+ const std::string& name, |
+ bool active, |
+ bool alert_existing_records, |
+ MDnsListenerFactory::Delegate* delegate, |
+ MDnsListenerFactoryImpl::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; |
+ |
+ MDnsListenerFactory::Delegate* delegate() { return delegate_; } |
+ |
+ void AlertDelegate(MDnsUpdateType update_type, |
+ const RecordParsed* record_parsed); |
+ private: |
+ uint16 rrtype_; |
+ std::string name_; |
+ bool active_; |
+ MDnsListenerFactoryImpl::Core* parent_; |
+ MDnsListenerFactory::Delegate* delegate_; |
+}; |
+ |
+class MDnsTransactionImpl : public MDnsTransaction, |
+ public base::SupportsWeakPtr<MDnsTransactionImpl>, |
+ public MDnsListenerFactoryImpl::Delegate { |
+ public: |
+ MDnsTransactionImpl(uint16 rrtype, |
+ const std::string& name, |
+ const MDnsListenerFactory::QueryCallback& callback, |
+ base::TaskRunner* task_runner); |
+ virtual ~MDnsTransactionImpl(); |
+ |
+ // Start the transaction. Returns true on success. |
+ bool Init(MDnsListenerFactoryImpl* factory, |
+ MDnsListenerFactoryImpl::Core* core); |
+ |
+ // MDnsListenerFactoryImpl::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_; |
+ MDnsListenerFactory::QueryCallback callback_; |
+ |
+ bool triggered_; |
+ scoped_ptr<MDnsListener> listener_; |
+ base::CancelableCallback<void()> timeout_; |
+ scoped_refptr<base::TaskRunner> task_runner_; |
+}; |
+} |
+#endif // NET_DNS_MDNS_LISTENER_IMPL_H_ |