Skip to content

Commit

Permalink
net: read the dns query id in terms of base::ReadBigEndian() function
Browse files Browse the repository at this point in the history
This allow us to write DnsQuery::id() function without the
reinterpret_cast. To be able to do that, we added a pointer to
dns_protocol::Header in DnsQuery. That allowed us to also remove
the header() function.

BUG=None
TEST=net_unittests --gtest_filter=*Dns*
R=ttuttle@chromium.org

Review URL: https://codereview.chromium.org/1597463005

Cr-Commit-Position: refs/heads/master@{#370227}
  • Loading branch information
tfarina authored and Commit bot committed Jan 19, 2016
1 parent f7d5084 commit 2271111
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
28 changes: 12 additions & 16 deletions net/dns/dns_query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ namespace net {
// bit, which directs the name server to pursue query recursively, and sets
// the QDCOUNT to 1, meaning the question section has a single entry.
DnsQuery::DnsQuery(uint16_t id, const base::StringPiece& qname, uint16_t qtype)
: qname_size_(qname.size()) {
: qname_size_(qname.size()),
io_buffer_(
new IOBufferWithSize(sizeof(dns_protocol::Header) + question_size())),
header_(reinterpret_cast<dns_protocol::Header*>(io_buffer_->data())) {
DCHECK(!DNSDomainToString(qname).empty());
io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) +
question_size());
*header() = {};
header()->id = base::HostToNet16(id);
header()->flags = base::HostToNet16(dns_protocol::kFlagRD);
header()->qdcount = base::HostToNet16(1);
*header_ = {};
header_->id = base::HostToNet16(id);
header_->flags = base::HostToNet16(dns_protocol::kFlagRD);
header_->qdcount = base::HostToNet16(1);

// Write question section after the header.
base::BigEndianWriter writer(
Expand All @@ -42,9 +43,7 @@ scoped_ptr<DnsQuery> DnsQuery::CloneWithNewId(uint16_t id) const {
}

uint16_t DnsQuery::id() const {
const dns_protocol::Header* header =
reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
return base::NetToHost16(header->id);
return base::NetToHost16(header_->id);
}

base::StringPiece DnsQuery::qname() const {
Expand All @@ -65,19 +64,16 @@ base::StringPiece DnsQuery::question() const {
}

void DnsQuery::set_flags(uint16_t flags) {
header()->flags = flags;
header_->flags = flags;
}

DnsQuery::DnsQuery(const DnsQuery& orig, uint16_t id) {
qname_size_ = orig.qname_size_;
io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size());
memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(),
io_buffer_.get()->size());
header()->id = base::HostToNet16(id);
}

dns_protocol::Header* DnsQuery::header() {
return reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
header_->id = base::HostToNet16(id);
}

} // namespace net
6 changes: 3 additions & 3 deletions net/dns/dns_query.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ class NET_EXPORT_PRIVATE DnsQuery {
private:
DnsQuery(const DnsQuery& orig, uint16_t id);

// Convenience for header access.
dns_protocol::Header* header();

// Returns the size of the question section.
size_t question_size() const {
// QNAME + QTYPE + QCLASS
Expand All @@ -67,6 +64,9 @@ class NET_EXPORT_PRIVATE DnsQuery {
// Contains query bytes to be consumed by higher level Write() call.
scoped_refptr<IOBufferWithSize> io_buffer_;

// Pointer to the dns header section.
dns_protocol::Header* header_;

DISALLOW_COPY_AND_ASSIGN(DnsQuery);
};

Expand Down

0 comments on commit 2271111

Please sign in to comment.