1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 
10 #include "private.h"
11 
12 #include <wdns.h>
13 
14 static int
process_data(const uint8_t * data,size_t len)15 process_data(const uint8_t *data, size_t len)
16 {
17 	wdns_message_t m;
18 	wdns_res res;
19 
20 	res = wdns_parse_message(&m, data, len);
21 	if (res == wdns_res_success) {
22 		wdns_print_message(stdout, &m);
23 		wdns_clear_message(&m);
24 		putchar('\n');
25 		return EXIT_SUCCESS;
26 	} else {
27 		return EXIT_FAILURE;
28 	}
29 }
30 
31 int
main(int argc,char ** argv)32 main(int argc, char **argv) {
33 	FILE *fp;
34 
35 	uint8_t data[4096] = {0};
36 	size_t len;
37 
38 	if (argc != 2) {
39 		fprintf(stderr, "Usage: %s <INFILE>\n", argv[0]);
40 		return EXIT_FAILURE;
41 	}
42 
43 	fp = fopen(argv[1], "r");
44 	if (fp == NULL) {
45 		fprintf(stderr, "Error: unable to open %s: %s\n", argv[1], strerror(errno));
46 		return EXIT_FAILURE;
47 	}
48 
49 	len = fread(data, 1, sizeof(data), fp);
50 	if (ferror(fp)) {
51 		fprintf(stderr, "Error: fread() returned an error on %s\n", argv[1]);
52 		return EXIT_FAILURE;
53 	}
54 
55 	if (!feof(fp)) {
56 		fprintf(stderr, "Error: did not make a complete read of %s\n", argv[1]);
57 		return EXIT_FAILURE;
58 	}
59 
60 	return process_data(data, len);
61 }
62