diff --git a/bin/dig/dig.c b/bin/dig/dig.c index d4dca378e6..41a25bc343 100644 --- a/bin/dig/dig.c +++ b/bin/dig/dig.c @@ -64,8 +64,7 @@ static char hexcookie[81]; static bool short_form = false, printcmd = true, plusquest = false, pluscomm = false, - ipv4only = false, ipv6only = false, digrc = true, - yaml = false; + ipv4only = false, ipv6only = false, digrc = true; static uint32_t splitwidth = 0xffffffff; /*% opcode text */ @@ -2446,6 +2445,36 @@ dig_error(const char *format, ...) { } } +static void +dig_warning(const char *format, ...) { + va_list args; + + if (!yaml) { + printf(";; "); + + va_start(args, format); + vprintf(format, args); + va_end(args); + + printf("\n"); + } +} + +static void +dig_comments(dig_lookup_t *lookup, const char *format, ...) { + va_list args; + + if (lookup->comments && !yaml) { + printf(";; "); + + va_start(args, format); + vprintf(format, args); + va_end(args); + + printf("\n"); + } +} + void dig_setup(int argc, char **argv) { isc_result_t result; @@ -2462,6 +2491,8 @@ dig_setup(int argc, char **argv) { dighost_trying = trying; dighost_shutdown = query_finished; dighost_error = dig_error; + dighost_warning = dig_warning; + dighost_comments = dig_comments; progname = argv[0]; preparse_args(argc, argv); diff --git a/bin/dig/dighost.c b/bin/dig/dighost.c index 06169db44d..3a21d6516f 100644 --- a/bin/dig/dighost.c +++ b/bin/dig/dighost.c @@ -112,7 +112,8 @@ bool showsearch = false, is_dst_up = false, keep_open = false, - verbose = false; + verbose = false, + yaml = false; in_port_t port = 53; unsigned int timeout = 0; unsigned int extrabytes; @@ -190,6 +191,30 @@ dig_lookup_t *current_lookup = NULL; "isc_mutex_unlock");\ } +static void +default_warnerr(const char *format, ...) { + va_list args; + + printf(";; "); + va_start(args, format); + vprintf(format, args); + va_end(args); + printf("\n"); +}; + +static void +default_comments(dig_lookup_t *lookup, const char *format, ...) { + va_list args; + + if (lookup->comments) { + printf(";; "); + va_start(args, format); + vprintf(format, args); + va_end(args); + printf("\n"); + } +}; + /* dynamic callbacks */ isc_result_t @@ -197,7 +222,13 @@ isc_result_t dns_message_t *msg, bool headers); void -(*dighost_error)(const char *format, ...); +(*dighost_error)(const char *format, ...) = default_warnerr; + +void +(*dighost_warning)(const char *format, ...) = default_warnerr; + +void +(*dighost_comments)(dig_lookup_t *lookup, const char *format, ...) = default_comments; void (*dighost_received)(unsigned int bytes, isc_sockaddr_t *from, @@ -1760,12 +1791,15 @@ followup_lookup(dns_message_t *msg, dig_query_t *query, dns_section_t section) namereln = dns_name_fullcompare(name, domain, &order, &nlabels); if (namereln == dns_namereln_equal) { - if (!horizontal) - printf(";; BAD (HORIZONTAL) REFERRAL\n"); + if (!horizontal) { + dighost_warning("BAD (HORIZONTAL) " + "REFERRAL"); + } horizontal = true; } else if (namereln != dns_namereln_subdomain) { - if (!bad) - printf(";; BAD REFERRAL\n"); + if (!bad) { + dighost_warning( "BAD REFERRAL"); + } bad = true; continue; } @@ -2687,7 +2721,7 @@ send_tcp_connect(dig_query_t *query) { isc_netaddr_fromsockaddr(&netaddr, &query->sockaddr); isc_netaddr_format(&netaddr, buf, sizeof(buf)); - printf(";; Skipping mapped address '%s'\n", buf); + dighost_warning("Skipping mapped address '%s'", buf); query->waiting_connect = false; if (ISC_LINK_LINKED(query, link)) @@ -2697,7 +2731,7 @@ send_tcp_connect(dig_query_t *query) { l = query->lookup; clear_query(query); if (next == NULL) { - printf(";; No acceptable nameservers\n"); + dighost_warning("No acceptable nameservers"); check_next_lookup(l); return; } @@ -2758,6 +2792,14 @@ send_tcp_connect(dig_query_t *query) { } } +static void +print_query_size(dig_query_t *query) { + if (!yaml) { + printf(";; QUERY SIZE: %u\n\n", + isc_buffer_usedlength(&query->lookup->renderbuf)); + } +} + /*% * Send a UDP packet to the remote nameserver, possible starting the * recv action as well. Also make sure that the timer is running and @@ -2796,13 +2838,12 @@ send_udp(dig_query_t *query) { isc_netaddr_fromsockaddr(&netaddr, &query->sockaddr); isc_netaddr_format(&netaddr, buf, sizeof(buf)); - printf(";; Skipping mapped address '%s'\n", buf); - + dighost_warning("Skipping mapped address '%s'", buf); next = ISC_LIST_NEXT(query, link); l = query->lookup; clear_query(query); if (next == NULL) { - printf(";; No acceptable nameservers\n"); + dighost_warning("No acceptable nameservers"); check_next_lookup(l); } else { send_udp(next); @@ -2861,8 +2902,7 @@ send_udp(dig_query_t *query) { &query->lookup->renderbuf, query->lookup->sendmsg, true); if (query->lookup->stats) { - printf(";; QUERY SIZE: %u\n\n", - isc_buffer_usedlength(&query->lookup->renderbuf)); + print_query_size(query); } } } @@ -3148,9 +3188,7 @@ launch_next_query(dig_query_t *query, bool include_question) { &query->lookup->renderbuf, query->lookup->sendmsg, true); if (query->lookup->stats) { - printf(";; QUERY SIZE: %u\n\n", - isc_buffer_usedlength( - &query->lookup->renderbuf)); + print_query_size(query); } } } @@ -3192,11 +3230,12 @@ connect_done(isc_task_t *task, isc_event_t *event) { if (sevent->result == ISC_R_CANCELED) { debug("in cancel handler"); isc_sockaddr_format(&query->sockaddr, sockstr, sizeof(sockstr)); - if (query->timedout) - printf(";; Connection to %s(%s) for %s failed: %s.\n", - sockstr, query->servname, - query->lookup->textname, - isc_result_totext(ISC_R_TIMEDOUT)); + if (query->timedout) { + dighost_warning("Connection to %s(%s) for %s failed: " + "%s.", sockstr, query->servname, + query->lookup->textname, + isc_result_totext(ISC_R_TIMEDOUT)); + } isc_socket_detach(&query->sock); INSIST(sockcount > 0); sockcount--; @@ -3214,11 +3253,12 @@ connect_done(isc_task_t *task, isc_event_t *event) { debug("unsuccessful connection: %s", isc_result_totext(sevent->result)); isc_sockaddr_format(&query->sockaddr, sockstr, sizeof(sockstr)); - if (sevent->result != ISC_R_CANCELED) - printf(";; Connection to %s(%s) for %s failed: " - "%s.\n", sockstr, - query->servname, query->lookup->textname, - isc_result_totext(sevent->result)); + if (sevent->result != ISC_R_CANCELED) { + dighost_warning("Connection to %s(%s) for %s failed: " + "%s.", sockstr, query->servname, + query->lookup->textname, + isc_result_totext(sevent->result)); + } isc_socket_detach(&query->sock); INSIST(sockcount > 0); sockcount--; @@ -3426,12 +3466,12 @@ process_cookie(dig_lookup_t *l, dns_message_t *msg, if (isc_safe_memequal(isc_buffer_current(optbuf), sent, 8)) { msg->cc_ok = 1; } else { - printf(";; Warning: Client COOKIE mismatch\n"); + dighost_warning("Warning: Client COOKIE mismatch"); msg->cc_bad = 1; copy = false; } } else { - printf(";; Warning: COOKIE bad token (too short)\n"); + dighost_warning("Warning: COOKIE bad token (too short)"); msg->cc_bad = 1; copy = false; } @@ -3603,8 +3643,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { sizeof(buf1)); isc_sockaddr_format(&query->sockaddr, buf2, sizeof(buf2)); - printf(";; reply from unexpected source: %s," - " expected %s\n", buf1, buf2); + dighost_warning("reply from unexpected source: %s," + " expected %s\n", buf1, buf2); if (!l->accept_reply_unexpected_src) { match = false; } @@ -3617,19 +3657,22 @@ recv_done(isc_task_t *task, isc_event_t *event) { if (l->tcp_mode) { bool fail = true; if (result == ISC_R_SUCCESS) { - if (!query->first_soa_rcvd || - query->warn_id) - printf(";; %s: ID mismatch: " - "expected ID %u, got %u\n", - query->first_soa_rcvd ? - "WARNING" : "ERROR", - l->sendmsg->id, id); + if ((!query->first_soa_rcvd || query->warn_id)) + { + dighost_warning("%s: ID mismatch: " + "expected ID %u, got " + "%u", + query->first_soa_rcvd ? + "WARNING" : "ERROR", + l->sendmsg->id, id); + } if (query->first_soa_rcvd) fail = false; query->warn_id = false; - } else - printf(";; ERROR: short " - "(< header size) message\n"); + } else { + dighost_warning("ERROR: short (< header size) " + "message"); + } if (fail) { isc_event_free(&event); clear_query(query); @@ -3639,16 +3682,18 @@ recv_done(isc_task_t *task, isc_event_t *event) { return; } match = true; - } else if (result == ISC_R_SUCCESS) - printf(";; Warning: ID mismatch: " - "expected ID %u, got %u\n", l->sendmsg->id, id); - else - printf(";; Warning: short " - "(< header size) message received\n"); + } else if (result == ISC_R_SUCCESS) { + dighost_warning("Warning: ID mismatch: expected ID %u," + " got %u", l->sendmsg->id, id); + } else { + dighost_warning("Warning: short (< header size) " + "message received"); + } } - if (result == ISC_R_SUCCESS && (msgflags & DNS_MESSAGEFLAG_QR) == 0) - printf(";; Warning: query response not set\n"); + if (result == ISC_R_SUCCESS && (msgflags & DNS_MESSAGEFLAG_QR) == 0) { + dighost_warning("Warning: query response not set"); + } if (!match) goto udp_mismatch; @@ -3682,13 +3727,16 @@ recv_done(isc_task_t *task, isc_event_t *event) { } result = dns_message_parse(msg, &b, parseflags); if (result == DNS_R_RECOVERABLE) { - printf(";; Warning: Message parser reports malformed " - "message packet.\n"); + dighost_warning("Warning: Message parser reports malformed " + "message packet."); result = ISC_R_SUCCESS; } if (result != ISC_R_SUCCESS) { - printf(";; Got bad packet: %s\n", isc_result_totext(result)); - hex_dump(&b); + if (!yaml) { + printf(";; Got bad packet: %s\n", + isc_result_totext(result)); + hex_dump(&b); + } query->waiting_connect = false; dns_message_destroy(&msg); isc_event_free(&event); @@ -3725,9 +3773,10 @@ recv_done(isc_task_t *task, isc_event_t *event) { dns_rdataclass_format(rdataset->rdclass, classbuf, sizeof(classbuf)); - printf(";; Question section mismatch: " - "got %s/%s/%s\n", - namestr, typebuf, classbuf); + dighost_warning(";; Question section " + "mismatch: got " + "%s/%s/%s", namestr, + typebuf, classbuf); match = false; } } @@ -3750,9 +3799,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { /* * Add minimum EDNS version required checks here if needed. */ - if (l->comments) - printf(";; BADVERS, retrying with EDNS version %u.\n", - (unsigned int)newedns); + dighost_comments(l, "BADVERS, retrying with EDNS version %u.", + (unsigned int)newedns); l->edns = newedns; n = requeue_lookup(l, true); if (l->trace && l->trace_root) @@ -3769,8 +3817,7 @@ recv_done(isc_task_t *task, isc_event_t *event) { !l->ignore && !l->tcp_mode) { if (l->cookie == NULL && l->sendcookie && msg->opt != NULL) process_opt(l, msg); - if (l->comments) - printf(";; Truncated, retrying in TCP mode.\n"); + dighost_comments(l, "Truncated, retrying in TCP mode."); n = requeue_lookup(l, true); n->tcp_mode = true; if (l->trace && l->trace_root) @@ -3787,9 +3834,9 @@ recv_done(isc_task_t *task, isc_event_t *event) { l->sendcookie && l->badcookie) { process_opt(l, msg); if (msg->cc_ok) { - if (l->comments) - printf(";; BADCOOKIE, retrying%s.\n", - l->seenbadcookie ? " in TCP mode" : ""); + dighost_comments(l, "BADCOOKIE, retrying%s.", + l->seenbadcookie ? + " in TCP mode" : ""); n = requeue_lookup(l, true); if (l->seenbadcookie) n->tcp_mode = true; @@ -3826,13 +3873,12 @@ recv_done(isc_task_t *task, isc_event_t *event) { */ if ((ISC_LIST_HEAD(l->q) != query) || (ISC_LIST_NEXT(query, link) != NULL)) { - if (l->comments) - printf(";; Got %s from %s, " - "trying next server\n", - msg->rcode == dns_rcode_servfail ? - "SERVFAIL reply" : - "recursion not available", - query->servname); + dighost_comments(l, "Got %s from %s, trying next " + "server", + msg->rcode == dns_rcode_servfail ? + "SERVFAIL reply" : + "recursion not available", + query->servname); clear_query(query); check_next_lookup(l); dns_message_destroy(&msg); @@ -3845,8 +3891,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { if (tsigkey != NULL) { result = dns_tsig_verify(&b, msg, NULL, NULL); if (result != ISC_R_SUCCESS) { - printf(";; Couldn't verify signature: %s\n", - isc_result_totext(result)); + dighost_warning("Couldn't verify signature: %s", + isc_result_totext(result)); validated = false; } l->tsigctx = msg->tsigctx; @@ -3897,7 +3943,8 @@ recv_done(isc_task_t *task, isc_event_t *event) { if (!done_process_opt) { if (l->cookie != NULL) { if (msg->opt == NULL) { - printf(";; expected opt record in response\n"); + dighost_warning("expected opt record in " + "response"); } else { process_opt(l, msg); } diff --git a/bin/dig/host.c b/bin/dig/host.c index aa7cb8473e..5942fd159b 100644 --- a/bin/dig/host.c +++ b/bin/dig/host.c @@ -857,17 +857,6 @@ parse_args(bool is_batchfile, int argc, char **argv) { ISC_LIST_APPEND(lookup_list, lookup, link); } -static void -host_error(const char *format, ...) { - va_list args; - - printf(";; "); - va_start(args, format); - vfprintf(stdout, format, args); - va_end(args); - printf("\n"); -} - int main(int argc, char **argv) { isc_result_t result; @@ -885,7 +874,6 @@ main(int argc, char **argv) { dighost_received = received; dighost_trying = trying; dighost_shutdown = host_shutdown; - dighost_error = host_error; debug("main()"); progname = argv[0]; diff --git a/bin/dig/include/dig/dig.h b/bin/dig/include/dig/dig.h index 563cd9d4ed..a68bde01da 100644 --- a/bin/dig/include/dig/dig.h +++ b/bin/dig/include/dig/dig.h @@ -252,7 +252,7 @@ extern dig_searchlistlist_t search_list; extern unsigned int extrabytes; extern bool check_ra, have_ipv4, have_ipv6, specified_source, - usesearch, showsearch; + usesearch, showsearch, yaml; extern in_port_t port; extern unsigned int timeout; extern isc_mem_t *mctx; @@ -391,6 +391,18 @@ extern isc_result_t extern void (*dighost_error)(const char *format, ...); +/* + * Print a warning message in the appropriate format. + */ +extern void +(*dighost_warning)(const char *format, ...); + +/* + * Print a comment in the appropriate format. + */ +extern void +(*dighost_comments)(dig_lookup_t *lookup, const char *format, ...); + /*%< * Print the final result of the lookup. */ diff --git a/bin/dig/nslookup.c b/bin/dig/nslookup.c index f343fec7bc..6d2d073864 100644 --- a/bin/dig/nslookup.c +++ b/bin/dig/nslookup.c @@ -983,17 +983,6 @@ getinput(isc_task_t *task, isc_event_t *event) { isc_app_shutdown(); } -static void -nsl_error(const char *format, ...) { - va_list args; - - printf(";; "); - va_start(args, format); - vfprintf(stdout, format, args); - va_end(args); - printf("\n"); -} - int main(int argc, char **argv) { isc_result_t result; @@ -1011,7 +1000,6 @@ main(int argc, char **argv) { dighost_received = received; dighost_trying = trying; dighost_shutdown = query_finished; - dighost_error = nsl_error; result = isc_app_start(); check_result(result, "isc_app_start");