1 /*
2  * Copyright (c) 1995 - 2002 Kungliga Tekniska H�gskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /* $Id: resolve.h,v 1.24 2005/04/12 11:29:02 lha Exp $ */
35 
36 #ifndef __RESOLVE_H__
37 #define __RESOLVE_H__
38 
39 #ifndef ROKEN_LIB_FUNCTION
40 #ifdef _WIN32
41 #define ROKEN_LIB_FUNCTION _stdcall
42 #else
43 #define ROKEN_LIB_FUNCTION
44 #endif
45 #endif
46 
47 typedef enum {
48 	rk_ns_t_invalid = 0,	/* Cookie. */
49 	rk_ns_t_a = 1,		/* Host address. */
50 	rk_ns_t_ns = 2,		/* Authoritative server. */
51 	rk_ns_t_md = 3,		/* Mail destination. */
52 	rk_ns_t_mf = 4,		/* Mail forwarder. */
53 	rk_ns_t_cname = 5,	/* Canonical name. */
54 	rk_ns_t_soa = 6,	/* Start of authority zone. */
55 	rk_ns_t_mb = 7,		/* Mailbox domain name. */
56 	rk_ns_t_mg = 8,		/* Mail group member. */
57 	rk_ns_t_mr = 9,		/* Mail rename name. */
58 	rk_ns_t_null = 10,	/* Null resource record. */
59 	rk_ns_t_wks = 11,	/* Well known service. */
60 	rk_ns_t_ptr = 12,	/* Domain name pointer. */
61 	rk_ns_t_hinfo = 13,	/* Host information. */
62 	rk_ns_t_minfo = 14,	/* Mailbox information. */
63 	rk_ns_t_mx = 15,	/* Mail routing information. */
64 	rk_ns_t_txt = 16,	/* Text strings. */
65 	rk_ns_t_rp = 17,	/* Responsible person. */
66 	rk_ns_t_afsdb = 18,	/* AFS cell database. */
67 	rk_ns_t_x25 = 19,	/* X_25 calling address. */
68 	rk_ns_t_isdn = 20,	/* ISDN calling address. */
69 	rk_ns_t_rt = 21,	/* Router. */
70 	rk_ns_t_nsap = 22,	/* NSAP address. */
71 	rk_ns_t_nsap_ptr = 23,	/* Reverse NSAP lookup (deprecated). */
72 	rk_ns_t_sig = 24,	/* Security signature. */
73 	rk_ns_t_key = 25,	/* Security key. */
74 	rk_ns_t_px = 26,	/* X.400 mail mapping. */
75 	rk_ns_t_gpos = 27,	/* Geographical position (withdrawn). */
76 	rk_ns_t_aaaa = 28,	/* Ip6 Address. */
77 	rk_ns_t_loc = 29,	/* Location Information. */
78 	rk_ns_t_nxt = 30,	/* Next domain (security). */
79 	rk_ns_t_eid = 31,	/* Endpoint identifier. */
80 	rk_ns_t_nimloc = 32,	/* Nimrod Locator. */
81 	rk_ns_t_srv = 33,	/* Server Selection. */
82 	rk_ns_t_atma = 34,	/* ATM Address */
83 	rk_ns_t_naptr = 35,	/* Naming Authority PoinTeR */
84 	rk_ns_t_kx = 36,	/* Key Exchange */
85 	rk_ns_t_cert = 37,	/* Certification record */
86 	rk_ns_t_a6 = 38,	/* IPv6 address (deprecates AAAA) */
87 	rk_ns_t_dname = 39,	/* Non-terminal DNAME (for IPv6) */
88 	rk_ns_t_sink = 40,	/* Kitchen sink (experimentatl) */
89 	rk_ns_t_opt = 41,	/* EDNS0 option (meta-RR) */
90 	rk_ns_t_apl = 42,	/* Address prefix list (RFC 3123) */
91 	rk_ns_t_ds = 43,	/* Delegation Signer (RFC 3658) */
92 	rk_ns_t_sshfp = 44,	/* SSH fingerprint */
93 	rk_ns_t_tkey = 249,	/* Transaction key */
94 	rk_ns_t_tsig = 250,	/* Transaction signature. */
95 	rk_ns_t_ixfr = 251,	/* Incremental zone transfer. */
96 	rk_ns_t_axfr = 252,	/* Transfer zone of authority. */
97 	rk_ns_t_mailb = 253,	/* Transfer mailbox records. */
98 	rk_ns_t_maila = 254,	/* Transfer mail agent records. */
99 	rk_ns_t_any = 255,	/* Wildcard match. */
100 	rk_ns_t_zxfr = 256,	/* BIND-specific, nonstandard. */
101 	rk_ns_t_max = 65536
102 } rk_ns_type;
103 
104 /* We use these, but they are not always present in <arpa/nameser.h> */
105 
106 #ifndef C_IN
107 #define C_IN		1
108 #endif
109 
110 #ifndef T_A
111 #define T_A		1
112 #endif
113 #ifndef T_NS
114 #define T_NS		2
115 #endif
116 #ifndef T_CNAME
117 #define T_CNAME		5
118 #endif
119 #ifndef T_SOA
120 #define T_SOA		5
121 #endif
122 #ifndef T_PTR
123 #define T_PTR		12
124 #endif
125 #ifndef T_MX
126 #define T_MX		15
127 #endif
128 #ifndef T_TXT
129 #define T_TXT		16
130 #endif
131 #ifndef T_AFSDB
132 #define T_AFSDB		18
133 #endif
134 #ifndef T_SIG
135 #define T_SIG		24
136 #endif
137 #ifndef T_KEY
138 #define T_KEY		25
139 #endif
140 #ifndef T_AAAA
141 #define T_AAAA		28
142 #endif
143 #ifndef T_SRV
144 #define T_SRV		33
145 #endif
146 #ifndef T_NAPTR
147 #define T_NAPTR		35
148 #endif
149 #ifndef T_CERT
150 #define T_CERT		37
151 #endif
152 #ifndef T_SSHFP
153 #define T_SSHFP		44
154 #endif
155 
156 #ifndef MAXDNAME
157 #define MAXDNAME	1025
158 #endif
159 
160 #define dns_query		rk_dns_query
161 #define mx_record		rk_mx_record
162 #define srv_record		rk_srv_record
163 #define key_record		rk_key_record
164 #define sig_record		rk_sig_record
165 #define cert_record		rk_cert_record
166 #define sshfp_record		rk_sshfp_record
167 #define resource_record		rk_resource_record
168 #define dns_reply		rk_dns_reply
169 
170 #define dns_lookup		rk_dns_lookup
171 #define dns_free_data		rk_dns_free_data
172 #define dns_string_to_type	rk_dns_string_to_type
173 #define dns_type_to_string	rk_dns_type_to_string
174 #define dns_srv_order		rk_dns_srv_order
175 
176 struct dns_query{
177     char *domain;
178     unsigned type;
179     unsigned class;
180 };
181 
182 struct mx_record{
183     unsigned  preference;
184     char domain[1];
185 };
186 
187 struct srv_record{
188     unsigned priority;
189     unsigned weight;
190     unsigned port;
191     char target[1];
192 };
193 
194 struct key_record {
195     unsigned flags;
196     unsigned protocol;
197     unsigned algorithm;
198     size_t   key_len;
199     u_char   key_data[1];
200 };
201 
202 struct sig_record {
203     unsigned type;
204     unsigned algorithm;
205     unsigned labels;
206     unsigned orig_ttl;
207     unsigned sig_expiration;
208     unsigned sig_inception;
209     unsigned key_tag;
210     char     *signer;
211     unsigned sig_len;
212     char     sig_data[1];	/* also includes signer */
213 };
214 
215 struct cert_record {
216     unsigned type;
217     unsigned tag;
218     unsigned algorithm;
219     size_t   cert_len;
220     u_char   cert_data[1];
221 };
222 
223 struct sshfp_record {
224     unsigned algorithm;
225     unsigned type;
226     size_t   sshfp_len;
227     u_char   sshfp_data[1];
228 };
229 
230 struct ds_record {
231     unsigned key_tag;
232     unsigned algorithm;
233     unsigned digest_type;
234     unsigned digest_len;
235     u_char digest_data[1];
236 };
237 
238 struct resource_record{
239     char *domain;
240     unsigned type;
241     unsigned class;
242     unsigned ttl;
243     unsigned size;
244     union {
245 	void *data;
246 	struct mx_record *mx;
247 	struct mx_record *afsdb; /* mx and afsdb are identical */
248 	struct srv_record *srv;
249 	struct in_addr *a;
250 	char *txt;
251 	struct key_record *key;
252 	struct cert_record *cert;
253 	struct sig_record *sig;
254 	struct sshfp_record *sshfp;
255 	struct ds_record *ds;
256     }u;
257     struct resource_record *next;
258 };
259 
260 #define rk_DNS_MAX_PACKET_SIZE		0xffff
261 
262 struct dns_header {
263     unsigned id;
264     unsigned flags;
265 #define rk_DNS_HEADER_RESPONSE_FLAG		1
266 #define rk_DNS_HEADER_AUTHORITIVE_ANSWER	2
267 #define rk_DNS_HEADER_TRUNCATED_MESSAGE		4
268 #define rk_DNS_HEADER_RECURSION_DESIRED		8
269 #define rk_DNS_HEADER_RECURSION_AVAILABLE	16
270 #define rk_DNS_HEADER_AUTHENTIC_DATA		32
271 #define rk_DNS_HEADER_CHECKING_DISABLED		64
272     unsigned opcode;
273     unsigned response_code;
274     unsigned qdcount;
275     unsigned ancount;
276     unsigned nscount;
277     unsigned arcount;
278 };
279 
280 struct dns_reply{
281     struct dns_header h;
282     struct dns_query q;
283     struct resource_record *head;
284 };
285 
286 
287 struct dns_reply* ROKEN_LIB_FUNCTION
288 	dns_lookup(const char *, const char *);
289 void ROKEN_LIB_FUNCTION
290 	dns_free_data(struct dns_reply *);
291 int ROKEN_LIB_FUNCTION
292 	dns_string_to_type(const char *name);
293 const char *ROKEN_LIB_FUNCTION
294 	dns_type_to_string(int type);
295 void ROKEN_LIB_FUNCTION
296 	dns_srv_order(struct dns_reply*);
297 
298 #endif /* __RESOLVE_H__ */
299