diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-01-14 15:03:48 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-01-14 15:03:48 +0000 |
commit | be956cd27353a4bb585b1a648e8469cf7adb5edf (patch) | |
tree | a473793c3fd59ace461f23a8d75d9ca29a82b4ff /src/test | |
parent | Initial commit. (diff) | |
download | dnswire-upstream/0.2.0.tar.xz dnswire-upstream/0.2.0.zip |
Adding upstream version 0.2.0.upstream/0.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/Makefile.am | 70 | ||||
-rw-r--r-- | src/test/create_dnstap.c | 51 | ||||
-rw-r--r-- | src/test/print_dnstap.c | 117 | ||||
-rw-r--r-- | src/test/reader_push.c | 66 | ||||
-rw-r--r-- | src/test/reader_read.c | 48 | ||||
-rw-r--r-- | src/test/reader_unixsock.c | 86 | ||||
-rw-r--r-- | src/test/test.dnstap | bin | 0 -> 240 bytes | |||
-rw-r--r-- | src/test/test1.gold | 28 | ||||
-rwxr-xr-x | src/test/test1.sh | 4 | ||||
-rw-r--r-- | src/test/test2.gold | 29 | ||||
-rwxr-xr-x | src/test/test2.sh | 9 | ||||
-rw-r--r-- | src/test/test3.gold | 31 | ||||
-rwxr-xr-x | src/test/test3.sh | 6 | ||||
-rw-r--r-- | src/test/test4.gold | 30 | ||||
-rwxr-xr-x | src/test/test4.sh | 6 | ||||
-rw-r--r-- | src/test/test5.gold | 30 | ||||
-rwxr-xr-x | src/test/test5.sh | 7 | ||||
-rwxr-xr-x | src/test/test6.sh | 7 | ||||
-rw-r--r-- | src/test/test_decoder.c | 33 | ||||
-rw-r--r-- | src/test/test_dnstap.c | 60 | ||||
-rw-r--r-- | src/test/test_encoder.c | 41 | ||||
-rw-r--r-- | src/test/test_reader.c | 73 | ||||
-rw-r--r-- | src/test/test_writer.c | 94 | ||||
-rw-r--r-- | src/test/writer_pop.c | 119 | ||||
-rw-r--r-- | src/test/writer_unixsock.c | 134 | ||||
-rw-r--r-- | src/test/writer_write.c | 92 |
26 files changed, 1271 insertions, 0 deletions
diff --git a/src/test/Makefile.am b/src/test/Makefile.am new file mode 100644 index 0000000..f7bb5cf --- /dev/null +++ b/src/test/Makefile.am @@ -0,0 +1,70 @@ +4MAINTAINERCLEANFILES = $(srcdir)/Makefile.in + +CLEANFILES = test*.log test*.trs \ + test1.out test2.out test3.out test3.dnstap test4.out test4.dnstap \ + test5.out test5.sock *.gcda *.gcno *.gcov + +AM_CFLAGS = -I$(top_srcdir)/src \ + $(tinyframe_CFLAGS) \ + $(protobuf_c_CFLAGS) + +check_PROGRAMS = reader_read reader_push writer_write writer_pop \ + reader_unixsock writer_unixsock test_dnstap test_encoder test_decoder \ + test_reader test_writer +TESTS = test1.sh test2.sh test3.sh test4.sh test5.sh test6.sh +EXTRA_DIST = create_dnstap.c print_dnstap.c $(TESTS) test.dnstap \ + test1.gold test2.gold test3.gold test4.gold test5.gold + +reader_read_SOURCES = reader_read.c +reader_read_LDADD = ../libdnswire.la +reader_read_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +reader_push_SOURCES = reader_push.c +reader_push_LDADD = ../libdnswire.la +reader_push_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +writer_write_SOURCES = writer_write.c +writer_write_LDADD = ../libdnswire.la +writer_write_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +writer_pop_SOURCES = writer_pop.c +writer_pop_LDADD = ../libdnswire.la +writer_pop_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +reader_unixsock_SOURCES = reader_unixsock.c +reader_unixsock_LDADD = ../libdnswire.la +reader_unixsock_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +writer_unixsock_SOURCES = writer_unixsock.c +writer_unixsock_LDADD = ../libdnswire.la +writer_unixsock_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +test_dnstap_SOURCES = test_dnstap.c +test_dnstap_LDADD = ../libdnswire.la +test_dnstap_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +test_encoder_SOURCES = test_encoder.c +test_encoder_LDADD = ../libdnswire.la +test_encoder_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +test_decoder_SOURCES = test_decoder.c +test_decoder_LDADD = ../libdnswire.la +test_decoder_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +test_reader_SOURCES = test_reader.c +test_reader_LDADD = ../libdnswire.la +test_reader_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +test_writer_SOURCES = test_writer.c +test_writer_LDADD = ../libdnswire.la +test_writer_LDFLAGS = $(protobuf_c_LIBS) $(tinyframe_LIBS) -static + +if ENABLE_GCOV +gcov-local: + for src in $(reader_read_SOURCES) $(reader_push_SOURCES) \ +$(writer_write_SOURCES) $(writer_pop_SOURCES) $(reader_unixsock_SOURCES) \ +$(writer_unixsock_SOURCES) $(test_dnstap_SOURCES) $(test_encoder_SOURCES) \ +$(test_decoder_SOURCES) $(test_reader_SOURCES) $(test_writer_SOURCES); do \ + gcov -l -r -s "$(srcdir)" "$$src"; \ + done +endif diff --git a/src/test/create_dnstap.c b/src/test/create_dnstap.c new file mode 100644 index 0000000..9fbd4a8 --- /dev/null +++ b/src/test/create_dnstap.c @@ -0,0 +1,51 @@ +#include <dnswire/version.h> +#include <dnswire/dnstap.h> + +#include <errno.h> +#include <string.h> +#include <arpa/inet.h> +#include <time.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> + +static char dns_wire_format_placeholder[] = "dns_wire_format_placeholder"; +static unsigned char query_address[sizeof(struct in_addr)]; +static unsigned char response_address[sizeof(struct in_addr)]; + +static inline void create_dnstap(struct dnstap* d, const char* identity) +{ + dnstap_set_identity_string(*d, identity); + dnstap_set_version_string(*d, DNSWIRE_VERSION_STRING); + dnstap_set_type(*d, DNSTAP_TYPE_MESSAGE); + dnstap_message_set_type(*d, DNSTAP_MESSAGE_TYPE_TOOL_QUERY); + dnstap_message_set_socket_family(*d, DNSTAP_SOCKET_FAMILY_INET); + dnstap_message_set_socket_protocol(*d, DNSTAP_SOCKET_PROTOCOL_UDP); + + if (inet_pton(AF_INET, "127.0.0.1", query_address) != 1) { + fprintf(stderr, "inet_pton(127.0.0.1) failed: %s\n", strerror(errno)); + } else { + dnstap_message_set_query_address(*d, query_address, sizeof(query_address)); + } + dnstap_message_set_query_port(*d, 12345); + + struct timespec query_time = { 0, 0 }; + clock_gettime(CLOCK_REALTIME, &query_time); + dnstap_message_set_query_time_sec(*d, query_time.tv_sec); + dnstap_message_set_query_time_nsec(*d, query_time.tv_nsec); + + if (inet_pton(AF_INET, "127.0.0.1", response_address) != 1) { + fprintf(stderr, "inet_pton(127.0.0.1) failed: %s\n", strerror(errno)); + } else { + dnstap_message_set_response_address(*d, response_address, sizeof(response_address)); + } + dnstap_message_set_response_port(*d, 53); + + struct timespec response_time = { 0, 0 }; + clock_gettime(CLOCK_REALTIME, &response_time); + dnstap_message_set_response_time_sec(*d, response_time.tv_sec); + dnstap_message_set_response_time_nsec(*d, response_time.tv_nsec); + + dnstap_message_set_query_message(*d, dns_wire_format_placeholder, sizeof(dns_wire_format_placeholder) - 1); + dnstap_message_set_response_message(*d, dns_wire_format_placeholder, sizeof(dns_wire_format_placeholder) - 1); +} diff --git a/src/test/print_dnstap.c b/src/test/print_dnstap.c new file mode 100644 index 0000000..b758119 --- /dev/null +++ b/src/test/print_dnstap.c @@ -0,0 +1,117 @@ +#include <dnswire/dnstap.h> + +#include <stdio.h> +#include <inttypes.h> +#include <arpa/inet.h> +#include <ctype.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> + +static const char* printable_string(const uint8_t* data, size_t len) +{ + static char buf[512], hex; + size_t r = 0, w = 0; + + while (r < len && w < sizeof(buf) - 1) { + if (isprint(data[r])) { + buf[w++] = data[r++]; + } else { + if (w + 4 >= sizeof(buf) - 1) { + break; + } + + buf[w++] = '\\'; + buf[w++] = 'x'; + hex = (data[r] & 0xf0) >> 4; + if (hex > 9) { + buf[w++] = 'a' + (hex - 10); + } else { + buf[w++] = '0' + hex; + } + hex = data[r++] & 0xf; + if (hex > 9) { + buf[w++] = 'a' + (hex - 10); + } else { + buf[w++] = '0' + hex; + } + } + } + if (w >= sizeof(buf)) { + buf[sizeof(buf) - 1] = 0; + } else { + buf[w] = 0; + } + + return buf; +} + +static const char* printable_ip_address(const uint8_t* data, size_t len) +{ + static char buf[INET6_ADDRSTRLEN]; + + buf[0] = 0; + if (len == 4) { + inet_ntop(AF_INET, data, buf, sizeof(buf)); + } else if (len == 16) { + inet_ntop(AF_INET6, data, buf, sizeof(buf)); + } + + return buf; +} + +static void print_dnstap(const struct dnstap* d) +{ + printf("---- dnstap\n"); + if (dnstap_has_identity(*d)) { + printf("identity: %s\n", printable_string(dnstap_identity(*d), dnstap_identity_length(*d))); + } + if (dnstap_has_version(*d)) { + printf("version: %s\n", printable_string(dnstap_version(*d), dnstap_version_length(*d))); + } + if (dnstap_has_extra(*d)) { + printf("extra: %s\n", printable_string(dnstap_extra(*d), dnstap_extra_length(*d))); + } + + if (dnstap_type(*d) == DNSTAP_TYPE_MESSAGE && dnstap_has_message(*d)) { + printf("message:\n type: %s\n", DNSTAP_MESSAGE_TYPE_STRING[dnstap_message_type(*d)]); + + if (dnstap_message_has_query_time_sec(*d) && dnstap_message_has_query_time_nsec(*d)) { + printf(" query_time: %" PRIu64 ".%" PRIu32 "\n", dnstap_message_query_time_sec(*d), dnstap_message_query_time_nsec(*d)); + } + if (dnstap_message_has_response_time_sec(*d) && dnstap_message_has_response_time_nsec(*d)) { + printf(" response_time: %" PRIu64 ".%" PRIu32 "\n", dnstap_message_response_time_sec(*d), dnstap_message_response_time_nsec(*d)); + } + if (dnstap_message_has_socket_family(*d)) { + printf(" socket_family: %s\n", DNSTAP_SOCKET_FAMILY_STRING[dnstap_message_socket_family(*d)]); + } + if (dnstap_message_has_socket_protocol(*d)) { + printf(" socket_protocol: %s\n", DNSTAP_SOCKET_PROTOCOL_STRING[dnstap_message_socket_protocol(*d)]); + } + if (dnstap_message_has_query_address(*d)) { + printf(" query_address: %s\n", printable_ip_address(dnstap_message_query_address(*d), dnstap_message_query_address_length(*d))); + } + if (dnstap_message_has_query_port(*d)) { + printf(" query_port: %u\n", dnstap_message_query_port(*d)); + } + if (dnstap_message_has_response_address(*d)) { + printf(" response_address: %s\n", printable_ip_address(dnstap_message_response_address(*d), dnstap_message_response_address_length(*d))); + } + if (dnstap_message_has_response_port(*d)) { + printf(" response_port: %u\n", dnstap_message_response_port(*d)); + } + if (dnstap_message_has_query_zone(*d)) { + printf(" query_zone: %s\n", printable_string(dnstap_message_query_zone(*d), dnstap_message_query_zone_length(*d))); + } + if (dnstap_message_has_query_message(*d)) { + printf(" query_message_length: %zu\n", dnstap_message_query_message_length(*d)); + printf(" query_message: %s\n", printable_string(dnstap_message_query_message(*d), dnstap_message_query_message_length(*d))); + } + if (dnstap_message_has_response_message(*d)) { + printf(" response_message_length: %zu\n", dnstap_message_response_message_length(*d)); + printf(" response_message: %s\n", printable_string(dnstap_message_response_message(*d), dnstap_message_response_message_length(*d))); + } + } + + printf("----\n"); +} diff --git a/src/test/reader_push.c b/src/test/reader_push.c new file mode 100644 index 0000000..e077609 --- /dev/null +++ b/src/test/reader_push.c @@ -0,0 +1,66 @@ +#include <dnswire/reader.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "print_dnstap.c" + +int main(int argc, const char* argv[]) +{ + if (argc < 3) { + return 1; + } + + FILE* fp = fopen(argv[1], "r"); + if (!fp) { + return 1; + } + + int rbuf_len = atoi(argv[2]); + + struct dnswire_reader reader; + if (dnswire_reader_init(&reader) != dnswire_ok) { + return 1; + } + dnswire_reader_allow_bidirectional(&reader, false); + + size_t have = 0, at = 0; + uint8_t rbuf[rbuf_len]; + int done = 0; + + enum dnswire_result res = dnswire_need_more; + + while (!done) { + switch (res) { + case dnswire_have_dnstap: + print_dnstap(dnswire_reader_dnstap(reader)); + // fallthrough + case dnswire_again: + res = dnswire_reader_push(&reader, &rbuf[at], have, 0, 0); + have -= dnswire_reader_pushed(reader); + at += dnswire_reader_pushed(reader); + break; + case dnswire_need_more: + if (!have) { + have = fread(rbuf, 1, sizeof(rbuf), fp); + printf("read %zu\n", have); + at = 0; + } + res = dnswire_reader_push(&reader, &rbuf[at], have, 0, 0); + have -= dnswire_reader_pushed(reader); + at += dnswire_reader_pushed(reader); + break; + case dnswire_endofdata: + done = 1; + break; + default: + fprintf(stderr, "dnswire_reader_push() error\n"); + return 1; + } + } + + dnswire_reader_destroy(reader); + fclose(fp); + return 0; +} diff --git a/src/test/reader_read.c b/src/test/reader_read.c new file mode 100644 index 0000000..833d2ac --- /dev/null +++ b/src/test/reader_read.c @@ -0,0 +1,48 @@ +#include <dnswire/reader.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "print_dnstap.c" + +int main(int argc, const char* argv[]) +{ + if (argc < 2) { + return 1; + } + + FILE* fp = fopen(argv[1], "r"); + if (!fp) { + return 1; + } + + struct dnswire_reader reader; + if (dnswire_reader_init(&reader) != dnswire_ok) { + return 1; + } + dnswire_reader_allow_bidirectional(&reader, false); + + int done = 0; + + while (!done) { + switch (dnswire_reader_fread(&reader, fp)) { + case dnswire_have_dnstap: + print_dnstap(dnswire_reader_dnstap(reader)); + break; + case dnswire_again: + case dnswire_need_more: + break; + case dnswire_endofdata: + done = 1; + break; + default: + fprintf(stderr, "dnswire_reader_read() error\n"); + return 1; + } + } + + dnswire_reader_destroy(reader); + fclose(fp); + return 0; +} diff --git a/src/test/reader_unixsock.c b/src/test/reader_unixsock.c new file mode 100644 index 0000000..f9f0f56 --- /dev/null +++ b/src/test/reader_unixsock.c @@ -0,0 +1,86 @@ +#include <dnswire/writer.h> +#include <dnswire/reader.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <sys/types.h> +#include <signal.h> +#include <sys/wait.h> + +#include "print_dnstap.c" + +int main(int argc, const char* argv[]) +{ + if (argc < 2) { + return 1; + } + + struct sockaddr_un path; + memset(&path, 0, sizeof(struct sockaddr_un)); + path.sun_family = AF_UNIX; + strncpy(path.sun_path, argv[1], sizeof(path.sun_path) - 1); + + int readfd = socket(AF_UNIX, SOCK_STREAM, 0); + if (readfd == -1 + || bind(readfd, (struct sockaddr*)&path, sizeof(struct sockaddr_un)) + || listen(readfd, 1)) { + close(readfd); + return 1; + } + fprintf(stderr, "bind & listen\n"); + + int fd, ret = 1; + alarm(5); + if ((fd = accept(readfd, 0, 0))) { + fprintf(stderr, "accept\n"); + + struct dnswire_reader reader; + if (dnswire_reader_init(&reader) != dnswire_ok) { + return 1; + } + dnswire_reader_allow_bidirectional(&reader, true); + + // force small buffers to trigger buf resizing + reader.write_size = 4; + reader.write_inc = 4; + if (dnswire_reader_set_bufsize(&reader, 4) != dnswire_ok) { + return 1; + } + if (dnswire_reader_set_bufinc(&reader, 4) != dnswire_ok) { + return 1; + } + + int done = 0; + + while (!done) { + switch (dnswire_reader_read(&reader, fd)) { + case dnswire_have_dnstap: + print_dnstap(dnswire_reader_dnstap(reader)); + fflush(stdout); + break; + case dnswire_again: + case dnswire_need_more: + break; + case dnswire_endofdata: + done = 1; + break; + default: + fprintf(stderr, "dnswire_reader_read() error\n"); + shutdown(fd, SHUT_RDWR); + close(fd); + close(readfd); + return 1; + } + } + + dnswire_reader_destroy(reader); + shutdown(fd, SHUT_RDWR); + close(fd); + } + close(readfd); + return ret; +} diff --git a/src/test/test.dnstap b/src/test/test.dnstap Binary files differnew file mode 100644 index 0000000..e5d8f66 --- /dev/null +++ b/src/test/test.dnstap diff --git a/src/test/test1.gold b/src/test/test1.gold new file mode 100644 index 0000000..c1c799e --- /dev/null +++ b/src/test/test1.gold @@ -0,0 +1,28 @@ +---- dnstap +version: kdig 2.6.5 +message: + type: TOOL_QUERY + query_time: 1573730567.83350 + socket_family: INET + socket_protocol: UDP + query_address: 0.0.0.0 + query_port: 60417 + response_address: 172.17.0.1 + response_port: 53 + query_message_length: 28 + query_message: \x85 \x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01 +---- +---- dnstap +version: kdig 2.6.5 +message: + type: TOOL_RESPONSE + response_time: 1573730567.65816434 + socket_family: INET + socket_protocol: UDP + query_address: 0.0.0.0 + query_port: 60417 + response_address: 172.17.0.1 + response_port: 53 + response_message_length: 44 + response_message: \x85 \x81\x80\x00\x01\x00\x01\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x01,\x00\x04\xd8:\xd3\x0e +---- diff --git a/src/test/test1.sh b/src/test/test1.sh new file mode 100755 index 0000000..f54886f --- /dev/null +++ b/src/test/test1.sh @@ -0,0 +1,4 @@ +#!/bin/sh -xe + +./reader_read "$srcdir/test.dnstap" > test1.out +diff -u "$srcdir/test1.gold" test1.out diff --git a/src/test/test2.gold b/src/test/test2.gold new file mode 100644 index 0000000..25a3bc6 --- /dev/null +++ b/src/test/test2.gold @@ -0,0 +1,29 @@ +read 240 +---- dnstap +version: kdig 2.6.5 +message: + type: TOOL_QUERY + query_time: 1573730567.83350 + socket_family: INET + socket_protocol: UDP + query_address: 0.0.0.0 + query_port: 60417 + response_address: 172.17.0.1 + response_port: 53 + query_message_length: 28 + query_message: \x85 \x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01 +---- +---- dnstap +version: kdig 2.6.5 +message: + type: TOOL_RESPONSE + response_time: 1573730567.65816434 + socket_family: INET + socket_protocol: UDP + query_address: 0.0.0.0 + query_port: 60417 + response_address: 172.17.0.1 + response_port: 53 + response_message_length: 44 + response_message: \x85 \x81\x80\x00\x01\x00\x01\x00\x00\x00\x00\x06google\x03com\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x01,\x00\x04\xd8:\xd3\x0e +---- diff --git a/src/test/test2.sh b/src/test/test2.sh new file mode 100755 index 0000000..2b8e381 --- /dev/null +++ b/src/test/test2.sh @@ -0,0 +1,9 @@ +#!/bin/sh -xe + +./reader_push "$srcdir/test.dnstap" 10 +./reader_push "$srcdir/test.dnstap" 18 +./reader_push "$srcdir/test.dnstap" 32 +./reader_push "$srcdir/test.dnstap" 64 + +./reader_push "$srcdir/test.dnstap" 4096 > test2.out +diff -u "$srcdir/test2.gold" test2.out diff --git a/src/test/test3.gold b/src/test/test3.gold new file mode 100644 index 0000000..a6e5c22 --- /dev/null +++ b/src/test/test3.gold @@ -0,0 +1,31 @@ +read 322 +---- dnstap +identity: writer_write-1 +message: + type: TOOL_QUERY + socket_family: INET + socket_protocol: UDP + query_address: 127.0.0.1 + query_port: 12345 + response_address: 127.0.0.1 + response_port: 53 + query_message_length: 27 + query_message: dns_wire_format_placeholder + response_message_length: 27 + response_message: dns_wire_format_placeholder +---- +---- dnstap +identity: writer_write-2 +message: + type: TOOL_QUERY + socket_family: INET + socket_protocol: UDP + query_address: 127.0.0.1 + query_port: 12345 + response_address: 127.0.0.1 + response_port: 53 + query_message_length: 27 + query_message: dns_wire_format_placeholder + response_message_length: 27 + response_message: dns_wire_format_placeholder +---- diff --git a/src/test/test3.sh b/src/test/test3.sh new file mode 100755 index 0000000..a19eeb0 --- /dev/null +++ b/src/test/test3.sh @@ -0,0 +1,6 @@ +#!/bin/sh -xe + +rm -f test3.dnstap +./writer_write test3.dnstap > test3.out +./reader_push test3.dnstap 4096 | grep -v _time | grep -v ^version: >> test3.out +diff -u "$srcdir/test3.gold" test3.out diff --git a/src/test/test4.gold b/src/test/test4.gold new file mode 100644 index 0000000..f6c9f7f --- /dev/null +++ b/src/test/test4.gold @@ -0,0 +1,30 @@ +---- dnstap +identity: writer_pop-1 +message: + type: TOOL_QUERY + socket_family: INET + socket_protocol: UDP + query_address: 127.0.0.1 + query_port: 12345 + response_address: 127.0.0.1 + response_port: 53 + query_message_length: 27 + query_message: dns_wire_format_placeholder + response_message_length: 27 + response_message: dns_wire_format_placeholder +---- +---- dnstap +identity: writer_pop-2 +message: + type: TOOL_QUERY + socket_family: INET + socket_protocol: UDP + query_address: 127.0.0.1 + query_port: 12345 + response_address: 127.0.0.1 + response_port: 53 + query_message_length: 27 + query_message: dns_wire_format_placeholder + response_message_length: 27 + response_message: dns_wire_format_placeholder +---- diff --git a/src/test/test4.sh b/src/test/test4.sh new file mode 100755 index 0000000..e1e9f2e --- /dev/null +++ b/src/test/test4.sh @@ -0,0 +1,6 @@ +#!/bin/sh -xe + +rm -f test4.dnstap +./writer_pop test4.dnstap > test4.out +./reader_read test4.dnstap | grep -v _time | grep -v ^version: >> test4.out +diff -u "$srcdir/test4.gold" test4.out diff --git a/src/test/test5.gold b/src/test/test5.gold new file mode 100644 index 0000000..86404aa --- /dev/null +++ b/src/test/test5.gold @@ -0,0 +1,30 @@ +---- dnstap +identity: writer_reader_unixsock-1 +message: + type: TOOL_QUERY + socket_family: INET + socket_protocol: UDP + query_address: 127.0.0.1 + query_port: 12345 + response_address: 127.0.0.1 + response_port: 53 + query_message_length: 27 + query_message: dns_wire_format_placeholder + response_message_length: 27 + response_message: dns_wire_format_placeholder +---- +---- dnstap +identity: writer_reader_unixsock-2 +message: + type: TOOL_QUERY + socket_family: INET + socket_protocol: UDP + query_address: 127.0.0.1 + query_port: 12345 + response_address: 127.0.0.1 + response_port: 53 + query_message_length: 27 + query_message: dns_wire_format_placeholder + response_message_length: 27 + response_message: dns_wire_format_placeholder +---- diff --git a/src/test/test5.sh b/src/test/test5.sh new file mode 100755 index 0000000..0b75dff --- /dev/null +++ b/src/test/test5.sh @@ -0,0 +1,7 @@ +#!/bin/sh -xe + +rm -f test5.sock +./writer_unixsock test5.sock & +./reader_unixsock test5.sock | grep -v _time | grep -v ^version: > test5.out +rm -f test5.sock +diff -u "$srcdir/test5.gold" test5.out diff --git a/src/test/test6.sh b/src/test/test6.sh new file mode 100755 index 0000000..834137c --- /dev/null +++ b/src/test/test6.sh @@ -0,0 +1,7 @@ +#!/bin/sh -xe + +./test_dnstap +./test_encoder +./test_decoder +./test_reader +./test_writer diff --git a/src/test/test_decoder.c b/src/test/test_decoder.c new file mode 100644 index 0000000..2229512 --- /dev/null +++ b/src/test/test_decoder.c @@ -0,0 +1,33 @@ +#include <dnswire/decoder.h> + +#include <assert.h> + +#include "create_dnstap.c" + +int main(void) +{ + uint8_t buf[1]; + struct dnswire_decoder d = DNSWIRE_DECODER_INITIALIZER; + + // decoder * need more + d.state = dnswire_decoder_reading_control; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + d.state = dnswire_decoder_checking_ready; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + d.state = dnswire_decoder_checking_accept; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + d.state = dnswire_decoder_reading_start; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + d.state = dnswire_decoder_checking_start; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + d.state = dnswire_decoder_reading_frames; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + d.state = dnswire_decoder_checking_finish; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_need_more); + + // decoder done + d.state = dnswire_decoder_done; + assert(dnswire_decoder_decode(&d, buf, 1) == dnswire_error); + + return 0; +} diff --git a/src/test/test_dnstap.c b/src/test/test_dnstap.c new file mode 100644 index 0000000..fd199f8 --- /dev/null +++ b/src/test/test_dnstap.c @@ -0,0 +1,60 @@ +#include <dnswire/dnstap.h> + +#include <assert.h> + +#include "create_dnstap.c" + +int main(void) +{ + uint8_t buf[256 * 1024]; + size_t s; + struct dnstap d = DNSTAP_INITIALIZER; + struct dnstap u = DNSTAP_INITIALIZER; + + create_dnstap(&d, "test_dnstap"); + + // failed unpack + assert(dnstap_decode_protobuf(&u, buf, 1) == 1); + + // invalid dnstap.type + d.dnstap.type = (enum _Dnstap__Dnstap__Type)(DNSTAP_TYPE_MESSAGE + 1); + s = dnstap_encode_protobuf_size(&d); + assert(s < sizeof(buf)); + assert(dnstap_encode_protobuf(&d, buf) == s); + assert(dnstap_decode_protobuf(&u, buf, s) == 0); + assert(u.dnstap.type == (enum _Dnstap__Dnstap__Type)DNSTAP_TYPE_UNKNOWN); + dnstap_cleanup(&u); + d.dnstap.type = (enum _Dnstap__Dnstap__Type)DNSTAP_TYPE_MESSAGE; + + // invalid message.type + d.message.type = (enum _Dnstap__Message__Type)(DNSTAP_MESSAGE_TYPE_TOOL_RESPONSE + 1); + s = dnstap_encode_protobuf_size(&d); + assert(s < sizeof(buf)); + assert(dnstap_encode_protobuf(&d, buf) == s); + assert(dnstap_decode_protobuf(&u, buf, s) == 0); + assert(u.message.type == (enum _Dnstap__Message__Type)DNSTAP_MESSAGE_TYPE_UNKNOWN); + dnstap_cleanup(&u); + d.message.type = (enum _Dnstap__Message__Type)DNSTAP_MESSAGE_TYPE_TOOL_QUERY; + + // invalid message.socket_family + d.message.socket_family = (enum _Dnstap__SocketFamily)(DNSTAP_SOCKET_FAMILY_INET6 + 1); + s = dnstap_encode_protobuf_size(&d); + assert(s < sizeof(buf)); + assert(dnstap_encode_protobuf(&d, buf) == s); + assert(dnstap_decode_protobuf(&u, buf, s) == 0); + assert(u.message.socket_family == (enum _Dnstap__SocketFamily)DNSTAP_SOCKET_FAMILY_UNKNOWN); + dnstap_cleanup(&u); + d.message.socket_family = (enum _Dnstap__SocketFamily)DNSTAP_SOCKET_FAMILY_INET; + + // invalid message.socket_protocol + d.message.socket_protocol = (enum _Dnstap__SocketProtocol)(DNSTAP_SOCKET_PROTOCOL_TCP + 1); + s = dnstap_encode_protobuf_size(&d); + assert(s < sizeof(buf)); + assert(dnstap_encode_protobuf(&d, buf) == s); + assert(dnstap_decode_protobuf(&u, buf, s) == 0); + assert(u.message.socket_protocol == (enum _Dnstap__SocketProtocol)DNSTAP_SOCKET_PROTOCOL_UNKNOWN); + dnstap_cleanup(&u); + d.message.socket_protocol = (enum _Dnstap__SocketProtocol)DNSTAP_SOCKET_PROTOCOL_UDP; + + return 0; +} diff --git a/src/test/test_encoder.c b/src/test/test_encoder.c new file mode 100644 index 0000000..65161f1 --- /dev/null +++ b/src/test/test_encoder.c @@ -0,0 +1,41 @@ +#include <dnswire/encoder.h> + +#include <assert.h> + +#include "create_dnstap.c" + +int main(void) +{ + uint8_t buf[1]; + struct dnswire_encoder e = DNSWIRE_ENCODER_INITIALIZER; + struct dnstap d = DNSTAP_INITIALIZER; + + create_dnstap(&d, "test_encoder"); + + // encoder stop at wrong state + assert(dnswire_encoder_stop(&e) == dnswire_error); + + // encoder * need more + e.state = dnswire_encoder_control_ready; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_need_more); + e.state = dnswire_encoder_control_start; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_need_more); + e.state = dnswire_encoder_control_accept; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_need_more); + e.state = dnswire_encoder_control_finish; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_need_more); + e.state = dnswire_encoder_control_stop; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_need_more); + + // encoder frame + e.state = dnswire_encoder_frames; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_error); + dnswire_encoder_set_dnstap(e, &d); + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_need_more); + + // encoder done + e.state = dnswire_encoder_done; + assert(dnswire_encoder_encode(&e, buf, 1) == dnswire_error); + + return 0; +} diff --git a/src/test/test_reader.c b/src/test/test_reader.c new file mode 100644 index 0000000..7c64450 --- /dev/null +++ b/src/test/test_reader.c @@ -0,0 +1,73 @@ +#include <dnswire/reader.h> + +#include <assert.h> + +int main(void) +{ + uint8_t buf[1], buf2[1]; + size_t s; + struct dnswire_reader r; + assert(dnswire_reader_init(&r) == dnswire_ok); + assert(dnswire_reader_allow_bidirectional(&r, true) == dnswire_ok); + + // set bufsize + r.left = 2; + assert(dnswire_reader_set_bufsize(&r, 1) == dnswire_error); + r.left = 1; + r.at = 1; + assert(dnswire_reader_set_bufsize(&r, 1) == dnswire_ok); + r.left = 0; + assert(dnswire_reader_set_bufsize(&r, DNSWIRE_MAXIMUM_BUF_SIZE + 1) == dnswire_error); + assert(dnswire_reader_set_bufsize(&r, DNSWIRE_DEFAULT_BUF_SIZE) == dnswire_ok); + + // set bufinc + assert(dnswire_reader_set_bufinc(&r, DNSWIRE_DEFAULT_BUF_SIZE) == dnswire_ok); + + // set bufmax + assert(dnswire_reader_set_bufmax(&r, DNSWIRE_DEFAULT_BUF_SIZE - 1) == dnswire_error); + assert(dnswire_reader_set_bufmax(&r, DNSWIRE_MAXIMUM_BUF_SIZE) == dnswire_ok); + + // reader push + r.state = dnswire_reader_reading_control; + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_need_more); + r.state = dnswire_reader_encoding_accept; + s = sizeof(buf2); + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_again); + r.state = dnswire_reader_reading; + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_need_more); + r.state = dnswire_reader_encoding_finish; + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_again); + r.decoder.state = dnswire_decoder_done; + r.left = 1; + r.state = dnswire_reader_decoding_control; + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_error); + r.state = dnswire_reader_decoding; + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_error); + r.state = dnswire_reader_done; + assert(dnswire_reader_push(&r, buf, 0, buf2, &s) == dnswire_error); + + // reset reader + dnswire_reader_destroy(r); + assert(dnswire_reader_init(&r) == dnswire_ok); + assert(dnswire_reader_allow_bidirectional(&r, true) == dnswire_ok); + + // reader read + r.state = dnswire_reader_reading_control; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + r.state = dnswire_reader_writing_accept; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + r.state = dnswire_reader_reading; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + r.state = dnswire_reader_writing_finish; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + r.decoder.state = dnswire_decoder_done; + r.left = 1; + r.state = dnswire_reader_decoding_control; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + r.state = dnswire_reader_decoding; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + r.state = dnswire_reader_done; + assert(dnswire_reader_read(&r, -1) == dnswire_error); + + return 0; +} diff --git a/src/test/test_writer.c b/src/test/test_writer.c new file mode 100644 index 0000000..74453b5 --- /dev/null +++ b/src/test/test_writer.c @@ -0,0 +1,94 @@ +#include <dnswire/writer.h> + +#include <assert.h> + +int main(void) +{ + uint8_t buf[1], buf2[1]; + size_t s; + struct dnswire_writer w; + assert(dnswire_writer_init(&w) == dnswire_ok); + + // set bidirectional + assert(dnswire_writer_set_bidirectional(&w, true) == dnswire_ok); + assert(dnswire_writer_set_bidirectional(&w, false) == dnswire_ok); + + // set bufsize + w.left = 2; + assert(dnswire_writer_set_bufsize(&w, 1) == dnswire_error); + w.left = 1; + w.at = 1; + assert(dnswire_writer_set_bufsize(&w, 1) == dnswire_ok); + w.left = 0; + assert(dnswire_writer_set_bufsize(&w, DNSWIRE_MAXIMUM_BUF_SIZE + 1) == dnswire_error); + assert(dnswire_writer_set_bufsize(&w, DNSWIRE_DEFAULT_BUF_SIZE) == dnswire_ok); + + // set bufinc + assert(dnswire_writer_set_bufinc(&w, DNSWIRE_DEFAULT_BUF_SIZE) == dnswire_ok); + + // set bufmax + assert(dnswire_writer_set_bufmax(&w, DNSWIRE_DEFAULT_BUF_SIZE - 1) == dnswire_error); + assert(dnswire_writer_set_bufmax(&w, DNSWIRE_MAXIMUM_BUF_SIZE) == dnswire_ok); + + // writer pop + w.state = dnswire_writer_encoding_ready; + s = sizeof(buf2); + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_again); + w.state = dnswire_writer_reading_accept; + s = 0; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_need_more); + s = sizeof(buf2); + w.read_left = 1; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_need_more); + w.state = dnswire_writer_stopping; + w.read_left = 1; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_again); + w.read_left = 0; + w.state = dnswire_writer_reading_finish; + s = 0; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_need_more); + s = sizeof(buf2); + w.read_left = 1; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_need_more); + w.decoder.state = dnswire_decoder_done; + w.state = dnswire_writer_decoding_accept; + w.read_left = 1; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_error); + w.state = dnswire_writer_decoding_finish; + w.read_left = 1; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_error); + w.state = dnswire_writer_done; + assert(dnswire_writer_pop(&w, buf, 1, buf2, &s) == dnswire_error); + + // reset writer + dnswire_writer_destroy(w); + assert(dnswire_writer_init(&w) == dnswire_ok); + assert(dnswire_writer_set_bidirectional(&w, true) == dnswire_ok); + + // writer write + w.state = dnswire_writer_writing_ready; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.state = dnswire_writer_reading_accept; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.state = dnswire_writer_writing; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.state = dnswire_writer_stopping; + w.left = 1; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.state = dnswire_writer_writing_stop; + w.left = 1; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.left = 0; + w.state = dnswire_writer_reading_finish; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.read_left = 1; + w.decoder.state = dnswire_decoder_done; + w.state = dnswire_writer_decoding_accept; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.state = dnswire_writer_decoding_finish; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + w.state = dnswire_writer_done; + assert(dnswire_writer_write(&w, -1) == dnswire_error); + + return 0; +} diff --git a/src/test/writer_pop.c b/src/test/writer_pop.c new file mode 100644 index 0000000..69070b3 --- /dev/null +++ b/src/test/writer_pop.c @@ -0,0 +1,119 @@ +#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; + } + + uint8_t buf[4096]; + + struct dnstap d = DNSTAP_INITIALIZER; + create_dnstap(&d, "writer_pop-1"); + + dnswire_writer_set_dnstap(writer, &d); + + while (1) { + if (dnswire_writer_popped(writer)) { + if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { + fprintf(stderr, "fwrite() failed\n"); + return 1; + } + } + enum dnswire_result res = dnswire_writer_pop(&writer, buf, sizeof(buf), 0, 0); + switch (res) { + case dnswire_ok: + break; + + case dnswire_again: + case dnswire_need_more: + continue; + + default: + fprintf(stderr, "dnswire_writer_pop() error\n"); + return 1; + } + break; + } + + create_dnstap(&d, "writer_pop-2"); + + while (1) { + if (dnswire_writer_popped(writer)) { + if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { + fprintf(stderr, "fwrite() failed\n"); + return 1; + } + } + enum dnswire_result res = dnswire_writer_pop(&writer, buf, sizeof(buf), 0, 0); + switch (res) { + case dnswire_ok: + break; + + case dnswire_again: + case dnswire_need_more: + continue; + + default: + fprintf(stderr, "dnswire_writer_pop() error\n"); + return 1; + } + break; + } + + if (dnswire_writer_stop(&writer) != dnswire_ok) { + fprintf(stderr, "dnswire_writer_stop() failed\n"); + return 1; + } + + while (1) { + if (dnswire_writer_popped(writer)) { + if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { + fprintf(stderr, "fwrite() failed\n"); + return 1; + } + } + enum dnswire_result res = dnswire_writer_pop(&writer, buf, sizeof(buf), 0, 0); + 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; + } + + if (dnswire_writer_popped(writer)) { + if (fwrite(buf, 1, dnswire_writer_popped(writer), fp) != dnswire_writer_popped(writer)) { + fprintf(stderr, "fwrite() failed\n"); + return 1; + } + } + + dnswire_writer_destroy(writer); + fclose(fp); + return 0; +} diff --git a/src/test/writer_unixsock.c b/src/test/writer_unixsock.c new file mode 100644 index 0000000..3290c9b --- /dev/null +++ b/src/test/writer_unixsock.c @@ -0,0 +1,134 @@ +#include <dnswire/writer.h> +#include <dnswire/reader.h> + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <sys/types.h> +#include <signal.h> +#include <sys/wait.h> + +#include "create_dnstap.c" + +int main(int argc, const char* argv[]) +{ + if (argc < 2) { + return 1; + } + + struct sockaddr_un path; + memset(&path, 0, sizeof(struct sockaddr_un)); + path.sun_family = AF_UNIX; + strncpy(path.sun_path, argv[1], sizeof(path.sun_path) - 1); + + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if (fd == -1) { + return 1; + } + + int ret = 1, attempts = 5; + while (attempts--) { + if (!(ret = connect(fd, (struct sockaddr*)&path, sizeof(struct sockaddr_un)))) { + break; + } else { + fprintf(stderr, "sleep\n"); + sleep(1); + } + } + if (ret) { + close(fd); + return ret; + } + + struct dnswire_writer writer; + if (dnswire_writer_init(&writer) != dnswire_ok) { + return 1; + } + if (dnswire_writer_set_bidirectional(&writer, true) != dnswire_ok) { + return 1; + } + + // force small buffers to trigger buf resizing + writer.read_size = 4; + writer.read_inc = 4; + if (dnswire_writer_set_bufsize(&writer, 4) != dnswire_ok) { + return 1; + } + if (dnswire_writer_set_bufinc(&writer, 4) != dnswire_ok) { + return 1; + } + + struct dnstap d = DNSTAP_INITIALIZER; + create_dnstap(&d, "writer_reader_unixsock-1"); + + dnswire_writer_set_dnstap(writer, &d); + + while (1) { + enum dnswire_result res = dnswire_writer_write(&writer, fd); + switch (res) { + case dnswire_ok: + break; + + case dnswire_again: + case dnswire_need_more: + continue; + + default: + fprintf(stderr, "dnswire_writer_write() error\n"); + return 1; + } + break; + } + + create_dnstap(&d, "writer_reader_unixsock-2"); + + while (1) { + enum dnswire_result res = dnswire_writer_write(&writer, fd); + switch (res) { + case dnswire_ok: + break; + + case dnswire_again: + case dnswire_need_more: + continue; + + default: + fprintf(stderr, "dnswire_writer_write() 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_write(&writer, fd); + switch (res) { + case dnswire_ok: + case dnswire_endofdata: + break; + + case dnswire_again: + case dnswire_need_more: + continue; + + default: + fprintf(stderr, "dnswire_writer_write() error\n"); + return 1; + } + break; + } + + dnswire_writer_destroy(writer); + + shutdown(fd, SHUT_RDWR); + close(fd); + + return ret; +} diff --git a/src/test/writer_write.c b/src/test/writer_write.c new file mode 100644 index 0000000..c003895 --- /dev/null +++ b/src/test/writer_write.c @@ -0,0 +1,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; +} |