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) {
282 log_err("can't create socket: %s", strerror(errno));
284 log_err("can't create socket: %s",
285 wsa_strerror(WSAGetLastError()));
290 if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
291 (socklen_t)sizeof(on)) < 0) {
293 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
297 log_err("setsockopt(.. SO_REUSEADDR ..) failed: %s",
298 wsa_strerror(WSAGetLastError()));
303 #endif /* SO_REUSEADDR */
304 if(bind(s, (struct sockaddr*)&addr, len) != 0) {
306 log_err_addr("can't bind socket", strerror(errno),
310 log_err_addr("can't bind socket",
311 wsa_strerror(WSAGetLastError()), &addr, len);
316 if(!fd_set_nonblock(s)) {
324 if(listen(s, LISTEN_BACKLOG) == -1) {
326 log_err("can't listen: %s", strerror(errno));
329 log_err("can't listen: %s", wsa_strerror(WSAGetLastError()));
337 /** setup socket on event base */
338 static int tap_socket_setup(struct tap_socket* s, struct ub_event_base* base)
341 /* AF_LOCAL accept socket */
342 s->fd = create_local_accept_sock(s->socketpath, NULL, 0);
344 log_err("could not create local socket");
347 } else if(s->ip || s->sslctx) {
348 /* TCP accept socket */
349 s->fd = make_tcp_accept(s->ip);
351 log_err("could not create tcp socket");
355 s->ev = ub_event_new(base, s->fd, UB_EV_READ | UB_EV_PERSIST,
358 log_err("could not ub_event_new");
361 if(ub_event_add(s->ev, NULL) != 0) {
362 log_err("could not ub_event_add");
369 /** add tap socket to list */
370 static int tap_socket_list_insert(struct tap_socket_list** liststart,
371 struct tap_socket* s)
373 struct tap_socket_list* entry = (struct tap_socket_list*)
374 malloc(sizeof(*entry));
377 entry->next = *liststart;
383 /** delete the list */
384 static void tap_socket_list_delete(struct tap_socket_list* list)
386 struct tap_socket_list* e = list, *next;
389 tap_socket_delev(e->s);
390 tap_socket_close(e->s);
391 tap_socket_delete(e->s);
397 /** setup accept events */
398 static int tap_socket_list_addevs(struct tap_socket_list* list,
399 struct ub_event_base* base)
401 struct tap_socket_list* entry;
402 for(entry = list; entry; entry = entry->next) {
403 if(!tap_socket_setup(entry->s, base)) {
404 log_err("could not setup socket");
412 /** log control frame contents */
413 static void log_control_frame(uint8_t* pkt, size_t len)
416 if(verbosity == 0) return;
417 desc = fstrm_describe_control(pkt, len);
419 log_err("out of memory");
422 log_info("control frame %s", desc);
426 /** convert mtype to string */
427 static const char* mtype_to_str(enum _Dnstap__Message__Type mtype)
430 case DNSTAP__MESSAGE__TYPE__AUTH_QUERY:
432 case DNSTAP__MESSAGE__TYPE__AUTH_RESPONSE:
433 return "AUTH_RESPONSE";
434 case DNSTAP__MESSAGE__TYPE__RESOLVER_QUERY:
435 return "RESOLVER_QUERY";
436 case DNSTAP__MESSAGE__TYPE__RESOLVER_RESPONSE:
437 return "RESOLVER_RESPONSE";
438 case DNSTAP__MESSAGE__TYPE__CLIENT_QUERY:
439 return "CLIENT_QUERY";
440 case DNSTAP__MESSAGE__TYPE__CLIENT_RESPONSE:
441 return "CLIENT_RESPONSE";
442 case DNSTAP__MESSAGE__TYPE__FORWARDER_QUERY:
443 return "FORWARDER_QUERY";
444 case DNSTAP__MESSAGE__TYPE__FORWARDER_RESPONSE:
445 return "FORWARDER_RESPONSE";
446 case DNSTAP__MESSAGE__TYPE__STUB_QUERY:
448 case DNSTAP__MESSAGE__TYPE__STUB_RESPONSE:
449 return "STUB_RESPONSE";
452 return "unknown_message_type";
455 /** convert type address to a string ip4 or ip6, malloced or NULL on fail */
456 static char* str_of_addr(ProtobufCBinaryData address)
459 socklen_t len = sizeof(buf);
460 if(address.len == 4) {
461 if(inet_ntop(AF_INET, address.data, buf, len)!=0)
463 } else if(address.len == 16) {
464 if(inet_ntop(AF_INET6, address.data, buf, len)!=0)
470 /** convert message buffer (of dns bytes) to the first qname, type, class,
471 * malloced or NULL on fail */
472 static char* q_of_msg(ProtobufCBinaryData message)
475 /* header, name, type, class minimum to get the query tuple */
476 if(message.len < 12 + 1 + 4 + 4) return NULL;
477 if(sldns_wire2str_rrquestion_buf(message.data+12, message.len-12,
478 buf, sizeof(buf)) != 0) {
479 /* remove trailing newline, tabs to spaces */
480 /* remove the newline: */
481 if(buf[0] != 0) buf[strlen(buf)-1]=0;
482 /* remove first tab (before type) */
483 if(strrchr(buf, '\t')) *strrchr(buf, '\t')=' ';
484 /* remove second tab (before class) */
485 if(strrchr(buf, '\t')) *strrchr(buf, '\t')=' ';
491 /** convert possible string or hex data to string. malloced or NULL */
492 static char* possible_str(ProtobufCBinaryData str)
496 for(i=0; i<str.len; i++) {
497 if(!isprint((unsigned char)str.data[i]))
501 char* res = malloc(str.len+1);
503 memmove(res, str.data, str.len);
508 const char* hex = "0123456789ABCDEF";
509 char* res = malloc(str.len*2+1);
511 for(i=0; i<str.len; i++) {
512 res[i*2] = hex[(str.data[i]&0xf0)>>4];
513 res[i*2+1] = hex[str.data[i]&0x0f];
522 /** convert timeval to string, malloced or NULL */
523 static char* tv_to_str(protobuf_c_boolean has_time_sec, uint64_t time_sec,
524 protobuf_c_boolean has_time_nsec, uint32_t time_nsec)
526 char buf[64], buf2[256];
529 memset(&tv, 0, sizeof(tv));
530 if(has_time_sec) tv.tv_sec = time_sec;
531 if(has_time_nsec) tv.tv_usec = time_nsec;
534 time_t_sec = tv.tv_sec;
535 (void)ctime_r(&time_t_sec, buf);
536 snprintf(buf2, sizeof(buf2), "%u.%9.9u %s",
537 (unsigned)time_sec, (unsigned)time_nsec, buf);
541 /** log data frame contents */
542 static void log_data_frame(uint8_t* pkt, size_t len)
544 Dnstap__Dnstap* d = dnstap__dnstap__unpack(NULL, len, pkt);
545 const char* mtype = NULL;
546 char* maddr=NULL, *qinf=NULL;
548 log_err("could not unpack");
551 if(d->base.descriptor != &dnstap__dnstap__descriptor) {
552 log_err("wrong base descriptor");
553 dnstap__dnstap__free_unpacked(d, NULL);
556 if(d->type != DNSTAP__DNSTAP__TYPE__MESSAGE) {
557 log_err("dnstap type not type_message");
558 dnstap__dnstap__free_unpacked(d, NULL);
562 mtype = mtype_to_str(d->message->type);
563 if(d->message->has_query_address)
564 maddr = str_of_addr(d->message->query_address);
565 else if(d->message->has_response_address)
566 maddr = str_of_addr(d->message->response_address);
567 if(d->message->has_query_message)
568 qinf = q_of_msg(d->message->query_message);
569 else if(d->message->has_response_message)
570 qinf = q_of_msg(d->message->response_message);
576 printf("%s%s%s%s%s\n", mtype, (maddr?" ":""), (maddr?maddr:""),
577 (qinf?" ":""), (qinf?qinf:""));
582 char* id=NULL, *vs=NULL;
583 if(d->has_identity) {
584 id=possible_str(d->identity);
587 vs=possible_str(d->version);
590 printf("identity: %s%s%s\n", (id?id:""),
591 (id&&vs?" ":""), (vs?vs:""));
595 if(d->message && d->message->has_query_message &&
596 d->message->query_message.data) {
597 char* qmsg = sldns_wire2str_pkt(
598 d->message->query_message.data,
599 d->message->query_message.len);
601 printf("query_message:\n%s", qmsg);
605 if(d->message && d->message->has_query_time_sec) {
606 char* qtv = tv_to_str(d->message->has_query_time_sec,
607 d->message->query_time_sec,
608 d->message->has_query_time_nsec,
609 d->message->query_time_nsec);
611 printf("query_time: %s\n", qtv);
615 if(d->message && d->message->has_response_message &&
616 d->message->response_message.data) {
617 char* rmsg = sldns_wire2str_pkt(
618 d->message->response_message.data,
619 d->message->response_message.len);
621 printf("response_message:\n%s", rmsg);
625 if(d->message && d->message->has_response_time_sec) {
626 char* rtv = tv_to_str(d->message->has_response_time_sec,
627 d->message->response_time_sec,
628 d->message->has_response_time_nsec,
629 d->message->response_time_nsec);
631 printf("response_time: %s\n", rtv);
637 dnstap__dnstap__free_unpacked(d, NULL);
639 #endif /* USE_DNSTAP */
641 /** receive bytes from fd, prints errors if bad,
642 * returns 0: closed/error, -1: continue, >0 number of bytes */
643 static ssize_t receive_bytes(struct tap_data* data, int fd, void* buf,
646 ssize_t ret = recv(fd, buf, len, 0);
649 if(verbosity) log_info("dnstap client stream closed from %s",
650 (data->id?data->id:""));
652 } else if(ret == -1) {
655 if(errno == EINTR || errno == EAGAIN)
657 log_err("could not recv: %s", strerror(errno));
658 #else /* USE_WINSOCK */
659 if(WSAGetLastError() == WSAEINPROGRESS)
661 if(WSAGetLastError() == WSAEWOULDBLOCK) {
662 ub_winsock_tcp_wouldblock(data->ev, UB_EV_READ);
665 log_err("could not recv: %s",
666 wsa_strerror(WSAGetLastError()));
668 if(verbosity) log_info("dnstap client stream closed from %s",
669 (data->id?data->id:""));
675 /* define routine for have_ssl only to avoid unused function warning */
677 /** set to wait briefly for a write event, for one event call */
678 static void tap_enable_brief_write(struct tap_data* data)
680 ub_event_del(data->ev);
681 ub_event_del_bits(data->ev, UB_EV_READ);
682 ub_event_add_bits(data->ev, UB_EV_WRITE);
683 if(ub_event_add(data->ev, NULL) != 0)
684 log_err("could not ub_event_add in tap_enable_brief_write");
685 data->ssl_brief_write = 1;
687 #endif /* HAVE_SSL */
689 /* define routine for have_ssl only to avoid unused function warning */
691 /** stop the brief wait for a write event. back to reading. */
692 static void tap_disable_brief_write(struct tap_data* data)
694 ub_event_del(data->ev);
695 ub_event_del_bits(data->ev, UB_EV_WRITE);
696 ub_event_add_bits(data->ev, UB_EV_READ);
697 if(ub_event_add(data->ev, NULL) != 0)
698 log_err("could not ub_event_add in tap_disable_brief_write");
699 data->ssl_brief_write = 0;
701 #endif /* HAVE_SSL */
704 /** receive bytes over ssl stream, prints errors if bad,
705 * returns 0: closed/error, -1: continue, >0 number of bytes */
706 static ssize_t ssl_read_bytes(struct tap_data* data, void* buf, size_t len)
710 r = SSL_read(data->ssl, buf, len);
712 int want = SSL_get_error(data->ssl, r);
713 if(want == SSL_ERROR_ZERO_RETURN) {
715 if(verbosity) log_info("dnstap client stream closed from %s",
716 (data->id?data->id:""));
718 } else if(want == SSL_ERROR_WANT_READ) {
721 } else if(want == SSL_ERROR_WANT_WRITE) {
722 /* set to briefly write */
723 tap_enable_brief_write(data);
725 } else if(want == SSL_ERROR_SYSCALL) {
727 if(errno == ECONNRESET && verbosity < 2)
728 return 0; /* silence reset by peer */
731 log_err("SSL_read syscall: %s",
733 if(verbosity) log_info("dnstap client stream closed from %s",
734 (data->id?data->id:""));
737 log_crypto_err("could not SSL_read");
738 if(verbosity) log_info("dnstap client stream closed from %s",
739 (data->id?data->id:""));
744 #endif /* HAVE_SSL */
746 /** receive bytes on the tap connection, prints errors if bad,
747 * returns 0: closed/error, -1: continue, >0 number of bytes */
748 static ssize_t tap_receive(struct tap_data* data, void* buf, size_t len)
752 return ssl_read_bytes(data, buf, len);
754 return receive_bytes(data, data->fd, buf, len);
757 /** delete the tap structure */
758 void tap_data_free(struct tap_data* data)
760 ub_event_del(data->ev);
761 ub_event_free(data->ev);
771 /** reply with ACCEPT control frame to bidirectional client,
772 * returns 0 on error */
773 static int reply_with_accept(struct tap_data* data)
776 /* len includes the escape and framelength */
779 void* acceptframe = fstrm_create_control_frame_accept(
780 DNSTAP_CONTENT_TYPE, &len);
782 log_err("out of memory");
786 fd_set_block(data->fd);
788 if((r=SSL_write(data->ssl, acceptframe, len)) <= 0) {
789 if(SSL_get_error(data->ssl, r) == SSL_ERROR_ZERO_RETURN)
790 log_err("SSL_write, peer closed connection");
792 log_err("could not SSL_write");
793 fd_set_nonblock(data->fd);
798 if(send(data->fd, acceptframe, len, 0) == -1) {
800 log_err("send failed: %s", strerror(errno));
802 log_err("send failed: %s",
803 wsa_strerror(WSAGetLastError()));
805 fd_set_nonblock(data->fd);
810 if(verbosity) log_info("sent control frame(accept) content-type:(%s)",
811 DNSTAP_CONTENT_TYPE);
813 fd_set_nonblock(data->fd);
817 log_err("no dnstap compiled, no reply");
823 /** reply with FINISH control frame to bidirectional client,
824 * returns 0 on error */
825 static int reply_with_finish(int fd)
829 void* finishframe = fstrm_create_control_frame_finish(&len);
831 log_err("out of memory");
836 if(send(fd, finishframe, len, 0) == -1) {
838 log_err("send failed: %s", strerror(errno));
840 log_err("send failed: %s", wsa_strerror(WSAGetLastError()));
846 if(verbosity) log_info("sent control frame(finish)");
852 log_err("no dnstap compiled, no reply");
859 /** check SSL peer certificate, return 0 on fail */
860 static int tap_check_peer(struct tap_data* data)
862 if((SSL_get_verify_mode(data->ssl)&SSL_VERIFY_PEER)) {
864 if(SSL_get_verify_result(data->ssl) == X509_V_OK) {
865 X509* x = SSL_get_peer_certificate(data->ssl);
867 if(verbosity) log_info("SSL connection %s"
868 " failed no certificate", data->id);
872 log_cert(VERB_ALGO, "peer certificate", x);
873 #ifdef HAVE_SSL_GET0_PEERNAME
874 if(SSL_get0_peername(data->ssl)) {
875 if(verbosity) log_info("SSL connection %s "
876 "to %s authenticated", data->id,
877 SSL_get0_peername(data->ssl));
880 if(verbosity) log_info("SSL connection %s "
881 "authenticated", data->id);
882 #ifdef HAVE_SSL_GET0_PEERNAME
887 X509* x = SSL_get_peer_certificate(data->ssl);
890 log_cert(VERB_ALGO, "peer certificate", x);
893 if(verbosity) log_info("SSL connection %s failed: "
894 "failed to authenticate", data->id);
898 /* unauthenticated, the verify peer flag was not set
899 * in ssl when the ssl object was created from ssl_ctx */
900 if(verbosity) log_info("SSL connection %s", data->id);
904 #endif /* HAVE_SSL */
907 /** perform SSL handshake, return 0 to wait for events, 1 if done */
908 static int tap_handshake(struct tap_data* data)
911 if(data->ssl_brief_write) {
912 /* write condition has been satisfied, back to reading */
913 tap_disable_brief_write(data);
915 if(data->ssl_handshake_done)
919 r = SSL_do_handshake(data->ssl);
921 int want = SSL_get_error(data->ssl, r);
922 if(want == SSL_ERROR_WANT_READ) {
924 } else if(want == SSL_ERROR_WANT_WRITE) {
925 tap_enable_brief_write(data);
931 } else if(want == SSL_ERROR_SYSCALL) {
932 /* SYSCALL and errno==0 means closed uncleanly */
935 if(errno == EPIPE && verbosity < 2)
936 silent = 1; /* silence 'broken pipe' */
939 if(errno == ECONNRESET && verbosity < 2)
940 silent = 1; /* silence reset by peer */
945 log_err("SSL_handshake syscall: %s",
950 unsigned long err = ERR_get_error();
951 if(!squelch_err_ssl_handshake(err)) {
952 log_crypto_err_code("ssl handshake failed",
954 verbose(VERB_OPS, "ssl handshake failed "
955 "from %s", data->id);
961 /* check peer verification */
962 data->ssl_handshake_done = 1;
963 if(!tap_check_peer(data)) {
970 #endif /* HAVE_SSL */
972 /** callback for dnstap listener */
973 void dtio_tap_callback(int fd, short ATTR_UNUSED(bits), void* arg)
975 struct tap_data* data = (struct tap_data*)arg;
976 if(verbosity>=3) log_info("tap callback");
978 if(data->ssl && (!data->ssl_handshake_done ||
979 data->ssl_brief_write)) {
980 if(!tap_handshake(data))
984 while(data->len_done < 4) {
985 uint32_t l = (uint32_t)data->len;
986 ssize_t ret = tap_receive(data,
987 ((uint8_t*)&l)+data->len_done, 4-data->len_done);
988 if(verbosity>=4) log_info("s recv %d", (int)ret);
990 /* closed or error */
993 } else if(ret == -1) {
997 data->len_done += ret;
998 data->len = (size_t)l;
999 if(data->len_done < 4)
1000 return; /* continue later */
1001 data->len = (size_t)(ntohl(l));
1002 if(verbosity>=3) log_info("length is %d", (int)data->len);
1003 if(data->len == 0) {
1004 /* it is a control frame */
1005 data->control_frame = 1;
1006 /* read controlframelen */
1009 /* allocate frame size */
1010 data->frame = calloc(1, data->len);
1012 log_err("out of memory");
1013 tap_data_free(data);
1019 /* we want to read the full length now */
1020 if(data->data_done < data->len) {
1021 ssize_t r = tap_receive(data, data->frame + data->data_done,
1022 data->len - data->data_done);
1023 if(verbosity>=4) log_info("f recv %d", (int)r);
1025 /* closed or error */
1026 tap_data_free(data);
1028 } else if(r == -1) {
1029 /* continue later */
1032 data->data_done += r;
1033 if(data->data_done < data->len)
1034 return; /* continue later */
1037 /* we are done with a frame */
1038 if(verbosity>=3) log_info("received %sframe len %d",
1039 (data->control_frame?"control ":""), (int)data->len);
1041 if(data->control_frame)
1042 log_control_frame(data->frame, data->len);
1043 else log_data_frame(data->frame, data->len);
1046 if(data->len >= 4 && sldns_read_uint32(data->frame) ==
1047 FSTRM_CONTROL_FRAME_READY) {
1048 data->is_bidirectional = 1;
1049 if(verbosity) log_info("bidirectional stream");
1050 if(!reply_with_accept(data)) {
1051 tap_data_free(data);
1053 } else if(data->len >= 4 && sldns_read_uint32(data->frame) ==
1054 FSTRM_CONTROL_FRAME_STOP && data->is_bidirectional) {
1055 if(!reply_with_finish(fd)) {
1056 tap_data_free(data);
1061 /* prepare for next frame */
1064 data->control_frame = 0;
1067 data->data_done = 0;
1071 /** callback for main listening file descriptor */
1072 void dtio_mainfdcallback(int fd, short ATTR_UNUSED(bits), void* arg)
1074 struct tap_socket* tap_sock = (struct tap_socket*)arg;
1075 struct main_tap_data* maindata = (struct main_tap_data*)
1077 struct tap_data* data;
1079 struct sockaddr_storage addr;
1080 socklen_t addrlen = (socklen_t)sizeof(addr);
1081 int s = accept(fd, (struct sockaddr*)&addr, &addrlen);
1084 /* EINTR is signal interrupt. others are closed connection. */
1085 if( errno == EINTR || errno == EAGAIN
1087 || errno == EWOULDBLOCK
1090 || errno == ECONNABORTED
1097 log_err_addr("accept failed", strerror(errno), &addr, addrlen);
1098 #else /* USE_WINSOCK */
1099 if(WSAGetLastError() == WSAEINPROGRESS ||
1100 WSAGetLastError() == WSAECONNRESET)
1102 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1103 ub_winsock_tcp_wouldblock(maindata->ev, UB_EV_READ);
1106 log_err_addr("accept failed", wsa_strerror(WSAGetLastError()),
1113 if(addr.ss_family == AF_LOCAL) {
1114 #ifdef HAVE_SYS_UN_H
1115 struct sockaddr_un* usock = calloc(1, sizeof(struct sockaddr_un) + 1);
1117 socklen_t ulen = sizeof(struct sockaddr_un);
1118 if(getsockname(fd, (struct sockaddr*)usock, &ulen) != -1) {
1119 log_info("accepted new dnstap client from %s", usock->sun_path);
1120 id = strdup(usock->sun_path);
1122 log_info("accepted new dnstap client");
1126 log_info("accepted new dnstap client");
1128 #endif /* HAVE_SYS_UN_H */
1129 } else if(addr.ss_family == AF_INET ||
1130 addr.ss_family == AF_INET6) {
1132 addr_to_str(&addr, addrlen, ip, sizeof(ip));
1133 log_info("accepted new dnstap client from %s", ip);
1136 log_info("accepted new dnstap client");
1140 data = calloc(1, sizeof(*data));
1141 if(!data) fatal_exit("out of memory");
1144 if(tap_sock->sslctx) {
1145 data->ssl = incoming_ssl_fd(tap_sock->sslctx, data->fd);
1146 if(!data->ssl) fatal_exit("could not SSL_new");
1148 data->ev = ub_event_new(maindata->base, s, UB_EV_READ | UB_EV_PERSIST,
1149 &dtio_tap_callback, data);
1150 if(!data->ev) fatal_exit("could not ub_event_new");
1151 if(ub_event_add(data->ev, NULL) != 0) fatal_exit("could not ub_event_add");
1154 /** setup local accept sockets */
1155 static void setup_local_list(struct main_tap_data* maindata,
1156 struct config_strlist_head* local_list)
1158 struct config_strlist* item;
1159 for(item = local_list->first; item; item = item->next) {
1160 struct tap_socket* s;
1161 s = tap_socket_new_local(item->str, &dtio_mainfdcallback,
1163 if(!s) fatal_exit("out of memory");
1164 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1165 fatal_exit("out of memory");
1169 /** setup tcp accept sockets */
1170 static void setup_tcp_list(struct main_tap_data* maindata,
1171 struct config_strlist_head* tcp_list)
1173 struct config_strlist* item;
1174 for(item = tcp_list->first; item; item = item->next) {
1175 struct tap_socket* s;
1176 s = tap_socket_new_tcpaccept(item->str, &dtio_mainfdcallback,
1178 if(!s) fatal_exit("out of memory");
1179 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1180 fatal_exit("out of memory");
1184 /** setup tls accept sockets */
1185 static void setup_tls_list(struct main_tap_data* maindata,
1186 struct config_strlist_head* tls_list, char* server_key,
1187 char* server_cert, char* verifypem)
1189 struct config_strlist* item;
1190 for(item = tls_list->first; item; item = item->next) {
1191 struct tap_socket* s;
1192 s = tap_socket_new_tlsaccept(item->str, &dtio_mainfdcallback,
1193 maindata, server_key, server_cert, verifypem);
1194 if(!s) fatal_exit("out of memory");
1195 if(!tap_socket_list_insert(&maindata->acceptlist, s))
1196 fatal_exit("out of memory");
1200 /** signal variable */
1201 static struct ub_event_base* sig_base = NULL;
1202 /** do we have to quit */
1204 /** signal handler for user quit */
1205 static RETSIGTYPE main_sigh(int sig)
1207 verbose(VERB_ALGO, "exit on signal %d\n", sig);
1209 ub_event_base_loopexit(sig_base);
1213 /** setup and run the server to listen to DNSTAP messages */
1215 setup_and_run(struct config_strlist_head* local_list,
1216 struct config_strlist_head* tcp_list,
1217 struct config_strlist_head* tls_list, char* server_key,
1218 char* server_cert, char* verifypem)
1222 struct main_tap_data* maindata;
1223 struct ub_event_base* base;
1224 const char *evnm="event", *evsys="", *evmethod="";
1226 maindata = calloc(1, sizeof(*maindata));
1227 if(!maindata) fatal_exit("out of memory");
1228 memset(&now, 0, sizeof(now));
1229 base = ub_default_event_base(1, &secs, &now);
1230 if(!base) fatal_exit("could not create ub_event base");
1231 maindata->base = base;
1234 ub_event_base_free(base);
1238 ub_get_event_sys(base, &evnm, &evsys, &evmethod);
1239 if(verbosity) log_info("%s %s uses %s method", evnm, evsys, evmethod);
1241 setup_local_list(maindata, local_list);
1242 setup_tcp_list(maindata, tcp_list);
1243 setup_tls_list(maindata, tls_list, server_key, server_cert,
1245 if(!tap_socket_list_addevs(maindata->acceptlist, base))
1246 fatal_exit("could not setup accept events");
1247 if(verbosity) log_info("start of service");
1249 ub_event_base_dispatch(base);
1251 if(verbosity) log_info("end of service");
1253 tap_socket_list_delete(maindata->acceptlist);
1254 ub_event_base_free(base);
1258 /** getopt global, in case header files fail to declare it. */
1260 /** getopt global, in case header files fail to declare it. */
1261 extern char* optarg;
1263 /** main program for streamtcp */
1264 int main(int argc, char** argv)
1268 struct config_strlist_head local_list;
1269 struct config_strlist_head tcp_list;
1270 struct config_strlist_head tls_list;
1271 char* server_key = NULL, *server_cert = NULL, *verifypem = NULL;
1274 if(WSAStartup(MAKEWORD(2,2), &wsa_data) != 0) {
1275 printf("WSAStartup failed\n");
1279 if(signal(SIGINT, main_sigh) == SIG_ERR ||
1281 signal(SIGQUIT, main_sigh) == SIG_ERR ||
1284 signal(SIGHUP, main_sigh) == SIG_ERR ||
1287 signal(SIGBREAK, main_sigh) == SIG_ERR ||
1289 signal(SIGTERM, main_sigh) == SIG_ERR)
1290 fatal_exit("could not bind to signal");
1291 memset(&local_list, 0, sizeof(local_list));
1292 memset(&tcp_list, 0, sizeof(tcp_list));
1293 memset(&tls_list, 0, sizeof(tls_list));
1295 /* lock debug start (if any) */
1296 log_ident_set("unbound-dnstap-socket");
1301 if(signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
1302 perror("could not install signal handler for SIGPIPE");
1307 /* command line options */
1308 while( (c=getopt(argc, argv, "hls:t:u:vx:y:z:")) != -1) {
1311 if(!cfg_strlist_append(&local_list,
1313 fatal_exit("out of memory");
1316 if(!cfg_strlist_append(&tcp_list,
1318 fatal_exit("out of memory");
1321 if(!cfg_strlist_append(&tls_list,
1323 fatal_exit("out of memory");
1327 server_key = optarg;
1331 server_cert = optarg;
1355 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
1356 ERR_load_SSL_strings();
1358 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_CRYPTO)
1360 OpenSSL_add_all_algorithms();
1363 OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
1364 | OPENSSL_INIT_ADD_ALL_DIGESTS
1365 | OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
1367 #if OPENSSL_VERSION_NUMBER < 0x10100000 || !defined(HAVE_OPENSSL_INIT_SSL)
1368 (void)SSL_library_init();
1370 (void)OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
1372 #endif /* HAVE_SSL */
1374 setup_and_run(&local_list, &tcp_list, &tls_list, server_key,
1375 server_cert, verifypem);
1376 config_delstrlist(local_list.first);
1377 config_delstrlist(tcp_list.first);
1378 config_delstrlist(tls_list.first);
1387 /***--- definitions to make fptr_wlist work. ---***/
1388 /* These are callbacks, similar to smallapp callbacks, except the debug
1389 * tool callbacks are not in it */
1392 #include "util/data/packed_rrset.h"
1394 void worker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1395 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1396 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1401 int worker_handle_request(struct comm_point* ATTR_UNUSED(c),
1402 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1403 struct comm_reply* ATTR_UNUSED(repinfo))
1409 int worker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1410 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1411 struct comm_reply* ATTR_UNUSED(reply_info))
1417 int worker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1418 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1419 struct comm_reply* ATTR_UNUSED(reply_info))
1425 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c),
1426 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1427 struct comm_reply* ATTR_UNUSED(repinfo))
1433 int remote_control_callback(struct comm_point* ATTR_UNUSED(c),
1434 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1435 struct comm_reply* ATTR_UNUSED(repinfo))
1441 void worker_sighandler(int ATTR_UNUSED(sig), void* ATTR_UNUSED(arg))
1446 struct outbound_entry* worker_send_query(
1447 struct query_info* ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags),
1448 int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1449 int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
1450 socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1451 size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(ssl_upstream),
1452 char* ATTR_UNUSED(tls_auth_name), struct module_qstate* ATTR_UNUSED(q))
1458 #ifdef UB_ON_WINDOWS
1460 worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), void*
1466 wsvc_cron_cb(void* ATTR_UNUSED(arg))
1470 #endif /* UB_ON_WINDOWS */
1473 worker_alloc_cleanup(void* ATTR_UNUSED(arg))
1478 struct outbound_entry* libworker_send_query(
1479 struct query_info* ATTR_UNUSED(qinfo), uint16_t ATTR_UNUSED(flags),
1480 int ATTR_UNUSED(dnssec), int ATTR_UNUSED(want_dnssec),
1481 int ATTR_UNUSED(nocaps), struct sockaddr_storage* ATTR_UNUSED(addr),
1482 socklen_t ATTR_UNUSED(addrlen), uint8_t* ATTR_UNUSED(zone),
1483 size_t ATTR_UNUSED(zonelen), int ATTR_UNUSED(ssl_upstream),
1484 char* ATTR_UNUSED(tls_auth_name), struct module_qstate* ATTR_UNUSED(q))
1490 int libworker_handle_reply(struct comm_point* ATTR_UNUSED(c),
1491 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1492 struct comm_reply* ATTR_UNUSED(reply_info))
1498 int libworker_handle_service_reply(struct comm_point* ATTR_UNUSED(c),
1499 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
1500 struct comm_reply* ATTR_UNUSED(reply_info))
1506 void libworker_handle_control_cmd(struct tube* ATTR_UNUSED(tube),
1507 uint8_t* ATTR_UNUSED(buffer), size_t ATTR_UNUSED(len),
1508 int ATTR_UNUSED(error), void* ATTR_UNUSED(arg))
1513 void libworker_fg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1514 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1515 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1520 void libworker_bg_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1521 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1522 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1527 void libworker_event_done_cb(void* ATTR_UNUSED(arg), int ATTR_UNUSED(rcode),
1528 struct sldns_buffer* ATTR_UNUSED(buf), enum sec_status ATTR_UNUSED(s),
1529 char* ATTR_UNUSED(why_bogus), int ATTR_UNUSED(was_ratelimited))
1534 int context_query_cmp(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1540 void worker_stat_timer_cb(void* ATTR_UNUSED(arg))
1545 void worker_probe_timer_cb(void* ATTR_UNUSED(arg))
1550 void worker_start_accept(void* ATTR_UNUSED(arg))
1555 void worker_stop_accept(void* ATTR_UNUSED(arg))
1560 /** keep track of lock id in lock-verify application */
1562 /** the thread id that created it */
1564 /** the instance number of creation */
1568 int order_lock_cmp(const void* e1, const void* e2)
1570 const struct order_id* o1 = e1;
1571 const struct order_id* o2 = e2;
1572 if(o1->thr < o2->thr) return -1;
1573 if(o1->thr > o2->thr) return 1;
1574 if(o1->instance < o2->instance) return -1;
1575 if(o1->instance > o2->instance) return 1;
1580 codeline_cmp(const void* a, const void* b)
1582 return strcmp(a, b);
1585 int replay_var_compare(const void* ATTR_UNUSED(a), const void* ATTR_UNUSED(b))
1591 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg))