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