[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 | #include "net/dns/mdns_client_impl.h" |
| 6 | |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 7 | #include <algorithm> |
Peter Boström | 8a754069 | 2021-04-05 20:48:20 | [diff] [blame] | 8 | #include <memory> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 9 | #include <utility> |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 10 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 11 | #include "base/bind.h" |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 12 | #include "base/location.h" |
David Sanders | de5fee54 | 2022-03-23 02:47:44 | [diff] [blame] | 13 | #include "base/observer_list.h" |
Piotr Pawliczek | 3318cc9 | 2020-07-25 05:10:13 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 15 | #include "base/task/single_thread_task_runner.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 16 | #include "base/threading/thread_task_runner_handle.h" |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 17 | #include "base/time/clock.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 18 | #include "base/time/default_clock.h" |
prashhir | d22b4a8 | 2014-09-17 17:27:27 | [diff] [blame] | 19 | #include "base/time/time.h" |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 20 | #include "base/timer/timer.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 21 | #include "net/base/net_errors.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 22 | #include "net/base/rand_callback.h" |
tfarina | 77021d6 | 2015-10-11 20:19:03 | [diff] [blame] | 23 | #include "net/dns/dns_util.h" |
Eric Orth | 8afaf15 | 2018-11-07 21:01:26 | [diff] [blame] | 24 | #include "net/dns/public/dns_protocol.h" |
Eric Orth | 1556c38 | 2018-11-08 06:05:02 | [diff] [blame] | 25 | #include "net/dns/public/util.h" |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 26 | #include "net/dns/record_rdata.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame] | 27 | #include "net/socket/datagram_socket.h" |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 28 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 29 | // TODO(gene): Remove this temporary method of disabling NSEC support once it |
| 30 | // becomes clear whether this feature should be |
| 31 | // supported. http://crbug.com/255232 |
| 32 | #define ENABLE_NSEC |
| 33 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 34 | namespace net { |
| 35 | |
| 36 | namespace { |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 37 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 38 | // The fractions of the record's original TTL after which an active listener |
| 39 | // (one that had |SetActiveRefresh(true)| called) will send a query to refresh |
| 40 | // its cache. This happens both at 85% of the original TTL and again at 95% of |
| 41 | // the original TTL. |
| 42 | const double kListenerRefreshRatio1 = 0.85; |
| 43 | const double kListenerRefreshRatio2 = 0.95; |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 44 | |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 45 | } // namespace |
| 46 | |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 47 | void MDnsSocketFactoryImpl::CreateSockets( |
| 48 | std::vector<std::unique_ptr<DatagramServerSocket>>* sockets) { |
[email protected] | 90d3214 | 2013-12-10 10:53:06 | [diff] [blame] | 49 | InterfaceIndexFamilyList interfaces(GetMDnsInterfacesToBind()); |
| 50 | for (size_t i = 0; i < interfaces.size(); ++i) { |
ttuttle | 859dc7a | 2015-04-23 19:42:29 | [diff] [blame] | 51 | DCHECK(interfaces[i].second == ADDRESS_FAMILY_IPV4 || |
| 52 | interfaces[i].second == ADDRESS_FAMILY_IPV6); |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 53 | std::unique_ptr<DatagramServerSocket> socket(CreateAndBindMDnsSocket( |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 54 | interfaces[i].second, interfaces[i].first, net_log_)); |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 55 | if (socket) |
| 56 | sockets->push_back(std::move(socket)); |
[email protected] | 90d3214 | 2013-12-10 10:53:06 | [diff] [blame] | 57 | } |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 58 | } |
| 59 | |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 60 | MDnsConnection::SocketHandler::SocketHandler( |
| 61 | std::unique_ptr<DatagramServerSocket> socket, |
| 62 | MDnsConnection* connection) |
| 63 | : socket_(std::move(socket)), |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 64 | connection_(connection), |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 65 | response_(dns_protocol::kMaxMulticastSize), |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 66 | send_in_progress_(false) {} |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 67 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 68 | MDnsConnection::SocketHandler::~SocketHandler() = default; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 69 | |
| 70 | int MDnsConnection::SocketHandler::Start() { |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 71 | IPEndPoint end_point; |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 72 | int rv = socket_->GetLocalAddress(&end_point); |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 73 | if (rv != OK) |
| 74 | return rv; |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 75 | DCHECK(end_point.GetFamily() == ADDRESS_FAMILY_IPV4 || |
| 76 | end_point.GetFamily() == ADDRESS_FAMILY_IPV6); |
| 77 | multicast_addr_ = dns_util::GetMdnsGroupEndPoint(end_point.GetFamily()); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 78 | return DoLoop(0); |
| 79 | } |
| 80 | |
| 81 | int MDnsConnection::SocketHandler::DoLoop(int rv) { |
| 82 | do { |
| 83 | if (rv > 0) |
[email protected] | 95b331b4 | 2013-12-02 06:26:31 | [diff] [blame] | 84 | connection_->OnDatagramReceived(&response_, recv_addr_, rv); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 85 | |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 86 | rv = socket_->RecvFrom( |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 87 | response_.io_buffer(), response_.io_buffer_size(), &recv_addr_, |
Yannic Bonenberger | 922f601 | 2019-09-07 16:20:12 | [diff] [blame] | 88 | base::BindOnce(&MDnsConnection::SocketHandler::OnDatagramReceived, |
| 89 | base::Unretained(this))); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 90 | } while (rv > 0); |
| 91 | |
| 92 | if (rv != ERR_IO_PENDING) |
| 93 | return rv; |
| 94 | |
| 95 | return OK; |
| 96 | } |
| 97 | |
| 98 | void MDnsConnection::SocketHandler::OnDatagramReceived(int rv) { |
| 99 | if (rv >= OK) |
| 100 | rv = DoLoop(rv); |
| 101 | |
| 102 | if (rv != OK) |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 103 | connection_->PostOnError(this, rv); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 104 | } |
| 105 | |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 106 | void MDnsConnection::SocketHandler::Send(const scoped_refptr<IOBuffer>& buffer, |
| 107 | unsigned size) { |
| 108 | if (send_in_progress_) { |
| 109 | send_queue_.push(std::make_pair(buffer, size)); |
| 110 | return; |
| 111 | } |
Yannic Bonenberger | 922f601 | 2019-09-07 16:20:12 | [diff] [blame] | 112 | int rv = |
| 113 | socket_->SendTo(buffer.get(), size, multicast_addr_, |
| 114 | base::BindOnce(&MDnsConnection::SocketHandler::SendDone, |
| 115 | base::Unretained(this))); |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 116 | if (rv == ERR_IO_PENDING) { |
| 117 | send_in_progress_ = true; |
| 118 | } else if (rv < OK) { |
| 119 | connection_->PostOnError(this, rv); |
| 120 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void MDnsConnection::SocketHandler::SendDone(int rv) { |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 124 | DCHECK(send_in_progress_); |
| 125 | send_in_progress_ = false; |
| 126 | if (rv != OK) |
| 127 | connection_->PostOnError(this, rv); |
| 128 | while (!send_in_progress_ && !send_queue_.empty()) { |
| 129 | std::pair<scoped_refptr<IOBuffer>, unsigned> buffer = send_queue_.front(); |
| 130 | send_queue_.pop(); |
| 131 | Send(buffer.first, buffer.second); |
| 132 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 133 | } |
| 134 | |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 135 | MDnsConnection::MDnsConnection(MDnsConnection::Delegate* delegate) |
Jeremy Roman | d54000b2 | 2019-07-08 18:40:16 | [diff] [blame] | 136 | : delegate_(delegate) {} |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 137 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 138 | MDnsConnection::~MDnsConnection() = default; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 139 | |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 140 | int MDnsConnection::Init(MDnsSocketFactory* socket_factory) { |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 141 | std::vector<std::unique_ptr<DatagramServerSocket>> sockets; |
| 142 | socket_factory->CreateSockets(&sockets); |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 143 | |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 144 | for (std::unique_ptr<DatagramServerSocket>& socket : sockets) { |
Bence Béky | 8f9d7d395 | 2017-10-09 19:58:04 | [diff] [blame] | 145 | socket_handlers_.push_back(std::make_unique<MDnsConnection::SocketHandler>( |
Qingsi Wang | 5410ef0b | 2019-03-05 20:26:47 | [diff] [blame] | 146 | std::move(socket), this)); |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | 03dd9425 | 2013-09-01 23:25:31 | [diff] [blame] | 149 | // All unbound sockets need to be bound before processing untrusted input. |
| 150 | // This is done for security reasons, so that an attacker can't get an unbound |
| 151 | // socket. |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 152 | int last_failure = ERR_FAILED; |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 153 | for (size_t i = 0; i < socket_handlers_.size();) { |
[email protected] | 9ac0c57a | 2013-10-02 23:00:54 | [diff] [blame] | 154 | int rv = socket_handlers_[i]->Start(); |
| 155 | if (rv != OK) { |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 156 | last_failure = rv; |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 157 | socket_handlers_.erase(socket_handlers_.begin() + i); |
[email protected] | 9ac0c57a | 2013-10-02 23:00:54 | [diff] [blame] | 158 | VLOG(1) << "Start failed, socket=" << i << ", error=" << rv; |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 159 | } else { |
| 160 | ++i; |
| 161 | } |
| 162 | } |
[email protected] | 9ac0c57a | 2013-10-02 23:00:54 | [diff] [blame] | 163 | VLOG(1) << "Sockets ready:" << socket_handlers_.size(); |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 164 | DCHECK_NE(ERR_IO_PENDING, last_failure); |
| 165 | return socket_handlers_.empty() ? last_failure : OK; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 166 | } |
| 167 | |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 168 | void MDnsConnection::Send(const scoped_refptr<IOBuffer>& buffer, |
| 169 | unsigned size) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 170 | for (std::unique_ptr<SocketHandler>& handler : socket_handlers_) |
olli.raula | d8734f2c7 | 2015-12-03 12:36:34 | [diff] [blame] | 171 | handler->Send(buffer, size); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 172 | } |
| 173 | |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 174 | void MDnsConnection::PostOnError(SocketHandler* loop, int rv) { |
olli.raula | d8734f2c7 | 2015-12-03 12:36:34 | [diff] [blame] | 175 | int id = 0; |
| 176 | for (const auto& it : socket_handlers_) { |
| 177 | if (it.get() == loop) |
| 178 | break; |
| 179 | id++; |
| 180 | } |
| 181 | VLOG(1) << "Socket error. id=" << id << ", error=" << rv; |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 182 | // Post to allow deletion of this object by delegate. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 183 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 184 | FROM_HERE, base::BindOnce(&MDnsConnection::OnError, |
| 185 | weak_ptr_factory_.GetWeakPtr(), rv)); |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void MDnsConnection::OnError(int rv) { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 189 | // TODO(noamsml): Specific handling of intermittent errors that can be handled |
| 190 | // in the connection. |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 191 | delegate_->OnConnectionError(rv); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 194 | void MDnsConnection::OnDatagramReceived( |
| 195 | DnsResponse* response, |
| 196 | const IPEndPoint& recv_addr, |
| 197 | int bytes_read) { |
| 198 | // TODO(noamsml): More sophisticated error handling. |
| 199 | DCHECK_GT(bytes_read, 0); |
| 200 | delegate_->HandlePacket(response, bytes_read); |
| 201 | } |
| 202 | |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 203 | MDnsClientImpl::Core::Core(base::Clock* clock, base::OneShotTimer* timer) |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 204 | : clock_(clock), |
| 205 | cleanup_timer_(timer), |
Eric Orth | 6cb27ea | 2019-05-03 16:31:04 | [diff] [blame] | 206 | connection_(new MDnsConnection(this)) { |
| 207 | DCHECK(cleanup_timer_); |
| 208 | DCHECK(!cleanup_timer_->IsRunning()); |
| 209 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 210 | |
Eric Orth | 6cb27ea | 2019-05-03 16:31:04 | [diff] [blame] | 211 | MDnsClientImpl::Core::~Core() { |
| 212 | cleanup_timer_->Stop(); |
| 213 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 214 | |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 215 | int MDnsClientImpl::Core::Init(MDnsSocketFactory* socket_factory) { |
Eric Orth | 6cb27ea | 2019-05-03 16:31:04 | [diff] [blame] | 216 | CHECK(!cleanup_timer_->IsRunning()); |
[email protected] | e4411fbe | 2013-09-26 21:10:11 | [diff] [blame] | 217 | return connection_->Init(socket_factory); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 218 | } |
| 219 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 220 | bool MDnsClientImpl::Core::SendQuery(uint16_t rrtype, const std::string& name) { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 221 | std::string name_dns; |
Bailey Berro | e70f06c | 2019-03-11 22:22:46 | [diff] [blame] | 222 | if (!DNSDomainFromUnrestrictedDot(name, &name_dns)) |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 223 | return false; |
| 224 | |
| 225 | DnsQuery query(0, name_dns, rrtype); |
| 226 | query.set_flags(0); // Remove the RD flag from the query. It is unneeded. |
| 227 | |
vitalybuka | 2085974 | 2014-09-22 23:42:30 | [diff] [blame] | 228 | connection_->Send(query.io_buffer(), query.io_buffer()->size()); |
| 229 | return true; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void MDnsClientImpl::Core::HandlePacket(DnsResponse* response, |
| 233 | int bytes_read) { |
| 234 | unsigned offset; |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 235 | // Note: We store cache keys rather than record pointers to avoid |
| 236 | // erroneous behavior in case a packet contains multiple exclusive |
| 237 | // records with the same type and name. |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 238 | std::map<MDnsCache::Key, MDnsCache::UpdateType> update_keys; |
Brad Lassey | 786929ad | 2018-02-21 20:54:27 | [diff] [blame] | 239 | DCHECK_GT(bytes_read, 0); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 240 | if (!response->InitParseWithoutQuery(bytes_read)) { |
[email protected] | 423a4d4 | 2014-03-05 21:55:06 | [diff] [blame] | 241 | DVLOG(1) << "Could not understand an mDNS packet."; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 242 | return; // Message is unreadable. |
| 243 | } |
| 244 | |
| 245 | // TODO(noamsml): duplicate query suppression. |
| 246 | if (!(response->flags() & dns_protocol::kFlagResponse)) |
| 247 | return; // Message is a query. ignore it. |
| 248 | |
| 249 | DnsRecordParser parser = response->Parser(); |
| 250 | unsigned answer_count = response->answer_count() + |
| 251 | response->additional_answer_count(); |
| 252 | |
| 253 | for (unsigned i = 0; i < answer_count; i++) { |
| 254 | offset = parser.GetOffset(); |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 255 | std::unique_ptr<const RecordParsed> record = |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 256 | RecordParsed::CreateFrom(&parser, clock_->Now()); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 257 | |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 258 | if (!record) { |
[email protected] | 423a4d4 | 2014-03-05 21:55:06 | [diff] [blame] | 259 | DVLOG(1) << "Could not understand an mDNS record."; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 260 | |
| 261 | if (offset == parser.GetOffset()) { |
[email protected] | 423a4d4 | 2014-03-05 21:55:06 | [diff] [blame] | 262 | DVLOG(1) << "Abandoned parsing the rest of the packet."; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 263 | return; // The parser did not advance, abort reading the packet. |
| 264 | } else { |
| 265 | continue; // We may be able to extract other records from the packet. |
| 266 | } |
| 267 | } |
| 268 | |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 269 | if ((record->klass() & dns_protocol::kMDnsClassMask) != |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 270 | dns_protocol::kClassIN) { |
[email protected] | 423a4d4 | 2014-03-05 21:55:06 | [diff] [blame] | 271 | DVLOG(1) << "Received an mDNS record with non-IN class. Ignoring."; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 272 | continue; // Ignore all records not in the IN class. |
| 273 | } |
| 274 | |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 275 | MDnsCache::Key update_key = MDnsCache::Key::CreateFor(record.get()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 276 | MDnsCache::UpdateType update = cache_.UpdateDnsRecord(std::move(record)); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 277 | |
| 278 | // Cleanup time may have changed. |
| 279 | ScheduleCleanup(cache_.next_expiration()); |
| 280 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 281 | update_keys.insert(std::make_pair(update_key, update)); |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 282 | } |
| 283 | |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 284 | for (auto i = update_keys.begin(); i != update_keys.end(); i++) { |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 285 | const RecordParsed* record = cache_.LookupKey(i->first); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 286 | if (!record) |
| 287 | continue; |
| 288 | |
| 289 | if (record->type() == dns_protocol::kTypeNSEC) { |
| 290 | #if defined(ENABLE_NSEC) |
| 291 | NotifyNsecRecord(record); |
| 292 | #endif |
| 293 | } else { |
| 294 | AlertListeners(i->second, ListenerKey(record->name(), record->type()), |
[email protected] | 5e6f4273 | 2013-06-19 03:43:31 | [diff] [blame] | 295 | record); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | void MDnsClientImpl::Core::NotifyNsecRecord(const RecordParsed* record) { |
| 301 | DCHECK_EQ(dns_protocol::kTypeNSEC, record->type()); |
| 302 | const NsecRecordRdata* rdata = record->rdata<NsecRecordRdata>(); |
| 303 | DCHECK(rdata); |
| 304 | |
| 305 | // Remove all cached records matching the nonexistent RR types. |
| 306 | std::vector<const RecordParsed*> records_to_remove; |
| 307 | |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 308 | cache_.FindDnsRecords(0, record->name(), &records_to_remove, clock_->Now()); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 309 | |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 310 | for (auto i = records_to_remove.begin(); i != records_to_remove.end(); i++) { |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 311 | if ((*i)->type() == dns_protocol::kTypeNSEC) |
| 312 | continue; |
| 313 | if (!rdata->GetBit((*i)->type())) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 314 | std::unique_ptr<const RecordParsed> record_removed = |
| 315 | cache_.RemoveRecord((*i)); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 316 | DCHECK(record_removed); |
| 317 | OnRecordRemoved(record_removed.get()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Alert all listeners waiting for the nonexistent RR types. |
Piotr Pawliczek | 3318cc9 | 2020-07-25 05:10:13 | [diff] [blame] | 322 | ListenerKey key(record->name(), 0); |
| 323 | auto i = listeners_.upper_bound(key); |
| 324 | for (; i != listeners_.end() && |
| 325 | i->first.name_lowercase() == key.name_lowercase(); |
| 326 | i++) { |
| 327 | if (!rdata->GetBit(i->first.type())) { |
ericwilligers | 9d64a5f | 2016-10-18 00:28:49 | [diff] [blame] | 328 | for (auto& observer : *i->second) |
| 329 | observer.AlertNsecRecord(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | void MDnsClientImpl::Core::OnConnectionError(int error) { |
| 335 | // TODO(noamsml): On connection error, recreate connection and flush cache. |
kmarshall | 73a89b1 | 2015-09-01 22:18:13 | [diff] [blame] | 336 | VLOG(1) << "MDNS OnConnectionError (code: " << error << ")"; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 337 | } |
| 338 | |
Piotr Pawliczek | 3318cc9 | 2020-07-25 05:10:13 | [diff] [blame] | 339 | MDnsClientImpl::Core::ListenerKey::ListenerKey(const std::string& name, |
| 340 | uint16_t type) |
| 341 | : name_lowercase_(base::ToLowerASCII(name)), type_(type) {} |
| 342 | |
| 343 | bool MDnsClientImpl::Core::ListenerKey::operator<( |
| 344 | const MDnsClientImpl::Core::ListenerKey& key) const { |
| 345 | if (name_lowercase_ == key.name_lowercase_) |
| 346 | return type_ < key.type_; |
| 347 | return name_lowercase_ < key.name_lowercase_; |
| 348 | } |
| 349 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 350 | void MDnsClientImpl::Core::AlertListeners( |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 351 | MDnsCache::UpdateType update_type, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 352 | const ListenerKey& key, |
| 353 | const RecordParsed* record) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 354 | auto listener_map_iterator = listeners_.find(key); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 355 | if (listener_map_iterator == listeners_.end()) return; |
| 356 | |
ericwilligers | 9d64a5f | 2016-10-18 00:28:49 | [diff] [blame] | 357 | for (auto& observer : *listener_map_iterator->second) |
| 358 | observer.HandleRecordUpdate(update_type, record); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void MDnsClientImpl::Core::AddListener( |
| 362 | MDnsListenerImpl* listener) { |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 363 | ListenerKey key(listener->GetName(), listener->GetType()); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 364 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 365 | auto& observer_list = listeners_[key]; |
avi | 9e72eb48 | 2016-12-09 21:10:54 | [diff] [blame] | 366 | if (!observer_list) |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 367 | observer_list = std::make_unique<ObserverListType>(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 368 | |
| 369 | observer_list->AddObserver(listener); |
| 370 | } |
| 371 | |
| 372 | void MDnsClientImpl::Core::RemoveListener(MDnsListenerImpl* listener) { |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 373 | ListenerKey key(listener->GetName(), listener->GetType()); |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 374 | auto observer_list_iterator = listeners_.find(key); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 375 | |
| 376 | DCHECK(observer_list_iterator != listeners_.end()); |
| 377 | DCHECK(observer_list_iterator->second->HasObserver(listener)); |
| 378 | |
| 379 | observer_list_iterator->second->RemoveObserver(listener); |
| 380 | |
| 381 | // Remove the observer list from the map if it is empty |
Mitsuru Oshima | 8bc2746e8 | 2021-01-15 07:01:13 | [diff] [blame] | 382 | if (observer_list_iterator->second->empty()) { |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 383 | // Schedule the actual removal for later in case the listener removal |
| 384 | // happens while iterating over the observer list. |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 385 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | f4fe517 | 2019-02-15 18:53:49 | [diff] [blame] | 386 | FROM_HERE, base::BindOnce(&MDnsClientImpl::Core::CleanupObserverList, |
| 387 | AsWeakPtr(), key)); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | void MDnsClientImpl::Core::CleanupObserverList(const ListenerKey& key) { |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 392 | auto found = listeners_.find(key); |
Mitsuru Oshima | 8bc2746e8 | 2021-01-15 07:01:13 | [diff] [blame] | 393 | if (found != listeners_.end() && found->second->empty()) { |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 394 | listeners_.erase(found); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | void MDnsClientImpl::Core::ScheduleCleanup(base::Time cleanup) { |
Eric Orth | acdd8ae | 2019-03-29 22:07:19 | [diff] [blame] | 399 | // If cache is overfilled. Force an immediate cleanup. |
| 400 | if (cache_.IsCacheOverfilled()) |
| 401 | cleanup = clock_->Now(); |
| 402 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 403 | // Cleanup is already scheduled, no need to do anything. |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 404 | if (cleanup == scheduled_cleanup_) { |
| 405 | return; |
| 406 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 407 | scheduled_cleanup_ = cleanup; |
| 408 | |
| 409 | // This cancels the previously scheduled cleanup. |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 410 | cleanup_timer_->Stop(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 411 | |
| 412 | // If |cleanup| is empty, then no cleanup necessary. |
| 413 | if (cleanup != base::Time()) { |
Yannic Bonenberger | 922f601 | 2019-09-07 16:20:12 | [diff] [blame] | 414 | cleanup_timer_->Start(FROM_HERE, |
| 415 | std::max(base::TimeDelta(), cleanup - clock_->Now()), |
| 416 | base::BindOnce(&MDnsClientImpl::Core::DoCleanup, |
| 417 | base::Unretained(this))); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | void MDnsClientImpl::Core::DoCleanup() { |
Anna Malova | 9b2095b | 2020-03-04 15:41:18 | [diff] [blame] | 422 | cache_.CleanupRecords( |
| 423 | clock_->Now(), base::BindRepeating(&MDnsClientImpl::Core::OnRecordRemoved, |
| 424 | base::Unretained(this))); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 425 | |
| 426 | ScheduleCleanup(cache_.next_expiration()); |
| 427 | } |
| 428 | |
| 429 | void MDnsClientImpl::Core::OnRecordRemoved( |
| 430 | const RecordParsed* record) { |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 431 | AlertListeners(MDnsCache::RecordRemoved, |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 432 | ListenerKey(record->name(), record->type()), record); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void MDnsClientImpl::Core::QueryCache( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 436 | uint16_t rrtype, |
| 437 | const std::string& name, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 438 | std::vector<const RecordParsed*>* records) const { |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 439 | cache_.FindDnsRecords(rrtype, name, records, clock_->Now()); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 440 | } |
| 441 | |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 442 | MDnsClientImpl::MDnsClientImpl() |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 443 | : clock_(base::DefaultClock::GetInstance()), |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 444 | cleanup_timer_(new base::OneShotTimer()) {} |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 445 | |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 446 | MDnsClientImpl::MDnsClientImpl(base::Clock* clock, |
tzik | 08d8d6e | 2018-07-09 04:11:47 | [diff] [blame] | 447 | std::unique_ptr<base::OneShotTimer> timer) |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 448 | : clock_(clock), cleanup_timer_(std::move(timer)) {} |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 449 | |
Eric Orth | 6cb27ea | 2019-05-03 16:31:04 | [diff] [blame] | 450 | MDnsClientImpl::~MDnsClientImpl() { |
| 451 | StopListening(); |
| 452 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 453 | |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 454 | int MDnsClientImpl::StartListening(MDnsSocketFactory* socket_factory) { |
[email protected] | 9c61d25 | 2013-07-02 23:26:22 | [diff] [blame] | 455 | DCHECK(!core_.get()); |
Peter Boström | 8a754069 | 2021-04-05 20:48:20 | [diff] [blame] | 456 | core_ = std::make_unique<Core>(clock_, cleanup_timer_.get()); |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 457 | int rv = core_->Init(socket_factory); |
| 458 | if (rv != OK) { |
| 459 | DCHECK_NE(ERR_IO_PENDING, rv); |
[email protected] | 9c61d25 | 2013-07-02 23:26:22 | [diff] [blame] | 460 | core_.reset(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 461 | } |
Eric Orth | e857ebb | 2019-03-13 23:02:07 | [diff] [blame] | 462 | return rv; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 463 | } |
| 464 | |
[email protected] | 9c61d25 | 2013-07-02 23:26:22 | [diff] [blame] | 465 | void MDnsClientImpl::StopListening() { |
| 466 | core_.reset(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 467 | } |
| 468 | |
[email protected] | 9c61d25 | 2013-07-02 23:26:22 | [diff] [blame] | 469 | bool MDnsClientImpl::IsListening() const { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 470 | return core_.get() != nullptr; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 471 | } |
| 472 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 473 | std::unique_ptr<MDnsListener> MDnsClientImpl::CreateListener( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 474 | uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 475 | const std::string& name, |
| 476 | MDnsListener::Delegate* delegate) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 477 | return std::unique_ptr<MDnsListener>( |
tzik | 2633174 | 2017-12-07 07:28:33 | [diff] [blame] | 478 | new MDnsListenerImpl(rrtype, name, clock_, delegate, this)); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 479 | } |
| 480 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 481 | std::unique_ptr<MDnsTransaction> MDnsClientImpl::CreateTransaction( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 482 | uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 483 | const std::string& name, |
| 484 | int flags, |
| 485 | const MDnsTransaction::ResultCallback& callback) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 486 | return std::unique_ptr<MDnsTransaction>( |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 487 | new MDnsTransactionImpl(rrtype, name, flags, callback, this)); |
| 488 | } |
| 489 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 490 | MDnsListenerImpl::MDnsListenerImpl(uint16_t rrtype, |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 491 | const std::string& name, |
| 492 | base::Clock* clock, |
| 493 | MDnsListener::Delegate* delegate, |
| 494 | MDnsClientImpl* client) |
| 495 | : rrtype_(rrtype), |
| 496 | name_(name), |
| 497 | clock_(clock), |
| 498 | client_(client), |
| 499 | delegate_(delegate), |
| 500 | started_(false), |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 501 | active_refresh_(false) {} |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 502 | |
| 503 | MDnsListenerImpl::~MDnsListenerImpl() { |
| 504 | if (started_) { |
| 505 | DCHECK(client_->core()); |
| 506 | client_->core()->RemoveListener(this); |
| 507 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | bool MDnsListenerImpl::Start() { |
| 511 | DCHECK(!started_); |
| 512 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 513 | started_ = true; |
| 514 | |
| 515 | DCHECK(client_->core()); |
| 516 | client_->core()->AddListener(this); |
| 517 | |
| 518 | return true; |
| 519 | } |
| 520 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 521 | void MDnsListenerImpl::SetActiveRefresh(bool active_refresh) { |
| 522 | active_refresh_ = active_refresh; |
| 523 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 524 | if (started_) { |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 525 | if (!active_refresh_) { |
| 526 | next_refresh_.Cancel(); |
| 527 | } else if (last_update_ != base::Time()) { |
| 528 | ScheduleNextRefresh(); |
| 529 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
| 533 | const std::string& MDnsListenerImpl::GetName() const { |
| 534 | return name_; |
| 535 | } |
| 536 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 537 | uint16_t MDnsListenerImpl::GetType() const { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 538 | return rrtype_; |
| 539 | } |
| 540 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 541 | void MDnsListenerImpl::HandleRecordUpdate(MDnsCache::UpdateType update_type, |
| 542 | const RecordParsed* record) { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 543 | DCHECK(started_); |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 544 | |
| 545 | if (update_type != MDnsCache::RecordRemoved) { |
| 546 | ttl_ = record->ttl(); |
| 547 | last_update_ = record->time_created(); |
| 548 | |
| 549 | ScheduleNextRefresh(); |
| 550 | } |
| 551 | |
| 552 | if (update_type != MDnsCache::NoChange) { |
| 553 | MDnsListener::UpdateType update_external; |
| 554 | |
| 555 | switch (update_type) { |
| 556 | case MDnsCache::RecordAdded: |
| 557 | update_external = MDnsListener::RECORD_ADDED; |
| 558 | break; |
| 559 | case MDnsCache::RecordChanged: |
| 560 | update_external = MDnsListener::RECORD_CHANGED; |
| 561 | break; |
| 562 | case MDnsCache::RecordRemoved: |
| 563 | update_external = MDnsListener::RECORD_REMOVED; |
| 564 | break; |
| 565 | case MDnsCache::NoChange: |
| 566 | default: |
| 567 | NOTREACHED(); |
| 568 | // Dummy assignment to suppress compiler warning. |
| 569 | update_external = MDnsListener::RECORD_CHANGED; |
| 570 | break; |
| 571 | } |
| 572 | |
| 573 | delegate_->OnRecordUpdate(update_external, record); |
| 574 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 575 | } |
| 576 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 577 | void MDnsListenerImpl::AlertNsecRecord() { |
| 578 | DCHECK(started_); |
| 579 | delegate_->OnNsecRecord(name_, rrtype_); |
| 580 | } |
| 581 | |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 582 | void MDnsListenerImpl::ScheduleNextRefresh() { |
| 583 | DCHECK(last_update_ != base::Time()); |
| 584 | |
| 585 | if (!active_refresh_) |
| 586 | return; |
| 587 | |
| 588 | // A zero TTL is a goodbye packet and should not be refreshed. |
| 589 | if (ttl_ == 0) { |
| 590 | next_refresh_.Cancel(); |
| 591 | return; |
| 592 | } |
| 593 | |
Steve Kobes | f5bea3e | 2020-07-08 20:15:43 | [diff] [blame] | 594 | next_refresh_.Reset( |
| 595 | base::BindRepeating(&MDnsListenerImpl::DoRefresh, AsWeakPtr())); |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 596 | |
| 597 | // Schedule refreshes at both 85% and 95% of the original TTL. These will both |
| 598 | // be canceled and rescheduled if the record's TTL is updated due to a |
| 599 | // response being received. |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 600 | base::Time next_refresh1 = |
| 601 | last_update_ + |
| 602 | base::Milliseconds(static_cast<int>(base::Time::kMillisecondsPerSecond * |
| 603 | kListenerRefreshRatio1 * ttl_)); |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 604 | |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 605 | base::Time next_refresh2 = |
| 606 | last_update_ + |
| 607 | base::Milliseconds(static_cast<int>(base::Time::kMillisecondsPerSecond * |
| 608 | kListenerRefreshRatio2 * ttl_)); |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 609 | |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 610 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 611 | FROM_HERE, next_refresh_.callback(), next_refresh1 - clock_->Now()); |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 612 | |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 613 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
kmarshall | 3e740be | 2015-03-02 21:30:44 | [diff] [blame] | 614 | FROM_HERE, next_refresh_.callback(), next_refresh2 - clock_->Now()); |
[email protected] | bdd6c3d | 2014-01-30 08:07:42 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | void MDnsListenerImpl::DoRefresh() { |
| 618 | client_->core()->SendQuery(rrtype_, name_); |
| 619 | } |
| 620 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 621 | MDnsTransactionImpl::MDnsTransactionImpl( |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 622 | uint16_t rrtype, |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 623 | const std::string& name, |
| 624 | int flags, |
| 625 | const MDnsTransaction::ResultCallback& callback, |
| 626 | MDnsClientImpl* client) |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 627 | : rrtype_(rrtype), |
| 628 | name_(name), |
| 629 | callback_(callback), |
| 630 | client_(client), |
| 631 | started_(false), |
| 632 | flags_(flags) { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 633 | DCHECK((flags_ & MDnsTransaction::FLAG_MASK) == flags_); |
| 634 | DCHECK(flags_ & MDnsTransaction::QUERY_CACHE || |
| 635 | flags_ & MDnsTransaction::QUERY_NETWORK); |
| 636 | } |
| 637 | |
| 638 | MDnsTransactionImpl::~MDnsTransactionImpl() { |
| 639 | timeout_.Cancel(); |
| 640 | } |
| 641 | |
| 642 | bool MDnsTransactionImpl::Start() { |
| 643 | DCHECK(!started_); |
| 644 | started_ = true; |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 645 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 646 | base::WeakPtr<MDnsTransactionImpl> weak_this = AsWeakPtr(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 647 | if (flags_ & MDnsTransaction::QUERY_CACHE) { |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 648 | ServeRecordsFromCache(); |
| 649 | |
| 650 | if (!weak_this || !is_active()) return true; |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 651 | } |
| 652 | |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 653 | if (flags_ & MDnsTransaction::QUERY_NETWORK) { |
| 654 | return QueryAndListen(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 655 | } |
| 656 | |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 657 | // If this is a cache only query, signal that the transaction is over |
| 658 | // immediately. |
| 659 | SignalTransactionOver(); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 660 | return true; |
| 661 | } |
| 662 | |
| 663 | const std::string& MDnsTransactionImpl::GetName() const { |
| 664 | return name_; |
| 665 | } |
| 666 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 667 | uint16_t MDnsTransactionImpl::GetType() const { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 668 | return rrtype_; |
| 669 | } |
| 670 | |
| 671 | void MDnsTransactionImpl::CacheRecordFound(const RecordParsed* record) { |
| 672 | DCHECK(started_); |
| 673 | OnRecordUpdate(MDnsListener::RECORD_ADDED, record); |
| 674 | } |
| 675 | |
| 676 | void MDnsTransactionImpl::TriggerCallback(MDnsTransaction::Result result, |
| 677 | const RecordParsed* record) { |
| 678 | DCHECK(started_); |
| 679 | if (!is_active()) return; |
| 680 | |
| 681 | // Ensure callback is run after touching all class state, so that |
| 682 | // the callback can delete the transaction. |
| 683 | MDnsTransaction::ResultCallback callback = callback_; |
| 684 | |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 685 | // Reset the transaction if it expects a single result, or if the result |
| 686 | // is a final one (everything except for a record). |
| 687 | if (flags_ & MDnsTransaction::SINGLE_RESULT || |
| 688 | result != MDnsTransaction::RESULT_RECORD) { |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 689 | Reset(); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 690 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 691 | |
| 692 | callback.Run(result, record); |
| 693 | } |
| 694 | |
| 695 | void MDnsTransactionImpl::Reset() { |
| 696 | callback_.Reset(); |
| 697 | listener_.reset(); |
| 698 | timeout_.Cancel(); |
| 699 | } |
| 700 | |
| 701 | void MDnsTransactionImpl::OnRecordUpdate(MDnsListener::UpdateType update, |
| 702 | const RecordParsed* record) { |
| 703 | DCHECK(started_); |
| 704 | if (update == MDnsListener::RECORD_ADDED || |
| 705 | update == MDnsListener::RECORD_CHANGED) |
| 706 | TriggerCallback(MDnsTransaction::RESULT_RECORD, record); |
| 707 | } |
| 708 | |
| 709 | void MDnsTransactionImpl::SignalTransactionOver() { |
| 710 | DCHECK(started_); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 711 | if (flags_ & MDnsTransaction::SINGLE_RESULT) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 712 | TriggerCallback(MDnsTransaction::RESULT_NO_RESULTS, nullptr); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 713 | } else { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 714 | TriggerCallback(MDnsTransaction::RESULT_DONE, nullptr); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 715 | } |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 716 | } |
| 717 | |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 718 | void MDnsTransactionImpl::ServeRecordsFromCache() { |
| 719 | std::vector<const RecordParsed*> records; |
| 720 | base::WeakPtr<MDnsTransactionImpl> weak_this = AsWeakPtr(); |
| 721 | |
| 722 | if (client_->core()) { |
| 723 | client_->core()->QueryCache(rrtype_, name_, &records); |
jdoerrie | 22a91d8b9 | 2018-10-05 08:43:26 | [diff] [blame] | 724 | for (auto i = records.begin(); i != records.end() && weak_this; ++i) { |
[email protected] | 63b43f3e | 2013-08-07 19:21:12 | [diff] [blame] | 725 | weak_this->TriggerCallback(MDnsTransaction::RESULT_RECORD, *i); |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 726 | } |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 727 | |
| 728 | #if defined(ENABLE_NSEC) |
| 729 | if (records.empty()) { |
| 730 | DCHECK(weak_this); |
| 731 | client_->core()->QueryCache(dns_protocol::kTypeNSEC, name_, &records); |
| 732 | if (!records.empty()) { |
| 733 | const NsecRecordRdata* rdata = |
| 734 | records.front()->rdata<NsecRecordRdata>(); |
| 735 | DCHECK(rdata); |
| 736 | if (!rdata->GetBit(rrtype_)) |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 737 | weak_this->TriggerCallback(MDnsTransaction::RESULT_NSEC, nullptr); |
[email protected] | 21df1696 | 2013-07-01 17:39:53 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | #endif |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 741 | } |
| 742 | } |
| 743 | |
| 744 | bool MDnsTransactionImpl::QueryAndListen() { |
| 745 | listener_ = client_->CreateListener(rrtype_, name_, this); |
| 746 | if (!listener_->Start()) |
| 747 | return false; |
| 748 | |
| 749 | DCHECK(client_->core()); |
| 750 | if (!client_->core()->SendQuery(rrtype_, name_)) |
| 751 | return false; |
| 752 | |
Steve Kobes | f5bea3e | 2020-07-08 20:15:43 | [diff] [blame] | 753 | timeout_.Reset( |
| 754 | base::BindOnce(&MDnsTransactionImpl::SignalTransactionOver, AsWeakPtr())); |
skyostil | 4891b25b | 2015-06-11 11:43:45 | [diff] [blame] | 755 | base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
Eric Orth | 9871aafa | 2018-10-02 19:59:18 | [diff] [blame] | 756 | FROM_HERE, timeout_.callback(), kTransactionTimeout); |
[email protected] | f3c0999 | 2013-06-25 00:40:48 | [diff] [blame] | 757 | |
| 758 | return true; |
| 759 | } |
| 760 | |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 761 | void MDnsTransactionImpl::OnNsecRecord(const std::string& name, unsigned type) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 762 | TriggerCallback(RESULT_NSEC, nullptr); |
[email protected] | 245b164e | 2013-06-13 22:31:42 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void MDnsTransactionImpl::OnCachePurged() { |
| 766 | // TODO(noamsml): Cache purge situations not yet implemented |
| 767 | } |
| 768 | |
| 769 | } // namespace net |