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 <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/cancelable_callback.h" | 13 #include "base/cancelable_callback.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "net/base/io_buffer.h" | 16 #include "net/base/io_buffer.h" |
17 #include "net/base/ip_endpoint.h" | 17 #include "net/base/ip_endpoint.h" |
18 #include "net/dns/mdns_cache.h" | 18 #include "net/dns/mdns_cache.h" |
19 #include "net/dns/mdns_client.h" | 19 #include "net/dns/mdns_client.h" |
20 #include "net/udp/datagram_server_socket.h" | 20 #include "net/udp/datagram_server_socket.h" |
21 #include "net/udp/udp_server_socket.h" | 21 #include "net/udp/udp_server_socket.h" |
22 #include "net/udp/udp_socket.h" | 22 #include "net/udp/udp_socket.h" |
23 | 23 |
24 namespace net { | 24 namespace net { |
25 | 25 |
| 26 class MDnsSocketFactoryImpl : public MDnsSocketFactory { |
| 27 public: |
| 28 MDnsSocketFactoryImpl() {}; |
| 29 virtual ~MDnsSocketFactoryImpl() {}; |
| 30 |
| 31 virtual void CreateSockets( |
| 32 ScopedVector<DatagramServerSocket>* sockets) OVERRIDE; |
| 33 |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(MDnsSocketFactoryImpl); |
| 36 }; |
| 37 |
26 // A connection to the network for multicast DNS clients. It reads data into | 38 // A connection to the network for multicast DNS clients. It reads data into |
27 // DnsResponse objects and alerts the delegate that a packet has been received. | 39 // DnsResponse objects and alerts the delegate that a packet has been received. |
28 class NET_EXPORT_PRIVATE MDnsConnection { | 40 class NET_EXPORT_PRIVATE MDnsConnection { |
29 public: | 41 public: |
30 class SocketFactory { | |
31 public: | |
32 virtual ~SocketFactory() {} | |
33 | |
34 virtual scoped_ptr<DatagramServerSocket> CreateSocket() = 0; | |
35 | |
36 static scoped_ptr<SocketFactory> CreateDefault(); | |
37 }; | |
38 | |
39 class Delegate { | 42 class Delegate { |
40 public: | 43 public: |
41 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. | 44 // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. |
42 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; | 45 virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; |
43 virtual void OnConnectionError(int error) = 0; | 46 virtual void OnConnectionError(int error) = 0; |
44 virtual ~Delegate() {} | 47 virtual ~Delegate() {} |
45 }; | 48 }; |
46 | 49 |
47 explicit MDnsConnection(MDnsConnection::Delegate* delegate); | 50 explicit MDnsConnection(MDnsConnection::Delegate* delegate); |
48 | |
49 virtual ~MDnsConnection(); | 51 virtual ~MDnsConnection(); |
50 | 52 |
51 // Both methods return true if at least one of the socket handlers succeeded. | 53 // Both methods return true if at least one of the socket handlers succeeded. |
52 bool Init(MDnsConnection::SocketFactory* socket_factory); | 54 bool Init(MDnsSocketFactory* socket_factory); |
53 bool Send(IOBuffer* buffer, unsigned size); | 55 bool Send(IOBuffer* buffer, unsigned size); |
54 | 56 |
55 private: | 57 private: |
56 class SocketHandler { | 58 class SocketHandler { |
57 public: | 59 public: |
58 SocketHandler(MDnsConnection* connection, | 60 SocketHandler(scoped_ptr<DatagramServerSocket> socket, |
59 const IPEndPoint& multicast_addr, | 61 MDnsConnection* connection); |
60 SocketFactory* socket_factory); | |
61 ~SocketHandler(); | 62 ~SocketHandler(); |
62 int Bind(); | 63 |
63 int Start(); | 64 int Start(); |
64 | |
65 int Send(IOBuffer* buffer, unsigned size); | 65 int Send(IOBuffer* buffer, unsigned size); |
66 | 66 |
67 private: | 67 private: |
68 int DoLoop(int rv); | 68 int DoLoop(int rv); |
69 void OnDatagramReceived(int rv); | 69 void OnDatagramReceived(int rv); |
70 | 70 |
71 // Callback for when sending a query has finished. | 71 // Callback for when sending a query has finished. |
72 void SendDone(int rv); | 72 void SendDone(int rv); |
73 | 73 |
74 scoped_ptr<DatagramServerSocket> socket_; | 74 scoped_ptr<DatagramServerSocket> socket_; |
75 | |
76 MDnsConnection* connection_; | 75 MDnsConnection* connection_; |
77 IPEndPoint recv_addr_; | 76 IPEndPoint recv_addr_; |
78 scoped_ptr<DnsResponse> response_; | 77 DnsResponse response_; |
79 IPEndPoint multicast_addr_; | 78 IPEndPoint multicast_addr_; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(SocketHandler); |
80 }; | 81 }; |
81 | 82 |
82 // Callback for handling a datagram being received on either ipv4 or ipv6. | 83 // Callback for handling a datagram being received on either ipv4 or ipv6. |
83 void OnDatagramReceived(DnsResponse* response, | 84 void OnDatagramReceived(DnsResponse* response, |
84 const IPEndPoint& recv_addr, | 85 const IPEndPoint& recv_addr, |
85 int bytes_read); | 86 int bytes_read); |
86 | 87 |
87 void OnError(SocketHandler* loop, int error); | 88 void OnError(SocketHandler* loop, int error); |
88 | 89 |
89 // Only socket handlers which successfully bound and started are kept. | 90 // Only socket handlers which successfully bound and started are kept. |
(...skipping 11 matching lines...) Expand all Loading... |
101 // The core object exists while the MDnsClient is listening, and is deleted | 102 // The core object exists while the MDnsClient is listening, and is deleted |
102 // whenever the number of listeners reaches zero. The deletion happens | 103 // whenever the number of listeners reaches zero. The deletion happens |
103 // asychronously, so destroying the last listener does not immediately | 104 // asychronously, so destroying the last listener does not immediately |
104 // invalidate the core. | 105 // invalidate the core. |
105 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { | 106 class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
106 public: | 107 public: |
107 explicit Core(MDnsClientImpl* client); | 108 explicit Core(MDnsClientImpl* client); |
108 virtual ~Core(); | 109 virtual ~Core(); |
109 | 110 |
110 // Initialize the core. Returns true on success. | 111 // Initialize the core. Returns true on success. |
111 bool Init(MDnsConnection::SocketFactory* socket_factory); | 112 bool Init(MDnsSocketFactory* socket_factory); |
112 | 113 |
113 // Send a query with a specific rrtype and name. Returns true on success. | 114 // Send a query with a specific rrtype and name. Returns true on success. |
114 bool SendQuery(uint16 rrtype, std::string name); | 115 bool SendQuery(uint16 rrtype, std::string name); |
115 | 116 |
116 // Add/remove a listener to the list of listeners. | 117 // Add/remove a listener to the list of listeners. |
117 void AddListener(MDnsListenerImpl* listener); | 118 void AddListener(MDnsListenerImpl* listener); |
118 void RemoveListener(MDnsListenerImpl* listener); | 119 void RemoveListener(MDnsListenerImpl* listener); |
119 | 120 |
120 // Query the cache for records of a specific type and name. | 121 // Query the cache for records of a specific type and name. |
121 void QueryCache(uint16 rrtype, const std::string& name, | 122 void QueryCache(uint16 rrtype, const std::string& name, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 MDnsCache cache_; | 157 MDnsCache cache_; |
157 | 158 |
158 base::CancelableCallback<void()> cleanup_callback_; | 159 base::CancelableCallback<void()> cleanup_callback_; |
159 base::Time scheduled_cleanup_; | 160 base::Time scheduled_cleanup_; |
160 | 161 |
161 scoped_ptr<MDnsConnection> connection_; | 162 scoped_ptr<MDnsConnection> connection_; |
162 | 163 |
163 DISALLOW_COPY_AND_ASSIGN(Core); | 164 DISALLOW_COPY_AND_ASSIGN(Core); |
164 }; | 165 }; |
165 | 166 |
166 explicit MDnsClientImpl( | 167 MDnsClientImpl(); |
167 scoped_ptr<MDnsConnection::SocketFactory> socket_factory_); | |
168 virtual ~MDnsClientImpl(); | 168 virtual ~MDnsClientImpl(); |
169 | 169 |
170 // MDnsClient implementation: | 170 // MDnsClient implementation: |
171 virtual scoped_ptr<MDnsListener> CreateListener( | 171 virtual scoped_ptr<MDnsListener> CreateListener( |
172 uint16 rrtype, | 172 uint16 rrtype, |
173 const std::string& name, | 173 const std::string& name, |
174 MDnsListener::Delegate* delegate) OVERRIDE; | 174 MDnsListener::Delegate* delegate) OVERRIDE; |
175 | 175 |
176 virtual scoped_ptr<MDnsTransaction> CreateTransaction( | 176 virtual scoped_ptr<MDnsTransaction> CreateTransaction( |
177 uint16 rrtype, | 177 uint16 rrtype, |
178 const std::string& name, | 178 const std::string& name, |
179 int flags, | 179 int flags, |
180 const MDnsTransaction::ResultCallback& callback) OVERRIDE; | 180 const MDnsTransaction::ResultCallback& callback) OVERRIDE; |
181 | 181 |
182 virtual bool StartListening() OVERRIDE; | 182 virtual bool StartListening(MDnsSocketFactory* socket_factory) OVERRIDE; |
183 virtual void StopListening() OVERRIDE; | 183 virtual void StopListening() OVERRIDE; |
184 virtual bool IsListening() const OVERRIDE; | 184 virtual bool IsListening() const OVERRIDE; |
185 | 185 |
186 Core* core() { return core_.get(); } | 186 Core* core() { return core_.get(); } |
187 | 187 |
188 private: | 188 private: |
189 scoped_ptr<Core> core_; | 189 scoped_ptr<Core> core_; |
190 | 190 |
191 scoped_ptr<MDnsConnection::SocketFactory> socket_factory_; | |
192 | |
193 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); | 191 DISALLOW_COPY_AND_ASSIGN(MDnsClientImpl); |
194 }; | 192 }; |
195 | 193 |
196 class MDnsListenerImpl : public MDnsListener, | 194 class MDnsListenerImpl : public MDnsListener, |
197 public base::SupportsWeakPtr<MDnsListenerImpl> { | 195 public base::SupportsWeakPtr<MDnsListenerImpl> { |
198 public: | 196 public: |
199 MDnsListenerImpl(uint16 rrtype, | 197 MDnsListenerImpl(uint16 rrtype, |
200 const std::string& name, | 198 const std::string& name, |
201 MDnsListener::Delegate* delegate, | 199 MDnsListener::Delegate* delegate, |
202 MDnsClientImpl* client); | 200 MDnsClientImpl* client); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
287 MDnsClientImpl* client_; | 285 MDnsClientImpl* client_; |
288 | 286 |
289 bool started_; | 287 bool started_; |
290 int flags_; | 288 int flags_; |
291 | 289 |
292 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); | 290 DISALLOW_COPY_AND_ASSIGN(MDnsTransactionImpl); |
293 }; | 291 }; |
294 | 292 |
295 } // namespace net | 293 } // namespace net |
296 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ | 294 #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |
OLD | NEW |