Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(289)

Side by Side Diff: net/dns/dns_query.cc

Issue 8835011: Revert 113282 - Isolates generic DnsClient from AsyncHostResolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/dns/dns_query.h ('k') | net/dns/dns_query_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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> 7 #include <limits>
8 8
9 #include "net/base/big_endian.h"
10 #include "net/base/dns_util.h" 9 #include "net/base/dns_util.h"
11 #include "net/base/io_buffer.h" 10 #include "net/base/io_buffer.h"
12 #include "net/base/sys_byteorder.h"
13 #include "net/dns/dns_protocol.h"
14 11
15 namespace net { 12 namespace net {
16 13
14 namespace {
15
16 void PackUint16BE(char buf[2], uint16 v) {
17 buf[0] = v >> 8;
18 buf[1] = v & 0xff;
19 }
20
21 uint16 UnpackUint16BE(char buf[2]) {
22 return static_cast<uint8>(buf[0]) << 8 | static_cast<uint8>(buf[1]);
23 }
24
25 } // namespace
26
17 // DNS query consists of a 12-byte header followed by a question section. 27 // 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 28 // 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 29 // 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. 30 // the QDCOUNT to 1, meaning the question section has a single entry. The
21 DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype) 31 // first two bytes of the header form a 16-bit random query ID to be copied
22 : qname_size_(qname.size()) { 32 // in the corresponding reply by the name server -- randomized during
23 DCHECK(!DNSDomainToString(qname).empty()); 33 // DnsQuery construction.
24 // QNAME + QTYPE + QCLASS 34 static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01,
25 size_t question_size = qname_size_ + sizeof(uint16) + sizeof(uint16); 35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
26 io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) + 36 static const size_t kHeaderSize = arraysize(kHeader);
27 question_size);
28 dns_protocol::Header* header =
29 reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
30 memset(header, 0, sizeof(dns_protocol::Header));
31 header->id = htons(id);
32 header->flags[0] = 0x1; // RD bit
33 header->qdcount = htons(1);
34 37
35 // Write question section after the header. 38 DnsQuery::DnsQuery(const std::string& qname,
36 BigEndianWriter writer(reinterpret_cast<char*>(header + 1), question_size); 39 uint16 qtype,
37 writer.WriteBytes(qname.data(), qname.size()); 40 const RandIntCallback& rand_int_cb)
38 writer.WriteU16(qtype); 41 : qname_size_(qname.size()),
39 writer.WriteU16(dns_protocol::kClassIN); 42 rand_int_cb_(rand_int_cb) {
43 DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(qname.c_str()),
44 qname.size()).DNSName(NULL));
45 DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA);
46
47 io_buffer_ = new IOBufferWithSize(kHeaderSize + question_size());
48
49 int byte_offset = 0;
50 char* buffer_head = io_buffer_->data();
51 memcpy(&buffer_head[byte_offset], kHeader, kHeaderSize);
52 byte_offset += kHeaderSize;
53 memcpy(&buffer_head[byte_offset], &qname[0], qname_size_);
54 byte_offset += qname_size_;
55 PackUint16BE(&buffer_head[byte_offset], qtype);
56 byte_offset += sizeof(qtype);
57 PackUint16BE(&buffer_head[byte_offset], kClassIN);
58 RandomizeId();
40 } 59 }
41 60
42 DnsQuery::~DnsQuery() { 61 DnsQuery::~DnsQuery() {
43 } 62 }
44 63
45 DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const {
46 return new DnsQuery(*this, id);
47 }
48
49 uint16 DnsQuery::id() const { 64 uint16 DnsQuery::id() const {
50 const dns_protocol::Header* header = 65 return UnpackUint16BE(&io_buffer_->data()[0]);
51 reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
52 return ntohs(header->id);
53 }
54
55 base::StringPiece DnsQuery::qname() const {
56 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
57 qname_size_);
58 } 66 }
59 67
60 uint16 DnsQuery::qtype() const { 68 uint16 DnsQuery::qtype() const {
61 uint16 type; 69 return UnpackUint16BE(&io_buffer_->data()[kHeaderSize + qname_size_]);
62 ReadBigEndian<uint16>(io_buffer_->data() +
63 sizeof(dns_protocol::Header) +
64 qname_size_, &type);
65 return type;
66 } 70 }
67 71
68 base::StringPiece DnsQuery::question() const { 72 DnsQuery* DnsQuery::CloneWithNewId() const {
69 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header), 73 return new DnsQuery(qname(), qtype(), rand_int_cb_);
70 qname_size_ + sizeof(uint16) + sizeof(uint16));
71 } 74 }
72 75
73 DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) { 76 size_t DnsQuery::question_size() const {
74 qname_size_ = orig.qname_size_; 77 return qname_size_ // QNAME
75 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size()); 78 + sizeof(uint16) // QTYPE
76 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(), 79 + sizeof(uint16); // QCLASS
77 io_buffer_.get()->size()); 80 }
78 dns_protocol::Header* header = 81
79 reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); 82 const char* DnsQuery::question_data() const {
80 header->id = htons(id); 83 return &io_buffer_->data()[kHeaderSize];
84 }
85
86 const std::string DnsQuery::qname() const {
87 return std::string(question_data(), qname_size_);
88 }
89
90 void DnsQuery::RandomizeId() {
91 PackUint16BE(&io_buffer_->data()[0], rand_int_cb_.Run(
92 std::numeric_limits<uint16>::min(),
93 std::numeric_limits<uint16>::max()));
81 } 94 }
82 95
83 } // namespace net 96 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/dns_query.h ('k') | net/dns/dns_query_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698