Coverage Report

Created: 2025-08-05 06:59

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