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