xref: /aosp_15_r20/external/cronet/net/dns/record_rdata.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 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_RECORD_RDATA_H_
6 #define NET_DNS_RECORD_RDATA_H_
7 
8 #include <stdint.h>
9 
10 #include <map>
11 #include <memory>
12 #include <optional>
13 #include <string>
14 #include <string_view>
15 #include <vector>
16 
17 #include "base/check_op.h"
18 #include "base/compiler_specific.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/ip_address.h"
21 #include "net/base/net_export.h"
22 #include "net/dns/public/dns_protocol.h"
23 #include "third_party/boringssl/src/include/openssl/sha.h"
24 
25 namespace net {
26 
27 class DnsRecordParser;
28 
29 // Parsed represenation of the extra data in a record. Does not include standard
30 // DNS record data such as TTL, Name, Type and Class.
31 class NET_EXPORT RecordRdata {
32  public:
33   virtual ~RecordRdata() = default;
34 
35   // Return true if `data` represents RDATA in the wire format with a valid size
36   // for the give `type`. Always returns true for unrecognized `type`s as the
37   // size is never known to be invalid.
38   static bool HasValidSize(std::string_view data, uint16_t type);
39 
40   virtual bool IsEqual(const RecordRdata* other) const = 0;
41   virtual uint16_t Type() const = 0;
42 };
43 
44 // SRV record format (http://www.ietf.org/rfc/rfc2782.txt):
45 // 2 bytes network-order unsigned priority
46 // 2 bytes network-order unsigned weight
47 // 2 bytes network-order unsigned port
48 // target: domain name (on-the-wire representation)
49 class NET_EXPORT_PRIVATE SrvRecordRdata : public RecordRdata {
50  public:
51   static const uint16_t kType = dns_protocol::kTypeSRV;
52 
53   SrvRecordRdata(const SrvRecordRdata&) = delete;
54   SrvRecordRdata& operator=(const SrvRecordRdata&) = delete;
55 
56   ~SrvRecordRdata() override;
57   static std::unique_ptr<SrvRecordRdata> Create(std::string_view data,
58                                                 const DnsRecordParser& parser);
59 
60   bool IsEqual(const RecordRdata* other) const override;
61   uint16_t Type() const override;
62 
priority()63   uint16_t priority() const { return priority_; }
weight()64   uint16_t weight() const { return weight_; }
port()65   uint16_t port() const { return port_; }
66 
target()67   const std::string& target() const { return target_; }
68 
69  private:
70   SrvRecordRdata();
71 
72   uint16_t priority_ = 0;
73   uint16_t weight_ = 0;
74   uint16_t port_ = 0;
75 
76   std::string target_;
77 };
78 
79 // A Record format (http://www.ietf.org/rfc/rfc1035.txt):
80 // 4 bytes for IP address.
81 class NET_EXPORT ARecordRdata : public RecordRdata {
82  public:
83   static const uint16_t kType = dns_protocol::kTypeA;
84 
85   ARecordRdata(const ARecordRdata&) = delete;
86   ARecordRdata& operator=(const ARecordRdata&) = delete;
87 
88   ~ARecordRdata() override;
89   static std::unique_ptr<ARecordRdata> Create(std::string_view data,
90                                               const DnsRecordParser& parser);
91   bool IsEqual(const RecordRdata* other) const override;
92   uint16_t Type() const override;
93 
address()94   const IPAddress& address() const { return address_; }
95 
96  private:
97   ARecordRdata();
98 
99   IPAddress address_;
100 };
101 
102 // AAAA Record format (http://www.ietf.org/rfc/rfc1035.txt):
103 // 16 bytes for IP address.
104 class NET_EXPORT AAAARecordRdata : public RecordRdata {
105  public:
106   static const uint16_t kType = dns_protocol::kTypeAAAA;
107 
108   AAAARecordRdata(const AAAARecordRdata&) = delete;
109   AAAARecordRdata& operator=(const AAAARecordRdata&) = delete;
110 
111   ~AAAARecordRdata() override;
112   static std::unique_ptr<AAAARecordRdata> Create(std::string_view data,
113                                                  const DnsRecordParser& parser);
114   bool IsEqual(const RecordRdata* other) const override;
115   uint16_t Type() const override;
116 
address()117   const IPAddress& address() const { return address_; }
118 
119  private:
120   AAAARecordRdata();
121 
122   IPAddress address_;
123 };
124 
125 // CNAME record format (http://www.ietf.org/rfc/rfc1035.txt):
126 // cname: On the wire representation of domain name.
127 class NET_EXPORT_PRIVATE CnameRecordRdata : public RecordRdata {
128  public:
129   static const uint16_t kType = dns_protocol::kTypeCNAME;
130 
131   CnameRecordRdata(const CnameRecordRdata&) = delete;
132   CnameRecordRdata& operator=(const CnameRecordRdata&) = delete;
133 
134   ~CnameRecordRdata() override;
135   static std::unique_ptr<CnameRecordRdata> Create(
136       std::string_view data,
137       const DnsRecordParser& parser);
138   bool IsEqual(const RecordRdata* other) const override;
139   uint16_t Type() const override;
140 
cname()141   const std::string& cname() const { return cname_; }
142 
143  private:
144   CnameRecordRdata();
145 
146   std::string cname_;
147 };
148 
149 // PTR record format (http://www.ietf.org/rfc/rfc1035.txt):
150 // domain: On the wire representation of domain name.
151 class NET_EXPORT_PRIVATE PtrRecordRdata : public RecordRdata {
152  public:
153   static const uint16_t kType = dns_protocol::kTypePTR;
154 
155   PtrRecordRdata(const PtrRecordRdata&) = delete;
156   PtrRecordRdata& operator=(const PtrRecordRdata&) = delete;
157 
158   ~PtrRecordRdata() override;
159   static std::unique_ptr<PtrRecordRdata> Create(std::string_view data,
160                                                 const DnsRecordParser& parser);
161   bool IsEqual(const RecordRdata* other) const override;
162   uint16_t Type() const override;
163 
ptrdomain()164   std::string ptrdomain() const { return ptrdomain_; }
165 
166  private:
167   PtrRecordRdata();
168 
169   std::string ptrdomain_;
170 };
171 
172 // TXT record format (http://www.ietf.org/rfc/rfc1035.txt):
173 // texts: One or more <character-string>s.
174 // a <character-string> is a length octet followed by as many characters.
175 class NET_EXPORT_PRIVATE TxtRecordRdata : public RecordRdata {
176  public:
177   static const uint16_t kType = dns_protocol::kTypeTXT;
178 
179   TxtRecordRdata(const TxtRecordRdata&) = delete;
180   TxtRecordRdata& operator=(const TxtRecordRdata&) = delete;
181 
182   ~TxtRecordRdata() override;
183   static std::unique_ptr<TxtRecordRdata> Create(std::string_view data,
184                                                 const DnsRecordParser& parser);
185   bool IsEqual(const RecordRdata* other) const override;
186   uint16_t Type() const override;
187 
texts()188   const std::vector<std::string>& texts() const { return texts_; }
189 
190  private:
191   TxtRecordRdata();
192 
193   std::vector<std::string> texts_;
194 };
195 
196 // Only the subset of the NSEC record format required by mDNS is supported.
197 // Nsec record format is described in http://www.ietf.org/rfc/rfc3845.txt and
198 // the limited version required for mDNS described in
199 // http://www.rfc-editor.org/rfc/rfc6762.txt Section 6.1.
200 class NET_EXPORT_PRIVATE NsecRecordRdata : public RecordRdata {
201  public:
202   static const uint16_t kType = dns_protocol::kTypeNSEC;
203 
204   NsecRecordRdata(const NsecRecordRdata&) = delete;
205   NsecRecordRdata& operator=(const NsecRecordRdata&) = delete;
206 
207   ~NsecRecordRdata() override;
208   static std::unique_ptr<NsecRecordRdata> Create(std::string_view data,
209                                                  const DnsRecordParser& parser);
210   bool IsEqual(const RecordRdata* other) const override;
211   uint16_t Type() const override;
212 
213   // Length of the bitmap in bits.
214   // This will be between 8 and 256, per RFC 3845, Section 2.1.2.
bitmap_length()215   uint16_t bitmap_length() const {
216     DCHECK_LE(bitmap_.size(), 32u);
217     return static_cast<uint16_t>(bitmap_.size() * 8);
218   }
219 
220   // Returns bit i-th bit in the bitmap, where bits withing a byte are organized
221   // most to least significant. If it is set, a record with rrtype i exists for
222   // the domain name of this nsec record.
223   bool GetBit(unsigned i) const;
224 
225  private:
226   NsecRecordRdata();
227 
228   std::vector<uint8_t> bitmap_;
229 };
230 
231 }  // namespace net
232 
233 #endif  // NET_DNS_RECORD_RDATA_H_
234