]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/dns.hh
rec: allow exception to proxy protocal usage for specific listen addresses
[thirdparty/pdns.git] / pdns / dns.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 #include "qtype.hh"
24 #include "dnsname.hh"
25 #include <ctime>
26 #include <sys/types.h>
27
28 #undef BADSIG // signal.h SIG_ERR
29
30 struct DNSRecord;
31
32 class RCode
33 {
34 public:
35 enum rcodes_ : uint8_t { NoError=0, FormErr=1, ServFail=2, NXDomain=3, NotImp=4, Refused=5, YXDomain=6, YXRRSet=7, NXRRSet=8, NotAuth=9, NotZone=10};
36 static std::string to_s(uint8_t rcode);
37 static std::string to_short_s(uint8_t rcode);
38 const static std::array<std::string, 24> rcodes_s;
39 };
40
41 class ERCode
42 {
43 public:
44 enum rcodes_ : uint16_t { BADVERS=16, BADSIG=16, BADKEY=17, BADTIME=18, BADMODE=19, BADNAME=20, BADALG=21, BADTRUNC=22, BADCOOKIE=23 };
45 static std::string to_s(uint16_t rcode);
46 };
47
48 class Opcode
49 {
50 public:
51 enum opcodes_ : uint8_t { Query=0, IQuery=1, Status=2, Notify=4, Update=5 };
52 static std::string to_s(uint8_t opcode);
53 };
54
55 //! This class represents a resource record
56 class DNSResourceRecord
57 {
58 public:
59 static DNSResourceRecord fromWire(const DNSRecord& wire);
60
61 enum Place : uint8_t
62 {
63 QUESTION = 0,
64 ANSWER = 1,
65 AUTHORITY = 2,
66 ADDITIONAL = 3
67 }; //!< Type describing the positioning within, say, a DNSPacket
68
69 void setContent(const string& content);
70 [[nodiscard]] string getZoneRepresentation(bool noDot = false) const;
71
72 // data
73 DNSName qname; //!< the name of this record, for example: www.powerdns.com
74 DNSName ordername;
75 DNSName wildcardname;
76 string content; //!< what this record points to. Example: 10.1.2.3
77
78 // Aligned on 8-byte boundaries on systems where time_t is 8 bytes and int
79 // is 4 bytes, aka modern linux on x86_64
80 time_t last_modified{}; //!< For autocalculating SOA serial numbers - the backend needs to fill this in
81
82 uint32_t ttl{}; //!< Time To Live of this record
83 uint32_t signttl{}; //!< If non-zero, use this TTL as original TTL in the RRSIG
84
85 int domain_id{-1}; //!< If a backend implements this, the domain_id of the zone this record is in
86 QType qtype; //!< qtype of this record, ie A, CNAME, MX etc
87 uint16_t qclass{1}; //!< class of this record
88
89 uint8_t scopeMask{};
90 bool auth{true};
91 bool disabled{};
92
93 bool operator==(const DNSResourceRecord& rhs);
94
95 bool operator<(const DNSResourceRecord& other) const
96 {
97 if (qname < other.qname) {
98 return true;
99 }
100 if (qname == other.qname) {
101 return (content < other.content);
102 }
103 return false;
104 }
105 };
106
107 #define GCCPACKATTRIBUTE __attribute__((packed))
108
109 struct dnsrecordheader
110 {
111 uint16_t d_type;
112 uint16_t d_class;
113 uint32_t d_ttl;
114 uint16_t d_clen;
115 } GCCPACKATTRIBUTE;
116
117 struct EDNS0Record
118 {
119 uint8_t extRCode, version;
120 uint16_t extFlags;
121 } GCCPACKATTRIBUTE;
122
123 static_assert(sizeof(EDNS0Record) == 4, "EDNS0Record size must be 4");
124
125 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
126 #include <machine/endian.h>
127 #elif __linux__ || __GNU__
128 # include <endian.h>
129
130 #else // with thanks to <arpa/nameser.h>
131
132 # define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
133 # define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
134 # define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */
135
136 #if defined(vax) || defined(ns32000) || defined(sun386) || defined(i386) || \
137 defined(__i386) || defined(__ia64) || defined(__amd64) || \
138 defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
139 defined(__alpha__) || defined(__alpha) || \
140 (defined(__Lynx__) && defined(__x86__))
141 # define BYTE_ORDER LITTLE_ENDIAN
142 #endif
143
144 #if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
145 defined(__sparc) || \
146 defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
147 defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
148 defined(apollo) || defined(__convex__) || defined(_CRAY) || \
149 defined(__hppa) || defined(__hp9000) || \
150 defined(__hp9000s300) || defined(__hp9000s700) || \
151 defined(__hp3000s900) || defined(MPE) || \
152 defined(BIT_ZERO_ON_LEFT) || defined(m68k) || \
153 (defined(__Lynx__) && \
154 (defined(__68k__) || defined(__sparc__) || defined(__powerpc__)))
155 # define BYTE_ORDER BIG_ENDIAN
156 #endif
157
158 #endif
159
160 struct dnsheader {
161 uint16_t id; /* query identification number */
162 #if BYTE_ORDER == BIG_ENDIAN
163 /* fields in third byte */
164 unsigned qr: 1; /* response flag */
165 unsigned opcode: 4; /* purpose of message */
166 unsigned aa: 1; /* authoritative answer */
167 unsigned tc: 1; /* truncated message */
168 unsigned rd: 1; /* recursion desired */
169 /* fields in fourth byte */
170 unsigned ra: 1; /* recursion available */
171 unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */
172 unsigned ad: 1; /* authentic data from named */
173 unsigned cd: 1; /* checking disabled by resolver */
174 unsigned rcode :4; /* response code */
175 #elif BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
176 /* fields in third byte */
177 unsigned rd :1; /* recursion desired */
178 unsigned tc :1; /* truncated message */
179 unsigned aa :1; /* authoritative answer */
180 unsigned opcode :4; /* purpose of message */
181 unsigned qr :1; /* response flag */
182 /* fields in fourth byte */
183 unsigned rcode :4; /* response code */
184 unsigned cd: 1; /* checking disabled by resolver */
185 unsigned ad: 1; /* authentic data from named */
186 unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */
187 unsigned ra :1; /* recursion available */
188 #endif
189 /* remaining bytes */
190 uint16_t qdcount; /* number of question entries */
191 uint16_t ancount; /* number of answer entries */
192 uint16_t nscount; /* number of authority entries */
193 uint16_t arcount; /* number of resource entries */
194 };
195
196 static_assert(sizeof(dnsheader) == 12, "dnsheader size must be 12");
197
198 class dnsheader_aligned
199 {
200 public:
201 static bool isMemoryAligned(const void* mem)
202 {
203 return reinterpret_cast<uintptr_t>(mem) % sizeof(uint32_t) == 0; // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
204 }
205
206 dnsheader_aligned(const void* mem)
207 {
208 if (isMemoryAligned(mem)) {
209 d_p = reinterpret_cast<const dnsheader*>(mem); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
210 }
211 else {
212 memcpy(&d_h, mem, sizeof(dnsheader));
213 d_p = &d_h;
214 }
215 }
216
217 [[nodiscard]] const dnsheader* get() const
218 {
219 return d_p;
220 }
221
222 [[nodiscard]] const dnsheader& operator*() const
223 {
224 return *d_p;
225 }
226
227 [[nodiscard]] const dnsheader* operator->() const
228 {
229 return d_p;
230 }
231
232 private:
233 dnsheader d_h{};
234 const dnsheader* d_p{};
235 };
236
237 inline uint16_t* getFlagsFromDNSHeader(dnsheader* dh)
238 {
239 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
240 return reinterpret_cast<uint16_t*>(reinterpret_cast<char*>(dh) + sizeof(uint16_t));
241 }
242
243 inline const uint16_t * getFlagsFromDNSHeader(const dnsheader* dh)
244 {
245 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
246 return reinterpret_cast<const uint16_t*>(reinterpret_cast<const char*>(dh) + sizeof(uint16_t));
247 }
248
249 #define DNS_TYPE_SIZE (2)
250 #define DNS_CLASS_SIZE (2)
251 #define DNS_TTL_SIZE (4)
252 #define DNS_RDLENGTH_SIZE (2)
253 #define EDNS_EXTENDED_RCODE_SIZE (1)
254 #define EDNS_VERSION_SIZE (1)
255 #define EDNS_OPTION_CODE_SIZE (2)
256 #define EDNS_OPTION_LENGTH_SIZE (2)
257
258 #if BYTE_ORDER == BIG_ENDIAN
259 #define FLAGS_RD_OFFSET (8)
260 #define FLAGS_CD_OFFSET (12)
261 #elif BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN
262 #define FLAGS_RD_OFFSET (0)
263 #define FLAGS_CD_OFFSET (12)
264 #endif
265
266 uint32_t hashQuestion(const uint8_t* packet, uint16_t len, uint32_t init, bool& ok);
267
268 struct TSIGTriplet
269 {
270 DNSName name, algo;
271 string secret;
272 };