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