| 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_query.h" | 5 #include "net/dns/dns_query.h" |
| 6 | 6 |
| 7 #include "base/big_endian.h" | 7 #include "base/big_endian.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
| 10 #include "net/base/io_buffer.h" | 10 #include "net/base/io_buffer.h" |
| 11 #include "net/dns/dns_protocol.h" | 11 #include "net/dns/dns_protocol.h" |
| 12 #include "net/dns/dns_util.h" | 12 #include "net/dns/dns_util.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const size_t kHeaderSize = sizeof(dns_protocol::Header); | 18 const size_t kHeaderSize2 = sizeof(dns_protocol::Header); |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 // DNS query consists of a 12-byte header followed by a question section. | 22 // DNS query consists of a 12-byte header followed by a question section. |
| 23 // For details, see RFC 1035 section 4.1.1. This header template sets RD | 23 // For details, see RFC 1035 section 4.1.1. This header template sets RD |
| 24 // bit, which directs the name server to pursue query recursively, and sets | 24 // bit, which directs the name server to pursue query recursively, and sets |
| 25 // the QDCOUNT to 1, meaning the question section has a single entry. | 25 // the QDCOUNT to 1, meaning the question section has a single entry. |
| 26 DnsQuery::DnsQuery(uint16_t id, const base::StringPiece& qname, uint16_t qtype) | 26 DnsQuery::DnsQuery(uint16_t id, const base::StringPiece& qname, uint16_t qtype) |
| 27 : qname_size_(qname.size()), | 27 : qname_size_(qname.size()), |
| 28 io_buffer_(new IOBufferWithSize(kHeaderSize + question_size())), | 28 io_buffer_(new IOBufferWithSize(kHeaderSize2 + question_size())), |
| 29 header_(reinterpret_cast<dns_protocol::Header*>(io_buffer_->data())) { | 29 header_(reinterpret_cast<dns_protocol::Header*>(io_buffer_->data())) { |
| 30 DCHECK(!DNSDomainToString(qname).empty()); | 30 DCHECK(!DNSDomainToString(qname).empty()); |
| 31 *header_ = {}; | 31 *header_ = {}; |
| 32 header_->id = base::HostToNet16(id); | 32 header_->id = base::HostToNet16(id); |
| 33 header_->flags = base::HostToNet16(dns_protocol::kFlagRD); | 33 header_->flags = base::HostToNet16(dns_protocol::kFlagRD); |
| 34 header_->qdcount = base::HostToNet16(1); | 34 header_->qdcount = base::HostToNet16(1); |
| 35 | 35 |
| 36 // Write question section after the header. | 36 // Write question section after the header. |
| 37 base::BigEndianWriter writer(io_buffer_->data() + kHeaderSize, | 37 base::BigEndianWriter writer(io_buffer_->data() + kHeaderSize2, |
| 38 question_size()); | 38 question_size()); |
| 39 writer.WriteBytes(qname.data(), qname.size()); | 39 writer.WriteBytes(qname.data(), qname.size()); |
| 40 writer.WriteU16(qtype); | 40 writer.WriteU16(qtype); |
| 41 writer.WriteU16(dns_protocol::kClassIN); | 41 writer.WriteU16(dns_protocol::kClassIN); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DnsQuery::~DnsQuery() { | 44 DnsQuery::~DnsQuery() { |
| 45 } | 45 } |
| 46 | 46 |
| 47 std::unique_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const { | 47 std::unique_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const { |
| 48 return base::WrapUnique(new DnsQuery(*this, id)); | 48 return base::WrapUnique(new DnsQuery(*this, id)); |
| 49 } | 49 } |
| 50 | 50 |
| 51 uint16_t DnsQuery::id() const { | 51 uint16_t DnsQuery::id() const { |
| 52 return base::NetToHost16(header_->id); | 52 return base::NetToHost16(header_->id); |
| 53 } | 53 } |
| 54 | 54 |
| 55 base::StringPiece DnsQuery::qname() const { | 55 base::StringPiece DnsQuery::qname() const { |
| 56 return base::StringPiece(io_buffer_->data() + kHeaderSize, qname_size_); | 56 return base::StringPiece(io_buffer_->data() + kHeaderSize2, qname_size_); |
| 57 } | 57 } |
| 58 | 58 |
| 59 uint16_t DnsQuery::qtype() const { | 59 uint16_t DnsQuery::qtype() const { |
| 60 uint16_t type; | 60 uint16_t type; |
| 61 base::ReadBigEndian<uint16_t>(io_buffer_->data() + kHeaderSize + qname_size_, | 61 base::ReadBigEndian<uint16_t>(io_buffer_->data() + kHeaderSize2 + qname_size_, |
| 62 &type); | 62 &type); |
| 63 return type; | 63 return type; |
| 64 } | 64 } |
| 65 | 65 |
| 66 base::StringPiece DnsQuery::question() const { | 66 base::StringPiece DnsQuery::question() const { |
| 67 return base::StringPiece(io_buffer_->data() + kHeaderSize, question_size()); | 67 return base::StringPiece(io_buffer_->data() + kHeaderSize2, question_size()); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void DnsQuery::set_flags(uint16_t flags) { | 70 void DnsQuery::set_flags(uint16_t flags) { |
| 71 header_->flags = flags; | 71 header_->flags = flags; |
| 72 } | 72 } |
| 73 | 73 |
| 74 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) { | 74 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) { |
| 75 qname_size_ = orig.qname_size_; | 75 qname_size_ = orig.qname_size_; |
| 76 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); | 76 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); |
| 77 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), | 77 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), |
| 78 io_buffer_.get()->size()); | 78 io_buffer_.get()->size()); |
| 79 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 79 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); |
| 80 header_->id = base::HostToNet16(id); | 80 header_->id = base::HostToNet16(id); |
| 81 } | 81 } |
| 82 | 82 |
| 83 } // namespace net | 83 } // namespace net |
| OLD | NEW |