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

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

Issue 7008021: Added DnsQuery class (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added DnsResponse class Created 9 years, 7 months 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "net/base/dns_query.h"
6
7 #include <string>
8
9 #include "base/rand_util.h"
10 #include "net/base/address_family.h"
11 #include "net/base/dns_util.h"
12
13 #include <iostream>
14
15 namespace net {
16
17 namespace {
18
19 void PackUint16BE(char buf[2], uint16 v) {
20 buf[1] = v & 0xff;
agl 2011/05/30 18:35:30 please flip these two lines.
agayev 2011/05/31 15:19:06 Will do.
21 buf[0] = v >> 8;
22 }
23
24 uint16 QTypeFromAddressFamily(AddressFamily address_family) {
25 uint16 qtype = kDNS_A;
agl 2011/05/30 18:35:30 switch (address_family) { case ADDRESS_FAMILY_IP
agayev 2011/05/31 15:19:06 Will do.
26 if (address_family == ADDRESS_FAMILY_IPV6)
27 qtype = kDNS_AAAA;
28 else if (address_family != ADDRESS_FAMILY_IPV4)
29 NOTREACHED() << "Bad address family";
30 return qtype;
31 }
32
33 }
34
35 // DNS query consists of a 12-byte header followed by a question section,
agl 2011/05/30 18:35:30 s/section,/section./ (and s/for/For/ on the next l
agayev 2011/05/31 15:19:06 Will do.
36 // for details, see RFC 1035 section 4.1.1. This header template sets RD
37 // bit, which directs the name server to pursue query recursively, and sets
38 // the QDCOUNT to 1, meaning the question section has a single entry. The
39 // first two bytes of the header form a 16-bit query ID to be copied in the
40 // corresponding reply by the name server; it's set to 0 here and is
41 // randomized every time the underlying buffer is fetched.
42 static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
44 static const int kHeaderLen = arraysize(kHeader);
agl 2011/05/30 18:35:30 s/int/size_t/
agayev 2011/05/31 15:19:06 Same argument as previous one regarding int vs siz
45
46 DnsQuery::DnsQuery(const std::string& hostname,
47 AddressFamily address_family,
48 int port)
49 : port_(port),
50 id_(0),
51 qtype_(QTypeFromAddressFamily(address_family)),
52 qclass_(kClassIN),
53 hostname_(hostname) {
54 std::string qname;
55 if (!net::DNSDomainFromDot(hostname, &qname))
56 return;
57
58 size_t query_size = kHeaderLen + qname.size() +
59 sizeof(qtype_) + sizeof(qclass_);
60
61 io_buffer_ = new IOBufferWithSize(query_size);
62
63 int iterator = 0;
64 memcpy(&io_buffer_->data()[iterator], kHeader, kHeaderLen);
65 iterator += kHeaderLen;
66 memcpy(&io_buffer_->data()[iterator], &qname[0], qname.size());
67 iterator += qname.size();
68 PackUint16BE(&io_buffer_->data()[iterator], qtype_);
69 iterator += sizeof(qtype_);
70 PackUint16BE(&io_buffer_->data()[iterator], qclass_);
71 iterator += sizeof(qclass_);
72
73 // RFC2181 section 11: A full domain name is limited to 255 octets
74 // (including the separators).
75 static const int kMaxQuerySize = kHeaderLen + 255 +
agl 2011/05/30 18:35:30 DNSDomainFromDot should fail if the name is too lo
agayev 2011/05/31 15:19:06 Will do.
76 sizeof(qtype_) + sizeof(qclass_);
77
78 DCHECK(iterator <= kMaxQuerySize);
cbentzel 2011/05/27 15:15:54 This shouldn't be a DCHECK - unless there are chec
agayev 2011/05/31 15:19:06 Will do.
79 DCHECK(io_buffer_->size() > kHeaderLen && io_buffer_->size() <= kMaxQuerySize) ;
80
81 RandomizeId();
82 }
83
84 void DnsQuery::RandomizeId() {
85 DCHECK(IsValid());
86 id_ = base::RandUint64() & 0xffff;
87 PackUint16BE(io_buffer_->data(), id_);
88 }
89
90 int DnsQuery::port() const {
91 DCHECK(IsValid());
92 return port_;
93 }
94
95 uint16 DnsQuery::id() const {
96 DCHECK(IsValid());
97 return id_;
98 }
99
100 uint16 DnsQuery::qtype() const {
101 DCHECK(IsValid());
102 return qtype_;
103 }
104
105 uint16 DnsQuery::qclass() const {
106 DCHECK(IsValid());
107 return qclass_;
108 }
109
110 const std::string& DnsQuery::hostname() const {
111 DCHECK(IsValid());
112 return hostname_;
113 }
114
115 int DnsQuery::size() const {
116 DCHECK(IsValid());
117 return io_buffer_->size();
118 }
119
120 IOBuffer* DnsQuery::io_buffer() {
121 DCHECK(IsValid());
122 RandomizeId();
123 return io_buffer_.get();
124 }
125
126 char* DnsQuery::data() const {
127 DCHECK(IsValid());
128 return io_buffer_->data();
129 }
130
131 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698