[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 | #include "net/dns/record_rdata.h" |
| 6 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 7 | #include "base/big_endian.h" |
eroman | c9a6b72 | 2015-06-03 22:19:00 | [diff] [blame] | 8 | #include "net/base/ip_address_number.h" |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 9 | #include "net/dns/dns_protocol.h" |
| 10 | #include "net/dns/dns_response.h" |
| 11 | |
| 12 | namespace net { |
| 13 | |
| 14 | static const size_t kSrvRecordMinimumSize = 6; |
| 15 | |
| 16 | RecordRdata::RecordRdata() { |
| 17 | } |
| 18 | |
| 19 | SrvRecordRdata::SrvRecordRdata() : priority_(0), weight_(0), port_(0) { |
| 20 | } |
| 21 | |
| 22 | SrvRecordRdata::~SrvRecordRdata() {} |
| 23 | |
| 24 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 25 | std::unique_ptr<SrvRecordRdata> SrvRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 26 | const base::StringPiece& data, |
| 27 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 28 | if (data.size() < kSrvRecordMinimumSize) |
| 29 | return std::unique_ptr<SrvRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 30 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 31 | std::unique_ptr<SrvRecordRdata> rdata(new SrvRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 32 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 33 | base::BigEndianReader reader(data.data(), data.size()); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 34 | // 2 bytes for priority, 2 bytes for weight, 2 bytes for port. |
| 35 | reader.ReadU16(&rdata->priority_); |
| 36 | reader.ReadU16(&rdata->weight_); |
| 37 | reader.ReadU16(&rdata->port_); |
| 38 | |
| 39 | if (!parser.ReadName(data.substr(kSrvRecordMinimumSize).begin(), |
| 40 | &rdata->target_)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 41 | return std::unique_ptr<SrvRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 42 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 43 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 44 | } |
| 45 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 46 | uint16_t SrvRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 47 | return SrvRecordRdata::kType; |
| 48 | } |
| 49 | |
| 50 | bool SrvRecordRdata::IsEqual(const RecordRdata* other) const { |
| 51 | if (other->Type() != Type()) return false; |
| 52 | const SrvRecordRdata* srv_other = static_cast<const SrvRecordRdata*>(other); |
| 53 | return weight_ == srv_other->weight_ && |
| 54 | port_ == srv_other->port_ && |
| 55 | priority_ == srv_other->priority_ && |
| 56 | target_ == srv_other->target_; |
| 57 | } |
| 58 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 59 | ARecordRdata::ARecordRdata() { |
| 60 | } |
| 61 | |
| 62 | ARecordRdata::~ARecordRdata() { |
| 63 | } |
| 64 | |
| 65 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 66 | std::unique_ptr<ARecordRdata> ARecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 67 | const base::StringPiece& data, |
| 68 | const DnsRecordParser& parser) { |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 69 | if (data.size() != IPAddress::kIPv4AddressSize) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 70 | return std::unique_ptr<ARecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 71 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 72 | std::unique_ptr<ARecordRdata> rdata(new ARecordRdata); |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 73 | rdata->address_ = |
| 74 | IPAddress(reinterpret_cast<const uint8_t*>(data.data()), data.length()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 75 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 76 | } |
| 77 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 78 | uint16_t ARecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 79 | return ARecordRdata::kType; |
| 80 | } |
| 81 | |
| 82 | bool ARecordRdata::IsEqual(const RecordRdata* other) const { |
| 83 | if (other->Type() != Type()) return false; |
| 84 | const ARecordRdata* a_other = static_cast<const ARecordRdata*>(other); |
| 85 | return address_ == a_other->address_; |
| 86 | } |
| 87 | |
| 88 | AAAARecordRdata::AAAARecordRdata() { |
| 89 | } |
| 90 | |
| 91 | AAAARecordRdata::~AAAARecordRdata() { |
| 92 | } |
| 93 | |
| 94 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 95 | std::unique_ptr<AAAARecordRdata> AAAARecordRdata::Create( |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 96 | const base::StringPiece& data, |
| 97 | const DnsRecordParser& parser) { |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 98 | if (data.size() != IPAddress::kIPv6AddressSize) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 99 | return std::unique_ptr<AAAARecordRdata>(); |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 100 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 101 | std::unique_ptr<AAAARecordRdata> rdata(new AAAARecordRdata); |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 102 | rdata->address_ = |
| 103 | IPAddress(reinterpret_cast<const uint8_t*>(data.data()), data.length()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 104 | return rdata; |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 105 | } |
| 106 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 107 | uint16_t AAAARecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 108 | return AAAARecordRdata::kType; |
| 109 | } |
| 110 | |
| 111 | bool AAAARecordRdata::IsEqual(const RecordRdata* other) const { |
| 112 | if (other->Type() != Type()) return false; |
| 113 | const AAAARecordRdata* a_other = static_cast<const AAAARecordRdata*>(other); |
| 114 | return address_ == a_other->address_; |
| 115 | } |
| 116 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 117 | CnameRecordRdata::CnameRecordRdata() { |
| 118 | } |
| 119 | |
| 120 | CnameRecordRdata::~CnameRecordRdata() { |
| 121 | } |
| 122 | |
| 123 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 124 | std::unique_ptr<CnameRecordRdata> CnameRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 125 | const base::StringPiece& data, |
| 126 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 127 | std::unique_ptr<CnameRecordRdata> rdata(new CnameRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 128 | |
| 129 | if (!parser.ReadName(data.begin(), &rdata->cname_)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 130 | return std::unique_ptr<CnameRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 131 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 132 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 133 | } |
| 134 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 135 | uint16_t CnameRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 136 | return CnameRecordRdata::kType; |
| 137 | } |
| 138 | |
| 139 | bool CnameRecordRdata::IsEqual(const RecordRdata* other) const { |
| 140 | if (other->Type() != Type()) return false; |
| 141 | const CnameRecordRdata* cname_other = |
| 142 | static_cast<const CnameRecordRdata*>(other); |
| 143 | return cname_ == cname_other->cname_; |
| 144 | } |
| 145 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 146 | PtrRecordRdata::PtrRecordRdata() { |
| 147 | } |
| 148 | |
| 149 | PtrRecordRdata::~PtrRecordRdata() { |
| 150 | } |
| 151 | |
| 152 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 153 | std::unique_ptr<PtrRecordRdata> PtrRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 154 | const base::StringPiece& data, |
| 155 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 156 | std::unique_ptr<PtrRecordRdata> rdata(new PtrRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 157 | |
| 158 | if (!parser.ReadName(data.begin(), &rdata->ptrdomain_)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 159 | return std::unique_ptr<PtrRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 160 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 161 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 162 | } |
| 163 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 164 | uint16_t PtrRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 165 | return PtrRecordRdata::kType; |
| 166 | } |
| 167 | |
| 168 | bool PtrRecordRdata::IsEqual(const RecordRdata* other) const { |
| 169 | if (other->Type() != Type()) return false; |
| 170 | const PtrRecordRdata* ptr_other = static_cast<const PtrRecordRdata*>(other); |
| 171 | return ptrdomain_ == ptr_other->ptrdomain_; |
| 172 | } |
| 173 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 174 | TxtRecordRdata::TxtRecordRdata() { |
| 175 | } |
| 176 | |
| 177 | TxtRecordRdata::~TxtRecordRdata() { |
| 178 | } |
| 179 | |
| 180 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 181 | std::unique_ptr<TxtRecordRdata> TxtRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 182 | const base::StringPiece& data, |
| 183 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 184 | std::unique_ptr<TxtRecordRdata> rdata(new TxtRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 185 | |
| 186 | for (size_t i = 0; i < data.size(); ) { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 187 | uint8_t length = data[i]; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 188 | |
| 189 | if (i + length >= data.size()) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 190 | return std::unique_ptr<TxtRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 191 | |
| 192 | rdata->texts_.push_back(data.substr(i + 1, length).as_string()); |
| 193 | |
| 194 | // Move to the next string. |
| 195 | i += length + 1; |
| 196 | } |
| 197 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 198 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 199 | } |
| 200 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 201 | uint16_t TxtRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 202 | return TxtRecordRdata::kType; |
| 203 | } |
| 204 | |
| 205 | bool TxtRecordRdata::IsEqual(const RecordRdata* other) const { |
| 206 | if (other->Type() != Type()) return false; |
| 207 | const TxtRecordRdata* txt_other = static_cast<const TxtRecordRdata*>(other); |
| 208 | return texts_ == txt_other->texts_; |
| 209 | } |
| 210 | |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 211 | NsecRecordRdata::NsecRecordRdata() { |
| 212 | } |
| 213 | |
| 214 | NsecRecordRdata::~NsecRecordRdata() { |
| 215 | } |
| 216 | |
| 217 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 218 | std::unique_ptr<NsecRecordRdata> NsecRecordRdata::Create( |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 219 | const base::StringPiece& data, |
| 220 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 221 | std::unique_ptr<NsecRecordRdata> rdata(new NsecRecordRdata); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 222 | |
| 223 | // Read the "next domain". This part for the NSEC record format is |
| 224 | // ignored for mDNS, since it has no semantic meaning. |
| 225 | unsigned next_domain_length = parser.ReadName(data.data(), NULL); |
| 226 | |
| 227 | // If we did not succeed in getting the next domain or the data length |
| 228 | // is too short for reading the bitmap header, return. |
| 229 | if (next_domain_length == 0 || data.length() < next_domain_length + 2) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 230 | return std::unique_ptr<NsecRecordRdata>(); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 231 | |
| 232 | struct BitmapHeader { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 233 | uint8_t block_number; // The block number should be zero. |
| 234 | uint8_t length; // Bitmap length in bytes. Between 1 and 32. |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | const BitmapHeader* header = reinterpret_cast<const BitmapHeader*>( |
| 238 | data.data() + next_domain_length); |
| 239 | |
| 240 | // The block number must be zero in mDns-specific NSEC records. The bitmap |
| 241 | // length must be between 1 and 32. |
| 242 | if (header->block_number != 0 || header->length == 0 || header->length > 32) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 243 | return std::unique_ptr<NsecRecordRdata>(); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 244 | |
| 245 | base::StringPiece bitmap_data = data.substr(next_domain_length + 2); |
| 246 | |
| 247 | // Since we may only have one block, the data length must be exactly equal to |
| 248 | // the domain length plus bitmap size. |
| 249 | if (bitmap_data.length() != header->length) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame^] | 250 | return std::unique_ptr<NsecRecordRdata>(); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 251 | |
| 252 | rdata->bitmap_.insert(rdata->bitmap_.begin(), |
| 253 | bitmap_data.begin(), |
| 254 | bitmap_data.end()); |
| 255 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 256 | return rdata; |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 257 | } |
| 258 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 259 | uint16_t NsecRecordRdata::Type() const { |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 260 | return NsecRecordRdata::kType; |
| 261 | } |
| 262 | |
| 263 | bool NsecRecordRdata::IsEqual(const RecordRdata* other) const { |
| 264 | if (other->Type() != Type()) |
| 265 | return false; |
| 266 | const NsecRecordRdata* nsec_other = |
| 267 | static_cast<const NsecRecordRdata*>(other); |
| 268 | return bitmap_ == nsec_other->bitmap_; |
| 269 | } |
| 270 | |
| 271 | bool NsecRecordRdata::GetBit(unsigned i) const { |
| 272 | unsigned byte_num = i/8; |
| 273 | if (bitmap_.size() < byte_num + 1) |
| 274 | return false; |
| 275 | |
| 276 | unsigned bit_num = 7 - i % 8; |
| 277 | return (bitmap_[byte_num] & (1 << bit_num)) != 0; |
| 278 | } |
| 279 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 280 | } // namespace net |