1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/dns/host_resolver_mdns_task.h"
#include <algorithm>
#include <memory>
#include <set>
#include <utility>
#include <vector>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/notreached.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/dns/dns_util.h"
#include "net/dns/host_resolver_internal_result.h"
#include "net/dns/public/dns_protocol.h"
#include "net/dns/public/dns_query_type.h"
#include "net/dns/record_parsed.h"
#include "net/dns/record_rdata.h"
namespace net {
namespace {
std::unique_ptr<HostResolverInternalResult> ParseHostnameResult(
std::string query_name,
DnsQueryType query_type,
std::string_view host,
uint16_t port) {
// Filter out root domain. Depending on the type, it either means no-result
// or is simply not a result important to any expected Chrome usecases.
if (host.empty()) {
return std::make_unique<HostResolverInternalErrorResult>(
std::move(query_name), query_type,
/*expiration=*/std::nullopt,
/*timed_expiration=*/std::nullopt,
HostResolverInternalResult::Source::kUnknown, ERR_NAME_NOT_RESOLVED);
} else {
return std::make_unique<HostResolverInternalDataResult>(
std::move(query_name), query_type,
/*expiration=*/base::TimeTicks::Now(),
/*timed_expiration=*/base::Time::Now(),
HostResolverInternalResult::Source::kUnknown,
/*endpoints=*/std::vector<IPEndPoint>{},
/*strings=*/std::vector<std::string>{},
/*hosts=*/
std::vector<HostPortPair>{HostPortPair(host, port)});
}
}
} // namespace
class HostResolverMdnsTask::Transaction {
public:
Transaction(DnsQueryType query_type, HostResolverMdnsTask* task)
: query_type_(query_type),
task_(task) {}
void Start() {
DCHECK_CALLED_ON_VALID_SEQUENCE(task_->sequence_checker_);
// Should not be completed or running yet.
DCHECK(!result_);
DCHECK(!async_transaction_);
// TODO(crbug.com/40611558): Use |allow_cached_response| to set the
// QUERY_CACHE flag or not.
int flags = MDnsTransaction::SINGLE_RESULT | MDnsTransaction::QUERY_CACHE |
MDnsTransaction::QUERY_NETWORK;
// If |this| is destroyed, destruction of |internal_transaction_| should
// cancel and prevent invocation of OnComplete.
std::unique_ptr<MDnsTransaction> inner_transaction =
task_->mdns_client_->CreateTransaction(
DnsQueryTypeToQtype(query_type_), task_->hostname_, flags,
base::BindRepeating(&HostResolverMdnsTask::Transaction::OnComplete,
base::Unretained(this)));
// Side effect warning: Start() may finish and invoke callbacks inline.
bool start_result = inner_transaction->Start();
if (!start_result) {
task_->Complete(true /* post_needed */);
} else if (!result_) {
async_transaction_ = std::move(inner_transaction);
}
}
bool IsDone() const { return !!result_; }
bool IsError() const {
return IsDone() &&
result_->type() == HostResolverInternalResult::Type::kError &&
result_->AsError().error() != ERR_NAME_NOT_RESOLVED;
}
const HostResolverInternalResult* result() const { return result_.get(); }
void Cancel() {
DCHECK_CALLED_ON_VALID_SEQUENCE(task_->sequence_checker_);
DCHECK(!result_);
result_ = std::make_unique<HostResolverInternalErrorResult>(
task_->hostname_, query_type_, /*expiration=*/std::nullopt,
/*timed_expiration=*/std::nullopt,
HostResolverInternalResult::Source::kUnknown, ERR_FAILED);
}
private:
void OnComplete(MDnsTransaction::Result result, const RecordParsed* parsed) {
DCHECK_CALLED_ON_VALID_SEQUENCE(task_->sequence_checker_);
DCHECK(!result_);
int error = ERR_UNEXPECTED;
switch (result) {
case MDnsTransaction::RESULT_RECORD:
DCHECK(parsed);
error = OK;
break;
case MDnsTransaction::RESULT_NO_RESULTS:
case MDnsTransaction::RESULT_NSEC:
error = ERR_NAME_NOT_RESOLVED;
break;
default:
// No other results should be possible with the request flags used.
NOTREACHED();
}
result_ = HostResolverMdnsTask::ParseResult(error, task_->hostname_,
query_type_, parsed);
// If we don't have a saved async_transaction, it means OnComplete was
// invoked inline in MDnsTransaction::Start. Callbacks will need to be
// invoked via post.
task_->CheckCompletion(!async_transaction_);
}
const DnsQueryType query_type_;
// null until transaction completes (or is cancelled).
std::unique_ptr<HostResolverInternalResult> result_;
// Not saved until MDnsTransaction::Start completes to differentiate inline
// completion.
std::unique_ptr<MDnsTransaction> async_transaction_;
// Back pointer. Expected to destroy |this| before destroying itself.
const raw_ptr<HostResolverMdnsTask> task_;
};
HostResolverMdnsTask::HostResolverMdnsTask(MDnsClient* mdns_client,
std::string hostname,
DnsQueryTypeSet query_types)
: mdns_client_(mdns_client), hostname_(std::move(hostname)) {
CHECK(!query_types.empty());
DCHECK(!query_types.Has(DnsQueryType::UNSPECIFIED));
static constexpr DnsQueryTypeSet kUnwantedQueries = {DnsQueryType::HTTPS};
for (DnsQueryType query_type : Difference(query_types, kUnwantedQueries)) {
transactions_.emplace_back(query_type, this);
}
CHECK(!transactions_.empty()) << "Only unwanted query types supplied.";
}
HostResolverMdnsTask::~HostResolverMdnsTask() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
transactions_.clear();
}
void HostResolverMdnsTask::Start(base::OnceClosure completion_closure) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!completion_closure_);
DCHECK(mdns_client_);
completion_closure_ = std::move(completion_closure);
for (auto& transaction : transactions_) {
// Only start transaction if it is not already marked done. A transaction
// could be marked done before starting if it is preemptively canceled by
// a previously started transaction finishing with an error.
if (!transaction.IsDone())
transaction.Start();
}
}
// TODO(ericorth@chromium.org): This is a bit wasteful in always copying out
// results from `transactions_`. We should consider moving out the results,
// either by making it a requirement to only call GetResults() once or by
// refactoring to pass the results out once with the completion signal.
std::set<std::unique_ptr<HostResolverInternalResult>>
HostResolverMdnsTask::GetResults() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!transactions_.empty());
DCHECK(!completion_closure_);
DCHECK(std::ranges::all_of(transactions_,
[](const Transaction& t) { return t.IsDone(); }));
std::set<std::unique_ptr<HostResolverInternalResult>> combined_results;
auto found_error = std::ranges::find_if(transactions_, &Transaction::IsError);
if (found_error == transactions_.end()) {
for (const Transaction& transaction : transactions_) {
CHECK(transaction.result());
combined_results.insert(transaction.result()->Clone());
}
} else {
CHECK(found_error->result());
combined_results.insert(found_error->result()->Clone());
}
return combined_results;
}
// static
//
// Because MDnsClient does its own internal caching, true expiration times are
// not known here and cannot be determined from the TTL in `parsed`.
// Fortunately, HostResolverManager is not expected to try to cache mDNS results
// (partly because it assumes the existence of that internal cache), so it is
// not necessary to fill useful information into the expiration fields of the
// results returned here. For the sake of filling something in, use
// `base::TimeTicks::Now()` as the expiration in data results.
std::unique_ptr<HostResolverInternalResult> HostResolverMdnsTask::ParseResult(
int error,
std::string query_hostname,
DnsQueryType query_type,
const RecordParsed* parsed) {
if (error != OK) {
return std::make_unique<HostResolverInternalErrorResult>(
std::move(query_hostname), query_type,
/*expiration=*/std::nullopt,
/*timed_expiration=*/std::nullopt,
HostResolverInternalResult::Source::kUnknown, error);
}
DCHECK(parsed);
// Expected to be validated by MDnsClient.
DCHECK_EQ(DnsQueryTypeToQtype(query_type), parsed->type());
DCHECK(base::EqualsCaseInsensitiveASCII(query_hostname, parsed->name()));
switch (query_type) {
case DnsQueryType::UNSPECIFIED:
// Should create two separate transactions with specified type.
case DnsQueryType::HTTPS:
// Not supported.
// TODO(ericorth@chromium.org): Consider support for HTTPS in mDNS if it
// is ever decided to support HTTPS via non-DoH.
NOTREACHED();
case DnsQueryType::A:
return std::make_unique<HostResolverInternalDataResult>(
std::move(query_hostname), query_type,
/*expiration=*/base::TimeTicks::Now(),
/*timed_expiration=*/base::Time::Now(),
HostResolverInternalResult::Source::kUnknown,
std::vector<IPEndPoint>{
IPEndPoint(parsed->rdata<net::ARecordRdata>()->address(), 0)},
/*strings=*/std::vector<std::string>{},
/*hosts=*/std::vector<HostPortPair>{});
case DnsQueryType::AAAA:
return std::make_unique<HostResolverInternalDataResult>(
std::move(query_hostname), query_type,
/*expiration=*/base::TimeTicks::Now(),
/*timed_expiration=*/base::Time::Now(),
HostResolverInternalResult::Source::kUnknown,
std::vector<IPEndPoint>{
IPEndPoint(parsed->rdata<net::AAAARecordRdata>()->address(), 0)},
/*strings=*/std::vector<std::string>{},
/*hosts=*/std::vector<HostPortPair>{});
case DnsQueryType::TXT:
// TXT invalid without at least one string. If none, should be rejected by
// parser.
CHECK(!parsed->rdata<net::TxtRecordRdata>()->texts().empty());
return std::make_unique<HostResolverInternalDataResult>(
std::move(query_hostname), query_type,
/*expiration=*/base::TimeTicks::Now(),
/*timed_expiration=*/base::Time::Now(),
HostResolverInternalResult::Source::kUnknown,
/*endpoints=*/std::vector<IPEndPoint>{},
/*strings=*/parsed->rdata<net::TxtRecordRdata>()->texts(),
/*hosts=*/std::vector<HostPortPair>{});
case DnsQueryType::PTR:
return ParseHostnameResult(std::move(query_hostname), query_type,
parsed->rdata<PtrRecordRdata>()->ptrdomain(),
/*port=*/0);
case DnsQueryType::SRV:
return ParseHostnameResult(std::move(query_hostname), query_type,
parsed->rdata<SrvRecordRdata>()->target(),
parsed->rdata<SrvRecordRdata>()->port());
}
}
void HostResolverMdnsTask::CheckCompletion(bool post_needed) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Finish immediately if any transactions completed with an error.
if (std::ranges::any_of(transactions_,
[](const Transaction& t) { return t.IsError(); })) {
Complete(post_needed);
return;
}
if (std::ranges::all_of(transactions_,
[](const Transaction& t) { return t.IsDone(); })) {
Complete(post_needed);
return;
}
}
void HostResolverMdnsTask::Complete(bool post_needed) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Cancel any incomplete async transactions.
for (auto& transaction : transactions_) {
if (!transaction.IsDone())
transaction.Cancel();
}
if (post_needed) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(
[](base::WeakPtr<HostResolverMdnsTask> task) {
if (task)
std::move(task->completion_closure_).Run();
},
weak_ptr_factory_.GetWeakPtr()));
} else {
std::move(completion_closure_).Run();
}
}
} // namespace net
|