• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Moves do not clone an independent buffer.
66   DnsQuery(DnsQuery&& query);
67   DnsQuery& operator=(DnsQuery&& query);
68 
69   ~DnsQuery();
70 
71   // Clones |this| verbatim, with ID field of the header set to |id|.
72   std::unique_ptr<DnsQuery> CloneWithNewId(uint16_t id) const;
73 
74   // Returns true and populates the query if the internally stored raw packet
75   // can be parsed. This should only be called when DnsQuery is constructed from
76   // the raw buffer.
77   // |valid_bytes| indicates the number of initialized bytes in the raw buffer.
78   // E.g. if the buffer holds a packet received from the network, the buffer may
79   // be allocated with the maximum size of a UDP packet, but |valid_bytes|
80   // indicates the number of bytes actually received from the network. If the
81   // parsing requires reading more than the number of initialized bytes, this
82   // method fails and returns false.
83   bool Parse(size_t valid_bytes);
84 
85   // DnsQuery field accessors.
86   uint16_t id() const;
87   base::span<const uint8_t> qname() const;
88   uint16_t qtype() const;
89 
90   // Returns the Question section of the query.  Used when matching the
91   // response.
92   std::string_view question() const;
93 
94   // Returns the size of the question section.
95   size_t question_size() const;
96 
97   // IOBuffer accessor to be used for writing out the query. The buffer has
98   // the same byte layout as the DNS query wire format.
io_buffer()99   IOBufferWithSize* io_buffer() const { return io_buffer_.get(); }
100 
101   void set_flags(uint16_t flags);
102 
103  private:
104   DnsQuery(const DnsQuery& orig, uint16_t id);
105   void CopyFrom(const DnsQuery& orig);
106 
107   bool ReadHeader(base::SpanReader<const uint8_t>* reader,
108                   dns_protocol::Header* out);
109   // After read, |out| is in the DNS format, e.g.
110   // "\x03""www""\x08""chromium""\x03""com""\x00". Use DNSDomainToString to
111   // convert to the dotted format "www.chromium.com" with no trailing dot.
112   bool ReadName(base::SpanReader<const uint8_t>* reader, std::string* out);
113 
114   // Returns the Header pointer into the `io_buffer_`. Only valid to call on a
115   // DNSQuery has a valid IOBuffer, so this never returns null.
116   //
117   // TODO(davidben): Dereferencing the returned pointer will be UB. The correct
118   // shape of this function would be to do a memcpy into/out of a Header to read
119   // out of/into the buffer.
header_in_io_buffer()120   const dns_protocol::Header* header_in_io_buffer() const {
121     CHECK(io_buffer_ && !io_buffer_->span().empty());
122     return reinterpret_cast<dns_protocol::Header*>(io_buffer_->span().data());
123   }
header_in_io_buffer()124   dns_protocol::Header* header_in_io_buffer() {
125     CHECK(io_buffer_ && !io_buffer_->span().empty());
126     return reinterpret_cast<dns_protocol::Header*>(io_buffer_->span().data());
127   }
128 
129   // Size of the DNS name (*NOT* hostname) we are trying to resolve; used
130   // to calculate offsets.
131   size_t qname_size_ = 0;
132 
133   // Contains query bytes to be consumed by higher level Write() call.
134   scoped_refptr<IOBufferWithSize> io_buffer_;
135 };
136 
137 }  // namespace net
138 
139 #endif  // NET_DNS_DNS_QUERY_H_
140