2 * dnstap/unbound-dnstap-socket.c - debug program that listens for DNSTAP logs.
4 * Copyright (c) 2020, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This program listens on a DNSTAP socket for logged messages.
53 #include <openssl/ssl.h>
54 #include <openssl/rand.h>
55 #include <openssl/err.h>
56 #include "dnstap/dtstream.h"
57 #include "dnstap/dnstap_fstrm.h"
59 #include "util/ub_event.h"
60 #include "util/net_help.h"
61 #include "services/listen_dnsport.h"
62 #include "sldns/sbuffer.h"
63 #include "sldns/wire2str.h"
64 #include "sldns/pkthdr.h"
66 #include <protobuf-c/protobuf-c.h>
67 #include "dnstap/dnstap.pb-c.h"
68 #endif /* USE_DNSTAP */
69 #include "util/config_file.h"
71 /** listen backlog on TCP connections for dnstap logs */
72 #define LISTEN_BACKLOG 16
74 /** usage information for streamtcp */
75 static void usage(char* argv[])
77 printf("usage: %s [options]\n", argv[0]);
78 printf(" Listen to dnstap messages\n");
79 printf("stdout has dnstap log, stderr has verbose server log\n");
80 printf("-u <socketpath> listen to unix socket with this file name\n");
81 printf("-s <serverip[@port]> listen for TCP on the IP and port\n");
82 printf("-t <serverip[@port]> listen for TLS on IP and port\n");
83 printf("-x <server.key> server key file for TLS service\n");
84 printf("-y <server.pem> server cert file for TLS service\n");
85 printf("-z <verify.pem> cert file to verify client connections\n");
86 printf("-l long format for DNS printout\n");
87 printf("-v more verbose log output\n");
88 printf("-h this help text\n");
92 /** long format option, for multiline printout per message */
93 static int longformat = 0;
95 struct tap_socket_list;
97 /** main tap callback data */
98 struct main_tap_data {
99 /** the event base (to loopexit) */
100 struct ub_event_base* base;
101 /** the list of accept sockets */
102 struct tap_socket_list* acceptlist;
105 /** tap callback variables */
111 /** the SSL for TLS streams */
113 /** is the ssl handshake done */
114 int ssl_handshake_done;
115 /** we are briefly waiting to write (in the struct event) */
117 /** string that identifies the socket (or NULL), like IP address */
119 /** have we read the length, and how many bytes of it */
121 /** have we read the data, and how many bytes of it */
123 /** are we reading a control frame */
125 /** are we bi-directional (if false, uni-directional) */
126 int is_bidirectional;
127 /** data of the frame */
129 /** length of this frame */
133 /** list of sockets */
134 struct tap_socket_list {
136 struct tap_socket_list* next;
138 struct tap_socket* s;
145 /** the event for it */
147 /** has the event been added */
149 /** the callback, for the event, ev_cb(fd, bits, arg) */
150 void (*ev_cb)(int, short, void*);
151 /** data element, (arg for the tap_socket struct) */
153 /** socketpath, if this is an AF_LOCAL socket */
155 /** IP, if this is a TCP socket */
157 /** for a TLS socket, the tls context */
161 /** del the tap event */
162 static void tap_socket_delev(struct tap_socket* s)
166 if(!s->ev_added) return;
171 /** close the tap socket */
172 static void tap_socket_close(struct tap_socket* s)
175 if(s->fd == -1) return;
180 /** delete tap socket */
181 static void tap_socket_delete(struct tap_socket* s)
185 SSL_CTX_free(s->sslctx);
187 ub_event_free(s->ev);
193 /** create new socket (unconnected, not base-added), or NULL malloc fail */
194 static struct tap_socket* tap_socket_new_local(char* socketpath,
195 void (*ev_cb)(int, short, void*), void* data)
197 struct tap_socket* s = calloc(1, sizeof(*s));
199 log_err("malloc failure");
202 s->socketpath = strdup(socketpath);
205 log_err("malloc failure");
214 /** create new socket (unconnected, not base-added), or NULL malloc fail */
215 static struct tap_socket* tap_socket_new_tcpaccept(char* ip,
216 void (*ev_cb)(int, short, void*), void* data)
218 struct tap_socket* s = calloc(1, sizeof(*s));
220 log_err("malloc failure");
226 log_err("malloc failure");
235 /** create new socket (unconnected, not base-added), or NULL malloc fail */
236 static struct tap_socket* tap_socket_new_tlsaccept(char* ip,
237 void (*ev_cb)(int, short, void*), void* data, char* server_key,
238 char* server_cert, char* verifypem)
240 struct tap_socket* s = calloc(1, sizeof(*s));
242 log_err("malloc failure");
248 log_err("malloc failure");
254 s->sslctx = listen_sslctx_create(server_key, server_cert, verifypem);
256 log_err("could not create ssl context");
264 /** setup tcp accept socket on IP string */
265 static int make_tcp_accept(char* ip)
270 struct sockaddr_storage addr;
274 memset(&addr, 0, sizeof(addr));
275 len = (socklen_t)sizeof(addr);
276 if(!extstrtoaddr(ip, &addr, &len, UNBOUND_DNS_PORT)) {
277 log_err("could not parse IP '%s'", ip);
281 if((s = socket(addr.ss_family, SOCK_STREAM, 0)) == -1) {
282 log_err("can't create socket: %s", sock_strerror(errno));
286 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
287 (socklen_t)sizeof(on)) < 0) {
288 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
289 sock_strerror(errno));
293 #endif /* SO_REUSEADDR */
294 if(bind(s, (struct sockaddr*)&addr, len) != 0) {
295 log_err_addr("can't bind socket", sock_strerror(errno),
300 if(!fd_set_nonblock(s)) {
304 if(listen(s, LISTEN_BACKLOG) == -1) {
305 log_err("can't listen: %s", sock_strerror(errno));
312 /** setup socket on event base */
313 static int tap_socket_setup(struct tap_socket* s, struct ub_event_base* base)
316 /* AF_LOCAL accept socket */
317 s->fd = create_local_accept_sock(s->socketpath, NULL, 0);
319 log_err("could not create local socket");
322 } else if(s->ip || s->sslctx) {
323 /* TCP accept socket */
324 s->fd = make_tcp_accept(s->ip);
326 log_err("could not create tcp socket");
330 s->ev = ub_event_new(base, s->fd, UB_EV_READ | UB_EV_PERSIST,
333 log_err("could not ub_event_new");
336 if(ub_event_add(s->ev, NULL) != 0) {
337 log_err("could not ub_event_add");
344 /** add tap socket to list */
345 static int tap_socket_list_insert(struct tap_socket_list** liststart,
346 struct tap_socket* s)
348 struct tap_socket_list* entry = (struct tap_socket_list*)
349 malloc(sizeof(*entry));
352 entry->next = *liststart;
358 /** delete the list */
359 static void tap_socket_list_delete(struct tap_socket_list* list)
361 struct tap_socket_list* e = list, *next;
364 tap_socket_delev(e->s);
365 tap_socket_close(e->s);
366 tap_socket_delete(e->s);
372 /** setup accept events */
373 static int tap_socket_list_addevs(struct tap_socket_list* list,
374 struct ub_event_base* base)
376 struct tap_socket_list* entry;
377 for(entry = list; entry; entry = entry->next) {
378 if(!tap_socket_setup(entry->s, base)) {
379 log_err("could not setup socket");
387 /** log control frame contents */
388 static void log_control_frame(uint8_t* pkt, size_t len)
391 if(verbosity == 0) return;
392 desc = fstrm_describe_control(pkt, len);
394 log_err("out of memory");
397 log_info("control frame %s", desc);
401 /** convert mtype to string */
402 static const char* mtype_to_str(enum _Dnstap__Message__Type mtype)
405 case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
407 case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
408 return "AUTH_RESPONSE";
409 case DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY:
410 return "RESOLVER_QUERY";
411 case DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE:
412 return "RESOLVER_RESPONSE";
413 case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
414 return "CLIENT_QUERY";
415 case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
416 return "CLIENT_RESPONSE";
417 case DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY:
418 return "FORWARDER_QUERY";
419 case DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE:
420 return "FORWARDER_RESPONSE";
421 case DNSTAP__MESSAGE__TYPE__STUB_QUERY:
423 case DNSTAP__MESSAGE__TYPE__STUB_RESPONSE:
424 return "STUB_RESPONSE";
427 return "unknown_message_type";
430 /** convert type address to a string ip4 or ip6, malloced or NULL on fail */
431 static char* str_of_addr(ProtobufCBinaryData address)
434 socklen_t len = sizeof(buf);
435 if(address.len == 4) {
436 if(inet_ntop(AF_INET, address.data, buf, len)!=0)
438 } else if(address.len == 16) {
439 if(inet_ntop(AF_INET6, address.data, buf, len)!=0)
445 /** convert message buffer (of dns bytes) to the first qname, type, class,
446 * malloced or NULL on fail */
447 static char* q_of_msg(ProtobufCBinaryData message)
450 /* header, name, type, class minimum to get the query tuple */
451 if(message.len < 12 + 1 + 4 + 4) return NULL;
452 if(LDNS_QDCOUNT(message.data) < 1) return NULL;
453 if(sldns_wire2str_rrquestion_buf(message.data+12, message.len-12,
454 buf, sizeof(buf)) != 0) {
455 /* remove trailing newline, tabs to spaces */
456 /* remove the newline: */
457 if(buf[0] != 0) buf[strlen(buf)-1]=0;
458 /* remove first tab (before type) */
459 if(strrchr(buf, '\t')) *strrchr(buf, '\t')=' ';
460 /* remove second tab (before class) */
461 if(strrchr(buf, '\t')) *strrchr(buf, '\t')=' ';
467 /** convert possible string or hex data to string. malloced or NULL */
468 static char* possible_str(ProtobufCBinaryData str)
472 for(i=0; i<str.len; i++) {
473 if(!isprint((unsigned char)str.data[i]))
477 char* res = malloc(str.len+1);
479 memmove(res, str.data, str.len);
484 const char* hex = "0123456789ABCDEF";
485 char* res = malloc(str.len*2+1);
487 for(i=0; i<str.len; i++) {
488 res[i*2] = hex[(str.data[i]&0xf0)>>4];
489 res[i*2+1] = hex[str.data[i]&0x0f];
498 /** convert timeval to string, malloced or NULL */
499 static char* tv_to_str(protobuf_c_boolean has_time_sec, uint64_t time_sec,
500 protobuf_c_boolean has_time_nsec, uint32_t time_nsec)
502 char buf[64], buf2[256];
505 memset(&tv, 0, sizeof(tv));
506 if(has_time_sec) tv.tv_sec = time_sec;
507 if(has_time_nsec) tv.tv_usec = time_nsec/1000;
510 time_t_sec = tv.tv_sec;
511 (void)ctime_r(&time_t_sec, buf);
512 snprintf(buf2, sizeof(buf2), "%u.%9.9u %s",
513 (unsigned)time_sec, (unsigned)time_nsec, buf);
517 /** log data frame contents */
518 static void log_data_frame(uint8_t* pkt, size_t len)
520 Dnstap__Dnstap* d = dnstap__dnstap__unpack(NULL, len, pkt);
521 const char* mtype = NULL;
522 char* maddr=NULL, *qinf=NULL;
524 log_err("could not unpack");
527 if(d->base.descriptor != &dnstap__dnstap__descriptor) {
528 log_err("wrong base descriptor");
529 dnstap__dnstap__free_unpacked(d, NULL);
532 if(d->type != DNSTAP__DNSTAP__TYPE__MESSAGE) {
533 log_err("dnstap type not type_message");
534 dnstap__dnstap__free_unpacked(d, NULL);
538 mtype = mtype_to_str(d->message->type);
539 if(d->message->has_query_address)
540 maddr = str_of_addr(d->message->query_address);
541 else if(d->message->has_response_address)
542 maddr = str_of_addr(d->message->response_address);
543 if(d->message->has_query_message)
544 qinf = q_of_msg(d->message->query_message);
545 else if(d->message->has_response_message)
546 qinf = q_of_msg(d->message->response_message);
552 printf("%s%s%s%s%s\n", mtype, (maddr?" ":""), (maddr?maddr:""),
553 (qinf?" ":""), (qinf?qinf:""));
558 char* id=NULL, *vs=NULL;
559 if(d->has_identity) {
560 id=possible_str(d->identity);
563 vs=possible_str(d->version);
566 printf("identity: %s%s%s\n", (id?id:""),
567 (id&&vs?" ":""), (vs?vs:""));
571 if(d->message && d->message->has_query_message &&
572 d->message->query_message.data) {
573 char* qmsg = sldns_wire2str_pkt(
574 d->message->query_message.data,
575 d->message->query_message.len);
577 printf("query_message:\n%s", qmsg);
581 if(d->message && d->message->has_query_time_sec) {
582 char* qtv = tv_to_str(d->message->has_query_time_sec,
583 d->message->query_time_sec,
584 d->message->has_query_time_nsec,
585 d->message->query_time_nsec);
587 printf("query_time: %s\n", qtv);
591 if(d->message && d->message->has_response_message &&
592 d->message->response_message.data) {
593 char* rmsg = sldns_wire2str_pkt(
594 d->message->response_message.data,
595 d->message->response_message.len);
597 printf("response_message:\n%s", rmsg);
601 if(d->message && d->message->has_response_time_sec) {
602 char* rtv = tv_to_str(d->message->has_response_time_sec,
603 d->message->response_time_sec,
604 d->message->has_response_time_nsec,
605 d->message->response_time_nsec);
607 printf("response_time: %s\n", rtv);
613 dnstap__dnstap__free_unpacked(d, NULL);
615 #endif /* USE_DNSTAP */
617 /** receive bytes from fd, prints errors if bad,
618 * returns 0: closed/error, -1: continue, >0 number of bytes */
619 static ssize_t receive_bytes(struct tap_data* data, int fd, void* buf,
622 ssize_t ret = recv(fd, buf, len, MSG_DONTWAIT);
625 if(verbosity) log_info("dnstap client stream closed from %s",
626 (data->id?data->id:""));
628 } else if(ret == -1) {
631 if(errno == EINTR || errno == EAGAIN)
633 #else /* USE_WINSOCK */
634 if(WSAGetLastError() == WSAEINPROGRESS)
636 if(WSAGetLastError() == WSAEWOULDBLOCK) {
637 ub_winsock_tcp_wouldblock(data->ev, UB_EV_READ);
641 log_err("could not recv: %s", sock_strerror(errno));
642 if(verbosity) log_info("dnstap client stream closed from %s",
643 (data->id?data->id:""));
649 /* define routine for have_ssl only to avoid unused function warning */
651 /** set to wait briefly for a write event, for one event call */
652 static void tap_enable_brief_write(struct tap_data* data)
654 ub_event_del(data->ev);
655 ub_event_del_bits(data->ev, UB_EV_READ);
656 ub_event_add_bits(data->ev, UB_EV_WRITE);
657 if(ub_event_add(data->ev, NULL) != 0)
658 log_err("could not ub_event_add in tap_enable_brief_write");
659 data->ssl_brief_write = 1;
661 #endif /* HAVE_SSL */
663 /* define routine for have_ssl only to avoid unused function warning */
665 /** stop the brief wait for a write event. back to reading. */
666 static void tap_disable_brief_write(struct tap_data* data)
668 ub_event_del(data->ev);
669 ub_event_del_bits(data->ev, UB_EV_WRITE);
670 ub_event_add_bits(data->ev, UB_EV_READ);
671 if(ub_event_add(data->ev, NULL) != 0)
672 log_err("could not ub_event_add in tap_disable_brief_write");
673 data->ssl_brief_write = 0;
675 #endif /* HAVE_SSL */
678 /** receive bytes over ssl stream, prints errors if bad,
679 * returns 0: closed/error, -1: continue, >0 number of bytes */
680 static ssize_t ssl_read_bytes(struct tap_data* data, void* buf, size_t len)
684 r = SSL_read(data->ssl, buf, len);
686 int want = SSL_get_error(data->ssl, r);
687 if(want == SSL_ERROR_ZERO_RETURN) {
689 if(verbosity) log_info("dnstap client stream closed from %s",
690 (data->id?data->id:""));
692 } else if(want == SSL_ERROR_WANT_READ) {
695 } else if(want == SSL_ERROR_WANT_WRITE) {
696 /* set to briefly write */
697 tap_enable_brief_write(data);
699 } else if(want == SSL_ERROR_SYSCALL) {
701 if(errno == ECONNRESET && verbosity < 2)
702 return 0; /* silence reset by peer */
705 log_err("SSL_read syscall: %s",
707 if(verbosity) log_info("dnstap client stream closed from %s",
708 (data->id?data->id:""));
711 log_crypto_err("could not SSL_read");
712 if(verbosity) log_info("dnstap client stream closed from %s",
713 (data->id?data->id:""));
718 #endif /* HAVE_SSL */
720 /** receive bytes on the tap connection, prints errors if bad,
721 * returns 0: closed/error, -1: continue, >0 number of bytes */
722 static ssize_t tap_receive(struct tap_data* data, void* buf, size_t len)
726 return ssl_read_bytes(data, buf, len);
728 return receive_bytes(data, data->fd, buf, len);
731 /** delete the tap structure */
732 static void tap_data_free(struct tap_data* data)
734 ub_event_del(data->ev);
735 ub_event_free(data->ev);
745 /** reply with ACCEPT control frame to bidirectional client,
746 * returns 0 on error */
747 static int reply_with_accept(struct tap_data* data)
750 /* len includes the escape and framelength */
753 void* acceptframe = fstrm_create_control_frame_accept(
754 DNSTAP_CONTENT_TYPE, &len);
756 log_err("out of memory");
760 fd_set_block(data->fd);
762 if((r=SSL_write(data->ssl, acceptframe, len)) <= 0) {
763 if(SSL_get_error(data->ssl, r) == SSL_ERROR_ZERO_RETURN)
764 log_err("SSL_write, peer closed connection");
766 log_err("could not SSL_write");
767 fd_set_nonblock(data->fd);
772 if(send(data->fd, acceptframe, len, 0) == -1) {
773 log_err("send failed: %s", sock_strerror(errno));
774 fd_set_nonblock(data->fd);
779 if(verbosity) log_info("sent control frame(accept) content-type:(%s)",
780 DNSTAP_CONTENT_TYPE);
782 fd_set_nonblock(data->fd);
786 log_err("no dnstap compiled, no reply");
792 /** reply with FINISH control frame to bidirectional client,
793 * returns 0 on error */
794 static int reply_with_finish(struct tap_data* data)
798 void* finishframe = fstrm_create_control_frame_finish(&len);
800 log_err("out of memory");
804 fd_set_block(data->fd);
807 if((r=SSL_write(data->ssl, finishframe, len)) <= 0) {
808 if(SSL_get_error(data->ssl, r) == SSL_ERROR_ZERO_RETURN)
809 log_err("SSL_write, peer closed connection");
811 log_err("could not SSL_write");
812 fd_set_nonblock(data->fd);
817 if(send(data->fd, finishframe, len, 0) == -1) {
818 log_err("send failed: %s", sock_strerror(errno));
819 fd_set_nonblock(data->fd);
824 if(verbosity) log_info("sent control frame(finish)");
826 fd_set_nonblock(data->fd);
830 log_err("no dnstap compiled, no reply");
837 /** check SSL peer certificate, return 0 on fail */
838 static int tap_check_peer(struct tap_data* data)
840 if((SSL_get_verify_mode(data->ssl)&SSL_VERIFY_PEER)) {
842 if(SSL_get_verify_result(data->ssl) == X509_V_OK) {
843 X509* x = SSL_get_peer_certificate(data->ssl);
845 if(verbosity) log_info("SSL connection %s"
846 " failed no certificate", data->id);
850 log_cert(VERB_ALGO, "peer certificate", x);
851 #ifdef HAVE_SSL_GET0_PEERNAME
852 if(SSL_get0_peername(data->ssl)) {
853 if(verbosity) log_info("SSL connection %s "
854 "to %s authenticated", data->id,
855 SSL_get0_peername(data->ssl));
858 if(verbosity) log_info("SSL connection %s "
859 "authenticated", data->id);
860 #ifdef HAVE_SSL_GET0_PEERNAME
865 X509* x = SSL_get_peer_certificate(data->ssl);
868 log_cert(VERB_ALGO, "peer certificate", x);
871 if(verbosity) log_info("SSL connection %s failed: "
872 "failed to authenticate", data->id);
876 /* unauthenticated, the verify peer flag was not set
877 * in ssl when the ssl object was created from ssl_ctx */
878 if(verbosity) log_info("SSL connection %s", data->id);
882 #endif /* HAVE_SSL */
885 /** perform SSL handshake, return 0 to wait for events, 1 if done */
886 static int tap_handshake(struct tap_data* data)
889 if(data->ssl_brief_write) {
890 /* write condition has been satisfied, back to reading */
891 tap_disable_brief_write(data);
893 if(data->ssl_handshake_done)
897 r = SSL_do_handshake(data->ssl);
899 int want = SSL_get_error(data->ssl, r);
900 if(want == SSL_ERROR_WANT_READ) {
902 } else if(want == SSL_ERROR_WANT_WRITE) {
903 tap_enable_brief_write(data);
909 } else if(want == SSL_ERROR_SYSCALL) {
910 /* SYSCALL and errno==0 means closed uncleanly */
913 if(errno == EPIPE && verbosity < 2)
914 silent = 1; /* silence 'broken pipe' */
917 if(errno == ECONNRESET && verbosity < 2)
918 silent = 1; /* silence reset by peer */
923 log_err("SSL_handshake syscall: %s",
928 unsigned long err = ERR_get_error();
929 if(!squelch_err_ssl_handshake(err)) {
930 log_crypto_err_code("ssl handshake failed",
932 verbose(VERB_OPS, "ssl handshake failed "
933 "from %s", data->id);
939 /* check peer verification */
940 data->ssl_handshake_done = 1;
941 if(!tap_check_peer(data)) {
948 #endif /* HAVE_SSL */
950 /** callback for dnstap listener */
951 void dtio_tap_callback(int ATTR_UNUSED(fd), short ATTR_UNUSED(bits), void* arg)
953 struct tap_data* data = (struct tap_data*)arg;
954 if(verbosity>=3) log_info("tap callback");
956 if(data->ssl && (!data->ssl_handshake_done ||
957 data->ssl_brief_write)) {
958 if(!tap_handshake(data))
962 while(data->len_done < 4) {
963 uint32_t l = (uint32_t)data->len;
964 ssize_t ret = tap_receive(data,
965 ((uint8_t*)&l)+data->len_done, 4-data->len_done);
966 if(verbosity>=4) log_info("s recv %d", (int)ret);
968 /* closed or error */
971 } else if(ret == -1) {
975 data->len_done += ret;
976 data->len = (size_t)l;
977 if(data->len_done < 4)
978 return; /* continue later */
979 data->len = (size_t)(ntohl(l));
980 if(verbosity>=3) log_info("length is %d", (int)data->len);
982 /* it is a control frame */
983 data->control_frame = 1;
984 /* read controlframelen */
987 /* allocate frame size */
988 data->frame = calloc(1, data->len);
990 log_err("out of memory");
997 /* we want to read the full length now */
998 if(data->data_done < data->len) {
999 ssize_t r = tap_receive(data, data->frame + data->data_done,
1000 data->len - data->data_done);
1001 if(verbosity>=4) log_info("f recv %d", (int)r);
1003 /* closed or error */
1004 tap_data_free(data);
1006 } else if(r == -1) {
1007 /* continue later */
1010 data->data_done += r;
1011 if(data->data_done < data->len)
1012 return; /* continue later */
1015 /* we are done with a frame */
1016 if(verbosity>=3) log_info("received %sframe len %d",
1017 (data->control_frame?"control ":""), (int)data->len);
1019 if(data->control_frame)
1020 log_control_frame(data->frame, data->len);
1021 else log_data_frame(data->frame, data->len);
1024 if(data->len >= 4 && sldns_read_uint32(data->frame) ==
1025 FSTRM_CONTROL_FRAME_READY) {
1026 data->is_bidirectional = 1;
1027 if(verbosity) log_info("bidirectional stream");
1028 if(!reply_with_accept(data)) {
1029 tap_data_free(data);
1032 } else if(data->len >= 4 && sldns_read_uint32(data->frame) ==
1033 FSTRM_CONTROL_FRAME_STOP && data->is_bidirectional) {
1034 if(!reply_with_finish(data)) {
1035 tap_data_free(data);
1040 /* prepare for next frame */
1043 data->control_frame = 0;
1046 data->data_done = 0;
1050 /** callback for main listening file descriptor */
1051 void dtio_mainfdcallback(int fd, short ATTR_UNUSED(bits), void* arg)
1053 struct tap_socket* tap_sock = (struct tap_socket*)arg;
1054 struct main_tap_data* maindata = (struct main_tap_data*)
1056 struct tap_data* data;
1058 struct sockaddr_storage addr;
1059 socklen_t addrlen = (socklen_t)sizeof(addr);
1060 int s = accept(fd, (struct sockaddr*)&addr, &addrlen);
1063 /* EINTR is signal interrupt. others are closed connection. */
1064 if( errno == EINTR || errno == EAGAIN
1066 || errno == EWOULDBLOCK
1069 || errno == ECONNABORTED
1076 #else /* USE_WINSOCK */
1077 if(WSAGetLastError() == WSAEINPROGRESS ||
1078 WSAGetLastError() == WSAECONNRESET)
1080 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1081 ub_winsock_tcp_wouldblock(maindata->ev, UB_EV_READ);
1085 log_err_addr("accept failed", sock_strerror(errno), &addr,
1091 if(addr.ss_family == AF_LOCAL) {
1092 #ifdef HAVE_SYS_UN_H
1093 struct sockaddr_un* usock = calloc(1, sizeof(struct sockaddr_un) + 1);
1095 socklen_t ulen = sizeof(struct sockaddr_un);
1096 if(getsockname(fd, (struct sockaddr*)usock, &ulen) != -1) {
1097 log_info("accepted new dnstap client from %s", usock->sun_path);
1098 id = strdup(usock->sun_path);
1100 log_info("accepted new dnstap client");
1104 log_info("accepted new dnstap client");
1106 #endif /* HAVE_SYS_UN_H */
1107 } else if(addr.ss_family == AF_INET ||
1108 addr.ss_family == AF_INET6) {
1110 addr_to_str(&addr, addrlen, ip, sizeof(ip));
1111 log_info("accepted new dnstap client from %s", ip);
1114 log_info("accepted new dnstap client");
1118 data = calloc(1, sizeof(*data));
1119 if(!data) fatal_exit("out of memory");
1122 if(tap_sock->sslctx) {
1123 data->ssl = incoming_ssl_fd(tap_sock->sslctx, data->fd);
1124 if(!data->ssl) fatal_exit("could not SSL_new");
1126 data->ev = ub_event_new(maindata->base, s, UB_EV_READ | UB_EV_PERSIST,
1127 &dtio_tap_callback, data);
1128 if(!data->ev) fatal_exit("could not ub_event_new");
1129 if(ub_event_add(data->ev, NULL) != 0) fatal_exit("could not ub_event_add");
1132 /** setup local accept sockets */
1133 static void setup_local_list(struct main_tap_data* maindata,
1134 struct config_strlist_head* local_list)
1136 struct config_strlist* item;
1137 for(item = local_list->first; item; item = item->next) {
1138 struct tap_socket* s;
1139 s = tap_socket_new_local(item->str, &dtio_mainfdcallback,
1141 if(!s) fatal_exit("out of memory");
1142 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1143 fatal_exit("out of memory");
1147 /** setup tcp accept sockets */
1148 static void setup_tcp_list(struct main_tap_data* maindata,
1149 struct config_strlist_head* tcp_list)
1151 struct config_strlist* item;
1152 for(item = tcp_list->first; item; item = item->next) {
1153 struct tap_socket* s;
1154 s = tap_socket_new_tcpaccept(item->str, &dtio_mainfdcallback,
1156 if(!s) fatal_exit("out of memory");
1157 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1158 fatal_exit("out of memory");
1162 /** setup tls accept sockets */
1163 static void setup_tls_list(struct main_tap_data* maindata,
1164 struct config_strlist_head* tls_list, char* server_key,
1165 char* server_cert, char* verifypem)
1167 struct config_strlist* item;
1168 for(item = tls_list->first; item; item = item->next) {
1169 struct tap_socket* s;
1170 s = tap_socket_new_tlsaccept(item->str, &dtio_mainfdcallback,
1171 maindata, server_key, server_cert, verifypem);
1172 if(!s) fatal_exit("out of memory");
1173 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1174 fatal_exit("out of memory");
1178 /** signal variable */
1179 static struct ub_event_base* sig_base = NULL;
1180 /** do we have to quit */
1182 /** signal handler for user quit */
1183 static RETSIGTYPE main_sigh(int sig)
1186 char str[] = "exit on signal \n";
1187 str[15] = '0' + (sig/10)%10;
1188 str[16] = '0' + sig%10;
1189 /* simple cast to void will not silence Wunused-result */
1190 (void)!write(STDERR_FILENO, str, strlen(str));
1193 ub_event_base_loopexit(sig_base);
1199 /** setup and run the server to listen to DNSTAP messages */
1201 setup_and_run(struct config_strlist_head* local_list,
1202 struct config_strlist_head* tcp_list,
1203 struct config_strlist_head* tls_list, char* server_key,
1204 char* server_cert, char* verifypem)
1208 struct main_tap_data* maindata;
1209 struct ub_event_base* base;
1210 const char *evnm="event", *evsys="", *evmethod="";
1212 maindata = calloc(1, sizeof(*maindata));
1213 if(!maindata) fatal_exit("out of memory");
1214 memset(&now, 0, sizeof(now));
1215 base = ub_default_event_base(1, &secs, &now);
1216 if(!base) fatal_exit("could not create ub_event base");
1217 maindata->base = base;
1220 ub_event_base_free(base);
1224 ub_get_event_sys(base, &evnm, &evsys, &evmethod);
1225 if(verbosity) log_info("%s %s uses %s method", evnm, evsys, evmethod);
1227 setup_local_list(maindata, local_list);
1228 setup_tcp_list(maindata, tcp_list);
1229 setup_tls_list(maindata, tls_list, server_key, server_cert,
1231 if(!tap_socket_list_addevs(maindata->acceptlist, base))
1232 fatal_exit("could not setup accept events");
1233 if(verbosity) log_info("start of service");
1235 ub_event_base_dispatch(base);
1238 if(verbosity) log_info("end of service");
1239 tap_socket_list_delete(maindata->acceptlist);
1240 ub_event_base_free(base);
1244 /** getopt global, in case header files fail to declare it. */
1246 /** getopt global, in case header files fail to declare it. */
1247 extern char* optarg;
1249 /** main program for streamtcp */
1250 int main(int argc, char** argv)
1254 struct config_strlist_head local_list;
1255 struct config_strlist_head tcp_list;
1256 struct config_strlist_head tls_list;
1257 char* server_key = NULL, *server_cert = NULL, *verifypem = NULL;
1260 if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) {
1261 printf("WSAStartup failed\n");
1265 if(signal(SIGINT, main_sigh) == SIG_ERR ||
1267 signal(SIGQUIT, main_sigh) == SIG_ERR ||
1270 signal(SIGHUP, main_sigh) == SIG_ERR ||
1273 signal(SIGBREAK, main_sigh) == SIG_ERR ||
1275 signal(SIGTERM, main_sigh) == SIG_ERR)
1276 fatal_exit("could not bind to signal");
1277 memset(&local_list, 0, sizeof(local_list));
1278 memset(&tcp_list, 0, sizeof(tcp_list));
1279 memset(&tls_list, 0, sizeof(tls_list));
1281 /* lock debug start (if any) */
1283 log_ident_set("unbound-dnstap-socket");
1287 if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
1288 perror("could not install signal handler for SIGPIPE");
1293 /* command line options */
1294 while( (c=getopt(argc, argv, "hls:t:u:vx:y:z:")) != -1) {
1297 if(!cfg_strlist_append(&local_list,
1299 fatal_exit("out of memory");
1302 if(!cfg_strlist_append(&tcp_list,
1304 fatal_exit("out of memory");
1307 if(!cfg_strlist_append(&tls_list,
1309 fatal_exit("out of memory");
1313 server_key = optarg;
1317 server_cert = optarg;
1341 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
1342 ERR_load_SSL_strings();
1344 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
1346 OpenSSL_add_all_algorithms();
1349 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
1350 | OPENSSL_INIT_ADD_ALL_DIGESTS
1351 | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
1353 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
1354 (void)SSL_library_init();
1356 (void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
1358 #endif /* HAVE_SSL */
1360 setup_and_run(&local_list, &tcp_list, &tls_list, server_key,
1361 server_cert, verifypem);
1362 config_delstrlist(local_list.first);
1363 config_delstrlist(tcp_list.first);
1364 config_delstrlist(tls_list.first);
1373 /***--- definitions to make fptr_wlist work. ---***/
1374 /* These are callbacks, similar to smallapp callbacks, except the debug
1375 * tool callbacks are not in it */
1378 #include "util/data/packed_rrset.h"
1379 #include "daemon/worker.h"
1380 #include "daemon/remote.h"
1381 #include "util/fptr_wlist.h"
1382 #include "libunbound/context.h"
1384 void worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1385 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1386 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1391 int worker_handle_request(struct comm_point* ATTR_UNUSED(c),
1392 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1393 struct comm_reply* ATTR_UNUSED(repinfo))
1399 int worker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1400 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1401 struct comm_reply* ATTR_UNUSED(reply_info))
1407 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c),
1408 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1409 struct comm_reply* ATTR_UNUSED(repinfo))
1415 int remote_control_callback(struct comm_point* ATTR_UNUSED(c),
1416 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1417 struct comm_reply* ATTR_UNUSED(repinfo))
1423 void worker_sighandler(int ATTR_UNUSED(sig), void* ATTR_UNUSED(arg))
1428 struct outbound_entry* worker_send_query(
1429 struct query_info* ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags),
1430 int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1431 int ATTR_UNUSED(nocaps), int ATTR_UNUSED(check_ratelimit),
1432 struct sockaddr_storage* ATTR_UNUSED(addr),
1433 socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1434 size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(tcp_upstream),
1435 int ATTR_UNUSED(ssl_upstream), char* ATTR_UNUSED(tls_auth_name),
1436 struct module_qstate* ATTR_UNUSED(q), int* ATTR_UNUSED(was_ratelimited))
1442 #ifdef UB_ON_WINDOWS
1444 worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), void*
1450 wsvc_cron_cb(void* ATTR_UNUSED(arg))
1454 #endif /* UB_ON_WINDOWS */
1457 worker_alloc_cleanup(void* ATTR_UNUSED(arg))
1462 struct outbound_entry* libworker_send_query(
1463 struct query_info* ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags),
1464 int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1465 int ATTR_UNUSED(nocaps), int ATTR_UNUSED(check_ratelimit),
1466 struct sockaddr_storage* ATTR_UNUSED(addr),
1467 socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1468 size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(tcp_upstream),
1469 int ATTR_UNUSED(ssl_upstream), char* ATTR_UNUSED(tls_auth_name),
1470 struct module_qstate* ATTR_UNUSED(q), int* ATTR_UNUSED(was_ratelimited))
1476 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1477 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1478 struct comm_reply* ATTR_UNUSED(reply_info))
1484 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1485 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1486 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1491 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1492 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1493 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1498 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1499 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1500 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1505 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1506 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1507 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1512 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1518 void worker_stat_timer_cb(void* ATTR_UNUSED(arg))
1523 void worker_probe_timer_cb(void* ATTR_UNUSED(arg))
1528 void worker_start_accept(void* ATTR_UNUSED(arg))
1533 void worker_stop_accept(void* ATTR_UNUSED(arg))
1538 /** keep track of lock id in lock-verify application */
1540 /** the thread id that created it */
1542 /** the instance number of creation */
1546 int order_lock_cmp(const void* e1, const void* e2)
1548 const struct order_id* o1 = e1;
1549 const struct order_id* o2 = e2;
1550 if(o1->thr < o2->thr) return -1;
1551 if(o1->thr > o2->thr) return 1;
1552 if(o1->instance < o2->instance) return -1;
1553 if(o1->instance > o2->instance) return 1;
1558 codeline_cmp(const void* a, const void* b)
1560 return strcmp(a, b);
1563 int replay_var_compare(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1569 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg))