]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/recursordist/rec-protozero.cc
8a698093d36a96924b071ab5d0866663671797c7
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "dnsrecords.hh"
24 #include "rec-protozero.hh"
27 void pdns::ProtoZero::RecMessage::addRR(const DNSRecord
& record
, const std::set
<uint16_t>& exportTypes
, bool udr
)
29 if (record
.d_place
!= DNSResourceRecord::ANSWER
|| record
.d_class
!= QClass::IN
) {
33 if (exportTypes
.count(record
.d_type
) == 0) {
37 protozero::pbf_writer pbf_rr
{d_response
, static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::ResponseField::rrs
)};
39 encodeDNSName(pbf_rr
, d_rspbuf
, static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::name
), record
.d_name
);
40 pbf_rr
.add_uint32(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::type
), record
.d_type
);
41 pbf_rr
.add_uint32(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::class_
), record
.d_class
);
42 pbf_rr
.add_uint32(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::ttl
), record
.d_ttl
);
44 switch (record
.d_type
) {
46 const auto& content
= dynamic_cast<const ARecordContent
&>(*(record
.d_content
));
47 ComboAddress data
= content
.getCA();
48 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
));
52 const auto& content
= dynamic_cast<const AAAARecordContent
&>(*(record
.d_content
));
53 ComboAddress data
= content
.getCA();
54 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
));
58 const auto& content
= dynamic_cast<const CNAMERecordContent
&>(*(record
.d_content
));
59 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.getTarget().toString());
63 const auto& content
= dynamic_cast<const TXTRecordContent
&>(*(record
.d_content
));
64 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.d_text
);
68 const auto& content
= dynamic_cast<const NSRecordContent
&>(*(record
.d_content
));
69 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.getNS().toString());
73 const auto& content
= dynamic_cast<const PTRRecordContent
&>(*(record
.d_content
));
74 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.getContent().toString());
78 const auto& content
= dynamic_cast<const MXRecordContent
&>(*(record
.d_content
));
79 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.d_mxname
.toString());
83 const auto& content
= dynamic_cast<const SPFRecordContent
&>(*(record
.d_content
));
84 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.getText());
88 const auto& content
= dynamic_cast<const SRVRecordContent
&>(*(record
.d_content
));
89 pbf_rr
.add_string(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::rdata
), content
.d_target
.toString());
96 pbf_rr
.add_bool(static_cast<protozero::pbf_tag_type
>(pdns::ProtoZero::Message::RRField::udr
), udr
);
99 // Save the offset of the byte containing the just added bool. We can do this since
100 // we know a bit about how protobuf's encoding works.
101 offsets
.push_back(d_rspbuf
.length() - 1);
106 void pdns::ProtoZero::RecMessage::clearUDR(std::string
& str
)
108 for (auto i
: offsets
) {
114 void pdns::ProtoZero::RecMessage::addEvents(const RecEventTrace
& trace
)
116 for (const auto& t
: trace
.getEvents()) {
117 protozero::pbf_writer pbf_trace
{d_message
, static_cast<protozero::pbf_tag_type
>(Field::trace
)};
118 pbf_trace
.add_int64(static_cast<protozero::pbf_tag_type
>(Event::ts
), t
.d_ts
);
119 pbf_trace
.add_uint32(static_cast<protozero::pbf_tag_type
>(Event::event
), t
.d_event
);
120 pbf_trace
.add_bool(static_cast<protozero::pbf_tag_type
>(Event::start
), t
.d_start
);
122 const auto& v
= t
.d_value
;
123 if (std::holds_alternative
<std::nullopt_t
>(v
)) {
125 else if (std::holds_alternative
<bool>(v
)) {
126 pbf_trace
.add_bool(static_cast<protozero::pbf_tag_type
>(Event::boolVal
), std::get
<bool>(v
));
128 else if (std::holds_alternative
<int64_t>(v
)) {
129 pbf_trace
.add_int64(static_cast<protozero::pbf_tag_type
>(Event::intVal
), std::get
<int64_t>(v
));
131 else if (std::holds_alternative
<std::string
>(v
)) {
132 pbf_trace
.add_string(static_cast<protozero::pbf_tag_type
>(Event::stringVal
), std::get
<std::string
>(v
));
134 else if (std::holds_alternative
<PacketBuffer
>(v
)) {
135 const PacketBuffer
& p
= std::get
<PacketBuffer
>(v
);
136 pbf_trace
.add_bytes(static_cast<protozero::pbf_tag_type
>(Event::bytesVal
), reinterpret_cast<const char*>(p
.data()), p
.size());
138 if (!t
.d_custom
.empty()) {
139 pbf_trace
.add_string(static_cast<protozero::pbf_tag_type
>(Event::custom
), t
.d_custom
);