summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am48
-rw-r--r--src/decoder.c203
-rw-r--r--src/dnstap.c141
-rw-r--r--src/dnstap.fields16
-rw-r--r--src/dnswire.c34
-rw-r--r--src/dnswire/decoder.h64
-rw-r--r--src/dnswire/dnstap.h151
-rw-r--r--src/dnswire/dnswire.h39
-rw-r--r--src/dnswire/encoder.h60
-rw-r--r--src/dnswire/reader.h91
-rw-r--r--src/dnswire/trace.h.in41
-rw-r--r--src/dnswire/version.h.in33
-rw-r--r--src/dnswire/writer.h90
-rw-r--r--src/encoder.c172
-rwxr-xr-xsrc/gen-macros.sh34
-rw-r--r--src/reader.c700
-rw-r--r--src/test/Makefile.am70
-rw-r--r--src/test/create_dnstap.c51
-rw-r--r--src/test/print_dnstap.c117
-rw-r--r--src/test/reader_push.c66
-rw-r--r--src/test/reader_read.c48
-rw-r--r--src/test/reader_unixsock.c86
-rw-r--r--src/test/test.dnstapbin0 -> 240 bytes
-rw-r--r--src/test/test1.gold28
-rwxr-xr-xsrc/test/test1.sh4
-rw-r--r--src/test/test2.gold29
-rwxr-xr-xsrc/test/test2.sh9
-rw-r--r--src/test/test3.gold31
-rwxr-xr-xsrc/test/test3.sh6
-rw-r--r--src/test/test4.gold30
-rwxr-xr-xsrc/test/test4.sh6
-rw-r--r--src/test/test5.gold30
-rwxr-xr-xsrc/test/test5.sh7
-rwxr-xr-xsrc/test/test6.sh7
-rw-r--r--src/test/test_decoder.c33
-rw-r--r--src/test/test_dnstap.c60
-rw-r--r--src/test/test_encoder.c41
-rw-r--r--src/test/test_reader.c73
-rw-r--r--src/test/test_writer.c94
-rw-r--r--src/test/writer_pop.c119
-rw-r--r--src/test/writer_unixsock.c134
-rw-r--r--src/test/writer_write.c92
-rw-r--r--src/trace.c52
-rw-r--r--src/writer.c710
44 files changed, 3950 insertions, 0 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
new file mode 100644
index 0000000..07a49dd
--- /dev/null
+++ b/src/Makefile.am
@@ -0,0 +1,48 @@
+MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
+BUILT_SOURCES =
+CLEANFILES = *.gcda *.gcno *.gcov
+EXTRA_DIST =
+
+SUBDIRS = test
+
+AM_CFLAGS = $(tinyframe_CFLAGS) \
+ $(protobuf_c_CFLAGS)
+
+lib_LTLIBRARIES = libdnswire.la
+
+libdnswire_la_SOURCES = decoder.c dnstap.c dnswire.c encoder.c reader.c \
+ writer.c trace.c
+nodist_libdnswire_la_SOURCES = dnstap.pb-c.c
+BUILT_SOURCES += dnswire/dnstap.pb-c.h
+nobase_include_HEADERS = dnswire/decoder.h dnswire/dnstap.h \
+ dnswire/dnswire.h dnswire/encoder.h dnswire/reader.h dnswire/writer.h
+nobase_nodist_include_HEADERS = dnswire/version.h dnswire/dnstap.pb-c.h \
+ dnswire/dnstap-macros.h dnswire/trace.h
+libdnswire_la_LDFLAGS = -version-info $(DNSWIRE_LIBRARY_VERSION) \
+ $(protobuf_c_LIBS) \
+ $(tinyframe_LIBS)
+
+CLEANFILES += $(nodist_libdnswire_la_SOURCES)
+EXTRA_DIST += dnstap.pb/dnstap.proto dnstap.pb/LICENSE dnstap.pb/README.md
+
+dnswire/dnstap.pb-c.h: dnstap.pb-c.c
+ mkdir -p dnswire/
+ cp dnstap.pb-c.h dnswire/
+
+dnstap.pb-c.c: dnstap.pb/dnstap.proto
+ $(AM_V_GEN)@PROTOC_C@ "--c_out=." -I$(srcdir)/dnstap.pb "$(srcdir)/dnstap.pb/dnstap.proto"
+
+BUILT_SOURCES += dnswire/dnstap-macros.h
+EXTRA_DIST += gen-macros.sh dnstap.fields
+
+dnswire/dnstap-macros.h: dnstap.fields gen-macros.sh
+ $(AM_V_GEN)"$(srcdir)/gen-macros.sh" "$(srcdir)/dnstap.fields" >dnswire/dnstap-macros.h
+
+CLEANFILES += $(BUILT_SOURCES)
+
+if ENABLE_GCOV
+gcov-local:
+ for src in $(libdnswire_la_SOURCES); do \
+ gcov -l -r -s "$(srcdir)" "$$src"; \
+ done
+endif
diff --git a/src/decoder.c b/src/decoder.c
new file mode 100644
index 0000000..821b14e
--- /dev/null
+++ b/src/decoder.c
@@ -0,0 +1,203 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/decoder.h"
+#include "dnswire/trace.h"
+
+#include <assert.h>
+
+const char* const dnswire_decoder_state_string[] = {
+ "reading_control",
+ "checking_ready",
+ "checking_accept",
+ "reading_start",
+ "checking_start",
+ "reading_frames",
+ "checking_finish",
+ "done",
+};
+
+#define __state(h, s) \
+ __trace("state %s => %s", dnswire_decoder_state_string[(h)->state], dnswire_decoder_state_string[s]); \
+ (h)->state = s;
+
+enum dnswire_result dnswire_decoder_decode(struct dnswire_decoder* handle, const uint8_t* data, size_t len)
+{
+ assert(handle);
+ assert(data);
+ assert(len);
+
+ switch (handle->state) {
+ case dnswire_decoder_reading_control:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_have_control:
+ switch (handle->reader.control.type) {
+ case TINYFRAME_CONTROL_READY:
+ // indicate that we have bidirectional communications
+ __state(handle, dnswire_decoder_checking_ready);
+ return dnswire_again;
+
+ case TINYFRAME_CONTROL_ACCEPT:
+ // indicate that we have bidirectional communications
+ __state(handle, dnswire_decoder_checking_accept);
+ return dnswire_again;
+
+ case TINYFRAME_CONTROL_START:
+ __state(handle, dnswire_decoder_checking_start);
+ return dnswire_again;
+
+ default:
+ break;
+ }
+ break;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_decoder_checking_ready:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_have_control_field:
+ if (handle->reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE) {
+ return dnswire_error;
+ }
+ if (!strncmp(DNSTAP_PROTOBUF_CONTENT_TYPE, (const char*)handle->reader.control_field.data, handle->reader.control_field.length)) {
+ handle->ready_support_dnstap_protobuf = 1;
+ }
+ if (!handle->reader.control_length_left) {
+ __state(handle, dnswire_decoder_reading_start);
+ return dnswire_bidirectional;
+ }
+ return dnswire_again;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_decoder_checking_accept:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_have_control_field:
+ if (handle->reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE) {
+ return dnswire_error;
+ }
+ if (!strncmp(DNSTAP_PROTOBUF_CONTENT_TYPE, (const char*)handle->reader.control_field.data, handle->reader.control_field.length)) {
+ handle->accept_support_dnstap_protobuf = 1;
+ }
+ if (!handle->reader.control_length_left) {
+ __state(handle, dnswire_decoder_checking_finish);
+ return dnswire_bidirectional;
+ }
+ return dnswire_again;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_decoder_reading_start:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_have_control:
+ switch (handle->reader.control.type) {
+ case TINYFRAME_CONTROL_START:
+ __state(handle, dnswire_decoder_checking_start);
+ return dnswire_again;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_decoder_checking_start:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_have_control_field:
+ if (handle->reader.control_field.type != TINYFRAME_CONTROL_FIELD_CONTENT_TYPE) {
+ return dnswire_error;
+ }
+ if (strncmp(DNSTAP_PROTOBUF_CONTENT_TYPE, (const char*)handle->reader.control_field.data, handle->reader.control_field.length)) {
+ return dnswire_error;
+ }
+ __state(handle, dnswire_decoder_reading_frames);
+ return dnswire_again;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_decoder_reading_frames:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_have_frame:
+ dnstap_cleanup(&handle->dnstap);
+ if (dnstap_decode_protobuf(&handle->dnstap, handle->reader.frame.data, handle->reader.frame.length)) {
+ return dnswire_error;
+ }
+ return dnswire_have_dnstap;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ case tinyframe_stopped:
+ __state(handle, dnswire_decoder_done);
+ return dnswire_endofdata;
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_decoder_checking_finish:
+ switch (tinyframe_read(&handle->reader, data, len)) {
+ case tinyframe_finished:
+ __state(handle, dnswire_decoder_done);
+ return dnswire_endofdata;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_decoder_done:
+ break;
+ }
+
+ return dnswire_error;
+}
diff --git a/src/dnstap.c b/src/dnstap.c
new file mode 100644
index 0000000..c5275b7
--- /dev/null
+++ b/src/dnstap.c
@@ -0,0 +1,141 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/dnstap.h"
+
+const char* const DNSTAP_TYPE_STRING[] = {
+ "UNKNOWN",
+ "MESSAGE",
+};
+const char* const DNSTAP_MESSAGE_TYPE_STRING[] = {
+ "UNKNOWN",
+ "AUTH_QUERY",
+ "AUTH_RESPONSE",
+ "RESOLVER_QUERY",
+ "RESOLVER_RESPONSE",
+ "CLIENT_QUERY",
+ "CLIENT_RESPONSE",
+ "FORWARDER_QUERY",
+ "FORWARDER_RESPONSE",
+ "STUB_QUERY",
+ "STUB_RESPONSE",
+ "TOOL_QUERY",
+ "TOOL_RESPONSE",
+};
+const char* const DNSTAP_SOCKET_FAMILY_STRING[] = {
+ "UNKNOWN",
+ "INET",
+ "INET6",
+};
+const char* const DNSTAP_SOCKET_PROTOCOL_STRING[] = {
+ "UNKNOWN",
+ "UDP",
+ "TCP",
+};
+
+int dnstap_decode_protobuf(struct dnstap* dnstap, const uint8_t* data, size_t len)
+{
+ assert(dnstap);
+ assert(data);
+ assert(!dnstap->unpacked_dnstap);
+
+ if (!(dnstap->unpacked_dnstap = dnstap__dnstap__unpack(NULL, len, data))) {
+ return 1;
+ }
+
+ dnstap->dnstap = *dnstap->unpacked_dnstap;
+
+ switch (dnstap->dnstap.type) {
+ case DNSTAP_TYPE_MESSAGE:
+ break;
+ default:
+ dnstap->dnstap.type = (enum _Dnstap__Dnstap__Type)DNSTAP_TYPE_UNKNOWN;
+ }
+
+ if (dnstap->dnstap.message) {
+ dnstap->message = *dnstap->dnstap.message;
+
+ switch (dnstap->message.type) {
+ case DNSTAP_MESSAGE_TYPE_AUTH_QUERY:
+ case DNSTAP_MESSAGE_TYPE_AUTH_RESPONSE:
+ case DNSTAP_MESSAGE_TYPE_RESOLVER_QUERY:
+ case DNSTAP_MESSAGE_TYPE_RESOLVER_RESPONSE:
+ case DNSTAP_MESSAGE_TYPE_CLIENT_QUERY:
+ case DNSTAP_MESSAGE_TYPE_CLIENT_RESPONSE:
+ case DNSTAP_MESSAGE_TYPE_FORWARDER_QUERY:
+ case DNSTAP_MESSAGE_TYPE_FORWARDER_RESPONSE:
+ case DNSTAP_MESSAGE_TYPE_STUB_QUERY:
+ case DNSTAP_MESSAGE_TYPE_STUB_RESPONSE:
+ case DNSTAP_MESSAGE_TYPE_TOOL_QUERY:
+ case DNSTAP_MESSAGE_TYPE_TOOL_RESPONSE:
+ break;
+ default:
+ dnstap->message.type = (enum _Dnstap__Message__Type)DNSTAP_MESSAGE_TYPE_UNKNOWN;
+ }
+
+ switch (dnstap->message.socket_family) {
+ case DNSTAP_SOCKET_FAMILY_INET:
+ case DNSTAP_SOCKET_FAMILY_INET6:
+ break;
+ default:
+ dnstap->message.has_socket_family = false;
+ dnstap->message.socket_family = (enum _Dnstap__SocketFamily)DNSTAP_SOCKET_FAMILY_UNKNOWN;
+ }
+
+ switch (dnstap->message.socket_protocol) {
+ case DNSTAP_SOCKET_PROTOCOL_UDP:
+ case DNSTAP_SOCKET_PROTOCOL_TCP:
+ break;
+ default:
+ dnstap->message.has_socket_protocol = false;
+ dnstap->message.socket_protocol = (enum _Dnstap__SocketProtocol)DNSTAP_SOCKET_PROTOCOL_UNKNOWN;
+ }
+ }
+
+ return 0;
+}
+
+void dnstap_cleanup(struct dnstap* dnstap)
+{
+ assert(dnstap);
+
+ if (dnstap->unpacked_dnstap) {
+ dnstap__dnstap__free_unpacked(dnstap->unpacked_dnstap, NULL);
+ dnstap->unpacked_dnstap = 0;
+ }
+}
+
+size_t dnstap_encode_protobuf_size(const struct dnstap* dnstap)
+{
+ assert(dnstap);
+
+ return dnstap__dnstap__get_packed_size(&dnstap->dnstap);
+}
+
+size_t dnstap_encode_protobuf(const struct dnstap* dnstap, uint8_t* data)
+{
+ assert(dnstap);
+ assert(data);
+
+ return dnstap__dnstap__pack(&dnstap->dnstap, data);
+}
diff --git a/src/dnstap.fields b/src/dnstap.fields
new file mode 100644
index 0000000..e38b6e5
--- /dev/null
+++ b/src/dnstap.fields
@@ -0,0 +1,16 @@
+dnstap dnstap identity string
+dnstap dnstap version string
+dnstap dnstap extra bytes
+dnstap_message message socket_family enum dnstap_socket_family
+dnstap_message message socket_protocol enum dnstap_socket_protocol
+dnstap_message message query_address bytes
+dnstap_message message response_address bytes
+dnstap_message message query_port value uint32_t
+dnstap_message message response_port value uint32_t
+dnstap_message message query_time_sec value uint64_t
+dnstap_message message query_time_nsec value uint32_t
+dnstap_message message query_message bytes
+dnstap_message message query_zone bytes
+dnstap_message message response_time_sec value uint64_t
+dnstap_message message response_time_nsec value uint32_t
+dnstap_message message response_message bytes
diff --git a/src/dnswire.c b/src/dnswire.c
new file mode 100644
index 0000000..5468434
--- /dev/null
+++ b/src/dnswire.c
@@ -0,0 +1,34 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/dnswire.h"
+
+const char* const dnswire_result_string[] = {
+ "ok",
+ "error",
+ "again",
+ "need_more",
+ "have_dnstap",
+ "endofdata",
+ "bidirectional",
+};
diff --git a/src/dnswire/decoder.h b/src/dnswire/decoder.h
new file mode 100644
index 0000000..60ab44e
--- /dev/null
+++ b/src/dnswire/decoder.h
@@ -0,0 +1,64 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dnswire/dnswire.h>
+#include <dnswire/dnstap.h>
+
+#include <tinyframe/tinyframe.h>
+
+#ifndef __dnswire_h_decoder
+#define __dnswire_h_decoder 1
+
+enum dnswire_decoder_state {
+ dnswire_decoder_reading_control = 0,
+ dnswire_decoder_checking_ready = 1,
+ dnswire_decoder_checking_accept = 2,
+ dnswire_decoder_reading_start = 3,
+ dnswire_decoder_checking_start = 4,
+ dnswire_decoder_reading_frames = 5,
+ dnswire_decoder_checking_finish = 6,
+ dnswire_decoder_done = 7,
+};
+extern const char* const dnswire_decoder_state_string[];
+
+struct dnswire_decoder {
+ enum dnswire_decoder_state state;
+ struct tinyframe_reader reader;
+ struct dnstap dnstap;
+
+ unsigned ready_support_dnstap_protobuf : 1;
+ unsigned accept_support_dnstap_protobuf : 1;
+};
+
+#define DNSWIRE_DECODER_INITIALIZER \
+ { \
+ .state = dnswire_decoder_reading_control, \
+ .reader = TINYFRAME_READER_INITIALIZER, \
+ .dnstap = DNSTAP_INITIALIZER, \
+ }
+
+#define dnswire_decoder_decoded(d) (d).reader.bytes_read
+#define dnswire_decoder_dnstap(d) (&(d).dnstap)
+#define dnswire_decoder_cleanup(d) dnstap_cleanup(&(d).dnstap)
+
+enum dnswire_result dnswire_decoder_decode(struct dnswire_decoder*, const uint8_t*, size_t);
+
+#endif
diff --git a/src/dnswire/dnstap.h b/src/dnswire/dnstap.h
new file mode 100644
index 0000000..97da495
--- /dev/null
+++ b/src/dnswire/dnstap.h
@@ -0,0 +1,151 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dnswire/dnstap.pb-c.h>
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <string.h>
+
+#ifndef __dnswire_h_dnstap
+#define __dnswire_h_dnstap 1
+
+#define DNSTAP_PROTOBUF_CONTENT_TYPE "protobuf:dnstap.Dnstap"
+#define DNSTAP_PROTOBUF_CONTENT_TYPE_LENGTH 22
+
+enum dnstap_type {
+ DNSTAP_TYPE_UNKNOWN = 0,
+ DNSTAP_TYPE_MESSAGE = 1,
+};
+extern const char* const DNSTAP_TYPE_STRING[];
+
+enum dnstap_message_type {
+ DNSTAP_MESSAGE_TYPE_UNKNOWN = 0,
+ DNSTAP_MESSAGE_TYPE_AUTH_QUERY = 1,
+ DNSTAP_MESSAGE_TYPE_AUTH_RESPONSE = 2,
+ DNSTAP_MESSAGE_TYPE_RESOLVER_QUERY = 3,
+ DNSTAP_MESSAGE_TYPE_RESOLVER_RESPONSE = 4,
+ DNSTAP_MESSAGE_TYPE_CLIENT_QUERY = 5,
+ DNSTAP_MESSAGE_TYPE_CLIENT_RESPONSE = 6,
+ DNSTAP_MESSAGE_TYPE_FORWARDER_QUERY = 7,
+ DNSTAP_MESSAGE_TYPE_FORWARDER_RESPONSE = 8,
+ DNSTAP_MESSAGE_TYPE_STUB_QUERY = 9,
+ DNSTAP_MESSAGE_TYPE_STUB_RESPONSE = 10,
+ DNSTAP_MESSAGE_TYPE_TOOL_QUERY = 11,
+ DNSTAP_MESSAGE_TYPE_TOOL_RESPONSE = 12,
+};
+extern const char* const DNSTAP_MESSAGE_TYPE_STRING[];
+
+enum dnstap_socket_family {
+ DNSTAP_SOCKET_FAMILY_UNKNOWN = 0,
+ DNSTAP_SOCKET_FAMILY_INET = 1,
+ DNSTAP_SOCKET_FAMILY_INET6 = 2,
+};
+extern const char* const DNSTAP_SOCKET_FAMILY_STRING[];
+
+enum dnstap_socket_protocol {
+ DNSTAP_SOCKET_PROTOCOL_UNKNOWN = 0,
+ DNSTAP_SOCKET_PROTOCOL_UDP = 1,
+ DNSTAP_SOCKET_PROTOCOL_TCP = 2,
+};
+extern const char* const DNSTAP_SOCKET_PROTOCOL_STRING[];
+
+struct dnstap {
+ Dnstap__Dnstap dnstap;
+ Dnstap__Message message;
+
+ Dnstap__Dnstap* unpacked_dnstap;
+};
+
+#define DNSTAP_INITIALIZER \
+ { \
+ .dnstap = DNSTAP__DNSTAP__INIT, \
+ .message = DNSTAP__MESSAGE__INIT, \
+ .unpacked_dnstap = 0, \
+ }
+
+#include <dnswire/dnstap-macros.h>
+#define dnstap_type(d) (enum dnstap_type)((d).dnstap.type)
+#define dnstap_set_type(d, v) \
+ switch (v) { \
+ case DNSTAP_TYPE_MESSAGE: \
+ (d).dnstap.type = (enum _Dnstap__Dnstap__Type)v; \
+ (d).dnstap.message = &(d).message; \
+ break; \
+ default: \
+ (d).dnstap.type = (enum _Dnstap__Dnstap__Type)DNSTAP_TYPE_UNKNOWN; \
+ (d).dnstap.message = 0; \
+ }
+#define dnstap_has_message(d) ((d).dnstap.message != 0)
+
+#define dnstap_message_type(d) (enum dnstap_message_type)((d).message.type)
+#define dnstap_message_set_type(d, v) \
+ switch (v) { \
+ case DNSTAP_MESSAGE_TYPE_AUTH_QUERY: \
+ case DNSTAP_MESSAGE_TYPE_AUTH_RESPONSE: \
+ case DNSTAP_MESSAGE_TYPE_RESOLVER_QUERY: \
+ case DNSTAP_MESSAGE_TYPE_RESOLVER_RESPONSE: \
+ case DNSTAP_MESSAGE_TYPE_CLIENT_QUERY: \
+ case DNSTAP_MESSAGE_TYPE_CLIENT_RESPONSE: \
+ case DNSTAP_MESSAGE_TYPE_FORWARDER_QUERY: \
+ case DNSTAP_MESSAGE_TYPE_FORWARDER_RESPONSE: \
+ case DNSTAP_MESSAGE_TYPE_STUB_QUERY: \
+ case DNSTAP_MESSAGE_TYPE_STUB_RESPONSE: \
+ case DNSTAP_MESSAGE_TYPE_TOOL_QUERY: \
+ case DNSTAP_MESSAGE_TYPE_TOOL_RESPONSE: \
+ (d).message.type = (enum _Dnstap__Message__Type)v; \
+ break; \
+ default: \
+ (d).message.type = (enum _Dnstap__Message__Type)DNSTAP_MESSAGE_TYPE_UNKNOWN; \
+ }
+#define dnstap_message_set_socket_family(d, v) \
+ switch (v) { \
+ case DNSTAP_SOCKET_FAMILY_INET: \
+ case DNSTAP_SOCKET_FAMILY_INET6: \
+ (d).message.has_socket_family = true; \
+ (d).message.socket_family = (enum _Dnstap__SocketFamily)v; \
+ break; \
+ default: \
+ (d).message.has_socket_family = false; \
+ (d).message.socket_family = (enum _Dnstap__SocketFamily)DNSTAP_MESSAGE_TYPE_UNKNOWN; \
+ }
+#define dnstap_message_set_socket_protocol(d, v) \
+ switch (v) { \
+ case DNSTAP_SOCKET_PROTOCOL_UDP: \
+ case DNSTAP_SOCKET_PROTOCOL_TCP: \
+ (d).message.has_socket_protocol = true; \
+ (d).message.socket_protocol = (enum _Dnstap__SocketProtocol)v; \
+ break; \
+ default: \
+ (d).message.has_socket_protocol = false; \
+ (d).message.socket_protocol = (enum _Dnstap__SocketProtocol)DNSTAP_MESSAGE_TYPE_UNKNOWN; \
+ }
+
+int dnstap_decode_protobuf(struct dnstap*, const uint8_t*, size_t);
+// int dnstap_decode_cbor(struct dnstap*, const uint8_t*, size_t);
+
+size_t dnstap_encode_protobuf_size(const struct dnstap*);
+size_t dnstap_encode_protobuf(const struct dnstap*, uint8_t*);
+
+void dnstap_cleanup(struct dnstap*);
+
+#endif
diff --git a/src/dnswire/dnswire.h b/src/dnswire/dnswire.h
new file mode 100644
index 0000000..f113b22
--- /dev/null
+++ b/src/dnswire/dnswire.h
@@ -0,0 +1,39 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __dnswire_h_dnswire
+#define __dnswire_h_dnswire 1
+
+#define DNSWIRE_DEFAULT_BUF_SIZE (4 * 1024)
+#define DNSWIRE_MAXIMUM_BUF_SIZE (64 * 1024)
+
+enum dnswire_result {
+ dnswire_ok = 0,
+ dnswire_error = 1,
+ dnswire_again = 2,
+ dnswire_need_more = 3,
+ dnswire_have_dnstap = 4,
+ dnswire_endofdata = 5,
+ dnswire_bidirectional = 6,
+};
+extern const char* const dnswire_result_string[];
+
+#endif
diff --git a/src/dnswire/encoder.h b/src/dnswire/encoder.h
new file mode 100644
index 0000000..c5fbc6a
--- /dev/null
+++ b/src/dnswire/encoder.h
@@ -0,0 +1,60 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dnswire/dnswire.h>
+#include <dnswire/dnstap.h>
+
+#include <tinyframe/tinyframe.h>
+
+#ifndef __dnswire_h_encoder
+#define __dnswire_h_encoder 1
+
+enum dnswire_encoder_state {
+ dnswire_encoder_control_ready = 0,
+ dnswire_encoder_control_start = 1,
+ dnswire_encoder_control_accept = 2,
+ dnswire_encoder_control_finish = 3,
+ dnswire_encoder_frames = 4,
+ dnswire_encoder_control_stop = 5,
+ dnswire_encoder_done = 6,
+};
+extern const char* const dnswire_encoder_state_string[];
+
+struct dnswire_encoder {
+ enum dnswire_encoder_state state;
+ struct tinyframe_writer writer;
+ const struct dnstap* dnstap;
+};
+
+#define DNSWIRE_ENCODER_INITIALIZER \
+ { \
+ .state = dnswire_encoder_control_start, \
+ .writer = TINYFRAME_WRITER_INITIALIZER, \
+ .dnstap = 0, \
+ }
+
+#define dnswire_encoder_set_dnstap(e, d) (e).dnstap = d
+#define dnswire_encoder_encoded(e) (e).writer.bytes_wrote
+
+enum dnswire_result dnswire_encoder_encode(struct dnswire_encoder*, uint8_t*, size_t);
+enum dnswire_result dnswire_encoder_stop(struct dnswire_encoder*);
+
+#endif
diff --git a/src/dnswire/reader.h b/src/dnswire/reader.h
new file mode 100644
index 0000000..5fdfad5
--- /dev/null
+++ b/src/dnswire/reader.h
@@ -0,0 +1,91 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dnswire/dnswire.h>
+#include <dnswire/decoder.h>
+#include <dnswire/encoder.h>
+
+#include <stdlib.h>
+
+#ifndef __dnswire_h_reader
+#define __dnswire_h_reader 1
+
+enum dnswire_reader_state {
+ dnswire_reader_reading_control = 0,
+ dnswire_reader_decoding_control = 1,
+ dnswire_reader_encoding_accept = 2,
+ dnswire_reader_writing_accept = 3,
+ dnswire_reader_reading = 4,
+ dnswire_reader_decoding = 5,
+ dnswire_reader_encoding_finish = 6,
+ dnswire_reader_writing_finish = 7,
+ dnswire_reader_done = 8,
+};
+extern const char* const dnswire_reader_state_string[];
+
+/*
+ * Attributes:
+ * - size: The current size of the buffer
+ * - inc: How much the buffer will be increased by if more spare is needed
+ * - max: The maximum size the buffer is allowed to have
+ * - at: Where in the buffer we are decoding (start of data)
+ * - left: How much data that is still left in the buffer from `at`
+ * - pushed: How much data that was pushed to the buffer by `dnswire_reader_push()`
+ */
+struct dnswire_reader {
+ enum dnswire_reader_state state;
+
+ struct dnswire_decoder decoder;
+ uint8_t* buf;
+ size_t size, inc, max, at, left, pushed;
+
+ struct dnswire_encoder encoder;
+ uint8_t* write_buf;
+ size_t write_size, write_inc, write_max, write_at, write_left;
+
+ bool allow_bidirectional, is_bidirectional;
+};
+
+enum dnswire_result dnswire_reader_init(struct dnswire_reader*);
+
+#define dnswire_reader_pushed(r) (r).pushed
+#define dnswire_reader_dnstap(r) (&(r).decoder.dnstap)
+#define dnswire_reader_cleanup(r) \
+ dnswire_decoder_cleanup((r).decoder)
+#define dnswire_reader_destroy(r) \
+ free((r).buf); \
+ free((r).write_buf); \
+ dnswire_decoder_cleanup((r).decoder)
+#define dnswire_reader_is_bidirectional(r) (r).is_bidirectional
+
+enum dnswire_result dnswire_reader_allow_bidirectional(struct dnswire_reader*, bool);
+enum dnswire_result dnswire_reader_set_bufsize(struct dnswire_reader*, size_t);
+enum dnswire_result dnswire_reader_set_bufinc(struct dnswire_reader*, size_t);
+enum dnswire_result dnswire_reader_set_bufmax(struct dnswire_reader*, size_t);
+
+enum dnswire_result dnswire_reader_push(struct dnswire_reader*, const uint8_t*, size_t, uint8_t*, size_t*);
+enum dnswire_result dnswire_reader_read(struct dnswire_reader*, int);
+static inline enum dnswire_result dnswire_reader_fread(struct dnswire_reader* handle, FILE* fp)
+{
+ return dnswire_reader_read(handle, fileno(fp));
+}
+
+#endif
diff --git a/src/dnswire/trace.h.in b/src/dnswire/trace.h.in
new file mode 100644
index 0000000..7b8e038
--- /dev/null
+++ b/src/dnswire/trace.h.in
@@ -0,0 +1,41 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __dnswire_h_trace
+#define __dnswire_h_trace 1
+
+#if @DNSWIRE_TRACE@
+#define DNSWIRE_TRACE 1
+#include <stdio.h>
+#include <ctype.h>
+#include <stdint.h>
+const char* __printable_string(const uint8_t* data, size_t len);
+#define __trace(x...) \
+ fprintf(stderr, "dnswire %s(): ", __func__); \
+ fprintf(stderr, x); \
+ fprintf(stderr, "\n"); \
+ fflush(stderr);
+#else
+#define __trace(x...)
+#define __printable_string(x...)
+#endif
+
+#endif
diff --git a/src/dnswire/version.h.in b/src/dnswire/version.h.in
new file mode 100644
index 0000000..1a4f96f
--- /dev/null
+++ b/src/dnswire/version.h.in
@@ -0,0 +1,33 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dnswire/dnswire.h>
+
+#ifndef __dnswire_h_version
+#define __dnswire_h_version 1
+
+#define DNSWIRE_VERSION @DNSWIRE_VERSION_MAJOR@@DNSWIRE_VERSION_MINOR@@DNSWIRE_VERSION_PATCH@
+#define DNSWIRE_VERSION_MAJOR @DNSWIRE_VERSION_MAJOR@
+#define DNSWIRE_VERSION_MINOR @DNSWIRE_VERSION_MINOR@
+#define DNSWIRE_VERSION_PATCH @DNSWIRE_VERSION_PATCH@
+#define DNSWIRE_VERSION_STRING "@PACKAGE_VERSION@"
+
+#endif
diff --git a/src/dnswire/writer.h b/src/dnswire/writer.h
new file mode 100644
index 0000000..99bae1a
--- /dev/null
+++ b/src/dnswire/writer.h
@@ -0,0 +1,90 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <dnswire/dnswire.h>
+#include <dnswire/encoder.h>
+#include <dnswire/decoder.h>
+
+#include <stdlib.h>
+
+#ifndef __dnswire_h_writer
+#define __dnswire_h_writer 1
+
+enum dnswire_writer_state {
+ dnswire_writer_encoding_ready = 0,
+ dnswire_writer_writing_ready = 1,
+ dnswire_writer_reading_accept = 2,
+ dnswire_writer_decoding_accept = 3,
+ dnswire_writer_encoding = 4,
+ dnswire_writer_writing = 5,
+ dnswire_writer_stopping = 6,
+ dnswire_writer_encoding_stop = 7,
+ dnswire_writer_writing_stop = 8,
+ dnswire_writer_reading_finish = 9,
+ dnswire_writer_decoding_finish = 10,
+ dnswire_writer_done = 11,
+};
+extern const char* const dnswire_writer_state_string[];
+
+/*
+ * Attributes:
+ * - size: The current size of the buffer
+ * - inc: How much the buffer will be increased by if more spare is needed
+ * - max: The maximum size the buffer is allowed to have
+ * - at: Where in the buffer we are encoding to (end of data)
+ * - left: How much data that is still left in the buffer before `at`
+ */
+struct dnswire_writer {
+ enum dnswire_writer_state state;
+
+ struct dnswire_encoder encoder;
+ uint8_t* buf;
+ size_t size, inc, max, at, left, popped;
+
+ struct dnswire_decoder decoder;
+ uint8_t* read_buf;
+ size_t read_size, read_inc, read_max, read_at, read_left, read_pushed;
+
+ bool bidirectional;
+};
+
+enum dnswire_result dnswire_writer_init(struct dnswire_writer*);
+
+#define dnswire_writer_popped(w) (w).popped
+#define dnswire_writer_set_dnstap(w, d) (w).encoder.dnstap = d
+#define dnswire_writer_destroy(w) \
+ free((w).buf); \
+ free((w).read_buf)
+
+enum dnswire_result dnswire_writer_set_bidirectional(struct dnswire_writer*, bool);
+enum dnswire_result dnswire_writer_set_bufsize(struct dnswire_writer*, size_t);
+enum dnswire_result dnswire_writer_set_bufinc(struct dnswire_writer*, size_t);
+enum dnswire_result dnswire_writer_set_bufmax(struct dnswire_writer*, size_t);
+
+enum dnswire_result dnswire_writer_pop(struct dnswire_writer*, uint8_t*, size_t, uint8_t*, size_t*);
+enum dnswire_result dnswire_writer_write(struct dnswire_writer*, int);
+static inline enum dnswire_result dnswire_writer_fwrite(struct dnswire_writer* handle, FILE* fp)
+{
+ return dnswire_writer_write(handle, fileno(fp));
+}
+enum dnswire_result dnswire_writer_stop(struct dnswire_writer*);
+
+#endif
diff --git a/src/encoder.c b/src/encoder.c
new file mode 100644
index 0000000..4b7ad27
--- /dev/null
+++ b/src/encoder.c
@@ -0,0 +1,172 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/encoder.h"
+#include "dnswire/trace.h"
+
+#include <assert.h>
+
+const char* const dnswire_encoder_state_string[] = {
+ "control_ready",
+ "control_start",
+ "control_accept",
+ "control_finish",
+ "frames",
+ "control_stop",
+ "done",
+};
+
+#define __state(h, s) \
+ __trace("state %s => %s", dnswire_encoder_state_string[(h)->state], dnswire_encoder_state_string[s]); \
+ (h)->state = s;
+
+static struct tinyframe_control_field _content_type = {
+ TINYFRAME_CONTROL_FIELD_CONTENT_TYPE,
+ DNSTAP_PROTOBUF_CONTENT_TYPE_LENGTH,
+ (uint8_t*)DNSTAP_PROTOBUF_CONTENT_TYPE,
+};
+
+enum dnswire_result dnswire_encoder_encode(struct dnswire_encoder* handle, uint8_t* out, size_t len)
+{
+ assert(handle);
+ assert(out);
+ assert(len);
+
+ switch (handle->state) {
+ case dnswire_encoder_control_ready:
+ switch (tinyframe_write_control(&handle->writer, out, len, TINYFRAME_CONTROL_READY, &_content_type, 1)) {
+ case tinyframe_ok:
+ __state(handle, dnswire_encoder_control_start);
+ return dnswire_again;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_encoder_control_start:
+ switch (tinyframe_write_control_start(&handle->writer, out, len, DNSTAP_PROTOBUF_CONTENT_TYPE, DNSTAP_PROTOBUF_CONTENT_TYPE_LENGTH)) {
+ case tinyframe_ok:
+ __state(handle, dnswire_encoder_frames);
+ return dnswire_again;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_encoder_control_accept:
+ switch (tinyframe_write_control(&handle->writer, out, len, TINYFRAME_CONTROL_ACCEPT, &_content_type, 1)) {
+ case tinyframe_ok:
+ __state(handle, dnswire_encoder_control_finish);
+ return dnswire_again;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_encoder_control_finish:
+ switch (tinyframe_write_control(&handle->writer, out, len, TINYFRAME_CONTROL_FINISH, 0, 0)) {
+ case tinyframe_ok:
+ __state(handle, dnswire_encoder_done);
+ return dnswire_endofdata;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_encoder_frames: {
+ if (!handle->dnstap) {
+ return dnswire_error;
+ }
+
+ size_t frame_len = dnstap_encode_protobuf_size(handle->dnstap);
+ if (len < tinyframe_frame_size(frame_len)) {
+ return dnswire_need_more;
+ }
+ uint8_t frame[frame_len];
+ dnstap_encode_protobuf(handle->dnstap, frame);
+
+ switch (tinyframe_write_frame(&handle->writer, out, len, frame, sizeof(frame))) {
+ case tinyframe_ok:
+ return dnswire_ok;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+ }
+
+ case dnswire_encoder_control_stop:
+ switch (tinyframe_write_control_stop(&handle->writer, out, len)) {
+ case tinyframe_ok:
+ __state(handle, dnswire_encoder_done);
+ return dnswire_endofdata;
+
+ case tinyframe_need_more:
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_encoder_done:
+ break;
+ }
+
+ return dnswire_error;
+}
+
+enum dnswire_result dnswire_encoder_stop(struct dnswire_encoder* handle)
+{
+ assert(handle);
+
+ switch (handle->state) {
+ case dnswire_encoder_frames:
+ __state(handle, dnswire_encoder_control_stop);
+ return dnswire_ok;
+
+ default:
+ break;
+ }
+
+ return dnswire_error;
+}
diff --git a/src/gen-macros.sh b/src/gen-macros.sh
new file mode 100755
index 0000000..190d74a
--- /dev/null
+++ b/src/gen-macros.sh
@@ -0,0 +1,34 @@
+#!/bin/sh -e
+
+echo "/* autogenerated, don't edit */"
+
+while read prefix base name type typedef; do
+ echo "// $base.$name ($type)"
+ case "$type" in
+ string )
+ echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
+#define ${prefix}_${name}(d) (const uint8_t*)((d).${base}.${name}.data)
+#define ${prefix}_${name}_length(d) (size_t)((d).${base}.${name}.len)
+#define ${prefix}_set_${name}(d, v, l) (d).${base}.has_${name} = true; (d).${base}.${name}.data = v; (d).${base}.${name}.len = l
+#define ${prefix}_set_${name}_string(d, v) (d).${base}.has_${name} = true; (d).${base}.${name}.data = (uint8_t*)v; (d).${base}.${name}.len = strlen(v)"
+ ;;
+ bytes )
+ echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
+#define ${prefix}_${name}(d) (const uint8_t*)((d).${base}.${name}.data)
+#define ${prefix}_${name}_length(d) (size_t)((d).${base}.${name}.len)
+#define ${prefix}_set_${name}(d, v, l) (d).${base}.has_${name} = true; (d).${base}.${name}.data = (uint8_t*)v; (d).${base}.${name}.len = l"
+ ;;
+ enum )
+ echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
+#define ${prefix}_${name}(d) (enum ${typedef})((d).${base}.${name})"
+ ;;
+ value )
+ echo "#define ${prefix}_has_${name}(d) (bool)((d).${base}.has_${name})
+#define ${prefix}_${name}(d) (${typedef})((d).${base}.${name})
+#define ${prefix}_set_${name}(d, v) (d).${base}.has_${name} = true; (d).${base}.${name} = v"
+ ;;
+ * )
+ echo "#error \"invalid type $type for $base.$name\""
+ ;;
+ esac
+done < "$1"
diff --git a/src/reader.c b/src/reader.c
new file mode 100644
index 0000000..ba04284
--- /dev/null
+++ b/src/reader.c
@@ -0,0 +1,700 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/reader.h"
+#include "dnswire/trace.h"
+
+#include <assert.h>
+#include <stdlib.h>
+
+const char* const dnswire_reader_state_string[] = {
+ "reading_control",
+ "decoding_control",
+ "encoding_accept",
+ "writing_accept",
+ "reading",
+ "decoding",
+ "encoding_finish",
+ "writing_finish",
+ "done",
+};
+
+#define __state(h, s) \
+ __trace("state %s => %s", dnswire_reader_state_string[(h)->state], dnswire_reader_state_string[s]); \
+ (h)->state = s;
+
+static struct dnswire_reader _defaults = {
+ .state = dnswire_reader_reading_control,
+
+ .decoder = DNSWIRE_DECODER_INITIALIZER,
+ .buf = 0,
+ .size = DNSWIRE_DEFAULT_BUF_SIZE,
+ .inc = DNSWIRE_DEFAULT_BUF_SIZE,
+ .max = DNSWIRE_MAXIMUM_BUF_SIZE,
+ .at = 0,
+ .left = 0,
+ .pushed = 0,
+
+ .encoder = DNSWIRE_ENCODER_INITIALIZER,
+ .write_buf = 0,
+ .write_size = DNSWIRE_DEFAULT_BUF_SIZE,
+ .write_inc = DNSWIRE_DEFAULT_BUF_SIZE,
+ .write_max = DNSWIRE_MAXIMUM_BUF_SIZE,
+ .write_at = 0,
+ .write_left = 0,
+
+ .allow_bidirectional = false,
+ .is_bidirectional = false,
+};
+
+enum dnswire_result dnswire_reader_init(struct dnswire_reader* handle)
+{
+ assert(handle);
+
+ *handle = _defaults;
+
+ if (!(handle->buf = malloc(handle->size))) {
+ return dnswire_error;
+ }
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_reader_allow_bidirectional(struct dnswire_reader* handle, bool allow_bidirectional)
+{
+ assert(handle);
+
+ if (allow_bidirectional) {
+ if (!handle->write_buf) {
+ if (!(handle->write_buf = malloc(handle->write_size))) {
+ return dnswire_error;
+ }
+ }
+
+ handle->encoder.state = dnswire_encoder_control_accept;
+ } else {
+ handle->encoder.state = dnswire_encoder_control_start;
+ }
+
+ handle->allow_bidirectional = allow_bidirectional;
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_reader_set_bufsize(struct dnswire_reader* handle, size_t size)
+{
+ assert(handle);
+ assert(size);
+ assert(handle->buf);
+
+ if (handle->left > size) {
+ // we got data and it doesn't fit in the new size
+ return dnswire_error;
+ }
+ if (size > handle->max) {
+ // don't expand over max
+ return dnswire_error;
+ }
+
+ if (handle->at + handle->left > size) {
+ // move what's left to the start
+ if (handle->left) {
+ memmove(handle->buf, &handle->buf[handle->at], handle->left);
+ }
+ handle->at = 0;
+ }
+
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+
+ handle->buf = buf;
+ handle->size = size;
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_reader_set_bufinc(struct dnswire_reader* handle, size_t inc)
+{
+ assert(handle);
+ assert(inc);
+
+ handle->inc = inc;
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_reader_set_bufmax(struct dnswire_reader* handle, size_t max)
+{
+ assert(handle);
+ assert(max);
+
+ if (max < handle->size) {
+ return dnswire_error;
+ }
+
+ handle->max = max;
+
+ return dnswire_ok;
+}
+
+static enum dnswire_result _encoding(struct dnswire_reader* handle)
+{
+ enum dnswire_result res;
+
+ while (1) {
+ res = dnswire_encoder_encode(&handle->encoder, &handle->write_buf[handle->write_at], handle->write_size - handle->write_at);
+ __trace("encode %s", dnswire_result_string[res]);
+
+ switch (res) {
+ case dnswire_ok:
+ case dnswire_again:
+ case dnswire_endofdata:
+ handle->write_at += dnswire_encoder_encoded(handle->encoder);
+ handle->write_left += dnswire_encoder_encoded(handle->encoder);
+ break;
+
+ case dnswire_need_more: {
+ if (handle->write_size >= handle->write_max) {
+ // already at max size and it's not enough
+ return dnswire_error;
+ }
+
+ // no space left, expand
+ size_t size = handle->write_size + handle->write_inc > handle->write_max ? handle->write_max : handle->write_size + handle->write_inc;
+ uint8_t* buf = realloc(handle->write_buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->write_buf = buf;
+ handle->write_size = size;
+ continue;
+ }
+ default:
+ break;
+ }
+ break;
+ }
+ return res;
+}
+
+enum dnswire_result dnswire_reader_push(struct dnswire_reader* handle, const uint8_t* data, size_t len, uint8_t* out_data, size_t* out_len)
+{
+ assert(handle);
+ assert(data);
+ assert(handle->buf);
+ assert(!handle->allow_bidirectional || out_data);
+ assert(!handle->allow_bidirectional || out_len);
+
+ handle->pushed = 0;
+ size_t out_len_orig = 0;
+ if (out_len) {
+ out_len_orig = *out_len;
+ *out_len = 0;
+ }
+
+ switch (handle->state) {
+ case dnswire_reader_reading_control:
+ if (!len) {
+ return dnswire_need_more;
+ }
+ // if the space we have left is less then len we only move that amount
+ handle->pushed = handle->size - handle->at - handle->left < len ? handle->size - handle->at - handle->left : len;
+ // if we can't add more we fallthrough and let it decode some or expand the buffer
+ if (handle->pushed) {
+ memcpy(&handle->buf[handle->at + handle->left], data, handle->pushed);
+ __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], handle->pushed));
+ handle->left += handle->pushed;
+ }
+ __state(handle, dnswire_reader_decoding_control);
+ // fallthrough
+
+ case dnswire_reader_decoding_control:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
+ case dnswire_bidirectional:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading_control);
+ }
+
+ if (!handle->allow_bidirectional) {
+ return dnswire_error;
+ }
+
+ handle->is_bidirectional = true;
+
+ if (!handle->decoder.ready_support_dnstap_protobuf) {
+ return dnswire_error;
+ }
+
+ __state(handle, dnswire_reader_encoding_accept);
+ return dnswire_again;
+
+ case dnswire_again:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading_control);
+ }
+ return dnswire_again;
+
+ case dnswire_need_more:
+ if (handle->left < handle->size) {
+ // still space left to fill
+ if (handle->at) {
+ // move what's left to the start
+ if (handle->left) {
+ memmove(handle->buf, &handle->buf[handle->at], handle->left);
+ }
+ handle->at = 0;
+ }
+ } else if (handle->size < handle->max) {
+ // no space left, expand
+ size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->buf = buf;
+ handle->size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_reader_reading_control);
+ if (len && handle->pushed < len) {
+ // we had more to push but won't loop, call again please
+ return dnswire_again;
+ }
+ return dnswire_need_more;
+
+ case dnswire_have_dnstap:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (handle->left) {
+ __state(handle, dnswire_reader_decoding);
+ } else {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading);
+ }
+ return dnswire_have_dnstap;
+
+ case dnswire_endofdata:
+ if (handle->is_bidirectional) {
+ __state(handle, dnswire_reader_encoding_finish);
+ return dnswire_again;
+ }
+ __state(handle, dnswire_reader_done);
+ return dnswire_endofdata;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_reader_encoding_accept:
+ switch (_encoding(handle)) {
+ case dnswire_again:
+ __state(handle, dnswire_reader_writing_accept);
+ break;
+ // fallthrough
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_reader_writing_accept:
+ // if what we have left to write is less then the space available, we write it all
+ assert(out_len);
+ *out_len = handle->write_left < out_len_orig ? handle->write_left : out_len_orig;
+ memcpy(out_data, &handle->write_buf[handle->write_at - handle->write_left], *out_len);
+ __trace("pushed %zu", *out_len);
+ handle->write_left -= *out_len;
+ __trace("left %zu", handle->write_left);
+ if (!handle->write_left) {
+ handle->write_at = 0;
+ __state(handle, dnswire_reader_reading_control);
+ }
+ return dnswire_again;
+
+ case dnswire_reader_reading:
+ if (!len) {
+ return dnswire_need_more;
+ }
+ // if the space we have left is less then len we only move that amount
+ handle->pushed = handle->size - handle->at - handle->left < len ? handle->size - handle->at - handle->left : len;
+ // if we can't add more we fallthrough and let it decode some or expand the buffer
+ if (handle->pushed) {
+ memcpy(&handle->buf[handle->at + handle->left], data, handle->pushed);
+ __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], handle->pushed));
+ handle->left += handle->pushed;
+ }
+ __state(handle, dnswire_reader_decoding);
+ // fallthrough
+
+ case dnswire_reader_decoding:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
+ case dnswire_again:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading);
+ }
+ return dnswire_again;
+
+ case dnswire_need_more:
+ if (handle->left < handle->size) {
+ // still space left to fill
+ if (handle->at) {
+ // move what's left to the start
+ if (handle->left) {
+ memmove(handle->buf, &handle->buf[handle->at], handle->left);
+ }
+ handle->at = 0;
+ }
+ } else if (handle->size < handle->max) {
+ // no space left, expand
+ size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->buf = buf;
+ handle->size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_reader_reading);
+ if (len && handle->pushed < len) {
+ // we had more to push but won't loop, call again please
+ return dnswire_again;
+ }
+ return dnswire_need_more;
+
+ case dnswire_have_dnstap:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading);
+ }
+ return dnswire_have_dnstap;
+
+ case dnswire_endofdata:
+ if (handle->is_bidirectional) {
+ __state(handle, dnswire_reader_encoding_finish);
+ return dnswire_again;
+ }
+ __state(handle, dnswire_reader_done);
+ return dnswire_endofdata;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_reader_encoding_finish:
+ switch (_encoding(handle)) {
+ case dnswire_endofdata:
+ __state(handle, dnswire_reader_writing_finish);
+ break;
+ // fallthrough
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_reader_writing_finish:
+ // if what we have left to write is less then the space available, we write it all
+ assert(out_len);
+ *out_len = handle->write_left < out_len_orig ? handle->write_left : out_len_orig;
+ memcpy(out_data, &handle->write_buf[handle->write_at - handle->write_left], *out_len);
+ __trace("pushed %zu", *out_len);
+ handle->write_left -= *out_len;
+ __trace("left %zu", handle->write_left);
+ if (!handle->write_left) {
+ handle->write_at = 0;
+ __state(handle, dnswire_reader_done);
+ return dnswire_endofdata;
+ }
+ return dnswire_again;
+
+ case dnswire_reader_done:
+ break;
+ }
+
+ return dnswire_error;
+}
+
+enum dnswire_result dnswire_reader_read(struct dnswire_reader* handle, int fd)
+{
+ assert(handle);
+ assert(handle->buf);
+
+ switch (handle->state) {
+ case dnswire_reader_reading_control: {
+ ssize_t nread = read(fd, &handle->buf[handle->at + handle->left], handle->size - handle->at - handle->left);
+ if (nread < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nread) {
+ // TODO
+ return dnswire_error;
+ }
+ __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], nread));
+ handle->left += nread;
+ __state(handle, dnswire_reader_decoding_control);
+ // fallthrough
+ }
+
+ case dnswire_reader_decoding_control:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
+ case dnswire_bidirectional:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading_control);
+ }
+
+ if (!handle->allow_bidirectional) {
+ return dnswire_error;
+ }
+
+ handle->is_bidirectional = true;
+
+ if (!handle->decoder.ready_support_dnstap_protobuf) {
+ return dnswire_error;
+ }
+
+ __state(handle, dnswire_reader_encoding_accept);
+ return dnswire_again;
+
+ case dnswire_again:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading_control);
+ }
+ return dnswire_again;
+
+ case dnswire_need_more:
+ if (handle->left < handle->size) {
+ // still space left to fill
+ if (handle->at) {
+ // move what's left to the start
+ if (handle->left) {
+ memmove(handle->buf, &handle->buf[handle->at], handle->left);
+ }
+ handle->at = 0;
+ }
+ } else if (handle->size < handle->max) {
+ // no space left, expand
+ size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->buf = buf;
+ handle->size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_reader_reading_control);
+ return dnswire_need_more;
+
+ case dnswire_have_dnstap:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (handle->left) {
+ __state(handle, dnswire_reader_decoding);
+ } else {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading);
+ }
+ return dnswire_have_dnstap;
+
+ case dnswire_endofdata:
+ if (handle->is_bidirectional) {
+ __state(handle, dnswire_reader_encoding_finish);
+ return dnswire_again;
+ }
+ __state(handle, dnswire_reader_done);
+ return dnswire_endofdata;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_reader_encoding_accept:
+ switch (_encoding(handle)) {
+ case dnswire_again:
+ __state(handle, dnswire_reader_writing_accept);
+ break;
+ // fallthrough
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_reader_writing_accept: {
+ ssize_t nwrote = write(fd, &handle->write_buf[handle->write_at - handle->write_left], handle->write_left);
+ __trace("wrote %zd", nwrote);
+ if (nwrote < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nwrote) {
+ // TODO
+ return dnswire_error;
+ }
+
+ handle->write_left -= nwrote;
+ __trace("left %zu", handle->write_left);
+ if (!handle->write_left) {
+ handle->write_at = 0;
+ __state(handle, dnswire_reader_reading_control);
+ }
+ return dnswire_again;
+ }
+
+ case dnswire_reader_reading: {
+ ssize_t nread = read(fd, &handle->buf[handle->at + handle->left], handle->size - handle->at - handle->left);
+ if (nread < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nread) {
+ // TODO
+ return dnswire_error;
+ }
+ __trace("%s", __printable_string(&handle->buf[handle->at + handle->left], nread));
+ handle->left += nread;
+ __state(handle, dnswire_reader_decoding);
+ // fallthrough
+ }
+
+ case dnswire_reader_decoding:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->buf[handle->at], handle->left)) {
+ case dnswire_again:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading);
+ }
+ return dnswire_again;
+
+ case dnswire_need_more:
+ if (handle->left < handle->size) {
+ // still space left to fill
+ if (handle->at) {
+ // move what's left to the start
+ if (handle->left) {
+ memmove(handle->buf, &handle->buf[handle->at], handle->left);
+ }
+ handle->at = 0;
+ }
+ } else if (handle->size < handle->max) {
+ // no space left, expand
+ size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->buf = buf;
+ handle->size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_reader_reading);
+ return dnswire_need_more;
+
+ case dnswire_have_dnstap:
+ handle->at += dnswire_decoder_decoded(handle->decoder);
+ handle->left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_reader_reading);
+ }
+ return dnswire_have_dnstap;
+
+ case dnswire_endofdata:
+ if (handle->is_bidirectional) {
+ __state(handle, dnswire_reader_encoding_finish);
+ return dnswire_again;
+ }
+ __state(handle, dnswire_reader_done);
+ return dnswire_endofdata;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_reader_encoding_finish:
+ switch (_encoding(handle)) {
+ case dnswire_endofdata:
+ __state(handle, dnswire_reader_writing_finish);
+ break;
+ // fallthrough
+
+ default:
+ return dnswire_error;
+ }
+
+ case dnswire_reader_writing_finish: {
+ ssize_t nwrote = write(fd, &handle->write_buf[handle->write_at - handle->write_left], handle->write_left);
+ __trace("wrote %zd", nwrote);
+ if (nwrote < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nwrote) {
+ // TODO
+ return dnswire_error;
+ }
+
+ handle->write_left -= nwrote;
+ __trace("left %zu", handle->write_left);
+ if (!handle->write_left) {
+ handle->write_at = 0;
+ __state(handle, dnswire_reader_done);
+ return dnswire_endofdata;
+ }
+ return dnswire_again;
+ }
+
+ case dnswire_reader_done:
+ break;
+ }
+
+ return dnswire_error;
+}
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
new file mode 100644
index 0000000..e5d8f66
--- /dev/null
+++ b/src/test/test.dnstap
Binary files differ
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;
+}
diff --git a/src/trace.c b/src/trace.c
new file mode 100644
index 0000000..742f819
--- /dev/null
+++ b/src/trace.c
@@ -0,0 +1,52 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/trace.h"
+
+#if DNSWIRE_TRACE
+const char* __printable_string(const uint8_t* data, size_t len)
+{
+ static char buf[512];
+ 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;
+ }
+
+ sprintf(&buf[w], "\\x%02x", data[r++]);
+ w += 4;
+ }
+ }
+ if (w >= sizeof(buf)) {
+ buf[sizeof(buf) - 1] = 0;
+ } else {
+ buf[w] = 0;
+ }
+
+ return buf;
+}
+#endif
diff --git a/src/writer.c b/src/writer.c
new file mode 100644
index 0000000..885ff91
--- /dev/null
+++ b/src/writer.c
@@ -0,0 +1,710 @@
+/*
+ * Author Jerry Lundström <jerry@dns-oarc.net>
+ * Copyright (c) 2019, OARC, Inc.
+ * All rights reserved.
+ *
+ * This file is part of the dnswire library.
+ *
+ * dnswire library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * dnswire library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with dnswire library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "dnswire/writer.h"
+#include "dnswire/trace.h"
+#include "dnswire/dnswire.h"
+
+#include <assert.h>
+#include <stdlib.h>
+
+const char* const dnswire_writer_state_string[] = {
+ "encoding_ready",
+ "writing_ready",
+ "reading_accept",
+ "decoding_accept",
+ "encoding",
+ "writing",
+ "stopping",
+ "encoding_stop",
+ "writing_stop",
+ "reading_finish",
+ "decoding_finish",
+ "done",
+};
+
+#define __state(h, s) \
+ __trace("state %s => %s", dnswire_writer_state_string[(h)->state], dnswire_writer_state_string[s]); \
+ (h)->state = s;
+
+static struct dnswire_writer _defaults = {
+ .state = dnswire_writer_encoding,
+
+ .encoder = DNSWIRE_ENCODER_INITIALIZER,
+ .buf = 0,
+ .size = DNSWIRE_DEFAULT_BUF_SIZE,
+ .inc = DNSWIRE_DEFAULT_BUF_SIZE,
+ .max = DNSWIRE_MAXIMUM_BUF_SIZE,
+ .at = 0,
+ .left = 0,
+ .popped = 0,
+
+ .decoder = DNSWIRE_DECODER_INITIALIZER,
+ .read_buf = 0,
+ .read_size = DNSWIRE_DEFAULT_BUF_SIZE,
+ .read_inc = DNSWIRE_DEFAULT_BUF_SIZE,
+ .read_max = DNSWIRE_MAXIMUM_BUF_SIZE,
+ .read_at = 0,
+ .read_left = 0,
+ .read_pushed = 0,
+
+ .bidirectional = false,
+};
+
+enum dnswire_result dnswire_writer_init(struct dnswire_writer* handle)
+{
+ assert(handle);
+
+ *handle = _defaults;
+
+ if (!(handle->buf = malloc(handle->size))) {
+ return dnswire_error;
+ }
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_writer_set_bidirectional(struct dnswire_writer* handle, bool bidirectional)
+{
+ assert(handle);
+
+ if (bidirectional) {
+ if (!handle->read_buf) {
+ if (!(handle->read_buf = malloc(handle->read_size))) {
+ return dnswire_error;
+ }
+ }
+
+ handle->encoder.state = dnswire_encoder_control_ready;
+ __state(handle, dnswire_writer_encoding_ready);
+ } else {
+ handle->encoder.state = dnswire_encoder_control_start;
+ __state(handle, dnswire_writer_encoding);
+ }
+
+ handle->bidirectional = bidirectional;
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_writer_set_bufsize(struct dnswire_writer* handle, size_t size)
+{
+ assert(handle);
+ assert(size);
+ assert(handle->buf);
+
+ if (handle->left > size) {
+ // we got data and it doesn't fit in the new size
+ return dnswire_error;
+ }
+ if (size > handle->max) {
+ // don't expand over max
+ return dnswire_error;
+ }
+
+ if (handle->at + handle->left > size) {
+ // move what's left to the start
+ if (handle->left) {
+ memmove(handle->buf, &handle->buf[handle->at], handle->left);
+ }
+ handle->at = 0;
+ }
+
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+
+ handle->buf = buf;
+ handle->size = size;
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_writer_set_bufinc(struct dnswire_writer* handle, size_t inc)
+{
+ assert(handle);
+ assert(inc);
+
+ handle->inc = inc;
+
+ return dnswire_ok;
+}
+
+enum dnswire_result dnswire_writer_set_bufmax(struct dnswire_writer* handle, size_t max)
+{
+ assert(handle);
+ assert(max);
+
+ if (max < handle->size) {
+ return dnswire_error;
+ }
+
+ handle->max = max;
+
+ return dnswire_ok;
+}
+
+static enum dnswire_result _encoding(struct dnswire_writer* handle)
+{
+ enum dnswire_result res;
+
+ while (1) {
+ res = dnswire_encoder_encode(&handle->encoder, &handle->buf[handle->at], handle->size - handle->at);
+ __trace("encode %s", dnswire_result_string[res]);
+
+ switch (res) {
+ case dnswire_ok:
+ case dnswire_again:
+ case dnswire_endofdata:
+ handle->at += dnswire_encoder_encoded(handle->encoder);
+ handle->left += dnswire_encoder_encoded(handle->encoder);
+ break;
+
+ case dnswire_need_more: {
+ if (handle->size >= handle->max) {
+ // already at max size and it's not enough
+ return dnswire_error;
+ }
+
+ // no space left, expand
+ size_t size = handle->size + handle->inc > handle->max ? handle->max : handle->size + handle->inc;
+ uint8_t* buf = realloc(handle->buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->buf = buf;
+ handle->size = size;
+ continue;
+ }
+ default:
+ break;
+ }
+ break;
+ }
+ return res;
+}
+
+enum dnswire_result dnswire_writer_pop(struct dnswire_writer* handle, uint8_t* data, size_t len, uint8_t* in_data, size_t* in_len)
+{
+ assert(handle);
+ assert(data);
+ assert(len);
+ assert(handle->buf);
+ assert(!handle->bidirectional || in_data);
+
+ handle->popped = 0;
+ size_t in_len_orig = 0;
+ if (in_len) {
+ in_len_orig = *in_len;
+ *in_len = 0;
+ }
+
+ enum dnswire_result res = dnswire_again;
+
+ __trace("state %s", dnswire_writer_state_string[handle->state]);
+
+ switch (handle->state) {
+ case dnswire_writer_encoding_ready:
+ res = _encoding(handle);
+ __trace("left %zu", handle->left);
+ if (res != dnswire_error && handle->left) {
+ __state(handle, dnswire_writer_writing);
+ // fallthrough
+ } else {
+ break;
+ }
+
+ case dnswire_writer_writing_ready:
+ handle->popped = len < handle->left ? len : handle->left;
+ memcpy(data, &handle->buf[handle->at - handle->left], handle->popped);
+ __trace("wrote %zd", handle->popped);
+ handle->left -= handle->popped;
+ __trace("left %zu", handle->left);
+ if (handle->left) {
+ break;
+ }
+ handle->at = 0;
+ __state(handle, dnswire_writer_reading_accept);
+
+ case dnswire_writer_reading_accept:
+ if (!in_len_orig) {
+ return dnswire_need_more;
+ }
+ *in_len = handle->read_size - handle->read_at - handle->read_left < in_len_orig ? handle->read_size - handle->read_at - handle->read_left : in_len_orig;
+ if (*in_len) {
+ memcpy(&handle->read_buf[handle->read_at + handle->read_left], in_data, *in_len);
+ __trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], *in_len));
+ handle->left += *in_len;
+ }
+ __state(handle, dnswire_writer_decoding_accept);
+ // fallthrough
+
+ case dnswire_writer_decoding_accept:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) {
+ case dnswire_bidirectional:
+ handle->read_at += dnswire_decoder_decoded(handle->decoder);
+ handle->read_left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->read_left) {
+ handle->read_at = 0;
+ }
+
+ if (!handle->decoder.accept_support_dnstap_protobuf) {
+ return dnswire_error;
+ }
+
+ __state(handle, dnswire_writer_encoding);
+ return dnswire_again;
+
+ case dnswire_again:
+ handle->read_at += dnswire_decoder_decoded(handle->decoder);
+ handle->read_left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->read_left) {
+ handle->read_at = 0;
+ __state(handle, dnswire_writer_reading_accept);
+ }
+ return dnswire_again;
+
+ case dnswire_need_more:
+ if (handle->read_left < handle->read_size) {
+ // still space left to fill
+ if (handle->read_at) {
+ // move what's left to the start
+ if (handle->read_left) {
+ memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left);
+ }
+ handle->read_at = 0;
+ }
+ } else if (handle->read_size < handle->read_max) {
+ // no space left, expand
+ size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc;
+ uint8_t* buf = realloc(handle->read_buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->read_buf = buf;
+ handle->read_size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_writer_reading_accept);
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_writer_encoding:
+ res = _encoding(handle);
+ __trace("left %zu", handle->left);
+ if (res != dnswire_error && handle->left) {
+ __state(handle, dnswire_writer_writing);
+ // fallthrough
+ } else {
+ break;
+ }
+
+ case dnswire_writer_writing:
+ handle->popped = len < handle->left ? len : handle->left;
+ memcpy(data, &handle->buf[handle->at - handle->left], handle->popped);
+ __trace("wrote %zd", handle->popped);
+ handle->left -= handle->popped;
+ __trace("left %zu", handle->left);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_writer_encoding);
+ }
+ break;
+
+ case dnswire_writer_stopping:
+ if (handle->left) {
+ handle->popped = len < handle->left ? len : handle->left;
+ memcpy(data, &handle->buf[handle->at - handle->left], handle->popped);
+ __trace("wrote %zd", handle->popped);
+ handle->left -= handle->popped;
+ if (handle->left) {
+ __trace("left %zu", handle->left);
+ return dnswire_again;
+ }
+ handle->at = 0;
+ }
+ __state(handle, dnswire_writer_encoding_stop);
+ // fallthrough
+
+ case dnswire_writer_encoding_stop:
+ res = _encoding(handle);
+ if (res == dnswire_endofdata) {
+ __state(handle, dnswire_writer_writing_stop);
+ // fallthrough
+ } else {
+ break;
+ }
+
+ case dnswire_writer_writing_stop:
+ if (handle->left) {
+ handle->popped = len < handle->left ? len : handle->left;
+ memcpy(data, &handle->buf[handle->at - handle->left], handle->popped);
+ __trace("wrote %zd", handle->popped);
+ handle->left -= handle->popped;
+ if (handle->left) {
+ __trace("left %zu", handle->left);
+ return dnswire_again;
+ }
+ handle->at = 0;
+ }
+ if (handle->bidirectional) {
+ __state(handle, dnswire_writer_reading_finish);
+ return dnswire_again;
+ }
+ __state(handle, dnswire_writer_done);
+ return dnswire_endofdata;
+
+ case dnswire_writer_reading_finish:
+ if (!in_len_orig) {
+ return dnswire_need_more;
+ }
+ *in_len = handle->read_size - handle->read_at - handle->read_left < in_len_orig ? handle->read_size - handle->read_at - handle->read_left : in_len_orig;
+ if (*in_len) {
+ memcpy(&handle->read_buf[handle->read_at + handle->read_left], in_data, *in_len);
+ __trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], *in_len));
+ handle->left += *in_len;
+ }
+ __state(handle, dnswire_writer_decoding_finish);
+ // fallthrough
+
+ case dnswire_writer_decoding_finish:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) {
+ case dnswire_endofdata:
+ __state(handle, dnswire_writer_done);
+ return dnswire_endofdata;
+
+ case dnswire_need_more:
+ if (handle->read_left < handle->read_size) {
+ // still space left to fill
+ if (handle->read_at) {
+ // move what's left to the start
+ if (handle->read_left) {
+ memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left);
+ }
+ handle->read_at = 0;
+ }
+ } else if (handle->read_size < handle->read_max) {
+ // no space left, expand
+ size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc;
+ uint8_t* buf = realloc(handle->read_buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->read_buf = buf;
+ handle->read_size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_writer_reading_accept);
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_writer_done:
+ return dnswire_error;
+ }
+
+ return res;
+}
+
+enum dnswire_result dnswire_writer_write(struct dnswire_writer* handle, int fd)
+{
+ assert(handle);
+ assert(handle->buf);
+
+ enum dnswire_result res = dnswire_again;
+
+ __trace("state %s", dnswire_writer_state_string[handle->state]);
+
+ switch (handle->state) {
+ case dnswire_writer_encoding_ready:
+ res = _encoding(handle);
+ __trace("left %zu", handle->left);
+ if (res != dnswire_error && handle->left) {
+ __state(handle, dnswire_writer_writing);
+ // fallthrough
+ } else {
+ break;
+ }
+
+ case dnswire_writer_writing_ready: {
+ ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left);
+ __trace("wrote %zd", nwrote);
+ if (nwrote < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nwrote) {
+ // TODO
+ return dnswire_error;
+ }
+
+ handle->left -= nwrote;
+ __trace("left %zu", handle->left);
+ if (handle->left) {
+ break;
+ }
+ handle->at = 0;
+ __state(handle, dnswire_writer_reading_accept);
+ // fallthrough
+ }
+
+ case dnswire_writer_reading_accept: {
+ ssize_t nread = read(fd, &handle->read_buf[handle->read_at + handle->read_left], handle->read_size - handle->read_at - handle->read_left);
+ if (nread < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nread) {
+ // TODO
+ return dnswire_error;
+ }
+ __trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], nread));
+ handle->read_left += nread;
+ __state(handle, dnswire_writer_decoding_accept);
+ // fallthrough
+ }
+
+ case dnswire_writer_decoding_accept:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) {
+ case dnswire_bidirectional:
+ handle->read_at += dnswire_decoder_decoded(handle->decoder);
+ handle->read_left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->read_left) {
+ handle->read_at = 0;
+ }
+
+ if (!handle->decoder.accept_support_dnstap_protobuf) {
+ return dnswire_error;
+ }
+
+ __state(handle, dnswire_writer_encoding);
+ return dnswire_again;
+
+ case dnswire_again:
+ handle->read_at += dnswire_decoder_decoded(handle->decoder);
+ handle->read_left -= dnswire_decoder_decoded(handle->decoder);
+ if (!handle->read_left) {
+ handle->read_at = 0;
+ __state(handle, dnswire_writer_reading_accept);
+ }
+ return dnswire_again;
+
+ case dnswire_need_more:
+ if (handle->read_left < handle->read_size) {
+ // still space left to fill
+ if (handle->read_at) {
+ // move what's left to the start
+ if (handle->read_left) {
+ memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left);
+ }
+ handle->read_at = 0;
+ }
+ } else if (handle->read_size < handle->read_max) {
+ // no space left, expand
+ size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc;
+ uint8_t* buf = realloc(handle->read_buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->read_buf = buf;
+ handle->read_size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_writer_reading_accept);
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_writer_encoding:
+ res = _encoding(handle);
+ __trace("left %zu", handle->left);
+ if (res != dnswire_error && handle->left) {
+ __state(handle, dnswire_writer_writing);
+ // fallthrough
+ } else {
+ break;
+ }
+
+ case dnswire_writer_writing: {
+ ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left);
+ __trace("wrote %zd", nwrote);
+ if (nwrote < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nwrote) {
+ // TODO
+ return dnswire_error;
+ }
+
+ handle->left -= nwrote;
+ __trace("left %zu", handle->left);
+ if (!handle->left) {
+ handle->at = 0;
+ __state(handle, dnswire_writer_encoding);
+ }
+ break;
+ }
+
+ case dnswire_writer_stopping:
+ if (handle->left) {
+ ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left);
+ __trace("wrote %zd", nwrote);
+ if (nwrote < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nwrote) {
+ // TODO
+ return dnswire_error;
+ }
+
+ handle->left -= nwrote;
+ if (handle->left) {
+ __trace("left %zu", handle->left);
+ return dnswire_again;
+ }
+ handle->at = 0;
+ }
+ __state(handle, dnswire_writer_encoding_stop);
+ // fallthrough
+
+ case dnswire_writer_encoding_stop:
+ res = _encoding(handle);
+ if (res == dnswire_endofdata) {
+ __state(handle, dnswire_writer_writing_stop);
+ // fallthrough
+ } else {
+ break;
+ }
+
+ case dnswire_writer_writing_stop:
+ if (handle->left) {
+ ssize_t nwrote = write(fd, &handle->buf[handle->at - handle->left], handle->left);
+ __trace("wrote %zd", nwrote);
+ if (nwrote < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nwrote) {
+ // TODO
+ return dnswire_error;
+ }
+
+ handle->left -= nwrote;
+ if (handle->left) {
+ __trace("left %zu", handle->left);
+ return dnswire_again;
+ }
+ handle->at = 0;
+ }
+ if (handle->bidirectional) {
+ __state(handle, dnswire_writer_reading_finish);
+ return dnswire_again;
+ }
+ __state(handle, dnswire_writer_done);
+ return dnswire_endofdata;
+
+ case dnswire_writer_reading_finish: {
+ ssize_t nread = read(fd, &handle->read_buf[handle->read_at + handle->read_left], handle->read_size - handle->read_at - handle->read_left);
+ if (nread < 0) {
+ // TODO
+ return dnswire_error;
+ } else if (!nread) {
+ // TODO
+ return dnswire_error;
+ }
+ __trace("%s", __printable_string(&handle->read_buf[handle->read_at + handle->read_left], nread));
+ handle->read_left += nread;
+ __state(handle, dnswire_writer_decoding_finish);
+ // fallthrough
+ }
+
+ case dnswire_writer_decoding_finish:
+ switch (dnswire_decoder_decode(&handle->decoder, &handle->read_buf[handle->read_at], handle->read_left)) {
+ case dnswire_endofdata:
+ __state(handle, dnswire_writer_done);
+ return dnswire_endofdata;
+
+ case dnswire_need_more:
+ if (handle->read_left < handle->read_size) {
+ // still space left to fill
+ if (handle->read_at) {
+ // move what's left to the start
+ if (handle->read_left) {
+ memmove(handle->read_buf, &handle->read_buf[handle->read_at], handle->read_left);
+ }
+ handle->read_at = 0;
+ }
+ } else if (handle->read_size < handle->read_max) {
+ // no space left, expand
+ size_t size = handle->read_size + handle->read_inc > handle->read_max ? handle->read_max : handle->read_size + handle->read_inc;
+ uint8_t* buf = realloc(handle->read_buf, size);
+ if (!buf) {
+ return dnswire_error;
+ }
+ handle->read_buf = buf;
+ handle->read_size = size;
+ } else {
+ // already at max size, and full
+ return dnswire_error;
+ }
+ __state(handle, dnswire_writer_reading_accept);
+ return dnswire_need_more;
+
+ default:
+ break;
+ }
+ return dnswire_error;
+
+ case dnswire_writer_done:
+ return dnswire_error;
+ }
+
+ return res;
+}
+
+enum dnswire_result dnswire_writer_stop(struct dnswire_writer* handle)
+{
+ assert(handle);
+
+ enum dnswire_result res = dnswire_encoder_stop(&handle->encoder);
+
+ if (res == dnswire_ok) {
+ __state(handle, dnswire_writer_stopping);
+ }
+
+ return res;
+}