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_query.h" | 5 #include "net/dns/dns_query.h" |
| 6 | 6 |
| 7 #include <limits> | |
| 8 | |
| 9 #include "base/big_endian.h" | 7 #include "base/big_endian.h" |
| 10 #include "base/sys_byteorder.h" | 8 #include "base/sys_byteorder.h" |
| 11 #include "net/base/io_buffer.h" | 9 #include "net/base/io_buffer.h" |
| 12 #include "net/dns/dns_protocol.h" | 10 #include "net/dns/dns_protocol.h" |
| 13 #include "net/dns/dns_util.h" | 11 #include "net/dns/dns_util.h" |
| 14 | 12 |
| 15 namespace net { | 13 namespace net { |
| 16 | 14 |
| 17 // DNS query consists of a 12-byte header followed by a question section. | 15 // DNS query consists of a 12-byte header followed by a question section. |
| 18 // For details, see RFC 1035 section 4.1.1. This header template sets RD | 16 // For details, see RFC 1035 section 4.1.1. This header template sets RD |
| 19 // bit, which directs the name server to pursue query recursively, and sets | 17 // bit, which directs the name server to pursue query recursively, and sets |
| 20 // the QDCOUNT to 1, meaning the question section has a single entry. | 18 // the QDCOUNT to 1, meaning the question section has a single entry. |
| 21 DnsQuery::DnsQuery(uint16_t id, const base::StringPiece& qname, uint16_t qtype) | 19 DnsQuery::DnsQuery(uint16_t id, const base::StringPiece& qname, uint16_t qtype) |
| 22 : qname_size_(qname.size()) { | 20 : qname_size_(qname.size()) { |
| 23 DCHECK(!DNSDomainToString(qname).empty()); | 21 DCHECK(!DNSDomainToString(qname).empty()); |
| 24 // QNAME + QTYPE + QCLASS | 22 // QNAME + QTYPE + QCLASS |
| 25 size_t question_size = qname_size_ + sizeof(uint16_t) + sizeof(uint16_t); | 23 size_t question_size = qname_size_ + sizeof(uint16_t) + sizeof(uint16_t); |
| 26 io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) + | 24 io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) + |
| 27 question_size); | 25 question_size); |
| 28 dns_protocol::Header* header = | 26 memset(header(), 0, sizeof(dns_protocol::Header)); |
|
pauljensen
2015/12/29 12:38:02
I don't like how the sizeof() uses a length differ
tfarina
2015/12/29 13:57:43
I don't see how this matters much. I don't think c
pauljensen
2015/12/29 15:06:22
You're making this code more fragile and susceptib
tfarina
2015/12/29 18:48:01
Sorry, I'm not getting your point Paul. The code h
pauljensen
2015/12/29 19:42:37
I dislike memset() because it's very type-unsafe.
tfarina
2015/12/29 19:57:07
Sorry, someone changing one but not the other? How
| |
| 29 reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | 27 header()->id = base::HostToNet16(id); |
| 30 memset(header, 0, sizeof(dns_protocol::Header)); | 28 header()->flags = base::HostToNet16(dns_protocol::kFlagRD); |
| 31 header->id = base::HostToNet16(id); | 29 header()->qdcount = base::HostToNet16(1); |
| 32 header->flags = base::HostToNet16(dns_protocol::kFlagRD); | |
| 33 header->qdcount = base::HostToNet16(1); | |
| 34 | 30 |
| 35 // Write question section after the header. | 31 // Write question section after the header. |
| 36 base::BigEndianWriter writer(reinterpret_cast<char*>(header + 1), | 32 base::BigEndianWriter writer(reinterpret_cast<char*>(header() + 1), |
|
pauljensen
2015/12/29 12:38:02
I'd much rather all this was rewritten in a more t
tfarina
2015/12/29 13:57:43
Maybe? I don't think this belongs to this change.
pauljensen
2015/12/29 15:06:22
Acknowledged.
| |
| 37 question_size); | 33 question_size); |
| 38 writer.WriteBytes(qname.data(), qname.size()); | 34 writer.WriteBytes(qname.data(), qname.size()); |
| 39 writer.WriteU16(qtype); | 35 writer.WriteU16(qtype); |
| 40 writer.WriteU16(dns_protocol::kClassIN); | 36 writer.WriteU16(dns_protocol::kClassIN); |
| 41 } | 37 } |
| 42 | 38 |
| 43 DnsQuery::~DnsQuery() { | 39 DnsQuery::~DnsQuery() { |
| 44 } | 40 } |
| 45 | 41 |
| 46 scoped_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const { | 42 scoped_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 63 base::ReadBigEndian<uint16_t>( | 59 base::ReadBigEndian<uint16_t>( |
| 64 io_buffer_->data() + sizeof(dns_protocol::Header) + qname_size_, &type); | 60 io_buffer_->data() + sizeof(dns_protocol::Header) + qname_size_, &type); |
| 65 return type; | 61 return type; |
| 66 } | 62 } |
| 67 | 63 |
| 68 base::StringPiece DnsQuery::question() const { | 64 base::StringPiece DnsQuery::question() const { |
| 69 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), | 65 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), |
| 70 qname_size_ + sizeof(uint16_t) + sizeof(uint16_t)); | 66 qname_size_ + sizeof(uint16_t) + sizeof(uint16_t)); |
| 71 } | 67 } |
| 72 | 68 |
| 69 void DnsQuery::set_flags(uint16_t flags) { | |
| 70 header()->flags = flags; | |
| 71 } | |
| 72 | |
| 73 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) { | 73 DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) { |
| 74 qname_size_ = orig.qname_size_; | 74 qname_size_ = orig.qname_size_; |
| 75 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); | 75 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); |
| 76 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), | 76 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), |
| 77 io_buffer_.get()->size()); | 77 io_buffer_.get()->size()); |
| 78 dns_protocol::Header* header = | 78 header()->id = base::HostToNet16(id); |
| 79 reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | |
| 80 header->id = base::HostToNet16(id); | |
| 81 } | 79 } |
| 82 | 80 |
| 83 void DnsQuery::set_flags(uint16_t flags) { | 81 dns_protocol::Header* DnsQuery::header() { |
| 84 dns_protocol::Header* header = | 82 return reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); |
| 85 reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); | |
| 86 header->flags = flags; | |
| 87 } | 83 } |
| 88 | 84 |
| 89 } // namespace net | 85 } // namespace net |
| OLD | NEW |