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 { |
| 17 |
| 18 const size_t kHeaderSize = sizeof(dns_protocol::Header); |
| 19 |
| 20 } // namespace |
| 21 |
16 // 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. |
17 // 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 |
18 // 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 |
19 // 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. |
20 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) |
21 : qname_size_(qname.size()), | 27 : qname_size_(qname.size()), |
22 io_buffer_( | 28 io_buffer_(new IOBufferWithSize(kHeaderSize + question_size())), |
23 new IOBufferWithSize(sizeof(dns_protocol::Header) + question_size())), | |
24 header_(reinterpret_cast<dns_protocol::Header*>(io_buffer_->data())) { | 29 header_(reinterpret_cast<dns_protocol::Header*>(io_buffer_->data())) { |
25 DCHECK(!DNSDomainToString(qname).empty()); | 30 DCHECK(!DNSDomainToString(qname).empty()); |
26 *header_ = {}; | 31 *header_ = {}; |
27 header_->id = base::HostToNet16(id); | 32 header_->id = base::HostToNet16(id); |
28 header_->flags = base::HostToNet16(dns_protocol::kFlagRD); | 33 header_->flags = base::HostToNet16(dns_protocol::kFlagRD); |
29 header_->qdcount = base::HostToNet16(1); | 34 header_->qdcount = base::HostToNet16(1); |
30 | 35 |
31 // Write question section after the header. | 36 // Write question section after the header. |
32 base::BigEndianWriter writer( | 37 base::BigEndianWriter writer(io_buffer_->data() + kHeaderSize, |
33 io_buffer_->data() + sizeof(dns_protocol::Header), question_size()); | 38 question_size()); |
34 writer.WriteBytes(qname.data(), qname.size()); | 39 writer.WriteBytes(qname.data(), qname.size()); |
35 writer.WriteU16(qtype); | 40 writer.WriteU16(qtype); |
36 writer.WriteU16(dns_protocol::kClassIN); | 41 writer.WriteU16(dns_protocol::kClassIN); |
37 } | 42 } |
38 | 43 |
39 DnsQuery::~DnsQuery() { | 44 DnsQuery::~DnsQuery() { |
40 } | 45 } |
41 | 46 |
42 std::unique_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const { | 47 std::unique_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const { |
43 return base::WrapUnique(new DnsQuery(*this, id)); | 48 return base::WrapUnique(new DnsQuery(*this, id)); |
44 } | 49 } |
45 | 50 |
46 uint16_t DnsQuery::id() const { | 51 uint16_t DnsQuery::id() const { |
47 return base::NetToHost16(header_->id); | 52 return base::NetToHost16(header_->id); |
48 } | 53 } |
49 | 54 |
50 base::StringPiece DnsQuery::qname() const { | 55 base::StringPiece DnsQuery::qname() const { |
51 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 56 return base::StringPiece(io_buffer_->data() + kHeaderSize, qname_size_); |
52 qname_size_); | |
53 } | 57 } |
54 | 58 |
55 uint16_t DnsQuery::qtype() const { | 59 uint16_t DnsQuery::qtype() const { |
56 uint16_t type; | 60 uint16_t type; |
57 base::ReadBigEndian<uint16_t>( | 61 base::ReadBigEndian<uint16_t>(io_buffer_->data() + kHeaderSize + qname_size_, |
58 io_buffer_->data() + sizeof(dns_protocol::Header) + qname_size_, &type); | 62 &type); |
59 return type; | 63 return type; |
60 } | 64 } |
61 | 65 |
62 base::StringPiece DnsQuery::question() const { | 66 base::StringPiece DnsQuery::question() const { |
63 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 67 return base::StringPiece(io_buffer_->data() + kHeaderSize, question_size()); |
64 question_size()); | |
65 } | 68 } |
66 | 69 |
67 void DnsQuery::set_flags(uint16_t flags) { | 70 void DnsQuery::set_flags(uint16_t flags) { |
68 header_->flags = flags; | 71 header_->flags = flags; |
69 } | 72 } |
70 | 73 |
71 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) { | 74 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) { |
72 qname_size_ = orig.qname_size_; | 75 qname_size_ = orig.qname_size_; |
73 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); | 76 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); |
74 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), | 77 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), |
75 io_buffer_.get()->size()); | 78 io_buffer_.get()->size()); |
76 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 79 header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); |
77 header_->id = base::HostToNet16(id); | 80 header_->id = base::HostToNet16(id); |
78 } | 81 } |
79 | 82 |
80 } // namespace net | 83 } // namespace net |
OLD | NEW |