1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7 *
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 */
11
12 #include <inttypes.h>
13 #include <stdbool.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <isc/app.h>
19 #include <isc/attributes.h>
20 #include <isc/base64.h>
21 #include <isc/hash.h>
22 #include <isc/hex.h>
23 #include <isc/log.h>
24 #include <isc/managers.h>
25 #include <isc/mem.h>
26 #include <isc/net.h>
27 #include <isc/netmgr.h>
28 #include <isc/nonce.h>
29 #include <isc/parseint.h>
30 #include <isc/print.h>
31 #include <isc/random.h>
32 #include <isc/result.h>
33 #include <isc/sockaddr.h>
34 #include <isc/string.h>
35 #include <isc/task.h>
36 #include <isc/util.h>
37
38 #include <dns/byaddr.h>
39 #include <dns/dispatch.h>
40 #include <dns/events.h>
41 #include <dns/fixedname.h>
42 #include <dns/message.h>
43 #include <dns/name.h>
44 #include <dns/rdata.h>
45 #include <dns/rdataclass.h>
46 #include <dns/rdataset.h>
47 #include <dns/rdatatype.h>
48 #include <dns/request.h>
49 #include <dns/resolver.h>
50 #include <dns/types.h>
51 #include <dns/view.h>
52
53 #include <bind9/getaddresses.h>
54
55 #define CHECK(str, x) \
56 { \
57 if ((x) != ISC_R_SUCCESS) { \
58 fprintf(stderr, "mdig: %s failed with %s\n", (str), \
59 isc_result_totext(x)); \
60 exit(-1); \
61 } \
62 }
63
64 #define RUNCHECK(x) RUNTIME_CHECK((x) == ISC_R_SUCCESS)
65
66 #define ADD_STRING(b, s) \
67 { \
68 if (strlen(s) >= isc_buffer_availablelength(b)) \
69 return ((ISC_R_NOSPACE)); \
70 else \
71 isc_buffer_putstr(b, s); \
72 }
73
74 #define MXNAME (DNS_NAME_MAXTEXT + 1)
75 #define COMMSIZE 0xffff
76 #define OUTPUTBUF 32767
77 #define MAXPORT 0xffff
78 #define PORT 53
79 #define MAXTIMEOUT 0xffff
80 #define TCPTIMEOUT 10
81 #define UDPTIMEOUT 5
82 #define MAXTRIES 0xffffffff
83
84 #define NS_PER_US 1000 /*%< Nanoseconds per microsecond. */
85 #define US_PER_SEC 1000000 /*%< Microseconds per second. */
86 #define US_PER_MS 1000 /*%< Microseconds per millisecond. */
87
88 static isc_mem_t *mctx = NULL;
89 static dns_requestmgr_t *requestmgr = NULL;
90 static const char *batchname = NULL;
91 static FILE *batchfp = NULL;
92 static bool burst = false;
93 static bool have_ipv4 = false;
94 static bool have_ipv6 = false;
95 static bool have_src = false;
96 static bool tcp_mode = false;
97 static bool besteffort = true;
98 static bool display_short_form = false;
99 static bool display_headers = true;
100 static bool display_comments = true;
101 static int display_rrcomments = 0;
102 static bool display_ttlunits = true;
103 static bool display_ttl = true;
104 static bool display_class = true;
105 static bool display_crypto = true;
106 static bool display_multiline = false;
107 static bool display_question = true;
108 static bool display_answer = true;
109 static bool display_authority = true;
110 static bool display_additional = true;
111 static bool display_unknown_format = false;
112 static bool yaml = false;
113 static bool continue_on_error = false;
114 static uint32_t display_splitwidth = 0xffffffff;
115 static isc_sockaddr_t srcaddr;
116 static char *server = NULL;
117 static isc_sockaddr_t dstaddr;
118 static in_port_t port = 53;
119 static isc_dscp_t dscp = -1;
120 static unsigned char cookie_secret[33];
121 static int onfly = 0;
122 static char hexcookie[81];
123
124 struct query {
125 char textname[MXNAME]; /*% Name we're going to be
126 * looking up */
127 bool recurse;
128 bool have_aaonly;
129 bool have_adflag;
130 bool have_cdflag;
131 bool have_zflag;
132 bool dnssec;
133 bool expire;
134 bool send_cookie;
135 char *cookie;
136 bool nsid;
137 dns_rdatatype_t rdtype;
138 dns_rdataclass_t rdclass;
139 uint16_t udpsize;
140 int16_t edns;
141 dns_ednsopt_t *ednsopts;
142 unsigned int ednsoptscnt;
143 unsigned int ednsflags;
144 isc_sockaddr_t *ecs_addr;
145 unsigned int timeout;
146 unsigned int udptimeout;
147 unsigned int udpretries;
148 ISC_LINK(struct query) link;
149 };
150 static struct query default_query;
151 static ISC_LIST(struct query) queries;
152
153 #define EDNSOPTS 100U
154 /*% opcode text */
155 static const char *const opcodetext[] = {
156 "QUERY", "IQUERY", "STATUS", "RESERVED3",
157 "NOTIFY", "UPDATE", "RESERVED6", "RESERVED7",
158 "RESERVED8", "RESERVED9", "RESERVED10", "RESERVED11",
159 "RESERVED12", "RESERVED13", "RESERVED14", "RESERVED15"
160 };
161
162 /*% return code text */
163 static const char *const rcodetext[] = {
164 "NOERROR", "FORMERR", "SERVFAIL", "NXDOMAIN", "NOTIMP",
165 "REFUSED", "YXDOMAIN", "YXRRSET", "NXRRSET", "NOTAUTH",
166 "NOTZONE", "RESERVED11", "RESERVED12", "RESERVED13", "RESERVED14",
167 "RESERVED15", "BADVERS"
168 };
169
170 /*% safe rcodetext[] */
171 static char *
rcode_totext(dns_rcode_t rcode)172 rcode_totext(dns_rcode_t rcode) {
173 static char buf[sizeof("?65535")];
174 union {
175 const char *consttext;
176 char *deconsttext;
177 } totext;
178
179 if (rcode >= (sizeof(rcodetext) / sizeof(rcodetext[0]))) {
180 snprintf(buf, sizeof(buf), "?%u", rcode);
181 totext.deconsttext = buf;
182 } else {
183 totext.consttext = rcodetext[rcode];
184 }
185 return (totext.deconsttext);
186 }
187
188 /* receive response event handler */
189 static void
recvresponse(isc_task_t * task,isc_event_t * event)190 recvresponse(isc_task_t *task, isc_event_t *event) {
191 dns_requestevent_t *reqev = (dns_requestevent_t *)event;
192 isc_result_t result;
193 dns_message_t *query = NULL, *response = NULL;
194 unsigned int parseflags = 0;
195 isc_buffer_t *msgbuf = NULL, *buf = NULL;
196 unsigned int len = OUTPUTBUF;
197 dns_master_style_t *style = NULL;
198 unsigned int styleflags = 0;
199 dns_messagetextflag_t flags;
200
201 UNUSED(task);
202
203 REQUIRE(reqev != NULL);
204 query = reqev->ev_arg;
205
206 if (reqev->result != ISC_R_SUCCESS) {
207 fprintf(stderr, "response failed with %s\n",
208 isc_result_totext(reqev->result));
209 if (continue_on_error) {
210 goto cleanup;
211 } else {
212 exit(-1);
213 }
214 }
215
216 dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &response);
217
218 parseflags |= DNS_MESSAGEPARSE_PRESERVEORDER;
219 if (besteffort) {
220 parseflags |= DNS_MESSAGEPARSE_BESTEFFORT;
221 parseflags |= DNS_MESSAGEPARSE_IGNORETRUNCATION;
222 }
223
224 msgbuf = dns_request_getanswer(reqev->request);
225 result = dns_request_getresponse(reqev->request, response, parseflags);
226 CHECK("dns_request_getresponse", result);
227
228 styleflags |= DNS_STYLEFLAG_REL_OWNER;
229 if (yaml) {
230 styleflags |= DNS_STYLEFLAG_YAML;
231 response->indent.string = " ";
232 response->indent.count = 3;
233 } else {
234 if (display_comments) {
235 styleflags |= DNS_STYLEFLAG_COMMENT;
236 }
237 if (display_unknown_format) {
238 styleflags |= DNS_STYLEFLAG_UNKNOWNFORMAT;
239 }
240 if (display_rrcomments > 0) {
241 styleflags |= DNS_STYLEFLAG_RRCOMMENT;
242 }
243 if (display_ttlunits) {
244 styleflags |= DNS_STYLEFLAG_TTL_UNITS;
245 }
246 if (!display_ttl) {
247 styleflags |= DNS_STYLEFLAG_NO_TTL;
248 }
249 if (!display_class) {
250 styleflags |= DNS_STYLEFLAG_NO_CLASS;
251 }
252 if (!display_crypto) {
253 styleflags |= DNS_STYLEFLAG_NOCRYPTO;
254 }
255 if (display_multiline) {
256 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
257 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
258 styleflags |= DNS_STYLEFLAG_REL_DATA;
259 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
260 styleflags |= DNS_STYLEFLAG_TTL;
261 styleflags |= DNS_STYLEFLAG_MULTILINE;
262 styleflags |= DNS_STYLEFLAG_COMMENT;
263 /* Turn on rrcomments unless explicitly disabled */
264 if (display_rrcomments >= 0) {
265 styleflags |= DNS_STYLEFLAG_RRCOMMENT;
266 }
267 }
268 }
269 if (display_multiline || (!display_ttl && !display_class)) {
270 result = dns_master_stylecreate(&style, styleflags, 24, 24, 24,
271 32, 80, 8, display_splitwidth,
272 mctx);
273 } else if (!display_ttl || !display_class) {
274 result = dns_master_stylecreate(&style, styleflags, 24, 24, 32,
275 40, 80, 8, display_splitwidth,
276 mctx);
277 } else {
278 result = dns_master_stylecreate(&style, styleflags, 24, 32, 40,
279 48, 80, 8, display_splitwidth,
280 mctx);
281 }
282 CHECK("dns_master_stylecreate2", result);
283
284 flags = 0;
285 if (!display_headers) {
286 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
287 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
288 }
289 if (!display_comments) {
290 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
291 }
292
293 isc_buffer_allocate(mctx, &buf, len);
294
295 if (yaml) {
296 char sockstr[ISC_SOCKADDR_FORMATSIZE];
297 uint16_t sport;
298 char *hash;
299 int pf;
300
301 printf("-\n");
302 printf(" type: MESSAGE\n");
303 printf(" message:\n");
304
305 if (((response->flags & DNS_MESSAGEFLAG_RD) != 0) &&
306 ((response->flags & DNS_MESSAGEFLAG_RA) != 0))
307 {
308 printf(" type: RECURSIVE_RESPONSE\n");
309 } else {
310 printf(" type: AUTH_RESPONSE\n");
311 }
312
313 printf(" message_size: %ub\n",
314 isc_buffer_usedlength(msgbuf));
315
316 pf = isc_sockaddr_pf(&dstaddr);
317 if (pf == PF_INET || pf == PF_INET6) {
318 printf(" socket_family: %s\n",
319 pf == PF_INET ? "INET" : "INET6");
320
321 printf(" socket_protocol: %s\n",
322 tcp_mode ? "TCP" : "UDP");
323
324 sport = isc_sockaddr_getport(&dstaddr);
325 isc_sockaddr_format(&dstaddr, sockstr, sizeof(sockstr));
326 hash = strchr(sockstr, '#');
327 if (hash != NULL) {
328 *hash = '\0';
329 }
330 printf(" response_address: %s\n", sockstr);
331 printf(" response_port: %u\n", sport);
332 }
333
334 if (have_src) {
335 sport = isc_sockaddr_getport(&srcaddr);
336 isc_sockaddr_format(&srcaddr, sockstr, sizeof(sockstr));
337 hash = strchr(sockstr, '#');
338 if (hash != NULL) {
339 *hash = '\0';
340 }
341 printf(" query_address: %s\n", sockstr);
342 printf(" query_port: %u\n", sport);
343 }
344
345 printf(" %s:\n", "response_message_data");
346 result = dns_message_headertotext(response, style, flags, buf);
347 CHECK("dns_message_headertotext", result);
348 } else if (display_comments && !display_short_form) {
349 printf(";; Got answer:\n");
350
351 if (display_headers) {
352 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
353 "id: %u\n",
354 opcodetext[response->opcode],
355 rcode_totext(response->rcode), response->id);
356 printf(";; flags:");
357 if ((response->flags & DNS_MESSAGEFLAG_QR) != 0) {
358 printf(" qr");
359 }
360 if ((response->flags & DNS_MESSAGEFLAG_AA) != 0) {
361 printf(" aa");
362 }
363 if ((response->flags & DNS_MESSAGEFLAG_TC) != 0) {
364 printf(" tc");
365 }
366 if ((response->flags & DNS_MESSAGEFLAG_RD) != 0) {
367 printf(" rd");
368 }
369 if ((response->flags & DNS_MESSAGEFLAG_RA) != 0) {
370 printf(" ra");
371 }
372 if ((response->flags & DNS_MESSAGEFLAG_AD) != 0) {
373 printf(" ad");
374 }
375 if ((response->flags & DNS_MESSAGEFLAG_CD) != 0) {
376 printf(" cd");
377 }
378 if ((response->flags & 0x0040U) != 0) {
379 printf("; MBZ: 0x4");
380 }
381
382 printf("; QUERY: %u, ANSWER: %u, "
383 "AUTHORITY: %u, ADDITIONAL: %u\n",
384 response->counts[DNS_SECTION_QUESTION],
385 response->counts[DNS_SECTION_ANSWER],
386 response->counts[DNS_SECTION_AUTHORITY],
387 response->counts[DNS_SECTION_ADDITIONAL]);
388
389 if ((response->flags & DNS_MESSAGEFLAG_RD) != 0 &&
390 (response->flags & DNS_MESSAGEFLAG_RA) == 0)
391 {
392 printf(";; WARNING: recursion requested "
393 "but not available\n");
394 }
395 }
396 }
397
398 repopulate_buffer:
399
400 if (display_comments && display_headers && !display_short_form) {
401 result = dns_message_pseudosectiontotext(
402 response, DNS_PSEUDOSECTION_OPT, style, flags, buf);
403 if (result == ISC_R_NOSPACE) {
404 buftoosmall:
405 len += OUTPUTBUF;
406 isc_buffer_free(&buf);
407 isc_buffer_allocate(mctx, &buf, len);
408 goto repopulate_buffer;
409 }
410 CHECK("dns_message_pseudosectiontotext", result);
411 }
412
413 if (display_question && display_headers && !display_short_form) {
414 result = dns_message_sectiontotext(
415 response, DNS_SECTION_QUESTION, style, flags, buf);
416 if (result == ISC_R_NOSPACE) {
417 goto buftoosmall;
418 }
419 CHECK("dns_message_sectiontotext", result);
420 }
421
422 if (display_answer && !display_short_form) {
423 result = dns_message_sectiontotext(response, DNS_SECTION_ANSWER,
424 style, flags, buf);
425 if (result == ISC_R_NOSPACE) {
426 goto buftoosmall;
427 }
428 CHECK("dns_message_sectiontotext", result);
429 } else if (display_answer) {
430 dns_name_t *name;
431 dns_rdataset_t *rdataset;
432 isc_result_t loopresult;
433 dns_name_t empty_name;
434 dns_rdata_t rdata = DNS_RDATA_INIT;
435 unsigned int answerstyleflags = 0;
436
437 if (!display_crypto) {
438 answerstyleflags |= DNS_STYLEFLAG_NOCRYPTO;
439 }
440 if (display_unknown_format) {
441 answerstyleflags |= DNS_STYLEFLAG_UNKNOWNFORMAT;
442 }
443
444 dns_name_init(&empty_name, NULL);
445 result = dns_message_firstname(response, DNS_SECTION_ANSWER);
446 if (result != ISC_R_NOMORE) {
447 CHECK("dns_message_firstname", result);
448 }
449
450 for (;;) {
451 if (result == ISC_R_NOMORE) {
452 break;
453 }
454 CHECK("dns_message_nextname", result);
455 name = NULL;
456 dns_message_currentname(response, DNS_SECTION_ANSWER,
457 &name);
458
459 for (rdataset = ISC_LIST_HEAD(name->list);
460 rdataset != NULL;
461 rdataset = ISC_LIST_NEXT(rdataset, link))
462 {
463 loopresult = dns_rdataset_first(rdataset);
464 while (loopresult == ISC_R_SUCCESS) {
465 dns_rdataset_current(rdataset, &rdata);
466 result = dns_rdata_tofmttext(
467 &rdata, NULL, answerstyleflags,
468 0, 60, " ", buf);
469 if (result == ISC_R_NOSPACE) {
470 goto buftoosmall;
471 }
472 CHECK("dns_rdata_tofmttext", result);
473 loopresult =
474 dns_rdataset_next(rdataset);
475 dns_rdata_reset(&rdata);
476 if (strlen("\n") >=
477 isc_buffer_availablelength(buf)) {
478 goto buftoosmall;
479 }
480 isc_buffer_putstr(buf, "\n");
481 }
482 }
483 result = dns_message_nextname(response,
484 DNS_SECTION_ANSWER);
485 }
486 }
487
488 if (display_authority && !display_short_form) {
489 result = dns_message_sectiontotext(
490 response, DNS_SECTION_AUTHORITY, style, flags, buf);
491 if (result == ISC_R_NOSPACE) {
492 goto buftoosmall;
493 }
494 CHECK("dns_message_sectiontotext", result);
495 }
496
497 if (display_additional && !display_short_form) {
498 result = dns_message_sectiontotext(
499 response, DNS_SECTION_ADDITIONAL, style, flags, buf);
500 if (result == ISC_R_NOSPACE) {
501 goto buftoosmall;
502 }
503 CHECK("dns_message_sectiontotext", result);
504 }
505
506 if (display_additional && !display_short_form && display_headers) {
507 /*
508 * Only print the signature on the first record.
509 */
510 result = dns_message_pseudosectiontotext(
511 response, DNS_PSEUDOSECTION_TSIG, style, flags, buf);
512 if (result == ISC_R_NOSPACE) {
513 goto buftoosmall;
514 }
515 CHECK("dns_message_pseudosectiontotext", result);
516 result = dns_message_pseudosectiontotext(
517 response, DNS_PSEUDOSECTION_SIG0, style, flags, buf);
518 if (result == ISC_R_NOSPACE) {
519 goto buftoosmall;
520 }
521 CHECK("dns_message_pseudosectiontotext", result);
522 }
523
524 if (display_headers && display_comments && !display_short_form && !yaml)
525 {
526 printf("\n");
527 }
528
529 printf("%.*s", (int)isc_buffer_usedlength(buf),
530 (char *)isc_buffer_base(buf));
531 isc_buffer_free(&buf);
532
533 cleanup:
534 fflush(stdout);
535 if (style != NULL) {
536 dns_master_styledestroy(&style, mctx);
537 }
538 if (query != NULL) {
539 dns_message_detach(&query);
540 }
541 if (response != NULL) {
542 dns_message_detach(&response);
543 }
544 dns_request_destroy(&reqev->request);
545 isc_event_free(&event);
546
547 if (--onfly == 0) {
548 isc_app_shutdown();
549 }
550 return;
551 }
552
553 /*%
554 * Add EDNS0 option record to a message. Currently, the only supported
555 * options are UDP buffer size, the DO bit, and EDNS options
556 * (e.g., NSID, COOKIE, client-subnet)
557 */
558 static void
add_opt(dns_message_t * msg,uint16_t udpsize,uint16_t edns,unsigned int flags,dns_ednsopt_t * opts,size_t count)559 add_opt(dns_message_t *msg, uint16_t udpsize, uint16_t edns, unsigned int flags,
560 dns_ednsopt_t *opts, size_t count) {
561 dns_rdataset_t *rdataset = NULL;
562 isc_result_t result;
563
564 result = dns_message_buildopt(msg, &rdataset, edns, udpsize, flags,
565 opts, count);
566 CHECK("dns_message_buildopt", result);
567 result = dns_message_setopt(msg, rdataset);
568 CHECK("dns_message_setopt", result);
569 }
570
571 static void
compute_cookie(unsigned char * cookie,size_t len)572 compute_cookie(unsigned char *cookie, size_t len) {
573 /* XXXMPA need to fix, should be per server. */
574 INSIST(len >= 8U);
575 memmove(cookie, cookie_secret, 8);
576 }
577
578 static isc_result_t
sendquery(struct query * query,isc_task_t * task)579 sendquery(struct query *query, isc_task_t *task) {
580 dns_request_t *request = NULL;
581 dns_message_t *message = NULL;
582 dns_name_t *qname = NULL;
583 dns_rdataset_t *qrdataset = NULL;
584 isc_result_t result;
585 dns_fixedname_t queryname;
586 isc_buffer_t buf;
587 unsigned int options;
588
589 onfly++;
590
591 dns_fixedname_init(&queryname);
592 isc_buffer_init(&buf, query->textname, strlen(query->textname));
593 isc_buffer_add(&buf, strlen(query->textname));
594 result = dns_name_fromtext(dns_fixedname_name(&queryname), &buf,
595 dns_rootname, 0, NULL);
596 CHECK("dns_name_fromtext", result);
597
598 dns_message_create(mctx, DNS_MESSAGE_INTENTRENDER, &message);
599
600 message->opcode = dns_opcode_query;
601 if (query->recurse) {
602 message->flags |= DNS_MESSAGEFLAG_RD;
603 }
604 if (query->have_aaonly) {
605 message->flags |= DNS_MESSAGEFLAG_AA;
606 }
607 if (query->have_adflag) {
608 message->flags |= DNS_MESSAGEFLAG_AD;
609 }
610 if (query->have_cdflag) {
611 message->flags |= DNS_MESSAGEFLAG_CD;
612 }
613 if (query->have_zflag) {
614 message->flags |= 0x0040U;
615 }
616 message->rdclass = query->rdclass;
617 message->id = (unsigned short)(random() & 0xFFFF);
618
619 result = dns_message_gettempname(message, &qname);
620 CHECK("dns_message_gettempname", result);
621
622 result = dns_message_gettemprdataset(message, &qrdataset);
623 CHECK("dns_message_gettemprdataset", result);
624
625 dns_name_clone(dns_fixedname_name(&queryname), qname);
626 dns_rdataset_makequestion(qrdataset, query->rdclass, query->rdtype);
627 ISC_LIST_APPEND(qname->list, qrdataset, link);
628 dns_message_addname(message, qname, DNS_SECTION_QUESTION);
629
630 if (query->udpsize > 0 || query->dnssec || query->edns > -1 ||
631 query->ecs_addr != NULL)
632 {
633 dns_ednsopt_t opts[EDNSOPTS + DNS_EDNSOPTIONS];
634 unsigned int flags;
635 int i = 0;
636 char ecsbuf[20];
637 unsigned char cookie[40];
638
639 if (query->udpsize == 0) {
640 query->udpsize = 1232;
641 }
642 if (query->edns < 0) {
643 query->edns = 0;
644 }
645
646 if (query->nsid) {
647 INSIST(i < DNS_EDNSOPTIONS);
648 opts[i].code = DNS_OPT_NSID;
649 opts[i].length = 0;
650 opts[i].value = NULL;
651 i++;
652 }
653
654 if (query->ecs_addr != NULL) {
655 uint8_t addr[16], family;
656 uint32_t plen;
657 struct sockaddr *sa;
658 struct sockaddr_in *sin;
659 struct sockaddr_in6 *sin6;
660 size_t addrl;
661 isc_buffer_t b;
662
663 sa = &query->ecs_addr->type.sa;
664 plen = query->ecs_addr->length;
665
666 /* Round up prefix len to a multiple of 8 */
667 addrl = (plen + 7) / 8;
668
669 INSIST(i < DNS_EDNSOPTIONS);
670 opts[i].code = DNS_OPT_CLIENT_SUBNET;
671 opts[i].length = (uint16_t)addrl + 4;
672 CHECK("isc_buffer_allocate", result);
673 isc_buffer_init(&b, ecsbuf, sizeof(ecsbuf));
674 if (sa->sa_family == AF_INET) {
675 family = 1;
676 sin = (struct sockaddr_in *)sa;
677 memmove(addr, &sin->sin_addr, 4);
678 if ((plen % 8) != 0) {
679 addr[addrl - 1] &= ~0U
680 << (8 - (plen % 8));
681 }
682 } else {
683 family = 2;
684 sin6 = (struct sockaddr_in6 *)sa;
685 memmove(addr, &sin6->sin6_addr, 16);
686 }
687
688 /* Mask off last address byte */
689 if (addrl > 0 && (plen % 8) != 0) {
690 addr[addrl - 1] &= ~0U << (8 - (plen % 8));
691 }
692
693 /* family */
694 isc_buffer_putuint16(&b, family);
695 /* source prefix-length */
696 isc_buffer_putuint8(&b, plen);
697 /* scope prefix-length */
698 isc_buffer_putuint8(&b, 0);
699 /* address */
700 if (addrl > 0) {
701 isc_buffer_putmem(&b, addr, (unsigned)addrl);
702 }
703
704 opts[i].value = (uint8_t *)ecsbuf;
705 i++;
706 }
707
708 if (query->send_cookie) {
709 INSIST(i < DNS_EDNSOPTIONS);
710 opts[i].code = DNS_OPT_COOKIE;
711 if (query->cookie != NULL) {
712 isc_buffer_t b;
713
714 isc_buffer_init(&b, cookie, sizeof(cookie));
715 result = isc_hex_decodestring(query->cookie,
716 &b);
717 CHECK("isc_hex_decodestring", result);
718 opts[i].value = isc_buffer_base(&b);
719 opts[i].length = isc_buffer_usedlength(&b);
720 } else {
721 compute_cookie(cookie, 8);
722 opts[i].length = 8;
723 opts[i].value = cookie;
724 }
725 i++;
726 }
727
728 if (query->expire) {
729 INSIST(i < DNS_EDNSOPTIONS);
730 opts[i].code = DNS_OPT_EXPIRE;
731 opts[i].length = 0;
732 opts[i].value = NULL;
733 i++;
734 }
735
736 if (query->ednsoptscnt != 0) {
737 memmove(&opts[i], query->ednsopts,
738 sizeof(dns_ednsopt_t) * query->ednsoptscnt);
739 i += query->ednsoptscnt;
740 }
741
742 flags = query->ednsflags;
743 flags &= ~DNS_MESSAGEEXTFLAG_DO;
744 if (query->dnssec) {
745 flags |= DNS_MESSAGEEXTFLAG_DO;
746 }
747 add_opt(message, query->udpsize, query->edns, flags, opts, i);
748 }
749
750 options = 0;
751 if (tcp_mode) {
752 options |= DNS_REQUESTOPT_TCP;
753 }
754 request = NULL;
755 result = dns_request_createvia(
756 requestmgr, message, have_src ? &srcaddr : NULL, &dstaddr, dscp,
757 options, NULL, query->timeout, query->udptimeout,
758 query->udpretries, task, recvresponse, message, &request);
759 CHECK("dns_request_createvia", result);
760
761 return (ISC_R_SUCCESS);
762 }
763
764 static void
sendqueries(isc_task_t * task,isc_event_t * event)765 sendqueries(isc_task_t *task, isc_event_t *event) {
766 struct query *query = (struct query *)event->ev_arg;
767
768 isc_event_free(&event);
769
770 while (query != NULL) {
771 struct query *next = ISC_LIST_NEXT(query, link);
772
773 sendquery(query, task);
774 query = next;
775 }
776
777 if (onfly == 0) {
778 isc_app_shutdown();
779 }
780 return;
781 }
782
783 ISC_NORETURN static void
784 usage(void);
785
786 static void
usage(void)787 usage(void) {
788 fprintf(stderr, "Usage: mdig @server {global-opt} host\n"
789 " {local-opt} [ host {local-opt} [...]]\n"
790 "\nUse \"mdig -h\" (or \"mdig -h | more\") "
791 "for complete list of options\n");
792 exit(1);
793 }
794
795 /*% help */
796 static void
help(void)797 help(void) {
798 printf("Usage: mdig @server {global-opt} host\n"
799 " {local-opt} [ host {local-opt} [...]]\n"
800 "Where:\n"
801 " anywhere opt is one of:\n"
802 " -f filename (batch mode)\n"
803 " -h (print help and exit)\n"
804 " -v (print version and exit)\n"
805 " global opt is one of:\n"
806 " -4 (use IPv4 query transport "
807 "only)\n"
808 " -6 (use IPv6 query transport "
809 "only)\n"
810 " -b address[#port] (bind to source "
811 "address/port)\n"
812 " -p port (specify port number)\n"
813 " -m (enable memory usage "
814 "debugging)\n"
815 " +[no]dscp[=###] (Set the DSCP value to "
816 "### "
817 "[0..63])\n"
818 " +[no]vc (TCP mode)\n"
819 " +[no]tcp (TCP mode, alternate "
820 "syntax)\n"
821 " +[no]besteffort (Try to parse even "
822 "illegal "
823 "messages)\n"
824 " +[no]cl (Control display of class "
825 "in records)\n"
826 " +[no]comments (Control display of "
827 "comment lines)\n"
828 " +[no]rrcomments (Control display of "
829 "per-record "
830 "comments)\n"
831 " +[no]crypto (Control display of "
832 "cryptographic "
833 "fields in records)\n"
834 " +[no]question (Control display of "
835 "question)\n"
836 " +[no]answer (Control display of "
837 "answer)\n"
838 " +[no]authority (Control display of "
839 "authority)\n"
840 " +[no]additional (Control display of "
841 "additional)\n"
842 " +[no]short (Disable everything "
843 "except "
844 "short\n"
845 " form of answer)\n"
846 " +[no]ttlid (Control display of ttls "
847 "in records)\n"
848 " +[no]ttlunits (Display TTLs in "
849 "human-readable units)\n"
850 " +[no]unknownformat (Print RDATA in RFC 3597 "
851 "\"unknown\" format)\n"
852 " +[no]all (Set or clear all display "
853 "flags)\n"
854 " +[no]multiline (Print records in an "
855 "expanded format)\n"
856 " +[no]split=## (Split hex/base64 fields "
857 "into chunks)\n"
858 " local opt is one of:\n"
859 " -c class (specify query class)\n"
860 " -t type (specify query type)\n"
861 " -x dot-notation (shortcut for reverse "
862 "lookups)\n"
863 " +timeout=### (Set query timeout) "
864 "[UDP=5,TCP=10]\n"
865 " +udptimeout=### (Set timeout before UDP "
866 "retry)\n"
867 " +tries=### (Set number of UDP "
868 "attempts) [3]\n"
869 " +retry=### (Set number of UDP "
870 "retries) [2]\n"
871 " +bufsize=### (Set EDNS0 Max UDP packet "
872 "size)\n"
873 " +subnet=addr (Set edns-client-subnet "
874 "option)\n"
875 " +[no]edns[=###] (Set EDNS version) [0]\n"
876 " +ednsflags=### (Set EDNS flag bits)\n"
877 " +ednsopt=###[:value] (Send specified EDNS "
878 "option)\n"
879 " +noednsopt (Clear list of +ednsopt "
880 "options)\n"
881 " +[no]recurse (Recursive mode)\n"
882 " +[no]aaonly (Set AA flag in query "
883 "(+[no]aaflag))\n"
884 " +[no]adflag (Set AD flag in query)\n"
885 " +[no]cdflag (Set CD flag in query)\n"
886 " +[no]zflag (Set Z flag in query)\n"
887 " +[no]dnssec (Request DNSSEC records)\n"
888 " +[no]expire (Request time to expire)\n"
889 " +[no]cookie[=###] (Send a COOKIE option)\n"
890 " +[no]nsid (Request Name "
891 "Server ID)\n");
892 }
893
894 ISC_NORETURN static void
895 fatal(const char *format, ...) ISC_FORMAT_PRINTF(1, 2);
896
897 static void
fatal(const char * format,...)898 fatal(const char *format, ...) {
899 va_list args;
900
901 fflush(stdout);
902 fprintf(stderr, "mdig: ");
903 va_start(args, format);
904 vfprintf(stderr, format, args);
905 va_end(args);
906 fprintf(stderr, "\n");
907 exit(-2);
908 }
909
910 static isc_result_t
parse_uint_helper(uint32_t * uip,const char * value,uint32_t max,const char * desc,int base)911 parse_uint_helper(uint32_t *uip, const char *value, uint32_t max,
912 const char *desc, int base) {
913 uint32_t n;
914 isc_result_t result = isc_parse_uint32(&n, value, base);
915 if (result == ISC_R_SUCCESS && n > max) {
916 result = ISC_R_RANGE;
917 }
918 if (result != ISC_R_SUCCESS) {
919 printf("invalid %s '%s': %s\n", desc, value,
920 isc_result_totext(result));
921 return (result);
922 }
923 *uip = n;
924 return (ISC_R_SUCCESS);
925 }
926
927 static isc_result_t
parse_uint(uint32_t * uip,const char * value,uint32_t max,const char * desc)928 parse_uint(uint32_t *uip, const char *value, uint32_t max, const char *desc) {
929 return (parse_uint_helper(uip, value, max, desc, 10));
930 }
931
932 static isc_result_t
parse_xint(uint32_t * uip,const char * value,uint32_t max,const char * desc)933 parse_xint(uint32_t *uip, const char *value, uint32_t max, const char *desc) {
934 return (parse_uint_helper(uip, value, max, desc, 0));
935 }
936
937 static void
newopts(struct query * query)938 newopts(struct query *query) {
939 size_t len = sizeof(query->ednsopts[0]) * EDNSOPTS;
940 size_t i;
941
942 query->ednsopts = isc_mem_allocate(mctx, len);
943
944 for (i = 0; i < EDNSOPTS; i++) {
945 query->ednsopts[i].code = 0;
946 query->ednsopts[i].length = 0;
947 query->ednsopts[i].value = NULL;
948 }
949 }
950
951 static void
save_opt(struct query * query,char * code,char * value)952 save_opt(struct query *query, char *code, char *value) {
953 uint32_t num;
954 isc_buffer_t b;
955 isc_result_t result;
956
957 if (query->ednsopts == NULL) {
958 newopts(query);
959 }
960
961 if (query->ednsoptscnt == EDNSOPTS) {
962 fatal("too many ednsopts");
963 }
964
965 result = parse_uint(&num, code, 65535, "ednsopt");
966 if (result != ISC_R_SUCCESS) {
967 fatal("bad edns code point: %s", code);
968 }
969
970 query->ednsopts[query->ednsoptscnt].code = num;
971 query->ednsopts[query->ednsoptscnt].length = 0;
972 query->ednsopts[query->ednsoptscnt].value = NULL;
973
974 if (value != NULL) {
975 char *buf;
976 buf = isc_mem_allocate(mctx, strlen(value) / 2 + 1);
977 isc_buffer_init(&b, buf, strlen(value) / 2 + 1);
978 result = isc_hex_decodestring(value, &b);
979 CHECK("isc_hex_decodestring", result);
980 query->ednsopts[query->ednsoptscnt].value = isc_buffer_base(&b);
981 query->ednsopts[query->ednsoptscnt].length =
982 isc_buffer_usedlength(&b);
983 }
984
985 query->ednsoptscnt++;
986 }
987
988 static isc_result_t
parse_netprefix(isc_sockaddr_t ** sap,const char * value)989 parse_netprefix(isc_sockaddr_t **sap, const char *value) {
990 isc_sockaddr_t *sa = NULL;
991 struct in_addr in4;
992 struct in6_addr in6;
993 uint32_t netmask = 0xffffffff;
994 char *slash = NULL;
995 bool parsed = false;
996 char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:XXX.XXX.XXX.XXX/128")];
997
998 if (strlcpy(buf, value, sizeof(buf)) >= sizeof(buf)) {
999 fatal("invalid prefix '%s'\n", value);
1000 }
1001
1002 slash = strchr(buf, '/');
1003 if (slash != NULL) {
1004 isc_result_t result;
1005 *slash = '\0';
1006 result = isc_parse_uint32(&netmask, slash + 1, 10);
1007 if (result != ISC_R_SUCCESS) {
1008 fatal("invalid prefix length in '%s': %s\n", value,
1009 isc_result_totext(result));
1010 }
1011 } else if (strcmp(value, "0") == 0) {
1012 netmask = 0;
1013 }
1014
1015 sa = isc_mem_allocate(mctx, sizeof(*sa));
1016 if (inet_pton(AF_INET6, buf, &in6) == 1) {
1017 parsed = true;
1018 isc_sockaddr_fromin6(sa, &in6, 0);
1019 if (netmask > 128) {
1020 netmask = 128;
1021 }
1022 } else if (inet_pton(AF_INET, buf, &in4) == 1) {
1023 parsed = true;
1024 isc_sockaddr_fromin(sa, &in4, 0);
1025 if (netmask > 32) {
1026 netmask = 32;
1027 }
1028 } else if (netmask != 0xffffffff) {
1029 int i;
1030
1031 for (i = 0; i < 3 && strlen(buf) < sizeof(buf) - 2; i++) {
1032 strlcat(buf, ".0", sizeof(buf));
1033 if (inet_pton(AF_INET, buf, &in4) == 1) {
1034 parsed = true;
1035 isc_sockaddr_fromin(sa, &in4, 0);
1036 break;
1037 }
1038 }
1039
1040 if (netmask > 32) {
1041 netmask = 32;
1042 }
1043 }
1044
1045 if (!parsed) {
1046 fatal("invalid address '%s'", value);
1047 }
1048
1049 sa->length = netmask;
1050 *sap = sa;
1051
1052 return (ISC_R_SUCCESS);
1053 }
1054
1055 /*%
1056 * Append 'len' bytes of 'text' at '*p', failing with
1057 * ISC_R_NOSPACE if that would advance p past 'end'.
1058 */
1059 static isc_result_t
append(const char * text,int len,char ** p,char * end)1060 append(const char *text, int len, char **p, char *end) {
1061 if (len > end - *p) {
1062 return (ISC_R_NOSPACE);
1063 }
1064 memmove(*p, text, len);
1065 *p += len;
1066 return (ISC_R_SUCCESS);
1067 }
1068
1069 static isc_result_t
reverse_octets(const char * in,char ** p,char * end)1070 reverse_octets(const char *in, char **p, char *end) {
1071 const char *dot = strchr(in, '.');
1072 int len;
1073 if (dot != NULL) {
1074 isc_result_t result;
1075 result = reverse_octets(dot + 1, p, end);
1076 CHECK("reverse_octets", result);
1077 result = append(".", 1, p, end);
1078 CHECK("append", result);
1079 len = (int)(dot - in);
1080 } else {
1081 len = strlen(in);
1082 }
1083 return (append(in, len, p, end));
1084 }
1085
1086 static void
get_reverse(char * reverse,size_t len,const char * value)1087 get_reverse(char *reverse, size_t len, const char *value) {
1088 int r;
1089 isc_result_t result;
1090 isc_netaddr_t addr;
1091
1092 addr.family = AF_INET6;
1093 r = inet_pton(AF_INET6, value, &addr.type.in6);
1094 if (r > 0) {
1095 /* This is a valid IPv6 address. */
1096 dns_fixedname_t fname;
1097 dns_name_t *name;
1098 unsigned int options = 0;
1099
1100 name = dns_fixedname_initname(&fname);
1101 result = dns_byaddr_createptrname(&addr, options, name);
1102 CHECK("dns_byaddr_createptrname2", result);
1103 dns_name_format(name, reverse, (unsigned int)len);
1104 return;
1105 } else {
1106 /*
1107 * Not a valid IPv6 address. Assume IPv4.
1108 * Construct the in-addr.arpa name by blindly
1109 * reversing octets whether or not they look like
1110 * integers, so that this can be used for RFC2317
1111 * names and such.
1112 */
1113 char *p = reverse;
1114 char *end = reverse + len;
1115 result = reverse_octets(value, &p, end);
1116 CHECK("reverse_octets", result);
1117 /* Append .in-addr.arpa. and a terminating NUL. */
1118 result = append(".in-addr.arpa.", 15, &p, end);
1119 CHECK("append", result);
1120 return;
1121 }
1122 }
1123
1124 /*%
1125 * We're not using isc_commandline_parse() here since the command line
1126 * syntax of mdig is quite a bit different from that which can be described
1127 * by that routine.
1128 * XXX doc options
1129 */
1130
1131 static void
plus_option(char * option,struct query * query,bool global)1132 plus_option(char *option, struct query *query, bool global) {
1133 isc_result_t result;
1134 char *cmd, *value, *last = NULL, *code;
1135 uint32_t num;
1136 bool state = true;
1137 size_t n;
1138
1139 INSIST(option != NULL);
1140
1141 if ((cmd = strtok_r(option, "=", &last)) == NULL) {
1142 printf(";; Invalid option %s\n", option);
1143 return;
1144 }
1145 if (strncasecmp(cmd, "no", 2) == 0) {
1146 cmd += 2;
1147 state = false;
1148 }
1149 /* parse the rest of the string */
1150 value = strtok_r(NULL, "", &last);
1151
1152 #define FULLCHECK(A) \
1153 do { \
1154 size_t _l = strlen(cmd); \
1155 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
1156 goto invalid_option; \
1157 } while (0)
1158 #define FULLCHECK2(A, B) \
1159 do { \
1160 size_t _l = strlen(cmd); \
1161 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
1162 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
1163 goto invalid_option; \
1164 } while (0)
1165 #define GLOBAL() \
1166 do { \
1167 if (!global) \
1168 goto global_option; \
1169 } while (0)
1170
1171 switch (cmd[0]) {
1172 case 'a':
1173 switch (cmd[1]) {
1174 case 'a': /* aaonly / aaflag */
1175 FULLCHECK2("aaonly", "aaflag");
1176 query->have_aaonly = state;
1177 break;
1178 case 'd':
1179 switch (cmd[2]) {
1180 case 'd': /* additional */
1181 FULLCHECK("additional");
1182 display_additional = state;
1183 break;
1184 case 'f': /* adflag */
1185 case '\0': /* +ad is a synonym for +adflag */
1186 FULLCHECK("adflag");
1187 query->have_adflag = state;
1188 break;
1189 default:
1190 goto invalid_option;
1191 }
1192 break;
1193 case 'l': /* all */
1194 FULLCHECK("all");
1195 GLOBAL();
1196 display_question = state;
1197 display_answer = state;
1198 display_authority = state;
1199 display_additional = state;
1200 display_comments = state;
1201 display_rrcomments = state ? 1 : -1;
1202 break;
1203 case 'n': /* answer */
1204 FULLCHECK("answer");
1205 GLOBAL();
1206 display_answer = state;
1207 break;
1208 case 'u': /* authority */
1209 FULLCHECK("authority");
1210 GLOBAL();
1211 display_authority = state;
1212 break;
1213 default:
1214 goto invalid_option;
1215 }
1216 break;
1217 case 'b':
1218 switch (cmd[1]) {
1219 case 'e': /* besteffort */
1220 FULLCHECK("besteffort");
1221 GLOBAL();
1222 besteffort = state;
1223 break;
1224 case 'u':
1225 switch (cmd[2]) {
1226 case 'f': /* bufsize */
1227 FULLCHECK("bufsize");
1228 if (value == NULL) {
1229 goto need_value;
1230 }
1231 if (!state) {
1232 goto invalid_option;
1233 }
1234 result = parse_uint(&num, value, COMMSIZE,
1235 "buffer size");
1236 CHECK("parse_uint(buffer size)", result);
1237 query->udpsize = num;
1238 break;
1239 case 'r': /* burst */
1240 FULLCHECK("burst");
1241 GLOBAL();
1242 burst = state;
1243 break;
1244 default:
1245 goto invalid_option;
1246 }
1247 break;
1248 default:
1249 goto invalid_option;
1250 }
1251 break;
1252 case 'c':
1253 switch (cmd[1]) {
1254 case 'd': /* cdflag */
1255 switch (cmd[2]) {
1256 case 'f': /* cdflag */
1257 case '\0': /* +cd is a synonym for +cdflag */
1258 FULLCHECK("cdflag");
1259 query->have_cdflag = state;
1260 break;
1261 default:
1262 goto invalid_option;
1263 }
1264 break;
1265 case 'l': /* cl */
1266 FULLCHECK("cl");
1267 GLOBAL();
1268 display_class = state;
1269 break;
1270 case 'o': /* comments */
1271 switch (cmd[2]) {
1272 case 'm':
1273 FULLCHECK("comments");
1274 GLOBAL();
1275 display_comments = state;
1276 break;
1277 case 'n':
1278 FULLCHECK("continue");
1279 GLOBAL();
1280 continue_on_error = state;
1281 break;
1282 case 'o':
1283 FULLCHECK("cookie");
1284 if (state && query->edns == -1) {
1285 query->edns = 0;
1286 }
1287 query->send_cookie = state;
1288 if (value != NULL) {
1289 n = strlcpy(hexcookie, value,
1290 sizeof(hexcookie));
1291 if (n >= sizeof(hexcookie)) {
1292 fatal("COOKIE data too large");
1293 }
1294 query->cookie = hexcookie;
1295 } else {
1296 query->cookie = NULL;
1297 }
1298 break;
1299 default:
1300 goto invalid_option;
1301 }
1302 break;
1303 case 'r':
1304 FULLCHECK("crypto");
1305 GLOBAL();
1306 display_crypto = state;
1307 break;
1308 default:
1309 goto invalid_option;
1310 }
1311 break;
1312 case 'd':
1313 switch (cmd[1]) {
1314 case 'n': /* dnssec */
1315 FULLCHECK("dnssec");
1316 if (state && query->edns == -1) {
1317 query->edns = 0;
1318 }
1319 query->dnssec = state;
1320 break;
1321 case 's': /* dscp */
1322 FULLCHECK("dscp");
1323 GLOBAL();
1324 if (!state) {
1325 dscp = -1;
1326 break;
1327 }
1328 if (value == NULL) {
1329 goto need_value;
1330 }
1331 result = parse_uint(&num, value, 0x3f, "DSCP");
1332 CHECK("parse_uint(DSCP)", result);
1333 dscp = num;
1334 break;
1335 default:
1336 goto invalid_option;
1337 }
1338 break;
1339 case 'e':
1340 switch (cmd[1]) {
1341 case 'd':
1342 switch (cmd[2]) {
1343 case 'n':
1344 switch (cmd[3]) {
1345 case 's':
1346 switch (cmd[4]) {
1347 case 0:
1348 FULLCHECK("edns");
1349 if (!state) {
1350 query->edns = -1;
1351 break;
1352 }
1353 if (value == NULL) {
1354 query->edns = 0;
1355 break;
1356 }
1357 result = parse_uint(&num, value,
1358 255,
1359 "edns");
1360 CHECK("parse_uint(edns)",
1361 result);
1362 query->edns = num;
1363 break;
1364 case 'f':
1365 FULLCHECK("ednsflags");
1366 if (!state) {
1367 query->ednsflags = 0;
1368 break;
1369 }
1370 if (value == NULL) {
1371 query->ednsflags = 0;
1372 break;
1373 }
1374 result = parse_xint(
1375 &num, value, 0xffff,
1376 "ednsflags");
1377 CHECK("parse_xint(ednsflags)",
1378 result);
1379 query->ednsflags = num;
1380 break;
1381 case 'o':
1382 FULLCHECK("ednsopt");
1383 if (!state) {
1384 query->ednsoptscnt = 0;
1385 break;
1386 }
1387 code = NULL;
1388 if (value != NULL) {
1389 code = strtok_r(value,
1390 ":",
1391 &last);
1392 }
1393 if (code == NULL) {
1394 fatal("ednsopt no "
1395 "code point "
1396 "specified");
1397 }
1398 value = strtok_r(NULL, "\0",
1399 &last);
1400 save_opt(query, code, value);
1401 break;
1402 default:
1403 goto invalid_option;
1404 }
1405 break;
1406 default:
1407 goto invalid_option;
1408 }
1409 break;
1410 default:
1411 goto invalid_option;
1412 }
1413 break;
1414 case 'x':
1415 FULLCHECK("expire");
1416 query->expire = state;
1417 break;
1418 default:
1419 goto invalid_option;
1420 }
1421 break;
1422 case 'm': /* multiline */
1423 FULLCHECK("multiline");
1424 GLOBAL();
1425 display_multiline = state;
1426 break;
1427 case 'n':
1428 FULLCHECK("nsid");
1429 if (state && query->edns == -1) {
1430 query->edns = 0;
1431 }
1432 query->nsid = state;
1433 break;
1434 case 'q':
1435 FULLCHECK("question");
1436 GLOBAL();
1437 display_question = state;
1438 break;
1439 case 'r':
1440 switch (cmd[1]) {
1441 case 'e':
1442 switch (cmd[2]) {
1443 case 'c': /* recurse */
1444 FULLCHECK("recurse");
1445 query->recurse = state;
1446 break;
1447 case 't': /* retry / retries */
1448 FULLCHECK2("retry", "retries");
1449 if (value == NULL) {
1450 goto need_value;
1451 }
1452 if (!state) {
1453 goto invalid_option;
1454 }
1455 result = parse_uint(&query->udpretries, value,
1456 MAXTRIES - 1, "udpretries");
1457 CHECK("parse_uint(udpretries)", result);
1458 query->udpretries++;
1459 break;
1460 default:
1461 goto invalid_option;
1462 }
1463 break;
1464 case 'r':
1465 FULLCHECK("rrcomments");
1466 GLOBAL();
1467 display_rrcomments = state ? 1 : -1;
1468 break;
1469 default:
1470 goto invalid_option;
1471 }
1472 break;
1473 case 's':
1474 switch (cmd[1]) {
1475 case 'h':
1476 FULLCHECK("short");
1477 GLOBAL();
1478 display_short_form = state;
1479 if (state) {
1480 display_question = false;
1481 display_answer = true;
1482 display_authority = false;
1483 display_additional = false;
1484 display_comments = false;
1485 display_rrcomments = -1;
1486 }
1487 break;
1488 case 'p': /* split */
1489 FULLCHECK("split");
1490 GLOBAL();
1491 if (value != NULL && !state) {
1492 goto invalid_option;
1493 }
1494 if (!state) {
1495 display_splitwidth = 0;
1496 break;
1497 } else if (value == NULL) {
1498 break;
1499 }
1500
1501 result = parse_uint(&display_splitwidth, value, 1023,
1502 "split");
1503 if ((display_splitwidth % 4) != 0) {
1504 display_splitwidth =
1505 ((display_splitwidth + 3) / 4) * 4;
1506 fprintf(stderr,
1507 ";; Warning, split must be "
1508 "a multiple of 4; adjusting "
1509 "to %u\n",
1510 display_splitwidth);
1511 }
1512 /*
1513 * There is an adjustment done in the
1514 * totext_<rrtype>() functions which causes
1515 * splitwidth to shrink. This is okay when we're
1516 * using the default width but incorrect in this
1517 * case, so we correct for it
1518 */
1519 if (display_splitwidth) {
1520 display_splitwidth += 3;
1521 }
1522 CHECK("parse_uint(split)", result);
1523 break;
1524 case 'u': /* subnet */
1525 FULLCHECK("subnet");
1526 if (state && value == NULL) {
1527 goto need_value;
1528 }
1529 if (!state) {
1530 if (query->ecs_addr != NULL) {
1531 isc_mem_free(mctx, query->ecs_addr);
1532 query->ecs_addr = NULL;
1533 }
1534 break;
1535 }
1536 if (query->edns == -1) {
1537 query->edns = 0;
1538 }
1539 result = parse_netprefix(&query->ecs_addr, value);
1540 CHECK("parse_netprefix", result);
1541 break;
1542 default:
1543 goto invalid_option;
1544 }
1545 break;
1546 case 't':
1547 switch (cmd[1]) {
1548 case 'c': /* tcp */
1549 FULLCHECK("tcp");
1550 GLOBAL();
1551 tcp_mode = state;
1552 break;
1553 case 'i': /* timeout */
1554 FULLCHECK("timeout");
1555 if (value == NULL) {
1556 goto need_value;
1557 }
1558 if (!state) {
1559 goto invalid_option;
1560 }
1561 result = parse_uint(&query->timeout, value, MAXTIMEOUT,
1562 "timeout");
1563 CHECK("parse_uint(timeout)", result);
1564 if (query->timeout == 0) {
1565 query->timeout = 1;
1566 }
1567 break;
1568 case 'r':
1569 FULLCHECK("tries");
1570 if (value == NULL) {
1571 goto need_value;
1572 }
1573 if (!state) {
1574 goto invalid_option;
1575 }
1576 result = parse_uint(&query->udpretries, value, MAXTRIES,
1577 "udpretries");
1578 CHECK("parse_uint(udpretries)", result);
1579 if (query->udpretries == 0) {
1580 query->udpretries = 1;
1581 }
1582 break;
1583 case 't':
1584 switch (cmd[2]) {
1585 case 'l':
1586 switch (cmd[3]) {
1587 case 0:
1588 case 'i': /* ttlid */
1589 FULLCHECK2("ttl", "ttlid");
1590 GLOBAL();
1591 display_ttl = state;
1592 break;
1593 case 'u': /* ttlunits */
1594 FULLCHECK("ttlunits");
1595 GLOBAL();
1596 display_ttl = true;
1597 display_ttlunits = state;
1598 break;
1599 default:
1600 goto invalid_option;
1601 }
1602 break;
1603 default:
1604 goto invalid_option;
1605 }
1606 break;
1607 default:
1608 goto invalid_option;
1609 }
1610 break;
1611 case 'u':
1612 switch (cmd[1]) {
1613 case 'd':
1614 FULLCHECK("udptimeout");
1615 if (value == NULL) {
1616 goto need_value;
1617 }
1618 if (!state) {
1619 goto invalid_option;
1620 }
1621 result = parse_uint(&query->udptimeout, value,
1622 MAXTIMEOUT, "udptimeout");
1623 CHECK("parse_uint(udptimeout)", result);
1624 break;
1625 case 'n':
1626 FULLCHECK("unknownformat");
1627 display_unknown_format = state;
1628 break;
1629 default:
1630 goto invalid_option;
1631 }
1632 break;
1633 case 'v':
1634 FULLCHECK("vc");
1635 GLOBAL();
1636 tcp_mode = state;
1637 break;
1638 case 'y': /* yaml */
1639 FULLCHECK("yaml");
1640 yaml = state;
1641 if (state) {
1642 display_rrcomments = state;
1643 }
1644 break;
1645 case 'z': /* zflag */
1646 FULLCHECK("zflag");
1647 query->have_zflag = state;
1648 break;
1649 global_option:
1650 fprintf(stderr, "Ignored late global option: +%s\n", option);
1651 break;
1652 default:
1653 invalid_option:
1654 need_value:
1655 fprintf(stderr, "Invalid option: +%s\n", option);
1656 usage();
1657 }
1658 return;
1659 }
1660
1661 /*%
1662 * #true returned if value was used
1663 */
1664 static const char *single_dash_opts = "46himv";
1665 static const char *dash_opts = "46bcfhiptvx";
1666 static bool
dash_option(const char * option,char * next,struct query * query,bool global,bool * setname)1667 dash_option(const char *option, char *next, struct query *query, bool global,
1668 bool *setname) {
1669 char opt;
1670 const char *value;
1671 isc_result_t result;
1672 bool value_from_next;
1673 isc_consttextregion_t tr;
1674 dns_rdatatype_t rdtype;
1675 dns_rdataclass_t rdclass;
1676 char textname[MXNAME];
1677 struct in_addr in4;
1678 struct in6_addr in6;
1679 in_port_t srcport;
1680 char *hash;
1681 uint32_t num;
1682
1683 while (strpbrk(option, single_dash_opts) == &option[0]) {
1684 /*
1685 * Since the -[46hiv] options do not take an argument,
1686 * account for them (in any number and/or combination)
1687 * if they appear as the first character(s) of an opt.
1688 */
1689 opt = option[0];
1690 switch (opt) {
1691 case '4':
1692 GLOBAL();
1693 if (have_ipv4) {
1694 isc_net_disableipv6();
1695 have_ipv6 = false;
1696 } else {
1697 fatal("can't find IPv4 networking");
1698 /* NOTREACHED */
1699 return (false);
1700 }
1701 break;
1702 case '6':
1703 GLOBAL();
1704 if (have_ipv6) {
1705 isc_net_disableipv4();
1706 have_ipv4 = false;
1707 } else {
1708 fatal("can't find IPv6 networking");
1709 /* NOTREACHED */
1710 return (false);
1711 }
1712 break;
1713 case 'h':
1714 help();
1715 exit(0);
1716 break;
1717 case 'i':
1718 /* deprecated */
1719 break;
1720 case 'm':
1721 /*
1722 * handled by preparse_args()
1723 */
1724 break;
1725 case 'v':
1726 fprintf(stderr, "mDiG %s\n", PACKAGE_VERSION);
1727 exit(0);
1728 break;
1729 }
1730 if (strlen(option) > 1U) {
1731 option = &option[1];
1732 } else {
1733 return (false);
1734 }
1735 }
1736 opt = option[0];
1737 if (strlen(option) > 1U) {
1738 value_from_next = false;
1739 value = &option[1];
1740 } else {
1741 value_from_next = true;
1742 value = next;
1743 }
1744 if (value == NULL) {
1745 goto invalid_option;
1746 }
1747 switch (opt) {
1748 case 'b':
1749 GLOBAL();
1750 hash = strchr(value, '#');
1751 if (hash != NULL) {
1752 result = parse_uint(&num, hash + 1, MAXPORT,
1753 "port number");
1754 CHECK("parse_uint(srcport)", result);
1755 srcport = num;
1756 *hash = '\0';
1757 } else {
1758 srcport = 0;
1759 }
1760 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1761 isc_sockaddr_fromin6(&srcaddr, &in6, srcport);
1762 isc_net_disableipv4();
1763 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1764 isc_sockaddr_fromin(&srcaddr, &in4, srcport);
1765 isc_net_disableipv6();
1766 } else {
1767 if (hash != NULL) {
1768 *hash = '#';
1769 }
1770 fatal("invalid address %s", value);
1771 }
1772 if (hash != NULL) {
1773 *hash = '#';
1774 }
1775 have_src = true;
1776 return (value_from_next);
1777 case 'c':
1778 tr.base = value;
1779 tr.length = strlen(value);
1780 result = dns_rdataclass_fromtext(&rdclass,
1781 (isc_textregion_t *)&tr);
1782 CHECK("dns_rdataclass_fromtext", result);
1783 query->rdclass = rdclass;
1784 return (value_from_next);
1785 case 'f':
1786 batchname = value;
1787 return (value_from_next);
1788 case 'p':
1789 GLOBAL();
1790 result = parse_uint(&num, value, MAXPORT, "port number");
1791 CHECK("parse_uint(port)", result);
1792 port = num;
1793 return (value_from_next);
1794 case 't':
1795 tr.base = value;
1796 tr.length = strlen(value);
1797 result = dns_rdatatype_fromtext(&rdtype,
1798 (isc_textregion_t *)&tr);
1799 CHECK("dns_rdatatype_fromtext", result);
1800 query->rdtype = rdtype;
1801 return (value_from_next);
1802 case 'x':
1803 get_reverse(textname, sizeof(textname), value);
1804 strlcpy(query->textname, textname, sizeof(query->textname));
1805 query->rdtype = dns_rdatatype_ptr;
1806 query->rdclass = dns_rdataclass_in;
1807 *setname = true;
1808 return (value_from_next);
1809 global_option:
1810 fprintf(stderr, "Ignored late global option: -%s\n", option);
1811 usage();
1812 default:
1813 invalid_option:
1814 fprintf(stderr, "Invalid option: -%s\n", option);
1815 usage();
1816 }
1817 /* NOTREACHED */
1818 return (false);
1819 }
1820
1821 static struct query *
clone_default_query(void)1822 clone_default_query(void) {
1823 struct query *query;
1824
1825 query = isc_mem_allocate(mctx, sizeof(struct query));
1826 memmove(query, &default_query, sizeof(struct query));
1827 if (default_query.ecs_addr != NULL) {
1828 size_t len = sizeof(isc_sockaddr_t);
1829
1830 query->ecs_addr = isc_mem_allocate(mctx, len);
1831 memmove(query->ecs_addr, default_query.ecs_addr, len);
1832 }
1833
1834 if (query->timeout == 0) {
1835 query->timeout = tcp_mode ? TCPTIMEOUT : UDPTIMEOUT;
1836 }
1837
1838 return (query);
1839 }
1840
1841 /*%
1842 * Because we may be trying to do memory allocation recording, we're going
1843 * to need to parse the arguments for the -m *before* we start the main
1844 * argument parsing routine.
1845 *
1846 * I'd prefer not to have to do this, but I am not quite sure how else to
1847 * fix the problem. Argument parsing in mdig involves memory allocation
1848 * by its nature, so it can't be done in the main argument parser.
1849 */
1850 static void
preparse_args(int argc,char ** argv)1851 preparse_args(int argc, char **argv) {
1852 int rc;
1853 char **rv;
1854 char *option;
1855 bool ipv4only = false, ipv6only = false;
1856
1857 rc = argc;
1858 rv = argv;
1859 for (rc--, rv++; rc > 0; rc--, rv++) {
1860 if (rv[0][0] != '-') {
1861 continue;
1862 }
1863 option = &rv[0][1];
1864 while (strpbrk(option, single_dash_opts) == &option[0]) {
1865 switch (option[0]) {
1866 case 'm':
1867 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1868 ISC_MEM_DEBUGRECORD;
1869 break;
1870 case '4':
1871 if (ipv6only) {
1872 fatal("only one of -4 and -6 allowed");
1873 }
1874 ipv4only = true;
1875 break;
1876 case '6':
1877 if (ipv4only) {
1878 fatal("only one of -4 and -6 allowed");
1879 }
1880 ipv6only = true;
1881 break;
1882 }
1883 option = &option[1];
1884 }
1885 if (strlen(option) == 0U) {
1886 continue;
1887 }
1888 /* Look for dash value option. */
1889 if (strpbrk(option, dash_opts) != &option[0] ||
1890 strlen(option) > 1U) {
1891 /* Error or value in option. */
1892 continue;
1893 }
1894 /* Dash value is next argument so we need to skip it. */
1895 rc--, rv++;
1896 /* Handle missing argument */
1897 if (rc == 0) {
1898 break;
1899 }
1900 }
1901 }
1902
1903 static void
parse_args(bool is_batchfile,int argc,char ** argv)1904 parse_args(bool is_batchfile, int argc, char **argv) {
1905 struct query *query = NULL;
1906 char batchline[MXNAME];
1907 int bargc;
1908 char *bargv[64];
1909 int rc;
1910 char **rv;
1911 bool global = true;
1912 char *last;
1913
1914 /*
1915 * The semantics for parsing the args is a bit complex; if
1916 * we don't have a host yet, make the arg apply globally,
1917 * otherwise make it apply to the latest host. This is
1918 * a bit different than the previous versions, but should
1919 * form a consistent user interface.
1920 *
1921 * First, create a "default query" which won't actually be used
1922 * anywhere, except for cloning into new queries
1923 */
1924
1925 if (!is_batchfile) {
1926 default_query.textname[0] = 0;
1927 default_query.recurse = true;
1928 default_query.have_aaonly = false;
1929 default_query.have_adflag = true; /*XXX*/
1930 default_query.have_cdflag = false;
1931 default_query.have_zflag = false;
1932 default_query.dnssec = false;
1933 default_query.expire = false;
1934 default_query.send_cookie = false;
1935 default_query.cookie = NULL;
1936 default_query.nsid = false;
1937 default_query.rdtype = dns_rdatatype_a;
1938 default_query.rdclass = dns_rdataclass_in;
1939 default_query.udpsize = 0;
1940 default_query.edns = 0; /*XXX*/
1941 default_query.ednsopts = NULL;
1942 default_query.ednsoptscnt = 0;
1943 default_query.ednsflags = 0;
1944 default_query.ecs_addr = NULL;
1945 default_query.timeout = 0;
1946 default_query.udptimeout = 0;
1947 default_query.udpretries = 3;
1948 ISC_LINK_INIT(&default_query, link);
1949 }
1950
1951 if (is_batchfile) {
1952 /* Processing '-f batchfile'. */
1953 query = clone_default_query();
1954 global = false;
1955 } else {
1956 query = &default_query;
1957 }
1958
1959 rc = argc;
1960 rv = argv;
1961 for (rc--, rv++; rc > 0; rc--, rv++) {
1962 if (strncmp(rv[0], "%", 1) == 0) {
1963 break;
1964 }
1965 if (rv[0][0] == '@') {
1966 if (server != NULL) {
1967 fatal("server already set to @%s", server);
1968 }
1969 server = &rv[0][1];
1970 } else if (rv[0][0] == '+') {
1971 plus_option(&rv[0][1], query, global);
1972 } else if (rv[0][0] == '-') {
1973 bool setname = false;
1974
1975 if (rc <= 1) {
1976 if (dash_option(&rv[0][1], NULL, query, global,
1977 &setname)) {
1978 rc--;
1979 rv++;
1980 }
1981 } else {
1982 if (dash_option(&rv[0][1], rv[1], query, global,
1983 &setname)) {
1984 rc--;
1985 rv++;
1986 }
1987 }
1988 if (setname) {
1989 if (query == &default_query) {
1990 query = clone_default_query();
1991 }
1992 ISC_LIST_APPEND(queries, query, link);
1993
1994 default_query.textname[0] = 0;
1995 query = clone_default_query();
1996 global = false;
1997 }
1998 } else {
1999 /*
2000 * Anything which isn't an option
2001 */
2002 if (query == &default_query) {
2003 query = clone_default_query();
2004 }
2005 strlcpy(query->textname, rv[0],
2006 sizeof(query->textname));
2007 ISC_LIST_APPEND(queries, query, link);
2008
2009 query = clone_default_query();
2010 global = false;
2011 /* XXX Error message */
2012 }
2013 }
2014
2015 /*
2016 * If we have a batchfile, read the query list from it.
2017 */
2018 if ((batchname != NULL) && !is_batchfile) {
2019 if (strcmp(batchname, "-") == 0) {
2020 batchfp = stdin;
2021 } else {
2022 batchfp = fopen(batchname, "r");
2023 }
2024 if (batchfp == NULL) {
2025 perror(batchname);
2026 fatal("couldn't open batch file '%s'", batchname);
2027 }
2028 while (fgets(batchline, sizeof(batchline), batchfp) != 0) {
2029 if (batchline[0] == '\r' || batchline[0] == '\n' ||
2030 batchline[0] == '#' || batchline[0] == ';')
2031 {
2032 continue;
2033 }
2034 for (bargc = 1, bargv[bargc] = strtok_r(
2035 batchline, " \t\r\n", &last);
2036 (bargc < 14) && bargv[bargc]; bargc++,
2037 bargv[bargc] = strtok_r(NULL, " \t\r\n", &last))
2038 {
2039 /* empty body */
2040 }
2041
2042 bargv[0] = argv[0];
2043 parse_args(true, bargc, (char **)bargv);
2044 }
2045 if (batchfp != stdin) {
2046 fclose(batchfp);
2047 }
2048 }
2049 if (query != &default_query) {
2050 if (query->ecs_addr != NULL) {
2051 isc_mem_free(mctx, query->ecs_addr);
2052 }
2053 isc_mem_free(mctx, query);
2054 }
2055 }
2056
2057 /*% Main processing routine for mdig */
2058 int
main(int argc,char * argv[])2059 main(int argc, char *argv[]) {
2060 struct query *query = NULL;
2061 isc_result_t result;
2062 isc_sockaddr_t bind_any;
2063 isc_log_t *lctx = NULL;
2064 isc_logconfig_t *lcfg = NULL;
2065 isc_nm_t *netmgr = NULL;
2066 isc_taskmgr_t *taskmgr = NULL;
2067 isc_task_t *task = NULL;
2068 dns_dispatchmgr_t *dispatchmgr = NULL;
2069 dns_dispatch_t *dispatchvx = NULL;
2070 dns_view_t *view = NULL;
2071 unsigned int i;
2072 int ns;
2073
2074 RUNCHECK(isc_app_start());
2075
2076 if (isc_net_probeipv4() == ISC_R_SUCCESS) {
2077 have_ipv4 = true;
2078 }
2079 if (isc_net_probeipv6() == ISC_R_SUCCESS) {
2080 have_ipv6 = true;
2081 }
2082 if (!have_ipv4 && !have_ipv6) {
2083 fatal("could not find either IPv4 or IPv6");
2084 }
2085
2086 preparse_args(argc, argv);
2087
2088 isc_mem_create(&mctx);
2089 isc_log_create(mctx, &lctx, &lcfg);
2090
2091 RUNCHECK(dst_lib_init(mctx, NULL));
2092 isc_nonce_buf(cookie_secret, sizeof(cookie_secret));
2093
2094 ISC_LIST_INIT(queries);
2095 parse_args(false, argc, argv);
2096 if (server == NULL) {
2097 fatal("a server '@xxx' is required");
2098 }
2099
2100 ns = 0;
2101 result = bind9_getaddresses(server, port, &dstaddr, 1, &ns);
2102 if (result != ISC_R_SUCCESS) {
2103 fatal("couldn't get address for '%s': %s", server,
2104 isc_result_totext(result));
2105 }
2106
2107 if (isc_sockaddr_pf(&dstaddr) == PF_INET && have_ipv6) {
2108 isc_net_disableipv6();
2109 have_ipv6 = false;
2110 } else if (isc_sockaddr_pf(&dstaddr) == PF_INET6 && have_ipv4) {
2111 isc_net_disableipv4();
2112 have_ipv4 = false;
2113 }
2114 if (have_ipv4 && have_ipv6) {
2115 fatal("can't choose between IPv4 and IPv6");
2116 }
2117
2118 isc_managers_create(mctx, 1, 0, 0, &netmgr, &taskmgr, NULL, NULL);
2119 RUNCHECK(isc_task_create(taskmgr, 0, &task));
2120 RUNCHECK(dns_dispatchmgr_create(mctx, netmgr, &dispatchmgr));
2121
2122 if (have_ipv4) {
2123 isc_sockaddr_any(&bind_any);
2124 } else {
2125 isc_sockaddr_any6(&bind_any);
2126 }
2127 RUNCHECK(dns_dispatch_createudp(
2128 dispatchmgr, have_src ? &srcaddr : &bind_any, &dispatchvx));
2129
2130 RUNCHECK(dns_requestmgr_create(
2131 mctx, taskmgr, dispatchmgr, have_ipv4 ? dispatchvx : NULL,
2132 have_ipv6 ? dispatchvx : NULL, &requestmgr));
2133
2134 RUNCHECK(dns_view_create(mctx, 0, "_test", &view));
2135
2136 query = ISC_LIST_HEAD(queries);
2137 RUNCHECK(isc_app_onrun(mctx, task, sendqueries, query));
2138
2139 /*
2140 * Stall to the start of a new second.
2141 */
2142 if (burst) {
2143 isc_time_t start, now;
2144 RUNCHECK(isc_time_now(&start));
2145 /*
2146 * Sleep to 1ms of the end of the second then run a busy loop
2147 * until the second changes.
2148 */
2149 do {
2150 RUNCHECK(isc_time_now(&now));
2151 if (isc_time_seconds(&start) == isc_time_seconds(&now))
2152 {
2153 int us = US_PER_SEC -
2154 (isc_time_nanoseconds(&now) /
2155 NS_PER_US);
2156 if (us > US_PER_MS) {
2157 usleep(us - US_PER_MS);
2158 }
2159 } else {
2160 break;
2161 }
2162 } while (1);
2163 }
2164
2165 (void)isc_app_run();
2166
2167 dns_view_detach(&view);
2168
2169 dns_requestmgr_shutdown(requestmgr);
2170 dns_requestmgr_detach(&requestmgr);
2171
2172 dns_dispatch_detach(&dispatchvx);
2173 dns_dispatchmgr_detach(&dispatchmgr);
2174
2175 isc_task_shutdown(task);
2176 isc_task_detach(&task);
2177
2178 isc_managers_destroy(&netmgr, &taskmgr, NULL, NULL);
2179
2180 dst_lib_destroy();
2181
2182 isc_log_destroy(&lctx);
2183
2184 query = ISC_LIST_HEAD(queries);
2185 while (query != NULL) {
2186 struct query *next = ISC_LIST_NEXT(query, link);
2187
2188 if (query->ednsopts != NULL) {
2189 for (i = 0; i < EDNSOPTS; i++) {
2190 if (query->ednsopts[i].value != NULL) {
2191 isc_mem_free(mctx,
2192 query->ednsopts[i].value);
2193 }
2194 }
2195 isc_mem_free(mctx, query->ednsopts);
2196 }
2197 if (query->ecs_addr != NULL) {
2198 isc_mem_free(mctx, query->ecs_addr);
2199 query->ecs_addr = NULL;
2200 }
2201 isc_mem_free(mctx, query);
2202 query = next;
2203 }
2204
2205 if (default_query.ecs_addr != NULL) {
2206 isc_mem_free(mctx, default_query.ecs_addr);
2207 }
2208
2209 isc_mem_destroy(&mctx);
2210
2211 isc_app_finish();
2212
2213 return (0);
2214 }
2215