| 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/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
| 9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 10 #include "net/base/big_endian.h" | 10 #include "net/base/big_endian.h" |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 142 |
| 143 DnsResponse::~DnsResponse() { | 143 DnsResponse::~DnsResponse() { |
| 144 } | 144 } |
| 145 | 145 |
| 146 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) { | 146 bool DnsResponse::InitParse(int nbytes, const DnsQuery& query) { |
| 147 // Response includes query, it should be at least that size. | 147 // Response includes query, it should be at least that size. |
| 148 if (nbytes < query.io_buffer()->size() || nbytes > dns_protocol::kMaxUDPSize) | 148 if (nbytes < query.io_buffer()->size() || nbytes > dns_protocol::kMaxUDPSize) |
| 149 return false; | 149 return false; |
| 150 | 150 |
| 151 // Match the query id. | 151 // Match the query id. |
| 152 if (ntohs(header()->id) != query.id()) | 152 if (base::NetToHost16(header()->id) != query.id()) |
| 153 return false; | 153 return false; |
| 154 | 154 |
| 155 // Match question count. | 155 // Match question count. |
| 156 if (ntohs(header()->qdcount) != 1) | 156 if (base::NetToHost16(header()->qdcount) != 1) |
| 157 return false; | 157 return false; |
| 158 | 158 |
| 159 // Match the question section. | 159 // Match the question section. |
| 160 const size_t hdr_size = sizeof(dns_protocol::Header); | 160 const size_t hdr_size = sizeof(dns_protocol::Header); |
| 161 const base::StringPiece question = query.question(); | 161 const base::StringPiece question = query.question(); |
| 162 if (question != base::StringPiece(io_buffer_->data() + hdr_size, | 162 if (question != base::StringPiece(io_buffer_->data() + hdr_size, |
| 163 question.size())) { | 163 question.size())) { |
| 164 return false; | 164 return false; |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Construct the parser. | 167 // Construct the parser. |
| 168 parser_ = DnsRecordParser(io_buffer_->data(), | 168 parser_ = DnsRecordParser(io_buffer_->data(), |
| 169 nbytes, | 169 nbytes, |
| 170 hdr_size + question.size()); | 170 hdr_size + question.size()); |
| 171 return true; | 171 return true; |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool DnsResponse::IsValid() const { | 174 bool DnsResponse::IsValid() const { |
| 175 return parser_.IsValid(); | 175 return parser_.IsValid(); |
| 176 } | 176 } |
| 177 | 177 |
| 178 uint16 DnsResponse::flags() const { | 178 uint16 DnsResponse::flags() const { |
| 179 DCHECK(parser_.IsValid()); | 179 DCHECK(parser_.IsValid()); |
| 180 return ntohs(header()->flags) & ~(dns_protocol::kRcodeMask); | 180 return base::NetToHost16(header()->flags) & ~(dns_protocol::kRcodeMask); |
| 181 } | 181 } |
| 182 | 182 |
| 183 uint8 DnsResponse::rcode() const { | 183 uint8 DnsResponse::rcode() const { |
| 184 DCHECK(parser_.IsValid()); | 184 DCHECK(parser_.IsValid()); |
| 185 return ntohs(header()->flags) & dns_protocol::kRcodeMask; | 185 return base::NetToHost16(header()->flags) & dns_protocol::kRcodeMask; |
| 186 } | 186 } |
| 187 | 187 |
| 188 unsigned DnsResponse::answer_count() const { | 188 unsigned DnsResponse::answer_count() const { |
| 189 DCHECK(parser_.IsValid()); | 189 DCHECK(parser_.IsValid()); |
| 190 return ntohs(header()->ancount); | 190 return base::NetToHost16(header()->ancount); |
| 191 } | 191 } |
| 192 | 192 |
| 193 base::StringPiece DnsResponse::qname() const { | 193 base::StringPiece DnsResponse::qname() const { |
| 194 DCHECK(parser_.IsValid()); | 194 DCHECK(parser_.IsValid()); |
| 195 // The response is HEADER QNAME QTYPE QCLASS ANSWER. | 195 // The response is HEADER QNAME QTYPE QCLASS ANSWER. |
| 196 // |parser_| is positioned at the beginning of ANSWER, so the end of QNAME is | 196 // |parser_| is positioned at the beginning of ANSWER, so the end of QNAME is |
| 197 // two uint16s before it. | 197 // two uint16s before it. |
| 198 const size_t hdr_size = sizeof(dns_protocol::Header); | 198 const size_t hdr_size = sizeof(dns_protocol::Header); |
| 199 const size_t qname_size = parser_.GetOffset() - 2 * sizeof(uint16) - hdr_size; | 199 const size_t qname_size = parser_.GetOffset() - 2 * sizeof(uint16) - hdr_size; |
| 200 return base::StringPiece(io_buffer_->data() + hdr_size, qname_size); | 200 return base::StringPiece(io_buffer_->data() + hdr_size, qname_size); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 285 |
| 286 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. | 286 // getcanonname in eglibc returns the first owner name of an A or AAAA RR. |
| 287 // If the response passed all the checks so far, then |expected_name| is it. | 287 // If the response passed all the checks so far, then |expected_name| is it. |
| 288 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, | 288 *addr_list = AddressList::CreateFromIPAddressList(ip_addresses, |
| 289 expected_name); | 289 expected_name); |
| 290 *ttl = base::TimeDelta::FromSeconds(std::min(cname_ttl_sec, addr_ttl_sec)); | 290 *ttl = base::TimeDelta::FromSeconds(std::min(cname_ttl_sec, addr_ttl_sec)); |
| 291 return DNS_SUCCESS; | 291 return DNS_SUCCESS; |
| 292 } | 292 } |
| 293 | 293 |
| 294 } // namespace net | 294 } // namespace net |
| OLD | NEW |