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

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: fix issues mentioned by agl Created 9 years, 6 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[0] = v >> 8;
21 buf[1] = v & 0xff;
22 }
23
24 uint16 QTypeFromAddressFamily(AddressFamily address_family) {
25 switch (address_family) {
26 case ADDRESS_FAMILY_IPV4:
27 return kDNS_A;
28 case ADDRESS_FAMILY_IPV6:
29 return kDNS_AAAA;
30 default:
31 NOTREACHED() << "Bad address family";
32 return kDNS_A;
33 }
34 }
35
36 }
cbentzel 2011/06/01 17:17:03 Nit: // namespace to indicate end of anonymous nam
agayev 2011/06/01 19:15:01 Done.
37
38 // DNS query consists of a 12-byte header followed by a question section.
39 // For details, see RFC 1035 section 4.1.1. This header template sets RD
40 // bit, which directs the name server to pursue query recursively, and sets
41 // the QDCOUNT to 1, meaning the question section has a single entry. The
42 // first two bytes of the header form a 16-bit query ID to be copied in the
43 // corresponding reply by the name server; it's set to 0 here and is
44 // randomized every time the underlying buffer is fetched.
45 static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
47 static const size_t kHeaderLen = arraysize(kHeader);
48
49 DnsQuery::DnsQuery(const std::string& hostname,
50 AddressFamily address_family,
51 int port)
52 : port_(port),
53 id_(0),
54 qtype_(QTypeFromAddressFamily(address_family)),
55 hostname_(hostname) {
56 std::string qname;
57 if (!net::DNSDomainFromDot(hostname, &qname))
58 return;
59
60 size_t query_size = kHeaderLen + qname.size() +
61 sizeof(qtype_) + sizeof(kClassIN);
62
63 io_buffer_ = new IOBufferWithSize(query_size);
64
65 int iterator = 0;
cbentzel 2011/06/01 17:17:03 nit: byte_offset instead of iterator? Also, maybe
agayev 2011/06/01 19:15:01 Done.
66 memcpy(&io_buffer_->data()[iterator], kHeader, kHeaderLen);
67 iterator += kHeaderLen;
68 memcpy(&io_buffer_->data()[iterator], &qname[0], qname.size());
69 iterator += qname.size();
70 PackUint16BE(&io_buffer_->data()[iterator], qtype_);
71 iterator += sizeof(qtype_);
72 PackUint16BE(&io_buffer_->data()[iterator], kClassIN);
73 iterator += sizeof(kClassIN);
74
75 RandomizeId();
cbentzel 2011/06/01 17:17:03 Should you just do this in io_buffer()
agayev 2011/06/01 19:15:01 That would lead to a construction of a degenerate
cbentzel 2011/06/01 19:41:21 We talked and decided to have io_buffer() mutate t
76 }
77
78 void DnsQuery::RandomizeId() {
79 DCHECK(IsValid());
80 id_ = base::RandUint64() & 0xffff;
81 PackUint16BE(io_buffer_->data(), id_);
82 }
83
84 int DnsQuery::port() const {
85 DCHECK(IsValid());
86 return port_;
87 }
88
89 uint16 DnsQuery::id() const {
90 DCHECK(IsValid());
91 return id_;
92 }
93
94 uint16 DnsQuery::qtype() const {
95 DCHECK(IsValid());
96 return qtype_;
97 }
98
99 const std::string& DnsQuery::hostname() const {
100 DCHECK(IsValid());
101 return hostname_;
102 }
103
104 int DnsQuery::size() const {
105 DCHECK(IsValid());
106 return io_buffer_->size();
107 }
108
109 IOBuffer* DnsQuery::io_buffer() {
110 DCHECK(IsValid());
111 RandomizeId();
112 return io_buffer_.get();
113 }
114
115 const char* DnsQuery::data() const {
116 DCHECK(IsValid());
117 return io_buffer_->data();
118 }
119
120 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698