Blame


1 c45084ea 2005-11-29 pbug /*
2 324df6f2 2014-04-13 pjp * Copyright (c) 2002-2014 Peter J. Philipp
3 c45084ea 2005-11-29 pbug * All rights reserved.
4 c45084ea 2005-11-29 pbug *
5 c45084ea 2005-11-29 pbug * Redistribution and use in source and binary forms, with or without
6 c45084ea 2005-11-29 pbug * modification, are permitted provided that the following conditions
7 c45084ea 2005-11-29 pbug * are met:
8 c45084ea 2005-11-29 pbug * 1. Redistributions of source code must retain the above copyright
9 c45084ea 2005-11-29 pbug * notice, this list of conditions and the following disclaimer.
10 c45084ea 2005-11-29 pbug * 2. Redistributions in binary form must reproduce the above copyright
11 c45084ea 2005-11-29 pbug * notice, this list of conditions and the following disclaimer in the
12 c45084ea 2005-11-29 pbug * documentation and/or other materials provided with the distribution.
13 c45084ea 2005-11-29 pbug * 3. The name of the author may not be used to endorse or promote products
14 c45084ea 2005-11-29 pbug * derived from this software without specific prior written permission
15 c45084ea 2005-11-29 pbug *
16 c45084ea 2005-11-29 pbug * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 c45084ea 2005-11-29 pbug * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 c45084ea 2005-11-29 pbug * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 c45084ea 2005-11-29 pbug * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 c45084ea 2005-11-29 pbug * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 c45084ea 2005-11-29 pbug * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 c45084ea 2005-11-29 pbug * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 c45084ea 2005-11-29 pbug * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 c45084ea 2005-11-29 pbug * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 c45084ea 2005-11-29 pbug * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 c45084ea 2005-11-29 pbug *
27 c45084ea 2005-11-29 pbug */
28 c45084ea 2005-11-29 pbug #ifndef _DNS_H
29 c45084ea 2005-11-29 pbug #define _DNS_H
30 c45084ea 2005-11-29 pbug
31 c45084ea 2005-11-29 pbug
32 c45084ea 2005-11-29 pbug /* RFC 1035 - page 26 */
33 c45084ea 2005-11-29 pbug
34 c45084ea 2005-11-29 pbug struct dns_header {
35 c45084ea 2005-11-29 pbug u_int16_t id; /* ID of header */
36 c45084ea 2005-11-29 pbug u_int16_t query;
37 c45084ea 2005-11-29 pbug u_int16_t question; /* # of question entries */
38 c45084ea 2005-11-29 pbug u_int16_t answer; /* # of answer RR's */
39 c45084ea 2005-11-29 pbug u_int16_t nsrr; /* # of NS RR's */
40 c45084ea 2005-11-29 pbug u_int16_t additional; /* # additional RR's */
41 c45084ea 2005-11-29 pbug };
42 c45084ea 2005-11-29 pbug
43 c45084ea 2005-11-29 pbug struct dns_hints {
44 c45084ea 2005-11-29 pbug int proto;
45 c45084ea 2005-11-29 pbug u_int16_t id;
46 c45084ea 2005-11-29 pbug u_int16_t query;
47 c45084ea 2005-11-29 pbug u_int16_t question;
48 c45084ea 2005-11-29 pbug u_int16_t answer;
49 c45084ea 2005-11-29 pbug u_int16_t nsrr;
50 c45084ea 2005-11-29 pbug u_int16_t additional;
51 c45084ea 2005-11-29 pbug };
52 c45084ea 2005-11-29 pbug
53 c45084ea 2005-11-29 pbug /*
54 c45084ea 2005-11-29 pbug * resource record structure
55 c45084ea 2005-11-29 pbug * RFC 1035 - page 9
56 c45084ea 2005-11-29 pbug */
57 c45084ea 2005-11-29 pbug
58 c45084ea 2005-11-29 pbug struct dns_rr {
59 c45084ea 2005-11-29 pbug char *name; /* name of zone */
60 c45084ea 2005-11-29 pbug char *question; /* pointer to question */
61 c45084ea 2005-11-29 pbug u_int16_t type; /* type of RR */
62 c45084ea 2005-11-29 pbug u_int16_t class; /* class of reply */
63 c45084ea 2005-11-29 pbug u_int32_t ttl; /* ttl of record */
64 c45084ea 2005-11-29 pbug u_int16_t rdlen; /* length of record */
65 c45084ea 2005-11-29 pbug char *rdata; /* data of record */
66 c45084ea 2005-11-29 pbug };
67 c45084ea 2005-11-29 pbug
68 d64396b7 2014-05-09 pjp /*
69 d64396b7 2014-05-09 pjp * EDNS0 OPT RR, based on dns_rr
70 d64396b7 2014-05-09 pjp * RFC 6891 - page 7
71 d64396b7 2014-05-09 pjp */
72 d64396b7 2014-05-09 pjp
73 d64396b7 2014-05-09 pjp struct dns_optrr {
74 cd18f393 2014-05-10 pjp char name[1]; /* always 0 */
75 d64396b7 2014-05-09 pjp u_int16_t type; /* must be 41 */
76 d64396b7 2014-05-09 pjp u_int16_t class; /* UDP payload size (4096) */
77 d64396b7 2014-05-09 pjp u_int32_t ttl; /* extended RCODE */
78 d64396b7 2014-05-09 pjp u_int16_t rdlen; /* length of all RDATA */
79 cd18f393 2014-05-10 pjp char rdata[0]; /* attribute, value pairs */
80 cd18f393 2014-05-10 pjp }__attribute__((packed));
81 d64396b7 2014-05-09 pjp
82 c45084ea 2005-11-29 pbug /* RFC 1035 - page 28 */
83 c45084ea 2005-11-29 pbug struct dns_question_hdr {
84 c45084ea 2005-11-29 pbug char *name;
85 c45084ea 2005-11-29 pbug u_int namelen;
86 c45084ea 2005-11-29 pbug u_int16_t qtype;
87 c45084ea 2005-11-29 pbug u_int16_t qclass;
88 c45084ea 2005-11-29 pbug };
89 c45084ea 2005-11-29 pbug
90 c45084ea 2005-11-29 pbug
91 c45084ea 2005-11-29 pbug
92 c45084ea 2005-11-29 pbug /*
93 c45084ea 2005-11-29 pbug * flags RFC 1035, page 26
94 c45084ea 2005-11-29 pbug */
95 c45084ea 2005-11-29 pbug
96 c45084ea 2005-11-29 pbug #define DNS_REPLY 0x8000 /* if set response if not set query */
97 58eafea4 2014-05-17 pjp #define DNS_NOTIFY 0x2000 /* a NOTIFY query RFC 1996 */
98 c45084ea 2005-11-29 pbug #define DNS_SREQ 0x1000 /* if set a server status request (STATUS) */
99 c45084ea 2005-11-29 pbug #define DNS_INV 0x800 /* if set an inverse query */
100 c45084ea 2005-11-29 pbug #define DNS_AUTH 0x400 /* Authoritative Answer (AA) in replies */
101 c45084ea 2005-11-29 pbug #define DNS_TRUNC 0x200 /* Truncated (TC) */
102 c45084ea 2005-11-29 pbug #define DNS_RECURSE 0x100 /* if set Recursion Desired (RD) */
103 c45084ea 2005-11-29 pbug #define DNS_RECAVAIL 0x80 /* if set Recursion Available (RA) */
104 9ee2cd15 2014-09-20 pjp #define DNS_BADTIME 0x12 /* RCODE (18) BADTIME RFC 2845 p. 3 */
105 9ee2cd15 2014-09-20 pjp #define DNS_BADKEY 0x11 /* RCODE (17) BADKEY RFC 2845 p. 3 */
106 9ee2cd15 2014-09-20 pjp #define DNS_BADSIG 0x10 /* RCODE (16) BADSIG RFC 2845 p. 3 */
107 c45084ea 2005-11-29 pbug #define DNS_REFUSED 0x5 /* RCODE - Refused */
108 c45084ea 2005-11-29 pbug #define DNS_NOTIMPL 0x4 /* RCODE - Not Implemented */
109 84ea0365 2009-03-06 pbug #define DNS_NAMEERR 0x3 /* RCODE - Name Error, NXDOMAIN */
110 c45084ea 2005-11-29 pbug #define DNS_SERVFAIL 0x2 /* RCODE - Server Failure */
111 c45084ea 2005-11-29 pbug #define DNS_FORMATERR 0x1 /* RCODE - Format Error */
112 c45084ea 2005-11-29 pbug #define DNS_NOERR 0x0 /* RCODE - No error */
113 c45084ea 2005-11-29 pbug
114 c45084ea 2005-11-29 pbug /*
115 c45084ea 2005-11-29 pbug * macros to set flags (must be converted to network byte order after)
116 c45084ea 2005-11-29 pbug */
117 c45084ea 2005-11-29 pbug
118 c45084ea 2005-11-29 pbug #define SET_DNS_REPLY(x) ((x)->query |= (DNS_REPLY))
119 c7eceeba 2010-09-16 pbug #define SET_DNS_QUERY(x) ((x)->query &= ~(DNS_REPLY))
120 58eafea4 2014-05-17 pjp #define SET_DNS_NOTIFY(x) ((x)->query |= (DNS_NOTIFY))
121 c45084ea 2005-11-29 pbug #define SET_DNS_STATUS_REQ(x) ((x)->query |= (DNS_SREQ))
122 c45084ea 2005-11-29 pbug #define SET_DNS_INVERSE_QUERY(x) ((x)->query |= (DNS_INV))
123 c45084ea 2005-11-29 pbug #define SET_DNS_AUTHORITATIVE(x) ((x)->query |= (DNS_AUTH))
124 c45084ea 2005-11-29 pbug #define SET_DNS_TRUNCATION(x) ((x)->query |= (DNS_TRUNC))
125 c45084ea 2005-11-29 pbug #define SET_DNS_RECURSION(x) ((x)->query |= (DNS_RECURSE))
126 c45084ea 2005-11-29 pbug #define SET_DNS_RECURSION_AVAIL(x) ((x)->query |= (DNS_RECAVAIL))
127 c45084ea 2005-11-29 pbug #define SET_DNS_RCODE_REFUSED(x) ((x)->query |= (DNS_REFUSED))
128 c45084ea 2005-11-29 pbug #define SET_DNS_RCODE_NOTIMPL(x) ((x)->query |= (DNS_NOTIMPL))
129 c45084ea 2005-11-29 pbug #define SET_DNS_RCODE_NAMEERR(x) ((x)->query |= (DNS_NAMEERR))
130 c45084ea 2005-11-29 pbug #define SET_DNS_RCODE_SERVFAIL(x) ((x)->query |= (DNS_SERVFAIL))
131 f665f992 2005-12-01 pbug #define SET_DNS_RCODE_FORMATERR(x) ((x)->query |= (DNS_FORMATERR))
132 c45084ea 2005-11-29 pbug #define SET_DNS_RCODE_NOERR(x) ((x)->query |= (DNS_NOERR))
133 c45084ea 2005-11-29 pbug
134 58eafea4 2014-05-17 pjp #define UNSET_DNS_NOTIFY(x) ((x)->query &= ~(DNS_NOTIFY))
135 c7eceeba 2010-09-16 pbug #define UNSET_DNS_STATUS_REQ(x) ((x)->query &= ~(DNS_SREQ))
136 c7eceeba 2010-09-16 pbug #define UNSET_DNS_INVERSE_QUERY(x) ((x)->query &= ~(DNS_INV))
137 c7eceeba 2010-09-16 pbug #define UNSET_DNS_AUTHORITATIVE(x) ((x)->query &= ~(DNS_AUTH))
138 c7eceeba 2010-09-16 pbug #define UNSET_DNS_TRUNCATION(x) ((x)->query &= ~(DNS_TRUNC))
139 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RECURSION(x) ((x)->query &= ~(DNS_RECURSE))
140 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RECURSION_AVAIL(x) ((x)->query &= ~(DNS_RECAVAIL))
141 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RCODE_REFUSED(x) ((x)->query &= ~(DNS_REFUSED))
142 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RCODE_NOTIMPL(x) ((x)->query &= ~(DNS_NOTIMPL))
143 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RCODE_NAMEERR(x) ((x)->query &= ~(DNS_NAMEERR))
144 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RCODE_SERVFAIL(x) ((x)->query &= ~(DNS_SERVFAIL))
145 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RCODE_FORMATERR(x) ((x)->query &= ~(DNS_FORMATERR))
146 c7eceeba 2010-09-16 pbug #define UNSET_DNS_RCODE_NOERR(x) ((x)->query &= ~(DNS_NOERR))
147 c7eceeba 2010-09-16 pbug
148 d29dc98c 2014-05-09 pjp /* DNSSEC/EDNS0 options RFC 3225 */
149 d29dc98c 2014-05-09 pjp
150 d29dc98c 2014-05-09 pjp #define DNSSEC_OK 0x8000
151 d29dc98c 2014-05-09 pjp
152 d29dc98c 2014-05-09 pjp #define SET_DNS_ERCODE_DNSSECOK(x) ((x)->ttl |= (DNSSEC_OK))
153 d29dc98c 2014-05-09 pjp #define UNSET_DNS_ERCODE_DNSSECOK(x) ((x)->ttl &= ~(DNSSEC_OK))
154 d29dc98c 2014-05-09 pjp
155 c45084ea 2005-11-29 pbug /* DNS types - RFC 1035 page 12 */
156 c45084ea 2005-11-29 pbug
157 c45084ea 2005-11-29 pbug #define DNS_TYPE_A 1
158 c45084ea 2005-11-29 pbug #define DNS_TYPE_NS 2
159 c45084ea 2005-11-29 pbug #define DNS_TYPE_CNAME 5
160 c45084ea 2005-11-29 pbug #define DNS_TYPE_SOA 6
161 c45084ea 2005-11-29 pbug #define DNS_TYPE_PTR 12
162 c45084ea 2005-11-29 pbug #define DNS_TYPE_MX 15
163 c45084ea 2005-11-29 pbug #define DNS_TYPE_TXT 16
164 c45084ea 2005-11-29 pbug
165 6f8190d3 2012-04-30 pbug #define DNS_TYPE_SRV 33 /* RFC 2782, page 8 */
166 1f1faf13 2014-05-11 pjp #define DNS_TYPE_NAPTR 35 /* RFC 2915, page 3 */
167 d64396b7 2014-05-09 pjp #define DNS_TYPE_OPT 41 /* RFC 6891, page 7 */
168 df34d218 2014-04-21 pjp #define DNS_TYPE_SSHFP 44 /* RFC 4255 */
169 6f8190d3 2012-04-30 pbug
170 f5989689 2014-04-21 pjp #define DNS_TYPE_SPF 99 /* RFC 4408 */
171 f5989689 2014-04-21 pjp
172 0d1b8743 2012-01-27 pbug #define DNS_TYPE_TSIG 250 /* RFC 2845, page 3 */
173 8d753d40 2011-09-22 pbug #define DNS_TYPE_IXFR 251 /* RFC 1995, page 2 */
174 f98bb34d 2011-09-19 pbug #define DNS_TYPE_AXFR 252 /* RFC 5936, page 10 */
175 0d95ac68 2010-12-27 pbug #define DNS_TYPE_ANY 255
176 0d95ac68 2010-12-27 pbug
177 ed247332 2010-03-09 pbug /* DNS types 0xff00 -> 0xfffe (private use) RFC 5395, page 8 */
178 ed247332 2010-03-09 pbug
179 5841985a 2010-03-18 pbug #define DNS_TYPE_BALANCE 0xfffe /* split horizon dns */
180 5841985a 2010-03-18 pbug #define DNS_TYPE_DELEGATE 0xfffd /* ns delegations */
181 b0a72376 2010-04-01 pbug #define DNS_TYPE_HINT 0xfffc /* root hint */
182 ed247332 2010-03-09 pbug
183 c45084ea 2005-11-29 pbug /* quad A - RFC 3596 */
184 c45084ea 2005-11-29 pbug #define DNS_TYPE_AAAA 28
185 c45084ea 2005-11-29 pbug
186 c45084ea 2005-11-29 pbug
187 c45084ea 2005-11-29 pbug /* DNS CLASSES - RFC 1035 page 13 */
188 c45084ea 2005-11-29 pbug
189 c45084ea 2005-11-29 pbug #define DNS_CLASS_IN 1 /* internet */
190 c45084ea 2005-11-29 pbug #define DNS_CLASS_CH 3 /* chaos */
191 c45084ea 2005-11-29 pbug #define DNS_CLASS_HS 4 /* hesiod */
192 c45084ea 2005-11-29 pbug
193 c45084ea 2005-11-29 pbug #define DNS_CLASS_ANY 255 /* any class */
194 c45084ea 2005-11-29 pbug
195 c45084ea 2005-11-29 pbug /* limits */
196 c45084ea 2005-11-29 pbug
197 c45084ea 2005-11-29 pbug #define DNS_MAXLABEL 63
198 c45084ea 2005-11-29 pbug #define DNS_MAXNAME 255
199 c45084ea 2005-11-29 pbug #define DNS_MAXUDP 512
200 c45084ea 2005-11-29 pbug
201 df34d218 2014-04-21 pjp /* SSHFP fingerprint sizes */
202 c45084ea 2005-11-29 pbug
203 d67bdb62 2014-04-22 pjp #define DNS_SSHFP_SIZE_SHA1 20 /* RFC 4255 */
204 d67bdb62 2014-04-22 pjp #define DNS_SSHFP_SIZE_SHA256 32 /* RFC 6594 */
205 df34d218 2014-04-21 pjp
206 df34d218 2014-04-21 pjp
207 c45084ea 2005-11-29 pbug struct question {
208 c45084ea 2005-11-29 pbug struct dns_question_hdr *hdr;
209 c45084ea 2005-11-29 pbug char *converted_name;
210 d64396b7 2014-05-09 pjp int edns0len;
211 d29dc98c 2014-05-09 pjp int dnssecok;
212 c45084ea 2005-11-29 pbug };
213 c45084ea 2005-11-29 pbug
214 c45084ea 2005-11-29 pbug #endif /* DNS_H */