[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 | |
Eric Orth | bc28a52 | 2019-06-04 16:03:56 | [diff] [blame] | 7 | #include <algorithm> |
Rob Percival | c2b1a17 | 2017-09-25 13:30:42 | [diff] [blame] | 8 | #include <numeric> |
| 9 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 10 | #include "base/big_endian.h" |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 11 | #include "net/dns/dns_response.h" |
Eric Orth | 8afaf15 | 2018-11-07 21:01:26 | [diff] [blame] | 12 | #include "net/dns/public/dns_protocol.h" |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
| 16 | static const size_t kSrvRecordMinimumSize = 6; |
| 17 | |
Qingsi Wang | ecd6925 | 2018-09-27 21:35:55 | [diff] [blame] | 18 | bool RecordRdata::HasValidSize(const base::StringPiece& data, uint16_t type) { |
| 19 | switch (type) { |
| 20 | case dns_protocol::kTypeSRV: |
| 21 | return data.size() >= kSrvRecordMinimumSize; |
| 22 | case dns_protocol::kTypeA: |
| 23 | return data.size() == IPAddress::kIPv4AddressSize; |
| 24 | case dns_protocol::kTypeAAAA: |
| 25 | return data.size() == IPAddress::kIPv6AddressSize; |
| 26 | case dns_protocol::kTypeCNAME: |
| 27 | case dns_protocol::kTypePTR: |
| 28 | case dns_protocol::kTypeTXT: |
| 29 | case dns_protocol::kTypeNSEC: |
| 30 | case dns_protocol::kTypeOPT: |
Eric Orth | 828bd3ae | 2018-12-12 17:30:36 | [diff] [blame] | 31 | case dns_protocol::kTypeSOA: |
Qingsi Wang | ecd6925 | 2018-09-27 21:35:55 | [diff] [blame] | 32 | return true; |
| 33 | default: |
| 34 | VLOG(1) << "Unsupported RDATA type."; |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 39 | SrvRecordRdata::SrvRecordRdata() : priority_(0), weight_(0), port_(0) { |
| 40 | } |
| 41 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 42 | SrvRecordRdata::~SrvRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 43 | |
| 44 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 45 | std::unique_ptr<SrvRecordRdata> SrvRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 46 | const base::StringPiece& data, |
| 47 | const DnsRecordParser& parser) { |
Qingsi Wang | ecd6925 | 2018-09-27 21:35:55 | [diff] [blame] | 48 | if (!HasValidSize(data, kType)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 49 | return std::unique_ptr<SrvRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 50 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 51 | std::unique_ptr<SrvRecordRdata> rdata(new SrvRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 52 | |
[email protected] | d9806a97 | 2014-02-26 18:14:57 | [diff] [blame] | 53 | base::BigEndianReader reader(data.data(), data.size()); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 54 | // 2 bytes for priority, 2 bytes for weight, 2 bytes for port. |
| 55 | reader.ReadU16(&rdata->priority_); |
| 56 | reader.ReadU16(&rdata->weight_); |
| 57 | reader.ReadU16(&rdata->port_); |
| 58 | |
| 59 | if (!parser.ReadName(data.substr(kSrvRecordMinimumSize).begin(), |
| 60 | &rdata->target_)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 61 | return std::unique_ptr<SrvRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 62 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 63 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 64 | } |
| 65 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 66 | uint16_t SrvRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 67 | return SrvRecordRdata::kType; |
| 68 | } |
| 69 | |
| 70 | bool SrvRecordRdata::IsEqual(const RecordRdata* other) const { |
| 71 | if (other->Type() != Type()) return false; |
| 72 | const SrvRecordRdata* srv_other = static_cast<const SrvRecordRdata*>(other); |
| 73 | return weight_ == srv_other->weight_ && |
| 74 | port_ == srv_other->port_ && |
| 75 | priority_ == srv_other->priority_ && |
| 76 | target_ == srv_other->target_; |
| 77 | } |
| 78 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 79 | ARecordRdata::ARecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 80 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 81 | ARecordRdata::~ARecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 82 | |
| 83 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 84 | std::unique_ptr<ARecordRdata> ARecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 85 | const base::StringPiece& data, |
| 86 | const DnsRecordParser& parser) { |
Qingsi Wang | ecd6925 | 2018-09-27 21:35:55 | [diff] [blame] | 87 | if (!HasValidSize(data, kType)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 88 | return std::unique_ptr<ARecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 89 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 90 | std::unique_ptr<ARecordRdata> rdata(new ARecordRdata); |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 91 | rdata->address_ = |
| 92 | IPAddress(reinterpret_cast<const uint8_t*>(data.data()), data.length()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 93 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 94 | } |
| 95 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 96 | uint16_t ARecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 97 | return ARecordRdata::kType; |
| 98 | } |
| 99 | |
| 100 | bool ARecordRdata::IsEqual(const RecordRdata* other) const { |
| 101 | if (other->Type() != Type()) return false; |
| 102 | const ARecordRdata* a_other = static_cast<const ARecordRdata*>(other); |
| 103 | return address_ == a_other->address_; |
| 104 | } |
| 105 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 106 | AAAARecordRdata::AAAARecordRdata() = default; |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 107 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 108 | AAAARecordRdata::~AAAARecordRdata() = default; |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 109 | |
| 110 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 111 | std::unique_ptr<AAAARecordRdata> AAAARecordRdata::Create( |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 112 | const base::StringPiece& data, |
| 113 | const DnsRecordParser& parser) { |
Qingsi Wang | ecd6925 | 2018-09-27 21:35:55 | [diff] [blame] | 114 | if (!HasValidSize(data, kType)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 115 | return std::unique_ptr<AAAARecordRdata>(); |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 116 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 117 | std::unique_ptr<AAAARecordRdata> rdata(new AAAARecordRdata); |
martijn | a23c896 | 2016-03-04 18:18:51 | [diff] [blame] | 118 | rdata->address_ = |
| 119 | IPAddress(reinterpret_cast<const uint8_t*>(data.data()), data.length()); |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 120 | return rdata; |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 121 | } |
| 122 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 123 | uint16_t AAAARecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 124 | return AAAARecordRdata::kType; |
| 125 | } |
| 126 | |
| 127 | bool AAAARecordRdata::IsEqual(const RecordRdata* other) const { |
| 128 | if (other->Type() != Type()) return false; |
| 129 | const AAAARecordRdata* a_other = static_cast<const AAAARecordRdata*>(other); |
| 130 | return address_ == a_other->address_; |
| 131 | } |
| 132 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 133 | CnameRecordRdata::CnameRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 134 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 135 | CnameRecordRdata::~CnameRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 136 | |
| 137 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 138 | std::unique_ptr<CnameRecordRdata> CnameRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 139 | const base::StringPiece& data, |
| 140 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 141 | std::unique_ptr<CnameRecordRdata> rdata(new CnameRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 142 | |
| 143 | if (!parser.ReadName(data.begin(), &rdata->cname_)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 144 | return std::unique_ptr<CnameRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 145 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 146 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 147 | } |
| 148 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 149 | uint16_t CnameRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 150 | return CnameRecordRdata::kType; |
| 151 | } |
| 152 | |
| 153 | bool CnameRecordRdata::IsEqual(const RecordRdata* other) const { |
| 154 | if (other->Type() != Type()) return false; |
| 155 | const CnameRecordRdata* cname_other = |
| 156 | static_cast<const CnameRecordRdata*>(other); |
| 157 | return cname_ == cname_other->cname_; |
| 158 | } |
| 159 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 160 | PtrRecordRdata::PtrRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 161 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 162 | PtrRecordRdata::~PtrRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 163 | |
| 164 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 165 | std::unique_ptr<PtrRecordRdata> PtrRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 166 | const base::StringPiece& data, |
| 167 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 168 | std::unique_ptr<PtrRecordRdata> rdata(new PtrRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 169 | |
| 170 | if (!parser.ReadName(data.begin(), &rdata->ptrdomain_)) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 171 | return std::unique_ptr<PtrRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 172 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 173 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 174 | } |
| 175 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 176 | uint16_t PtrRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 177 | return PtrRecordRdata::kType; |
| 178 | } |
| 179 | |
| 180 | bool PtrRecordRdata::IsEqual(const RecordRdata* other) const { |
| 181 | if (other->Type() != Type()) return false; |
| 182 | const PtrRecordRdata* ptr_other = static_cast<const PtrRecordRdata*>(other); |
| 183 | return ptrdomain_ == ptr_other->ptrdomain_; |
| 184 | } |
| 185 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 186 | TxtRecordRdata::TxtRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 187 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 188 | TxtRecordRdata::~TxtRecordRdata() = default; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 189 | |
| 190 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 191 | std::unique_ptr<TxtRecordRdata> TxtRecordRdata::Create( |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 192 | const base::StringPiece& data, |
| 193 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 194 | std::unique_ptr<TxtRecordRdata> rdata(new TxtRecordRdata); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 195 | |
| 196 | for (size_t i = 0; i < data.size(); ) { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 197 | uint8_t length = data[i]; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 198 | |
| 199 | if (i + length >= data.size()) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 200 | return std::unique_ptr<TxtRecordRdata>(); |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 201 | |
| 202 | rdata->texts_.push_back(data.substr(i + 1, length).as_string()); |
| 203 | |
| 204 | // Move to the next string. |
| 205 | i += length + 1; |
| 206 | } |
| 207 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 208 | return rdata; |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 209 | } |
| 210 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 211 | uint16_t TxtRecordRdata::Type() const { |
[email protected] | f6a9add | 2013-05-23 00:56:36 | [diff] [blame] | 212 | return TxtRecordRdata::kType; |
| 213 | } |
| 214 | |
| 215 | bool TxtRecordRdata::IsEqual(const RecordRdata* other) const { |
| 216 | if (other->Type() != Type()) return false; |
| 217 | const TxtRecordRdata* txt_other = static_cast<const TxtRecordRdata*>(other); |
| 218 | return texts_ == txt_other->texts_; |
| 219 | } |
| 220 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 221 | NsecRecordRdata::NsecRecordRdata() = default; |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 222 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 223 | NsecRecordRdata::~NsecRecordRdata() = default; |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 224 | |
| 225 | // static |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 226 | std::unique_ptr<NsecRecordRdata> NsecRecordRdata::Create( |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 227 | const base::StringPiece& data, |
| 228 | const DnsRecordParser& parser) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 229 | std::unique_ptr<NsecRecordRdata> rdata(new NsecRecordRdata); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 230 | |
| 231 | // Read the "next domain". This part for the NSEC record format is |
| 232 | // ignored for mDNS, since it has no semantic meaning. |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 233 | unsigned next_domain_length = parser.ReadName(data.data(), nullptr); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 234 | |
| 235 | // If we did not succeed in getting the next domain or the data length |
| 236 | // is too short for reading the bitmap header, return. |
| 237 | if (next_domain_length == 0 || data.length() < next_domain_length + 2) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 238 | return std::unique_ptr<NsecRecordRdata>(); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 239 | |
| 240 | struct BitmapHeader { |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 241 | uint8_t block_number; // The block number should be zero. |
| 242 | uint8_t length; // Bitmap length in bytes. Between 1 and 32. |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | const BitmapHeader* header = reinterpret_cast<const BitmapHeader*>( |
| 246 | data.data() + next_domain_length); |
| 247 | |
| 248 | // The block number must be zero in mDns-specific NSEC records. The bitmap |
| 249 | // length must be between 1 and 32. |
| 250 | if (header->block_number != 0 || header->length == 0 || header->length > 32) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 251 | return std::unique_ptr<NsecRecordRdata>(); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 252 | |
| 253 | base::StringPiece bitmap_data = data.substr(next_domain_length + 2); |
| 254 | |
| 255 | // Since we may only have one block, the data length must be exactly equal to |
| 256 | // the domain length plus bitmap size. |
| 257 | if (bitmap_data.length() != header->length) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 258 | return std::unique_ptr<NsecRecordRdata>(); |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 259 | |
| 260 | rdata->bitmap_.insert(rdata->bitmap_.begin(), |
| 261 | bitmap_data.begin(), |
| 262 | bitmap_data.end()); |
| 263 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 264 | return rdata; |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 265 | } |
| 266 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 267 | uint16_t NsecRecordRdata::Type() const { |
[email protected] | 7e09a30 | 2013-06-26 04:12:30 | [diff] [blame] | 268 | return NsecRecordRdata::kType; |
| 269 | } |
| 270 | |
| 271 | bool NsecRecordRdata::IsEqual(const RecordRdata* other) const { |
| 272 | if (other->Type() != Type()) |
| 273 | return false; |
| 274 | const NsecRecordRdata* nsec_other = |
| 275 | static_cast<const NsecRecordRdata*>(other); |
| 276 | return bitmap_ == nsec_other->bitmap_; |
| 277 | } |
| 278 | |
| 279 | bool NsecRecordRdata::GetBit(unsigned i) const { |
| 280 | unsigned byte_num = i/8; |
| 281 | if (bitmap_.size() < byte_num + 1) |
| 282 | return false; |
| 283 | |
| 284 | unsigned bit_num = 7 - i % 8; |
| 285 | return (bitmap_[byte_num] & (1 << bit_num)) != 0; |
| 286 | } |
| 287 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 288 | OptRecordRdata::OptRecordRdata() = default; |
Rob Percival | c2b1a17 | 2017-09-25 13:30:42 | [diff] [blame] | 289 | |
Eric Orth | bc28a52 | 2019-06-04 16:03:56 | [diff] [blame] | 290 | OptRecordRdata::OptRecordRdata(OptRecordRdata&& other) = default; |
| 291 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 292 | OptRecordRdata::~OptRecordRdata() = default; |
Rob Percival | c2b1a17 | 2017-09-25 13:30:42 | [diff] [blame] | 293 | |
Eric Orth | bc28a52 | 2019-06-04 16:03:56 | [diff] [blame] | 294 | OptRecordRdata& OptRecordRdata::operator=(OptRecordRdata&& other) = default; |
| 295 | |
Rob Percival | c2b1a17 | 2017-09-25 13:30:42 | [diff] [blame] | 296 | // static |
| 297 | std::unique_ptr<OptRecordRdata> OptRecordRdata::Create( |
| 298 | const base::StringPiece& data, |
| 299 | const DnsRecordParser& parser) { |
| 300 | std::unique_ptr<OptRecordRdata> rdata(new OptRecordRdata); |
| 301 | rdata->buf_.assign(data.begin(), data.end()); |
| 302 | |
| 303 | base::BigEndianReader reader(data.data(), data.size()); |
| 304 | while (reader.remaining() > 0) { |
| 305 | uint16_t opt_code, opt_data_size; |
| 306 | base::StringPiece opt_data; |
| 307 | |
| 308 | if (!(reader.ReadU16(&opt_code) && reader.ReadU16(&opt_data_size) && |
| 309 | reader.ReadPiece(&opt_data, opt_data_size))) { |
| 310 | return std::unique_ptr<OptRecordRdata>(); |
| 311 | } |
| 312 | rdata->opts_.push_back(Opt(opt_code, opt_data)); |
| 313 | } |
| 314 | |
| 315 | return rdata; |
| 316 | } |
| 317 | |
| 318 | uint16_t OptRecordRdata::Type() const { |
| 319 | return OptRecordRdata::kType; |
| 320 | } |
| 321 | |
| 322 | bool OptRecordRdata::IsEqual(const RecordRdata* other) const { |
| 323 | if (other->Type() != Type()) |
| 324 | return false; |
| 325 | const OptRecordRdata* opt_other = static_cast<const OptRecordRdata*>(other); |
| 326 | return opt_other->opts_ == opts_; |
| 327 | } |
| 328 | |
| 329 | void OptRecordRdata::AddOpt(const Opt& opt) { |
| 330 | base::StringPiece opt_data = opt.data(); |
| 331 | |
| 332 | // Resize buffer to accommodate new OPT. |
| 333 | const size_t orig_rdata_size = buf_.size(); |
| 334 | buf_.resize(orig_rdata_size + Opt::kHeaderSize + opt_data.size()); |
| 335 | |
| 336 | // Start writing from the end of the existing rdata. |
| 337 | base::BigEndianWriter writer(buf_.data() + orig_rdata_size, buf_.size()); |
| 338 | bool success = writer.WriteU16(opt.code()) && |
| 339 | writer.WriteU16(opt_data.size()) && |
| 340 | writer.WriteBytes(opt_data.data(), opt_data.size()); |
| 341 | DCHECK(success); |
| 342 | |
| 343 | opts_.push_back(opt); |
| 344 | } |
| 345 | |
Eric Orth | bc28a52 | 2019-06-04 16:03:56 | [diff] [blame] | 346 | void OptRecordRdata::AddOpts(const OptRecordRdata& other) { |
| 347 | buf_.insert(buf_.end(), other.buf_.begin(), other.buf_.end()); |
| 348 | opts_.insert(opts_.end(), other.opts_.begin(), other.opts_.end()); |
| 349 | } |
| 350 | |
| 351 | bool OptRecordRdata::ContainsOptCode(uint16_t opt_code) const { |
| 352 | return std::any_of( |
| 353 | opts_.begin(), opts_.end(), |
| 354 | [=](const OptRecordRdata::Opt& opt) { return opt.code() == opt_code; }); |
| 355 | } |
| 356 | |
Rob Percival | c2b1a17 | 2017-09-25 13:30:42 | [diff] [blame] | 357 | OptRecordRdata::Opt::Opt(uint16_t code, base::StringPiece data) : code_(code) { |
| 358 | data.CopyToString(&data_); |
| 359 | } |
| 360 | |
| 361 | bool OptRecordRdata::Opt::operator==(const OptRecordRdata::Opt& other) const { |
| 362 | return code_ == other.code_ && data_ == other.data_; |
| 363 | } |
| 364 | |
[email protected] | 4e09d24 | 2013-05-02 03:19:35 | [diff] [blame] | 365 | } // namespace net |