[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_DNS_MDNS_CLIENT_IMPL_H_ |
| 6 | #define NET_DNS_MDNS_CLIENT_IMPL_H_ |
| 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 10 | #include <map> |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 12 | #include <string> |
| 13 | #include <utility> |
| 14 | #include <vector> |
| 15 | |
| 16 | #include "base/cancelable_callback.h" |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 17 | #include "base/containers/queue.h" |
thestig | a0e18cd | 2015-09-25 04:58:36 | [diff] [blame] | 18 | #include "base/gtest_prod_util.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 19 | #include "base/macros.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 20 | #include "base/observer_list.h" |
| 21 | #include "net/base/io_buffer.h" |
| 22 | #include "net/base/ip_endpoint.h" |
bnc | 81c46c1f | 2016-10-04 16:25:59 | [diff] [blame] | 23 | #include "net/base/net_export.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 24 | #include "net/dns/mdns_cache.h" |
| 25 | #include "net/dns/mdns_client.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 26 | #include "net/socket/datagram_server_socket.h" |
| 27 | #include "net/socket/udp_server_socket.h" |
| 28 | #include "net/socket/udp_socket.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 29 | |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 30 | namespace base { |
| 31 | class Clock; |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 32 | class OneShotTimer; |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 33 | } // namespace base |
| 34 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 35 | namespace net { |
| 36 | |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 37 | class NetLog; |
| 38 | |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 39 | class MDnsSocketFactoryImpl : public MDnsSocketFactory { |
| 40 | public: |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 41 | MDnsSocketFactoryImpl() : net_log_(nullptr) {} |
| 42 | explicit MDnsSocketFactoryImpl(NetLog* net_log) : net_log_(net_log) {} |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 43 | |
| 44 | MDnsSocketFactoryImpl(const MDnsSocketFactoryImpl&) = delete; |
| 45 | MDnsSocketFactoryImpl& operator=(const MDnsSocketFactoryImpl&) = delete; |
| 46 | |
juncai | 1ee189bd | 2017-06-09 04:25:43 | [diff] [blame] | 47 | ~MDnsSocketFactoryImpl() override {} |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 48 | |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 49 | void CreateSockets( |
| 50 | std::vector<std::unique_ptr<DatagramServerSocket>>* sockets) override; |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 51 | |
| 52 | private: |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 53 | NetLog* const net_log_; |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 54 | }; |
| 55 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 56 | // A connection to the network for multicast DNS clients. It reads data into |
| 57 | // DnsResponse objects and alerts the delegate that a packet has been received. |
[email protected] | ba77c27f | 2013-07-02 22:34:32 | [diff] [blame] | 58 | class NET_EXPORT_PRIVATE MDnsConnection { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 59 | public: |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 60 | class Delegate { |
| 61 | public: |
| 62 | // Handle an mDNS packet buffered in |response| with a size of |bytes_read|. |
| 63 | virtual void HandlePacket(DnsResponse* response, int bytes_read) = 0; |
| 64 | virtual void OnConnectionError(int error) = 0; |
| 65 | virtual ~Delegate() {} |
| 66 | }; |
| 67 | |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 68 | explicit MDnsConnection(MDnsConnection::Delegate* delegate); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 69 | |
| 70 | MDnsConnection(const MDnsConnection&) = delete; |
| 71 | MDnsConnection& operator=(const MDnsConnection&) = delete; |
| 72 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 73 | virtual ~MDnsConnection(); |
| 74 | |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 75 | // Succeeds if at least one of the socket handlers succeeded. |
| 76 | int Init(MDnsSocketFactory* socket_factory); |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 77 | void Send(const scoped_refptr<IOBuffer>& buffer, unsigned size); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | class SocketHandler { |
| 81 | public: |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 82 | SocketHandler(std::unique_ptr<DatagramServerSocket> socket, |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 83 | MDnsConnection* connection); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 84 | |
| 85 | SocketHandler(const SocketHandler&) = delete; |
| 86 | SocketHandler& operator=(const SocketHandler&) = delete; |
| 87 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 88 | ~SocketHandler(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 89 | |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 90 | int Start(); |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 91 | void Send(const scoped_refptr<IOBuffer>& buffer, unsigned size); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 92 | |
| 93 | private: |
[email protected] | 03dd9425 | 2013-09-01 23:25:31 | [diff] [blame] | 94 | int DoLoop(int rv); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 95 | void OnDatagramReceived(int rv); |
| 96 | |
| 97 | // Callback for when sending a query has finished. |
| 98 | void SendDone(int rv); |
| 99 | |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 100 | std::unique_ptr<DatagramServerSocket> socket_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 101 | MDnsConnection* connection_; |
| 102 | IPEndPoint recv_addr_; |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 103 | DnsResponse response_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 104 | IPEndPoint multicast_addr_; |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 105 | bool send_in_progress_; |
Brett Wilson | c6a0c82 | 2017-09-12 00:04:29 | [diff] [blame] | 106 | base::queue<std::pair<scoped_refptr<IOBuffer>, unsigned>> send_queue_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | // Callback for handling a datagram being received on either ipv4 or ipv6. |
| 110 | void OnDatagramReceived(DnsResponse* response, |
| 111 | const IPEndPoint& recv_addr, |
| 112 | int bytes_read); |
| 113 | |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 114 | void PostOnError(SocketHandler* loop, int rv); |
| 115 | void OnError(int rv); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 116 | |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 117 | // Only socket handlers which successfully bound and started are kept. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 118 | std::vector<std::unique_ptr<SocketHandler>> socket_handlers_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 119 | |
| 120 | Delegate* delegate_; |
| 121 | |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 122 | base::WeakPtrFactory<MDnsConnection> weak_ptr_factory_{this}; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | class MDnsListenerImpl; |
| 126 | |
[email protected] | ba77c27f | 2013-07-02 22:34:32 | [diff] [blame] | 127 | class NET_EXPORT_PRIVATE MDnsClientImpl : public MDnsClient { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 128 | public: |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 129 | // The core object exists while the MDnsClient is listening, and is deleted |
| 130 | // whenever the number of listeners reaches zero. The deletion happens |
| 131 | // asychronously, so destroying the last listener does not immediately |
| 132 | // invalidate the core. |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 133 | class Core : public base::SupportsWeakPtr<Core>, MDnsConnection::Delegate { |
| 134 | public: |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 135 | Core(base::Clock* clock, base::OneShotTimer* timer); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 136 | |
| 137 | Core(const Core&) = delete; |
| 138 | Core& operator=(const Core&) = delete; |
| 139 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 140 | ~Core() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 141 | |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 142 | // Initialize the core. |
| 143 | int Init(MDnsSocketFactory* socket_factory); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 144 | |
| 145 | // Send a query with a specific rrtype and name. Returns true on success. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 146 | bool SendQuery(uint16_t rrtype, const std::string& name); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 147 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 148 | // Add/remove a listener to the list of listeners. |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 149 | void AddListener(MDnsListenerImpl* listener); |
| 150 | void RemoveListener(MDnsListenerImpl* listener); |
| 151 | |
| 152 | // Query the cache for records of a specific type and name. |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 153 | void QueryCache(uint16_t rrtype, |
| 154 | const std::string& name, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 155 | std::vector<const RecordParsed*>* records) const; |
| 156 | |
| 157 | // Parse the response and alert relevant listeners. |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 158 | void HandlePacket(DnsResponse* response, int bytes_read) override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 159 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 160 | void OnConnectionError(int error) override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 161 | |
Eric Orth | acdd8ae | 2019-03-29 22:07:19 | [diff] [blame] | 162 | MDnsCache* cache_for_testing() { return &cache_; } |
| 163 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 164 | private: |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 165 | FRIEND_TEST_ALL_PREFIXES(MDnsTest, CacheCleanupWithShortTTL); |
| 166 | |
Piotr Pawliczek | 3318cc9 | 2020-07-25 05:10:13 | [diff] [blame] | 167 | class ListenerKey { |
| 168 | public: |
| 169 | ListenerKey(const std::string& name, uint16_t type); |
| 170 | ListenerKey(const ListenerKey&) = default; |
| 171 | ListenerKey(ListenerKey&&) = default; |
| 172 | bool operator<(const ListenerKey& key) const; |
| 173 | const std::string& name_lowercase() const { return name_lowercase_; } |
| 174 | uint16_t type() const { return type_; } |
| 175 | |
| 176 | private: |
| 177 | std::string name_lowercase_; |
| 178 | uint16_t type_; |
| 179 | }; |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 180 | typedef base::ObserverList<MDnsListenerImpl>::Unchecked ObserverListType; |
| 181 | typedef std::map<ListenerKey, std::unique_ptr<ObserverListType>> |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 182 | ListenerMap; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 183 | |
| 184 | // Alert listeners of an update to the cache. |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 185 | void AlertListeners(MDnsCache::UpdateType update_type, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 186 | const ListenerKey& key, const RecordParsed* record); |
| 187 | |
| 188 | // Schedule a cache cleanup to a specific time, cancelling other cleanups. |
| 189 | void ScheduleCleanup(base::Time cleanup); |
| 190 | |
| 191 | // Clean up the cache and schedule a new cleanup. |
| 192 | void DoCleanup(); |
| 193 | |
| 194 | // Callback for when a record is removed from the cache. |
| 195 | void OnRecordRemoved(const RecordParsed* record); |
| 196 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 197 | void NotifyNsecRecord(const RecordParsed* record); |
| 198 | |
| 199 | // Delete and erase the observer list for |key|. Only deletes the observer |
| 200 | // list if is empty. |
| 201 | void CleanupObserverList(const ListenerKey& key); |
| 202 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 203 | ListenerMap listeners_; |
| 204 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 205 | MDnsCache cache_; |
| 206 | |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 207 | base::Clock* clock_; |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 208 | base::OneShotTimer* cleanup_timer_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 209 | base::Time scheduled_cleanup_; |
| 210 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 211 | std::unique_ptr<MDnsConnection> connection_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 212 | }; |
| 213 | |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 214 | MDnsClientImpl(); |
Eric Orth | 026776a | 2019-01-18 00:13:28 | [diff] [blame] | 215 | |
| 216 | // Test constructor, takes a mock clock and mock timer. |
| 217 | MDnsClientImpl(base::Clock* clock, |
| 218 | std::unique_ptr<base::OneShotTimer> cleanup_timer); |
| 219 | |
Peter Boström | 407869b | 2021-10-07 04:42:48 | [diff] [blame] | 220 | MDnsClientImpl(const MDnsClientImpl&) = delete; |
| 221 | MDnsClientImpl& operator=(const MDnsClientImpl&) = delete; |
| 222 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 223 | ~MDnsClientImpl() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 224 | |
| 225 | // MDnsClient implementation: |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 226 | std::unique_ptr<MDnsListener> CreateListener( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 227 | uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 228 | const std::string& name, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 229 | MDnsListener::Delegate* delegate) override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 230 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 231 | std::unique_ptr<MDnsTransaction> CreateTransaction( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 232 | uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 233 | const std::string& name, |
| 234 | int flags, |
mostynb | ba063d603 | 2014-10-09 11:01:13 | [diff] [blame] | 235 | const MDnsTransaction::ResultCallback& callback) override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 236 | |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 237 | int StartListening(MDnsSocketFactory* socket_factory) override; |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 238 | void StopListening() override; |
| 239 | bool IsListening() const override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 240 | |
| 241 | Core* core() { return core_.get(); } |
| 242 | |
| 243 | private: |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 244 | base::Clock* clock_; |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 245 | std::unique_ptr<base::OneShotTimer> cleanup_timer_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 246 | |
Eric Orth | 6cb27ea | 2019-05-03 16:31:04 | [diff] [blame] | 247 | std::unique_ptr<Core> core_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | class MDnsListenerImpl : public MDnsListener, |
| 251 | public base::SupportsWeakPtr<MDnsListenerImpl> { |
| 252 | public: |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 253 | MDnsListenerImpl(uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 254 | const std::string& name, |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 255 | base::Clock* clock, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 256 | MDnsListener::Delegate* delegate, |
| 257 | MDnsClientImpl* client); |
| 258 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 259 | MDnsListenerImpl(const MDnsListenerImpl&) = delete; |
| 260 | MDnsListenerImpl& operator=(const MDnsListenerImpl&) = delete; |
| 261 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 262 | ~MDnsListenerImpl() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 263 | |
| 264 | // MDnsListener implementation: |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 265 | bool Start() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 266 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 267 | // Actively refresh any received records. |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 268 | void SetActiveRefresh(bool active_refresh) override; |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 269 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 270 | const std::string& GetName() const override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 271 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 272 | uint16_t GetType() const override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 273 | |
| 274 | MDnsListener::Delegate* delegate() { return delegate_; } |
| 275 | |
| 276 | // Alert the delegate of a record update. |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 277 | void HandleRecordUpdate(MDnsCache::UpdateType update_type, |
| 278 | const RecordParsed* record_parsed); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 279 | |
| 280 | // Alert the delegate of the existence of an Nsec record. |
| 281 | void AlertNsecRecord(); |
| 282 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 283 | private: |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 284 | void ScheduleNextRefresh(); |
| 285 | void DoRefresh(); |
| 286 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 287 | uint16_t rrtype_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 288 | std::string name_; |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 289 | base::Clock* clock_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 290 | MDnsClientImpl* client_; |
| 291 | MDnsListener::Delegate* delegate_; |
| 292 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 293 | base::Time last_update_; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 294 | uint32_t ttl_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 295 | bool started_; |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 296 | bool active_refresh_; |
| 297 | |
Alex Turner | 3ae7462 | 2020-11-25 15:42:24 | [diff] [blame] | 298 | base::CancelableRepeatingClosure next_refresh_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 299 | }; |
| 300 | |
| 301 | class MDnsTransactionImpl : public base::SupportsWeakPtr<MDnsTransactionImpl>, |
| 302 | public MDnsTransaction, |
| 303 | public MDnsListener::Delegate { |
| 304 | public: |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 305 | MDnsTransactionImpl(uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 306 | const std::string& name, |
| 307 | int flags, |
| 308 | const MDnsTransaction::ResultCallback& callback, |
| 309 | MDnsClientImpl* client); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 310 | |
| 311 | MDnsTransactionImpl(const MDnsTransactionImpl&) = delete; |
| 312 | MDnsTransactionImpl& operator=(const MDnsTransactionImpl&) = delete; |
| 313 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 314 | ~MDnsTransactionImpl() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 315 | |
| 316 | // MDnsTransaction implementation: |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 317 | bool Start() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 318 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 319 | const std::string& GetName() const override; |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 320 | uint16_t GetType() const override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 321 | |
| 322 | // MDnsListener::Delegate implementation: |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 323 | void OnRecordUpdate(MDnsListener::UpdateType update, |
| 324 | const RecordParsed* record) override; |
| 325 | void OnNsecRecord(const std::string& name, unsigned type) override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 326 | |
dcheng | 67be2b1f | 2014-10-27 21:47:29 | [diff] [blame] | 327 | void OnCachePurged() override; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 328 | |
| 329 | private: |
| 330 | bool is_active() { return !callback_.is_null(); } |
| 331 | |
| 332 | void Reset(); |
| 333 | |
| 334 | // Trigger the callback and reset all related variables. |
| 335 | void TriggerCallback(MDnsTransaction::Result result, |
| 336 | const RecordParsed* record); |
| 337 | |
| 338 | // Internal callback for when a cache record is found. |
| 339 | void CacheRecordFound(const RecordParsed* record); |
| 340 | |
| 341 | // Signal the transactionis over and release all related resources. |
| 342 | void SignalTransactionOver(); |
| 343 | |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 344 | // Reads records from the cache and calls the callback for every |
| 345 | // record read. |
| 346 | void ServeRecordsFromCache(); |
| 347 | |
| 348 | // Send a query to the network and set up a timeout to time out the |
| 349 | // transaction. Returns false if it fails to start listening on the network |
| 350 | // or if it fails to send a query. |
| 351 | bool QueryAndListen(); |
| 352 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 353 | uint16_t rrtype_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 354 | std::string name_; |
| 355 | MDnsTransaction::ResultCallback callback_; |
| 356 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 357 | std::unique_ptr<MDnsListener> listener_; |
Steve Kobes | f5bea3e | 2020-07-08 20:15:43 | [diff] [blame] | 358 | base::CancelableOnceCallback<void()> timeout_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 359 | |
| 360 | MDnsClientImpl* client_; |
| 361 | |
| 362 | bool started_; |
| 363 | int flags_; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | } // namespace net |
| 367 | #endif // NET_DNS_MDNS_CLIENT_IMPL_H_ |