1/*
2 * Copyright 2009-2020, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6/*
7 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 * SOFTWARE.
21 */
22
23/*
24 * Copyright (c) 1983, 1987, 1989
25 *    The Regents of the University of California.  All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 *    notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 *    notice, this list of conditions and the following disclaimer in the
34 *    documentation and/or other materials provided with the distribution.
35 * 3. Neither the name of the University nor the names of its contributors
36 *    may be used to endorse or promote products derived from this software
37 *    without specific prior written permission.
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 */
51
52#ifndef _RESOLV_H_
53#define	_RESOLV_H_
54
55#include <stdint.h>
56#include <stdio.h>
57#include <arpa/nameser.h>
58#include <netinet/in.h>
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
64#define	__RES	19991006
65
66struct res_sym {
67	int			number;	   /* Identifying number, like T_MX */
68	const char *name;	   /* Its symbolic name, like "MX" */
69	const char *humanname; /* Its fun name, like "mail exchanger" */
70};
71
72/*
73 * Global defines and variables for resolver stub.
74 */
75#define	MAXNS				3	/* max # name servers we'll track */
76#define	MAXDFLSRCH			3	/* # default domain levels to try */
77#define	MAXDNSRCH			6	/* max # domains in search path */
78#define	LOCALDOMAINPARTS	2	/* min levels in name that is "local" */
79#define	RES_TIMEOUT			5	/* min. seconds between retries */
80#define	MAXRESOLVSORT		10	/* number of net to sort on */
81#define	RES_MAXNDOTS		15	/* should reflect bit field size */
82#define	RES_MAXRETRANS		30	/* only for resolv.conf/RES_OPTIONS */
83#define	RES_MAXRETRY		5	/* only for resolv.conf/RES_OPTIONS */
84#define	RES_DFLRETRY		2	/* Default #/tries. */
85#define	RES_MAXTIME			65535	/* Infinity, in milliseconds. */
86
87struct __res_state {
88	int	retrans;		 	/* retransmission time interval */
89	int	retry;				/* number of times to retransmit */
90	unsigned long options;	/* option flags - see below. */
91	int	nscount;			/* number of name servers */
92	struct sockaddr_in
93		nsaddr_list[MAXNS];	/* address of name server */
94	unsigned short	id;		/* current message id */
95	char	*dnsrch[MAXDNSRCH+1];	/* components of domain to search */
96	char	defdname[256];	/* default domain (deprecated) */
97	unsigned long	pfcode;	/* RES_PRF_ flags - see below. */
98	unsigned ndots:4;		/* threshold for initial abs. query */
99	unsigned nsort:4;		/* number of elements in sort_list[] */
100	char	unused[3];
101	struct {
102		struct in_addr	addr;
103		uint32_t	mask;
104	} sort_list[MAXRESOLVSORT];
105	void* qhook;	/* query hook */
106	void* rhook;	/* response hook */
107	int	res_h_errno;		/* last one set for this context */
108	int	_vcsock;			/* PRIVATE: for res_send VC i/o */
109	unsigned int	_flags;			/* PRIVATE: see below */
110	unsigned char	_rnd[16];		/* PRIVATE: random state */
111	unsigned int	_pad;			/* make _u 64 bit aligned */
112	union {
113		/* On an 32-bit arch this means 512b total. */
114		char	pad[56 - 4*sizeof (int) - 2*sizeof (void *)];
115		struct {
116			uint16_t		nscount;
117			uint16_t		nstimes[MAXNS];	/* ms. */
118			int			nssocks[MAXNS];
119			struct __res_state_ext *ext;	/* extention for IPv6 */
120		} _ext;
121	} _u;
122};
123
124typedef struct __res_state *res_state;
125
126/* resolver flags; for _flags in __res_state. */
127#define	RES_F_VC		0x00000001	/* socket is TCP */
128#define	RES_F_CONN		0x00000002	/* socket is connected */
129#define	RES_F_EDNS0ERR	0x00000004	/* EDNS0 caused errors */
130
131/* res_findzonecut2() options */
132#define	RES_EXHAUSTIVE	0x00000001	/* always do all queries */
133#define	RES_IPV4ONLY	0x00000002	/* IPv4 only */
134#define	RES_IPV6ONLY	0x00000004	/* IPv6 only */
135
136/* resolver options */
137#define RES_INIT		(1 << 0)	/* address initialized */
138#define RES_DEBUG		(1 << 1)	/* print debug messages */
139#define RES_AAONLY		(1 << 2)	/* authoritative answers only (!IMPL)*/
140#define RES_USEVC		(1 << 3)	/* use virtual circuit */
141#define RES_PRIMARY		(1 << 4)	/* query primary server only (!IMPL) */
142#define RES_IGNTC		(1 << 5)	/* ignore trucation errors */
143#define RES_RECURSE		(1 << 6)	/* recursion desired */
144#define RES_DEFNAMES	(1 << 7)	/* use default domain name */
145#define RES_STAYOPEN	(1 << 8)	/* Keep TCP socket open */
146#define RES_DNSRCH		(1 << 9)	/* search up local domain tree */
147#define	RES_INSECURE1	(1 << 10)	/* type 1 security disabled */
148#define	RES_INSECURE2	(1 << 11)	/* type 2 security disabled */
149#define	RES_NOALIASES	(1 << 12)	/* shuts off HOSTALIASES feature */
150#define	RES_USE_INET6	(1 << 13)	/* use/map IPv6 in gethostbyname() */
151#define RES_ROTATE		(1 << 14)	/* rotate ns list after each query */
152#define	RES_NOCHECKNAME	(1 << 15)	/* do not check names for sanity. */
153#define	RES_KEEPTSIG	(1 << 16)	/* do not strip TSIG records */
154#define	RES_BLAST		(1 << 17)	/* blast all recursive servers */
155
156#define RES_NOTLDQUERY	(1 << 20)	/* don't unqualified name as a tld */
157#define RES_USE_DNSSEC	(1 << 21)	/* use DNSSEC using OK bit in OPT */
158
159#define RES_USE_INET4	(1 << 23)	/* use IPv4 in gethostbyname() */
160
161/* KAME extensions: use higher bit to avoid conflict with ISC use */
162#define RES_USE_EDNS0	0x40000000	/* use EDNS0 if configured */
163#define RES_NO_NIBBLE2	0x80000000	/* disable alternate nibble lookup */
164
165#define RES_DEFAULT	\
166	(RES_RECURSE | RES_DEFNAMES | RES_DNSRCH | RES_NO_NIBBLE2)
167
168/* resolver "pfcode" values, used by dig. */
169#define RES_PRF_STATS	0x00000001
170#define RES_PRF_UPDATE	0x00000002
171#define RES_PRF_CLASS   0x00000004
172#define RES_PRF_CMD		0x00000008
173#define RES_PRF_QUES	0x00000010
174#define RES_PRF_ANS		0x00000020
175#define RES_PRF_AUTH	0x00000040
176#define RES_PRF_ADD		0x00000080
177#define RES_PRF_HEAD1	0x00000100
178#define RES_PRF_HEAD2	0x00000200
179#define RES_PRF_TTLID	0x00000400
180#define RES_PRF_HEADX	0x00000800
181#define RES_PRF_QUERY	0x00001000
182#define RES_PRF_REPLY	0x00002000
183#define RES_PRF_INIT	0x00004000
184
185/* Things involving an internal (static) resolver context. */
186extern struct __res_state *__res_state(void) __attribute__ ((__const__));
187#define _res (*__res_state())
188
189#define fp_nquery		__fp_nquery
190#define fp_query		__fp_query
191#define hostalias		__hostalias
192#define p_query			__p_query
193#define res_close		__res_close
194#define res_init		__res_init
195#define res_isourserver	__res_isourserver
196#define res_mkquery		__res_mkquery
197#define res_query		__res_query
198#define res_querydomain	__res_querydomain
199#define res_search		__res_search
200#define res_send		__res_send
201
202void		fp_nquery(const unsigned char *, int, FILE *);
203void		fp_query(const unsigned char *, FILE *);
204const char *hostalias(const char *);
205void		p_query(const unsigned char *);
206void		res_close(void);
207int			res_init(void);
208int			res_isourserver(const struct sockaddr_in *);
209int			res_mkquery(int, const char *, int, int, const unsigned char *,
210				 int, const unsigned char *, unsigned char *, int);
211int			res_query(const char *, int, int, unsigned char *, int);
212int			res_querydomain(const char *, const char *, int, int,
213				unsigned char *, int);
214int			res_search(const char *, int, int, unsigned char *, int);
215int			res_send(const unsigned char *, int, unsigned char *, int);
216
217#define b64_ntop			__b64_ntop
218#define b64_pton			__b64_pton
219#define dn_comp				__dn_comp
220#define dn_count_labels		__dn_count_labels
221#define dn_expand			__dn_expand
222#define dn_skipname			__dn_skipname
223#define fp_resstat			__fp_resstat
224#define loc_aton			__loc_aton
225#define loc_ntoa			__loc_ntoa
226#define p_cdname			__p_cdname
227#define p_cdnname			__p_cdnname
228#define p_class				__p_class
229#define p_fqname			__p_fqname
230#define p_fqnname			__p_fqnname
231#define p_option			__p_option
232#define p_time				__p_time
233#define p_type				__p_type
234#define p_rcode				__p_rcode
235#define res_dnok			__res_dnok
236#define res_hnok			__res_hnok
237#define res_hostalias		__res_hostalias
238#define res_mailok			__res_mailok
239#define res_nameinquery		__res_nameinquery
240#define res_nclose			__res_nclose
241#define res_ninit			__res_ninit
242#define res_nmkquery		__res_nmkquery
243#define res_nquery			__res_nquery
244#define res_nquerydomain	__res_nquerydomain
245#define res_nsearch			__res_nsearch
246#define res_nsend			__res_nsend
247#define res_ownok			__res_ownok
248#define res_queriesmatch	__res_queriesmatch
249#define res_randomid		__res_randomid
250#define sym_ntop			__sym_ntop
251#define sym_ntos			__sym_ntos
252#define sym_ston			__sym_ston
253
254int				res_hnok(const char *);
255int				res_ownok(const char *);
256int				res_mailok(const char *);
257int				res_dnok(const char *);
258int				sym_ston(const struct res_sym *, const char *, int *);
259const char *	sym_ntos(const struct res_sym *, int, int *);
260const char *	sym_ntop(const struct res_sym *, int, int *);
261int				b64_ntop(unsigned char const *, size_t, char *, size_t);
262int				b64_pton(char const *, unsigned char *, size_t);
263int				loc_aton(const char *, unsigned char *);
264const char *	loc_ntoa(const unsigned char *, char *);
265int				dn_skipname(const unsigned char *, const unsigned char *);
266const char *	p_class(int);
267const char *	p_time(uint32_t);
268const char *	p_type(int);
269const char *	p_rcode(int);
270const unsigned char * p_cdnname(const unsigned char *, const unsigned char *,
271						int, FILE *);
272const unsigned char * p_cdname(const unsigned char *, const unsigned char *,
273						FILE *);
274const unsigned char * p_fqnname(const unsigned char *, const unsigned char *,
275						int, char *, int);
276const unsigned char * p_fqname(const unsigned char *, const unsigned char *,
277						FILE *);
278const char *	p_option(unsigned long);
279int				dn_count_labels(const char *);
280int				dn_comp(const char *, unsigned char *, int, unsigned char **,
281					unsigned char **);
282int				dn_expand(const unsigned char *, const unsigned char *,
283					const unsigned char *, char *, int);
284unsigned int	res_randomid(void);
285int				res_nameinquery(const char *, int, int, const unsigned char *,
286					const unsigned char *);
287int				res_queriesmatch(const unsigned char *, const unsigned char *,
288					const unsigned char *, const unsigned char *);
289
290/* Things involving a resolver context. */
291int				res_ninit(res_state);
292void			fp_resstat(const res_state, FILE *);
293const char *	res_hostalias(const res_state, const char *, char *, size_t);
294int				res_nquery(res_state, const char *, int, int, unsigned char *,
295					int);
296int				res_nsearch(res_state, const char *, int, int, unsigned char *,
297					int);
298int				res_nquerydomain(res_state, const char *, const char *, int,
299					int, unsigned char *, int);
300int				res_nmkquery(res_state, int, const char *, int, int,
301					const unsigned char *, int, const unsigned char *,
302					unsigned char *, int);
303int				res_nsend(res_state, const unsigned char *, int, unsigned char *,
304					int);
305void			res_nclose(res_state);
306
307#ifdef __cplusplus
308}
309#endif
310
311#endif /* !_RESOLV_H_ */
312