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