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"
65 #include <protobuf-c/protobuf-c.h>
66 #include "dnstap/dnstap.pb-c.h"
67 #endif /* USE_DNSTAP */
68 #include "util/config_file.h"
70 /** listen backlog on TCP connections for dnstap logs */
71 #define LISTEN_BACKLOG 16
73 /** usage information for streamtcp */
74 static void usage(char* argv[])
76 printf("usage: %s [options]\n", argv[0]);
77 printf(" Listen to dnstap messages\n");
78 printf("stdout has dnstap log, stderr has verbose server log\n");
79 printf("-u <socketpath> listen to unix socket with this file name\n");
80 printf("-s <serverip[@port]> listen for TCP on the IP and port\n");
81 printf("-t <serverip[@port]> listen for TLS on IP and port\n");
82 printf("-x <server.key> server key file for TLS service\n");
83 printf("-y <server.pem> server cert file for TLS service\n");
84 printf("-z <verify.pem> cert file to verify client connections\n");
85 printf("-l long format for DNS printout\n");
86 printf("-v more verbose log output\n");
87 printf("-h this help text\n");
91 /** long format option, for multiline printout per message */
92 static int longformat = 0;
94 struct tap_socket_list;
96 /** main tap callback data */
97 struct main_tap_data {
98 /** the event base (to loopexit) */
99 struct ub_event_base* base;
100 /** the list of accept sockets */
101 struct tap_socket_list* acceptlist;
104 /** tap callback variables */
110 /** the SSL for TLS streams */
112 /** is the ssl handshake done */
113 int ssl_handshake_done;
114 /** we are briefly waiting to write (in the struct event) */
116 /** string that identifies the socket (or NULL), like IP address */
118 /** have we read the length, and how many bytes of it */
120 /** have we read the data, and how many bytes of it */
122 /** are we reading a control frame */
124 /** are we bi-directional (if false, uni-directional) */
125 int is_bidirectional;
126 /** data of the frame */
128 /** length of this frame */
132 /** list of sockets */
133 struct tap_socket_list {
135 struct tap_socket_list* next;
137 struct tap_socket* s;
144 /** the event for it */
146 /** has the event been added */
148 /** the callback, for the event, ev_cb(fd, bits, arg) */
149 void (*ev_cb)(int, short, void*);
150 /** data element, (arg for the tap_socket struct) */
152 /** socketpath, if this is an AF_LOCAL socket */
154 /** IP, if this is a TCP socket */
156 /** for a TLS socket, the tls context */
160 /** del the tap event */
161 static void tap_socket_delev(struct tap_socket* s)
165 if(!s->ev_added) return;
170 /** close the tap socket */
171 static void tap_socket_close(struct tap_socket* s)
174 if(s->fd == -1) return;
179 /** delete tap socket */
180 static void tap_socket_delete(struct tap_socket* s)
184 SSL_CTX_free(s->sslctx);
186 ub_event_free(s->ev);
192 /** create new socket (unconnected, not base-added), or NULL malloc fail */
193 static struct tap_socket* tap_socket_new_local(char* socketpath,
194 void (*ev_cb)(int, short, void*), void* data)
196 struct tap_socket* s = calloc(1, sizeof(*s));
198 log_err("malloc failure");
201 s->socketpath = strdup(socketpath);
204 log_err("malloc failure");
213 /** create new socket (unconnected, not base-added), or NULL malloc fail */
214 static struct tap_socket* tap_socket_new_tcpaccept(char* ip,
215 void (*ev_cb)(int, short, void*), void* data)
217 struct tap_socket* s = calloc(1, sizeof(*s));
219 log_err("malloc failure");
225 log_err("malloc failure");
234 /** create new socket (unconnected, not base-added), or NULL malloc fail */
235 static struct tap_socket* tap_socket_new_tlsaccept(char* ip,
236 void (*ev_cb)(int, short, void*), void* data, char* server_key,
237 char* server_cert, char* verifypem)
239 struct tap_socket* s = calloc(1, sizeof(*s));
241 log_err("malloc failure");
247 log_err("malloc failure");
253 s->sslctx = listen_sslctx_create(server_key, server_cert, verifypem);
255 log_err("could not create ssl context");
263 /** setup tcp accept socket on IP string */
264 static int make_tcp_accept(char* ip)
269 struct sockaddr_storage addr;
273 memset(&addr, 0, sizeof(addr));
274 len = (socklen_t)sizeof(addr);
275 if(!extstrtoaddr(ip, &addr, &len)) {
276 log_err("could not parse IP '%s'", ip);
280 if((s = socket(addr.ss_family, SOCK_STREAM, 0)) == -1) {
281 log_err("can't create socket: %s", sock_strerror(errno));
285 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
286 (socklen_t)sizeof(on)) < 0) {
287 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
288 sock_strerror(errno));
292 #endif /* SO_REUSEADDR */
293 if(bind(s, (struct sockaddr*)&addr, len) != 0) {
294 log_err_addr("can't bind socket", sock_strerror(errno),
299 if(!fd_set_nonblock(s)) {
303 if(listen(s, LISTEN_BACKLOG) == -1) {
304 log_err("can't listen: %s", sock_strerror(errno));
311 /** setup socket on event base */
312 static int tap_socket_setup(struct tap_socket* s, struct ub_event_base* base)
315 /* AF_LOCAL accept socket */
316 s->fd = create_local_accept_sock(s->socketpath, NULL, 0);
318 log_err("could not create local socket");
321 } else if(s->ip || s->sslctx) {
322 /* TCP accept socket */
323 s->fd = make_tcp_accept(s->ip);
325 log_err("could not create tcp socket");
329 s->ev = ub_event_new(base, s->fd, UB_EV_READ | UB_EV_PERSIST,
332 log_err("could not ub_event_new");
335 if(ub_event_add(s->ev, NULL) != 0) {
336 log_err("could not ub_event_add");
343 /** add tap socket to list */
344 static int tap_socket_list_insert(struct tap_socket_list** liststart,
345 struct tap_socket* s)
347 struct tap_socket_list* entry = (struct tap_socket_list*)
348 malloc(sizeof(*entry));
351 entry->next = *liststart;
357 /** delete the list */
358 static void tap_socket_list_delete(struct tap_socket_list* list)
360 struct tap_socket_list* e = list, *next;
363 tap_socket_delev(e->s);
364 tap_socket_close(e->s);
365 tap_socket_delete(e->s);
371 /** setup accept events */
372 static int tap_socket_list_addevs(struct tap_socket_list* list,
373 struct ub_event_base* base)
375 struct tap_socket_list* entry;
376 for(entry = list; entry; entry = entry->next) {
377 if(!tap_socket_setup(entry->s, base)) {
378 log_err("could not setup socket");
386 /** log control frame contents */
387 static void log_control_frame(uint8_t* pkt, size_t len)
390 if(verbosity == 0) return;
391 desc = fstrm_describe_control(pkt, len);
393 log_err("out of memory");
396 log_info("control frame %s", desc);
400 /** convert mtype to string */
401 static const char* mtype_to_str(enum _Dnstap__Message__Type mtype)
404 case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
406 case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
407 return "AUTH_RESPONSE";
408 case DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY:
409 return "RESOLVER_QUERY";
410 case DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE:
411 return "RESOLVER_RESPONSE";
412 case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
413 return "CLIENT_QUERY";
414 case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
415 return "CLIENT_RESPONSE";
416 case DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY:
417 return "FORWARDER_QUERY";
418 case DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE:
419 return "FORWARDER_RESPONSE";
420 case DNSTAP__MESSAGE__TYPE__STUB_QUERY:
422 case DNSTAP__MESSAGE__TYPE__STUB_RESPONSE:
423 return "STUB_RESPONSE";
426 return "unknown_message_type";
429 /** convert type address to a string ip4 or ip6, malloced or NULL on fail */
430 static char* str_of_addr(ProtobufCBinaryData address)
433 socklen_t len = sizeof(buf);
434 if(address.len == 4) {
435 if(inet_ntop(AF_INET, address.data, buf, len)!=0)
437 } else if(address.len == 16) {
438 if(inet_ntop(AF_INET6, address.data, buf, len)!=0)
444 /** convert message buffer (of dns bytes) to the first qname, type, class,
445 * malloced or NULL on fail */
446 static char* q_of_msg(ProtobufCBinaryData message)
449 /* header, name, type, class minimum to get the query tuple */
450 if(message.len < 12 + 1 + 4 + 4) return NULL;
451 if(sldns_wire2str_rrquestion_buf(message.data+12, message.len-12,
452 buf, sizeof(buf)) != 0) {
453 /* remove trailing newline, tabs to spaces */
454 /* remove the newline: */
455 if(buf[0] != 0) buf[strlen(buf)-1]=0;
456 /* remove first tab (before type) */
457 if(strrchr(buf, '\t')) *strrchr(buf, '\t')=' ';
458 /* remove second tab (before class) */
459 if(strrchr(buf, '\t')) *strrchr(buf, '\t')=' ';
465 /** convert possible string or hex data to string. malloced or NULL */
466 static char* possible_str(ProtobufCBinaryData str)
470 for(i=0; i<str.len; i++) {
471 if(!isprint((unsigned char)str.data[i]))
475 char* res = malloc(str.len+1);
477 memmove(res, str.data, str.len);
482 const char* hex = "0123456789ABCDEF";
483 char* res = malloc(str.len*2+1);
485 for(i=0; i<str.len; i++) {
486 res[i*2] = hex[(str.data[i]&0xf0)>>4];
487 res[i*2+1] = hex[str.data[i]&0x0f];
496 /** convert timeval to string, malloced or NULL */
497 static char* tv_to_str(protobuf_c_boolean has_time_sec, uint64_t time_sec,
498 protobuf_c_boolean has_time_nsec, uint32_t time_nsec)
500 char buf[64], buf2[256];
503 memset(&tv, 0, sizeof(tv));
504 if(has_time_sec) tv.tv_sec = time_sec;
505 if(has_time_nsec) tv.tv_usec = time_nsec;
508 time_t_sec = tv.tv_sec;
509 (void)ctime_r(&time_t_sec, buf);
510 snprintf(buf2, sizeof(buf2), "%u.%9.9u %s",
511 (unsigned)time_sec, (unsigned)time_nsec, buf);
515 /** log data frame contents */
516 static void log_data_frame(uint8_t* pkt, size_t len)
518 Dnstap__Dnstap* d = dnstap__dnstap__unpack(NULL, len, pkt);
519 const char* mtype = NULL;
520 char* maddr=NULL, *qinf=NULL;
522 log_err("could not unpack");
525 if(d->base.descriptor != &dnstap__dnstap__descriptor) {
526 log_err("wrong base descriptor");
527 dnstap__dnstap__free_unpacked(d, NULL);
530 if(d->type != DNSTAP__DNSTAP__TYPE__MESSAGE) {
531 log_err("dnstap type not type_message");
532 dnstap__dnstap__free_unpacked(d, NULL);
536 mtype = mtype_to_str(d->message->type);
537 if(d->message->has_query_address)
538 maddr = str_of_addr(d->message->query_address);
539 else if(d->message->has_response_address)
540 maddr = str_of_addr(d->message->response_address);
541 if(d->message->has_query_message)
542 qinf = q_of_msg(d->message->query_message);
543 else if(d->message->has_response_message)
544 qinf = q_of_msg(d->message->response_message);
550 printf("%s%s%s%s%s\n", mtype, (maddr?" ":""), (maddr?maddr:""),
551 (qinf?" ":""), (qinf?qinf:""));
556 char* id=NULL, *vs=NULL;
557 if(d->has_identity) {
558 id=possible_str(d->identity);
561 vs=possible_str(d->version);
564 printf("identity: %s%s%s\n", (id?id:""),
565 (id&&vs?" ":""), (vs?vs:""));
569 if(d->message && d->message->has_query_message &&
570 d->message->query_message.data) {
571 char* qmsg = sldns_wire2str_pkt(
572 d->message->query_message.data,
573 d->message->query_message.len);
575 printf("query_message:\n%s", qmsg);
579 if(d->message && d->message->has_query_time_sec) {
580 char* qtv = tv_to_str(d->message->has_query_time_sec,
581 d->message->query_time_sec,
582 d->message->has_query_time_nsec,
583 d->message->query_time_nsec);
585 printf("query_time: %s\n", qtv);
589 if(d->message && d->message->has_response_message &&
590 d->message->response_message.data) {
591 char* rmsg = sldns_wire2str_pkt(
592 d->message->response_message.data,
593 d->message->response_message.len);
595 printf("response_message:\n%s", rmsg);
599 if(d->message && d->message->has_response_time_sec) {
600 char* rtv = tv_to_str(d->message->has_response_time_sec,
601 d->message->response_time_sec,
602 d->message->has_response_time_nsec,
603 d->message->response_time_nsec);
605 printf("response_time: %s\n", rtv);
611 dnstap__dnstap__free_unpacked(d, NULL);
613 #endif /* USE_DNSTAP */
615 /** receive bytes from fd, prints errors if bad,
616 * returns 0: closed/error, -1: continue, >0 number of bytes */
617 static ssize_t receive_bytes(struct tap_data* data, int fd, void* buf,
620 ssize_t ret = recv(fd, buf, len, 0);
623 if(verbosity) log_info("dnstap client stream closed from %s",
624 (data->id?data->id:""));
626 } else if(ret == -1) {
629 if(errno == EINTR || errno == EAGAIN)
631 #else /* USE_WINSOCK */
632 if(WSAGetLastError() == WSAEINPROGRESS)
634 if(WSAGetLastError() == WSAEWOULDBLOCK) {
635 ub_winsock_tcp_wouldblock(data->ev, UB_EV_READ);
639 log_err("could not recv: %s", sock_strerror(errno));
640 if(verbosity) log_info("dnstap client stream closed from %s",
641 (data->id?data->id:""));
647 /* define routine for have_ssl only to avoid unused function warning */
649 /** set to wait briefly for a write event, for one event call */
650 static void tap_enable_brief_write(struct tap_data* data)
652 ub_event_del(data->ev);
653 ub_event_del_bits(data->ev, UB_EV_READ);
654 ub_event_add_bits(data->ev, UB_EV_WRITE);
655 if(ub_event_add(data->ev, NULL) != 0)
656 log_err("could not ub_event_add in tap_enable_brief_write");
657 data->ssl_brief_write = 1;
659 #endif /* HAVE_SSL */
661 /* define routine for have_ssl only to avoid unused function warning */
663 /** stop the brief wait for a write event. back to reading. */
664 static void tap_disable_brief_write(struct tap_data* data)
666 ub_event_del(data->ev);
667 ub_event_del_bits(data->ev, UB_EV_WRITE);
668 ub_event_add_bits(data->ev, UB_EV_READ);
669 if(ub_event_add(data->ev, NULL) != 0)
670 log_err("could not ub_event_add in tap_disable_brief_write");
671 data->ssl_brief_write = 0;
673 #endif /* HAVE_SSL */
676 /** receive bytes over ssl stream, prints errors if bad,
677 * returns 0: closed/error, -1: continue, >0 number of bytes */
678 static ssize_t ssl_read_bytes(struct tap_data* data, void* buf, size_t len)
682 r = SSL_read(data->ssl, buf, len);
684 int want = SSL_get_error(data->ssl, r);
685 if(want == SSL_ERROR_ZERO_RETURN) {
687 if(verbosity) log_info("dnstap client stream closed from %s",
688 (data->id?data->id:""));
690 } else if(want == SSL_ERROR_WANT_READ) {
693 } else if(want == SSL_ERROR_WANT_WRITE) {
694 /* set to briefly write */
695 tap_enable_brief_write(data);
697 } else if(want == SSL_ERROR_SYSCALL) {
699 if(errno == ECONNRESET && verbosity < 2)
700 return 0; /* silence reset by peer */
703 log_err("SSL_read syscall: %s",
705 if(verbosity) log_info("dnstap client stream closed from %s",
706 (data->id?data->id:""));
709 log_crypto_err("could not SSL_read");
710 if(verbosity) log_info("dnstap client stream closed from %s",
711 (data->id?data->id:""));
716 #endif /* HAVE_SSL */
718 /** receive bytes on the tap connection, prints errors if bad,
719 * returns 0: closed/error, -1: continue, >0 number of bytes */
720 static ssize_t tap_receive(struct tap_data* data, void* buf, size_t len)
724 return ssl_read_bytes(data, buf, len);
726 return receive_bytes(data, data->fd, buf, len);
729 /** delete the tap structure */
730 void tap_data_free(struct tap_data* data)
732 ub_event_del(data->ev);
733 ub_event_free(data->ev);
743 /** reply with ACCEPT control frame to bidirectional client,
744 * returns 0 on error */
745 static int reply_with_accept(struct tap_data* data)
748 /* len includes the escape and framelength */
751 void* acceptframe = fstrm_create_control_frame_accept(
752 DNSTAP_CONTENT_TYPE, &len);
754 log_err("out of memory");
758 fd_set_block(data->fd);
760 if((r=SSL_write(data->ssl, acceptframe, len)) <= 0) {
761 if(SSL_get_error(data->ssl, r) == SSL_ERROR_ZERO_RETURN)
762 log_err("SSL_write, peer closed connection");
764 log_err("could not SSL_write");
765 fd_set_nonblock(data->fd);
770 if(send(data->fd, acceptframe, len, 0) == -1) {
771 log_err("send failed: %s", sock_strerror(errno));
772 fd_set_nonblock(data->fd);
777 if(verbosity) log_info("sent control frame(accept) content-type:(%s)",
778 DNSTAP_CONTENT_TYPE);
780 fd_set_nonblock(data->fd);
784 log_err("no dnstap compiled, no reply");
790 /** reply with FINISH control frame to bidirectional client,
791 * returns 0 on error */
792 static int reply_with_finish(int fd)
796 void* finishframe = fstrm_create_control_frame_finish(&len);
798 log_err("out of memory");
803 if(send(fd, finishframe, len, 0) == -1) {
804 log_err("send failed: %s", sock_strerror(errno));
809 if(verbosity) log_info("sent control frame(finish)");
815 log_err("no dnstap compiled, no reply");
822 /** check SSL peer certificate, return 0 on fail */
823 static int tap_check_peer(struct tap_data* data)
825 if((SSL_get_verify_mode(data->ssl)&SSL_VERIFY_PEER)) {
827 if(SSL_get_verify_result(data->ssl) == X509_V_OK) {
828 X509* x = SSL_get_peer_certificate(data->ssl);
830 if(verbosity) log_info("SSL connection %s"
831 " failed no certificate", data->id);
835 log_cert(VERB_ALGO, "peer certificate", x);
836 #ifdef HAVE_SSL_GET0_PEERNAME
837 if(SSL_get0_peername(data->ssl)) {
838 if(verbosity) log_info("SSL connection %s "
839 "to %s authenticated", data->id,
840 SSL_get0_peername(data->ssl));
843 if(verbosity) log_info("SSL connection %s "
844 "authenticated", data->id);
845 #ifdef HAVE_SSL_GET0_PEERNAME
850 X509* x = SSL_get_peer_certificate(data->ssl);
853 log_cert(VERB_ALGO, "peer certificate", x);
856 if(verbosity) log_info("SSL connection %s failed: "
857 "failed to authenticate", data->id);
861 /* unauthenticated, the verify peer flag was not set
862 * in ssl when the ssl object was created from ssl_ctx */
863 if(verbosity) log_info("SSL connection %s", data->id);
867 #endif /* HAVE_SSL */
870 /** perform SSL handshake, return 0 to wait for events, 1 if done */
871 static int tap_handshake(struct tap_data* data)
874 if(data->ssl_brief_write) {
875 /* write condition has been satisfied, back to reading */
876 tap_disable_brief_write(data);
878 if(data->ssl_handshake_done)
882 r = SSL_do_handshake(data->ssl);
884 int want = SSL_get_error(data->ssl, r);
885 if(want == SSL_ERROR_WANT_READ) {
887 } else if(want == SSL_ERROR_WANT_WRITE) {
888 tap_enable_brief_write(data);
894 } else if(want == SSL_ERROR_SYSCALL) {
895 /* SYSCALL and errno==0 means closed uncleanly */
898 if(errno == EPIPE && verbosity < 2)
899 silent = 1; /* silence 'broken pipe' */
902 if(errno == ECONNRESET && verbosity < 2)
903 silent = 1; /* silence reset by peer */
908 log_err("SSL_handshake syscall: %s",
913 unsigned long err = ERR_get_error();
914 if(!squelch_err_ssl_handshake(err)) {
915 log_crypto_err_code("ssl handshake failed",
917 verbose(VERB_OPS, "ssl handshake failed "
918 "from %s", data->id);
924 /* check peer verification */
925 data->ssl_handshake_done = 1;
926 if(!tap_check_peer(data)) {
933 #endif /* HAVE_SSL */
935 /** callback for dnstap listener */
936 void dtio_tap_callback(int fd, short ATTR_UNUSED(bits), void* arg)
938 struct tap_data* data = (struct tap_data*)arg;
939 if(verbosity>=3) log_info("tap callback");
941 if(data->ssl && (!data->ssl_handshake_done ||
942 data->ssl_brief_write)) {
943 if(!tap_handshake(data))
947 while(data->len_done < 4) {
948 uint32_t l = (uint32_t)data->len;
949 ssize_t ret = tap_receive(data,
950 ((uint8_t*)&l)+data->len_done, 4-data->len_done);
951 if(verbosity>=4) log_info("s recv %d", (int)ret);
953 /* closed or error */
956 } else if(ret == -1) {
960 data->len_done += ret;
961 data->len = (size_t)l;
962 if(data->len_done < 4)
963 return; /* continue later */
964 data->len = (size_t)(ntohl(l));
965 if(verbosity>=3) log_info("length is %d", (int)data->len);
967 /* it is a control frame */
968 data->control_frame = 1;
969 /* read controlframelen */
972 /* allocate frame size */
973 data->frame = calloc(1, data->len);
975 log_err("out of memory");
982 /* we want to read the full length now */
983 if(data->data_done < data->len) {
984 ssize_t r = tap_receive(data, data->frame + data->data_done,
985 data->len - data->data_done);
986 if(verbosity>=4) log_info("f recv %d", (int)r);
988 /* closed or error */
995 data->data_done += r;
996 if(data->data_done < data->len)
997 return; /* continue later */
1000 /* we are done with a frame */
1001 if(verbosity>=3) log_info("received %sframe len %d",
1002 (data->control_frame?"control ":""), (int)data->len);
1004 if(data->control_frame)
1005 log_control_frame(data->frame, data->len);
1006 else log_data_frame(data->frame, data->len);
1009 if(data->len >= 4 && sldns_read_uint32(data->frame) ==
1010 FSTRM_CONTROL_FRAME_READY) {
1011 data->is_bidirectional = 1;
1012 if(verbosity) log_info("bidirectional stream");
1013 if(!reply_with_accept(data)) {
1014 tap_data_free(data);
1016 } else if(data->len >= 4 && sldns_read_uint32(data->frame) ==
1017 FSTRM_CONTROL_FRAME_STOP && data->is_bidirectional) {
1018 if(!reply_with_finish(fd)) {
1019 tap_data_free(data);
1024 /* prepare for next frame */
1027 data->control_frame = 0;
1030 data->data_done = 0;
1034 /** callback for main listening file descriptor */
1035 void dtio_mainfdcallback(int fd, short ATTR_UNUSED(bits), void* arg)
1037 struct tap_socket* tap_sock = (struct tap_socket*)arg;
1038 struct main_tap_data* maindata = (struct main_tap_data*)
1040 struct tap_data* data;
1042 struct sockaddr_storage addr;
1043 socklen_t addrlen = (socklen_t)sizeof(addr);
1044 int s = accept(fd, (struct sockaddr*)&addr, &addrlen);
1047 /* EINTR is signal interrupt. others are closed connection. */
1048 if( errno == EINTR || errno == EAGAIN
1050 || errno == EWOULDBLOCK
1053 || errno == ECONNABORTED
1060 #else /* USE_WINSOCK */
1061 if(WSAGetLastError() == WSAEINPROGRESS ||
1062 WSAGetLastError() == WSAECONNRESET)
1064 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1065 ub_winsock_tcp_wouldblock(maindata->ev, UB_EV_READ);
1069 log_err_addr("accept failed", sock_strerror(errno), &addr,
1075 if(addr.ss_family == AF_LOCAL) {
1076 #ifdef HAVE_SYS_UN_H
1077 struct sockaddr_un* usock = calloc(1, sizeof(struct sockaddr_un) + 1);
1079 socklen_t ulen = sizeof(struct sockaddr_un);
1080 if(getsockname(fd, (struct sockaddr*)usock, &ulen) != -1) {
1081 log_info("accepted new dnstap client from %s", usock->sun_path);
1082 id = strdup(usock->sun_path);
1084 log_info("accepted new dnstap client");
1088 log_info("accepted new dnstap client");
1090 #endif /* HAVE_SYS_UN_H */
1091 } else if(addr.ss_family == AF_INET ||
1092 addr.ss_family == AF_INET6) {
1094 addr_to_str(&addr, addrlen, ip, sizeof(ip));
1095 log_info("accepted new dnstap client from %s", ip);
1098 log_info("accepted new dnstap client");
1102 data = calloc(1, sizeof(*data));
1103 if(!data) fatal_exit("out of memory");
1106 if(tap_sock->sslctx) {
1107 data->ssl = incoming_ssl_fd(tap_sock->sslctx, data->fd);
1108 if(!data->ssl) fatal_exit("could not SSL_new");
1110 data->ev = ub_event_new(maindata->base, s, UB_EV_READ | UB_EV_PERSIST,
1111 &dtio_tap_callback, data);
1112 if(!data->ev) fatal_exit("could not ub_event_new");
1113 if(ub_event_add(data->ev, NULL) != 0) fatal_exit("could not ub_event_add");
1116 /** setup local accept sockets */
1117 static void setup_local_list(struct main_tap_data* maindata,
1118 struct config_strlist_head* local_list)
1120 struct config_strlist* item;
1121 for(item = local_list->first; item; item = item->next) {
1122 struct tap_socket* s;
1123 s = tap_socket_new_local(item->str, &dtio_mainfdcallback,
1125 if(!s) fatal_exit("out of memory");
1126 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1127 fatal_exit("out of memory");
1131 /** setup tcp accept sockets */
1132 static void setup_tcp_list(struct main_tap_data* maindata,
1133 struct config_strlist_head* tcp_list)
1135 struct config_strlist* item;
1136 for(item = tcp_list->first; item; item = item->next) {
1137 struct tap_socket* s;
1138 s = tap_socket_new_tcpaccept(item->str, &dtio_mainfdcallback,
1140 if(!s) fatal_exit("out of memory");
1141 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1142 fatal_exit("out of memory");
1146 /** setup tls accept sockets */
1147 static void setup_tls_list(struct main_tap_data* maindata,
1148 struct config_strlist_head* tls_list, char* server_key,
1149 char* server_cert, char* verifypem)
1151 struct config_strlist* item;
1152 for(item = tls_list->first; item; item = item->next) {
1153 struct tap_socket* s;
1154 s = tap_socket_new_tlsaccept(item->str, &dtio_mainfdcallback,
1155 maindata, server_key, server_cert, verifypem);
1156 if(!s) fatal_exit("out of memory");
1157 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1158 fatal_exit("out of memory");
1162 /** signal variable */
1163 static struct ub_event_base* sig_base = NULL;
1164 /** do we have to quit */
1166 /** signal handler for user quit */
1167 static RETSIGTYPE main_sigh(int sig)
1169 verbose(VERB_ALGO, "exit on signal %d\n", sig);
1171 ub_event_base_loopexit(sig_base);
1177 /** setup and run the server to listen to DNSTAP messages */
1179 setup_and_run(struct config_strlist_head* local_list,
1180 struct config_strlist_head* tcp_list,
1181 struct config_strlist_head* tls_list, char* server_key,
1182 char* server_cert, char* verifypem)
1186 struct main_tap_data* maindata;
1187 struct ub_event_base* base;
1188 const char *evnm="event", *evsys="", *evmethod="";
1190 maindata = calloc(1, sizeof(*maindata));
1191 if(!maindata) fatal_exit("out of memory");
1192 memset(&now, 0, sizeof(now));
1193 base = ub_default_event_base(1, &secs, &now);
1194 if(!base) fatal_exit("could not create ub_event base");
1195 maindata->base = base;
1198 ub_event_base_free(base);
1202 ub_get_event_sys(base, &evnm, &evsys, &evmethod);
1203 if(verbosity) log_info("%s %s uses %s method", evnm, evsys, evmethod);
1205 setup_local_list(maindata, local_list);
1206 setup_tcp_list(maindata, tcp_list);
1207 setup_tls_list(maindata, tls_list, server_key, server_cert,
1209 if(!tap_socket_list_addevs(maindata->acceptlist, base))
1210 fatal_exit("could not setup accept events");
1211 if(verbosity) log_info("start of service");
1213 ub_event_base_dispatch(base);
1216 if(verbosity) log_info("end of service");
1217 tap_socket_list_delete(maindata->acceptlist);
1218 ub_event_base_free(base);
1222 /** getopt global, in case header files fail to declare it. */
1224 /** getopt global, in case header files fail to declare it. */
1225 extern char* optarg;
1227 /** main program for streamtcp */
1228 int main(int argc, char** argv)
1232 struct config_strlist_head local_list;
1233 struct config_strlist_head tcp_list;
1234 struct config_strlist_head tls_list;
1235 char* server_key = NULL, *server_cert = NULL, *verifypem = NULL;
1238 if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) {
1239 printf("WSAStartup failed\n");
1243 if(signal(SIGINT, main_sigh) == SIG_ERR ||
1245 signal(SIGQUIT, main_sigh) == SIG_ERR ||
1248 signal(SIGHUP, main_sigh) == SIG_ERR ||
1251 signal(SIGBREAK, main_sigh) == SIG_ERR ||
1253 signal(SIGTERM, main_sigh) == SIG_ERR)
1254 fatal_exit("could not bind to signal");
1255 memset(&local_list, 0, sizeof(local_list));
1256 memset(&tcp_list, 0, sizeof(tcp_list));
1257 memset(&tls_list, 0, sizeof(tls_list));
1259 /* lock debug start (if any) */
1260 log_ident_set("unbound-dnstap-socket");
1265 if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
1266 perror("could not install signal handler for SIGPIPE");
1271 /* command line options */
1272 while( (c=getopt(argc, argv, "hls:t:u:vx:y:z:")) != -1) {
1275 if(!cfg_strlist_append(&local_list,
1277 fatal_exit("out of memory");
1280 if(!cfg_strlist_append(&tcp_list,
1282 fatal_exit("out of memory");
1285 if(!cfg_strlist_append(&tls_list,
1287 fatal_exit("out of memory");
1291 server_key = optarg;
1295 server_cert = optarg;
1319 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
1320 ERR_load_SSL_strings();
1322 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
1324 OpenSSL_add_all_algorithms();
1327 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
1328 | OPENSSL_INIT_ADD_ALL_DIGESTS
1329 | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
1331 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
1332 (void)SSL_library_init();
1334 (void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
1336 #endif /* HAVE_SSL */
1338 setup_and_run(&local_list, &tcp_list, &tls_list, server_key,
1339 server_cert, verifypem);
1340 config_delstrlist(local_list.first);
1341 config_delstrlist(tcp_list.first);
1342 config_delstrlist(tls_list.first);
1351 /***--- definitions to make fptr_wlist work. ---***/
1352 /* These are callbacks, similar to smallapp callbacks, except the debug
1353 * tool callbacks are not in it */
1356 #include "util/data/packed_rrset.h"
1358 void worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1359 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1360 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1365 int worker_handle_request(struct comm_point* ATTR_UNUSED(c),
1366 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1367 struct comm_reply* ATTR_UNUSED(repinfo))
1373 int worker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1374 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1375 struct comm_reply* ATTR_UNUSED(reply_info))
1381 int worker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1382 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1383 struct comm_reply* ATTR_UNUSED(reply_info))
1389 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c),
1390 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1391 struct comm_reply* ATTR_UNUSED(repinfo))
1397 int remote_control_callback(struct comm_point* ATTR_UNUSED(c),
1398 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1399 struct comm_reply* ATTR_UNUSED(repinfo))
1405 void worker_sighandler(int ATTR_UNUSED(sig), void* ATTR_UNUSED(arg))
1410 struct outbound_entry* worker_send_query(
1411 struct query_info* ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags),
1412 int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1413 int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
1414 socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1415 size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(ssl_upstream),
1416 char* ATTR_UNUSED(tls_auth_name), struct module_qstate* ATTR_UNUSED(q))
1422 #ifdef UB_ON_WINDOWS
1424 worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), void*
1430 wsvc_cron_cb(void* ATTR_UNUSED(arg))
1434 #endif /* UB_ON_WINDOWS */
1437 worker_alloc_cleanup(void* ATTR_UNUSED(arg))
1442 struct outbound_entry* libworker_send_query(
1443 struct query_info* ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags),
1444 int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1445 int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
1446 socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1447 size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(ssl_upstream),
1448 char* ATTR_UNUSED(tls_auth_name), struct module_qstate* ATTR_UNUSED(q))
1454 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1455 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1456 struct comm_reply* ATTR_UNUSED(reply_info))
1462 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1463 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1464 struct comm_reply* ATTR_UNUSED(reply_info))
1470 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1471 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1472 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1477 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1478 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1479 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1484 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1485 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1486 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1491 void libworker_event_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 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1504 void worker_stat_timer_cb(void* ATTR_UNUSED(arg))
1509 void worker_probe_timer_cb(void* ATTR_UNUSED(arg))
1514 void worker_start_accept(void* ATTR_UNUSED(arg))
1519 void worker_stop_accept(void* ATTR_UNUSED(arg))
1524 /** keep track of lock id in lock-verify application */
1526 /** the thread id that created it */
1528 /** the instance number of creation */
1532 int order_lock_cmp(const void* e1, const void* e2)
1534 const struct order_id* o1 = e1;
1535 const struct order_id* o2 = e2;
1536 if(o1->thr < o2->thr) return -1;
1537 if(o1->thr > o2->thr) return 1;
1538 if(o1->instance < o2->instance) return -1;
1539 if(o1->instance > o2->instance) return 1;
1544 codeline_cmp(const void* a, const void* b)
1546 return strcmp(a, b);
1549 int replay_var_compare(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1555 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg))