| 1 |
#include <dnswire/writer.h> |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "create_dnstap.c" |
| 8 |
|
| 9 |
int main(int argc, const char* argv[]) |
| 10 |
{ |
| 11 |
if (argc < 2) { |
| 12 |
return 1; |
| 13 |
} |
| 14 |
|
| 15 |
FILE* fp = fopen(argv[1], "w"); |
| 16 |
if (!fp) { |
| 17 |
return 1; |
| 18 |
} |
| 19 |
|
| 20 |
struct dnswire_writer writer; |
| 21 |
if (dnswire_writer_init(&writer) != dnswire_ok) { |
| 22 |
return 1; |
| 23 |
} |
| 24 |
|
| 25 |
uint8_t buf[4096]; |
| 26 |
|
| 27 |
struct dnstap d = DNSTAP_INITIALIZER; |
| 28 |
create_dnstap(&d, "writer_pop-1"); |
| 29 |
|
| 30 |
dnswire_writer_set_dnstap(writer, &d); |
| 31 |
|
| 32 |
while (1) { |
| 33 |
if (dnswire_writer_popped(writer)) { |
| 34 |
if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { |
| 35 |
fprintf(stderr, "fwrite() failed\n"); |
| 36 |
return 1; |
| 37 |
} |
| 38 |
} |
| 39 |
enum dnswire_result res = dnswire_writer_pop(&writer, buf, sizeof(buf), 0, 0); |
| 40 |
switch (res) { |
| 41 |
case dnswire_ok: |
| 42 |
break; |
| 43 |
|
| 44 |
case dnswire_again: |
| 45 |
case dnswire_need_more: |
| 46 |
continue; |
| 47 |
|
| 48 |
default: |
| 49 |
fprintf(stderr, "dnswire_writer_pop() error\n"); |
| 50 |
return 1; |
| 51 |
} |
| 52 |
break; |
| 53 |
} |
| 54 |
|
| 55 |
create_dnstap(&d, "writer_pop-2"); |
| 56 |
|
| 57 |
while (1) { |
| 58 |
if (dnswire_writer_popped(writer)) { |
| 59 |
if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { |
| 60 |
fprintf(stderr, "fwrite() failed\n"); |
| 61 |
return 1; |
| 62 |
} |
| 63 |
} |
| 64 |
enum dnswire_result res = dnswire_writer_pop(&writer, buf, sizeof(buf), 0, 0); |
| 65 |
switch (res) { |
| 66 |
case dnswire_ok: |
| 67 |
break; |
| 68 |
|
| 69 |
case dnswire_again: |
| 70 |
case dnswire_need_more: |
| 71 |
continue; |
| 72 |
|
| 73 |
default: |
| 74 |
fprintf(stderr, "dnswire_writer_pop() error\n"); |
| 75 |
return 1; |
| 76 |
} |
| 77 |
break; |
| 78 |
} |
| 79 |
|
| 80 |
if (dnswire_writer_stop(&writer) != dnswire_ok) { |
| 81 |
fprintf(stderr, "dnswire_writer_stop() failed\n"); |
| 82 |
return 1; |
| 83 |
} |
| 84 |
|
| 85 |
while (1) { |
| 86 |
if (dnswire_writer_popped(writer)) { |
| 87 |
if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { |
| 88 |
fprintf(stderr, "fwrite() failed\n"); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
} |
| 92 |
enum dnswire_result res = dnswire_writer_pop(&writer, buf, sizeof(buf), 0, 0); |
| 93 |
switch (res) { |
| 94 |
case dnswire_ok: |
| 95 |
case dnswire_endofdata: |
| 96 |
break; |
| 97 |
|
| 98 |
case dnswire_again: |
| 99 |
case dnswire_need_more: |
| 100 |
continue; |
| 101 |
|
| 102 |
default: |
| 103 |
fprintf(stderr, "dnswire_reader_add() error\n"); |
| 104 |
return 1; |
| 105 |
} |
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
if (dnswire_writer_popped(writer)) { |
| 110 |
if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { |
| 111 |
fprintf(stderr, "fwrite() failed\n"); |
| 112 |
return 1; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
dnswire_writer_destroy(writer); |
| 117 |
fclose(fp); |
| 118 |
return 0; |
| 119 |
} |