summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_query.cc
diff options
context:
space:
mode:
authoragayev@chromium.org <agayev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-22 15:58:20 +0000
committeragayev@chromium.org <agayev@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-07-22 15:58:20 +0000
commit4b0112ab742b96585b897269dd27cc7818b3f129 (patch)
tree0ea379258252b88f5e323b9c6f547f8b3c2500fa /net/dns/dns_query.cc
parent17bf5d37a85eff17c372db57ef37cc4b57f28b53 (diff)
downloadchromium_src-4b0112ab742b96585b897269dd27cc7818b3f129.zip
chromium_src-4b0112ab742b96585b897269dd27cc7818b3f129.tar.gz
chromium_src-4b0112ab742b96585b897269dd27cc7818b3f129.tar.bz2
File reorganization: move AsyncHostResolver files to net/dns.
BUG=60149 TEST=net_unittest --gtest_filter="AsyncHostResolver*" Review URL: http://codereview.chromium.org/7484012 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@93644 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/dns/dns_query.cc')
-rw-r--r--net/dns/dns_query.cc96
1 files changed, 96 insertions, 0 deletions
diff --git a/net/dns/dns_query.cc b/net/dns/dns_query.cc
new file mode 100644
index 0000000..788d653
--- /dev/null
+++ b/net/dns/dns_query.cc
@@ -0,0 +1,96 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "net/dns/dns_query.h"
+
+#include <limits>
+
+#include "net/base/dns_util.h"
+#include "net/base/io_buffer.h"
+
+namespace net {
+
+namespace {
+
+void PackUint16BE(char buf[2], uint16 v) {
+ buf[0] = v >> 8;
+ buf[1] = v & 0xff;
+}
+
+uint16 UnpackUint16BE(char buf[2]) {
+ return static_cast<uint8>(buf[0]) << 8 | static_cast<uint8>(buf[1]);
+}
+
+} // namespace
+
+// DNS query consists of a 12-byte header followed by a question section.
+// For details, see RFC 1035 section 4.1.1. This header template sets RD
+// bit, which directs the name server to pursue query recursively, and sets
+// the QDCOUNT to 1, meaning the question section has a single entry. The
+// first two bytes of the header form a 16-bit random query ID to be copied
+// in the corresponding reply by the name server -- randomized during
+// DnsQuery construction.
+static const char kHeader[] = {0x00, 0x00, 0x01, 0x00, 0x00, 0x01,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
+static const size_t kHeaderSize = arraysize(kHeader);
+
+DnsQuery::DnsQuery(const std::string& qname,
+ uint16 qtype,
+ const RandIntCallback& rand_int_cb)
+ : qname_size_(qname.size()),
+ rand_int_cb_(rand_int_cb) {
+ DCHECK(DnsResponseBuffer(reinterpret_cast<const uint8*>(qname.c_str()),
+ qname.size()).DNSName(NULL));
+ DCHECK(qtype == kDNS_A || qtype == kDNS_AAAA);
+
+ io_buffer_ = new IOBufferWithSize(kHeaderSize + question_size());
+
+ int byte_offset = 0;
+ char* buffer_head = io_buffer_->data();
+ memcpy(&buffer_head[byte_offset], kHeader, kHeaderSize);
+ byte_offset += kHeaderSize;
+ memcpy(&buffer_head[byte_offset], &qname[0], qname_size_);
+ byte_offset += qname_size_;
+ PackUint16BE(&buffer_head[byte_offset], qtype);
+ byte_offset += sizeof(qtype);
+ PackUint16BE(&buffer_head[byte_offset], kClassIN);
+ RandomizeId();
+}
+
+DnsQuery::~DnsQuery() {
+}
+
+uint16 DnsQuery::id() const {
+ return UnpackUint16BE(&io_buffer_->data()[0]);
+}
+
+uint16 DnsQuery::qtype() const {
+ return UnpackUint16BE(&io_buffer_->data()[kHeaderSize + qname_size_]);
+}
+
+DnsQuery* DnsQuery::CloneWithNewId() const {
+ return new DnsQuery(qname(), qtype(), rand_int_cb_);
+}
+
+size_t DnsQuery::question_size() const {
+ return qname_size_ // QNAME
+ + sizeof(uint16) // QTYPE
+ + sizeof(uint16); // QCLASS
+}
+
+const char* DnsQuery::question_data() const {
+ return &io_buffer_->data()[kHeaderSize];
+}
+
+const std::string DnsQuery::qname() const {
+ return std::string(question_data(), qname_size_);
+}
+
+void DnsQuery::RandomizeId() {
+ PackUint16BE(&io_buffer_->data()[0], rand_int_cb_.Run(
+ std::numeric_limits<uint16>::min(),
+ std::numeric_limits<uint16>::max()));
+}
+
+} // namespace net