Skip to content

Commit

Permalink
Implement outputting final failures for NDJSON (as suggested by @info…
Browse files Browse the repository at this point in the history
  • Loading branch information
blechschmidt committed Mar 22, 2020
1 parent 5b66b51 commit cf60a8e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
28 changes: 27 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <net/if.h>
#endif

static char json_buffer[5 * 0xFFFF];

void print_help()
{
fprintf(stderr, ""
Expand Down Expand Up @@ -914,6 +916,16 @@ bool is_unacceptable(dns_pkt_t *packet)
return context.cmd_args.retry_codes[packet->head.header.rcode];
}

void write_exhausted_tries(lookup_t *lookup, char *status)
{
if(context.cmd_args.output == OUTPUT_NDJSON && context.format.write_exhausted_tries) {
json_escape(json_buffer, sizeof(json_buffer), lookup->key->name.name, lookup->key->name.length);
fprintf(context.outfile,
"{\"name\":\"%s\",\"type\":\"%s\",\"class\":\"%s\",\"error\":\"%s\"}\n", json_buffer,
dns_record_type2str(lookup->key->type), "IN", status);
}
}

void lookup_done(lookup_t *lookup)
{
context.stats.finished++;
Expand Down Expand Up @@ -963,6 +975,7 @@ void ring_timeout(void *param)
lookup_t *lookup = param;
if(!retry(lookup))
{
write_exhausted_tries(lookup, "TIMEOUT");
lookup_done(lookup);
}
}
Expand All @@ -973,7 +986,6 @@ void do_read(uint8_t *offset, size_t len, struct sockaddr_storage *recvaddr)
static uint8_t *parse_offset;
static lookup_t *lookup;
static resolver_t* resolver;
static char json_buffer[5 * 0xFFFF];

context.stats.current_rate++;
context.stats.numreplies++;
Expand Down Expand Up @@ -1019,6 +1031,7 @@ void do_read(uint8_t *offset, size_t len, struct sockaddr_storage *recvaddr)
// We may have tried to many times already.
if(!retry(lookup))
{
write_exhausted_tries(lookup, "MAXRETRIES");
// If this is the case, we will not try again.
lookup_done(lookup);
}
Expand Down Expand Up @@ -1983,6 +1996,19 @@ int parse_cmd(int argc, char **argv)

case 'J':
context.cmd_args.output = OUTPUT_NDJSON;

for(char *output_option = argv[i] + 1; *output_option != 0; output_option++)
{
switch(*output_option)
{
case 'e':
context.format.write_exhausted_tries = true;
break;
default:
log_msg("Unrecognized output option: %c\n", *output_option);
clean_exit(EXIT_FAILURE);
}
}
break;

case 'S':
Expand Down
1 change: 1 addition & 0 deletions massdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ typedef struct
bool include_meta;
bool indent_sections;
bool print_question;
bool write_exhausted_tries;
} format;

struct cmd_args
Expand Down

0 comments on commit cf60a8e

Please sign in to comment.