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