summaryrefslogtreecommitdiffstats
path: root/examples/writer.c
blob: d9088b843d36d4757aa489076577cf13534af79f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <dnswire/writer.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "create_dnstap.c"

int main(int argc, const char* argv[])
{
    if (argc < 2) {
        fprintf(stderr, "usage: writer <file>\n");
        return 1;
    }

    /*
     * We start by opening the output file for writing.
     */

    FILE* fp = fopen(argv[1], "w");
    if (!fp) {
        fprintf(stderr, "Unable to open %s: %s\n", argv[1], strerror(errno));
        return 1;
    }

    /*
     * We first initialize the writer and check that it can allocate the
     * buffers it needs.
     */

    struct dnswire_writer writer;

    if (dnswire_writer_init(&writer) != dnswire_ok) {
        fprintf(stderr, "Unable to initialize dnswire writer\n");
        return 1;
    }

    /*
     * Now we create a DNSTAP message.
     */

    struct dnstap d = create_dnstap("writer");

    /*
     * We set the DNSTAP message the writer should write.
     */

    dnswire_writer_set_dnstap(writer, &d);

    /*
     * We now loop and wait for the DNSTAP message to be written.
     */

    int done = 0;

    while (!done) {
        switch (dnswire_writer_fwrite(&writer, fp)) {
        case dnswire_ok:
            /*
             * The DNSTAP message was written successfully, we can now set
             * a new DNSTAP message for the writer or stop the stream.
             *
             * This stops the stream, loop again until it's stopped.
             */
            dnswire_writer_stop(&writer);
            break;
        case dnswire_again:
            break;
        case dnswire_endofdata:
            /*
             * The stream is stopped, we're done!
             */
            done = 1;
            break;
        default:
            fprintf(stderr, "dnswire_writer_fwrite() error\n");
            done = 1;
        }
    }

    dnswire_writer_destroy(writer);
    fclose(fp);
    return 0;
}