]>
Commit | Line | Data |
---|---|---|
12c86877 BH |
1 | /* |
2 | PowerDNS Versatile Database Driven Nameserver | |
3 | Copyright (C) 2002 PowerDNS.COM BV | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
22dc646a BH |
6 | it under the terms of the GNU General Public License version 2 |
7 | as published by the Free Software Foundation | |
8 | ||
12c86877 BH |
9 | |
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
06bd9ccf | 17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
12c86877 | 18 | */ |
092f210a | 19 | // $Id$ |
12c86877 BH |
20 | /* (C) 2002 POWERDNS.COM BV */ |
21 | #ifndef DNS_HH | |
22 | #define DNS_HH | |
5f8fcf64 BH |
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> | |
12c86877 BH |
28 | #include "utility.hh" |
29 | #include "qtype.hh" | |
30 | #include <time.h> | |
31 | #include <sys/types.h> | |
32 | class DNSBackend; | |
33 | ||
34 | struct SOAData | |
35 | { | |
36 | string qname; | |
37 | string nameserver; | |
38 | string hostmaster; | |
092f210a BH |
39 | uint32_t ttl; |
40 | uint32_t serial; | |
41 | uint32_t refresh; | |
42 | uint32_t retry; | |
43 | uint32_t expire; | |
44 | uint32_t default_ttl; | |
12c86877 BH |
45 | int domain_id; |
46 | DNSBackend *db; | |
47 | }; | |
48 | ||
49 | ||
50 | class RCode | |
51 | { | |
52 | public: | |
c1a49c1c | 53 | enum rcodes_ { NoError=0, FormErr=1, ServFail=2, NXDomain=3, NotImp=4, Refused=5 }; |
12c86877 BH |
54 | }; |
55 | ||
56 | class Opcode | |
57 | { | |
58 | public: | |
59 | enum { Query=0, IQuery=1, Status=2, Notify=4, Update=5 }; | |
60 | }; | |
61 | ||
12c86877 BH |
62 | //! This class represents a resource record |
63 | class DNSResourceRecord | |
64 | { | |
65 | public: | |
a9af3782 | 66 | DNSResourceRecord() : qclass(1), priority(0), d_place(ANSWER) {}; |
12c86877 BH |
67 | ~DNSResourceRecord(){}; |
68 | ||
69 | string serialize() const; | |
70 | int unSerialize(const string &str); | |
71 | ||
72 | // data | |
73 | ||
74 | QType qtype; //!< qtype of this record, ie A, CNAME, MX etc | |
a9af3782 | 75 | uint16_t qclass; //!< class of this record |
12c86877 BH |
76 | string qname; //!< the name of this record, for example: www.powerdns.com |
77 | string content; //!< what this record points to. Example: 10.1.2.3 | |
092f210a BH |
78 | uint16_t priority; //!< For qtype's that support a priority or preference. Currently only MX |
79 | uint32_t ttl; //!< Time To Live of this record | |
12c86877 BH |
80 | int domain_id; //!< If a backend implements this, the domain_id of the zone this record is in |
81 | time_t last_modified; //!< For autocalculating SOA serial numbers - the backend needs to fill this in | |
82 | enum Place {QUESTION=0, ANSWER=1, AUTHORITY=2, ADDITIONAL=3}; //!< Type describing the positioning of a DNSResourceRecord within, say, a DNSPacket | |
83 | Place d_place; //!< This specifies where a record goes within the packet | |
84 | ||
728485ca BH |
85 | bool operator<(const DNSResourceRecord &b) const |
86 | { | |
c536ca8d | 87 | if(qname < b.qname) |
728485ca | 88 | return true; |
c536ca8d BH |
89 | if(qname == b.qname) |
90 | return(content < b.content); | |
728485ca BH |
91 | return false; |
92 | } | |
93 | ||
12c86877 BH |
94 | private: |
95 | string escape(const string &str) const; | |
96 | }; | |
97 | ||
009f9f55 | 98 | #ifdef _MSC_VER |
705f31ae BH |
99 | # pragma pack ( push ) |
100 | # pragma pack ( 1 ) | |
009f9f55 BH |
101 | # define GCCPACKATTRIBUTE |
102 | #else | |
103 | # define GCCPACKATTRIBUTE __attribute__((packed)) | |
104 | #endif | |
5a57d2ea BH |
105 | struct dnsrecordheader |
106 | { | |
107 | uint16_t d_type; | |
108 | uint16_t d_class; | |
109 | uint32_t d_ttl; | |
110 | uint16_t d_clen; | |
009f9f55 BH |
111 | } GCCPACKATTRIBUTE; |
112 | ||
113 | struct EDNS0Record | |
114 | { | |
4957a608 BH |
115 | uint8_t extRCode, version; |
116 | uint16_t Z; | |
009f9f55 BH |
117 | } GCCPACKATTRIBUTE; |
118 | #ifdef _MSC_VER | |
119 | #pragma pack (pop) | |
120 | #endif | |
121 | ||
8b3cfcd3 | 122 | enum { |
5a57d2ea BH |
123 | ns_t_invalid = 0, /* Cookie. */ |
124 | ns_t_a = 1, /* Host address. */ | |
125 | ns_t_ns = 2, /* Authoritative server. */ | |
126 | ns_t_md = 3, /* Mail destination. */ | |
127 | ns_t_mf = 4, /* Mail forwarder. */ | |
128 | ns_t_cname = 5, /* Canonical name. */ | |
129 | ns_t_soa = 6, /* Start of authority zone. */ | |
130 | ns_t_mb = 7, /* Mailbox domain name. */ | |
131 | ns_t_mg = 8, /* Mail group member. */ | |
132 | ns_t_mr = 9, /* Mail rename name. */ | |
133 | ns_t_null = 10, /* Null resource record. */ | |
134 | ns_t_wks = 11, /* Well known service. */ | |
135 | ns_t_ptr = 12, /* Domain name pointer. */ | |
136 | ns_t_hinfo = 13, /* Host information. */ | |
137 | ns_t_minfo = 14, /* Mailbox information. */ | |
138 | ns_t_mx = 15, /* Mail routing information. */ | |
139 | ns_t_txt = 16, /* Text strings. */ | |
140 | ns_t_rp = 17, /* Responsible person. */ | |
141 | ns_t_afsdb = 18, /* AFS cell database. */ | |
142 | ns_t_x25 = 19, /* X_25 calling address. */ | |
143 | ns_t_isdn = 20, /* ISDN calling address. */ | |
8b3cfcd3 | 144 | ns_t_rt = 21, /* Router. */ |
5a57d2ea BH |
145 | ns_t_nsap = 22, /* NSAP address. */ |
146 | ns_t_nsap_ptr = 23, /* Reverse NSAP lookup (deprecated). */ | |
147 | ns_t_sig = 24, /* Security signature. */ | |
148 | ns_t_key = 25, /* Security key. */ | |
149 | ns_t_px = 26, /* X.400 mail mapping. */ | |
150 | ns_t_gpos = 27, /* Geographical position (withdrawn). */ | |
151 | ns_t_aaaa = 28, /* Ip6 Address. */ | |
152 | ns_t_loc = 29, /* Location Information. */ | |
153 | ns_t_nxt = 30, /* Next domain (security). */ | |
154 | ns_t_eid = 31, /* Endpoint identifier. */ | |
155 | ns_t_nimloc = 32, /* Nimrod Locator. */ | |
156 | ns_t_srv = 33, /* Server Selection. */ | |
157 | ns_t_atma = 34, /* ATM Address */ | |
158 | ns_t_naptr = 35, /* Naming Authority PoinTeR */ | |
159 | ns_t_kx = 36, /* Key Exchange */ | |
160 | ns_t_cert = 37, /* Certification record */ | |
161 | ns_t_a6 = 38, /* IPv6 address (deprecates AAAA) */ | |
162 | ns_t_dname = 39, /* Non-terminal DNAME (for IPv6) */ | |
163 | ns_t_sink = 40, /* Kitchen sink (experimentatl) */ | |
164 | ns_t_opt = 41, /* EDNS0 option (meta-RR) */ | |
165 | ns_t_tsig = 250, /* Transaction signature. */ | |
166 | ns_t_ixfr = 251, /* Incremental zone transfer. */ | |
167 | ns_t_axfr = 252, /* Transfer zone of authority. */ | |
168 | ns_t_mailb = 253, /* Transfer mailbox records. */ | |
169 | ns_t_maila = 254, /* Transfer mail agent records. */ | |
170 | ns_t_any = 255, /* Wildcard match. */ | |
171 | }; | |
172 | ||
009f9f55 | 173 | #ifdef WIN32 |
5a57d2ea BH |
174 | #define BYTE_ORDER 1 |
175 | #define LITTLE_ENDIAN 1 | |
1f56ef28 | 176 | #elif __FreeBSD__ || __APPLE__ |
009f9f55 | 177 | #include <machine/endian.h> |
a1a344bf BH |
178 | #elif __linux__ |
179 | # include <endian.h> | |
180 | ||
181 | #else // with thanks to <arpa/nameser.h> | |
182 | ||
183 | # define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */ | |
184 | # define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */ | |
185 | # define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp) */ | |
186 | ||
187 | #if defined(vax) || defined(ns32000) || defined(sun386) || defined(i386) || \ | |
188 | defined(__i386) || defined(__ia64) || defined(__amd64) || \ | |
189 | defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \ | |
190 | defined(__alpha__) || defined(__alpha) || \ | |
191 | (defined(__Lynx__) && defined(__x86__)) | |
192 | # define BYTE_ORDER LITTLE_ENDIAN | |
193 | #endif | |
194 | ||
195 | #if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \ | |
196 | defined(__sparc) || \ | |
197 | defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \ | |
198 | defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\ | |
199 | defined(apollo) || defined(__convex__) || defined(_CRAY) || \ | |
200 | defined(__hppa) || defined(__hp9000) || \ | |
201 | defined(__hp9000s300) || defined(__hp9000s700) || \ | |
202 | defined(__hp3000s900) || defined(MPE) || \ | |
203 | defined(BIT_ZERO_ON_LEFT) || defined(m68k) || \ | |
204 | (defined(__Lynx__) && \ | |
205 | (defined(__68k__) || defined(__sparc__) || defined(__powerpc__))) | |
206 | # define BYTE_ORDER BIG_ENDIAN | |
207 | #endif | |
208 | ||
5a57d2ea BH |
209 | #endif |
210 | ||
211 | struct dnsheader { | |
212 | unsigned id :16; /* query identification number */ | |
213 | #if BYTE_ORDER == BIG_ENDIAN | |
214 | /* fields in third byte */ | |
215 | unsigned qr: 1; /* response flag */ | |
216 | unsigned opcode: 4; /* purpose of message */ | |
217 | unsigned aa: 1; /* authoritive answer */ | |
218 | unsigned tc: 1; /* truncated message */ | |
219 | unsigned rd: 1; /* recursion desired */ | |
220 | /* fields in fourth byte */ | |
221 | unsigned ra: 1; /* recursion available */ | |
222 | unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */ | |
223 | unsigned ad: 1; /* authentic data from named */ | |
224 | unsigned cd: 1; /* checking disabled by resolver */ | |
225 | unsigned rcode :4; /* response code */ | |
226 | #endif | |
227 | #if BYTE_ORDER == LITTLE_ENDIAN || BYTE_ORDER == PDP_ENDIAN | |
228 | /* fields in third byte */ | |
229 | unsigned rd :1; /* recursion desired */ | |
230 | unsigned tc :1; /* truncated message */ | |
231 | unsigned aa :1; /* authoritive answer */ | |
232 | unsigned opcode :4; /* purpose of message */ | |
233 | unsigned qr :1; /* response flag */ | |
234 | /* fields in fourth byte */ | |
235 | unsigned rcode :4; /* response code */ | |
236 | unsigned cd: 1; /* checking disabled by resolver */ | |
237 | unsigned ad: 1; /* authentic data from named */ | |
238 | unsigned unused :1; /* unused bits (MBZ as of 4.9.3a3) */ | |
239 | unsigned ra :1; /* recursion available */ | |
240 | #endif | |
241 | /* remaining bytes */ | |
242 | unsigned qdcount :16; /* number of question entries */ | |
243 | unsigned ancount :16; /* number of answer entries */ | |
244 | unsigned nscount :16; /* number of authority entries */ | |
245 | unsigned arcount :16; /* number of resource entries */ | |
246 | }; | |
247 | ||
248 | ||
12c86877 BH |
249 | #define L theL() |
250 | extern time_t s_starttime; | |
3ea54bf0 | 251 | std::string questionExpand(const char* packet, uint16_t len, uint16_t& type); |
12c86877 | 252 | #endif |