xref: /openbsd/usr.bin/dig/dig.h (revision db6316a3)
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 
70 typedef struct dig_lookup dig_lookup_t;
71 typedef struct dig_query dig_query_t;
72 typedef struct dig_server dig_server_t;
73 typedef ISC_LIST(dig_server_t) dig_serverlist_t;
74 typedef struct dig_searchlist dig_searchlist_t;
75 
76 /*% The dig_lookup structure */
77 struct 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 */
161 struct 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 
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 int 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 struct sockaddr_storage 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 int validated;
240 extern isc_taskmgr_t *taskmgr;
241 extern isc_task_t *global_task;
242 extern int free_now;
243 extern int debugging, debugtiming;
244 extern int 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, struct sockaddr_storage *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, int ip6_int,
264 	    int 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 int
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 void
292 setup_libs(void);
293 
294 void
295 setup_system(int ipv4only, int ipv6only);
296 
297 isc_result_t
298 parse_netprefix(struct sockaddr_storage **sap, int *plen, const char *value);
299 
300 void
301 parse_hmac(const char *hmacstr);
302 
303 dig_lookup_t *
304 requeue_lookup(dig_lookup_t *lookold, int servers);
305 
306 dig_lookup_t *
307 make_empty_lookup(void);
308 
309 dig_lookup_t *
310 clone_lookup(dig_lookup_t *lookold, int servers);
311 
312 dig_server_t *
313 make_server(const char *servname, const char *userarg);
314 
315 void
316 flush_server_list(void);
317 
318 isc_result_t
319 set_nameserver(char *opt);
320 
321 void
322 clone_server_list(dig_serverlist_t src,
323 		  dig_serverlist_t *dest);
324 
325 void
326 cancel_all(void);
327 
328 void
329 destroy_libs(void);
330 
331 void
332 set_search_domain(char *domain);
333 
334 char *
335 next_token(char **stringp, const char *delim);
336 
337 int64_t
338 uelapsed(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 
345 extern 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 
351 extern 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 
359 extern void
360 (*dighost_trying)(char *frm, dig_lookup_t *lookup);
361 
362 extern void
363 (*dighost_shutdown)(void);
364 
365 void save_opt(dig_lookup_t *lookup, char *code, char *value);
366 
367 void setup_file_key(void);
368 void 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  */
378 void
379 dig_setup(int argc, char **argv);
380 
381 /*%<
382  * Call to supply new parameters for the next lookup
383  */
384 void
385 dig_query_setup(int, int, int argc, char **argv);
386 
387 /*%<
388  * set the main application event cycle running
389  */
390 void
391 dig_startup(void);
392 
393 /*%<
394  * Cleans up the application
395  */
396 void
397 dig_shutdown(void);
398 
399 #endif
400