• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DNS_DNS_RESPONSE_RESULT_EXTRACTOR_H_
6 #define NET_DNS_DNS_RESPONSE_RESULT_EXTRACTOR_H_
7 
8 #include <stdint.h>
9 
10 #include "base/memory/raw_ptr.h"
11 #include "base/strings/string_piece.h"
12 #include "net/base/net_export.h"
13 #include "net/dns/host_cache.h"
14 #include "net/dns/public/dns_query_type.h"
15 
16 namespace net {
17 
18 class DnsResponse;
19 
20 // Higher-level parser to take a DnsResponse and extract results.
21 class NET_EXPORT_PRIVATE DnsResponseResultExtractor {
22  public:
23   enum class ExtractionError {
24     kOk = 0,
25     // Record failed to parse.
26     kMalformedRecord,
27     // Malformed CNAME
28     kMalformedCname,
29     // Found CNAME or result record with an unexpected name.
30     kNameMismatch,
31     // Malformed result record
32     kMalformedResult,
33     // CNAME record after a result record
34     kCnameAfterResult,
35     // Multiple CNAME records for the same owner name.
36     kMultipleCnames,
37     // Invalid alias chain, e.g. contains loops or disjoint aliases.
38     kBadAliasChain,
39     // Not expected. Used for DCHECKs.
40     kUnexpected,
41   };
42 
43   explicit DnsResponseResultExtractor(const DnsResponse* response);
44   ~DnsResponseResultExtractor();
45 
46   DnsResponseResultExtractor(const DnsResponseResultExtractor&) = delete;
47   DnsResponseResultExtractor& operator=(const DnsResponseResultExtractor&) =
48       delete;
49 
50   // Extract results from the response. `query_type` must match the qtype from
51   // the DNS query, and it must have already been validated (expected to be done
52   // by DnsTransaction) that the response matches the query.
53   //
54   // `original_domain_name` is the query name (in dotted form) before any
55   // aliasing or prepending port/scheme. It is expected to be the name under
56   // which any basic query types, e.g. A or AAAA, are queried.
57   //
58   // May have the side effect of recording metrics about DnsResponses as they
59   // are parsed, so while not an absolute requirement, any given DnsResponse
60   // should only be used and extracted from at most once.
61   ExtractionError ExtractDnsResults(DnsQueryType query_type,
62                                     base::StringPiece original_domain_name,
63                                     uint16_t request_port,
64                                     HostCache::Entry* out_results) const;
65 
66   // Creates the results of a NODATA response (successfully parsed but without
67   // any results) appropriate for `query_type`.
68   static HostCache::Entry CreateEmptyResult(DnsQueryType query_type);
69 
70  private:
71   const raw_ptr<const DnsResponse, DanglingUntriaged> response_;
72 };
73 
74 }  // namespace net
75 
76 #endif  // NET_DNS_DNS_RESPONSE_RESULT_EXTRACTOR_H_
77