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

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

Issue 8762001: Isolates generic DnsClient from AsyncHostResolver. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: uintX_t -> uintX 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
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"
9 #include "net/base/dns_util.h" 10 #include "net/base/dns_util.h"
10 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
12 #include "net/base/sys_byteorder.h"
13 #include "net/dns/dns_protocol.h"
11 14
12 namespace net { 15 namespace net {
13 16
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
27 // DNS query consists of a 12-byte header followed by a question section. 17 // DNS query consists of a 12-byte header followed by a question section.
28 // For details, see RFC 1035 section 4.1.1. This header template sets RD 18 // For details, see RFC 1035 section 4.1.1. This header template sets RD
29 // bit, which directs the name server to pursue query recursively, and sets 19 // bit, which directs the name server to pursue query recursively, and sets
30 // the QDCOUNT to 1, meaning the question section has a single entry. The 20 // the QDCOUNT to 1, meaning the question section has a single entry.
31 // first two bytes of the header form a 16-bit random query ID to be copied 21 DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype)
32 // in the corresponding reply by the name server -- randomized during 22 : qname_size_(qname.size()) {
33 // DnsQuery construction. 23 DCHECK(!DNSDomainToString(qname).empty());
34 static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 24 // QNAME + QTYPE + QCLASS
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; 25 size_t question_size = qname_size_ + sizeof(uint16) + sizeof(uint16);
36 static const size_t kHeaderSize = arraysize(kHeader); 26 io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header)
27 + question_size);
mmenke 2011/12/06 18:08:39 nit: Move '+' onto previous line.
szym 2011/12/06 19:05:51 Done.
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);
37 34
38 DnsQuery::DnsQuery(const std::string& qname, 35 // Write question section after the header.
39 uint16 qtype, 36 BigEndianWriter writer(reinterpret_cast<char*>(header + 1), question_size);
40 const RandIntCallback& rand_int_cb) 37 writer.WriteBytes(qname.data(), qname.size());
41 : qname_size_(qname.size()), 38 writer.WriteU16(qtype);
42 rand_int_cb_(rand_int_cb) { 39 writer.WriteU16(dns_protocol::kClassIN);
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();
59 } 40 }
60 41
61 DnsQuery::~DnsQuery() { 42 DnsQuery::~DnsQuery() {
62 } 43 }
63 44
45 DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const {
46 return new DnsQuery(*this, id);
47 }
48
64 uint16 DnsQuery::id() const { 49 uint16 DnsQuery::id() const {
65 return UnpackUint16BE(&io_buffer_->data()[0]); 50 const dns_protocol::Header* h =
51 reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
52 return ntohs(h->id);
53 }
54
55 base::StringPiece DnsQuery::qname() const {
56 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
57 qname_size_);
66 } 58 }
67 59
68 uint16 DnsQuery::qtype() const { 60 uint16 DnsQuery::qtype() const {
69 return UnpackUint16BE(&io_buffer_->data()[kHeaderSize + qname_size_]); 61 uint16 type;
62 ReadBigEndian<uint16>(io_buffer_->data() +
63 sizeof(dns_protocol::Header) +
64 qname_size_, &type);
65 return type;
70 } 66 }
71 67
72 DnsQuery* DnsQuery::CloneWithNewId() const { 68 base::StringPiece DnsQuery::question() const {
73 return new DnsQuery(qname(), qtype(), rand_int_cb_); 69 return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
70 qname_size_ + sizeof(uint16) + sizeof(uint16));
74 } 71 }
75 72
76 size_t DnsQuery::question_size() const { 73 DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) {
77 return qname_size_ // QNAME 74 qname_size_ = orig.qname_size_;
78 + sizeof(uint16) // QTYPE 75 io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size());
79 + sizeof(uint16); // QCLASS 76 memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(),
80 } 77 io_buffer_.get()->size());
81 78 dns_protocol::Header* header =
82 const char* DnsQuery::question_data() const { 79 reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
83 return &io_buffer_->data()[kHeaderSize]; 80 header->id = htons(id);
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()));
94 } 81 }
95 82
96 } // namespace net 83 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698