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/big_endian.h" | 7 #include "base/big_endian.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 } | 109 } |
110 | 110 |
111 bool DnsRecordParser::ReadRecord(DnsResourceRecord* out) { | 111 bool DnsRecordParser::ReadRecord(DnsResourceRecord* out) { |
112 DCHECK(packet_); | 112 DCHECK(packet_); |
113 size_t consumed = ReadName(cur_, &out->name); | 113 size_t consumed = ReadName(cur_, &out->name); |
114 if (!consumed) | 114 if (!consumed) |
115 return false; | 115 return false; |
116 base::BigEndianReader reader(cur_ + consumed, | 116 base::BigEndianReader reader(cur_ + consumed, |
117 packet_ + length_ - (cur_ + consumed)); | 117 packet_ + length_ - (cur_ + consumed)); |
118 uint16 rdlen; | 118 uint16 rdlen; |
119 if (reader.ReadU16(&out->type) && | 119 if (reader.ReadU16(&out->type) && reader.ReadU16(&out->klass) && |
120 reader.ReadU16(&out->klass) && | 120 reader.ReadU32(&out->ttl) && reader.ReadU16(&rdlen) && |
121 reader.ReadU32(&out->ttl) && | |
122 reader.ReadU16(&rdlen) && | |
123 reader.ReadPiece(&out->rdata, rdlen)) { | 121 reader.ReadPiece(&out->rdata, rdlen)) { |
124 cur_ = reader.ptr(); | 122 cur_ = reader.ptr(); |
125 return true; | 123 return true; |
126 } | 124 } |
127 return false; | 125 return false; |
128 } | 126 } |
129 | 127 |
130 bool DnsRecordParser::SkipQuestion() { | 128 bool DnsRecordParser::SkipQuestion() { |
131 size_t consumed = ReadName(cur_, NULL); | 129 size_t consumed = ReadName(cur_, NULL); |
132 if (!consumed) | 130 if (!consumed) |
133 return false; | 131 return false; |
134 | 132 |
135 const char* next = cur_ + consumed + 2 * sizeof(uint16); // QTYPE + QCLASS | 133 const char* next = cur_ + consumed + 2 * sizeof(uint16); // QTYPE + QCLASS |
136 if (next > packet_ + length_) | 134 if (next > packet_ + length_) |
137 return false; | 135 return false; |
138 | 136 |
139 cur_ = next; | 137 cur_ = next; |
140 | 138 |
141 return true; | 139 return true; |
142 } | 140 } |
143 | 141 |
144 DnsResponse::DnsResponse() | 142 DnsResponse::DnsResponse() |
145 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) { | 143 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) { |
146 } | 144 } |
147 | 145 |
148 DnsResponse::DnsResponse(size_t length) | 146 DnsResponse::DnsResponse(size_t length) |
149 : io_buffer_(new IOBufferWithSize(length)) { | 147 : io_buffer_(new IOBufferWithSize(length)) { |
150 } | 148 } |
151 | 149 |
152 DnsResponse::DnsResponse(const void* data, | 150 DnsResponse::DnsResponse(const void* data, size_t length, size_t answer_offset) |
153 size_t length, | |
154 size_t answer_offset) | |
155 : io_buffer_(new IOBufferWithSize(length)), | 151 : io_buffer_(new IOBufferWithSize(length)), |
156 parser_(io_buffer_->data(), length, answer_offset) { | 152 parser_(io_buffer_->data(), length, answer_offset) { |
157 DCHECK(data); | 153 DCHECK(data); |
158 memcpy(io_buffer_->data(), data, length); | 154 memcpy(io_buffer_->data(), data, length); |
159 } | 155 } |
160 | 156 |
161 DnsResponse::~DnsResponse() { | 157 DnsResponse::~DnsResponse() { |
162 } | 158 } |
163 | 159 |
164 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) { | 160 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) { |
165 DCHECK_GE(nbytes, 0); | 161 DCHECK_GE(nbytes, 0); |
166 // Response includes query, it should be at least that size. | 162 // Response includes query, it should be at least that size. |
167 if (nbytes < query.io_buffer()->size() || nbytes >= io_buffer_->size()) | 163 if (nbytes < query.io_buffer()->size() || nbytes >= io_buffer_->size()) |
168 return false; | 164 return false; |
169 | 165 |
170 // Match the query id. | 166 // Match the query id. |
171 if (base::NetToHost16(header()->id) != query.id()) | 167 if (base::NetToHost16(header()->id) != query.id()) |
172 return false; | 168 return false; |
173 | 169 |
174 // Match question count. | 170 // Match question count. |
175 if (base::NetToHost16(header()->qdcount) != 1) | 171 if (base::NetToHost16(header()->qdcount) != 1) |
176 return false; | 172 return false; |
177 | 173 |
178 // Match the question section. | 174 // Match the question section. |
179 const size_t hdr_size = sizeof(dns_protocol::Header); | 175 const size_t hdr_size = sizeof(dns_protocol::Header); |
180 const base::StringPiece question = query.question(); | 176 const base::StringPiece question = query.question(); |
181 if (question != base::StringPiece(io_buffer_->data() + hdr_size, | 177 if (question != |
182 question.size())) { | 178 base::StringPiece(io_buffer_->data() + hdr_size, question.size())) { |
183 return false; | 179 return false; |
184 } | 180 } |
185 | 181 |
186 // Construct the parser. | 182 // Construct the parser. |
187 parser_ = DnsRecordParser(io_buffer_->data(), | 183 parser_ = |
188 nbytes, | 184 DnsRecordParser(io_buffer_->data(), nbytes, hdr_size + question.size()); |
189 hdr_size + question.size()); | |
190 return true; | 185 return true; |
191 } | 186 } |
192 | 187 |
193 bool DnsResponse::InitParseWithoutQuery(int nbytes) { | 188 bool DnsResponse::InitParseWithoutQuery(int nbytes) { |
194 DCHECK_GE(nbytes, 0); | 189 DCHECK_GE(nbytes, 0); |
195 | 190 |
196 size_t hdr_size = sizeof(dns_protocol::Header); | 191 size_t hdr_size = sizeof(dns_protocol::Header); |
197 | 192 |
198 if (nbytes < static_cast<int>(hdr_size) || nbytes >= io_buffer_->size()) | 193 if (nbytes < static_cast<int>(hdr_size) || nbytes >= io_buffer_->size()) |
199 return false; | 194 return false; |
200 | 195 |
201 parser_ = DnsRecordParser( | 196 parser_ = DnsRecordParser(io_buffer_->data(), nbytes, hdr_size); |
202 io_buffer_->data(), nbytes, hdr_size); | |
203 | 197 |
204 unsigned qdcount = base::NetToHost16(header()->qdcount); | 198 unsigned qdcount = base::NetToHost16(header()->qdcount); |
205 for (unsigned i = 0; i < qdcount; ++i) { | 199 for (unsigned i = 0; i < qdcount; ++i) { |
206 if (!parser_.SkipQuestion()) { | 200 if (!parser_.SkipQuestion()) { |
207 parser_ = DnsRecordParser(); // Make parser invalid again. | 201 parser_ = DnsRecordParser(); // Make parser invalid again. |
208 return false; | 202 return false; |
209 } | 203 } |
210 } | 204 } |
211 | 205 |
212 return true; | 206 return true; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 // we can always fall back to the system getaddrinfo. | 274 // we can always fall back to the system getaddrinfo. |
281 | 275 |
282 // Expected owner of record. No trailing dot. | 276 // Expected owner of record. No trailing dot. |
283 std::string expected_name = GetDottedName(); | 277 std::string expected_name = GetDottedName(); |
284 | 278 |
285 uint16 expected_type = qtype(); | 279 uint16 expected_type = qtype(); |
286 DCHECK(expected_type == dns_protocol::kTypeA || | 280 DCHECK(expected_type == dns_protocol::kTypeA || |
287 expected_type == dns_protocol::kTypeAAAA); | 281 expected_type == dns_protocol::kTypeAAAA); |
288 | 282 |
289 size_t expected_size = (expected_type == dns_protocol::kTypeAAAA) | 283 size_t expected_size = (expected_type == dns_protocol::kTypeAAAA) |
290 ? kIPv6AddressSize : kIPv4AddressSize; | 284 ? kIPv6AddressSize |
| 285 : kIPv4AddressSize; |
291 | 286 |
292 uint32 ttl_sec = kuint32max; | 287 uint32 ttl_sec = kuint32max; |
293 IPAddressList ip_addresses; | 288 IPAddressList ip_addresses; |
294 DnsRecordParser parser = Parser(); | 289 DnsRecordParser parser = Parser(); |
295 DnsResourceRecord record; | 290 DnsResourceRecord record; |
296 unsigned ancount = answer_count(); | 291 unsigned ancount = answer_count(); |
297 for (unsigned i = 0; i < ancount; ++i) { | 292 for (unsigned i = 0; i < ancount; ++i) { |
298 if (!parser.ReadRecord(&record)) | 293 if (!parser.ReadRecord(&record)) |
299 return DNS_MALFORMED_RESPONSE; | 294 return DNS_MALFORMED_RESPONSE; |
300 | 295 |
(...skipping 11 matching lines...) Expand all Loading... |
312 | 307 |
313 ttl_sec = std::min(ttl_sec, record.ttl); | 308 ttl_sec = std::min(ttl_sec, record.ttl); |
314 } else if (record.type == expected_type) { | 309 } else if (record.type == expected_type) { |
315 if (record.rdata.size() != expected_size) | 310 if (record.rdata.size() != expected_size) |
316 return DNS_SIZE_MISMATCH; | 311 return DNS_SIZE_MISMATCH; |
317 | 312 |
318 if (base::strcasecmp(record.name.c_str(), expected_name.c_str()) != 0) | 313 if (base::strcasecmp(record.name.c_str(), expected_name.c_str()) != 0) |
319 return DNS_NAME_MISMATCH; | 314 return DNS_NAME_MISMATCH; |
320 | 315 |
321 ttl_sec = std::min(ttl_sec, record.ttl); | 316 ttl_sec = std::min(ttl_sec, record.ttl); |
322 ip_addresses.push_back(IPAddressNumber(record.rdata.begin(), | 317 ip_addresses.push_back( |
323 record.rdata.end())); | 318 IPAddressNumber(record.rdata.begin(), record.rdata.end())); |
324 } | 319 } |
325 } | 320 } |
326 | 321 |
327 // TODO(szym): Extract TTL for NODATA results. http://crbug.com/115051 | 322 // TODO(szym): Extract TTL for NODATA results. http://crbug.com/115051 |
328 | 323 |
329 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 324 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. |
330 // If the response passed all the checks so far, then |expected_name| is it. | 325 // If the response passed all the checks so far, then |expected_name| is it. |
331 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 326 *addr_list = |
332 expected_name); | 327 AddressList::CreateFromIPAddressList(ip_addresses, expected_name); |
333 *ttl = base::TimeDelta::FromSeconds(ttl_sec); | 328 *ttl = base::TimeDelta::FromSeconds(ttl_sec); |
334 return DNS_PARSE_OK; | 329 return DNS_PARSE_OK; |
335 } | 330 } |
336 | 331 |
337 } // namespace net | 332 } // namespace net |
OLD | NEW |