Debian Codegraph @

examples/reader.c

raw
1 #include <dnswire/reader.h>
2
3 #include <stdio.h>
4 #include <errno.h>
5 #include <string.h>
6
7 #include "print_dnstap.c"
8
9 int main(int argc, const char* argv[])
10 {
11 if (argc < 2) {
12 fprintf(stderr, "usage: reader <file>\n");
13 return 1;
14 }
15
16 /*
17 * First we open the given file and read all of the content.
18 */
19
20 FILE* fp = fopen(argv[1], "r");
21 if (!fp) {
22 fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
23 return 1;
24 }
25
26 /*
27 * We now initialize the reader and check that it can allocate the
28 * buffers it needs.
29 */
30
31 struct dnswire_reader reader;
32 int done = 0;
33
34 if (dnswire_reader_init(&reader) != dnswire_ok) {
35 fprintf(stderr, "Unable to initialize dnswire reader\n");
36 return 1;
37 }
38
39 /*
40 * We now loop until we have a DNSTAP message, the stream was stopped
41 * or we got an error.
42 */
43
44 while (!done) {
45 switch (dnswire_reader_fread(&reader, fp)) {
46 case dnswire_have_dnstap:
47 print_dnstap(dnswire_reader_dnstap(reader));
48 break;
49 case dnswire_again:
50 case dnswire_need_more:
51 break;
52 case dnswire_endofdata:
53 done = 1;
54 break;
55 default:
56 fprintf(stderr, "dnswire_reader_fread() error\n");
57 done = 1;
58 }
59 }
60
61 dnswire_reader_destroy(reader);
62 fclose(fp);
63
64 return 0;
65 }