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

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: 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/bigendian.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);
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 (out) {
42 out->clear();
43 out->reserve(dns_protocol::kMaxNameLength);
44 }
45
46 for (;;) {
47 // The two couple of bits of the length give the type of the length. It's
48 // either a direct length or a pointer to the remainder of the name.
49 switch (*p & dns_protocol::kLabelMask) {
50 case dns_protocol::kLabelPointer: {
51 if (p + sizeof(uint16) > end)
52 return 0;
53 if (consumed == 0) {
54 consumed = p - pos + sizeof(uint16);
55 if (!out)
56 return consumed; // If name is not stored, that's all we need.
57 }
58 seen+= sizeof(uint16);
mmenke 2011/12/02 00:53:55 nit: space before +=
szym 2011/12/05 23:06:28 Done.
59 // If seen the whole packet, then we must be in a loop.
60 if (seen > length_)
61 return 0;
62 uint16 offset;
63 ReadBigEndian<uint16>(p, &offset);
64 offset&= dns_protocol::kOffsetMask;
mmenke 2011/12/02 00:53:55 nit: space before &=
szym 2011/12/05 23:06:28 Done.
65 p = packet_ + offset;
66 if (p >= end)
67 return 0;
68 break;
69 }
70 case dns_protocol::kLabelDirect: {
71 uint8 label_len = *p;
72 ++p;
73 // Note: root domain (".") is NOT included.
74 if (label_len == 0) {
75 if (consumed == 0) {
76 consumed = p - pos;
77 } // else we set |consumed| before first jump
78 return consumed;
79 }
80 if (p + label_len >= end)
81 return 0; // Truncated or missing label.
82 if (out) {
83 if (!out->empty())
84 out->append(".");
85 out->append(p, label_len);
86 }
87 p += label_len;
88 seen+= 1 + label_len;
mmenke 2011/12/02 00:53:55 nit: Space before +=
szym 2011/12/05 23:06:28 Done.
89 break;
90 }
91 default:
92 // unhandled label type
93 return 0;
94 }
95 }
96 }
97
98 bool DnsRecordParser::ParseRecord(DnsResourceRecord* out) {
99 DCHECK(packet_);
100 size_t consumed = ParseName(cur_, &out->name);
101 if (!consumed)
102 return false;
103 BigEndianReader r(cur_ + consumed, packet_ + length_ - (cur_ + consumed));
104 uint16 rdlen;
105 if (r.U16(&out->type) &&
106 r.U16(&out->klass) &&
107 r.U32(&out->ttl) &&
108 r.U16(&rdlen) &&
109 r.Piece(&out->rdata, rdlen)) {
110 cur_ = r.ptr();
111 return true;
112 }
113 return false;
114 }
115
116 DnsResponse::DnsResponse()
117 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) {
118 }
119
120 DnsResponse::DnsResponse(const void* data,
121 size_t length,
122 size_t answer_offset)
123 : io_buffer_(new IOBufferWithSize(length)),
124 parser_(io_buffer_->data(), length, answer_offset) {
125 memcpy(io_buffer_->data(), data, length);
22 } 126 }
23 127
24 DnsResponse::~DnsResponse() { 128 DnsResponse::~DnsResponse() {
25 } 129 }
26 130
27 int DnsResponse::Parse(int nbytes, IPAddressList* ip_addresses) { 131 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) {
28 // Response includes query, it should be at least that size. 132 // Response includes query, it should be at least that size.
29 if (nbytes < query_->io_buffer()->size() || nbytes > kMaxResponseSize) 133 if (nbytes < query.io_buffer()->size() || nbytes > dns_protocol::kMaxUDPSize)
30 return ERR_DNS_MALFORMED_RESPONSE; 134 return false;
31 135
32 DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()), 136 // Match the query id.
33 io_buffer_->size()); 137 if (ntohs(header()->id) != query.id())
34 uint16 id; 138 return false;
35 if (!response.U16(&id) || id != query_->id()) // Make sure IDs match.
36 return ERR_DNS_MALFORMED_RESPONSE;
37 139
38 uint8 flags, rcode; 140 // Match question count.
39 if (!response.U8(&flags) || !response.U8(&rcode)) 141 if (ntohs(header()->qdcount) != 1)
40 return ERR_DNS_MALFORMED_RESPONSE; 142 return false;
41 143
42 if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?). 144 // Match the question section.
43 return ERR_DNS_SERVER_REQUIRES_TCP; 145 const size_t hdr_size = sizeof(dns_protocol::Header);
146 const base::StringPiece q = query.question();
147 if (q != base::StringPiece(io_buffer_->data() + hdr_size, q.size()))
148 return false;
44 149
45 rcode &= 0x0f; // 3 means NXDOMAIN, the rest means server failed. 150 // Construct the iterator
46 if (rcode && (rcode != 3)) 151 parser_ = DnsRecordParser(io_buffer_->data(), nbytes, hdr_size + q.size());
47 return ERR_DNS_SERVER_FAILED; 152 return true;
153 }
48 154
49 uint16 query_count, answer_count, authority_count, additional_count; 155 uint8 DnsResponse::flags0() const {
50 if (!response.U16(&query_count) || 156 return header()->flags[0];
51 !response.U16(&answer_count) || 157 }
52 !response.U16(&authority_count) ||
53 !response.U16(&additional_count)) {
54 return ERR_DNS_MALFORMED_RESPONSE;
55 }
56 158
57 if (query_count != 1) // Sent a single question, shouldn't have changed. 159 uint8 DnsResponse::flags1() const {
58 return ERR_DNS_MALFORMED_RESPONSE; 160 return header()->flags[1] & ~(dns_protocol::kRcodeMask);
161 }
59 162
60 base::StringPiece question; // Make sure question section is echoed back. 163 uint8 DnsResponse::rcode() const {
61 if (!response.Block(&question, query_->question_size()) || 164 return header()->flags[1] & dns_protocol::kRcodeMask;
62 memcmp(question.data(), query_->question_data(), 165 }
63 query_->question_size())) {
64 return ERR_DNS_MALFORMED_RESPONSE;
65 }
66 166
67 if (answer_count < 1) 167 int DnsResponse::answer_count() const {
68 return ERR_NAME_NOT_RESOLVED; 168 return ntohs(header()->ancount);
169 }
69 170
70 IPAddressList rdatas; 171 const dns_protocol::Header* DnsResponse::header() const {
71 while (answer_count--) { 172 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
72 uint32 ttl;
73 uint16 rdlength, qtype, qclass;
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
92 if (rdatas.empty())
93 return ERR_NAME_NOT_RESOLVED;
94
95 if (ip_addresses)
96 ip_addresses->swap(rdatas);
97 return OK;
98 } 173 }
99 174
100 } // namespace net 175 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698