• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
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 <string>
9 #include <vector>
10 
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_piece.h"
15 #include "net/base/big_endian.h"
16 #include "net/base/net_export.h"
17 #include "net/base/net_util.h"
18 #include "net/dns/dns_protocol.h"
19 
20 namespace net {
21 
22 class DnsRecordParser;
23 
24 // Parsed represenation of the extra data in a record. Does not include standard
25 // DNS record data such as TTL, Name, Type and Class.
26 class NET_EXPORT_PRIVATE RecordRdata {
27  public:
~RecordRdata()28   virtual ~RecordRdata() {}
29 
30   virtual bool IsEqual(const RecordRdata* other) const = 0;
31   virtual uint16 Type() const = 0;
32 
33  protected:
34   RecordRdata();
35 
36   DISALLOW_COPY_AND_ASSIGN(RecordRdata);
37 };
38 
39 // SRV record format (http://www.ietf.org/rfc/rfc2782.txt):
40 // 2 bytes network-order unsigned priority
41 // 2 bytes network-order unsigned weight
42 // 2 bytes network-order unsigned port
43 // target: domain name (on-the-wire representation)
44 class NET_EXPORT_PRIVATE SrvRecordRdata : public RecordRdata {
45  public:
46   static const uint16 kType = dns_protocol::kTypeSRV;
47 
48   virtual ~SrvRecordRdata();
49   static scoped_ptr<SrvRecordRdata> Create(const base::StringPiece& data,
50                                            const DnsRecordParser& parser);
51 
52   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
53   virtual uint16 Type() const OVERRIDE;
54 
priority()55   uint16 priority() const { return priority_; }
weight()56   uint16 weight() const { return weight_; }
port()57   uint16 port() const { return port_; }
58 
target()59   const std::string& target() const { return target_; }
60 
61  private:
62   SrvRecordRdata();
63 
64   uint16 priority_;
65   uint16 weight_;
66   uint16 port_;
67 
68   std::string target_;
69 
70   DISALLOW_COPY_AND_ASSIGN(SrvRecordRdata);
71 };
72 
73 // A Record format (http://www.ietf.org/rfc/rfc1035.txt):
74 // 4 bytes for IP address.
75 class NET_EXPORT_PRIVATE ARecordRdata : public RecordRdata {
76  public:
77   static const uint16 kType = dns_protocol::kTypeA;
78 
79   virtual ~ARecordRdata();
80   static scoped_ptr<ARecordRdata> Create(const base::StringPiece& data,
81                                          const DnsRecordParser& parser);
82   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
83   virtual uint16 Type() const OVERRIDE;
84 
address()85   const IPAddressNumber& address() const { return address_; }
86 
87  private:
88   ARecordRdata();
89 
90   IPAddressNumber address_;
91 
92   DISALLOW_COPY_AND_ASSIGN(ARecordRdata);
93 };
94 
95 // AAAA Record format (http://www.ietf.org/rfc/rfc1035.txt):
96 // 16 bytes for IP address.
97 class NET_EXPORT_PRIVATE AAAARecordRdata : public RecordRdata {
98  public:
99   static const uint16 kType = dns_protocol::kTypeAAAA;
100 
101   virtual ~AAAARecordRdata();
102   static scoped_ptr<AAAARecordRdata> Create(const base::StringPiece& data,
103                                          const DnsRecordParser& parser);
104   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
105   virtual uint16 Type() const OVERRIDE;
106 
address()107   const IPAddressNumber& address() const { return address_; }
108 
109  private:
110   AAAARecordRdata();
111 
112   IPAddressNumber address_;
113 
114   DISALLOW_COPY_AND_ASSIGN(AAAARecordRdata);
115 };
116 
117 // CNAME record format (http://www.ietf.org/rfc/rfc1035.txt):
118 // cname: On the wire representation of domain name.
119 class NET_EXPORT_PRIVATE CnameRecordRdata : public RecordRdata {
120  public:
121   static const uint16 kType = dns_protocol::kTypeCNAME;
122 
123   virtual ~CnameRecordRdata();
124   static scoped_ptr<CnameRecordRdata> Create(const base::StringPiece& data,
125                                              const DnsRecordParser& parser);
126   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
127   virtual uint16 Type() const OVERRIDE;
128 
cname()129   std::string cname() const { return cname_; }
130 
131  private:
132   CnameRecordRdata();
133 
134   std::string cname_;
135 
136   DISALLOW_COPY_AND_ASSIGN(CnameRecordRdata);
137 };
138 
139 // PTR record format (http://www.ietf.org/rfc/rfc1035.txt):
140 // domain: On the wire representation of domain name.
141 class NET_EXPORT_PRIVATE PtrRecordRdata : public RecordRdata {
142  public:
143   static const uint16 kType = dns_protocol::kTypePTR;
144 
145   virtual ~PtrRecordRdata();
146   static scoped_ptr<PtrRecordRdata> Create(const base::StringPiece& data,
147                                            const DnsRecordParser& parser);
148   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
149   virtual uint16 Type() const OVERRIDE;
150 
ptrdomain()151   std::string ptrdomain() const { return ptrdomain_; }
152 
153  private:
154   PtrRecordRdata();
155 
156   std::string ptrdomain_;
157 
158   DISALLOW_COPY_AND_ASSIGN(PtrRecordRdata);
159 };
160 
161 // TXT record format (http://www.ietf.org/rfc/rfc1035.txt):
162 // texts: One or more <character-string>s.
163 // a <character-string> is a length octet followed by as many characters.
164 class NET_EXPORT_PRIVATE TxtRecordRdata : public RecordRdata {
165  public:
166   static const uint16 kType = dns_protocol::kTypeTXT;
167 
168   virtual ~TxtRecordRdata();
169   static scoped_ptr<TxtRecordRdata> Create(const base::StringPiece& data,
170                                            const DnsRecordParser& parser);
171   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
172   virtual uint16 Type() const OVERRIDE;
173 
texts()174   const std::vector<std::string>& texts() const { return texts_; }
175 
176  private:
177   TxtRecordRdata();
178 
179   std::vector<std::string> texts_;
180 
181   DISALLOW_COPY_AND_ASSIGN(TxtRecordRdata);
182 };
183 
184 // Only the subset of the NSEC record format required by mDNS is supported.
185 // Nsec record format is described in http://www.ietf.org/rfc/rfc3845.txt and
186 // the limited version required for mDNS described in
187 // http://www.rfc-editor.org/rfc/rfc6762.txt Section 6.1.
188 class NET_EXPORT_PRIVATE NsecRecordRdata : public RecordRdata {
189  public:
190   static const uint16 kType = dns_protocol::kTypeNSEC;
191 
192   virtual ~NsecRecordRdata();
193   static scoped_ptr<NsecRecordRdata> Create(const base::StringPiece& data,
194                                             const DnsRecordParser& parser);
195   virtual bool IsEqual(const RecordRdata* other) const OVERRIDE;
196   virtual uint16 Type() const OVERRIDE;
197 
198   // Length of the bitmap in bits.
bitmap_length()199   unsigned bitmap_length() const { return bitmap_.size() * 8; }
200 
201   // Returns bit i-th bit in the bitmap, where bits withing a byte are organized
202   // most to least significant. If it is set, a record with rrtype i exists for
203   // the domain name of this nsec record.
204   bool GetBit(unsigned i) const;
205 
206  private:
207   NsecRecordRdata();
208 
209   std::vector<uint8> bitmap_;
210 
211   DISALLOW_COPY_AND_ASSIGN(NsecRecordRdata);
212 };
213 
214 
215 }  // namespace net
216 
217 #endif  // NET_DNS_RECORD_RDATA_H_
218