xref: /openbsd/usr.bin/dig/dig.h (revision 3cab2bb3)
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/boolean.h>
27 #include <isc/buffer.h>
28 #include <isc/list.h>
29 #include <isc/sockaddr.h>
30 #include <isc/socket.h>
31 
32 #define MXSERV 20
33 #define MXNAME (DNS_NAME_MAXTEXT+1)
34 #define MXRD 32
35 /*% Buffer Size */
36 #define BUFSIZE 512
37 #define COMMSIZE 0xffff
38 /*% output buffer */
39 #define OUTPUTBUF 32767
40 /*% Max RR Limit */
41 #define MAXRRLIMIT 0xffffffff
42 #define MAXTIMEOUT 0xffff
43 /*% Max number of tries */
44 #define MAXTRIES 0xffffffff
45 /*% Max number of dots */
46 #define MAXNDOTS 0xffff
47 /*% Max number of ports */
48 #define MAXPORT 0xffff
49 /*% Max serial number */
50 #define MAXSERIAL 0xffffffff
51 
52 /*% Default TCP Timeout */
53 #define TCP_TIMEOUT 10
54 /*% Default UDP Timeout */
55 #define UDP_TIMEOUT 5
56 
57 #define SERVER_TIMEOUT 1
58 
59 #define LOOKUP_LIMIT 64
60 /*%
61  * Lookup_limit is just a limiter, keeping too many lookups from being
62  * created.  It's job is mainly to prevent the program from running away
63  * in a tight loop of constant lookups.  It's value is arbitrary.
64  */
65 
66 /*
67  * Defaults for the sigchase suboptions.  Consolidated here because
68  * these control the layout of dig_lookup_t (among other things).
69  */
70 
71 typedef struct dig_lookup dig_lookup_t;
72 typedef struct dig_query dig_query_t;
73 typedef struct dig_server dig_server_t;
74 typedef ISC_LIST(dig_server_t) dig_serverlist_t;
75 typedef struct dig_searchlist dig_searchlist_t;
76 
77 /*% The dig_lookup structure */
78 struct dig_lookup {
79 	isc_boolean_t
80 		pending, /*%< Pending a successful answer */
81 		waiting_connect,
82 		doing_xfr,
83 		ns_search_only, /*%< dig +nssearch, host -C */
84 		identify, /*%< Append an "on server <foo>" message */
85 		identify_previous_line, /*% Prepend a "Nameserver <foo>:"
86 					   message, with newline and tab */
87 		ignore,
88 		recurse,
89 		aaonly,
90 		adflag,
91 		cdflag,
92 		trace, /*% dig +trace */
93 		trace_root, /*% initial query for either +trace or +nssearch */
94 		tcp_mode,
95 		tcp_mode_set,
96 		ip6_int,
97 		comments,
98 		stats,
99 		section_question,
100 		section_answer,
101 		section_authority,
102 		section_additional,
103 		servfail_stops,
104 		new_search,
105 		need_search,
106 		done_as_is,
107 		besteffort,
108 		dnssec,
109 		expire,
110 		sit,
111 		nsid,   /*% Name Server ID (RFC 5001) */
112 		ednsneg,
113 		mapped,
114 		idnout;
115 
116 	char textname[MXNAME]; /*% Name we're going to be looking up */
117 	char cmdline[MXNAME];
118 	dns_rdatatype_t rdtype;
119 	dns_rdatatype_t qrdtype;
120 	dns_rdataclass_t rdclass;
121 	isc_boolean_t rdtypeset;
122 	isc_boolean_t rdclassset;
123 	char name_space[BUFSIZE];
124 	char oname_space[BUFSIZE];
125 	isc_buffer_t namebuf;
126 	isc_buffer_t onamebuf;
127 	isc_buffer_t renderbuf;
128 	char *sendspace;
129 	dns_name_t *name;
130 	struct timespec interval;
131 	dns_message_t *sendmsg;
132 	dns_name_t *oname;
133 	ISC_LINK(dig_lookup_t) link;
134 	ISC_LIST(dig_query_t) q;
135 	ISC_LIST(dig_query_t) connecting;
136 	dig_query_t *current_query;
137 	dig_serverlist_t my_server_list;
138 	dig_searchlist_t *origin;
139 	dig_query_t *xfr_q;
140 	uint32_t retries;
141 	int nsfound;
142 	uint16_t udpsize;
143 	int16_t edns;
144 	uint32_t ixfr_serial;
145 	isc_buffer_t rdatabuf;
146 	char rdatastore[MXNAME];
147 	dst_context_t *tsigctx;
148 	isc_buffer_t *querysig;
149 	uint32_t msgcounter;
150 	dns_fixedname_t fdomain;
151 	isc_sockaddr_t *ecs_addr;
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 */
161 struct dig_query {
162 	dig_lookup_t *lookup;
163 	isc_boolean_t 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 	isc_boolean_t 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 	isc_sockaddr_t 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 
200 struct dig_server {
201 	char servername[MXNAME];
202 	char userarg[MXNAME];
203 	ISC_LINK(dig_server_t) link;
204 };
205 
206 struct dig_searchlist {
207 	char origin[MXNAME];
208 	ISC_LINK(dig_searchlist_t) link;
209 };
210 
211 typedef ISC_LIST(dig_searchlist_t) dig_searchlistlist_t;
212 typedef ISC_LIST(dig_lookup_t) dig_lookuplist_t;
213 
214 /*
215  * Externals from dighost.c
216  */
217 
218 extern dig_lookuplist_t lookup_list;
219 extern dig_serverlist_t server_list;
220 extern dig_serverlist_t root_hints_server_list;
221 extern dig_searchlistlist_t search_list;
222 extern unsigned int extrabytes;
223 
224 extern isc_boolean_t check_ra, have_ipv4, have_ipv6, specified_source,
225 	usesearch, showsearch, qr;
226 extern in_port_t port;
227 extern unsigned int timeout;
228 extern int sendcount;
229 extern int ndots;
230 extern int lookup_counter;
231 extern int exitcode;
232 extern isc_sockaddr_t bind_address;
233 extern char keynametext[MXNAME];
234 extern char keyfile[MXNAME];
235 extern char keysecret[MXNAME];
236 extern dns_name_t *hmacname;
237 extern unsigned int digestbits;
238 extern dns_tsigkey_t *tsigkey;
239 extern isc_boolean_t validated;
240 extern isc_taskmgr_t *taskmgr;
241 extern isc_task_t *global_task;
242 extern isc_boolean_t free_now;
243 extern isc_boolean_t debugging, debugtiming;
244 extern isc_boolean_t keep_open;
245 
246 extern char *progname;
247 extern int tries;
248 extern int fatalexit;
249 
250 int host_main(int, char **);
251 int nslookup_main(int, char **);
252 
253 /*
254  * Routines in dighost.c.
255  */
256 isc_result_t
257 get_address(char *host, in_port_t port, isc_sockaddr_t *sockaddr);
258 
259 int
260 getaddresses(dig_lookup_t *lookup, const char *host, isc_result_t *resultp);
261 
262 isc_result_t
263 get_reverse(char *reverse, size_t len, char *value, isc_boolean_t ip6_int,
264 	    isc_boolean_t strict);
265 
266 __dead void
267 fatal(const char *format, ...)
268 __attribute__((__format__(__printf__, 1, 2)));
269 
270 void
271 debug(const char *format, ...) __attribute__((__format__(__printf__, 1, 2)));
272 
273 void
274 check_result(isc_result_t result, const char *msg);
275 
276 isc_boolean_t
277 setup_lookup(dig_lookup_t *lookup);
278 
279 void
280 destroy_lookup(dig_lookup_t *lookup);
281 
282 void
283 do_lookup(dig_lookup_t *lookup);
284 
285 void
286 start_lookup(void);
287 
288 void
289 onrun_callback(isc_task_t *task, isc_event_t *event);
290 
291 int
292 dhmain(int argc, char **argv);
293 
294 void
295 setup_libs(void);
296 
297 void
298 setup_system(isc_boolean_t ipv4only, isc_boolean_t ipv6only);
299 
300 isc_result_t
301 parse_uint(uint32_t *uip, const char *value, uint32_t max,
302 	   const char *desc);
303 
304 isc_result_t
305 parse_xint(uint32_t *uip, const char *value, uint32_t max,
306 	   const char *desc);
307 
308 isc_result_t
309 parse_netprefix(isc_sockaddr_t **sap, const char *value);
310 
311 void
312 parse_hmac(const char *hmacstr);
313 
314 dig_lookup_t *
315 requeue_lookup(dig_lookup_t *lookold, isc_boolean_t servers);
316 
317 dig_lookup_t *
318 make_empty_lookup(void);
319 
320 dig_lookup_t *
321 clone_lookup(dig_lookup_t *lookold, isc_boolean_t servers);
322 
323 dig_server_t *
324 make_server(const char *servname, const char *userarg);
325 
326 void
327 flush_server_list(void);
328 
329 isc_result_t
330 set_nameserver(char *opt);
331 
332 void
333 clone_server_list(dig_serverlist_t src,
334 		  dig_serverlist_t *dest);
335 
336 void
337 cancel_all(void);
338 
339 void
340 destroy_libs(void);
341 
342 void
343 set_search_domain(char *domain);
344 
345 char *
346 next_token(char **stringp, const char *delim);
347 
348 /*
349  * Routines to be defined in dig.c, host.c, and nslookup.c. and
350  * then assigned to the appropriate function pointer
351  */
352 
353 extern isc_result_t
354 (*dighost_printmessage)(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers);
355 /*%<
356  * Print the final result of the lookup.
357  */
358 
359 extern void
360 (*dighost_received)(unsigned int bytes, isc_sockaddr_t *from, dig_query_t *query);
361 /*%<
362  * Print a message about where and when the response
363  * was received from, like the final comment in the
364  * output of "dig".
365  */
366 
367 extern void
368 (*dighost_trying)(char *frm, dig_lookup_t *lookup);
369 
370 extern void
371 (*dighost_shutdown)(void);
372 
373 void save_opt(dig_lookup_t *lookup, char *code, char *value);
374 
375 void setup_file_key(void);
376 void setup_text_key(void);
377 
378 /*
379  * Routines exported from dig.c for use by dig for iOS
380  */
381 
382 /*%<
383  * Call once only to set up libraries, parse global
384  * parameters and initial command line query parameters
385  */
386 void
387 dig_setup(int argc, char **argv);
388 
389 /*%<
390  * Call to supply new parameters for the next lookup
391  */
392 void
393 dig_query_setup(isc_boolean_t, isc_boolean_t, int argc, char **argv);
394 
395 /*%<
396  * set the main application event cycle running
397  */
398 void
399 dig_startup(void);
400 
401 /*%<
402  * Cleans up the application
403  */
404 void
405 dig_shutdown(void);
406 
407 #endif
408