Chromium Code Reviews| 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 <climits> | |
|
gene
2013/04/17 02:05:38
you probably don't need this any more
Noam Samuel
2013/04/17 20:39:08
Done.
| |
| 8 | |
| 7 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 8 #include "base/sys_byteorder.h" | 10 #include "base/sys_byteorder.h" |
| 9 #include "net/base/address_list.h" | 11 #include "net/base/address_list.h" |
| 10 #include "net/base/big_endian.h" | 12 #include "net/base/big_endian.h" |
| 11 #include "net/base/dns_util.h" | 13 #include "net/base/dns_util.h" |
| 12 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 14 #include "net/dns/dns_protocol.h" | 16 #include "net/dns/dns_protocol.h" |
| 15 #include "net/dns/dns_query.h" | 17 #include "net/dns/dns_query.h" |
| 16 | 18 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 return false; | 170 return false; |
| 169 } | 171 } |
| 170 | 172 |
| 171 // Construct the parser. | 173 // Construct the parser. |
| 172 parser_ = DnsRecordParser(io_buffer_->data(), | 174 parser_ = DnsRecordParser(io_buffer_->data(), |
| 173 nbytes, | 175 nbytes, |
| 174 hdr_size + question.size()); | 176 hdr_size + question.size()); |
| 175 return true; | 177 return true; |
| 176 } | 178 } |
| 177 | 179 |
| 180 bool DnsResponse::InitParse(int nbytes) { | |
| 181 if (nbytes >= io_buffer_->size()) | |
| 182 return false; | |
| 183 | |
| 184 // Construct the parser. | |
| 185 parser_ = DnsRecordParser(io_buffer_->data(), | |
|
gene
2013/04/17 02:04:02
nit: you can fit this in one line now
Noam Samuel
2013/04/17 20:39:08
Done.
| |
| 186 nbytes, | |
| 187 GetResponseOffset()); | |
|
gene
2013/04/17 02:04:02
do you want to fail InitParse if GetResponseOffset
Noam Samuel
2013/04/17 20:39:08
Yes.
| |
| 188 return true; | |
| 189 } | |
| 190 | |
| 178 bool DnsResponse::IsValid() const { | 191 bool DnsResponse::IsValid() const { |
| 179 return parser_.IsValid(); | 192 return parser_.IsValid(); |
| 180 } | 193 } |
| 181 | 194 |
| 182 uint16 DnsResponse::flags() const { | 195 uint16 DnsResponse::flags() const { |
| 183 DCHECK(parser_.IsValid()); | 196 DCHECK(parser_.IsValid()); |
| 184 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); | 197 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); |
| 185 } | 198 } |
| 186 | 199 |
| 187 uint8 DnsResponse::rcode() const { | 200 uint8 DnsResponse::rcode() const { |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 210 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); | 223 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); |
| 211 uint16 type; | 224 uint16 type; |
| 212 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); | 225 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); |
| 213 return type; | 226 return type; |
| 214 } | 227 } |
| 215 | 228 |
| 216 std::string DnsResponse::GetDottedName() const { | 229 std::string DnsResponse::GetDottedName() const { |
| 217 return DNSDomainToString(qname()); | 230 return DNSDomainToString(qname()); |
| 218 } | 231 } |
| 219 | 232 |
| 233 size_t DnsResponse::GetResponseOffset() const { | |
| 234 uint16 qdcount = base::NetToHost16(header()->qdcount); | |
| 235 const char* data = io_buffer_->data(); | |
| 236 size_t data_size = io_buffer_->size(); | |
| 237 size_t pos = sizeof(dns_protocol::Header); | |
| 238 | |
| 239 for (uint16 i = 0; i < qdcount; i++) { | |
| 240 while (pos < data_size && data[pos] != 0) { | |
| 241 unsigned txt_len = static_cast<unsigned>(data[pos]); | |
| 242 // Traversal and checks adapted from DNSDomainToString. | |
| 243 | |
| 244 // Handling pointers, which have both high bits set and an extra | |
| 245 // byte trailing them to indicate a location in the buffer. | |
| 246 if (txt_len >= 192) { | |
| 247 pos += 2; | |
| 248 continue; | |
| 249 } | |
| 250 | |
| 251 if (txt_len > 63) { | |
| 252 return 0; | |
| 253 } | |
| 254 | |
| 255 pos += txt_len + 1; | |
| 256 } | |
| 257 | |
| 258 pos += 5; // 1 for 0-length text, 2 for qtype, 2 for qclass | |
| 259 } | |
| 260 | |
| 261 if (pos > data_size) return 0; | |
| 262 | |
| 263 return pos; | |
| 264 } | |
| 265 | |
| 220 DnsRecordParser DnsResponse::Parser() const { | 266 DnsRecordParser DnsResponse::Parser() const { |
| 221 DCHECK(parser_.IsValid()); | 267 DCHECK(parser_.IsValid()); |
| 222 // Return a copy of the parser. | 268 // Return a copy of the parser. |
| 223 return parser_; | 269 return parser_; |
| 224 } | 270 } |
| 225 | 271 |
| 226 const dns_protocol::Header* DnsResponse::header() const { | 272 const dns_protocol::Header* DnsResponse::header() const { |
| 227 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); | 273 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data()); |
| 228 } | 274 } |
| 229 | 275 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 286 | 332 |
| 287 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 333 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. |
| 288 // If the response passed all the checks so far, then |expected_name| is it. | 334 // If the response passed all the checks so far, then |expected_name| is it. |
| 289 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 335 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
| 290 expected_name); | 336 expected_name); |
| 291 *ttl = base::TimeDelta::FromSeconds(ttl_sec); | 337 *ttl = base::TimeDelta::FromSeconds(ttl_sec); |
| 292 return DNS_PARSE_OK; | 338 return DNS_PARSE_OK; |
| 293 } | 339 } |
| 294 | 340 |
| 295 } // namespace net | 341 } // namespace net |
| OLD | NEW |