summaryrefslogtreecommitdiffstats
path: root/src/test/writer_write.c
blob: c0038959036c27bab6296de5ec03988e5928348b (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
85
86
87
88
89
90
91
92
#include <dnswire/writer.h>

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

#include "create_dnstap.c"

int main(int argc, const char* argv[])
{
    if (argc < 2) {
        return 1;
    }

    FILE* fp = fopen(argv[1], "w");
    if (!fp) {
        return 1;
    }

    struct dnswire_writer writer;
    if (dnswire_writer_init(&writer) != dnswire_ok) {
        return 1;
    }

    struct dnstap d = DNSTAP_INITIALIZER;
    create_dnstap(&d, "writer_write-1");

    dnswire_writer_set_dnstap(writer, &d);

    while (1) {
        enum dnswire_result res = dnswire_writer_fwrite(&writer, fp);
        switch (res) {
        case dnswire_ok:
            break;

        case dnswire_again:
        case dnswire_need_more:
            continue;

        default:
            fprintf(stderr, "dnswire_writer_fwrite() error\n");
            return 1;
        }
        break;
    }

    create_dnstap(&d, "writer_write-2");

    while (1) {
        enum dnswire_result res = dnswire_writer_fwrite(&writer, fp);
        switch (res) {
        case dnswire_ok:
            break;

        case dnswire_again:
        case dnswire_need_more:
            continue;

        default:
            fprintf(stderr, "dnswire_writer_fwrite() error\n");
            return 1;
        }
        break;
    }

    if (dnswire_writer_stop(&writer) != dnswire_ok) {
        fprintf(stderr, "dnswire_writer_stop() failed\n");
        return 1;
    }

    while (1) {
        enum dnswire_result res = dnswire_writer_fwrite(&writer, fp);
        switch (res) {
        case dnswire_ok:
        case dnswire_endofdata:
            break;

        case dnswire_again:
        case dnswire_need_more:
            continue;

        default:
            fprintf(stderr, "dnswire_reader_add() error\n");
            return 1;
        }
        break;
    }

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