blob: 78e59f5d665ddab295b8203b4e8eff871be70665 [file] [log] [blame]
Kevin Cernekeed05be172017-06-17 17:40:21 -07001// Copyright (c) 2012 The Chromium OS 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
Garrick Evans3388a032020-03-24 11:25:55 +09005#include "patchpanel/dns/dns_response.h"
Kevin Cernekeed05be172017-06-17 17:40:21 -07006
7#include <limits>
8
9#include "base/strings/string_util.h"
10#include "base/sys_byteorder.h"
11
Garrick Evans3388a032020-03-24 11:25:55 +090012#include "patchpanel/dns/big_endian.h"
13#include "patchpanel/dns/dns_protocol.h"
14#include "patchpanel/dns/io_buffer.h"
Kevin Cernekeed05be172017-06-17 17:40:21 -070015
16namespace net {
17
18namespace {
19
20const size_t kHeaderSize = sizeof(dns_protocol::Header);
21
22const uint8_t kRcodeMask = 0xf;
23
24} // namespace
25
Ben Chan4f386502019-09-20 16:17:59 -070026DnsResourceRecord::DnsResourceRecord() = default;
Kevin Cernekeed05be172017-06-17 17:40:21 -070027
Ben Chan4f386502019-09-20 16:17:59 -070028DnsResourceRecord::~DnsResourceRecord() = default;
Kevin Cernekeed05be172017-06-17 17:40:21 -070029
Ben Chanac009042019-09-20 16:19:56 -070030DnsRecordParser::DnsRecordParser() : packet_(nullptr), length_(0), cur_(0) {}
Kevin Cernekeed05be172017-06-17 17:40:21 -070031
32DnsRecordParser::DnsRecordParser(const void* packet,
33 size_t length,
34 size_t offset)
35 : packet_(reinterpret_cast<const char*>(packet)),
36 length_(length),
37 cur_(packet_ + offset) {
38 DCHECK_LE(offset, length);
39}
40
41unsigned DnsRecordParser::ReadName(const void* const vpos,
42 std::string* out) const {
43 const char* const pos = reinterpret_cast<const char*>(vpos);
44 DCHECK(packet_);
45 DCHECK_LE(packet_, pos);
46 DCHECK_LE(pos, packet_ + length_);
47
48 const char* p = pos;
49 const char* end = packet_ + length_;
50 // Count number of seen bytes to detect loops.
51 unsigned seen = 0;
52 // Remember how many bytes were consumed before first jump.
53 unsigned consumed = 0;
54
55 if (pos >= end)
56 return 0;
57
58 if (out) {
59 out->clear();
60 out->reserve(dns_protocol::kMaxNameLength);
61 }
62
63 for (;;) {
64 // The first two bits of the length give the type of the length. It's
65 // either a direct length or a pointer to the remainder of the name.
66 switch (*p & dns_protocol::kLabelMask) {
67 case dns_protocol::kLabelPointer: {
68 if (p + sizeof(uint16_t) > end)
69 return 0;
70 if (consumed == 0) {
71 consumed = p - pos + sizeof(uint16_t);
72 if (!out)
73 return consumed; // If name is not stored, that's all we need.
74 }
75 seen += sizeof(uint16_t);
76 // If seen the whole packet, then we must be in a loop.
77 if (seen > length_)
78 return 0;
79 uint16_t offset;
80 base::ReadBigEndian<uint16_t>(p, &offset);
81 offset &= dns_protocol::kOffsetMask;
82 p = packet_ + offset;
83 if (p >= end)
84 return 0;
85 break;
86 }
87 case dns_protocol::kLabelDirect: {
88 uint8_t label_len = *p;
89 ++p;
90 // Note: root domain (".") is NOT included.
91 if (label_len == 0) {
92 if (consumed == 0) {
93 consumed = p - pos;
94 } // else we set |consumed| before first jump
95 return consumed;
96 }
97 if (p + label_len >= end)
98 return 0; // Truncated or missing label.
99 if (out) {
100 if (!out->empty())
101 out->append(".");
102 out->append(p, label_len);
103 }
104 p += label_len;
105 seen += 1 + label_len;
106 break;
107 }
108 default:
109 // unhandled label type
110 return 0;
111 }
112 }
113}
114
115bool DnsRecordParser::ReadRecord(DnsResourceRecord* out) {
116 DCHECK(packet_);
117 size_t consumed = ReadName(cur_, &out->name);
118 if (!consumed)
119 return false;
120 base::BigEndianReader reader(cur_ + consumed,
121 packet_ + length_ - (cur_ + consumed));
122 uint16_t rdlen;
Hidehiko Abe3a7e5132018-02-15 13:07:50 +0900123 if (reader.ReadU16(&out->type) && reader.ReadU16(&out->klass) &&
124 reader.ReadU32(&out->ttl) && reader.ReadU16(&rdlen) &&
Kevin Cernekeed05be172017-06-17 17:40:21 -0700125 reader.ReadPiece(&out->rdata, rdlen)) {
126 cur_ = reader.ptr();
127 return true;
128 }
129 return false;
130}
131
132bool DnsRecordParser::SkipQuestion() {
Ben Chanac009042019-09-20 16:19:56 -0700133 size_t consumed = ReadName(cur_, nullptr);
Kevin Cernekeed05be172017-06-17 17:40:21 -0700134 if (!consumed)
135 return false;
136
137 const char* next = cur_ + consumed + 2 * sizeof(uint16_t); // QTYPE + QCLASS
138 if (next > packet_ + length_)
139 return false;
140
141 cur_ = next;
142
143 return true;
144}
145
146DnsResponse::DnsResponse()
Hidehiko Abe3a7e5132018-02-15 13:07:50 +0900147 : io_buffer_(new IOBufferWithSize(dns_protocol::kMaxUDPSize + 1)) {}
Kevin Cernekeed05be172017-06-17 17:40:21 -0700148
149DnsResponse::DnsResponse(size_t length)
Hidehiko Abe3a7e5132018-02-15 13:07:50 +0900150 : io_buffer_(new IOBufferWithSize(length)) {}
Kevin Cernekeed05be172017-06-17 17:40:21 -0700151
Hidehiko Abe3a7e5132018-02-15 13:07:50 +0900152DnsResponse::DnsResponse(const void* data, size_t length, size_t answer_offset)
Kevin Cernekeed05be172017-06-17 17:40:21 -0700153 : io_buffer_(new IOBufferWithSize(length)),
154 parser_(io_buffer_->data(), length, answer_offset) {
155 DCHECK(data);
156 memcpy(io_buffer_->data(), data, length);
157}
158
Ben Chan4f386502019-09-20 16:17:59 -0700159DnsResponse::~DnsResponse() = default;
Kevin Cernekeed05be172017-06-17 17:40:21 -0700160
161bool DnsResponse::InitParseWithoutQuery(int nbytes) {
162 DCHECK_GE(nbytes, 0);
163
164 if (nbytes < static_cast<int>(kHeaderSize) || nbytes >= io_buffer_->size())
165 return false;
166
167 parser_ = DnsRecordParser(io_buffer_->data(), nbytes, kHeaderSize);
168
169 unsigned qdcount = base::NetToHost16(header()->qdcount);
170 for (unsigned i = 0; i < qdcount; ++i) {
171 if (!parser_.SkipQuestion()) {
172 parser_ = DnsRecordParser(); // Make parser invalid again.
173 return false;
174 }
175 }
176
177 return true;
178}
179
180bool DnsResponse::IsValid() const {
181 return parser_.IsValid();
182}
183
184uint16_t DnsResponse::flags() const {
185 DCHECK(parser_.IsValid());
186 return base::NetToHost16(header()->flags) & ~(kRcodeMask);
187}
188
189uint8_t DnsResponse::rcode() const {
190 DCHECK(parser_.IsValid());
191 return base::NetToHost16(header()->flags) & kRcodeMask;
192}
193
194unsigned DnsResponse::answer_count() const {
195 DCHECK(parser_.IsValid());
196 return base::NetToHost16(header()->ancount);
197}
198
199unsigned DnsResponse::additional_answer_count() const {
200 DCHECK(parser_.IsValid());
201 return base::NetToHost16(header()->arcount);
202}
203
204base::StringPiece DnsResponse::qname() const {
205 DCHECK(parser_.IsValid());
206 // The response is HEADER QNAME QTYPE QCLASS ANSWER.
207 // |parser_| is positioned at the beginning of ANSWER, so the end of QNAME is
208 // two uint16_ts before it.
209 const size_t qname_size =
210 parser_.GetOffset() - 2 * sizeof(uint16_t) - kHeaderSize;
211 return base::StringPiece(io_buffer_->data() + kHeaderSize, qname_size);
212}
213
214uint16_t DnsResponse::qtype() const {
215 DCHECK(parser_.IsValid());
216 // QTYPE starts where QNAME ends.
217 const size_t type_offset = parser_.GetOffset() - 2 * sizeof(uint16_t);
218 uint16_t type;
219 base::ReadBigEndian<uint16_t>(io_buffer_->data() + type_offset, &type);
220 return type;
221}
222
223DnsRecordParser DnsResponse::Parser() const {
224 DCHECK(parser_.IsValid());
225 // Return a copy of the parser.
226 return parser_;
227}
228
229const dns_protocol::Header* DnsResponse::header() const {
230 return reinterpret_cast<const dns_protocol::Header*>(io_buffer_->data());
231}
232
233} // namespace net