]> git.ipfire.org Git - thirdparty/pdns.git/blame - pdns/dnsdist-protobuf.hh
Merge pull request #11431 from jroessler-ox/docs-kskzskroll-update
[thirdparty/pdns.git] / pdns / dnsdist-protobuf.hh
CommitLineData
12471842
PL
1/*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
ec469dd7 22#pragma once
ec469dd7 23
87f46425
RG
24#include "dnsdist.hh"
25#include "dnsname.hh"
454e9974
RG
26
27#ifndef DISABLE_PROTOBUF
87f46425 28#include "protozero.hh"
ec469dd7 29
87f46425 30class DNSDistProtoBufMessage
d9d3f9c1
RG
31{
32public:
94219a42
CHB
33 DNSDistProtoBufMessage(const DNSQuestion& dnsquestion);
34 DNSDistProtoBufMessage(const DNSResponse& dnsresponse, bool includeCNAME);
35 DNSDistProtoBufMessage(const DNSQuestion&&) = delete;
36 DNSDistProtoBufMessage(const DNSResponse&&, bool) = delete;
87f46425
RG
37
38 void setServerIdentity(const std::string& serverId);
39 void setRequestor(const ComboAddress& requestor);
40 void setResponder(const ComboAddress& responder);
41 void setRequestorPort(uint16_t port);
42 void setResponderPort(uint16_t port);
43 void setResponseCode(uint8_t rcode);
89addb82 44 void setType(pdns::ProtoZero::Message::MessageType type);
a261cdeb 45 void setHTTPVersion(pdns::ProtoZero::Message::HTTPVersion version);
87f46425
RG
46 void setBytes(size_t bytes);
47 void setTime(time_t sec, uint32_t usec);
48 void setQueryTime(time_t sec, uint32_t usec);
49 void setQuestion(const DNSName& name, uint16_t qtype, uint16_t qclass);
94219a42 50 void setEDNSSubnet(const Netmask& netmask);
87f46425
RG
51
52 void addTag(const std::string& strValue);
83dc6a38 53 void addMeta(const std::string& key, std::vector<std::string>&& strValues, const std::vector<int64_t>& intValues);
268f3909 54 void addRR(DNSName&& qname, uint16_t uType, uint16_t uClass, uint32_t uTTL, const std::string& data);
87f46425
RG
55
56 void serialize(std::string& data) const;
57
94219a42 58 [[nodiscard]] std::string toDebugString() const;
87f46425
RG
59
60private:
61 struct PBRecord
62 {
63 DNSName d_name;
64 std::string d_data;
65 uint32_t d_ttl;
66 uint16_t d_type;
67 uint16_t d_class;
68 };
69 struct PBQuestion
70 {
94219a42
CHB
71 PBQuestion(DNSName name, uint16_t type, uint16_t class_) :
72 d_name(std::move(name)), d_type(type), d_class(class_)
87f46425
RG
73 {
74 }
75
76 DNSName d_name;
77 uint16_t d_type;
78 uint16_t d_class;
79 };
80
81 std::vector<PBRecord> d_additionalRRs;
82 std::vector<std::string> d_additionalTags;
83dc6a38
RG
83 struct MetaValue
84 {
85 std::unordered_set<std::string> d_strings;
86 std::unordered_set<int64_t> d_integers;
87 };
88 std::unordered_map<std::string, MetaValue> d_metaTags;
87f46425 89
94219a42 90 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
87f46425
RG
91 const DNSQuestion& d_dq;
92 const DNSResponse* d_dr{nullptr};
93 const std::string* d_ServerIdentityRef{nullptr};
94
95 boost::optional<PBQuestion> d_question{boost::none};
96 boost::optional<std::string> d_serverIdentity{boost::none};
97 boost::optional<ComboAddress> d_requestor{boost::none};
98 boost::optional<ComboAddress> d_responder{boost::none};
99 boost::optional<Netmask> d_ednsSubnet{boost::none};
100 boost::optional<std::pair<time_t, uint32_t>> d_time{boost::none};
101 boost::optional<std::pair<time_t, uint32_t>> d_queryTime{boost::none};
102 boost::optional<size_t> d_bytes{boost::none};
103 boost::optional<uint8_t> d_rcode{boost::none};
104
89addb82 105 pdns::ProtoZero::Message::MessageType d_type{pdns::ProtoZero::Message::MessageType::DNSQueryType};
87f46425 106 bool d_includeCNAME{false};
d9d3f9c1 107};
454e9974 108
c02b6723
RG
109class ProtoBufMetaKey
110{
db86777f
CHB
111 enum class Type : uint8_t
112 {
113 SNI,
114 Pool,
115 B64Content,
116 DoHHeader,
117 DoHHost,
118 DoHPath,
119 DoHQueryString,
120 DoHScheme,
121 ProxyProtocolValue,
122 ProxyProtocolValues,
123 Tag,
124 Tags
125 };
c02b6723
RG
126
127 struct KeyTypeDescription
128 {
42225bb4
CHB
129 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
130 const std::string d_name;
131 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
132 const Type d_type;
133 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
134 const std::function<std::vector<std::string>(const DNSQuestion&, const std::string&, uint8_t)> d_func;
c02b6723
RG
135 bool d_prefix{false};
136 bool d_caseSensitive{true};
137 bool d_numeric{false};
138 };
139
db86777f
CHB
140 struct NameTag
141 {
142 };
143 struct TypeTag
144 {
145 };
c02b6723 146
94219a42 147 using TypeContainer = boost::multi_index_container<
c02b6723 148 KeyTypeDescription,
db86777f 149 indexed_by<
c02b6723 150 hashed_unique<tag<NameTag>, member<KeyTypeDescription, const std::string, &KeyTypeDescription::d_name>>,
94219a42 151 hashed_unique<tag<TypeTag>, member<KeyTypeDescription, const Type, &KeyTypeDescription::d_type>>>>;
c02b6723
RG
152
153 static const TypeContainer s_types;
154
155public:
156 ProtoBufMetaKey(const std::string& key);
157
94219a42
CHB
158 [[nodiscard]] const std::string& getName() const;
159 [[nodiscard]] std::vector<std::string> getValues(const DNSQuestion& dnsquestion) const;
db86777f 160
c02b6723
RG
161private:
162 std::string d_subKey;
163 uint8_t d_numericSubKey{0};
164 Type d_type;
165};
166
454e9974 167#endif /* DISABLE_PROTOBUF */