| 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/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 8 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
| 9 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| 10 #include "net/base/big_endian.h" | |
| 11 #include "net/base/dns_util.h" | 11 #include "net/base/dns_util.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 #include "net/dns/dns_protocol.h" | 14 #include "net/dns/dns_protocol.h" |
| 15 #include "net/dns/dns_query.h" | 15 #include "net/dns/dns_query.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 DnsResourceRecord::DnsResourceRecord() { | 19 DnsResourceRecord::DnsResourceRecord() { |
| 20 } | 20 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 if (consumed == 0) { | 66 if (consumed == 0) { |
| 67 consumed = p - pos + sizeof(uint16); | 67 consumed = p - pos + sizeof(uint16); |
| 68 if (!out) | 68 if (!out) |
| 69 return consumed; // If name is not stored, that's all we need. | 69 return consumed; // If name is not stored, that's all we need. |
| 70 } | 70 } |
| 71 seen += sizeof(uint16); | 71 seen += sizeof(uint16); |
| 72 // If seen the whole packet, then we must be in a loop. | 72 // If seen the whole packet, then we must be in a loop. |
| 73 if (seen > length_) | 73 if (seen > length_) |
| 74 return 0; | 74 return 0; |
| 75 uint16 offset; | 75 uint16 offset; |
| 76 ReadBigEndian<uint16>(p, &offset); | 76 base::ReadBigEndian<uint16>(p, &offset); |
| 77 offset &= dns_protocol::kOffsetMask; | 77 offset &= dns_protocol::kOffsetMask; |
| 78 p = packet_ + offset; | 78 p = packet_ + offset; |
| 79 if (p >= end) | 79 if (p >= end) |
| 80 return 0; | 80 return 0; |
| 81 break; | 81 break; |
| 82 } | 82 } |
| 83 case dns_protocol::kLabelDirect: { | 83 case dns_protocol::kLabelDirect: { |
| 84 uint8 label_len = *p; | 84 uint8 label_len = *p; |
| 85 ++p; | 85 ++p; |
| 86 // Note: root domain (".") is NOT included. | 86 // Note: root domain (".") is NOT included. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 106 return 0; | 106 return 0; |
| 107 } | 107 } |
| 108 } | 108 } |
| 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 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) && |
| 120 reader.ReadU16(&out->klass) && | 120 reader.ReadU16(&out->klass) && |
| 121 reader.ReadU32(&out->ttl) && | 121 reader.ReadU32(&out->ttl) && |
| 122 reader.ReadU16(&rdlen) && | 122 reader.ReadU16(&rdlen) && |
| 123 reader.ReadPiece(&out->rdata, rdlen)) { | 123 reader.ReadPiece(&out->rdata, rdlen)) { |
| 124 cur_ = reader.ptr(); | 124 cur_ = reader.ptr(); |
| 125 return true; | 125 return true; |
| 126 } | 126 } |
| 127 return false; | 127 return false; |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 const size_t hdr_size = sizeof(dns_protocol::Header); | 244 const size_t hdr_size = sizeof(dns_protocol::Header); |
| 245 const size_t qname_size = parser_.GetOffset() - 2 * sizeof(uint16) - hdr_size; | 245 const size_t qname_size = parser_.GetOffset() - 2 * sizeof(uint16) - hdr_size; |
| 246 return base::StringPiece(io_buffer_->data() + hdr_size, qname_size); | 246 return base::StringPiece(io_buffer_->data() + hdr_size, qname_size); |
| 247 } | 247 } |
| 248 | 248 |
| 249 uint16 DnsResponse::qtype() const { | 249 uint16 DnsResponse::qtype() const { |
| 250 DCHECK(parser_.IsValid()); | 250 DCHECK(parser_.IsValid()); |
| 251 // QTYPE starts where QNAME ends. | 251 // QTYPE starts where QNAME ends. |
| 252 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); | 252 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16); |
| 253 uint16 type; | 253 uint16 type; |
| 254 ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); | 254 base::ReadBigEndian<uint16>(io_buffer_->data() + type_offset, &type); |
| 255 return type; | 255 return type; |
| 256 } | 256 } |
| 257 | 257 |
| 258 std::string DnsResponse::GetDottedName() const { | 258 std::string DnsResponse::GetDottedName() const { |
| 259 return DNSDomainToString(qname()); | 259 return DNSDomainToString(qname()); |
| 260 } | 260 } |
| 261 | 261 |
| 262 DnsRecordParser DnsResponse::Parser() const { | 262 DnsRecordParser DnsResponse::Parser() const { |
| 263 DCHECK(parser_.IsValid()); | 263 DCHECK(parser_.IsValid()); |
| 264 // Return a copy of the parser. | 264 // Return a copy of the parser. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 | 328 |
| 329 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 329 // 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. | 330 // If the response passed all the checks so far, then |expected_name| is it. |
| 331 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 331 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
| 332 expected_name); | 332 expected_name); |
| 333 *ttl = base::TimeDelta::FromSeconds(ttl_sec); | 333 *ttl = base::TimeDelta::FromSeconds(ttl_sec); |
| 334 return DNS_PARSE_OK; | 334 return DNS_PARSE_OK; |
| 335 } | 335 } |
| 336 | 336 |
| 337 } // namespace net | 337 } // namespace net |
| OLD | NEW |