]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/qtype.hh
dnsdist: Fix DNS over plain HTTP broken by `reloadAllCertificates()`
[thirdparty/pdns.git] / pdns / qtype.hh
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 */
22 #pragma once
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "namespaces.hh"
28
29 /** The QType class is meant to deal easily with the different kind of resource types, like 'A', 'NS',
30 * 'CNAME' etcetera. These types have both a name and a number. This class can seamlessly move between
31 * them. Use it like this:
32
33 \code
34 QType t;
35 t="CNAME";
36 cout<<t.getCode()<<endl; // prints '5'
37 t=6;
38 cout<<t.toString()<<endl; // prints 'SOA'
39 \endcode
40
41 */
42
43 class QType
44 {
45 public:
46 QType(uint16_t qtype = 0) : code(qtype) {}
47 QType &operator=(const char *);
48 QType &operator=(const string &);
49
50 operator uint16_t() const {
51 return code;
52 }
53
54 const string toString() const;
55 uint16_t getCode() const
56 {
57 return code;
58 }
59
60 /**
61 * \brief Return whether we know the name of this type.
62 *
63 * This does not presume that we have an implemented a content representation for this type,
64 * for that please see DNSRecordContent::isRegisteredType().
65 */
66 bool isSupportedType() const;
67 /**
68 * \brief Whether the type is either a QTYPE or Meta-Type as defined by rfc6895 section 3.1.
69 *
70 * Note that ANY is 255 and falls outside the range.
71 */
72 bool isMetadataType() const;
73
74 static uint16_t chartocode(const char* p);
75
76 enum typeenum : uint16_t {
77 ENT = 0,
78 A = 1,
79 NS = 2,
80 CNAME = 5,
81 SOA = 6,
82 MB = 7,
83 MG = 8,
84 MR = 9,
85 PTR = 12,
86 HINFO = 13,
87 MINFO = 14,
88 MX = 15,
89 TXT = 16,
90 RP = 17,
91 AFSDB = 18,
92 SIG = 24,
93 KEY = 25,
94 AAAA = 28,
95 LOC = 29,
96 SRV = 33,
97 NAPTR = 35,
98 KX = 36,
99 CERT = 37,
100 A6 = 38,
101 DNAME = 39,
102 OPT = 41,
103 APL = 42,
104 DS = 43,
105 SSHFP = 44,
106 IPSECKEY = 45,
107 RRSIG = 46,
108 NSEC = 47,
109 DNSKEY = 48,
110 DHCID = 49,
111 NSEC3 = 50,
112 NSEC3PARAM = 51,
113 TLSA = 52,
114 SMIMEA = 53,
115 RKEY = 57,
116 CDS = 59,
117 CDNSKEY = 60,
118 OPENPGPKEY = 61,
119 CSYNC = 62,
120 ZONEMD = 63,
121 SVCB = 64,
122 HTTPS = 65,
123 SPF = 99,
124 NID = 104,
125 L32 = 105,
126 L64 = 106,
127 LP = 107,
128 EUI48 = 108,
129 EUI64 = 109,
130 TKEY = 249,
131 TSIG = 250,
132 IXFR = 251,
133 AXFR = 252,
134 MAILB = 253,
135 MAILA = 254,
136 ANY = 255,
137 URI = 256,
138 CAA = 257,
139 DLV = 32769,
140 ADDR = 65400,
141 #if !defined(RECURSOR)
142 ALIAS = 65401,
143 LUA = 65402
144 #endif
145 };
146
147 const static uint16_t rfc6895MetaLowerBound = 128;
148 const static uint16_t rfc6895MetaUpperBound = 254; // Note 255: ANY is not included
149 const static uint16_t rfc6895Reserved = 65535;
150
151 const static map<const string, uint16_t> names;
152 const static map<uint16_t, const string> numbers;
153
154 private:
155
156 uint16_t code;
157 };
158
159 // Define hash function on QType. See https://en.cppreference.com/w/cpp/utility/hash
160 namespace std {
161 template<> struct hash<QType> {
162 std::size_t operator()(QType qtype) const noexcept {
163 return std::hash<uint16_t>{}(qtype.getCode());
164 }
165 };
166 }
167
168 inline std::ostream& operator<<(std::ostream& stream, const QType& qtype)
169 {
170 return stream << qtype.toString();
171 }
172
173 // Used by e.g. boost multi-index
174 inline size_t hash_value(const QType qtype) {
175 return qtype.getCode();
176 }
177
178 struct QClass
179 {
180 constexpr QClass(uint16_t code = 0) : qclass(code) {}
181
182 constexpr operator uint16_t() const {
183 return qclass;
184 }
185 constexpr uint16_t getCode() const
186 {
187 return qclass;
188 }
189 const std::string toString() const;
190
191 static const QClass IN;
192 static const QClass CHAOS;
193 static const QClass NONE;
194 static const QClass ANY;
195
196 private:
197 uint16_t qclass;
198 };
199
200 constexpr QClass QClass::IN(1);
201 constexpr QClass QClass::CHAOS(3);
202 constexpr QClass QClass::NONE(254);
203 constexpr QClass QClass::ANY(255);
204
205 inline std::ostream& operator<<(std::ostream& s, QClass qclass)
206 {
207 return s << qclass.toString();
208 }