summaryrefslogtreecommitdiffstats
path: root/net/dns/dns_query.cc
blob: 270757e7be9c9038cf165670f2e8686e9c91e3aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2012 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 "base/sys_byteorder.h"
#include "net/base/big_endian.h"
#include "net/base/dns_util.h"
#include "net/base/io_buffer.h"
#include "net/dns/dns_protocol.h"

namespace net {

// 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.
DnsQuery::DnsQuery(uint16 id, const base::StringPiece& qname, uint16 qtype)
    : qname_size_(qname.size()) {
  DCHECK(!DNSDomainToString(qname).empty());
  // QNAME + QTYPE + QCLASS
  size_t question_size = qname_size_ + sizeof(uint16) + sizeof(uint16);
  io_buffer_ = new IOBufferWithSize(sizeof(dns_protocol::Header) +
                                    question_size);
  dns_protocol::Header* header =
      reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
  memset(header, 0, sizeof(dns_protocol::Header));
  header->id = base::HostToNet16(id);
  header->flags = base::HostToNet16(dns_protocol::kFlagRD);
  header->qdcount = base::HostToNet16(1);

  // Write question section after the header.
  BigEndianWriter writer(reinterpret_cast<char*>(header + 1), question_size);
  writer.WriteBytes(qname.data(), qname.size());
  writer.WriteU16(qtype);
  writer.WriteU16(dns_protocol::kClassIN);
}

DnsQuery::~DnsQuery() {
}

DnsQuery* DnsQuery::CloneWithNewId(uint16 id) const {
  return new DnsQuery(*this, id);
}

uint16 DnsQuery::id() const {
  const dns_protocol::Header* header =
      reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
  return base::NetToHost16(header->id);
}

base::StringPiece DnsQuery::qname() const {
  return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
                           qname_size_);
}

uint16 DnsQuery::qtype() const {
  uint16 type;
  ReadBigEndian<uint16>(io_buffer_->data() +
                        sizeof(dns_protocol::Header) +
                        qname_size_, &type);
  return type;
}

base::StringPiece DnsQuery::question() const {
  return base::StringPiece(io_buffer_->data() + sizeof(dns_protocol::Header),
                           qname_size_ + sizeof(uint16) + sizeof(uint16));
}

DnsQuery::DnsQuery(const DnsQuery& orig, uint16 id) {
  qname_size_ = orig.qname_size_;
  io_buffer_ = new IOBufferWithSize(orig.io_buffer()->size());
  memcpy(io_buffer_.get()->data(), orig.io_buffer()->data(),
         io_buffer_.get()->size());
  dns_protocol::Header* header =
      reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
  header->id = base::HostToNet16(id);
}

void DnsQuery::set_flags(uint16 flags) {
  dns_protocol::Header* header =
      reinterpret_cast<dns_protocol::Header*>(io_buffer_->data());
  header->flags = flags;
}

}  // namespace net