File: dns_response_result_extractor.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (682 lines) | stat: -rw-r--r-- 25,819 bytes parent folder | download | duplicates (3)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
// Copyright 2020 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/dns_response_result_extractor.h"

#include <limits.h>
#include <stdint.h>

#include <algorithm>
#include <iterator>
#include <map>
#include <memory>
#include <optional>
#include <ostream>
#include <set>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/containers/to_vector.h"
#include "base/dcheck_is_on.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/numerics/checked_math.h"
#include "base/numerics/ostream_operators.h"
#include "base/rand_util.h"
#include "base/strings/string_util.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "net/base/address_list.h"
#include "net/base/connection_endpoint_metadata.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/net_errors.h"
#include "net/dns/dns_alias_utility.h"
#include "net/dns/dns_names_util.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_util.h"
#include "net/dns/host_cache.h"
#include "net/dns/host_resolver_internal_result.h"
#include "net/dns/https_record_rdata.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 {

using AliasMap = std::map<std::string,
                          std::unique_ptr<const RecordParsed>,
                          dns_names_util::DomainNameComparator>;
using ExtractionError = DnsResponseResultExtractor::ExtractionError;
using RecordsOrError =
    base::expected<std::vector<std::unique_ptr<const RecordParsed>>,
                   ExtractionError>;
using ResultsOrError = DnsResponseResultExtractor::ResultsOrError;
using Source = HostResolverInternalResult::Source;

void SaveMetricsForAdditionalHttpsRecord(const RecordParsed& record,
                                         bool is_unsolicited) {
  const HttpsRecordRdata* rdata = record.rdata<HttpsRecordRdata>();
  DCHECK(rdata);

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class UnsolicitedHttpsRecordStatus {
    kMalformed = 0,  // No longer recorded.
    kAlias = 1,
    kService = 2,
    kMaxValue = kService
  } status;

  if (rdata->IsAlias()) {
    status = UnsolicitedHttpsRecordStatus::kAlias;
  } else {
    status = UnsolicitedHttpsRecordStatus::kService;
  }

  if (is_unsolicited) {
    UMA_HISTOGRAM_ENUMERATION("Net.DNS.DnsTask.AdditionalHttps.Unsolicited",
                              status);
  } else {
    UMA_HISTOGRAM_ENUMERATION("Net.DNS.DnsTask.AdditionalHttps.Requested",
                              status);
  }
}

// Sort service targets per RFC2782.  In summary, sort first by `priority`,
// lowest first.  For targets with the same priority, secondary sort randomly
// using `weight` with higher weighted objects more likely to go first.
std::vector<HostPortPair> SortServiceTargets(
    const std::vector<const SrvRecordRdata*>& rdatas) {
  std::map<uint16_t, std::unordered_set<const SrvRecordRdata*>>
      ordered_by_priority;
  for (const SrvRecordRdata* rdata : rdatas) {
    ordered_by_priority[rdata->priority()].insert(rdata);
  }

  std::vector<HostPortPair> sorted_targets;
  for (auto& priority : ordered_by_priority) {
    // With (num results) <= UINT16_MAX (and in practice, much less) and
    // (weight per result) <= UINT16_MAX, then it should be the case that
    // (total weight) <= UINT32_MAX, but use CheckedNumeric for extra safety.
    auto total_weight = base::MakeCheckedNum<uint32_t>(0);
    for (const SrvRecordRdata* rdata : priority.second) {
      total_weight += rdata->weight();
    }

    // Add 1 to total weight because, to deal with 0-weight targets, we want
    // our random selection to be inclusive [0, total].
    total_weight++;

    // Order by weighted random. Make such random selections, removing from
    // |priority.second| until |priority.second| only contains 1 rdata.
    while (priority.second.size() >= 2) {
      uint32_t random_selection =
          base::RandGenerator(total_weight.ValueOrDie());
      const SrvRecordRdata* selected_rdata = nullptr;
      for (const SrvRecordRdata* rdata : priority.second) {
        // >= to always select the first target on |random_selection| == 0,
        // even if its weight is 0.
        if (rdata->weight() >= random_selection) {
          selected_rdata = rdata;
          break;
        }
        random_selection -= rdata->weight();
      }

      DCHECK(selected_rdata);
      sorted_targets.emplace_back(selected_rdata->target(),
                                  selected_rdata->port());
      total_weight -= selected_rdata->weight();
      size_t removed = priority.second.erase(selected_rdata);
      DCHECK_EQ(1u, removed);
    }

    DCHECK_EQ(1u, priority.second.size());
    DCHECK_EQ((total_weight - 1).ValueOrDie(),
              (*priority.second.begin())->weight());
    const SrvRecordRdata* rdata = *priority.second.begin();
    sorted_targets.emplace_back(rdata->target(), rdata->port());
  }

  return sorted_targets;
}

// Validates that all `aliases` form a single non-looping chain, starting from
// `query_name` and that all alias records are valid. Also validates that all
// `data_records` are at the final name at the end of the alias chain.
// TODO(crbug.com/40245250): Consider altering chain TTLs so that each TTL is
// less than or equal to all previous links in the chain.
ExtractionError ValidateNamesAndAliases(
    std::string_view query_name,
    const AliasMap& aliases,
    const std::vector<std::unique_ptr<const RecordParsed>>& data_records,
    std::string& out_final_chain_name) {
  // Validate that all aliases form a single non-looping chain, starting from
  // `query_name`.
  size_t aliases_in_chain = 0;
  std::string target_name =
      dns_names_util::UrlCanonicalizeNameIfAble(query_name);
  for (auto alias = aliases.find(target_name);
       alias != aliases.end() && aliases_in_chain <= aliases.size();
       alias = aliases.find(target_name)) {
    aliases_in_chain++;

    const CnameRecordRdata* cname_data =
        alias->second->rdata<CnameRecordRdata>();
    if (!cname_data) {
      return ExtractionError::kMalformedCname;
    }

    target_name =
        dns_names_util::UrlCanonicalizeNameIfAble(cname_data->cname());
    if (!dns_names_util::IsValidDnsRecordName(target_name)) {
      return ExtractionError::kMalformedCname;
    }
  }

  if (aliases_in_chain != aliases.size()) {
    return ExtractionError::kBadAliasChain;
  }

  // All records must match final alias name.
  for (const auto& record : data_records) {
    DCHECK_NE(record->type(), dns_protocol::kTypeCNAME);
    if (!base::EqualsCaseInsensitiveASCII(
            target_name,
            dns_names_util::UrlCanonicalizeNameIfAble(record->name()))) {
      return ExtractionError::kNameMismatch;
    }
  }

  out_final_chain_name = std::move(target_name);
  return ExtractionError::kOk;
}

// Common results (aliases and errors) are extracted into
// `out_non_data_results`.
RecordsOrError ExtractResponseRecords(
    const DnsResponse& response,
    DnsQueryType query_type,
    base::Time now,
    base::TimeTicks now_ticks,
    std::set<std::unique_ptr<HostResolverInternalResult>>&
        out_non_data_results) {
  DCHECK_EQ(response.question_count(), 1u);

  std::vector<std::unique_ptr<const RecordParsed>> data_records;
  std::optional<base::TimeDelta> response_ttl;

  DnsRecordParser parser = response.Parser();

  // Expected to be validated by DnsTransaction.
  DCHECK_EQ(DnsQueryTypeToQtype(query_type), response.GetSingleQType());

  AliasMap aliases;
  for (unsigned i = 0; i < response.answer_count(); ++i) {
    std::unique_ptr<const RecordParsed> record =
        RecordParsed::CreateFrom(&parser, now);

    if (!record || !dns_names_util::IsValidDnsRecordName(record->name())) {
      return base::unexpected(ExtractionError::kMalformedRecord);
    }

    if (record->klass() == dns_protocol::kClassIN &&
        record->type() == dns_protocol::kTypeCNAME) {
      std::string canonicalized_name =
          dns_names_util::UrlCanonicalizeNameIfAble(record->name());
      DCHECK(dns_names_util::IsValidDnsRecordName(canonicalized_name));

      bool added =
          aliases.emplace(canonicalized_name, std::move(record)).second;
      // Per RFC2181, multiple CNAME records are not allowed for the same name.
      if (!added) {
        return base::unexpected(ExtractionError::kMultipleCnames);
      }
    } else if (record->klass() == dns_protocol::kClassIN &&
               record->type() == DnsQueryTypeToQtype(query_type)) {
      base::TimeDelta ttl = base::Seconds(record->ttl());
      response_ttl =
          std::min(response_ttl.value_or(base::TimeDelta::Max()), ttl);

      data_records.push_back(std::move(record));
    }
  }

  std::string final_chain_name;
  ExtractionError name_and_alias_validation_error = ValidateNamesAndAliases(
      response.GetSingleDottedName(), aliases, data_records, final_chain_name);
  bool has_extraction_error =
      name_and_alias_validation_error != ExtractionError::kOk;

  if (query_type == DnsQueryType::A || query_type == DnsQueryType::AAAA) {
    UMA_HISTOGRAM_BOOLEAN(
        DnsResponseResultExtractor::kHasValidCnameRecordsHistogram,
        !has_extraction_error && !aliases.empty());
  }

  if (has_extraction_error) {
    return base::unexpected(name_and_alias_validation_error);
  }

  std::set<std::unique_ptr<HostResolverInternalResult>> non_data_results;
  for (const auto& alias : aliases) {
    DCHECK(alias.second->rdata<CnameRecordRdata>());
    non_data_results.insert(std::make_unique<HostResolverInternalAliasResult>(
        alias.first, query_type, now_ticks + base::Seconds(alias.second->ttl()),
        now + base::Seconds(alias.second->ttl()), Source::kDns,
        alias.second->rdata<CnameRecordRdata>()->cname()));
  }

  std::optional<base::TimeDelta> error_ttl;
  for (unsigned i = 0; i < response.authority_count(); ++i) {
    DnsResourceRecord record;
    if (!parser.ReadRecord(&record)) {
      // Stop trying to process records if things get malformed in the authority
      // section.
      break;
    }

    if (record.type == dns_protocol::kTypeSOA) {
      base::TimeDelta ttl = base::Seconds(record.ttl);
      error_ttl = std::min(error_ttl.value_or(base::TimeDelta::Max()), ttl);
    }
  }

  // For NXDOMAIN or NODATA (NOERROR with 0 answers matching the qtype), cache
  // an error if an error TTL was found from SOA records. Also, ignore the error
  // if we somehow have result records (most likely if the server incorrectly
  // sends NXDOMAIN with results). Note that, per the weird QNAME definition in
  // RFC2308, section 1, as well as the clarifications in RFC6604, section 3,
  // and in RFC8020, section 2, the cached error is specific to the final chain
  // name, not the query name.
  //
  // TODO(ericorth@chromium.org): Differentiate nxdomain errors by making it
  // cacheable across any query type (per RFC2308, Section 5).
  bool is_cachable_error = data_records.empty() &&
                           (response.rcode() == dns_protocol::kRcodeNXDOMAIN ||
                            response.rcode() == dns_protocol::kRcodeNOERROR);
  if (is_cachable_error && error_ttl.has_value()) {
    non_data_results.insert(std::make_unique<HostResolverInternalErrorResult>(
        final_chain_name, query_type, now_ticks + error_ttl.value(),
        now + error_ttl.value(), Source::kDns, ERR_NAME_NOT_RESOLVED));
  }

  for (unsigned i = 0; i < response.additional_answer_count(); ++i) {
    std::unique_ptr<const RecordParsed> record =
        RecordParsed::CreateFrom(&parser, base::Time::Now());
    if (record && record->klass() == dns_protocol::kClassIN &&
        record->type() == dns_protocol::kTypeHttps) {
      bool is_unsolicited = query_type != DnsQueryType::HTTPS;
      SaveMetricsForAdditionalHttpsRecord(*record, is_unsolicited);
    }
  }

  out_non_data_results = std::move(non_data_results);
  return data_records;
}

ResultsOrError ExtractAddressResults(const DnsResponse& response,
                                     DnsQueryType query_type,
                                     base::Time now,
                                     base::TimeTicks now_ticks) {
  DCHECK_EQ(response.question_count(), 1u);
  DCHECK(query_type == DnsQueryType::A || query_type == DnsQueryType::AAAA);

  std::set<std::unique_ptr<HostResolverInternalResult>> results;
  RecordsOrError records =
      ExtractResponseRecords(response, query_type, now, now_ticks, results);
  if (!records.has_value()) {
    return base::unexpected(records.error());
  }

  std::vector<IPEndPoint> ip_endpoints;
  auto min_ttl = base::TimeDelta::Max();
  for (const auto& record : records.value()) {
    IPAddress address;
    if (query_type == DnsQueryType::A) {
      const ARecordRdata* rdata = record->rdata<ARecordRdata>();
      DCHECK(rdata);
      address = rdata->address();
      DCHECK(address.IsIPv4());
    } else {
      DCHECK_EQ(query_type, DnsQueryType::AAAA);
      const AAAARecordRdata* rdata = record->rdata<AAAARecordRdata>();
      DCHECK(rdata);
      address = rdata->address();
      DCHECK(address.IsIPv6());
    }
    ip_endpoints.emplace_back(address, /*port=*/0);

    base::TimeDelta ttl = base::Seconds(record->ttl());
    min_ttl = std::min(ttl, min_ttl);
  }

  if (!ip_endpoints.empty()) {
    results.insert(std::make_unique<HostResolverInternalDataResult>(
        records->front()->name(), query_type, now_ticks + min_ttl,
        now + min_ttl, Source::kDns, std::move(ip_endpoints),
        std::vector<std::string>{}, std::vector<HostPortPair>{}));
  }

  return results;
}

ResultsOrError ExtractTxtResults(const DnsResponse& response,
                                 base::Time now,
                                 base::TimeTicks now_ticks) {
  std::set<std::unique_ptr<HostResolverInternalResult>> results;
  RecordsOrError txt_records = ExtractResponseRecords(
      response, DnsQueryType::TXT, now, now_ticks, results);
  if (!txt_records.has_value()) {
    return base::unexpected(txt_records.error());
  }

  std::vector<std::string> strings;
  base::TimeDelta min_ttl = base::TimeDelta::Max();
  for (const auto& record : txt_records.value()) {
    const TxtRecordRdata* rdata = record->rdata<net::TxtRecordRdata>();
    DCHECK(rdata);
    // TXT invalid without at least one string. If none, should be rejected by
    // parser.
    CHECK(!rdata->texts().empty());
    strings.insert(strings.end(), rdata->texts().begin(), rdata->texts().end());

    base::TimeDelta ttl = base::Seconds(record->ttl());
    min_ttl = std::min(ttl, min_ttl);
  }

  if (!strings.empty()) {
    results.insert(std::make_unique<HostResolverInternalDataResult>(
        txt_records->front()->name(), DnsQueryType::TXT, now_ticks + min_ttl,
        now + min_ttl, Source::kDns, std::vector<IPEndPoint>{},
        std::move(strings), std::vector<HostPortPair>{}));
  }

  return results;
}

ResultsOrError ExtractPointerResults(const DnsResponse& response,
                                     base::Time now,
                                     base::TimeTicks now_ticks) {
  std::set<std::unique_ptr<HostResolverInternalResult>> results;
  RecordsOrError ptr_records = ExtractResponseRecords(
      response, DnsQueryType::PTR, now, now_ticks, results);
  if (!ptr_records.has_value()) {
    return base::unexpected(ptr_records.error());
  }

  std::vector<HostPortPair> pointers;
  auto min_ttl = base::TimeDelta::Max();
  for (const auto& record : ptr_records.value()) {
    const PtrRecordRdata* rdata = record->rdata<net::PtrRecordRdata>();
    DCHECK(rdata);
    std::string pointer = rdata->ptrdomain();

    // Skip pointers to the root domain.
    if (!pointer.empty()) {
      pointers.emplace_back(std::move(pointer), 0);

      base::TimeDelta ttl = base::Seconds(record->ttl());
      min_ttl = std::min(ttl, min_ttl);
    }
  }

  if (!pointers.empty()) {
    results.insert(std::make_unique<HostResolverInternalDataResult>(
        ptr_records->front()->name(), DnsQueryType::PTR, now_ticks + min_ttl,
        now + min_ttl, Source::kDns, std::vector<IPEndPoint>{},
        std::vector<std::string>{}, std::move(pointers)));
  }

  return results;
}

ResultsOrError ExtractServiceResults(const DnsResponse& response,
                                     base::Time now,
                                     base::TimeTicks now_ticks) {
  std::set<std::unique_ptr<HostResolverInternalResult>> results;
  RecordsOrError srv_records = ExtractResponseRecords(
      response, DnsQueryType::SRV, now, now_ticks, results);
  if (!srv_records.has_value()) {
    return base::unexpected(srv_records.error());
  }

  std::vector<const SrvRecordRdata*> fitered_rdatas;
  auto min_ttl = base::TimeDelta::Max();
  for (const auto& record : srv_records.value()) {
    const SrvRecordRdata* rdata = record->rdata<net::SrvRecordRdata>();
    DCHECK(rdata);

    // Skip pointers to the root domain.
    if (!rdata->target().empty()) {
      fitered_rdatas.push_back(rdata);

      base::TimeDelta ttl = base::Seconds(record->ttl());
      min_ttl = std::min(ttl, min_ttl);
    }
  }

  std::vector<HostPortPair> ordered_service_targets =
      SortServiceTargets(fitered_rdatas);

  if (!ordered_service_targets.empty()) {
    results.insert(std::make_unique<HostResolverInternalDataResult>(
        srv_records->front()->name(), DnsQueryType::SRV, now_ticks + min_ttl,
        now + min_ttl, Source::kDns, std::vector<IPEndPoint>{},
        std::vector<std::string>{}, std::move(ordered_service_targets)));
  }

  return results;
}

const RecordParsed* UnwrapRecordPtr(
    const std::unique_ptr<const RecordParsed>& ptr) {
  return ptr.get();
}

bool RecordIsAlias(const RecordParsed* record) {
  DCHECK(record->rdata<HttpsRecordRdata>());
  return record->rdata<HttpsRecordRdata>()->IsAlias();
}

ResultsOrError ExtractHttpsResults(const DnsResponse& response,
                                   std::string_view original_domain_name,
                                   uint16_t request_port,
                                   base::Time now,
                                   base::TimeTicks now_ticks) {
  DCHECK(!original_domain_name.empty());

  std::set<std::unique_ptr<HostResolverInternalResult>> results;
  RecordsOrError https_records = ExtractResponseRecords(
      response, DnsQueryType::HTTPS, now, now_ticks, results);
  if (!https_records.has_value()) {
    return base::unexpected(https_records.error());
  }

  // Min TTL among records of full use to Chrome.
  std::optional<base::TimeDelta> min_ttl;

  // Min TTL among all records considered compatible with Chrome, per
  // RFC9460#section-8.
  std::optional<base::TimeDelta> min_compatible_ttl;

  std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata> metadatas;
  bool compatible_record_found = false;
  bool default_alpn_found = false;
  for (const auto& record : https_records.value()) {
    const HttpsRecordRdata* rdata = record->rdata<HttpsRecordRdata>();
    DCHECK(rdata);

    base::TimeDelta ttl = base::Seconds(record->ttl());

    // Chrome does not yet support alias records.
    if (rdata->IsAlias()) {
      // Alias records are always considered compatible because they do not
      // support "mandatory" params.
      compatible_record_found = true;
      min_compatible_ttl =
          std::min(ttl, min_compatible_ttl.value_or(base::TimeDelta::Max()));

      continue;
    }

    const ServiceFormHttpsRecordRdata* service = rdata->AsServiceForm();
    if (service->IsCompatible()) {
      compatible_record_found = true;
      min_compatible_ttl =
          std::min(ttl, min_compatible_ttl.value_or(base::TimeDelta::Max()));
    } else {
      // Ignore services incompatible with Chrome's HTTPS record parser.
      // draft-ietf-dnsop-svcb-https-12#section-8
      continue;
    }

    std::string target_name = dns_names_util::UrlCanonicalizeNameIfAble(
        service->service_name().empty() ? record->name()
                                        : service->service_name());

    // Chrome does not yet support followup queries. So only support services at
    // the original domain name or the canonical name (the record name).
    // Note: HostCache::Entry::GetEndpoints() will not return metadatas which
    // target name is different from the canonical name of A/AAAA query results.
    if (!base::EqualsCaseInsensitiveASCII(
            target_name,
            dns_names_util::UrlCanonicalizeNameIfAble(original_domain_name)) &&
        !base::EqualsCaseInsensitiveASCII(
            target_name,
            dns_names_util::UrlCanonicalizeNameIfAble(record->name()))) {
      continue;
    }

    // Ignore services at a different port from the request port. Chrome does
    // not yet support endpoints diverging by port.  Note that before supporting
    // port redirects, Chrome must ensure redirects to the "bad port list" are
    // disallowed. Unclear if such logic would belong here or in socket
    // connection logic.
    if (service->port().has_value() &&
        service->port().value() != request_port) {
      continue;
    }

    ConnectionEndpointMetadata metadata;

    metadata.supported_protocol_alpns = service->alpn_ids();
    if (service->default_alpn() &&
        !base::Contains(metadata.supported_protocol_alpns,
                        dns_protocol::kHttpsServiceDefaultAlpn)) {
      metadata.supported_protocol_alpns.push_back(
          dns_protocol::kHttpsServiceDefaultAlpn);
    }

    // Services with no supported ALPNs (those with "no-default-alpn" and no or
    // empty "alpn") are not self-consistent and are rejected.
    // draft-ietf-dnsop-svcb-https-12#section-7.1.1 and
    // draft-ietf-dnsop-svcb-https-12#section-2.4.3.
    if (metadata.supported_protocol_alpns.empty()) {
      continue;
    }

    metadata.ech_config_list = ConnectionEndpointMetadata::EchConfigList(
        service->ech_config().cbegin(), service->ech_config().cend());

    metadata.target_name = std::move(target_name);

    metadata.trust_anchor_ids = base::ToVector(service->trust_anchor_ids());

    metadatas.emplace(service->priority(), std::move(metadata));

    min_ttl = std::min(ttl, min_ttl.value_or(base::TimeDelta::Max()));

    if (service->default_alpn()) {
      default_alpn_found = true;
    }
  }

  // Ignore all records if any are an alias record. Chrome does not yet support
  // alias records, but aliases take precedence over any other records.
  if (std::ranges::any_of(https_records.value(), &RecordIsAlias,
                          &UnwrapRecordPtr)) {
    metadatas.clear();
  }

  // Ignore all records if they all mark "no-default-alpn". Domains should
  // always provide at least one endpoint allowing default ALPN to ensure a
  // reasonable expectation of connection success.
  // draft-ietf-dnsop-svcb-https-12#section-7.1.2
  if (!default_alpn_found) {
    metadatas.clear();
  }

  if (metadatas.empty() && compatible_record_found) {
    // Empty metadata result signifies that compatible HTTPS records were
    // received but with no contained metadata of use to Chrome. Use the min TTL
    // of all compatible records.
    CHECK(min_compatible_ttl.has_value());
    results.insert(std::make_unique<HostResolverInternalMetadataResult>(
        https_records->front()->name(), DnsQueryType::HTTPS,
        now_ticks + min_compatible_ttl.value(),
        now + min_compatible_ttl.value(), Source::kDns,
        /*metadatas=*/
        std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>{}));
  } else if (!metadatas.empty()) {
    // Use min TTL only of those records contributing useful metadata.
    CHECK(min_ttl.has_value());
    results.insert(std::make_unique<HostResolverInternalMetadataResult>(
        https_records->front()->name(), DnsQueryType::HTTPS,
        now_ticks + min_ttl.value(), now + min_ttl.value(), Source::kDns,
        std::move(metadatas)));
  }

  return results;
}

}  // namespace

DnsResponseResultExtractor::DnsResponseResultExtractor(
    const DnsResponse& response,
    const base::Clock& clock,
    const base::TickClock& tick_clock)
    : response_(response), clock_(clock), tick_clock_(tick_clock) {}

DnsResponseResultExtractor::~DnsResponseResultExtractor() = default;

ResultsOrError DnsResponseResultExtractor::ExtractDnsResults(
    DnsQueryType query_type,
    std::string_view original_domain_name,
    uint16_t request_port) const {
  DCHECK(!original_domain_name.empty());

  switch (query_type) {
    case DnsQueryType::UNSPECIFIED:
      // Should create multiple transactions with specified types.
      NOTREACHED();
    case DnsQueryType::A:
    case DnsQueryType::AAAA:
      return ExtractAddressResults(*response_, query_type, clock_->Now(),
                                   tick_clock_->NowTicks());
    case DnsQueryType::TXT:
      return ExtractTxtResults(*response_, clock_->Now(),
                               tick_clock_->NowTicks());
    case DnsQueryType::PTR:
      return ExtractPointerResults(*response_, clock_->Now(),
                                   tick_clock_->NowTicks());
    case DnsQueryType::SRV:
      return ExtractServiceResults(*response_, clock_->Now(),
                                   tick_clock_->NowTicks());
    case DnsQueryType::HTTPS:
      return ExtractHttpsResults(*response_, original_domain_name, request_port,
                                 clock_->Now(), tick_clock_->NowTicks());
  }
}

}  // namespace net