Jason Jeremy Iman | a21be27 | 2020-10-21 17:53:45 +0900 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 5 | #include "patchpanel/dns/dns_query.h" |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "base/big_endian.h" |
| 10 | #include "base/logging.h" |
| 11 | #include "base/memory/ptr_util.h" |
| 12 | #include "base/numerics/safe_conversions.h" |
| 13 | #include "base/optional.h" |
| 14 | #include "base/sys_byteorder.h" |
| 15 | |
| 16 | #include "patchpanel/dns/dns_protocol.h" |
| 17 | #include "patchpanel/dns/dns_util.h" |
| 18 | #include "patchpanel/dns/io_buffer.h" |
| 19 | |
| 20 | namespace patchpanel { |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | const size_t kHeaderSize = sizeof(dns_protocol::Header); |
| 25 | |
| 26 | size_t QuestionSize(size_t qname_size) { |
| 27 | // QNAME + QTYPE + QCLASS |
| 28 | return qname_size + sizeof(uint16_t) + sizeof(uint16_t); |
| 29 | } |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | DnsQuery::DnsQuery(scoped_refptr<IOBufferWithSize> buffer) |
| 34 | : io_buffer_(std::move(buffer)) {} |
| 35 | |
| 36 | DnsQuery::~DnsQuery() = default; |
| 37 | |
| 38 | bool DnsQuery::Parse(size_t valid_bytes) { |
| 39 | if (io_buffer_ == nullptr || io_buffer_->data() == nullptr) { |
| 40 | return false; |
| 41 | } |
| 42 | CHECK(valid_bytes <= base::checked_cast<size_t>(io_buffer_->size())); |
| 43 | // We should only parse the query once if the query is constructed from a raw |
| 44 | // buffer. If we have constructed the query from data or the query is already |
| 45 | // parsed after constructed from a raw buffer, |header_| is not null. |
| 46 | DCHECK(header_ == nullptr); |
| 47 | base::BigEndianReader reader(io_buffer_->data(), valid_bytes); |
| 48 | dns_protocol::Header header; |
| 49 | if (!ReadHeader(&reader, &header)) { |
| 50 | return false; |
| 51 | } |
| 52 | if (header.flags & dns_protocol::kFlagResponse) { |
| 53 | return false; |
| 54 | } |
| 55 | if (header.qdcount > 1) { |
| 56 | LOG(ERROR) << "Not supporting parsing a DNS query with multiple questions."; |
| 57 | return false; |
| 58 | } |
| 59 | std::string qname; |
| 60 | if (!ReadName(&reader, &qname)) { |
| 61 | return false; |
| 62 | } |
| 63 | uint16_t qtype; |
| 64 | uint16_t qclass; |
| 65 | if (!reader.ReadU16(&qtype) || !reader.ReadU16(&qclass) || |
| 66 | qclass != dns_protocol::kClassIN) { |
| 67 | return false; |
| 68 | } |
| 69 | // |io_buffer_| now contains the raw packet of a valid DNS query, we just |
| 70 | // need to properly initialize |qname_size_| and |header_|. |
| 71 | qname_size_ = qname.size(); |
| 72 | header_ = reinterpret_cast<dns_protocol::Header*>(io_buffer_->data()); |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | uint16_t DnsQuery::id() const { |
| 77 | return base::NetToHost16(header_->id); |
| 78 | } |
| 79 | |
| 80 | base::StringPiece DnsQuery::qname() const { |
| 81 | return base::StringPiece(io_buffer_->data() + kHeaderSize, qname_size_); |
| 82 | } |
| 83 | |
| 84 | uint16_t DnsQuery::qtype() const { |
| 85 | uint16_t type; |
| 86 | base::ReadBigEndian<uint16_t>(io_buffer_->data() + kHeaderSize + qname_size_, |
| 87 | &type); |
| 88 | return type; |
| 89 | } |
| 90 | |
| 91 | base::StringPiece DnsQuery::question() const { |
| 92 | return base::StringPiece(io_buffer_->data() + kHeaderSize, |
| 93 | QuestionSize(qname_size_)); |
| 94 | } |
| 95 | |
| 96 | size_t DnsQuery::question_size() const { |
| 97 | return QuestionSize(qname_size_); |
| 98 | } |
| 99 | |
| 100 | bool DnsQuery::ReadHeader(base::BigEndianReader* reader, |
| 101 | dns_protocol::Header* header) { |
| 102 | return ( |
| 103 | reader->ReadU16(&header->id) && reader->ReadU16(&header->flags) && |
| 104 | reader->ReadU16(&header->qdcount) && reader->ReadU16(&header->ancount) && |
| 105 | reader->ReadU16(&header->nscount) && reader->ReadU16(&header->arcount)); |
| 106 | } |
| 107 | |
| 108 | bool DnsQuery::ReadName(base::BigEndianReader* reader, std::string* out) { |
| 109 | DCHECK(out != nullptr); |
| 110 | out->clear(); |
| 111 | out->reserve(dns_protocol::kMaxNameLength); |
| 112 | uint8_t label_length; |
| 113 | if (!reader->ReadU8(&label_length)) { |
| 114 | return false; |
| 115 | } |
| 116 | out->append(reinterpret_cast<char*>(&label_length), 1); |
| 117 | while (label_length) { |
| 118 | base::StringPiece label; |
| 119 | if (!reader->ReadPiece(&label, label_length)) { |
| 120 | return false; |
| 121 | } |
| 122 | out->append(label.data(), label.size()); |
| 123 | if (!reader->ReadU8(&label_length)) { |
| 124 | return false; |
| 125 | } |
| 126 | out->append(reinterpret_cast<char*>(&label_length), 1); |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | } // namespace patchpanel |