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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "dnsrecords.hh"
#include "rec-protozero.hh"
#include <variant>
void pdns::ProtoZero::RecMessage::addRR(const DNSRecord& record, const std::set<uint16_t>& exportTypes, [[maybe_unused]] std::optional<bool> udr)
{
if (record.d_place != DNSResourceRecord::ANSWER || record.d_class != QClass::IN) {
return;
}
if (exportTypes.count(record.d_type) == 0) {
return;
}
protozero::pbf_writer pbf_rr{d_response, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::ResponseField::rrs)};
encodeDNSName(pbf_rr, d_rspbuf, static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::name), record.d_name);
pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::type), record.d_type);
pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::class_), record.d_class);
pbf_rr.add_uint32(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::ttl), record.d_ttl);
auto add = [&](const std::string& str) {
if (size() + str.length() < std::numeric_limits<uint16_t>::max() / 2) {
pbf_rr.add_string(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), str);
}
};
switch (record.d_type) {
case QType::A: {
const auto& content = getRR<ARecordContent>(record);
if (!content) {
return;
}
ComboAddress data = content->getCA();
pbf_rr.add_bytes(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), reinterpret_cast<const char*>(&data.sin4.sin_addr.s_addr), sizeof(data.sin4.sin_addr.s_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
break;
}
case QType::AAAA: {
const auto& content = getRR<AAAARecordContent>(record);
if (!content) {
return;
}
ComboAddress data = content->getCA();
pbf_rr.add_bytes(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::rdata), reinterpret_cast<const char*>(&data.sin6.sin6_addr.s6_addr), sizeof(data.sin6.sin6_addr.s6_addr)); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
break;
}
case QType::CNAME: {
const auto& content = getRR<CNAMERecordContent>(record);
if (!content) {
return;
}
add(content->getTarget().toString());
break;
}
case QType::TXT: {
const auto& content = getRR<TXTRecordContent>(record);
if (!content) {
return;
}
add(content->d_text);
break;
}
case QType::NS: {
const auto& content = getRR<NSRecordContent>(record);
if (!content) {
return;
}
add(content->getNS().toString());
break;
}
case QType::PTR: {
const auto& content = getRR<PTRRecordContent>(record);
if (!content) {
return;
}
add(content->getContent().toString());
break;
}
case QType::MX: {
const auto& content = getRR<MXRecordContent>(record);
if (!content) {
return;
}
add(content->d_mxname.toString());
break;
}
case QType::SPF: {
const auto& content = getRR<SPFRecordContent>(record);
if (!content) {
return;
}
add(content->getText());
break;
}
case QType::SRV: {
const auto& content = getRR<SRVRecordContent>(record);
if (!content) {
return;
}
add(content->d_target.toString());
break;
}
case QType::SVCB: {
const auto& content = getRR<SVCBRecordContent>(record);
if (!content) {
return;
}
add(content->getZoneRepresentation());
break;
}
case QType::HTTPS: {
const auto& content = getRR<HTTPSRecordContent>(record);
if (!content) {
return;
}
add(content->getZoneRepresentation());
break;
}
case QType::NAPTR: {
const auto& content = getRR<NAPTRRecordContent>(record);
if (!content) {
return;
}
add(content->getZoneRepresentation());
break;
}
default:
break;
}
#ifdef NOD_ENABLED
if (udr) {
pbf_rr.add_bool(static_cast<protozero::pbf_tag_type>(pdns::ProtoZero::Message::RRField::udr), *udr);
pbf_rr.commit();
// Save the offset of the byte containing the just added bool. We can do this since
// we know a bit about how protobuf's encoding works.
offsets.push_back(d_rspbuf.length() - 1);
}
#endif
}
#ifdef NOD_ENABLED
void pdns::ProtoZero::RecMessage::clearUDR(std::string& str)
{
for (auto offset : offsets) {
str.at(offset) = 0;
}
}
#endif
void pdns::ProtoZero::RecMessage::addEvents(const RecEventTrace& trace)
{
for (const auto& event : trace.getEvents()) {
protozero::pbf_writer pbf_trace{d_message, static_cast<protozero::pbf_tag_type>(Field::trace)};
pbf_trace.add_int64(static_cast<protozero::pbf_tag_type>(Event::ts), event.d_ts);
pbf_trace.add_uint32(static_cast<protozero::pbf_tag_type>(Event::event), event.d_event);
pbf_trace.add_bool(static_cast<protozero::pbf_tag_type>(Event::start), event.d_start);
const auto& value = event.d_value;
if (std::holds_alternative<std::nullopt_t>(value)) {
}
else if (std::holds_alternative<bool>(value)) {
pbf_trace.add_bool(static_cast<protozero::pbf_tag_type>(Event::boolVal), std::get<bool>(value));
}
else if (std::holds_alternative<int64_t>(value)) {
pbf_trace.add_int64(static_cast<protozero::pbf_tag_type>(Event::intVal), std::get<int64_t>(value));
}
else if (std::holds_alternative<std::string>(value)) {
pbf_trace.add_string(static_cast<protozero::pbf_tag_type>(Event::stringVal), std::get<std::string>(value));
}
else if (std::holds_alternative<PacketBuffer>(value)) {
const auto& packetBuffer = std::get<PacketBuffer>(value);
pbf_trace.add_bytes(static_cast<protozero::pbf_tag_type>(Event::bytesVal), reinterpret_cast<const char*>(packetBuffer.data()), packetBuffer.size()); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast): it's the API
}
if (!event.d_custom.empty()) {
pbf_trace.add_string(static_cast<protozero::pbf_tag_type>(Event::custom), event.d_custom);
}
}
}
|