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

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

Issue 8762001: Isolates generic DnsClient from AsyncHostResolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: applied review Created 9 years 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "net/base/dns_util.h" 7 #include "net/base/big_endian.h"
8 #include "net/base/io_buffer.h" 8 #include "net/base/io_buffer.h"
9 #include "net/base/net_errors.h" 9 #include "net/base/net_errors.h"
10 #include "net/base/sys_byteorder.h"
11 #include "net/dns/dns_protocol.h"
10 #include "net/dns/dns_query.h" 12 #include "net/dns/dns_query.h"
11 13
12 namespace net { 14 namespace net {
13 15
14 // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 16 DnsRecordParser::DnsRecordParser() : packet_(NULL), length_(0), cur_(0) {
15 // bytes (not counting the IP nor UDP headers). 17 }
16 static const int kMaxResponseSize = 512;
17 18
18 DnsResponse::DnsResponse(DnsQuery* query) 19 DnsRecordParser::DnsRecordParser(const void* packet,
19 : query_(query), 20 size_t length,
20 io_buffer_(new IOBufferWithSize(kMaxResponseSize + 1)) { 21 size_t offset)
21 DCHECK(query_); 22 : packet_(reinterpret_cast<const char*>(packet)),
23 length_(length),
24 cur_(packet_ + offset) {
25 DCHECK(offset <= length);
mmenke 2011/12/06 20:43:01 nit: Use DCHECK_LE here and in ParseName, where a
szym 2011/12/06 21:06:43 Done.
26 }
27
28 int DnsRecordParser::ParseName(const void* const vpos, std::string* out) const {
29 const char* const pos = reinterpret_cast<const char*>(vpos);
30 DCHECK(packet_);
31 DCHECK(packet_ <= pos);
32 DCHECK(pos <= packet_ + length_);
33
34 const char* p = pos;
35 const char* end = packet_ + length_;
36 // Count number of seen bytes to detect loops.
37 size_t seen = 0;
38 // Remember how many bytes were consumed before first jump.
39 size_t consumed = 0;
40
41 if (pos >= end)
42 return 0;
43
44 if (out) {
45 out->clear();
46 out->reserve(dns_protocol::kMaxNameLength);
47 }
48
49 for (;;) {
50 // The two couple of bits of the length give the type of the length. It's
51 // either a direct length or a pointer to the remainder of the name.
52 switch (*p & dns_protocol::kLabelMask) {
53 case dns_protocol::kLabelPointer: {
54 if (p + sizeof(uint16) > end)
55 return 0;
56 if (consumed == 0) {
57 consumed = p - pos + sizeof(uint16);
58 if (!out)
59 return consumed; // If name is not stored, that's all we need.
60 }
61 seen += sizeof(uint16);
62 // If seen the whole packet, then we must be in a loop.
63 if (seen > length_)
64 return 0;
65 uint16 offset;
66 ReadBigEndian<uint16>(p, &offset);
67 offset &= dns_protocol::kOffsetMask;
68 p = packet_ + offset;
69 if (p >= end)
70 return 0;
71 break;
72 }
73 case dns_protocol::kLabelDirect: {
74 uint8 label_len = *p;
75 ++p;
76 // Note: root domain (".") is NOT included.
77 if (label_len == 0) {
78 if (consumed == 0) {
79 consumed = p - pos;
80 } // else we set |consumed| before first jump
81 return consumed;
82 }
83 if (p + label_len >= end)
84 return 0; // Truncated or missing label.
85 if (out) {
86 if (!out->empty())
87 out->append(".");
88 out->append(p, label_len);
89 }
90 p += label_len;
91 seen += 1 + label_len;
92 break;
93 }
94 default:
95 // unhandled label type
96 return 0;
97 }
98 }
99 }
100
101 bool DnsRecordParser::ParseRecord(DnsResourceRecord* out) {
102 DCHECK(packet_);
103 size_t consumed = ParseName(cur_, &out->name);
104 if (!consumed)
105 return false;
106 BigEndianReader reader(cur_ + consumed,
107 packet_ + length_ - (cur_ + consumed));
108 uint16 rdlen;
109 if (reader.ReadU16(&out->type) &&
110 reader.ReadU16(&out->klass) &&
111 reader.ReadU32(&out->ttl) &&
112 reader.ReadU16(&rdlen) &&
113 reader.ReadPiece(&out->rdata, rdlen)) {
114 cur_ = reader.ptr();
115 return true;
116 }
117 return false;
118 }
119
120 DnsResponse::DnsResponse()
121 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) {
122 }
123
124 DnsResponse::DnsResponse(const void* data,
125 size_t length,
126 size_t answer_offset)
127 : io_buffer_(new IOBufferWithSize(length)),
128 parser_(io_buffer_->data(), length, answer_offset) {
129 memcpy(io_buffer_->data(), data, length);
22 } 130 }
23 131
24 DnsResponse::~DnsResponse() { 132 DnsResponse::~DnsResponse() {
25 } 133 }
26 134
27 int DnsResponse::Parse(int nbytes, IPAddressList* ip_addresses) { 135 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) {
28 // Response includes query, it should be at least that size. 136 // Response includes query, it should be at least that size.
29 if (nbytes < query_->io_buffer()->size() || nbytes > kMaxResponseSize) 137 if (nbytes < query.io_buffer()->size() || nbytes > dns_protocol::kMaxUDPSize)
30 return ERR_DNS_MALFORMED_RESPONSE; 138 return false;
31 139
32 DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()), 140 // Match the query id.
33 io_buffer_->size()); 141 if (ntohs(header()->id) != query.id())
34 uint16 id; 142 return false;
35 if (!response.U16(&id) || id != query_->id()) // Make sure IDs match.
36 return ERR_DNS_MALFORMED_RESPONSE;
37 143
38 uint8 flags, rcode; 144 // Match question count.
39 if (!response.U8(&flags) || !response.U8(&rcode)) 145 if (ntohs(header()->qdcount) != 1)
40 return ERR_DNS_MALFORMED_RESPONSE; 146 return false;
41 147
42 if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?). 148 // Match the question section.
43 return ERR_DNS_SERVER_REQUIRES_TCP; 149 const size_t hdr_size = sizeof(dns_protocol::Header);
150 const base::StringPiece question = query.question();
151 if (question != base::StringPiece(io_buffer_->data() + hdr_size,
152 question.size()))
mmenke 2011/12/06 20:43:01 nit: Add parentheses.
szym 2011/12/06 21:06:43 Done.
153 return false;
44 154
45 rcode &= 0x0f; // 3 means NXDOMAIN, the rest means server failed. 155 // Construct the parser.
46 if (rcode && (rcode != 3)) 156 parser_ = DnsRecordParser(io_buffer_->data(),
47 return ERR_DNS_SERVER_FAILED; 157 nbytes,
158 hdr_size + question.size());
159 return true;
160 }
48 161
49 uint16 query_count, answer_count, authority_count, additional_count; 162 uint8 DnsResponse::flags0() const {
50 if (!response.U16(&query_count) || 163 return header()->flags[0];
51 !response.U16(&answer_count) || 164 }
52 !response.U16(&authority_count) ||
53 !response.U16(&additional_count)) {
54 return ERR_DNS_MALFORMED_RESPONSE;
55 }
56 165
57 if (query_count != 1) // Sent a single question, shouldn't have changed. 166 uint8 DnsResponse::flags1() const {
58 return ERR_DNS_MALFORMED_RESPONSE; 167 return header()->flags[1] & ~(dns_protocol::kRcodeMask);
168 }
59 169
60 base::StringPiece question; // Make sure question section is echoed back. 170 uint8 DnsResponse::rcode() const {
61 if (!response.Block(&question, query_->question_size()) || 171 return header()->flags[1] & dns_protocol::kRcodeMask;
62 memcmp(question.data(), query_->question_data(), 172 }
63 query_->question_size())) {
64 return ERR_DNS_MALFORMED_RESPONSE;
65 }
66 173
67 if (answer_count < 1) 174 int DnsResponse::answer_count() const {
68 return ERR_NAME_NOT_RESOLVED; 175 return ntohs(header()->ancount);
176 }
69 177
70 IPAddressList rdatas; 178 DnsRecordParser DnsResponse::Parser() const {
71 while (answer_count--) { 179 DCHECK(parser_.IsValid());
72 uint32 ttl; 180 return parser_;
73 uint16 rdlength, qtype, qclass; 181 }
74 if (!response.DNSName(NULL) ||
75 !response.U16(&qtype) ||
76 !response.U16(&qclass) ||
77 !response.U32(&ttl) ||
78 !response.U16(&rdlength)) {
79 return ERR_DNS_MALFORMED_RESPONSE;
80 }
81 if (qtype == query_->qtype() &&
82 qclass == kClassIN &&
83 (rdlength == kIPv4AddressSize || rdlength == kIPv6AddressSize)) {
84 base::StringPiece rdata;
85 if (!response.Block(&rdata, rdlength))
86 return ERR_DNS_MALFORMED_RESPONSE;
87 rdatas.push_back(IPAddressNumber(rdata.begin(), rdata.end()));
88 } else if (!response.Skip(rdlength))
89 return ERR_DNS_MALFORMED_RESPONSE;
90 }
91 182
92 if (rdatas.empty()) 183 const dns_protocol::Header* DnsResponse::header() const {
93 return ERR_NAME_NOT_RESOLVED; 184 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
94
95 if (ip_addresses)
96 ip_addresses->swap(rdatas);
97 return OK;
98 } 185 }
99 186
100 } // namespace net 187 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698