1/*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef DIG_H
18#define DIG_H
19
20/*! \file */
21
22#include <time.h>
23
24#include <dst/dst.h>
25
26#include <isc/buffer.h>
27#include <isc/list.h>
28#include <isc/sockaddr.h>
29#include <isc/socket.h>
30
31#define MXSERV 20
32#define MXNAME (DNS_NAME_MAXTEXT+1)
33#define MXRD 32
34/*% Buffer Size */
35#define BUFSIZE 512
36#define COMMSIZE 0xffff
37/*% output buffer */
38#define OUTPUTBUF 32767
39/*% Max RR Limit */
40#define MAXRRLIMIT 0xffffffff
41#define MAXTIMEOUT 0xffff
42/*% Max number of tries */
43#define MAXTRIES 0xffffffff
44/*% Max number of dots */
45#define MAXNDOTS 0xffff
46/*% Max number of ports */
47#define MAXPORT 0xffff
48/*% Max serial number */
49#define MAXSERIAL 0xffffffff
50
51/*% Default TCP Timeout */
52#define TCP_TIMEOUT 10
53/*% Default UDP Timeout */
54#define UDP_TIMEOUT 5
55
56#define SERVER_TIMEOUT 1
57
58#define LOOKUP_LIMIT 64
59/*%
60 * Lookup_limit is just a limiter, keeping too many lookups from being
61 * created.  It's job is mainly to prevent the program from running away
62 * in a tight loop of constant lookups.  It's value is arbitrary.
63 */
64
65/*
66 * Defaults for the sigchase suboptions.  Consolidated here because
67 * these control the layout of dig_lookup_t (among other things).
68 */
69
70typedef struct dig_lookup dig_lookup_t;
71typedef struct dig_query dig_query_t;
72typedef struct dig_server dig_server_t;
73typedef ISC_LIST(dig_server_t) dig_serverlist_t;
74typedef struct dig_searchlist dig_searchlist_t;
75
76/*% The dig_lookup structure */
77struct dig_lookup {
78	int
79		pending, /*%< Pending a successful answer */
80		waiting_connect,
81		doing_xfr,
82		ns_search_only, /*%< dig +nssearch, host -C */
83		identify, /*%< Append an "on server <foo>" message */
84		identify_previous_line, /*% Prepend a "Nameserver <foo>:"
85					   message, with newline and tab */
86		ignore,
87		recurse,
88		aaonly,
89		adflag,
90		cdflag,
91		trace, /*% dig +trace */
92		trace_root, /*% initial query for either +trace or +nssearch */
93		tcp_mode,
94		tcp_mode_set,
95		ip6_int,
96		comments,
97		stats,
98		section_question,
99		section_answer,
100		section_authority,
101		section_additional,
102		servfail_stops,
103		new_search,
104		need_search,
105		done_as_is,
106		besteffort,
107		dnssec,
108		expire,
109		sit,
110		nsid,   /*% Name Server ID (RFC 5001) */
111		ednsneg,
112		mapped,
113		idnout;
114
115	char textname[MXNAME]; /*% Name we're going to be looking up */
116	char cmdline[MXNAME];
117	dns_rdatatype_t rdtype;
118	dns_rdatatype_t qrdtype;
119	dns_rdataclass_t rdclass;
120	int rdtypeset;
121	int rdclassset;
122	char name_space[BUFSIZE];
123	char oname_space[BUFSIZE];
124	isc_buffer_t namebuf;
125	isc_buffer_t onamebuf;
126	isc_buffer_t renderbuf;
127	char *sendspace;
128	dns_name_t *name;
129	struct timespec interval;
130	dns_message_t *sendmsg;
131	dns_name_t *oname;
132	ISC_LINK(dig_lookup_t) link;
133	ISC_LIST(dig_query_t) q;
134	ISC_LIST(dig_query_t) connecting;
135	dig_query_t *current_query;
136	dig_serverlist_t my_server_list;
137	dig_searchlist_t *origin;
138	dig_query_t *xfr_q;
139	uint32_t retries;
140	int nsfound;
141	uint16_t udpsize;
142	int16_t edns;
143	uint32_t ixfr_serial;
144	isc_buffer_t rdatabuf;
145	char rdatastore[MXNAME];
146	dst_context_t *tsigctx;
147	isc_buffer_t *querysig;
148	uint32_t msgcounter;
149	dns_fixedname_t fdomain;
150	struct sockaddr_storage *ecs_addr;
151	int ecs_plen;
152	char *sitvalue;
153	dns_ednsopt_t *ednsopts;
154	unsigned int ednsoptscnt;
155	unsigned int ednsflags;
156	dns_opcode_t opcode;
157	unsigned int eoferr;
158};
159
160/*% The dig_query structure */
161struct dig_query {
162	dig_lookup_t *lookup;
163	int waiting_connect,
164		pending_free,
165		waiting_senddone,
166		first_pass,
167		first_soa_rcvd,
168		second_rr_rcvd,
169		first_repeat_rcvd,
170		recv_made,
171		warn_id,
172		timedout;
173	uint32_t first_rr_serial;
174	uint32_t second_rr_serial;
175	uint32_t msg_count;
176	uint32_t rr_count;
177	int ixfr_axfr;
178	char *servname;
179	char *userarg;
180	isc_bufferlist_t sendlist,
181		recvlist,
182		lengthlist;
183	isc_buffer_t recvbuf,
184		lengthbuf,
185		slbuf;
186	char *recvspace,
187		lengthspace[4],
188		slspace[4];
189	isc_socket_t *sock;
190	ISC_LINK(dig_query_t) link;
191	ISC_LINK(dig_query_t) clink;
192	struct sockaddr_storage sockaddr;
193	struct timespec time_sent;
194	struct timespec time_recv;
195	uint64_t byte_count;
196	isc_buffer_t sendbuf;
197	isc_timer_t *timer;
198};
199
200struct dig_server {
201	char servername[MXNAME];
202	char userarg[MXNAME];
203	ISC_LINK(dig_server_t) link;
204};
205
206struct dig_searchlist {
207	char origin[MXNAME];
208	ISC_LINK(dig_searchlist_t) link;
209};
210
211typedef ISC_LIST(dig_searchlist_t) dig_searchlistlist_t;
212typedef ISC_LIST(dig_lookup_t) dig_lookuplist_t;
213
214/*
215 * Externals from dighost.c
216 */
217
218extern dig_lookuplist_t lookup_list;
219extern dig_serverlist_t server_list;
220extern dig_serverlist_t root_hints_server_list;
221extern dig_searchlistlist_t search_list;
222extern unsigned int extrabytes;
223
224extern int check_ra, have_ipv4, have_ipv6, specified_source,
225	usesearch, showsearch, qr;
226extern in_port_t port;
227extern unsigned int timeout;
228extern int sendcount;
229extern int ndots;
230extern int lookup_counter;
231extern int exitcode;
232extern struct sockaddr_storage bind_address;
233extern char keynametext[MXNAME];
234extern char keyfile[MXNAME];
235extern char keysecret[MXNAME];
236extern dns_name_t *hmacname;
237extern unsigned int digestbits;
238extern dns_tsigkey_t *tsigkey;
239extern int validated;
240extern isc_taskmgr_t *taskmgr;
241extern isc_task_t *global_task;
242extern int free_now;
243extern int debugging, debugtiming;
244extern int keep_open;
245
246extern char *progname;
247extern int tries;
248extern int fatalexit;
249
250int host_main(int, char **);
251int nslookup_main(int, char **);
252
253/*
254 * Routines in dighost.c.
255 */
256isc_result_t
257get_address(char *host, in_port_t port, struct sockaddr_storage *sockaddr);
258
259int
260getaddresses(dig_lookup_t *lookup, const char *host, isc_result_t *resultp);
261
262isc_result_t
263get_reverse(char *reverse, size_t len, char *value, int ip6_int,
264	    int strict);
265
266__dead void
267fatal(const char *format, ...)
268__attribute__((__format__(__printf__, 1, 2)));
269
270void
271debug(const char *format, ...) __attribute__((__format__(__printf__, 1, 2)));
272
273void
274check_result(isc_result_t result, const char *msg);
275
276int
277setup_lookup(dig_lookup_t *lookup);
278
279void
280destroy_lookup(dig_lookup_t *lookup);
281
282void
283do_lookup(dig_lookup_t *lookup);
284
285void
286start_lookup(void);
287
288void
289onrun_callback(isc_task_t *task, isc_event_t *event);
290
291void
292setup_libs(void);
293
294void
295setup_system(int ipv4only, int ipv6only);
296
297isc_result_t
298parse_netprefix(struct sockaddr_storage **sap, int *plen, const char *value);
299
300void
301parse_hmac(const char *hmacstr);
302
303dig_lookup_t *
304requeue_lookup(dig_lookup_t *lookold, int servers);
305
306dig_lookup_t *
307make_empty_lookup(void);
308
309dig_lookup_t *
310clone_lookup(dig_lookup_t *lookold, int servers);
311
312dig_server_t *
313make_server(const char *servname, const char *userarg);
314
315void
316flush_server_list(void);
317
318isc_result_t
319set_nameserver(char *opt);
320
321void
322clone_server_list(dig_serverlist_t src,
323		  dig_serverlist_t *dest);
324
325void
326cancel_all(void);
327
328void
329destroy_libs(void);
330
331void
332set_search_domain(char *domain);
333
334char *
335next_token(char **stringp, const char *delim);
336
337int64_t
338uelapsed(const struct timespec *t1, const struct timespec *t2);
339
340/*
341 * Routines to be defined in dig.c, host.c, and nslookup.c. and
342 * then assigned to the appropriate function pointer
343 */
344
345extern isc_result_t
346(*dighost_printmessage)(dig_query_t *query, dns_message_t *msg, int headers);
347/*%<
348 * Print the final result of the lookup.
349 */
350
351extern void
352(*dighost_received)(unsigned int bytes, struct sockaddr_storage *from, dig_query_t *query);
353/*%<
354 * Print a message about where and when the response
355 * was received from, like the final comment in the
356 * output of "dig".
357 */
358
359extern void
360(*dighost_trying)(char *frm, dig_lookup_t *lookup);
361
362extern void
363(*dighost_shutdown)(void);
364
365void save_opt(dig_lookup_t *lookup, char *code, char *value);
366
367void setup_file_key(void);
368void setup_text_key(void);
369
370/*
371 * Routines exported from dig.c for use by dig for iOS
372 */
373
374/*%<
375 * Call once only to set up libraries, parse global
376 * parameters and initial command line query parameters
377 */
378void
379dig_setup(int argc, char **argv);
380
381/*%<
382 * Call to supply new parameters for the next lookup
383 */
384void
385dig_query_setup(int, int, int argc, char **argv);
386
387/*%<
388 * set the main application event cycle running
389 */
390void
391dig_startup(void);
392
393/*%<
394 * Cleans up the application
395 */
396void
397dig_shutdown(void);
398
399#endif
400