OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/dns_response.h" | 5 #include "net/dns/dns_response.h" |
6 | 6 |
| 7 #include "base/string_util.h" |
7 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
| 9 #include "net/base/address_list.h" |
8 #include "net/base/big_endian.h" | 10 #include "net/base/big_endian.h" |
9 #include "net/base/dns_util.h" | 11 #include "net/base/dns_util.h" |
10 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
12 #include "net/dns/dns_protocol.h" | 14 #include "net/dns/dns_protocol.h" |
13 #include "net/dns/dns_query.h" | 15 #include "net/dns/dns_query.h" |
14 | 16 |
15 namespace net { | 17 namespace net { |
16 | 18 |
17 DnsResourceRecord::DnsResourceRecord() { | 19 DnsResourceRecord::DnsResourceRecord() { |
18 } | 20 } |
19 | 21 |
20 DnsResourceRecord::~DnsResourceRecord() { | 22 DnsResourceRecord::~DnsResourceRecord() { |
21 } | 23 } |
22 | 24 |
23 DnsRecordParser::DnsRecordParser() : packet_(NULL), length_(0), cur_(0) { | 25 DnsRecordParser::DnsRecordParser() : packet_(NULL), length_(0), cur_(0) { |
24 } | 26 } |
25 | 27 |
26 DnsRecordParser::DnsRecordParser(const void* packet, | 28 DnsRecordParser::DnsRecordParser(const void* packet, |
27 size_t length, | 29 size_t length, |
28 size_t offset) | 30 size_t offset) |
29 : packet_(reinterpret_cast<const char*>(packet)), | 31 : packet_(reinterpret_cast<const char*>(packet)), |
30 length_(length), | 32 length_(length), |
31 cur_(packet_ + offset) { | 33 cur_(packet_ + offset) { |
32 DCHECK_LE(offset, length); | 34 DCHECK_LE(offset, length); |
33 } | 35 } |
34 | 36 |
35 int DnsRecordParser::ParseName(const void* const vpos, std::string* out) const { | 37 unsigned DnsRecordParser::ReadName(const void* const vpos, |
| 38 std::string* out) const { |
36 const char* const pos = reinterpret_cast<const char*>(vpos); | 39 const char* const pos = reinterpret_cast<const char*>(vpos); |
37 DCHECK(packet_); | 40 DCHECK(packet_); |
38 DCHECK_LE(packet_, pos); | 41 DCHECK_LE(packet_, pos); |
39 DCHECK_LE(pos, packet_ + length_); | 42 DCHECK_LE(pos, packet_ + length_); |
40 | 43 |
41 const char* p = pos; | 44 const char* p = pos; |
42 const char* end = packet_ + length_; | 45 const char* end = packet_ + length_; |
43 // Count number of seen bytes to detect loops. | 46 // Count number of seen bytes to detect loops. |
44 size_t seen = 0; | 47 unsigned seen = 0; |
45 // Remember how many bytes were consumed before first jump. | 48 // Remember how many bytes were consumed before first jump. |
46 size_t consumed = 0; | 49 unsigned consumed = 0; |
47 | 50 |
48 if (pos >= end) | 51 if (pos >= end) |
49 return 0; | 52 return 0; |
50 | 53 |
51 if (out) { | 54 if (out) { |
52 out->clear(); | 55 out->clear(); |
53 out->reserve(dns_protocol::kMaxNameLength); | 56 out->reserve(dns_protocol::kMaxNameLength); |
54 } | 57 } |
55 | 58 |
56 for (;;) { | 59 for (;;) { |
57 // The two couple of bits of the length give the type of the length. It's | 60 // The first two bits of the length give the type of the length. It's |
58 // either a direct length or a pointer to the remainder of the name. | 61 // either a direct length or a pointer to the remainder of the name. |
59 switch (*p & dns_protocol::kLabelMask) { | 62 switch (*p & dns_protocol::kLabelMask) { |
60 case dns_protocol::kLabelPointer: { | 63 case dns_protocol::kLabelPointer: { |
61 if (p + sizeof(uint16) > end) | 64 if (p + sizeof(uint16) > end) |
62 return 0; | 65 return 0; |
63 if (consumed == 0) { | 66 if (consumed == 0) { |
64 consumed = p - pos + sizeof(uint16); | 67 consumed = p - pos + sizeof(uint16); |
65 if (!out) | 68 if (!out) |
66 return consumed; // If name is not stored, that's all we need. | 69 return consumed; // If name is not stored, that's all we need. |
67 } | 70 } |
(...skipping 30 matching lines...) Expand all Loading... |
98 seen += 1 + label_len; | 101 seen += 1 + label_len; |
99 break; | 102 break; |
100 } | 103 } |
101 default: | 104 default: |
102 // unhandled label type | 105 // unhandled label type |
103 return 0; | 106 return 0; |
104 } | 107 } |
105 } | 108 } |
106 } | 109 } |
107 | 110 |
108 bool DnsRecordParser::ParseRecord(DnsResourceRecord* out) { | 111 bool DnsRecordParser::ReadRecord(DnsResourceRecord* out) { |
109 DCHECK(packet_); | 112 DCHECK(packet_); |
110 size_t consumed = ParseName(cur_, &out->name); | 113 size_t consumed = ReadName(cur_, &out->name); |
111 if (!consumed) | 114 if (!consumed) |
112 return false; | 115 return false; |
113 BigEndianReader reader(cur_ + consumed, | 116 BigEndianReader reader(cur_ + consumed, |
114 packet_ + length_ - (cur_ + consumed)); | 117 packet_ + length_ - (cur_ + consumed)); |
115 uint16 rdlen; | 118 uint16 rdlen; |
116 if (reader.ReadU16(&out->type) && | 119 if (reader.ReadU16(&out->type) && |
117 reader.ReadU16(&out->klass) && | 120 reader.ReadU16(&out->klass) && |
118 reader.ReadU32(&out->ttl) && | 121 reader.ReadU32(&out->ttl) && |
119 reader.ReadU16(&rdlen) && | 122 reader.ReadU16(&rdlen) && |
120 reader.ReadPiece(&out->rdata, rdlen)) { | 123 reader.ReadPiece(&out->rdata, rdlen)) { |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 uint16 DnsResponse::flags() const { | 178 uint16 DnsResponse::flags() const { |
176 DCHECK(parser_.IsValid()); | 179 DCHECK(parser_.IsValid()); |
177 return ntohs(header()->flags) & ~(dns_protocol::kRcodeMask); | 180 return ntohs(header()->flags) & ~(dns_protocol::kRcodeMask); |
178 } | 181 } |
179 | 182 |
180 uint8 DnsResponse::rcode() const { | 183 uint8 DnsResponse::rcode() const { |
181 DCHECK(parser_.IsValid()); | 184 DCHECK(parser_.IsValid()); |
182 return ntohs(header()->flags) & dns_protocol::kRcodeMask; | 185 return ntohs(header()->flags) & dns_protocol::kRcodeMask; |
183 } | 186 } |
184 | 187 |
185 int DnsResponse::answer_count() const { | 188 unsigned DnsResponse::answer_count() const { |
186 DCHECK(parser_.IsValid()); | 189 DCHECK(parser_.IsValid()); |
187 return ntohs(header()->ancount); | 190 return ntohs(header()->ancount); |
188 } | 191 } |
189 | 192 |
190 base::StringPiece DnsResponse::qname() const { | 193 base::StringPiece DnsResponse::qname() const { |
191 DCHECK(parser_.IsValid()); | 194 DCHECK(parser_.IsValid()); |
192 // The response is HEADER QNAME QTYPE QCLASS ANSWER. | 195 // The response is HEADER QNAME QTYPE QCLASS ANSWER. |
193 // |parser_| is positioned at the beginning of ANSWER, so the end of QNAME is | 196 // |parser_| is positioned at the beginning of ANSWER, so the end of QNAME is |
194 // two uint16s before it. | 197 // two uint16s before it. |
195 const size_t hdr_size = sizeof(dns_protocol::Header); | 198 const size_t hdr_size = sizeof(dns_protocol::Header); |
(...skipping 17 matching lines...) Expand all Loading... |
213 DnsRecordParser DnsResponse::Parser() const { | 216 DnsRecordParser DnsResponse::Parser() const { |
214 DCHECK(parser_.IsValid()); | 217 DCHECK(parser_.IsValid()); |
215 // Return a copy of the parser. | 218 // Return a copy of the parser. |
216 return parser_; | 219 return parser_; |
217 } | 220 } |
218 | 221 |
219 const dns_protocol::Header* DnsResponse::header() const { | 222 const dns_protocol::Header* DnsResponse::header() const { |
220 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); | 223 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); |
221 } | 224 } |
222 | 225 |
| 226 DnsResponse::Result DnsResponse::ParseToAddressList( |
| 227 AddressList* addr_list, |
| 228 base::TimeDelta* ttl) const { |
| 229 DCHECK(IsValid()); |
| 230 // DnsTransaction already verified that |response| matches the issued query. |
| 231 // We still need to determine if there is a valid chain of CNAMEs from the |
| 232 // query name to the RR owner name. |
| 233 // We err on the side of caution with the assumption that if we are too picky, |
| 234 // we can always fall back to the system getaddrinfo. |
| 235 |
| 236 // Expected owner of record. No trailing dot. |
| 237 std::string expected_name = GetDottedName(); |
| 238 |
| 239 uint16 expected_type = qtype(); |
| 240 DCHECK(expected_type == dns_protocol::kTypeA || |
| 241 expected_type == dns_protocol::kTypeAAAA); |
| 242 |
| 243 size_t expected_size = (expected_type == dns_protocol::kTypeAAAA) |
| 244 ? kIPv6AddressSize : kIPv4AddressSize; |
| 245 |
| 246 uint32 cname_ttl_sec = kuint32max; |
| 247 uint32 addr_ttl_sec = kuint32max; |
| 248 IPAddressList ip_addresses; |
| 249 DnsRecordParser parser = Parser(); |
| 250 DnsResourceRecord record; |
| 251 unsigned ancount = answer_count(); |
| 252 for (unsigned i = 0; i < ancount; ++i) { |
| 253 if (!parser.ReadRecord(&record)) |
| 254 return DNS_MALFORMED_RESPONSE; |
| 255 |
| 256 if (base::strcasecmp(record.name.c_str(), expected_name.c_str()) != 0) |
| 257 return DNS_NAME_MISMATCH; |
| 258 if (record.type == dns_protocol::kTypeCNAME) { |
| 259 // Following the CNAME chain, only if no addresses seen. |
| 260 if (!ip_addresses.empty()) |
| 261 return DNS_CNAME_AFTER_ADDRESS; |
| 262 |
| 263 if (record.rdata.size() != |
| 264 parser.ReadName(record.rdata.begin(), &expected_name)) |
| 265 return DNS_MALFORMED_CNAME; |
| 266 |
| 267 cname_ttl_sec = std::min(cname_ttl_sec, record.ttl); |
| 268 } else if (record.type == expected_type) { |
| 269 if (record.rdata.size() != expected_size) |
| 270 return DNS_SIZE_MISMATCH; |
| 271 if (ip_addresses.empty()) { |
| 272 addr_ttl_sec = record.ttl; |
| 273 } else { |
| 274 if (addr_ttl_sec != record.ttl) |
| 275 return DNS_ADDRESS_TTL_MISMATCH; |
| 276 } |
| 277 ip_addresses.push_back(IPAddressNumber(record.rdata.begin(), |
| 278 record.rdata.end())); |
| 279 } |
| 280 } |
| 281 |
| 282 if (ip_addresses.empty()) |
| 283 return DNS_NO_ADDRESSES; |
| 284 |
| 285 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. |
| 286 // If the response passed all the checks so far, then |expected_name| is it. |
| 287 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
| 288 expected_name); |
| 289 *ttl = base::TimeDelta::FromSeconds(std::min(cname_ttl_sec, addr_ttl_sec)); |
| 290 return DNS_SUCCESS; |
| 291 } |
| 292 |
223 } // namespace net | 293 } // namespace net |
OLD | NEW |