Home | History | Annotate | Line # | Download | only in dnstap
dnstap.c revision 1.1.1.2
      1      1.1  christos /* dnstap support for Unbound */
      2      1.1  christos 
      3      1.1  christos /*
      4      1.1  christos  * Copyright (c) 2013-2014, Farsight Security, Inc.
      5      1.1  christos  * All rights reserved.
      6      1.1  christos  *
      7      1.1  christos  * Redistribution and use in source and binary forms, with or without
      8      1.1  christos  * modification, are permitted provided that the following conditions
      9      1.1  christos  * are met:
     10      1.1  christos  *
     11      1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12      1.1  christos  * notice, this list of conditions and the following disclaimer.
     13      1.1  christos  *
     14      1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     15      1.1  christos  * notice, this list of conditions and the following disclaimer in the
     16      1.1  christos  * documentation and/or other materials provided with the distribution.
     17      1.1  christos  *
     18      1.1  christos  * 3. Neither the name of the copyright holder nor the names of its
     19      1.1  christos  * contributors may be used to endorse or promote products derived from
     20      1.1  christos  * this software without specific prior written permission.
     21      1.1  christos  *
     22      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     23      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     24      1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     25      1.1  christos  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     26      1.1  christos  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     27      1.1  christos  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     28      1.1  christos  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     29      1.1  christos  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     30      1.1  christos  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     31      1.1  christos  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     32      1.1  christos  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33      1.1  christos  */
     34      1.1  christos 
     35      1.1  christos #include "dnstap/dnstap_config.h"
     36      1.1  christos 
     37      1.1  christos #ifdef USE_DNSTAP
     38      1.1  christos 
     39      1.1  christos #include "config.h"
     40      1.1  christos #include <string.h>
     41      1.1  christos #include <sys/time.h>
     42      1.1  christos #include "sldns/sbuffer.h"
     43      1.1  christos #include "util/config_file.h"
     44      1.1  christos #include "util/net_help.h"
     45      1.1  christos #include "util/netevent.h"
     46      1.1  christos #include "util/log.h"
     47      1.1  christos 
     48      1.1  christos #include <fstrm.h>
     49      1.1  christos #include <protobuf-c/protobuf-c.h>
     50      1.1  christos 
     51      1.1  christos #include "dnstap/dnstap.h"
     52      1.1  christos #include "dnstap/dnstap.pb-c.h"
     53      1.1  christos 
     54      1.1  christos #define DNSTAP_CONTENT_TYPE		"protobuf:dnstap.Dnstap"
     55      1.1  christos #define DNSTAP_INITIAL_BUF_SIZE		256
     56      1.1  christos 
     57      1.1  christos struct dt_msg {
     58      1.1  christos 	void		*buf;
     59      1.1  christos 	size_t		len_buf;
     60      1.1  christos 	Dnstap__Dnstap	d;
     61      1.1  christos 	Dnstap__Message	m;
     62      1.1  christos };
     63      1.1  christos 
     64      1.1  christos static int
     65      1.1  christos dt_pack(const Dnstap__Dnstap *d, void **buf, size_t *sz)
     66      1.1  christos {
     67      1.1  christos 	ProtobufCBufferSimple sbuf;
     68      1.1  christos 
     69      1.1  christos 	memset(&sbuf, 0, sizeof(sbuf));
     70      1.1  christos 	sbuf.base.append = protobuf_c_buffer_simple_append;
     71      1.1  christos 	sbuf.len = 0;
     72      1.1  christos 	sbuf.alloced = DNSTAP_INITIAL_BUF_SIZE;
     73      1.1  christos 	sbuf.data = malloc(sbuf.alloced);
     74      1.1  christos 	if (sbuf.data == NULL)
     75      1.1  christos 		return 0;
     76      1.1  christos 	sbuf.must_free_data = 1;
     77      1.1  christos 
     78      1.1  christos 	*sz = dnstap__dnstap__pack_to_buffer(d, (ProtobufCBuffer *) &sbuf);
     79      1.1  christos 	if (sbuf.data == NULL)
     80      1.1  christos 		return 0;
     81      1.1  christos 	*buf = sbuf.data;
     82      1.1  christos 
     83      1.1  christos 	return 1;
     84      1.1  christos }
     85      1.1  christos 
     86      1.1  christos static void
     87      1.1  christos dt_send(const struct dt_env *env, void *buf, size_t len_buf)
     88      1.1  christos {
     89      1.1  christos 	fstrm_res res;
     90      1.1  christos 	if (!buf)
     91      1.1  christos 		return;
     92      1.1  christos 	res = fstrm_iothr_submit(env->iothr, env->ioq, buf, len_buf,
     93      1.1  christos 				 fstrm_free_wrapper, NULL);
     94      1.1  christos 	if (res != fstrm_res_success)
     95      1.1  christos 		free(buf);
     96      1.1  christos }
     97      1.1  christos 
     98      1.1  christos static void
     99      1.1  christos dt_msg_init(const struct dt_env *env,
    100      1.1  christos 	    struct dt_msg *dm,
    101      1.1  christos 	    Dnstap__Message__Type mtype)
    102      1.1  christos {
    103      1.1  christos 	memset(dm, 0, sizeof(*dm));
    104      1.1  christos 	dm->d.base.descriptor = &dnstap__dnstap__descriptor;
    105      1.1  christos 	dm->m.base.descriptor = &dnstap__message__descriptor;
    106      1.1  christos 	dm->d.type = DNSTAP__DNSTAP__TYPE__MESSAGE;
    107      1.1  christos 	dm->d.message = &dm->m;
    108      1.1  christos 	dm->m.type = mtype;
    109      1.1  christos 	if (env->identity != NULL) {
    110      1.1  christos 		dm->d.identity.data = (uint8_t *) env->identity;
    111      1.1  christos 		dm->d.identity.len = (size_t) env->len_identity;
    112      1.1  christos 		dm->d.has_identity = 1;
    113      1.1  christos 	}
    114      1.1  christos 	if (env->version != NULL) {
    115      1.1  christos 		dm->d.version.data = (uint8_t *) env->version;
    116      1.1  christos 		dm->d.version.len = (size_t) env->len_version;
    117      1.1  christos 		dm->d.has_version = 1;
    118      1.1  christos 	}
    119      1.1  christos }
    120      1.1  christos 
    121      1.1  christos struct dt_env *
    122      1.1  christos dt_create(const char *socket_path, unsigned num_workers)
    123      1.1  christos {
    124  1.1.1.2  christos #ifdef UNBOUND_DEBUG
    125      1.1  christos 	fstrm_res res;
    126  1.1.1.2  christos #endif
    127      1.1  christos 	struct dt_env *env;
    128      1.1  christos 	struct fstrm_iothr_options *fopt;
    129      1.1  christos 	struct fstrm_unix_writer_options *fuwopt;
    130      1.1  christos 	struct fstrm_writer *fw;
    131      1.1  christos 	struct fstrm_writer_options *fwopt;
    132      1.1  christos 
    133      1.1  christos 	verbose(VERB_OPS, "attempting to connect to dnstap socket %s",
    134      1.1  christos 		socket_path);
    135      1.1  christos 	log_assert(socket_path != NULL);
    136      1.1  christos 	log_assert(num_workers > 0);
    137      1.1  christos 
    138      1.1  christos 	env = (struct dt_env *) calloc(1, sizeof(struct dt_env));
    139      1.1  christos 	if (!env)
    140      1.1  christos 		return NULL;
    141      1.1  christos 
    142      1.1  christos 	fwopt = fstrm_writer_options_init();
    143  1.1.1.2  christos #ifdef UNBOUND_DEBUG
    144  1.1.1.2  christos 	res =
    145  1.1.1.2  christos #else
    146  1.1.1.2  christos 	(void)
    147  1.1.1.2  christos #endif
    148  1.1.1.2  christos 	    fstrm_writer_options_add_content_type(fwopt,
    149      1.1  christos 		DNSTAP_CONTENT_TYPE, sizeof(DNSTAP_CONTENT_TYPE) - 1);
    150      1.1  christos 	log_assert(res == fstrm_res_success);
    151      1.1  christos 
    152      1.1  christos 	fuwopt = fstrm_unix_writer_options_init();
    153      1.1  christos 	fstrm_unix_writer_options_set_socket_path(fuwopt, socket_path);
    154      1.1  christos 
    155      1.1  christos 	fw = fstrm_unix_writer_init(fuwopt, fwopt);
    156      1.1  christos 	log_assert(fw != NULL);
    157      1.1  christos 
    158      1.1  christos 	fopt = fstrm_iothr_options_init();
    159      1.1  christos 	fstrm_iothr_options_set_num_input_queues(fopt, num_workers);
    160      1.1  christos 	env->iothr = fstrm_iothr_init(fopt, &fw);
    161      1.1  christos 	if (env->iothr == NULL) {
    162      1.1  christos 		verbose(VERB_DETAIL, "dt_create: fstrm_iothr_init() failed");
    163      1.1  christos 		fstrm_writer_destroy(&fw);
    164      1.1  christos 		free(env);
    165      1.1  christos 		env = NULL;
    166      1.1  christos 	}
    167      1.1  christos 	fstrm_iothr_options_destroy(&fopt);
    168      1.1  christos 	fstrm_unix_writer_options_destroy(&fuwopt);
    169      1.1  christos 	fstrm_writer_options_destroy(&fwopt);
    170      1.1  christos 
    171      1.1  christos 	return env;
    172      1.1  christos }
    173      1.1  christos 
    174      1.1  christos static void
    175      1.1  christos dt_apply_identity(struct dt_env *env, struct config_file *cfg)
    176      1.1  christos {
    177      1.1  christos 	char buf[MAXHOSTNAMELEN+1];
    178      1.1  christos 	if (!cfg->dnstap_send_identity)
    179      1.1  christos 		return;
    180      1.1  christos 	free(env->identity);
    181      1.1  christos 	if (cfg->dnstap_identity == NULL || cfg->dnstap_identity[0] == 0) {
    182      1.1  christos 		if (gethostname(buf, MAXHOSTNAMELEN) == 0) {
    183      1.1  christos 			buf[MAXHOSTNAMELEN] = 0;
    184      1.1  christos 			env->identity = strdup(buf);
    185      1.1  christos 		} else {
    186      1.1  christos 			fatal_exit("dt_apply_identity: gethostname() failed");
    187      1.1  christos 		}
    188      1.1  christos 	} else {
    189      1.1  christos 		env->identity = strdup(cfg->dnstap_identity);
    190      1.1  christos 	}
    191      1.1  christos 	if (env->identity == NULL)
    192      1.1  christos 		fatal_exit("dt_apply_identity: strdup() failed");
    193      1.1  christos 	env->len_identity = (unsigned int)strlen(env->identity);
    194      1.1  christos 	verbose(VERB_OPS, "dnstap identity field set to \"%s\"",
    195      1.1  christos 		env->identity);
    196      1.1  christos }
    197      1.1  christos 
    198      1.1  christos static void
    199      1.1  christos dt_apply_version(struct dt_env *env, struct config_file *cfg)
    200      1.1  christos {
    201      1.1  christos 	if (!cfg->dnstap_send_version)
    202      1.1  christos 		return;
    203      1.1  christos 	free(env->version);
    204      1.1  christos 	if (cfg->dnstap_version == NULL || cfg->dnstap_version[0] == 0)
    205      1.1  christos 		env->version = strdup(PACKAGE_STRING);
    206      1.1  christos 	else
    207      1.1  christos 		env->version = strdup(cfg->dnstap_version);
    208      1.1  christos 	if (env->version == NULL)
    209      1.1  christos 		fatal_exit("dt_apply_version: strdup() failed");
    210      1.1  christos 	env->len_version = (unsigned int)strlen(env->version);
    211      1.1  christos 	verbose(VERB_OPS, "dnstap version field set to \"%s\"",
    212      1.1  christos 		env->version);
    213      1.1  christos }
    214      1.1  christos 
    215      1.1  christos void
    216      1.1  christos dt_apply_cfg(struct dt_env *env, struct config_file *cfg)
    217      1.1  christos {
    218      1.1  christos 	if (!cfg->dnstap)
    219      1.1  christos 		return;
    220      1.1  christos 
    221      1.1  christos 	dt_apply_identity(env, cfg);
    222      1.1  christos 	dt_apply_version(env, cfg);
    223      1.1  christos 	if ((env->log_resolver_query_messages = (unsigned int)
    224      1.1  christos 	     cfg->dnstap_log_resolver_query_messages))
    225      1.1  christos 	{
    226      1.1  christos 		verbose(VERB_OPS, "dnstap Message/RESOLVER_QUERY enabled");
    227      1.1  christos 	}
    228      1.1  christos 	if ((env->log_resolver_response_messages = (unsigned int)
    229      1.1  christos 	     cfg->dnstap_log_resolver_response_messages))
    230      1.1  christos 	{
    231      1.1  christos 		verbose(VERB_OPS, "dnstap Message/RESOLVER_RESPONSE enabled");
    232      1.1  christos 	}
    233      1.1  christos 	if ((env->log_client_query_messages = (unsigned int)
    234      1.1  christos 	     cfg->dnstap_log_client_query_messages))
    235      1.1  christos 	{
    236      1.1  christos 		verbose(VERB_OPS, "dnstap Message/CLIENT_QUERY enabled");
    237      1.1  christos 	}
    238      1.1  christos 	if ((env->log_client_response_messages = (unsigned int)
    239      1.1  christos 	     cfg->dnstap_log_client_response_messages))
    240      1.1  christos 	{
    241      1.1  christos 		verbose(VERB_OPS, "dnstap Message/CLIENT_RESPONSE enabled");
    242      1.1  christos 	}
    243      1.1  christos 	if ((env->log_forwarder_query_messages = (unsigned int)
    244      1.1  christos 	     cfg->dnstap_log_forwarder_query_messages))
    245      1.1  christos 	{
    246      1.1  christos 		verbose(VERB_OPS, "dnstap Message/FORWARDER_QUERY enabled");
    247      1.1  christos 	}
    248      1.1  christos 	if ((env->log_forwarder_response_messages = (unsigned int)
    249      1.1  christos 	     cfg->dnstap_log_forwarder_response_messages))
    250      1.1  christos 	{
    251      1.1  christos 		verbose(VERB_OPS, "dnstap Message/FORWARDER_RESPONSE enabled");
    252      1.1  christos 	}
    253      1.1  christos }
    254      1.1  christos 
    255      1.1  christos int
    256      1.1  christos dt_init(struct dt_env *env)
    257      1.1  christos {
    258      1.1  christos 	env->ioq = fstrm_iothr_get_input_queue(env->iothr);
    259      1.1  christos 	if (env->ioq == NULL)
    260      1.1  christos 		return 0;
    261      1.1  christos 	return 1;
    262      1.1  christos }
    263      1.1  christos 
    264      1.1  christos void
    265      1.1  christos dt_delete(struct dt_env *env)
    266      1.1  christos {
    267      1.1  christos 	if (!env)
    268      1.1  christos 		return;
    269      1.1  christos 	verbose(VERB_OPS, "closing dnstap socket");
    270      1.1  christos 	fstrm_iothr_destroy(&env->iothr);
    271      1.1  christos 	free(env->identity);
    272      1.1  christos 	free(env->version);
    273      1.1  christos 	free(env);
    274      1.1  christos }
    275      1.1  christos 
    276      1.1  christos static void
    277      1.1  christos dt_fill_timeval(const struct timeval *tv,
    278      1.1  christos 		uint64_t *time_sec, protobuf_c_boolean *has_time_sec,
    279      1.1  christos 		uint32_t *time_nsec, protobuf_c_boolean *has_time_nsec)
    280      1.1  christos {
    281      1.1  christos #ifndef S_SPLINT_S
    282      1.1  christos 	*time_sec = tv->tv_sec;
    283      1.1  christos 	*time_nsec = tv->tv_usec * 1000;
    284      1.1  christos #endif
    285      1.1  christos 	*has_time_sec = 1;
    286      1.1  christos 	*has_time_nsec = 1;
    287      1.1  christos }
    288      1.1  christos 
    289      1.1  christos static void
    290      1.1  christos dt_fill_buffer(sldns_buffer *b, ProtobufCBinaryData *p, protobuf_c_boolean *has)
    291      1.1  christos {
    292      1.1  christos 	log_assert(b != NULL);
    293      1.1  christos 	p->len = sldns_buffer_limit(b);
    294      1.1  christos 	p->data = sldns_buffer_begin(b);
    295      1.1  christos 	*has = 1;
    296      1.1  christos }
    297      1.1  christos 
    298      1.1  christos static void
    299      1.1  christos dt_msg_fill_net(struct dt_msg *dm,
    300      1.1  christos 		struct sockaddr_storage *ss,
    301      1.1  christos 		enum comm_point_type cptype,
    302      1.1  christos 		ProtobufCBinaryData *addr, protobuf_c_boolean *has_addr,
    303      1.1  christos 		uint32_t *port, protobuf_c_boolean *has_port)
    304      1.1  christos {
    305      1.1  christos 	log_assert(ss->ss_family == AF_INET6 || ss->ss_family == AF_INET);
    306      1.1  christos 	if (ss->ss_family == AF_INET6) {
    307      1.1  christos 		struct sockaddr_in6 *s = (struct sockaddr_in6 *) ss;
    308      1.1  christos 
    309      1.1  christos 		/* socket_family */
    310      1.1  christos 		dm->m.socket_family = DNSTAP__SOCKET_FAMILY__INET6;
    311      1.1  christos 		dm->m.has_socket_family = 1;
    312      1.1  christos 
    313      1.1  christos 		/* addr: query_address or response_address */
    314      1.1  christos 		addr->data = s->sin6_addr.s6_addr;
    315      1.1  christos 		addr->len = 16; /* IPv6 */
    316      1.1  christos 		*has_addr = 1;
    317      1.1  christos 
    318      1.1  christos 		/* port: query_port or response_port */
    319      1.1  christos 		*port = ntohs(s->sin6_port);
    320      1.1  christos 		*has_port = 1;
    321      1.1  christos 	} else if (ss->ss_family == AF_INET) {
    322      1.1  christos 		struct sockaddr_in *s = (struct sockaddr_in *) ss;
    323      1.1  christos 
    324      1.1  christos 		/* socket_family */
    325      1.1  christos 		dm->m.socket_family = DNSTAP__SOCKET_FAMILY__INET;
    326      1.1  christos 		dm->m.has_socket_family = 1;
    327      1.1  christos 
    328      1.1  christos 		/* addr: query_address or response_address */
    329      1.1  christos 		addr->data = (uint8_t *) &s->sin_addr.s_addr;
    330      1.1  christos 		addr->len = 4; /* IPv4 */
    331      1.1  christos 		*has_addr = 1;
    332      1.1  christos 
    333      1.1  christos 		/* port: query_port or response_port */
    334      1.1  christos 		*port = ntohs(s->sin_port);
    335      1.1  christos 		*has_port = 1;
    336      1.1  christos 	}
    337      1.1  christos 
    338      1.1  christos 	log_assert(cptype == comm_udp || cptype == comm_tcp);
    339      1.1  christos 	if (cptype == comm_udp) {
    340      1.1  christos 		/* socket_protocol */
    341      1.1  christos 		dm->m.socket_protocol = DNSTAP__SOCKET_PROTOCOL__UDP;
    342      1.1  christos 		dm->m.has_socket_protocol = 1;
    343      1.1  christos 	} else if (cptype == comm_tcp) {
    344      1.1  christos 		/* socket_protocol */
    345      1.1  christos 		dm->m.socket_protocol = DNSTAP__SOCKET_PROTOCOL__TCP;
    346      1.1  christos 		dm->m.has_socket_protocol = 1;
    347      1.1  christos 	}
    348      1.1  christos }
    349      1.1  christos 
    350      1.1  christos void
    351      1.1  christos dt_msg_send_client_query(struct dt_env *env,
    352      1.1  christos 			 struct sockaddr_storage *qsock,
    353      1.1  christos 			 enum comm_point_type cptype,
    354      1.1  christos 			 sldns_buffer *qmsg)
    355      1.1  christos {
    356      1.1  christos 	struct dt_msg dm;
    357      1.1  christos 	struct timeval qtime;
    358      1.1  christos 
    359      1.1  christos 	gettimeofday(&qtime, NULL);
    360      1.1  christos 
    361      1.1  christos 	/* type */
    362      1.1  christos 	dt_msg_init(env, &dm, DNSTAP__MESSAGE__TYPE__CLIENT_QUERY);
    363      1.1  christos 
    364      1.1  christos 	/* query_time */
    365      1.1  christos 	dt_fill_timeval(&qtime,
    366      1.1  christos 			&dm.m.query_time_sec, &dm.m.has_query_time_sec,
    367      1.1  christos 			&dm.m.query_time_nsec, &dm.m.has_query_time_nsec);
    368      1.1  christos 
    369      1.1  christos 	/* query_message */
    370      1.1  christos 	dt_fill_buffer(qmsg, &dm.m.query_message, &dm.m.has_query_message);
    371      1.1  christos 
    372      1.1  christos 	/* socket_family, socket_protocol, query_address, query_port */
    373      1.1  christos 	log_assert(cptype == comm_udp || cptype == comm_tcp);
    374      1.1  christos 	dt_msg_fill_net(&dm, qsock, cptype,
    375      1.1  christos 			&dm.m.query_address, &dm.m.has_query_address,
    376      1.1  christos 			&dm.m.query_port, &dm.m.has_query_port);
    377      1.1  christos 
    378      1.1  christos 	if (dt_pack(&dm.d, &dm.buf, &dm.len_buf))
    379      1.1  christos 		dt_send(env, dm.buf, dm.len_buf);
    380      1.1  christos }
    381      1.1  christos 
    382      1.1  christos void
    383      1.1  christos dt_msg_send_client_response(struct dt_env *env,
    384      1.1  christos 			    struct sockaddr_storage *qsock,
    385      1.1  christos 			    enum comm_point_type cptype,
    386      1.1  christos 			    sldns_buffer *rmsg)
    387      1.1  christos {
    388      1.1  christos 	struct dt_msg dm;
    389      1.1  christos 	struct timeval rtime;
    390      1.1  christos 
    391      1.1  christos 	gettimeofday(&rtime, NULL);
    392      1.1  christos 
    393      1.1  christos 	/* type */
    394      1.1  christos 	dt_msg_init(env, &dm, DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE);
    395      1.1  christos 
    396      1.1  christos 	/* response_time */
    397      1.1  christos 	dt_fill_timeval(&rtime,
    398      1.1  christos 			&dm.m.response_time_sec, &dm.m.has_response_time_sec,
    399      1.1  christos 			&dm.m.response_time_nsec, &dm.m.has_response_time_nsec);
    400      1.1  christos 
    401      1.1  christos 	/* response_message */
    402      1.1  christos 	dt_fill_buffer(rmsg, &dm.m.response_message, &dm.m.has_response_message);
    403      1.1  christos 
    404      1.1  christos 	/* socket_family, socket_protocol, query_address, query_port */
    405      1.1  christos 	log_assert(cptype == comm_udp || cptype == comm_tcp);
    406      1.1  christos 	dt_msg_fill_net(&dm, qsock, cptype,
    407      1.1  christos 			&dm.m.query_address, &dm.m.has_query_address,
    408      1.1  christos 			&dm.m.query_port, &dm.m.has_query_port);
    409      1.1  christos 
    410      1.1  christos 	if (dt_pack(&dm.d, &dm.buf, &dm.len_buf))
    411      1.1  christos 		dt_send(env, dm.buf, dm.len_buf);
    412      1.1  christos }
    413      1.1  christos 
    414      1.1  christos void
    415      1.1  christos dt_msg_send_outside_query(struct dt_env *env,
    416      1.1  christos 			  struct sockaddr_storage *rsock,
    417      1.1  christos 			  enum comm_point_type cptype,
    418      1.1  christos 			  uint8_t *zone, size_t zone_len,
    419      1.1  christos 			  sldns_buffer *qmsg)
    420      1.1  christos {
    421      1.1  christos 	struct dt_msg dm;
    422      1.1  christos 	struct timeval qtime;
    423      1.1  christos 	uint16_t qflags;
    424      1.1  christos 
    425      1.1  christos 	gettimeofday(&qtime, NULL);
    426      1.1  christos 	qflags = sldns_buffer_read_u16_at(qmsg, 2);
    427      1.1  christos 
    428      1.1  christos 	/* type */
    429      1.1  christos 	if (qflags & BIT_RD) {
    430      1.1  christos 		if (!env->log_forwarder_query_messages)
    431      1.1  christos 			return;
    432      1.1  christos 		dt_msg_init(env, &dm, DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY);
    433      1.1  christos 	} else {
    434      1.1  christos 		if (!env->log_resolver_query_messages)
    435      1.1  christos 			return;
    436      1.1  christos 		dt_msg_init(env, &dm, DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY);
    437      1.1  christos 	}
    438      1.1  christos 
    439      1.1  christos 	/* query_zone */
    440      1.1  christos 	dm.m.query_zone.data = zone;
    441      1.1  christos 	dm.m.query_zone.len = zone_len;
    442      1.1  christos 	dm.m.has_query_zone = 1;
    443      1.1  christos 
    444      1.1  christos 	/* query_time_sec, query_time_nsec */
    445      1.1  christos 	dt_fill_timeval(&qtime,
    446      1.1  christos 			&dm.m.query_time_sec, &dm.m.has_query_time_sec,
    447      1.1  christos 			&dm.m.query_time_nsec, &dm.m.has_query_time_nsec);
    448      1.1  christos 
    449      1.1  christos 	/* query_message */
    450      1.1  christos 	dt_fill_buffer(qmsg, &dm.m.query_message, &dm.m.has_query_message);
    451      1.1  christos 
    452      1.1  christos 	/* socket_family, socket_protocol, response_address, response_port */
    453      1.1  christos 	log_assert(cptype == comm_udp || cptype == comm_tcp);
    454      1.1  christos 	dt_msg_fill_net(&dm, rsock, cptype,
    455      1.1  christos 			&dm.m.response_address, &dm.m.has_response_address,
    456      1.1  christos 			&dm.m.response_port, &dm.m.has_response_port);
    457      1.1  christos 
    458      1.1  christos 	if (dt_pack(&dm.d, &dm.buf, &dm.len_buf))
    459      1.1  christos 		dt_send(env, dm.buf, dm.len_buf);
    460      1.1  christos }
    461      1.1  christos 
    462      1.1  christos void
    463      1.1  christos dt_msg_send_outside_response(struct dt_env *env,
    464      1.1  christos 			     struct sockaddr_storage *rsock,
    465      1.1  christos 			     enum comm_point_type cptype,
    466      1.1  christos 			     uint8_t *zone, size_t zone_len,
    467      1.1  christos 			     uint8_t *qbuf, size_t qbuf_len,
    468      1.1  christos 			     const struct timeval *qtime,
    469      1.1  christos 			     const struct timeval *rtime,
    470      1.1  christos 			     sldns_buffer *rmsg)
    471      1.1  christos {
    472      1.1  christos 	struct dt_msg dm;
    473      1.1  christos 	uint16_t qflags;
    474      1.1  christos 
    475      1.1  christos 	log_assert(qbuf_len >= sizeof(qflags));
    476      1.1  christos 	memcpy(&qflags, qbuf, sizeof(qflags));
    477      1.1  christos 	qflags = ntohs(qflags);
    478      1.1  christos 
    479      1.1  christos 	/* type */
    480      1.1  christos 	if (qflags & BIT_RD) {
    481      1.1  christos 		if (!env->log_forwarder_response_messages)
    482      1.1  christos 			return;
    483      1.1  christos 		dt_msg_init(env, &dm, DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE);
    484      1.1  christos 	} else {
    485      1.1  christos 		if (!env->log_resolver_response_messages)
    486      1.1  christos 			return;
    487      1.1  christos 		dt_msg_init(env, &dm, DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE);
    488      1.1  christos 	}
    489      1.1  christos 
    490      1.1  christos 	/* query_zone */
    491      1.1  christos 	dm.m.query_zone.data = zone;
    492      1.1  christos 	dm.m.query_zone.len = zone_len;
    493      1.1  christos 	dm.m.has_query_zone = 1;
    494      1.1  christos 
    495      1.1  christos 	/* query_time_sec, query_time_nsec */
    496      1.1  christos 	dt_fill_timeval(qtime,
    497      1.1  christos 			&dm.m.query_time_sec, &dm.m.has_query_time_sec,
    498      1.1  christos 			&dm.m.query_time_nsec, &dm.m.has_query_time_nsec);
    499      1.1  christos 
    500      1.1  christos 	/* response_time_sec, response_time_nsec */
    501      1.1  christos 	dt_fill_timeval(rtime,
    502      1.1  christos 			&dm.m.response_time_sec, &dm.m.has_response_time_sec,
    503      1.1  christos 			&dm.m.response_time_nsec, &dm.m.has_response_time_nsec);
    504      1.1  christos 
    505      1.1  christos 	/* response_message */
    506      1.1  christos 	dt_fill_buffer(rmsg, &dm.m.response_message, &dm.m.has_response_message);
    507      1.1  christos 
    508      1.1  christos 	/* socket_family, socket_protocol, response_address, response_port */
    509      1.1  christos 	log_assert(cptype == comm_udp || cptype == comm_tcp);
    510      1.1  christos 	dt_msg_fill_net(&dm, rsock, cptype,
    511      1.1  christos 			&dm.m.response_address, &dm.m.has_response_address,
    512      1.1  christos 			&dm.m.response_port, &dm.m.has_response_port);
    513      1.1  christos 
    514      1.1  christos 	if (dt_pack(&dm.d, &dm.buf, &dm.len_buf))
    515      1.1  christos 		dt_send(env, dm.buf, dm.len_buf);
    516      1.1  christos }
    517      1.1  christos 
    518      1.1  christos #endif /* USE_DNSTAP */
    519