xref: /aosp_15_r20/external/cronet/net/dns/dns_query.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 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_QUERY_H_
6 #define NET_DNS_DNS_QUERY_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 #include <string_view>
14 
15 #include "base/containers/span.h"
16 #include "base/containers/span_reader.h"
17 #include "base/memory/raw_ptr.h"
18 #include "base/memory/scoped_refptr.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_export.h"
21 
22 namespace net {
23 
24 class OptRecordRdata;
25 
26 namespace dns_protocol {
27 struct Header;
28 }  // namespace dns_protocol
29 
30 class IOBufferWithSize;
31 
32 // Represents on-the-wire DNS query message as an object.
33 class NET_EXPORT_PRIVATE DnsQuery {
34  public:
35   enum class PaddingStrategy {
36     // Query will not be padded. Recommended strategy when query will not be
37     // encrypted.
38     NONE,
39 
40     // Query will be padded to the next multiple of 128 octets. Recommended
41     // strategy (per RFC 8467) when query will be encrypted, e.g. through
42     // DNS-over-HTTPS.
43     BLOCK_LENGTH_128,
44   };
45 
46   // Constructs a query message from |qname| which *MUST* be in a valid
47   // DNS name format, and |qtype|. The qclass is set to IN.
48   // If |opt_rdata| is not null, an OPT record will be added to the "Additional"
49   // section of the query.
50   DnsQuery(uint16_t id,
51            base::span<const uint8_t> qname,
52            uint16_t qtype,
53            const OptRecordRdata* opt_rdata = nullptr,
54            PaddingStrategy padding_strategy = PaddingStrategy::NONE);
55 
56   // Constructs an empty query from a raw packet in |buffer|. If the raw packet
57   // represents a valid DNS query in the wire format (RFC 1035), Parse() will
58   // populate the empty query.
59   explicit DnsQuery(scoped_refptr<IOBufferWithSize> buffer);
60 
61   // Copies are constructed with an independent cloned, not mirrored, buffer.
62   DnsQuery(const DnsQuery& query);
63   DnsQuery& operator=(const DnsQuery& query);
64 
65   ~DnsQuery();
66 
67   // Clones |this| verbatim, with ID field of the header set to |id|.
68   std::unique_ptr<DnsQuery> CloneWithNewId(uint16_t id) const;
69 
70   // Returns true and populates the query if the internally stored raw packet
71   // can be parsed. This should only be called when DnsQuery is constructed from
72   // the raw buffer.
73   // |valid_bytes| indicates the number of initialized bytes in the raw buffer.
74   // E.g. if the buffer holds a packet received from the network, the buffer may
75   // be allocated with the maximum size of a UDP packet, but |valid_bytes|
76   // indicates the number of bytes actually received from the network. If the
77   // parsing requires reading more than the number of initialized bytes, this
78   // method fails and returns false.
79   bool Parse(size_t valid_bytes);
80 
81   // DnsQuery field accessors.
82   uint16_t id() const;
83   base::span<const uint8_t> qname() const;
84   uint16_t qtype() const;
85 
86   // Returns the Question section of the query.  Used when matching the
87   // response.
88   std::string_view question() const;
89 
90   // Returns the size of the question section.
91   size_t question_size() const;
92 
93   // IOBuffer accessor to be used for writing out the query. The buffer has
94   // the same byte layout as the DNS query wire format.
io_buffer()95   IOBufferWithSize* io_buffer() const { return io_buffer_.get(); }
96 
97   void set_flags(uint16_t flags);
98 
99  private:
100   DnsQuery(const DnsQuery& orig, uint16_t id);
101   void CopyFrom(const DnsQuery& orig);
102 
103   bool ReadHeader(base::SpanReader<const uint8_t>* reader,
104                   dns_protocol::Header* out);
105   // After read, |out| is in the DNS format, e.g.
106   // "\x03""www""\x08""chromium""\x03""com""\x00". Use DNSDomainToString to
107   // convert to the dotted format "www.chromium.com" with no trailing dot.
108   bool ReadName(base::SpanReader<const uint8_t>* reader, std::string* out);
109 
110   // Returns the Header pointer into the `io_buffer_`. Only valid to call on a
111   // DNSQuery has a valid IOBuffer, so this never returns null.
112   //
113   // TODO(davidben): Dereferencing the returned pointer will be UB. The correct
114   // shape of this function would be to do a memcpy into/out of a Header to read
115   // out of/into the buffer.
header_in_io_buffer()116   const dns_protocol::Header* header_in_io_buffer() const {
117     CHECK(io_buffer_ && !io_buffer_->span().empty());
118     return reinterpret_cast<dns_protocol::Header*>(io_buffer_->span().data());
119   }
header_in_io_buffer()120   dns_protocol::Header* header_in_io_buffer() {
121     CHECK(io_buffer_ && !io_buffer_->span().empty());
122     return reinterpret_cast<dns_protocol::Header*>(io_buffer_->span().data());
123   }
124 
125   // Size of the DNS name (*NOT* hostname) we are trying to resolve; used
126   // to calculate offsets.
127   size_t qname_size_ = 0;
128 
129   // Contains query bytes to be consumed by higher level Write() call.
130   scoped_refptr<IOBufferWithSize> io_buffer_;
131 };
132 
133 }  // namespace net
134 
135 #endif  // NET_DNS_DNS_QUERY_H_
136