Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: net/dns/dns_response.cc

Issue 9369045: [net] HostResolverImpl + DnsTransaction + DnsConfigService = Asynchronous DNS ready for experiments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased to master. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 int DnsRecordParser::ReadName(const void* const vpos, std::string* out) const {
36 const char* const pos = reinterpret_cast<const char*>(vpos); 38 const char* const pos = reinterpret_cast<const char*>(vpos);
37 DCHECK(packet_); 39 DCHECK(packet_);
38 DCHECK_LE(packet_, pos); 40 DCHECK_LE(packet_, pos);
39 DCHECK_LE(pos, packet_ + length_); 41 DCHECK_LE(pos, packet_ + length_);
40 42
41 const char* p = pos; 43 const char* p = pos;
42 const char* end = packet_ + length_; 44 const char* end = packet_ + length_;
43 // Count number of seen bytes to detect loops. 45 // Count number of seen bytes to detect loops.
44 size_t seen = 0; 46 size_t seen = 0;
45 // Remember how many bytes were consumed before first jump. 47 // Remember how many bytes were consumed before first jump.
46 size_t consumed = 0; 48 size_t consumed = 0;
47 49
48 if (pos >= end) 50 if (pos >= end)
49 return 0; 51 return 0;
50 52
51 if (out) { 53 if (out) {
52 out->clear(); 54 out->clear();
53 out->reserve(dns_protocol::kMaxNameLength); 55 out->reserve(dns_protocol::kMaxNameLength);
54 } 56 }
55 57
56 for (;;) { 58 for (;;) {
57 // The two couple of bits of the length give the type of the length. It's 59 // 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. 60 // either a direct length or a pointer to the remainder of the name.
59 switch (*p & dns_protocol::kLabelMask) { 61 switch (*p & dns_protocol::kLabelMask) {
60 case dns_protocol::kLabelPointer: { 62 case dns_protocol::kLabelPointer: {
61 if (p + sizeof(uint16) > end) 63 if (p + sizeof(uint16) > end)
62 return 0; 64 return 0;
63 if (consumed == 0) { 65 if (consumed == 0) {
64 consumed = p - pos + sizeof(uint16); 66 consumed = p - pos + sizeof(uint16);
65 if (!out) 67 if (!out)
66 return consumed; // If name is not stored, that's all we need. 68 return consumed; // If name is not stored, that's all we need.
67 } 69 }
(...skipping 30 matching lines...) Expand all
98 seen += 1 + label_len; 100 seen += 1 + label_len;
99 break; 101 break;
100 } 102 }
101 default: 103 default:
102 // unhandled label type 104 // unhandled label type
103 return 0; 105 return 0;
104 } 106 }
105 } 107 }
106 } 108 }
107 109
108 bool DnsRecordParser::ParseRecord(DnsResourceRecord* out) { 110 bool DnsRecordParser::ReadRecord(DnsResourceRecord* out) {
109 DCHECK(packet_); 111 DCHECK(packet_);
110 size_t consumed = ParseName(cur_, &out->name); 112 size_t consumed = ReadName(cur_, &out->name);
111 if (!consumed) 113 if (!consumed)
112 return false; 114 return false;
113 BigEndianReader reader(cur_ + consumed, 115 BigEndianReader reader(cur_ + consumed,
114 packet_ + length_ - (cur_ + consumed)); 116 packet_ + length_ - (cur_ + consumed));
115 uint16 rdlen; 117 uint16 rdlen;
116 if (reader.ReadU16(&out->type) && 118 if (reader.ReadU16(&out->type) &&
117 reader.ReadU16(&out->klass) && 119 reader.ReadU16(&out->klass) &&
118 reader.ReadU32(&out->ttl) && 120 reader.ReadU32(&out->ttl) &&
119 reader.ReadU16(&rdlen) && 121 reader.ReadU16(&rdlen) &&
120 reader.ReadPiece(&out->rdata, rdlen)) { 122 reader.ReadPiece(&out->rdata, rdlen)) {
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 DnsRecordParser DnsResponse::Parser() const { 215 DnsRecordParser DnsResponse::Parser() const {
214 DCHECK(parser_.IsValid()); 216 DCHECK(parser_.IsValid());
215 // Return a copy of the parser. 217 // Return a copy of the parser.
216 return parser_; 218 return parser_;
217 } 219 }
218 220
219 const dns_protocol::Header* DnsResponse::header() const { 221 const dns_protocol::Header* DnsResponse::header() const {
220 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); 222 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
221 } 223 }
222 224
225 DnsResponse::Result DnsResponse::ParseToAddressList(
226 AddressList* addr_list,
227 base::TimeDelta* ttl) const {
228 DCHECK(IsValid());
229 // DnsTransaction already verified that |response| matches the issued query.
230 // We still need to determine if there is a valid chain of CNAMEs from the
231 // query name to the RR owner name.
232
233 // Expected owner of record. No trailing dot.
234 std::string expected_name = GetDottedName();
235
236 uint16 expected_type = qtype();
237 DCHECK(expected_type == dns_protocol::kTypeA ||
238 expected_type == dns_protocol::kTypeAAAA);
239
240 size_t expected_size = (expected_type == dns_protocol::kTypeAAAA)
241 ? kIPv6AddressSize : kIPv4AddressSize;
242
243 // getcanonname in eglibc returns the first owner name of an A or AAAA RR.
244 std::string canon_name;
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 while (parser.ReadRecord(&record)) {
252 if (base::strcasecmp(record.name.c_str(), expected_name.c_str()) != 0) {
253 if (record.type == expected_type)
cbentzel 2012/02/15 19:48:35 Are there any cases where DNS servers will append
szym 2012/02/15 20:28:05 It might. I'll add a comment clarifying that for n
254 return DNS_NAME_MISMATCH;
255 continue;
cbentzel 2012/02/15 19:48:35 Why do you want to continue in this case?
szym 2012/02/15 20:28:05 Hmmm. I'll make it unconditionally reject.
256 }
257 if ((record.type == dns_protocol::kTypeCNAME)) {
cbentzel 2012/02/15 19:48:35 Nit: double parents not needed.
szym 2012/02/15 20:28:05 clang barfed on it :-/
258 // Following the CNAME chain, only if no addresses seen.
259 if (!ip_addresses.empty()) {
cbentzel 2012/02/15 19:48:35 Nit: extra braces
260 return DNS_CNAME_AFTER_ADDRESS;
261 }
262
263 if (!parser.ReadName(record.rdata.begin(), &expected_name)) {
264 return DNS_MALFORMED_RESPONSE;
265 }
266 cname_ttl_sec = std::min(cname_ttl_sec, record.ttl);
267 } else if (record.type == expected_type) {
268 if (record.rdata.size() != expected_size)
269 return DNS_SIZE_MISMATCH;
270 if (ip_addresses.empty()) {
271 addr_ttl_sec = record.ttl;
272 } else {
273 if (addr_ttl_sec != record.ttl)
274 return DNS_ADDRESS_TTL_MISMATCH;
275 }
276 ip_addresses.push_back(IPAddressNumber(record.rdata.begin(),
277 record.rdata.end()));
278 if (canon_name.empty())
279 canon_name = record.name;
cbentzel 2012/02/15 19:48:35 Shouldn't the canon_name always be the same? Shoul
szym 2012/02/15 20:28:05 All the other checks will already catch that. This
280 }
281 }
282 if (!parser.AtEnd())
283 return DNS_MALFORMED_RESPONSE;
284
285 if (ip_addresses.empty())
286 return DNS_NO_ADDRESSES;
287
288 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses,
289 canon_name);
290 *ttl = base::TimeDelta::FromSeconds(std::min(cname_ttl_sec, addr_ttl_sec));
291 return DNS_SUCCESS;
292 }
293
223 } // namespace net 294 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698