Blob


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