Chromium Code Reviews| Index: net/base/dns_query.cc |
| diff --git a/net/base/dns_query.cc b/net/base/dns_query.cc |
| index ba7915000966912778b6e3d0fb0d1829610dbfc0..e7eb7e5d9437a1c24091540b358985065ffee3df 100644 |
| --- a/net/base/dns_query.cc |
| +++ b/net/base/dns_query.cc |
| @@ -7,7 +7,6 @@ |
| #include <string> |
| #include "base/rand_util.h" |
| -#include "net/base/address_family.h" |
| #include "net/base/dns_util.h" |
| namespace net { |
| @@ -19,16 +18,8 @@ void PackUint16BE(char buf[2], uint16 v) { |
| buf[1] = v & 0xff; |
| } |
| -uint16 QTypeFromAddressFamily(AddressFamily address_family) { |
| - switch (address_family) { |
| - case ADDRESS_FAMILY_IPV4: |
| - return kDNS_A; |
| - case ADDRESS_FAMILY_IPV6: |
| - return kDNS_AAAA; |
| - default: |
| - NOTREACHED() << "Bad address family"; |
| - return kDNS_A; |
| - } |
| +uint16 UnpackUint16BE(char buf[2]) { |
| + return static_cast<uint8>(buf[0]) << 8 | static_cast<uint8>(buf[1]); |
| } |
| } // namespace |
| @@ -42,70 +33,62 @@ uint16 QTypeFromAddressFamily(AddressFamily address_family) { |
| // DnsQuery construction. |
| static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01, |
| 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| -static const size_t kHeaderLen = arraysize(kHeader); |
| +static const size_t kHeaderSize = arraysize(kHeader); |
| -DnsQuery::DnsQuery(const std::string& hostname, |
| - AddressFamily address_family, |
| - int port) |
| - : port_(port), |
| - id_(0), |
| - qtype_(QTypeFromAddressFamily(address_family)), |
| - hostname_(hostname) { |
| - std::string qname; |
| - if (!net::DNSDomainFromDot(hostname, &qname)) |
| - return; |
| +DnsQuery::DnsQuery(const std::string& dns_name, uint16 qtype) |
| + : dns_name_size_(dns_name.size()) { |
| + DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(dns_name.c_str()), |
| + dns_name.size()).DNSName(NULL)); |
| + DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA); |
| - size_t query_size = kHeaderLen + qname.size() + |
| - sizeof(qtype_) + sizeof(kClassIN); |
| - |
| - io_buffer_ = new IOBufferWithSize(query_size); |
| + io_buffer_ = new IOBufferWithSize(kHeaderSize + question_size()); |
| int byte_offset = 0; |
| char* buffer_head = io_buffer_->data(); |
| - memcpy(&buffer_head[byte_offset], kHeader, kHeaderLen); |
| - byte_offset += kHeaderLen; |
| - memcpy(&buffer_head[byte_offset], &qname[0], qname.size()); |
| - byte_offset += qname.size(); |
| - PackUint16BE(&buffer_head[byte_offset], qtype_); |
| - byte_offset += sizeof(qtype_); |
| + memcpy(&buffer_head[byte_offset], kHeader, kHeaderSize); |
| + byte_offset += kHeaderSize; |
| + memcpy(&buffer_head[byte_offset], &dns_name[0], dns_name_size_); |
| + byte_offset += dns_name_size_; |
| + PackUint16BE(&buffer_head[byte_offset], qtype); |
| + byte_offset += sizeof(qtype); |
| PackUint16BE(&buffer_head[byte_offset], kClassIN); |
| - |
| - // Randomize ID, first two bytes. |
| - id_ = base::RandUint64() & 0xffff; |
| - PackUint16BE(&buffer_head[0], id_); |
| + RandomizeId(); |
| } |
| -DnsQuery::~DnsQuery() { |
| +DnsQuery::DnsQuery(const DnsQuery& rhs) { |
|
cbentzel
2011/06/09 01:16:06
Use initializer arguments rather than assignment w
agayev
2011/06/09 14:23:02
Done.
|
| + dns_name_size_ = rhs.dns_name_size_; |
| + io_buffer_ = new IOBufferWithSize(rhs.io_buffer()->size()); |
| + memcpy(io_buffer_->data(), rhs.io_buffer()->data(), rhs.io_buffer()->size()); |
| + RandomizeId(); |
| } |
| -int DnsQuery::port() const { |
| - DCHECK(IsValid()); |
| - return port_; |
| +DnsQuery::~DnsQuery() { |
| } |
| uint16 DnsQuery::id() const { |
| - DCHECK(IsValid()); |
| - return id_; |
| + return UnpackUint16BE(&io_buffer_->data()[0]); |
| } |
| uint16 DnsQuery::qtype() const { |
| - DCHECK(IsValid()); |
| - return qtype_; |
| + return UnpackUint16BE(&io_buffer_->data()[kHeaderSize + dns_name_size_]); |
| +} |
| + |
| +DnsQuery* DnsQuery::CloneWithNewId() const { |
| + return new DnsQuery(*this); |
| } |
| -AddressFamily DnsQuery::address_family() const { |
| - DCHECK(IsValid()); |
| - return address_family_; |
| +size_t DnsQuery::question_size() const { |
| + return dns_name_size_ // QNAME |
| + + sizeof(uint16) // QTYPE |
| + + sizeof(uint16); // QCLASS |
| } |
| -const std::string& DnsQuery::hostname() const { |
| - DCHECK(IsValid()); |
| - return hostname_; |
| +char* DnsQuery::question_data() const { |
| + return &io_buffer_->data()[kHeaderSize]; |
| } |
| -IOBufferWithSize* DnsQuery::io_buffer() const { |
| - DCHECK(IsValid()); |
| - return io_buffer_.get(); |
| +void DnsQuery::RandomizeId() { |
| + PackUint16BE(&io_buffer_->data()[0], base::RandUint64() & 0xffff); |
| } |
| } // namespace net |