OLD | NEW |
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/big_endian.h" | 7 #include "net/base/dns_util.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" | |
12 #include "net/dns/dns_query.h" | 10 #include "net/dns/dns_query.h" |
13 | 11 |
14 namespace net { | 12 namespace net { |
15 | 13 |
16 DnsRecordParser::DnsRecordParser() : packet_(NULL), length_(0), cur_(0) { | 14 // RFC 1035, section 4.2.1: Messages carried by UDP are restricted to 512 |
17 } | 15 // bytes (not counting the IP nor UDP headers). |
| 16 static const int kMaxResponseSize = 512; |
18 | 17 |
19 DnsRecordParser::DnsRecordParser(const void* packet, | 18 DnsResponse::DnsResponse(DnsQuery* query) |
20 size_t length, | 19 : query_(query), |
21 size_t offset) | 20 io_buffer_(new IOBufferWithSize(kMaxResponseSize + 1)) { |
22 : packet_(reinterpret_cast<const char*>(packet)), | 21 DCHECK(query_); |
23 length_(length), | |
24 cur_(packet_ + offset) { | |
25 DCHECK_LE(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_LE(packet_, pos); | |
32 DCHECK_LE(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); | |
130 } | 22 } |
131 | 23 |
132 DnsResponse::~DnsResponse() { | 24 DnsResponse::~DnsResponse() { |
133 } | 25 } |
134 | 26 |
135 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) { | 27 int DnsResponse::Parse(int nbytes, IPAddressList* ip_addresses) { |
136 // Response includes query, it should be at least that size. | 28 // Response includes query, it should be at least that size. |
137 if (nbytes < query.io_buffer()->size() || nbytes > dns_protocol::kMaxUDPSize) | 29 if (nbytes < query_->io_buffer()->size() || nbytes > kMaxResponseSize) |
138 return false; | 30 return ERR_DNS_MALFORMED_RESPONSE; |
139 | 31 |
140 // Match the query id. | 32 DnsResponseBuffer response(reinterpret_cast<uint8*>(io_buffer_->data()), |
141 if (ntohs(header()->id) != query.id()) | 33 io_buffer_->size()); |
142 return false; | 34 uint16 id; |
| 35 if (!response.U16(&id) || id != query_->id()) // Make sure IDs match. |
| 36 return ERR_DNS_MALFORMED_RESPONSE; |
143 | 37 |
144 // Match question count. | 38 uint8 flags, rcode; |
145 if (ntohs(header()->qdcount) != 1) | 39 if (!response.U8(&flags) || !response.U8(&rcode)) |
146 return false; | 40 return ERR_DNS_MALFORMED_RESPONSE; |
147 | 41 |
148 // Match the question section. | 42 if (flags & 2) // TC is set -- server wants TCP, we don't support it (yet?). |
149 const size_t hdr_size = sizeof(dns_protocol::Header); | 43 return ERR_DNS_SERVER_REQUIRES_TCP; |
150 const base::StringPiece question = query.question(); | 44 |
151 if (question != base::StringPiece(io_buffer_->data() + hdr_size, | 45 rcode &= 0x0f; // 3 means NXDOMAIN, the rest means server failed. |
152 question.size())) { | 46 if (rcode && (rcode != 3)) |
153 return false; | 47 return ERR_DNS_SERVER_FAILED; |
| 48 |
| 49 uint16 query_count, answer_count, authority_count, additional_count; |
| 50 if (!response.U16(&query_count) || |
| 51 !response.U16(&answer_count) || |
| 52 !response.U16(&authority_count) || |
| 53 !response.U16(&additional_count)) { |
| 54 return ERR_DNS_MALFORMED_RESPONSE; |
154 } | 55 } |
155 | 56 |
156 // Construct the parser. | 57 if (query_count != 1) // Sent a single question, shouldn't have changed. |
157 parser_ = DnsRecordParser(io_buffer_->data(), | 58 return ERR_DNS_MALFORMED_RESPONSE; |
158 nbytes, | |
159 hdr_size + question.size()); | |
160 return true; | |
161 } | |
162 | 59 |
163 uint8 DnsResponse::flags0() const { | 60 base::StringPiece question; // Make sure question section is echoed back. |
164 return header()->flags[0]; | 61 if (!response.Block(&question, query_->question_size()) || |
165 } | 62 memcmp(question.data(), query_->question_data(), |
| 63 query_->question_size())) { |
| 64 return ERR_DNS_MALFORMED_RESPONSE; |
| 65 } |
166 | 66 |
167 uint8 DnsResponse::flags1() const { | 67 if (answer_count < 1) |
168 return header()->flags[1] & ~(dns_protocol::kRcodeMask); | 68 return ERR_NAME_NOT_RESOLVED; |
169 } | |
170 | 69 |
171 uint8 DnsResponse::rcode() const { | 70 IPAddressList rdatas; |
172 return header()->flags[1] & dns_protocol::kRcodeMask; | 71 while (answer_count--) { |
173 } | 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 } |
174 | 91 |
175 int DnsResponse::answer_count() const { | 92 if (rdatas.empty()) |
176 return ntohs(header()->ancount); | 93 return ERR_NAME_NOT_RESOLVED; |
177 } | |
178 | 94 |
179 DnsRecordParser DnsResponse::Parser() const { | 95 if (ip_addresses) |
180 DCHECK(parser_.IsValid()); | 96 ip_addresses->swap(rdatas); |
181 return parser_; | 97 return OK; |
182 } | |
183 | |
184 const dns_protocol::Header* DnsResponse::header() const { | |
185 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); | |
186 } | 98 } |
187 | 99 |
188 } // namespace net | 100 } // namespace net |
OLD | NEW |