2 * util/netevent.c - event notification
4 * Copyright (c) 2007, 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 file contains event notification functions.
42 #include "util/netevent.h"
43 #include "util/ub_event.h"
45 #include "util/net_help.h"
46 #include "util/tcp_conn_limit.h"
47 #include "util/fptr_wlist.h"
48 #include "sldns/pkthdr.h"
49 #include "sldns/sbuffer.h"
50 #include "sldns/str2wire.h"
51 #include "dnstap/dnstap.h"
52 #include "dnscrypt/dnscrypt.h"
53 #include "services/listen_dnsport.h"
54 #ifdef HAVE_OPENSSL_SSL_H
55 #include <openssl/ssl.h>
57 #ifdef HAVE_OPENSSL_ERR_H
58 #include <openssl/err.h>
61 /* -------- Start of local definitions -------- */
62 /** if CMSG_ALIGN is not defined on this platform, a workaround */
65 # define CMSG_ALIGN(n) __CMSG_ALIGN(n)
66 # elif defined(CMSG_DATA_ALIGN)
67 # define CMSG_ALIGN _CMSG_DATA_ALIGN
69 # define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
73 /** if CMSG_LEN is not defined on this platform, a workaround */
75 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
78 /** if CMSG_SPACE is not defined on this platform, a workaround */
80 # ifdef _CMSG_HDR_ALIGN
81 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)))
83 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr)))
87 /** The TCP writing query timeout in milliseconds */
88 #define TCP_QUERY_TIMEOUT 120000
89 /** The minimum actual TCP timeout to use, regardless of what we advertise,
91 #define TCP_QUERY_TIMEOUT_MINIMUM 200
93 #ifndef NONBLOCKING_IS_BROKEN
94 /** number of UDP reads to perform per read indication from select */
95 #define NUM_UDP_PER_SELECT 100
97 #define NUM_UDP_PER_SELECT 1
101 * The internal event structure for keeping ub_event info for the event.
102 * Possibly other structures (list, tree) this is part of.
104 struct internal_event {
106 struct comm_base* base;
107 /** ub_event event type */
112 * Internal base structure, so that every thread has its own events.
114 struct internal_base {
115 /** ub_event event_base type. */
116 struct ub_event_base* base;
117 /** seconds time pointer points here */
119 /** timeval with current time */
121 /** the event used for slow_accept timeouts */
122 struct ub_event* slow_accept;
123 /** true if slow_accept is enabled */
124 int slow_accept_enabled;
128 * Internal timer structure, to store timer event in.
130 struct internal_timer {
131 /** the super struct from which derived */
132 struct comm_timer super;
134 struct comm_base* base;
135 /** ub_event event type */
137 /** is timer enabled */
142 * Internal signal structure, to store signal event in.
144 struct internal_signal {
145 /** ub_event event type */
147 /** next in signal list */
148 struct internal_signal* next;
151 /** create a tcp handler with a parent */
152 static struct comm_point* comm_point_create_tcp_handler(
153 struct comm_base *base, struct comm_point* parent, size_t bufsize,
154 struct sldns_buffer* spoolbuf, comm_point_callback_type* callback,
157 /* -------- End of local definitions -------- */
160 comm_base_create(int sigs)
162 struct comm_base* b = (struct comm_base*)calloc(1,
163 sizeof(struct comm_base));
164 const char *evnm="event", *evsys="", *evmethod="";
168 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
173 b->eb->base = ub_default_event_base(sigs, &b->eb->secs, &b->eb->now);
180 ub_get_event_sys(b->eb->base, &evnm, &evsys, &evmethod);
181 verbose(VERB_ALGO, "%s %s uses %s method.", evnm, evsys, evmethod);
186 comm_base_create_event(struct ub_event_base* base)
188 struct comm_base* b = (struct comm_base*)calloc(1,
189 sizeof(struct comm_base));
192 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
203 comm_base_delete(struct comm_base* b)
207 if(b->eb->slow_accept_enabled) {
208 if(ub_event_del(b->eb->slow_accept) != 0) {
209 log_err("could not event_del slow_accept");
211 ub_event_free(b->eb->slow_accept);
213 ub_event_base_free(b->eb->base);
220 comm_base_delete_no_base(struct comm_base* b)
224 if(b->eb->slow_accept_enabled) {
225 if(ub_event_del(b->eb->slow_accept) != 0) {
226 log_err("could not event_del slow_accept");
228 ub_event_free(b->eb->slow_accept);
236 comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv)
243 comm_base_dispatch(struct comm_base* b)
246 retval = ub_event_base_dispatch(b->eb->base);
248 fatal_exit("event_dispatch returned error %d, "
249 "errno is %s", retval, strerror(errno));
253 void comm_base_exit(struct comm_base* b)
255 if(ub_event_base_loopexit(b->eb->base) != 0) {
256 log_err("Could not loopexit");
260 void comm_base_set_slow_accept_handlers(struct comm_base* b,
261 void (*stop_acc)(void*), void (*start_acc)(void*), void* arg)
263 b->stop_accept = stop_acc;
264 b->start_accept = start_acc;
268 struct ub_event_base* comm_base_internal(struct comm_base* b)
273 /** see if errno for udp has to be logged or not uses globals */
275 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
277 /* do not log transient errors (unless high verbosity) */
278 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN)
292 if(verbosity < VERB_ALGO)
298 /* permission denied is gotten for every send if the
299 * network is disconnected (on some OS), squelch it */
300 if( ((errno == EPERM)
301 # ifdef EADDRNOTAVAIL
302 /* 'Cannot assign requested address' also when disconnected */
303 || (errno == EADDRNOTAVAIL)
305 ) && verbosity < VERB_DETAIL)
308 /* If SO_REUSEADDR is set, we could try to connect to the same server
309 * from the same source port twice. */
310 if(errno == EADDRINUSE && verbosity < VERB_DETAIL)
313 /* squelch errors where people deploy AAAA ::ffff:bla for
314 * authority servers, which we try for intranets. */
315 if(errno == EINVAL && addr_is_ip4mapped(
316 (struct sockaddr_storage*)addr, addrlen) &&
317 verbosity < VERB_DETAIL)
319 /* SO_BROADCAST sockopt can give access to 255.255.255.255,
320 * but a dns cache does not need it. */
321 if(errno == EACCES && addr_is_broadcast(
322 (struct sockaddr_storage*)addr, addrlen) &&
323 verbosity < VERB_DETAIL)
328 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
330 return udp_send_errno_needs_log(addr, addrlen);
333 /* send a UDP reply */
335 comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
336 struct sockaddr* addr, socklen_t addrlen)
339 log_assert(c->fd != -1);
341 if(sldns_buffer_remaining(packet) == 0)
342 log_err("error: send empty UDP packet");
345 log_assert(addr && addrlen > 0);
346 sent = sendto(c->fd, (void*)sldns_buffer_begin(packet),
347 sldns_buffer_remaining(packet), 0,
350 sent = send(c->fd, (void*)sldns_buffer_begin(packet),
351 sldns_buffer_remaining(packet), 0);
354 /* try again and block, waiting for IO to complete,
355 * we want to send the answer, and we will wait for
356 * the ethernet interface buffer to have space. */
358 if(errno == EAGAIN ||
360 errno == EWOULDBLOCK ||
364 if(WSAGetLastError() == WSAEINPROGRESS ||
365 WSAGetLastError() == WSAENOBUFS ||
366 WSAGetLastError() == WSAEWOULDBLOCK) {
370 sent = sendto(c->fd, (void*)sldns_buffer_begin(packet),
371 sldns_buffer_remaining(packet), 0,
374 fd_set_nonblock(c->fd);
379 if(!udp_send_errno_needs_log(addr, addrlen))
381 verbose(VERB_OPS, "sendto failed: %s", sock_strerror(errno));
382 log_addr(VERB_OPS, "remote address is",
383 (struct sockaddr_storage*)addr, addrlen);
385 } else if((size_t)sent != sldns_buffer_remaining(packet)) {
386 log_err("sent %d in place of %d bytes",
387 (int)sent, (int)sldns_buffer_remaining(packet));
393 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && (defined(HAVE_RECVMSG) || defined(HAVE_SENDMSG))
394 /** print debug ancillary info */
395 static void p_ancil(const char* str, struct comm_reply* r)
397 if(r->srctype != 4 && r->srctype != 6) {
398 log_info("%s: unknown srctype %d", str, r->srctype);
401 if(r->srctype == 6) {
403 if(inet_ntop(AF_INET6, &r->pktinfo.v6info.ipi6_addr,
404 buf, (socklen_t)sizeof(buf)) == 0) {
405 (void)strlcpy(buf, "(inet_ntop error)", sizeof(buf));
407 buf[sizeof(buf)-1]=0;
408 log_info("%s: %s %d", str, buf, r->pktinfo.v6info.ipi6_ifindex);
409 } else if(r->srctype == 4) {
411 char buf1[1024], buf2[1024];
412 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_addr,
413 buf1, (socklen_t)sizeof(buf1)) == 0) {
414 (void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1));
416 buf1[sizeof(buf1)-1]=0;
417 #ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST
418 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_spec_dst,
419 buf2, (socklen_t)sizeof(buf2)) == 0) {
420 (void)strlcpy(buf2, "(inet_ntop error)", sizeof(buf2));
422 buf2[sizeof(buf2)-1]=0;
426 log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex,
428 #elif defined(IP_RECVDSTADDR)
430 if(inet_ntop(AF_INET, &r->pktinfo.v4addr,
431 buf1, (socklen_t)sizeof(buf1)) == 0) {
432 (void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1));
434 buf1[sizeof(buf1)-1]=0;
435 log_info("%s: %s", str, buf1);
436 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */
439 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */
441 /** send a UDP reply over specified interface*/
443 comm_point_send_udp_msg_if(struct comm_point *c, sldns_buffer* packet,
444 struct sockaddr* addr, socklen_t addrlen, struct comm_reply* r)
446 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG)
455 struct cmsghdr *cmsg;
456 #endif /* S_SPLINT_S */
458 log_assert(c->fd != -1);
460 if(sldns_buffer_remaining(packet) == 0)
461 log_err("error: send empty UDP packet");
463 log_assert(addr && addrlen > 0);
466 msg.msg_namelen = addrlen;
467 iov[0].iov_base = sldns_buffer_begin(packet);
468 iov[0].iov_len = sldns_buffer_remaining(packet);
471 msg.msg_control = control.buf;
473 msg.msg_controllen = sizeof(control.buf);
474 #endif /* S_SPLINT_S */
478 cmsg = CMSG_FIRSTHDR(&msg);
479 if(r->srctype == 4) {
482 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
483 log_assert(msg.msg_controllen <= sizeof(control.buf));
484 cmsg->cmsg_level = IPPROTO_IP;
485 cmsg->cmsg_type = IP_PKTINFO;
486 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4info,
487 sizeof(struct in_pktinfo));
488 /* unset the ifindex to not bypass the routing tables */
489 cmsg_data = CMSG_DATA(cmsg);
490 ((struct in_pktinfo *) cmsg_data)->ipi_ifindex = 0;
491 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
492 #elif defined(IP_SENDSRCADDR)
493 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
494 log_assert(msg.msg_controllen <= sizeof(control.buf));
495 cmsg->cmsg_level = IPPROTO_IP;
496 cmsg->cmsg_type = IP_SENDSRCADDR;
497 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4addr,
498 sizeof(struct in_addr));
499 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
501 verbose(VERB_ALGO, "no IP_PKTINFO or IP_SENDSRCADDR");
502 msg.msg_control = NULL;
503 #endif /* IP_PKTINFO or IP_SENDSRCADDR */
504 } else if(r->srctype == 6) {
506 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
507 log_assert(msg.msg_controllen <= sizeof(control.buf));
508 cmsg->cmsg_level = IPPROTO_IPV6;
509 cmsg->cmsg_type = IPV6_PKTINFO;
510 memmove(CMSG_DATA(cmsg), &r->pktinfo.v6info,
511 sizeof(struct in6_pktinfo));
512 /* unset the ifindex to not bypass the routing tables */
513 cmsg_data = CMSG_DATA(cmsg);
514 ((struct in6_pktinfo *) cmsg_data)->ipi6_ifindex = 0;
515 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
517 /* try to pass all 0 to use default route */
518 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
519 log_assert(msg.msg_controllen <= sizeof(control.buf));
520 cmsg->cmsg_level = IPPROTO_IPV6;
521 cmsg->cmsg_type = IPV6_PKTINFO;
522 memset(CMSG_DATA(cmsg), 0, sizeof(struct in6_pktinfo));
523 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
525 #endif /* S_SPLINT_S */
526 if(verbosity >= VERB_ALGO)
527 p_ancil("send_udp over interface", r);
528 sent = sendmsg(c->fd, &msg, 0);
530 /* try again and block, waiting for IO to complete,
531 * we want to send the answer, and we will wait for
532 * the ethernet interface buffer to have space. */
534 if(errno == EAGAIN ||
536 errno == EWOULDBLOCK ||
540 if(WSAGetLastError() == WSAEINPROGRESS ||
541 WSAGetLastError() == WSAENOBUFS ||
542 WSAGetLastError() == WSAEWOULDBLOCK) {
546 sent = sendmsg(c->fd, &msg, 0);
548 fd_set_nonblock(c->fd);
553 if(!udp_send_errno_needs_log(addr, addrlen))
555 verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno));
556 log_addr(VERB_OPS, "remote address is",
557 (struct sockaddr_storage*)addr, addrlen);
559 /* netbsd 7 has IP_PKTINFO for recv but not send */
560 if(errno == EINVAL && r->srctype == 4)
561 log_err("sendmsg: No support for sendmsg(IP_PKTINFO). "
562 "Please disable interface-automatic");
565 } else if((size_t)sent != sldns_buffer_remaining(packet)) {
566 log_err("sent %d in place of %d bytes",
567 (int)sent, (int)sldns_buffer_remaining(packet));
577 log_err("sendmsg: IPV6_PKTINFO not supported");
579 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */
582 /** return true is UDP receive error needs to be logged */
583 static int udp_recv_needs_log(int err)
599 if(verbosity >= VERB_ALGO)
609 comm_point_udp_ancil_callback(int fd, short event, void* arg)
611 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
612 struct comm_reply rep;
622 struct cmsghdr* cmsg;
623 #endif /* S_SPLINT_S */
625 rep.c = (struct comm_point*)arg;
626 log_assert(rep.c->type == comm_udp);
628 if(!(event&UB_EV_READ))
630 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
631 ub_comm_base_now(rep.c->ev->base);
632 for(i=0; i<NUM_UDP_PER_SELECT; i++) {
633 sldns_buffer_clear(rep.c->buffer);
634 rep.addrlen = (socklen_t)sizeof(rep.addr);
635 log_assert(fd != -1);
636 log_assert(sldns_buffer_remaining(rep.c->buffer) > 0);
637 msg.msg_name = &rep.addr;
638 msg.msg_namelen = (socklen_t)sizeof(rep.addr);
639 iov[0].iov_base = sldns_buffer_begin(rep.c->buffer);
640 iov[0].iov_len = sldns_buffer_remaining(rep.c->buffer);
643 msg.msg_control = ancil.buf;
645 msg.msg_controllen = sizeof(ancil.buf);
646 #endif /* S_SPLINT_S */
648 rcv = recvmsg(fd, &msg, 0);
650 if(errno != EAGAIN && errno != EINTR
651 && udp_recv_needs_log(errno)) {
652 log_err("recvmsg failed: %s", strerror(errno));
656 rep.addrlen = msg.msg_namelen;
657 sldns_buffer_skip(rep.c->buffer, rcv);
658 sldns_buffer_flip(rep.c->buffer);
661 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
662 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
663 if( cmsg->cmsg_level == IPPROTO_IPV6 &&
664 cmsg->cmsg_type == IPV6_PKTINFO) {
666 memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg),
667 sizeof(struct in6_pktinfo));
670 } else if( cmsg->cmsg_level == IPPROTO_IP &&
671 cmsg->cmsg_type == IP_PKTINFO) {
673 memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg),
674 sizeof(struct in_pktinfo));
676 #elif defined(IP_RECVDSTADDR)
677 } else if( cmsg->cmsg_level == IPPROTO_IP &&
678 cmsg->cmsg_type == IP_RECVDSTADDR) {
680 memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg),
681 sizeof(struct in_addr));
683 #endif /* IP_PKTINFO or IP_RECVDSTADDR */
686 if(verbosity >= VERB_ALGO)
687 p_ancil("receive_udp on interface", &rep);
688 #endif /* S_SPLINT_S */
689 fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
690 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
691 /* send back immediate reply */
692 (void)comm_point_send_udp_msg_if(rep.c, rep.c->buffer,
693 (struct sockaddr*)&rep.addr, rep.addrlen, &rep);
695 if(!rep.c || rep.c->fd == -1) /* commpoint closed */
702 fatal_exit("recvmsg: No support for IPV6_PKTINFO; IP_PKTINFO or IP_RECVDSTADDR. "
703 "Please disable interface-automatic");
704 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG */
708 comm_point_udp_callback(int fd, short event, void* arg)
710 struct comm_reply rep;
713 struct sldns_buffer *buffer;
715 rep.c = (struct comm_point*)arg;
716 log_assert(rep.c->type == comm_udp);
718 if(!(event&UB_EV_READ))
720 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
721 ub_comm_base_now(rep.c->ev->base);
722 for(i=0; i<NUM_UDP_PER_SELECT; i++) {
723 sldns_buffer_clear(rep.c->buffer);
724 rep.addrlen = (socklen_t)sizeof(rep.addr);
725 log_assert(fd != -1);
726 log_assert(sldns_buffer_remaining(rep.c->buffer) > 0);
727 rcv = recvfrom(fd, (void*)sldns_buffer_begin(rep.c->buffer),
728 sldns_buffer_remaining(rep.c->buffer), 0,
729 (struct sockaddr*)&rep.addr, &rep.addrlen);
732 if(errno != EAGAIN && errno != EINTR
733 && udp_recv_needs_log(errno))
734 log_err("recvfrom %d failed: %s",
735 fd, strerror(errno));
737 if(WSAGetLastError() != WSAEINPROGRESS &&
738 WSAGetLastError() != WSAECONNRESET &&
739 WSAGetLastError()!= WSAEWOULDBLOCK)
740 log_err("recvfrom failed: %s",
741 wsa_strerror(WSAGetLastError()));
745 sldns_buffer_skip(rep.c->buffer, rcv);
746 sldns_buffer_flip(rep.c->buffer);
748 fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
749 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
750 /* send back immediate reply */
752 buffer = rep.c->dnscrypt_buffer;
754 buffer = rep.c->buffer;
756 (void)comm_point_send_udp_msg(rep.c, buffer,
757 (struct sockaddr*)&rep.addr, rep.addrlen);
759 if(!rep.c || rep.c->fd != fd) /* commpoint closed to -1 or reused for
760 another UDP port. Note rep.c cannot be reused with TCP fd. */
765 /** Use a new tcp handler for new query fd, set to read query */
767 setup_tcp_handler(struct comm_point* c, int fd, int cur, int max)
770 log_assert(c->type == comm_tcp || c->type == comm_http);
771 log_assert(c->fd == -1);
772 sldns_buffer_clear(c->buffer);
775 sldns_buffer_clear(c->dnscrypt_buffer);
777 c->tcp_is_reading = 1;
778 c->tcp_byte_count = 0;
779 /* if more than half the tcp handlers are in use, use a shorter
780 * timeout for this TCP connection, we need to make space for
781 * other connections to be able to get attention */
782 /* If > 50% TCP handler structures in use, set timeout to 1/100th
784 * If > 65%TCP handler structures in use, set to 1/500th configured
786 * If > 80% TCP handler structures in use, set to 0.
788 * If the timeout to use falls below 200 milliseconds, an actual
789 * timeout of 200ms is used.
791 handler_usage = (cur * 100) / max;
792 if(handler_usage > 50 && handler_usage <= 65)
793 c->tcp_timeout_msec /= 100;
794 else if (handler_usage > 65 && handler_usage <= 80)
795 c->tcp_timeout_msec /= 500;
796 else if (handler_usage > 80)
797 c->tcp_timeout_msec = 0;
798 comm_point_start_listening(c, fd,
799 c->tcp_timeout_msec < TCP_QUERY_TIMEOUT_MINIMUM
800 ? TCP_QUERY_TIMEOUT_MINIMUM
801 : c->tcp_timeout_msec);
804 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
805 short ATTR_UNUSED(event), void* arg)
807 struct comm_base* b = (struct comm_base*)arg;
808 /* timeout for the slow accept, re-enable accepts again */
809 if(b->start_accept) {
810 verbose(VERB_ALGO, "wait is over, slow accept disabled");
811 fptr_ok(fptr_whitelist_start_accept(b->start_accept));
812 (*b->start_accept)(b->cb_arg);
813 b->eb->slow_accept_enabled = 0;
817 int comm_point_perform_accept(struct comm_point* c,
818 struct sockaddr_storage* addr, socklen_t* addrlen)
821 *addrlen = (socklen_t)sizeof(*addr);
823 new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen);
825 /* SOCK_NONBLOCK saves extra calls to fcntl for the same result */
826 new_fd = accept4(c->fd, (struct sockaddr*)addr, addrlen, SOCK_NONBLOCK);
830 /* EINTR is signal interrupt. others are closed connection. */
831 if( errno == EINTR || errno == EAGAIN
833 || errno == EWOULDBLOCK
836 || errno == ECONNABORTED
843 #if defined(ENFILE) && defined(EMFILE)
844 if(errno == ENFILE || errno == EMFILE) {
845 /* out of file descriptors, likely outside of our
846 * control. stop accept() calls for some time */
847 if(c->ev->base->stop_accept) {
848 struct comm_base* b = c->ev->base;
850 verbose(VERB_ALGO, "out of file descriptors: "
852 b->eb->slow_accept_enabled = 1;
853 fptr_ok(fptr_whitelist_stop_accept(
855 (*b->stop_accept)(b->cb_arg);
856 /* set timeout, no mallocs */
857 tv.tv_sec = NETEVENT_SLOW_ACCEPT_TIME/1000;
858 tv.tv_usec = (NETEVENT_SLOW_ACCEPT_TIME%1000)*1000;
859 b->eb->slow_accept = ub_event_new(b->eb->base,
861 comm_base_handle_slow_accept, b);
862 if(b->eb->slow_accept == NULL) {
863 /* we do not want to log here, because
864 * that would spam the logfiles.
865 * error: "event_base_set failed." */
867 else if(ub_event_add(b->eb->slow_accept, &tv)
869 /* we do not want to log here,
870 * error: "event_add failed." */
876 #else /* USE_WINSOCK */
877 if(WSAGetLastError() == WSAEINPROGRESS ||
878 WSAGetLastError() == WSAECONNRESET)
880 if(WSAGetLastError() == WSAEWOULDBLOCK) {
881 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
885 log_err_addr("accept failed", sock_strerror(errno), addr,
889 if(c->tcp_conn_limit && c->type == comm_tcp_accept) {
890 c->tcl_addr = tcl_addr_lookup(c->tcp_conn_limit, addr, *addrlen);
891 if(!tcl_new_connection(c->tcl_addr)) {
893 log_err_addr("accept rejected",
894 "connection limit exceeded", addr, *addrlen);
900 fd_set_nonblock(new_fd);
906 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp),
907 int ATTR_UNUSED(argi), long argl, long retvalue)
909 int wsa_err = WSAGetLastError(); /* store errcode before it is gone */
910 verbose(VERB_ALGO, "bio_cb %d, %s %s %s", oper,
911 (oper&BIO_CB_RETURN)?"return":"before",
912 (oper&BIO_CB_READ)?"read":((oper&BIO_CB_WRITE)?"write":"other"),
913 wsa_err==WSAEWOULDBLOCK?"wsawb":"");
914 /* on windows, check if previous operation caused EWOULDBLOCK */
915 if( (oper == (BIO_CB_READ|BIO_CB_RETURN) && argl == 0) ||
916 (oper == (BIO_CB_GETS|BIO_CB_RETURN) && argl == 0)) {
917 if(wsa_err == WSAEWOULDBLOCK)
918 ub_winsock_tcp_wouldblock((struct ub_event*)
919 BIO_get_callback_arg(b), UB_EV_READ);
921 if( (oper == (BIO_CB_WRITE|BIO_CB_RETURN) && argl == 0) ||
922 (oper == (BIO_CB_PUTS|BIO_CB_RETURN) && argl == 0)) {
923 if(wsa_err == WSAEWOULDBLOCK)
924 ub_winsock_tcp_wouldblock((struct ub_event*)
925 BIO_get_callback_arg(b), UB_EV_WRITE);
927 /* return original return value */
931 /** set win bio callbacks for nonblocking operations */
933 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl)
935 SSL* ssl = (SSL*)thessl;
936 /* set them both just in case, but usually they are the same BIO */
937 BIO_set_callback(SSL_get_rbio(ssl), &win_bio_cb);
938 BIO_set_callback_arg(SSL_get_rbio(ssl), (char*)c->ev->ev);
939 BIO_set_callback(SSL_get_wbio(ssl), &win_bio_cb);
940 BIO_set_callback_arg(SSL_get_wbio(ssl), (char*)c->ev->ev);
945 /** Create http2 session server. Per connection, after TCP accepted.*/
946 static int http2_session_server_create(struct http2_session* h2_session)
948 log_assert(h2_session->callbacks);
949 h2_session->is_drop = 0;
950 if(nghttp2_session_server_new(&h2_session->session,
951 h2_session->callbacks,
952 h2_session) == NGHTTP2_ERR_NOMEM) {
953 log_err("failed to create nghttp2 session server");
960 /** Submit http2 setting to session. Once per session. */
961 static int http2_submit_settings(struct http2_session* h2_session)
964 nghttp2_settings_entry settings[1] = {
965 {NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS,
966 h2_session->c->http2_max_streams}};
968 ret = nghttp2_submit_settings(h2_session->session, NGHTTP2_FLAG_NONE,
971 verbose(VERB_QUERY, "http2: submit_settings failed, "
972 "error: %s", nghttp2_strerror(ret));
977 #endif /* HAVE_NGHTTP2 */
981 comm_point_tcp_accept_callback(int fd, short event, void* arg)
983 struct comm_point* c = (struct comm_point*)arg, *c_hdl;
985 log_assert(c->type == comm_tcp_accept);
986 if(!(event & UB_EV_READ)) {
987 log_info("ignoring tcp accept event %d", (int)event);
990 ub_comm_base_now(c->ev->base);
991 /* find free tcp handler. */
993 log_warn("accepted too many tcp, connections full");
996 /* accept incoming connection. */
998 /* clear leftover flags from previous use, and then set the
999 * correct event base for the event structure for libevent */
1000 ub_event_free(c_hdl->ev->ev);
1001 if((c_hdl->type == comm_tcp && c_hdl->tcp_req_info) ||
1002 c_hdl->type == comm_local || c_hdl->type == comm_raw)
1003 c_hdl->tcp_do_toggle_rw = 0;
1004 else c_hdl->tcp_do_toggle_rw = 1;
1006 if(c_hdl->type == comm_http) {
1008 if(!c_hdl->h2_session ||
1009 !http2_session_server_create(c_hdl->h2_session)) {
1010 log_warn("failed to create nghttp2");
1013 if(!c_hdl->h2_session ||
1014 !http2_submit_settings(c_hdl->h2_session)) {
1015 log_warn("failed to submit http2 settings");
1019 c_hdl->tcp_do_toggle_rw = 0;
1023 c_hdl->ev->ev = ub_event_new(c_hdl->ev->base->eb->base, -1,
1024 UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT,
1025 comm_point_http_handle_callback, c_hdl);
1027 c_hdl->ev->ev = ub_event_new(c_hdl->ev->base->eb->base, -1,
1028 UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT,
1029 comm_point_tcp_handle_callback, c_hdl);
1031 if(!c_hdl->ev->ev) {
1032 log_warn("could not ub_event_new, dropped tcp");
1035 log_assert(fd != -1);
1037 new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.addr,
1038 &c_hdl->repinfo.addrlen);
1042 c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd);
1045 comm_point_close(c_hdl);
1048 c_hdl->ssl_shake_state = comm_ssl_shake_read;
1050 comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl);
1054 /* grab the tcp handler buffers */
1056 c->tcp_free = c_hdl->tcp_free;
1058 /* stop accepting incoming queries for now. */
1059 comm_point_stop_listening(c);
1061 setup_tcp_handler(c_hdl, new_fd, c->cur_tcp_count, c->max_tcp_count);
1064 /** Make tcp handler free for next assignment */
1066 reclaim_tcp_handler(struct comm_point* c)
1068 log_assert(c->type == comm_tcp);
1071 SSL_shutdown(c->ssl);
1076 comm_point_close(c);
1078 c->tcp_parent->cur_tcp_count--;
1079 c->tcp_free = c->tcp_parent->tcp_free;
1080 c->tcp_parent->tcp_free = c;
1082 /* re-enable listening on accept socket */
1083 comm_point_start_listening(c->tcp_parent, -1, -1);
1086 c->tcp_more_read_again = NULL;
1087 c->tcp_more_write_again = NULL;
1090 /** do the callback when writing is done */
1092 tcp_callback_writer(struct comm_point* c)
1094 log_assert(c->type == comm_tcp);
1095 if(!c->tcp_write_and_read) {
1096 sldns_buffer_clear(c->buffer);
1097 c->tcp_byte_count = 0;
1099 if(c->tcp_do_toggle_rw)
1100 c->tcp_is_reading = 1;
1101 /* switch from listening(write) to listening(read) */
1102 if(c->tcp_req_info) {
1103 tcp_req_info_handle_writedone(c->tcp_req_info);
1105 comm_point_stop_listening(c);
1106 if(c->tcp_write_and_read) {
1107 fptr_ok(fptr_whitelist_comm_point(c->callback));
1108 if( (*c->callback)(c, c->cb_arg, NETEVENT_PKT_WRITTEN,
1110 comm_point_start_listening(c, -1,
1111 c->tcp_timeout_msec);
1114 comm_point_start_listening(c, -1, c->tcp_timeout_msec);
1119 /** do the callback when reading is done */
1121 tcp_callback_reader(struct comm_point* c)
1123 log_assert(c->type == comm_tcp || c->type == comm_local);
1124 sldns_buffer_flip(c->buffer);
1125 if(c->tcp_do_toggle_rw)
1126 c->tcp_is_reading = 0;
1127 c->tcp_byte_count = 0;
1128 if(c->tcp_req_info) {
1129 tcp_req_info_handle_readdone(c->tcp_req_info);
1131 if(c->type == comm_tcp)
1132 comm_point_stop_listening(c);
1133 fptr_ok(fptr_whitelist_comm_point(c->callback));
1134 if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) {
1135 comm_point_start_listening(c, -1, c->tcp_timeout_msec);
1141 /** true if the ssl handshake error has to be squelched from the logs */
1143 squelch_err_ssl_handshake(unsigned long err)
1145 if(verbosity >= VERB_QUERY)
1146 return 0; /* only squelch on low verbosity */
1147 /* this is very specific, we could filter on ERR_GET_REASON()
1148 * (the third element in ERR_PACK) */
1149 if(err == ERR_PACK(ERR_LIB_SSL, SSL_F_SSL3_GET_RECORD, SSL_R_HTTPS_PROXY_REQUEST) ||
1150 err == ERR_PACK(ERR_LIB_SSL, SSL_F_SSL3_GET_RECORD, SSL_R_HTTP_REQUEST) ||
1151 err == ERR_PACK(ERR_LIB_SSL, SSL_F_SSL3_GET_RECORD, SSL_R_WRONG_VERSION_NUMBER) ||
1152 err == ERR_PACK(ERR_LIB_SSL, SSL_F_SSL3_READ_BYTES, SSL_R_SSLV3_ALERT_BAD_CERTIFICATE)
1153 #ifdef SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
1154 || err == ERR_PACK(ERR_LIB_SSL, SSL_F_TLS_POST_PROCESS_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER)
1156 #ifdef SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
1157 || err == ERR_PACK(ERR_LIB_SSL, SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL)
1158 || err == ERR_PACK(ERR_LIB_SSL, SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_UNSUPPORTED_PROTOCOL)
1159 # ifdef SSL_R_VERSION_TOO_LOW
1160 || err == ERR_PACK(ERR_LIB_SSL, SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO, SSL_R_VERSION_TOO_LOW)
1167 #endif /* HAVE_SSL */
1169 /** continue ssl handshake */
1172 ssl_handshake(struct comm_point* c)
1175 if(c->ssl_shake_state == comm_ssl_shake_hs_read) {
1176 /* read condition satisfied back to writing */
1177 comm_point_listen_for_rw(c, 1, 1);
1178 c->ssl_shake_state = comm_ssl_shake_none;
1181 if(c->ssl_shake_state == comm_ssl_shake_hs_write) {
1182 /* write condition satisfied, back to reading */
1183 comm_point_listen_for_rw(c, 1, 0);
1184 c->ssl_shake_state = comm_ssl_shake_none;
1189 r = SSL_do_handshake(c->ssl);
1191 int want = SSL_get_error(c->ssl, r);
1192 if(want == SSL_ERROR_WANT_READ) {
1193 if(c->ssl_shake_state == comm_ssl_shake_read)
1195 c->ssl_shake_state = comm_ssl_shake_read;
1196 comm_point_listen_for_rw(c, 1, 0);
1198 } else if(want == SSL_ERROR_WANT_WRITE) {
1199 if(c->ssl_shake_state == comm_ssl_shake_write)
1201 c->ssl_shake_state = comm_ssl_shake_write;
1202 comm_point_listen_for_rw(c, 0, 1);
1205 return 0; /* closed */
1206 } else if(want == SSL_ERROR_SYSCALL) {
1207 /* SYSCALL and errno==0 means closed uncleanly */
1209 if(errno == EPIPE && verbosity < 2)
1210 return 0; /* silence 'broken pipe' */
1213 if(errno == ECONNRESET && verbosity < 2)
1214 return 0; /* silence reset by peer */
1217 log_err("SSL_handshake syscall: %s",
1221 unsigned long err = ERR_get_error();
1222 if(!squelch_err_ssl_handshake(err)) {
1223 log_crypto_err_code("ssl handshake failed", err);
1224 log_addr(VERB_OPS, "ssl handshake failed", &c->repinfo.addr,
1225 c->repinfo.addrlen);
1230 /* this is where peer verification could take place */
1231 if((SSL_get_verify_mode(c->ssl)&SSL_VERIFY_PEER)) {
1233 if(SSL_get_verify_result(c->ssl) == X509_V_OK) {
1234 X509* x = SSL_get_peer_certificate(c->ssl);
1236 log_addr(VERB_ALGO, "SSL connection failed: "
1238 &c->repinfo.addr, c->repinfo.addrlen);
1241 log_cert(VERB_ALGO, "peer certificate", x);
1242 #ifdef HAVE_SSL_GET0_PEERNAME
1243 if(SSL_get0_peername(c->ssl)) {
1245 snprintf(buf, sizeof(buf), "SSL connection "
1246 "to %s authenticated",
1247 SSL_get0_peername(c->ssl));
1248 log_addr(VERB_ALGO, buf, &c->repinfo.addr,
1249 c->repinfo.addrlen);
1252 log_addr(VERB_ALGO, "SSL connection "
1253 "authenticated", &c->repinfo.addr,
1254 c->repinfo.addrlen);
1255 #ifdef HAVE_SSL_GET0_PEERNAME
1260 X509* x = SSL_get_peer_certificate(c->ssl);
1262 log_cert(VERB_ALGO, "peer certificate", x);
1265 log_addr(VERB_ALGO, "SSL connection failed: "
1266 "failed to authenticate",
1267 &c->repinfo.addr, c->repinfo.addrlen);
1271 /* unauthenticated, the verify peer flag was not set
1272 * in c->ssl when the ssl object was created from ssl_ctx */
1273 log_addr(VERB_ALGO, "SSL connection", &c->repinfo.addr,
1274 c->repinfo.addrlen);
1277 /* check if http2 use is negotiated */
1278 if(c->type == comm_http && c->h2_session) {
1279 const unsigned char *alpn;
1280 unsigned int alpnlen = 0;
1281 SSL_get0_alpn_selected(c->ssl, &alpn, &alpnlen);
1282 if(alpnlen == 2 && memcmp("h2", alpn, 2) == 0) {
1283 /* connection upgraded to HTTP2 */
1284 c->tcp_do_toggle_rw = 0;
1289 /* setup listen rw correctly */
1290 if(c->tcp_is_reading) {
1291 if(c->ssl_shake_state != comm_ssl_shake_read)
1292 comm_point_listen_for_rw(c, 1, 0);
1294 comm_point_listen_for_rw(c, 1, 1);
1296 c->ssl_shake_state = comm_ssl_shake_none;
1299 #endif /* HAVE_SSL */
1301 /** ssl read callback on TCP */
1303 ssl_handle_read(struct comm_point* c)
1307 if(c->ssl_shake_state != comm_ssl_shake_none) {
1308 if(!ssl_handshake(c))
1310 if(c->ssl_shake_state != comm_ssl_shake_none)
1313 if(c->tcp_byte_count < sizeof(uint16_t)) {
1314 /* read length bytes */
1316 if((r=SSL_read(c->ssl, (void*)sldns_buffer_at(c->buffer,
1317 c->tcp_byte_count), (int)(sizeof(uint16_t) -
1318 c->tcp_byte_count))) <= 0) {
1319 int want = SSL_get_error(c->ssl, r);
1320 if(want == SSL_ERROR_ZERO_RETURN) {
1322 return tcp_req_info_handle_read_close(c->tcp_req_info);
1323 return 0; /* shutdown, closed */
1324 } else if(want == SSL_ERROR_WANT_READ) {
1325 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1326 return 1; /* read more later */
1327 } else if(want == SSL_ERROR_WANT_WRITE) {
1328 c->ssl_shake_state = comm_ssl_shake_hs_write;
1329 comm_point_listen_for_rw(c, 0, 1);
1331 } else if(want == SSL_ERROR_SYSCALL) {
1333 if(errno == ECONNRESET && verbosity < 2)
1334 return 0; /* silence reset by peer */
1337 log_err("SSL_read syscall: %s",
1341 log_crypto_err("could not SSL_read");
1344 c->tcp_byte_count += r;
1345 if(c->tcp_byte_count < sizeof(uint16_t))
1347 if(sldns_buffer_read_u16_at(c->buffer, 0) >
1348 sldns_buffer_capacity(c->buffer)) {
1349 verbose(VERB_QUERY, "ssl: dropped larger than buffer");
1352 sldns_buffer_set_limit(c->buffer,
1353 sldns_buffer_read_u16_at(c->buffer, 0));
1354 if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1355 verbose(VERB_QUERY, "ssl: dropped bogus too short.");
1358 sldns_buffer_skip(c->buffer, (ssize_t)(c->tcp_byte_count-sizeof(uint16_t)));
1359 verbose(VERB_ALGO, "Reading ssl tcp query of length %d",
1360 (int)sldns_buffer_limit(c->buffer));
1362 if(sldns_buffer_remaining(c->buffer) > 0) {
1364 r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
1365 (int)sldns_buffer_remaining(c->buffer));
1367 int want = SSL_get_error(c->ssl, r);
1368 if(want == SSL_ERROR_ZERO_RETURN) {
1370 return tcp_req_info_handle_read_close(c->tcp_req_info);
1371 return 0; /* shutdown, closed */
1372 } else if(want == SSL_ERROR_WANT_READ) {
1373 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1374 return 1; /* read more later */
1375 } else if(want == SSL_ERROR_WANT_WRITE) {
1376 c->ssl_shake_state = comm_ssl_shake_hs_write;
1377 comm_point_listen_for_rw(c, 0, 1);
1379 } else if(want == SSL_ERROR_SYSCALL) {
1381 if(errno == ECONNRESET && verbosity < 2)
1382 return 0; /* silence reset by peer */
1385 log_err("SSL_read syscall: %s",
1389 log_crypto_err("could not SSL_read");
1392 sldns_buffer_skip(c->buffer, (ssize_t)r);
1394 if(sldns_buffer_remaining(c->buffer) <= 0) {
1395 tcp_callback_reader(c);
1401 #endif /* HAVE_SSL */
1404 /** ssl write callback on TCP */
1406 ssl_handle_write(struct comm_point* c)
1410 if(c->ssl_shake_state != comm_ssl_shake_none) {
1411 if(!ssl_handshake(c))
1413 if(c->ssl_shake_state != comm_ssl_shake_none)
1416 /* ignore return, if fails we may simply block */
1417 (void)SSL_set_mode(c->ssl, (long)SSL_MODE_ENABLE_PARTIAL_WRITE);
1418 if((c->tcp_write_and_read?c->tcp_write_byte_count:c->tcp_byte_count) < sizeof(uint16_t)) {
1419 uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(c->buffer));
1421 if(c->tcp_write_and_read) {
1422 if(c->tcp_write_pkt_len + 2 < LDNS_RR_BUF_SIZE) {
1423 /* combine the tcp length and the query for
1424 * write, this emulates writev */
1425 uint8_t buf[LDNS_RR_BUF_SIZE];
1426 memmove(buf, &len, sizeof(uint16_t));
1427 memmove(buf+sizeof(uint16_t),
1429 c->tcp_write_pkt_len);
1430 r = SSL_write(c->ssl,
1431 (void*)(buf+c->tcp_write_byte_count),
1432 c->tcp_write_pkt_len + 2 -
1433 c->tcp_write_byte_count);
1435 r = SSL_write(c->ssl,
1436 (void*)(((uint8_t*)&len)+c->tcp_write_byte_count),
1437 (int)(sizeof(uint16_t)-c->tcp_write_byte_count));
1439 } else if(sizeof(uint16_t)+sldns_buffer_remaining(c->buffer) <
1441 /* combine the tcp length and the query for write,
1442 * this emulates writev */
1443 uint8_t buf[LDNS_RR_BUF_SIZE];
1444 memmove(buf, &len, sizeof(uint16_t));
1445 memmove(buf+sizeof(uint16_t),
1446 sldns_buffer_current(c->buffer),
1447 sldns_buffer_remaining(c->buffer));
1448 r = SSL_write(c->ssl, (void*)(buf+c->tcp_byte_count),
1449 (int)(sizeof(uint16_t)+
1450 sldns_buffer_remaining(c->buffer)
1451 - c->tcp_byte_count));
1453 r = SSL_write(c->ssl,
1454 (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1455 (int)(sizeof(uint16_t)-c->tcp_byte_count));
1458 int want = SSL_get_error(c->ssl, r);
1459 if(want == SSL_ERROR_ZERO_RETURN) {
1460 return 0; /* closed */
1461 } else if(want == SSL_ERROR_WANT_READ) {
1462 c->ssl_shake_state = comm_ssl_shake_hs_read;
1463 comm_point_listen_for_rw(c, 1, 0);
1464 return 1; /* wait for read condition */
1465 } else if(want == SSL_ERROR_WANT_WRITE) {
1466 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1467 return 1; /* write more later */
1468 } else if(want == SSL_ERROR_SYSCALL) {
1470 if(errno == EPIPE && verbosity < 2)
1471 return 0; /* silence 'broken pipe' */
1474 log_err("SSL_write syscall: %s",
1478 log_crypto_err("could not SSL_write");
1481 if(c->tcp_write_and_read) {
1482 c->tcp_write_byte_count += r;
1483 if(c->tcp_write_byte_count < sizeof(uint16_t))
1486 c->tcp_byte_count += r;
1487 if(c->tcp_byte_count < sizeof(uint16_t))
1489 sldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1492 if((!c->tcp_write_and_read && sldns_buffer_remaining(c->buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
1493 tcp_callback_writer(c);
1497 log_assert(c->tcp_write_and_read || sldns_buffer_remaining(c->buffer) > 0);
1498 log_assert(!c->tcp_write_and_read || c->tcp_write_byte_count < c->tcp_write_pkt_len + 2);
1500 if(c->tcp_write_and_read) {
1501 r = SSL_write(c->ssl, (void*)(c->tcp_write_pkt + c->tcp_write_byte_count - 2),
1502 (int)(c->tcp_write_pkt_len + 2 - c->tcp_write_byte_count));
1504 r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
1505 (int)sldns_buffer_remaining(c->buffer));
1508 int want = SSL_get_error(c->ssl, r);
1509 if(want == SSL_ERROR_ZERO_RETURN) {
1510 return 0; /* closed */
1511 } else if(want == SSL_ERROR_WANT_READ) {
1512 c->ssl_shake_state = comm_ssl_shake_hs_read;
1513 comm_point_listen_for_rw(c, 1, 0);
1514 return 1; /* wait for read condition */
1515 } else if(want == SSL_ERROR_WANT_WRITE) {
1516 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1517 return 1; /* write more later */
1518 } else if(want == SSL_ERROR_SYSCALL) {
1520 if(errno == EPIPE && verbosity < 2)
1521 return 0; /* silence 'broken pipe' */
1524 log_err("SSL_write syscall: %s",
1528 log_crypto_err("could not SSL_write");
1531 if(c->tcp_write_and_read) {
1532 c->tcp_write_byte_count += r;
1534 sldns_buffer_skip(c->buffer, (ssize_t)r);
1537 if((!c->tcp_write_and_read && sldns_buffer_remaining(c->buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
1538 tcp_callback_writer(c);
1544 #endif /* HAVE_SSL */
1547 /** handle ssl tcp connection with dns contents */
1549 ssl_handle_it(struct comm_point* c, int is_write)
1551 /* handle case where renegotiation wants read during write call
1552 * or write during read calls */
1553 if(is_write && c->ssl_shake_state == comm_ssl_shake_hs_write)
1554 return ssl_handle_read(c);
1555 else if(!is_write && c->ssl_shake_state == comm_ssl_shake_hs_read)
1556 return ssl_handle_write(c);
1557 /* handle read events for read operation and write events for a
1558 * write operation */
1560 return ssl_handle_read(c);
1561 return ssl_handle_write(c);
1564 /** Handle tcp reading callback.
1565 * @param fd: file descriptor of socket.
1566 * @param c: comm point to read from into buffer.
1567 * @param short_ok: if true, very short packets are OK (for comm_local).
1568 * @return: 0 on error
1571 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok)
1574 log_assert(c->type == comm_tcp || c->type == comm_local);
1576 return ssl_handle_it(c, 0);
1577 if(!c->tcp_is_reading && !c->tcp_write_and_read)
1580 log_assert(fd != -1);
1581 if(c->tcp_byte_count < sizeof(uint16_t)) {
1582 /* read length bytes */
1583 r = recv(fd,(void*)sldns_buffer_at(c->buffer,c->tcp_byte_count),
1584 sizeof(uint16_t)-c->tcp_byte_count, 0);
1587 return tcp_req_info_handle_read_close(c->tcp_req_info);
1589 } else if(r == -1) {
1591 if(errno == EINTR || errno == EAGAIN)
1594 if(errno == ECONNRESET && verbosity < 2)
1595 return 0; /* silence reset by peer */
1597 #else /* USE_WINSOCK */
1598 if(WSAGetLastError() == WSAECONNRESET)
1600 if(WSAGetLastError() == WSAEINPROGRESS)
1602 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1603 ub_winsock_tcp_wouldblock(c->ev->ev,
1608 log_err_addr("read (in tcp s)", sock_strerror(errno),
1609 &c->repinfo.addr, c->repinfo.addrlen);
1612 c->tcp_byte_count += r;
1613 if(c->tcp_byte_count != sizeof(uint16_t))
1615 if(sldns_buffer_read_u16_at(c->buffer, 0) >
1616 sldns_buffer_capacity(c->buffer)) {
1617 verbose(VERB_QUERY, "tcp: dropped larger than buffer");
1620 sldns_buffer_set_limit(c->buffer,
1621 sldns_buffer_read_u16_at(c->buffer, 0));
1623 sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1624 verbose(VERB_QUERY, "tcp: dropped bogus too short.");
1627 verbose(VERB_ALGO, "Reading tcp query of length %d",
1628 (int)sldns_buffer_limit(c->buffer));
1631 log_assert(sldns_buffer_remaining(c->buffer) > 0);
1632 r = recv(fd, (void*)sldns_buffer_current(c->buffer),
1633 sldns_buffer_remaining(c->buffer), 0);
1636 return tcp_req_info_handle_read_close(c->tcp_req_info);
1638 } else if(r == -1) {
1640 if(errno == EINTR || errno == EAGAIN)
1642 #else /* USE_WINSOCK */
1643 if(WSAGetLastError() == WSAECONNRESET)
1645 if(WSAGetLastError() == WSAEINPROGRESS)
1647 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1648 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1652 log_err_addr("read (in tcp r)", sock_strerror(errno),
1653 &c->repinfo.addr, c->repinfo.addrlen);
1656 sldns_buffer_skip(c->buffer, r);
1657 if(sldns_buffer_remaining(c->buffer) <= 0) {
1658 tcp_callback_reader(c);
1664 * Handle tcp writing callback.
1665 * @param fd: file descriptor of socket.
1666 * @param c: comm point to write buffer out of.
1667 * @return: 0 on error
1670 comm_point_tcp_handle_write(int fd, struct comm_point* c)
1673 struct sldns_buffer *buffer;
1674 log_assert(c->type == comm_tcp);
1676 buffer = c->dnscrypt_buffer;
1680 if(c->tcp_is_reading && !c->ssl && !c->tcp_write_and_read)
1682 log_assert(fd != -1);
1683 if(((!c->tcp_write_and_read && c->tcp_byte_count == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == 0)) && c->tcp_check_nb_connect) {
1684 /* check for pending error from nonblocking connect */
1685 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/
1687 socklen_t len = (socklen_t)sizeof(error);
1688 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
1691 error = errno; /* on solaris errno is error */
1692 #else /* USE_WINSOCK */
1693 error = WSAGetLastError();
1697 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1698 if(error == EINPROGRESS || error == EWOULDBLOCK)
1699 return 1; /* try again later */
1702 if(error != 0 && verbosity < 2)
1703 return 0; /* silence lots of chatter in the logs */
1704 else if(error != 0) {
1705 log_err_addr("tcp connect", strerror(error),
1706 &c->repinfo.addr, c->repinfo.addrlen);
1707 #else /* USE_WINSOCK */
1709 if(error == WSAEINPROGRESS)
1711 else if(error == WSAEWOULDBLOCK) {
1712 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1714 } else if(error != 0 && verbosity < 2)
1716 else if(error != 0) {
1717 log_err_addr("tcp connect", wsa_strerror(error),
1718 &c->repinfo.addr, c->repinfo.addrlen);
1719 #endif /* USE_WINSOCK */
1724 return ssl_handle_it(c, 1);
1726 #ifdef USE_MSG_FASTOPEN
1727 /* Only try this on first use of a connection that uses tfo,
1728 otherwise fall through to normal write */
1729 /* Also, TFO support on WINDOWS not implemented at the moment */
1730 if(c->tcp_do_fastopen == 1) {
1731 /* this form of sendmsg() does both a connect() and send() so need to
1732 look for various flavours of error*/
1733 uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(buffer));
1735 struct iovec iov[2];
1736 c->tcp_do_fastopen = 0;
1737 memset(&msg, 0, sizeof(msg));
1738 if(c->tcp_write_and_read) {
1739 iov[0].iov_base = (uint8_t*)&len + c->tcp_write_byte_count;
1740 iov[0].iov_len = sizeof(uint16_t) - c->tcp_write_byte_count;
1741 iov[1].iov_base = c->tcp_write_pkt;
1742 iov[1].iov_len = c->tcp_write_pkt_len;
1744 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
1745 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
1746 iov[1].iov_base = sldns_buffer_begin(buffer);
1747 iov[1].iov_len = sldns_buffer_limit(buffer);
1749 log_assert(iov[0].iov_len > 0);
1750 msg.msg_name = &c->repinfo.addr;
1751 msg.msg_namelen = c->repinfo.addrlen;
1754 r = sendmsg(fd, &msg, MSG_FASTOPEN);
1756 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1757 /* Handshake is underway, maybe because no TFO cookie available.
1758 Come back to write the message*/
1759 if(errno == EINPROGRESS || errno == EWOULDBLOCK)
1762 if(errno == EINTR || errno == EAGAIN)
1764 /* Not handling EISCONN here as shouldn't ever hit that case.*/
1765 if(errno != EPIPE && errno != 0 && verbosity < 2)
1766 return 0; /* silence lots of chatter in the logs */
1767 if(errno != EPIPE && errno != 0) {
1768 log_err_addr("tcp sendmsg", strerror(errno),
1769 &c->repinfo.addr, c->repinfo.addrlen);
1772 /* fallthrough to nonFASTOPEN
1773 * (MSG_FASTOPEN on Linux 3 produces EPIPE)
1774 * we need to perform connect() */
1775 if(connect(fd, (struct sockaddr *)&c->repinfo.addr, c->repinfo.addrlen) == -1) {
1777 if(errno == EINPROGRESS)
1778 return 1; /* wait until connect done*/
1781 if(WSAGetLastError() == WSAEINPROGRESS ||
1782 WSAGetLastError() == WSAEWOULDBLOCK)
1783 return 1; /* wait until connect done*/
1785 if(tcp_connect_errno_needs_log(
1786 (struct sockaddr *)&c->repinfo.addr, c->repinfo.addrlen)) {
1787 log_err_addr("outgoing tcp: connect after EPIPE for fastopen",
1788 strerror(errno), &c->repinfo.addr, c->repinfo.addrlen);
1794 if(c->tcp_write_and_read) {
1795 c->tcp_write_byte_count += r;
1796 if(c->tcp_write_byte_count < sizeof(uint16_t))
1799 c->tcp_byte_count += r;
1800 if(c->tcp_byte_count < sizeof(uint16_t))
1802 sldns_buffer_set_position(buffer, c->tcp_byte_count -
1805 if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
1806 tcp_callback_writer(c);
1811 #endif /* USE_MSG_FASTOPEN */
1813 if((c->tcp_write_and_read?c->tcp_write_byte_count:c->tcp_byte_count) < sizeof(uint16_t)) {
1814 uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(buffer));
1816 struct iovec iov[2];
1817 if(c->tcp_write_and_read) {
1818 iov[0].iov_base = (uint8_t*)&len + c->tcp_write_byte_count;
1819 iov[0].iov_len = sizeof(uint16_t) - c->tcp_write_byte_count;
1820 iov[1].iov_base = c->tcp_write_pkt;
1821 iov[1].iov_len = c->tcp_write_pkt_len;
1823 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
1824 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
1825 iov[1].iov_base = sldns_buffer_begin(buffer);
1826 iov[1].iov_len = sldns_buffer_limit(buffer);
1828 log_assert(iov[0].iov_len > 0);
1829 r = writev(fd, iov, 2);
1830 #else /* HAVE_WRITEV */
1831 if(c->tcp_write_and_read) {
1832 r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_write_byte_count),
1833 sizeof(uint16_t)-c->tcp_write_byte_count, 0);
1835 r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1836 sizeof(uint16_t)-c->tcp_byte_count, 0);
1838 #endif /* HAVE_WRITEV */
1842 if(errno == EPIPE && verbosity < 2)
1843 return 0; /* silence 'broken pipe' */
1845 if(errno == EINTR || errno == EAGAIN)
1848 if(errno == ECONNRESET && verbosity < 2)
1849 return 0; /* silence reset by peer */
1852 log_err_addr("tcp writev", strerror(errno),
1853 &c->repinfo.addr, c->repinfo.addrlen);
1854 # else /* HAVE_WRITEV */
1855 log_err_addr("tcp send s", strerror(errno),
1856 &c->repinfo.addr, c->repinfo.addrlen);
1857 # endif /* HAVE_WRITEV */
1859 if(WSAGetLastError() == WSAENOTCONN)
1861 if(WSAGetLastError() == WSAEINPROGRESS)
1863 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1864 ub_winsock_tcp_wouldblock(c->ev->ev,
1868 if(WSAGetLastError() == WSAECONNRESET && verbosity < 2)
1869 return 0; /* silence reset by peer */
1870 log_err_addr("tcp send s",
1871 wsa_strerror(WSAGetLastError()),
1872 &c->repinfo.addr, c->repinfo.addrlen);
1876 if(c->tcp_write_and_read) {
1877 c->tcp_write_byte_count += r;
1878 if(c->tcp_write_byte_count < sizeof(uint16_t))
1881 c->tcp_byte_count += r;
1882 if(c->tcp_byte_count < sizeof(uint16_t))
1884 sldns_buffer_set_position(buffer, c->tcp_byte_count -
1887 if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
1888 tcp_callback_writer(c);
1892 log_assert(c->tcp_write_and_read || sldns_buffer_remaining(buffer) > 0);
1893 log_assert(!c->tcp_write_and_read || c->tcp_write_byte_count < c->tcp_write_pkt_len + 2);
1894 if(c->tcp_write_and_read) {
1895 r = send(fd, (void*)c->tcp_write_pkt + c->tcp_write_byte_count - 2,
1896 c->tcp_write_pkt_len + 2 - c->tcp_write_byte_count, 0);
1898 r = send(fd, (void*)sldns_buffer_current(buffer),
1899 sldns_buffer_remaining(buffer), 0);
1903 if(errno == EINTR || errno == EAGAIN)
1906 if(errno == ECONNRESET && verbosity < 2)
1907 return 0; /* silence reset by peer */
1910 if(WSAGetLastError() == WSAEINPROGRESS)
1912 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1913 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1916 if(WSAGetLastError() == WSAECONNRESET && verbosity < 2)
1917 return 0; /* silence reset by peer */
1919 log_err_addr("tcp send r", sock_strerror(errno),
1920 &c->repinfo.addr, c->repinfo.addrlen);
1923 if(c->tcp_write_and_read) {
1924 c->tcp_write_byte_count += r;
1926 sldns_buffer_skip(buffer, r);
1929 if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
1930 tcp_callback_writer(c);
1936 /** read again to drain buffers when there could be more to read */
1938 tcp_req_info_read_again(int fd, struct comm_point* c)
1940 while(c->tcp_req_info->read_again) {
1942 c->tcp_req_info->read_again = 0;
1943 if(c->tcp_is_reading)
1944 r = comm_point_tcp_handle_read(fd, c, 0);
1945 else r = comm_point_tcp_handle_write(fd, c);
1947 reclaim_tcp_handler(c);
1948 if(!c->tcp_do_close) {
1949 fptr_ok(fptr_whitelist_comm_point(
1951 (void)(*c->callback)(c, c->cb_arg,
1952 NETEVENT_CLOSED, NULL);
1959 /** read again to drain buffers when there could be more to read */
1961 tcp_more_read_again(int fd, struct comm_point* c)
1963 /* if the packet is done, but another one could be waiting on
1964 * the connection, the callback signals this, and we try again */
1965 /* this continues until the read routines get EAGAIN or so,
1966 * and thus does not call the callback, and the bool is 0 */
1967 int* moreread = c->tcp_more_read_again;
1968 while(moreread && *moreread) {
1970 if(!comm_point_tcp_handle_read(fd, c, 0)) {
1971 reclaim_tcp_handler(c);
1972 if(!c->tcp_do_close) {
1973 fptr_ok(fptr_whitelist_comm_point(
1975 (void)(*c->callback)(c, c->cb_arg,
1976 NETEVENT_CLOSED, NULL);
1983 /** write again to fill up when there could be more to write */
1985 tcp_more_write_again(int fd, struct comm_point* c)
1987 /* if the packet is done, but another is waiting to be written,
1988 * the callback signals it and we try again. */
1989 /* this continues until the write routines get EAGAIN or so,
1990 * and thus does not call the callback, and the bool is 0 */
1991 int* morewrite = c->tcp_more_write_again;
1992 while(morewrite && *morewrite) {
1994 if(!comm_point_tcp_handle_write(fd, c)) {
1995 reclaim_tcp_handler(c);
1996 if(!c->tcp_do_close) {
1997 fptr_ok(fptr_whitelist_comm_point(
1999 (void)(*c->callback)(c, c->cb_arg,
2000 NETEVENT_CLOSED, NULL);
2008 comm_point_tcp_handle_callback(int fd, short event, void* arg)
2010 struct comm_point* c = (struct comm_point*)arg;
2011 log_assert(c->type == comm_tcp);
2012 ub_comm_base_now(c->ev->base);
2015 /* Initialize if this is a dnscrypt socket */
2017 c->dnscrypt = c->tcp_parent->dnscrypt;
2019 if(c->dnscrypt && c->dnscrypt_buffer == c->buffer) {
2020 c->dnscrypt_buffer = sldns_buffer_new(sldns_buffer_capacity(c->buffer));
2021 if(!c->dnscrypt_buffer) {
2022 log_err("Could not allocate dnscrypt buffer");
2023 reclaim_tcp_handler(c);
2024 if(!c->tcp_do_close) {
2025 fptr_ok(fptr_whitelist_comm_point(
2027 (void)(*c->callback)(c, c->cb_arg,
2028 NETEVENT_CLOSED, NULL);
2035 if(event&UB_EV_TIMEOUT) {
2036 verbose(VERB_QUERY, "tcp took too long, dropped");
2037 reclaim_tcp_handler(c);
2038 if(!c->tcp_do_close) {
2039 fptr_ok(fptr_whitelist_comm_point(c->callback));
2040 (void)(*c->callback)(c, c->cb_arg,
2041 NETEVENT_TIMEOUT, NULL);
2046 #ifdef USE_MSG_FASTOPEN
2047 && !(c->tcp_do_fastopen && (event&UB_EV_WRITE))
2050 int has_tcpq = (c->tcp_req_info != NULL);
2051 int* moreread = c->tcp_more_read_again;
2052 if(!comm_point_tcp_handle_read(fd, c, 0)) {
2053 reclaim_tcp_handler(c);
2054 if(!c->tcp_do_close) {
2055 fptr_ok(fptr_whitelist_comm_point(
2057 (void)(*c->callback)(c, c->cb_arg,
2058 NETEVENT_CLOSED, NULL);
2062 if(has_tcpq && c->tcp_req_info && c->tcp_req_info->read_again)
2063 tcp_req_info_read_again(fd, c);
2064 if(moreread && *moreread)
2065 tcp_more_read_again(fd, c);
2068 if(event&UB_EV_WRITE) {
2069 int has_tcpq = (c->tcp_req_info != NULL);
2070 int* morewrite = c->tcp_more_write_again;
2071 if(!comm_point_tcp_handle_write(fd, c)) {
2072 reclaim_tcp_handler(c);
2073 if(!c->tcp_do_close) {
2074 fptr_ok(fptr_whitelist_comm_point(
2076 (void)(*c->callback)(c, c->cb_arg,
2077 NETEVENT_CLOSED, NULL);
2081 if(has_tcpq && c->tcp_req_info && c->tcp_req_info->read_again)
2082 tcp_req_info_read_again(fd, c);
2083 if(morewrite && *morewrite)
2084 tcp_more_write_again(fd, c);
2087 log_err("Ignored event %d for tcphdl.", event);
2090 /** Make http handler free for next assignment */
2092 reclaim_http_handler(struct comm_point* c)
2094 log_assert(c->type == comm_http);
2097 SSL_shutdown(c->ssl);
2102 comm_point_close(c);
2104 c->tcp_parent->cur_tcp_count--;
2105 c->tcp_free = c->tcp_parent->tcp_free;
2106 c->tcp_parent->tcp_free = c;
2108 /* re-enable listening on accept socket */
2109 comm_point_start_listening(c->tcp_parent, -1, -1);
2114 /** read more data for http (with ssl) */
2116 ssl_http_read_more(struct comm_point* c)
2120 log_assert(sldns_buffer_remaining(c->buffer) > 0);
2122 r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
2123 (int)sldns_buffer_remaining(c->buffer));
2125 int want = SSL_get_error(c->ssl, r);
2126 if(want == SSL_ERROR_ZERO_RETURN) {
2127 return 0; /* shutdown, closed */
2128 } else if(want == SSL_ERROR_WANT_READ) {
2129 return 1; /* read more later */
2130 } else if(want == SSL_ERROR_WANT_WRITE) {
2131 c->ssl_shake_state = comm_ssl_shake_hs_write;
2132 comm_point_listen_for_rw(c, 0, 1);
2134 } else if(want == SSL_ERROR_SYSCALL) {
2136 if(errno == ECONNRESET && verbosity < 2)
2137 return 0; /* silence reset by peer */
2140 log_err("SSL_read syscall: %s",
2144 log_crypto_err("could not SSL_read");
2147 sldns_buffer_skip(c->buffer, (ssize_t)r);
2152 #endif /* HAVE_SSL */
2155 /** read more data for http */
2157 http_read_more(int fd, struct comm_point* c)
2160 log_assert(sldns_buffer_remaining(c->buffer) > 0);
2161 r = recv(fd, (void*)sldns_buffer_current(c->buffer),
2162 sldns_buffer_remaining(c->buffer), 0);
2165 } else if(r == -1) {
2167 if(errno == EINTR || errno == EAGAIN)
2169 #else /* USE_WINSOCK */
2170 if(WSAGetLastError() == WSAECONNRESET)
2172 if(WSAGetLastError() == WSAEINPROGRESS)
2174 if(WSAGetLastError() == WSAEWOULDBLOCK) {
2175 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
2179 log_err_addr("read (in http r)", sock_strerror(errno),
2180 &c->repinfo.addr, c->repinfo.addrlen);
2183 sldns_buffer_skip(c->buffer, r);
2187 /** return true if http header has been read (one line complete) */
2189 http_header_done(sldns_buffer* buf)
2192 for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) {
2193 /* there was a \r before the \n, but we ignore that */
2194 if((char)sldns_buffer_read_u8_at(buf, i) == '\n')
2200 /** return character string into buffer for header line, moves buffer
2201 * past that line and puts zero terminator into linefeed-newline */
2203 http_header_line(sldns_buffer* buf)
2205 char* result = (char*)sldns_buffer_current(buf);
2207 for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) {
2208 /* terminate the string on the \r */
2209 if((char)sldns_buffer_read_u8_at(buf, i) == '\r')
2210 sldns_buffer_write_u8_at(buf, i, 0);
2211 /* terminate on the \n and skip past the it and done */
2212 if((char)sldns_buffer_read_u8_at(buf, i) == '\n') {
2213 sldns_buffer_write_u8_at(buf, i, 0);
2214 sldns_buffer_set_position(buf, i+1);
2221 /** move unread buffer to start and clear rest for putting the rest into it */
2223 http_moveover_buffer(sldns_buffer* buf)
2225 size_t pos = sldns_buffer_position(buf);
2226 size_t len = sldns_buffer_remaining(buf);
2227 sldns_buffer_clear(buf);
2228 memmove(sldns_buffer_begin(buf), sldns_buffer_at(buf, pos), len);
2229 sldns_buffer_set_position(buf, len);
2232 /** a http header is complete, process it */
2234 http_process_initial_header(struct comm_point* c)
2236 char* line = http_header_line(c->buffer);
2238 verbose(VERB_ALGO, "http header: %s", line);
2239 if(strncasecmp(line, "HTTP/1.1 ", 9) == 0) {
2240 /* check returncode */
2241 if(line[9] != '2') {
2242 verbose(VERB_ALGO, "http bad status %s", line+9);
2245 } else if(strncasecmp(line, "Content-Length: ", 16) == 0) {
2246 if(!c->http_is_chunked)
2247 c->tcp_byte_count = (size_t)atoi(line+16);
2248 } else if(strncasecmp(line, "Transfer-Encoding: chunked", 19+7) == 0) {
2249 c->tcp_byte_count = 0;
2250 c->http_is_chunked = 1;
2251 } else if(line[0] == 0) {
2252 /* end of initial headers */
2253 c->http_in_headers = 0;
2254 if(c->http_is_chunked)
2255 c->http_in_chunk_headers = 1;
2256 /* remove header text from front of buffer
2257 * the buffer is going to be used to return the data segment
2258 * itself and we don't want the header to get returned
2259 * prepended with it */
2260 http_moveover_buffer(c->buffer);
2261 sldns_buffer_flip(c->buffer);
2264 /* ignore other headers */
2268 /** a chunk header is complete, process it, return 0=fail, 1=continue next
2269 * header line, 2=done with chunked transfer*/
2271 http_process_chunk_header(struct comm_point* c)
2273 char* line = http_header_line(c->buffer);
2275 if(c->http_in_chunk_headers == 3) {
2276 verbose(VERB_ALGO, "http chunk trailer: %s", line);
2278 if(line[0] == 0 && c->tcp_byte_count == 0) {
2279 /* callback of http reader when NETEVENT_DONE,
2280 * end of data, with no data in buffer */
2281 sldns_buffer_set_position(c->buffer, 0);
2282 sldns_buffer_set_limit(c->buffer, 0);
2283 fptr_ok(fptr_whitelist_comm_point(c->callback));
2284 (void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL);
2285 /* return that we are done */
2289 /* continue with header of the next chunk */
2290 c->http_in_chunk_headers = 1;
2291 /* remove header text from front of buffer */
2292 http_moveover_buffer(c->buffer);
2293 sldns_buffer_flip(c->buffer);
2296 /* ignore further trail headers */
2299 verbose(VERB_ALGO, "http chunk header: %s", line);
2300 if(c->http_in_chunk_headers == 1) {
2301 /* read chunked start line */
2303 c->tcp_byte_count = (size_t)strtol(line, &end, 16);
2306 c->http_in_chunk_headers = 0;
2307 /* remove header text from front of buffer */
2308 http_moveover_buffer(c->buffer);
2309 sldns_buffer_flip(c->buffer);
2310 if(c->tcp_byte_count == 0) {
2311 /* done with chunks, process chunk_trailer lines */
2312 c->http_in_chunk_headers = 3;
2316 /* ignore other headers */
2320 /** handle nonchunked data segment */
2322 http_nonchunk_segment(struct comm_point* c)
2324 /* c->buffer at position..limit has new data we read in.
2325 * the buffer itself is full of nonchunked data.
2326 * we are looking to read tcp_byte_count more data
2327 * and then the transfer is done. */
2328 size_t remainbufferlen;
2329 size_t got_now = sldns_buffer_limit(c->buffer) - c->http_stored;
2330 if(c->tcp_byte_count <= got_now) {
2331 /* done, this is the last data fragment */
2333 sldns_buffer_set_position(c->buffer, 0);
2334 fptr_ok(fptr_whitelist_comm_point(c->callback));
2335 (void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL);
2338 c->tcp_byte_count -= got_now;
2339 /* if we have the buffer space,
2340 * read more data collected into the buffer */
2341 remainbufferlen = sldns_buffer_capacity(c->buffer) -
2342 sldns_buffer_limit(c->buffer);
2343 if(remainbufferlen >= c->tcp_byte_count ||
2344 remainbufferlen >= 2048) {
2345 size_t total = sldns_buffer_limit(c->buffer);
2346 sldns_buffer_clear(c->buffer);
2347 sldns_buffer_set_position(c->buffer, total);
2348 c->http_stored = total;
2349 /* return and wait to read more */
2352 /* call callback with this data amount, then
2355 sldns_buffer_set_position(c->buffer, 0);
2356 fptr_ok(fptr_whitelist_comm_point(c->callback));
2357 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL);
2358 /* c->callback has to buffer_clear(c->buffer). */
2359 /* return and wait to read more */
2363 /** handle nonchunked data segment, return 0=fail, 1=wait, 2=process more */
2365 http_chunked_segment(struct comm_point* c)
2367 /* the c->buffer has from position..limit new data we read. */
2368 /* the current chunk has length tcp_byte_count.
2369 * once we read that read more chunk headers.
2371 size_t remainbufferlen;
2372 size_t got_now = sldns_buffer_limit(c->buffer) - c->http_stored;
2373 if(c->tcp_byte_count <= got_now) {
2374 /* the chunk has completed (with perhaps some extra data
2375 * from next chunk header and next chunk) */
2376 /* save too much info into temp buffer */
2378 struct comm_reply repinfo;
2380 sldns_buffer_skip(c->buffer, (ssize_t)c->tcp_byte_count);
2381 sldns_buffer_clear(c->http_temp);
2382 sldns_buffer_write(c->http_temp,
2383 sldns_buffer_current(c->buffer),
2384 sldns_buffer_remaining(c->buffer));
2385 sldns_buffer_flip(c->http_temp);
2387 /* callback with this fragment */
2388 fraglen = sldns_buffer_position(c->buffer);
2389 sldns_buffer_set_position(c->buffer, 0);
2390 sldns_buffer_set_limit(c->buffer, fraglen);
2391 repinfo = c->repinfo;
2392 fptr_ok(fptr_whitelist_comm_point(c->callback));
2393 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &repinfo);
2394 /* c->callback has to buffer_clear(). */
2396 /* is commpoint deleted? */
2400 /* copy waiting info */
2401 sldns_buffer_clear(c->buffer);
2402 sldns_buffer_write(c->buffer,
2403 sldns_buffer_begin(c->http_temp),
2404 sldns_buffer_remaining(c->http_temp));
2405 sldns_buffer_flip(c->buffer);
2406 /* process end of chunk trailer header lines, until
2408 c->http_in_chunk_headers = 3;
2409 /* process more data in buffer (if any) */
2412 c->tcp_byte_count -= got_now;
2414 /* if we have the buffer space,
2415 * read more data collected into the buffer */
2416 remainbufferlen = sldns_buffer_capacity(c->buffer) -
2417 sldns_buffer_limit(c->buffer);
2418 if(remainbufferlen >= c->tcp_byte_count ||
2419 remainbufferlen >= 2048) {
2420 size_t total = sldns_buffer_limit(c->buffer);
2421 sldns_buffer_clear(c->buffer);
2422 sldns_buffer_set_position(c->buffer, total);
2423 c->http_stored = total;
2424 /* return and wait to read more */
2428 /* callback of http reader for a new part of the data */
2430 sldns_buffer_set_position(c->buffer, 0);
2431 fptr_ok(fptr_whitelist_comm_point(c->callback));
2432 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL);
2433 /* c->callback has to buffer_clear(c->buffer). */
2434 /* return and wait to read more */
2439 /** Create new http2 session. Called when creating handling comm point. */
2440 struct http2_session* http2_session_create(struct comm_point* c)
2442 struct http2_session* session = calloc(1, sizeof(*session));
2444 log_err("malloc failure while creating http2 session");
2453 /** Delete http2 session. After closing connection or on error */
2454 void http2_session_delete(struct http2_session* h2_session)
2457 if(h2_session->callbacks)
2458 nghttp2_session_callbacks_del(h2_session->callbacks);
2466 struct http2_stream* http2_stream_create(int32_t stream_id)
2468 struct http2_stream* h2_stream = calloc(1, sizeof(*h2_stream));
2470 log_err("malloc failure while creating http2 stream");
2473 h2_stream->stream_id = stream_id;
2477 /** Delete http2 stream. After session delete or stream close callback */
2478 static void http2_stream_delete(struct http2_session* h2_session,
2479 struct http2_stream* h2_stream)
2481 if(h2_stream->mesh_state) {
2482 mesh_state_remove_reply(h2_stream->mesh, h2_stream->mesh_state,
2484 h2_stream->mesh_state = NULL;
2486 http2_req_stream_clear(h2_stream);
2491 void http2_stream_add_meshstate(struct http2_stream* h2_stream,
2492 struct mesh_area* mesh, struct mesh_state* m)
2494 h2_stream->mesh = mesh;
2495 h2_stream->mesh_state = m;
2498 /** delete http2 session server. After closing connection. */
2499 static void http2_session_server_delete(struct http2_session* h2_session)
2502 struct http2_stream* h2_stream, *next;
2503 nghttp2_session_del(h2_session->session); /* NULL input is fine */
2504 h2_session->session = NULL;
2505 for(h2_stream = h2_session->first_stream; h2_stream;) {
2506 next = h2_stream->next;
2507 http2_stream_delete(h2_session, h2_stream);
2510 h2_session->first_stream = NULL;
2511 h2_session->is_drop = 0;
2512 h2_session->postpone_drop = 0;
2513 h2_session->c->h2_stream = NULL;
2519 void http2_session_add_stream(struct http2_session* h2_session,
2520 struct http2_stream* h2_stream)
2522 if(h2_session->first_stream)
2523 h2_session->first_stream->prev = h2_stream;
2524 h2_stream->next = h2_session->first_stream;
2525 h2_session->first_stream = h2_stream;
2528 /** remove stream from session linked list. After stream close callback or
2529 * closing connection */
2530 void http2_session_remove_stream(struct http2_session* h2_session,
2531 struct http2_stream* h2_stream)
2534 h2_stream->prev->next = h2_stream->next;
2536 h2_session->first_stream = h2_stream->next;
2538 h2_stream->next->prev = h2_stream->prev;
2542 int http2_stream_close_cb(nghttp2_session* ATTR_UNUSED(session),
2543 int32_t stream_id, uint32_t ATTR_UNUSED(error_code), void* cb_arg)
2545 struct http2_stream* h2_stream;
2546 struct http2_session* h2_session = (struct http2_session*)cb_arg;
2547 if(!(h2_stream = nghttp2_session_get_stream_user_data(
2548 h2_session->session, stream_id))) {
2551 http2_session_remove_stream(h2_session, h2_stream);
2552 http2_stream_delete(h2_session, h2_stream);
2556 ssize_t http2_recv_cb(nghttp2_session* ATTR_UNUSED(session), uint8_t* buf,
2557 size_t len, int ATTR_UNUSED(flags), void* cb_arg)
2559 struct http2_session* h2_session = (struct http2_session*)cb_arg;
2562 log_assert(h2_session->c->type == comm_http);
2563 log_assert(h2_session->c->h2_session);
2566 if(h2_session->c->ssl) {
2569 r = SSL_read(h2_session->c->ssl, buf, len);
2571 int want = SSL_get_error(h2_session->c->ssl, r);
2572 if(want == SSL_ERROR_ZERO_RETURN) {
2573 return NGHTTP2_ERR_EOF;
2574 } else if(want == SSL_ERROR_WANT_READ) {
2575 return NGHTTP2_ERR_WOULDBLOCK;
2576 } else if(want == SSL_ERROR_WANT_WRITE) {
2577 h2_session->c->ssl_shake_state = comm_ssl_shake_hs_write;
2578 comm_point_listen_for_rw(h2_session->c, 0, 1);
2579 return NGHTTP2_ERR_WOULDBLOCK;
2580 } else if(want == SSL_ERROR_SYSCALL) {
2582 if(errno == ECONNRESET && verbosity < 2)
2583 return NGHTTP2_ERR_CALLBACK_FAILURE;
2586 log_err("SSL_read syscall: %s",
2588 return NGHTTP2_ERR_CALLBACK_FAILURE;
2590 log_crypto_err("could not SSL_read");
2591 return NGHTTP2_ERR_CALLBACK_FAILURE;
2595 #endif /* HAVE_SSL */
2597 ret = recv(h2_session->c->fd, buf, len, 0);
2599 return NGHTTP2_ERR_EOF;
2600 } else if(ret < 0) {
2602 if(errno == EINTR || errno == EAGAIN)
2603 return NGHTTP2_ERR_WOULDBLOCK;
2605 if(errno == ECONNRESET && verbosity < 2)
2606 return NGHTTP2_ERR_CALLBACK_FAILURE;
2608 log_err_addr("could not http2 recv: %s", strerror(errno),
2609 &h2_session->c->repinfo.addr,
2610 h2_session->c->repinfo.addrlen);
2611 #else /* USE_WINSOCK */
2612 if(WSAGetLastError() == WSAECONNRESET)
2613 return NGHTTP2_ERR_CALLBACK_FAILURE;
2614 if(WSAGetLastError() == WSAEINPROGRESS)
2615 return NGHTTP2_ERR_WOULDBLOCK;
2616 if(WSAGetLastError() == WSAEWOULDBLOCK) {
2617 ub_winsock_tcp_wouldblock(h2_session->c->ev->ev,
2619 return NGHTTP2_ERR_WOULDBLOCK;
2621 log_err_addr("could not http2 recv: %s",
2622 wsa_strerror(WSAGetLastError()),
2623 &h2_session->c->repinfo.addr,
2624 h2_session->c->repinfo.addrlen);
2626 return NGHTTP2_ERR_CALLBACK_FAILURE;
2630 #endif /* HAVE_NGHTTP2 */
2632 /** Handle http2 read */
2634 comm_point_http2_handle_read(int ATTR_UNUSED(fd), struct comm_point* c)
2638 log_assert(c->h2_session);
2640 /* reading until recv cb returns NGHTTP2_ERR_WOULDBLOCK */
2641 ret = nghttp2_session_recv(c->h2_session->session);
2643 if(ret != NGHTTP2_ERR_EOF &&
2644 ret != NGHTTP2_ERR_CALLBACK_FAILURE) {
2646 addr_to_str(&c->repinfo.addr, c->repinfo.addrlen,
2648 verbose(VERB_QUERY, "http2: session_recv from %s failed, "
2649 "error: %s", a, nghttp2_strerror(ret));
2653 if(nghttp2_session_want_write(c->h2_session->session)) {
2654 c->tcp_is_reading = 0;
2655 comm_point_stop_listening(c);
2656 comm_point_start_listening(c, -1, c->tcp_timeout_msec);
2657 } else if(!nghttp2_session_want_read(c->h2_session->session))
2658 return 0; /* connection can be closed */
2667 * Handle http reading callback.
2668 * @param fd: file descriptor of socket.
2669 * @param c: comm point to read from into buffer.
2670 * @return: 0 on error
2673 comm_point_http_handle_read(int fd, struct comm_point* c)
2675 log_assert(c->type == comm_http);
2676 log_assert(fd != -1);
2678 /* if we are in ssl handshake, handle SSL handshake */
2680 if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
2681 if(!ssl_handshake(c))
2683 if(c->ssl_shake_state != comm_ssl_shake_none)
2686 #endif /* HAVE_SSL */
2688 if(!c->tcp_is_reading)
2692 return comm_point_http2_handle_read(fd, c);
2695 /* http version is <= http/1.1 */
2697 if(c->http_min_version >= http_version_2) {
2698 /* HTTP/2 failed, not allowed to use lower version. */
2702 /* read more data */
2704 if(!ssl_http_read_more(c))
2707 if(!http_read_more(fd, c))
2711 sldns_buffer_flip(c->buffer);
2713 while(sldns_buffer_remaining(c->buffer) > 0) {
2714 /* Handle HTTP/1.x data */
2715 /* if we are reading headers, read more headers */
2716 if(c->http_in_headers || c->http_in_chunk_headers) {
2717 /* if header is done, process the header */
2718 if(!http_header_done(c->buffer)) {
2719 /* copy remaining data to front of buffer
2720 * and set rest for writing into it */
2721 http_moveover_buffer(c->buffer);
2722 /* return and wait to read more */
2725 if(!c->http_in_chunk_headers) {
2726 /* process initial headers */
2727 if(!http_process_initial_header(c))
2730 /* process chunk headers */
2731 int r = http_process_chunk_header(c);
2732 if(r == 0) return 0;
2733 if(r == 2) return 1; /* done */
2734 /* r == 1, continue */
2736 /* see if we have more to process */
2740 if(!c->http_is_chunked) {
2741 /* if we are reading nonchunks, process that*/
2742 return http_nonchunk_segment(c);
2744 /* if we are reading chunks, read the chunk */
2745 int r = http_chunked_segment(c);
2746 if(r == 0) return 0;
2747 if(r == 1) return 1;
2751 /* broke out of the loop; could not process header instead need
2753 /* moveover any remaining data and read more data */
2754 http_moveover_buffer(c->buffer);
2755 /* return and wait to read more */
2759 /** check pending connect for http */
2761 http_check_connect(int fd, struct comm_point* c)
2763 /* check for pending error from nonblocking connect */
2764 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/
2766 socklen_t len = (socklen_t)sizeof(error);
2767 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
2770 error = errno; /* on solaris errno is error */
2771 #else /* USE_WINSOCK */
2772 error = WSAGetLastError();
2776 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
2777 if(error == EINPROGRESS || error == EWOULDBLOCK)
2778 return 1; /* try again later */
2781 if(error != 0 && verbosity < 2)
2782 return 0; /* silence lots of chatter in the logs */
2783 else if(error != 0) {
2784 log_err_addr("http connect", strerror(error),
2785 &c->repinfo.addr, c->repinfo.addrlen);
2786 #else /* USE_WINSOCK */
2788 if(error == WSAEINPROGRESS)
2790 else if(error == WSAEWOULDBLOCK) {
2791 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2793 } else if(error != 0 && verbosity < 2)
2795 else if(error != 0) {
2796 log_err_addr("http connect", wsa_strerror(error),
2797 &c->repinfo.addr, c->repinfo.addrlen);
2798 #endif /* USE_WINSOCK */
2801 /* keep on processing this socket */
2805 /** write more data for http (with ssl) */
2807 ssl_http_write_more(struct comm_point* c)
2811 log_assert(sldns_buffer_remaining(c->buffer) > 0);
2813 r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
2814 (int)sldns_buffer_remaining(c->buffer));
2816 int want = SSL_get_error(c->ssl, r);
2817 if(want == SSL_ERROR_ZERO_RETURN) {
2818 return 0; /* closed */
2819 } else if(want == SSL_ERROR_WANT_READ) {
2820 c->ssl_shake_state = comm_ssl_shake_hs_read;
2821 comm_point_listen_for_rw(c, 1, 0);
2822 return 1; /* wait for read condition */
2823 } else if(want == SSL_ERROR_WANT_WRITE) {
2824 return 1; /* write more later */
2825 } else if(want == SSL_ERROR_SYSCALL) {
2827 if(errno == EPIPE && verbosity < 2)
2828 return 0; /* silence 'broken pipe' */
2831 log_err("SSL_write syscall: %s",
2835 log_crypto_err("could not SSL_write");
2838 sldns_buffer_skip(c->buffer, (ssize_t)r);
2843 #endif /* HAVE_SSL */
2846 /** write more data for http */
2848 http_write_more(int fd, struct comm_point* c)
2851 log_assert(sldns_buffer_remaining(c->buffer) > 0);
2852 r = send(fd, (void*)sldns_buffer_current(c->buffer),
2853 sldns_buffer_remaining(c->buffer), 0);
2856 if(errno == EINTR || errno == EAGAIN)
2859 if(WSAGetLastError() == WSAEINPROGRESS)
2861 if(WSAGetLastError() == WSAEWOULDBLOCK) {
2862 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2866 log_err_addr("http send r", sock_strerror(errno),
2867 &c->repinfo.addr, c->repinfo.addrlen);
2870 sldns_buffer_skip(c->buffer, r);
2875 ssize_t http2_send_cb(nghttp2_session* ATTR_UNUSED(session), const uint8_t* buf,
2876 size_t len, int ATTR_UNUSED(flags), void* cb_arg)
2879 struct http2_session* h2_session = (struct http2_session*)cb_arg;
2880 log_assert(h2_session->c->type == comm_http);
2881 log_assert(h2_session->c->h2_session);
2884 if(h2_session->c->ssl) {
2887 r = SSL_write(h2_session->c->ssl, buf, len);
2889 int want = SSL_get_error(h2_session->c->ssl, r);
2890 if(want == SSL_ERROR_ZERO_RETURN) {
2891 return NGHTTP2_ERR_CALLBACK_FAILURE;
2892 } else if(want == SSL_ERROR_WANT_READ) {
2893 h2_session->c->ssl_shake_state = comm_ssl_shake_hs_read;
2894 comm_point_listen_for_rw(h2_session->c, 1, 0);
2895 return NGHTTP2_ERR_WOULDBLOCK;
2896 } else if(want == SSL_ERROR_WANT_WRITE) {
2897 return NGHTTP2_ERR_WOULDBLOCK;
2898 } else if(want == SSL_ERROR_SYSCALL) {
2900 if(errno == EPIPE && verbosity < 2)
2901 return NGHTTP2_ERR_CALLBACK_FAILURE;
2904 log_err("SSL_write syscall: %s",
2906 return NGHTTP2_ERR_CALLBACK_FAILURE;
2908 log_crypto_err("could not SSL_write");
2909 return NGHTTP2_ERR_CALLBACK_FAILURE;
2913 #endif /* HAVE_SSL */
2915 ret = send(h2_session->c->fd, buf, len, 0);
2917 return NGHTTP2_ERR_CALLBACK_FAILURE;
2918 } else if(ret < 0) {
2920 if(errno == EINTR || errno == EAGAIN)
2921 return NGHTTP2_ERR_WOULDBLOCK;
2923 if(errno == EPIPE && verbosity < 2)
2924 return NGHTTP2_ERR_CALLBACK_FAILURE;
2927 if(errno == ECONNRESET && verbosity < 2)
2928 return NGHTTP2_ERR_CALLBACK_FAILURE;
2930 log_err_addr("could not http2 write: %s", strerror(errno),
2931 &h2_session->c->repinfo.addr,
2932 h2_session->c->repinfo.addrlen);
2933 #else /* USE_WINSOCK */
2934 if(WSAGetLastError() == WSAENOTCONN)
2935 return NGHTTP2_ERR_WOULDBLOCK;
2936 if(WSAGetLastError() == WSAEINPROGRESS)
2937 return NGHTTP2_ERR_WOULDBLOCK;
2938 if(WSAGetLastError() == WSAEWOULDBLOCK) {
2939 ub_winsock_tcp_wouldblock(h2_session->c->ev->ev,
2941 return NGHTTP2_ERR_WOULDBLOCK;
2943 if(WSAGetLastError() == WSAECONNRESET && verbosity < 2)
2944 return NGHTTP2_ERR_CALLBACK_FAILURE;
2945 log_err_addr("could not http2 write: %s",
2946 wsa_strerror(WSAGetLastError()),
2947 &h2_session->c->repinfo.addr,
2948 h2_session->c->repinfo.addrlen);
2950 return NGHTTP2_ERR_CALLBACK_FAILURE;
2954 #endif /* HAVE_NGHTTP2 */
2956 /** Handle http2 writing */
2958 comm_point_http2_handle_write(int ATTR_UNUSED(fd), struct comm_point* c)
2962 log_assert(c->h2_session);
2964 ret = nghttp2_session_send(c->h2_session->session);
2966 verbose(VERB_QUERY, "http2: session_send failed, "
2967 "error: %s", nghttp2_strerror(ret));
2971 if(nghttp2_session_want_read(c->h2_session->session)) {
2972 c->tcp_is_reading = 1;
2973 comm_point_stop_listening(c);
2974 comm_point_start_listening(c, -1, c->tcp_timeout_msec);
2975 } else if(!nghttp2_session_want_write(c->h2_session->session))
2976 return 0; /* connection can be closed */
2985 * Handle http writing callback.
2986 * @param fd: file descriptor of socket.
2987 * @param c: comm point to write buffer out of.
2988 * @return: 0 on error
2991 comm_point_http_handle_write(int fd, struct comm_point* c)
2993 log_assert(c->type == comm_http);
2994 log_assert(fd != -1);
2996 /* check pending connect errors, if that fails, we wait for more,
2997 * or we can continue to write contents */
2998 if(c->tcp_check_nb_connect) {
2999 int r = http_check_connect(fd, c);
3000 if(r == 0) return 0;
3001 if(r == 1) return 1;
3002 c->tcp_check_nb_connect = 0;
3004 /* if we are in ssl handshake, handle SSL handshake */
3006 if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
3007 if(!ssl_handshake(c))
3009 if(c->ssl_shake_state != comm_ssl_shake_none)
3012 #endif /* HAVE_SSL */
3013 if(c->tcp_is_reading)
3017 return comm_point_http2_handle_write(fd, c);
3020 /* http version is <= http/1.1 */
3022 if(c->http_min_version >= http_version_2) {
3023 /* HTTP/2 failed, not allowed to use lower version. */
3027 /* if we are writing, write more */
3029 if(!ssl_http_write_more(c))
3032 if(!http_write_more(fd, c))
3036 /* we write a single buffer contents, that can contain
3037 * the http request, and then flip to read the results */
3038 /* see if write is done */
3039 if(sldns_buffer_remaining(c->buffer) == 0) {
3040 sldns_buffer_clear(c->buffer);
3041 if(c->tcp_do_toggle_rw)
3042 c->tcp_is_reading = 1;
3043 c->tcp_byte_count = 0;
3044 /* switch from listening(write) to listening(read) */
3045 comm_point_stop_listening(c);
3046 comm_point_start_listening(c, -1, -1);
3052 comm_point_http_handle_callback(int fd, short event, void* arg)
3054 struct comm_point* c = (struct comm_point*)arg;
3055 log_assert(c->type == comm_http);
3056 ub_comm_base_now(c->ev->base);
3058 if(event&UB_EV_TIMEOUT) {
3059 verbose(VERB_QUERY, "http took too long, dropped");
3060 reclaim_http_handler(c);
3061 if(!c->tcp_do_close) {
3062 fptr_ok(fptr_whitelist_comm_point(c->callback));
3063 (void)(*c->callback)(c, c->cb_arg,
3064 NETEVENT_TIMEOUT, NULL);
3068 if(event&UB_EV_READ) {
3069 if(!comm_point_http_handle_read(fd, c)) {
3070 reclaim_http_handler(c);
3071 if(!c->tcp_do_close) {
3072 fptr_ok(fptr_whitelist_comm_point(
3074 (void)(*c->callback)(c, c->cb_arg,
3075 NETEVENT_CLOSED, NULL);
3080 if(event&UB_EV_WRITE) {
3081 if(!comm_point_http_handle_write(fd, c)) {
3082 reclaim_http_handler(c);
3083 if(!c->tcp_do_close) {
3084 fptr_ok(fptr_whitelist_comm_point(
3086 (void)(*c->callback)(c, c->cb_arg,
3087 NETEVENT_CLOSED, NULL);
3092 log_err("Ignored event %d for httphdl.", event);
3095 void comm_point_local_handle_callback(int fd, short event, void* arg)
3097 struct comm_point* c = (struct comm_point*)arg;
3098 log_assert(c->type == comm_local);
3099 ub_comm_base_now(c->ev->base);
3101 if(event&UB_EV_READ) {
3102 if(!comm_point_tcp_handle_read(fd, c, 1)) {
3103 fptr_ok(fptr_whitelist_comm_point(c->callback));
3104 (void)(*c->callback)(c, c->cb_arg, NETEVENT_CLOSED,
3109 log_err("Ignored event %d for localhdl.", event);
3112 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
3113 short event, void* arg)
3115 struct comm_point* c = (struct comm_point*)arg;
3116 int err = NETEVENT_NOERROR;
3117 log_assert(c->type == comm_raw);
3118 ub_comm_base_now(c->ev->base);
3120 if(event&UB_EV_TIMEOUT)
3121 err = NETEVENT_TIMEOUT;
3122 fptr_ok(fptr_whitelist_comm_point_raw(c->callback));
3123 (void)(*c->callback)(c, c->cb_arg, err, NULL);
3127 comm_point_create_udp(struct comm_base *base, int fd, sldns_buffer* buffer,
3128 comm_point_callback_type* callback, void* callback_arg)
3130 struct comm_point* c = (struct comm_point*)calloc(1,
3131 sizeof(struct comm_point));
3135 c->ev = (struct internal_event*)calloc(1,
3136 sizeof(struct internal_event));
3145 c->tcp_is_reading = 0;
3146 c->tcp_byte_count = 0;
3147 c->tcp_parent = NULL;
3148 c->max_tcp_count = 0;
3149 c->cur_tcp_count = 0;
3150 c->tcp_handlers = NULL;
3153 c->tcp_do_close = 0;
3154 c->do_not_close = 0;
3155 c->tcp_do_toggle_rw = 0;
3156 c->tcp_check_nb_connect = 0;
3157 #ifdef USE_MSG_FASTOPEN
3158 c->tcp_do_fastopen = 0;
3162 c->dnscrypt_buffer = buffer;
3165 c->callback = callback;
3166 c->cb_arg = callback_arg;
3167 evbits = UB_EV_READ | UB_EV_PERSIST;
3168 /* ub_event stuff */
3169 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3170 comm_point_udp_callback, c);
3171 if(c->ev->ev == NULL) {
3172 log_err("could not baseset udp event");
3173 comm_point_delete(c);
3176 if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) {
3177 log_err("could not add udp event");
3178 comm_point_delete(c);
3185 comm_point_create_udp_ancil(struct comm_base *base, int fd,
3186 sldns_buffer* buffer,
3187 comm_point_callback_type* callback, void* callback_arg)
3189 struct comm_point* c = (struct comm_point*)calloc(1,
3190 sizeof(struct comm_point));
3194 c->ev = (struct internal_event*)calloc(1,
3195 sizeof(struct internal_event));
3204 c->tcp_is_reading = 0;
3205 c->tcp_byte_count = 0;
3206 c->tcp_parent = NULL;
3207 c->max_tcp_count = 0;
3208 c->cur_tcp_count = 0;
3209 c->tcp_handlers = NULL;
3212 c->tcp_do_close = 0;
3213 c->do_not_close = 0;
3216 c->dnscrypt_buffer = buffer;
3219 c->tcp_do_toggle_rw = 0;
3220 c->tcp_check_nb_connect = 0;
3221 #ifdef USE_MSG_FASTOPEN
3222 c->tcp_do_fastopen = 0;
3224 c->callback = callback;
3225 c->cb_arg = callback_arg;
3226 evbits = UB_EV_READ | UB_EV_PERSIST;
3227 /* ub_event stuff */
3228 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3229 comm_point_udp_ancil_callback, c);
3230 if(c->ev->ev == NULL) {
3231 log_err("could not baseset udp event");
3232 comm_point_delete(c);
3235 if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) {
3236 log_err("could not add udp event");
3237 comm_point_delete(c);
3243 static struct comm_point*
3244 comm_point_create_tcp_handler(struct comm_base *base,
3245 struct comm_point* parent, size_t bufsize,
3246 struct sldns_buffer* spoolbuf, comm_point_callback_type* callback,
3249 struct comm_point* c = (struct comm_point*)calloc(1,
3250 sizeof(struct comm_point));
3254 c->ev = (struct internal_event*)calloc(1,
3255 sizeof(struct internal_event));
3262 c->buffer = sldns_buffer_new(bufsize);
3268 c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
3270 sldns_buffer_free(c->buffer);
3275 c->tcp_is_reading = 0;
3276 c->tcp_byte_count = 0;
3277 c->tcp_parent = parent;
3278 c->tcp_timeout_msec = parent->tcp_timeout_msec;
3279 c->tcp_conn_limit = parent->tcp_conn_limit;
3281 c->tcp_keepalive = 0;
3282 c->max_tcp_count = 0;
3283 c->cur_tcp_count = 0;
3284 c->tcp_handlers = NULL;
3287 c->tcp_do_close = 0;
3288 c->do_not_close = 0;
3289 c->tcp_do_toggle_rw = 1;
3290 c->tcp_check_nb_connect = 0;
3291 #ifdef USE_MSG_FASTOPEN
3292 c->tcp_do_fastopen = 0;
3296 /* We don't know just yet if this is a dnscrypt channel. Allocation
3297 * will be done when handling the callback. */
3298 c->dnscrypt_buffer = c->buffer;
3301 c->callback = callback;
3302 c->cb_arg = callback_arg;
3304 c->tcp_req_info = tcp_req_info_create(spoolbuf);
3305 if(!c->tcp_req_info) {
3306 log_err("could not create tcp commpoint");
3307 sldns_buffer_free(c->buffer);
3313 c->tcp_req_info->cp = c;
3314 c->tcp_do_close = 1;
3315 c->tcp_do_toggle_rw = 0;
3317 /* add to parent free list */
3318 c->tcp_free = parent->tcp_free;
3319 parent->tcp_free = c;
3320 /* ub_event stuff */
3321 evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT;
3322 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3323 comm_point_tcp_handle_callback, c);
3324 if(c->ev->ev == NULL)
3326 log_err("could not basetset tcphdl event");
3327 parent->tcp_free = c->tcp_free;
3328 tcp_req_info_delete(c->tcp_req_info);
3329 sldns_buffer_free(c->buffer);
3338 static struct comm_point*
3339 comm_point_create_http_handler(struct comm_base *base,
3340 struct comm_point* parent, size_t bufsize, int harden_large_queries,
3341 uint32_t http_max_streams, char* http_endpoint,
3342 comm_point_callback_type* callback, void* callback_arg)
3344 struct comm_point* c = (struct comm_point*)calloc(1,
3345 sizeof(struct comm_point));
3349 c->ev = (struct internal_event*)calloc(1,
3350 sizeof(struct internal_event));
3357 c->buffer = sldns_buffer_new(bufsize);
3363 c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
3365 sldns_buffer_free(c->buffer);
3370 c->tcp_is_reading = 0;
3371 c->tcp_byte_count = 0;
3372 c->tcp_parent = parent;
3373 c->tcp_timeout_msec = parent->tcp_timeout_msec;
3374 c->tcp_conn_limit = parent->tcp_conn_limit;
3376 c->tcp_keepalive = 0;
3377 c->max_tcp_count = 0;
3378 c->cur_tcp_count = 0;
3379 c->tcp_handlers = NULL;
3381 c->type = comm_http;
3382 c->tcp_do_close = 1;
3383 c->do_not_close = 0;
3384 c->tcp_do_toggle_rw = 1; /* will be set to 0 after http2 upgrade */
3385 c->tcp_check_nb_connect = 0;
3386 #ifdef USE_MSG_FASTOPEN
3387 c->tcp_do_fastopen = 0;
3391 c->dnscrypt_buffer = NULL;
3394 c->callback = callback;
3395 c->cb_arg = callback_arg;
3397 c->http_min_version = http_version_2;
3398 c->http2_stream_max_qbuffer_size = bufsize;
3399 if(harden_large_queries && bufsize > 512)
3400 c->http2_stream_max_qbuffer_size = 512;
3401 c->http2_max_streams = http_max_streams;
3402 if(!(c->http_endpoint = strdup(http_endpoint))) {
3403 log_err("could not strdup http_endpoint");
3404 sldns_buffer_free(c->buffer);
3412 if(!(c->h2_session = http2_session_create(c))) {
3413 log_err("could not create http2 session");
3414 free(c->http_endpoint);
3415 sldns_buffer_free(c->buffer);
3421 if(!(c->h2_session->callbacks = http2_req_callbacks_create())) {
3422 log_err("could not create http2 callbacks");
3423 http2_session_delete(c->h2_session);
3424 free(c->http_endpoint);
3425 sldns_buffer_free(c->buffer);
3433 /* add to parent free list */
3434 c->tcp_free = parent->tcp_free;
3435 parent->tcp_free = c;
3436 /* ub_event stuff */
3437 evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT;
3438 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3439 comm_point_http_handle_callback, c);
3440 if(c->ev->ev == NULL)
3442 log_err("could not set http handler event");
3443 parent->tcp_free = c->tcp_free;
3444 http2_session_delete(c->h2_session);
3445 sldns_buffer_free(c->buffer);
3455 comm_point_create_tcp(struct comm_base *base, int fd, int num,
3456 int idle_timeout, int harden_large_queries,
3457 uint32_t http_max_streams, char* http_endpoint,
3458 struct tcl_list* tcp_conn_limit, size_t bufsize,
3459 struct sldns_buffer* spoolbuf, enum listen_type port_type,
3460 comm_point_callback_type* callback, void* callback_arg)
3462 struct comm_point* c = (struct comm_point*)calloc(1,
3463 sizeof(struct comm_point));
3466 /* first allocate the TCP accept listener */
3469 c->ev = (struct internal_event*)calloc(1,
3470 sizeof(struct internal_event));
3479 c->tcp_is_reading = 0;
3480 c->tcp_byte_count = 0;
3481 c->tcp_timeout_msec = idle_timeout;
3482 c->tcp_conn_limit = tcp_conn_limit;
3484 c->tcp_keepalive = 0;
3485 c->tcp_parent = NULL;
3486 c->max_tcp_count = num;
3487 c->cur_tcp_count = 0;
3488 c->tcp_handlers = (struct comm_point**)calloc((size_t)num,
3489 sizeof(struct comm_point*));
3490 if(!c->tcp_handlers) {
3496 c->type = comm_tcp_accept;
3497 c->tcp_do_close = 0;
3498 c->do_not_close = 0;
3499 c->tcp_do_toggle_rw = 0;
3500 c->tcp_check_nb_connect = 0;
3501 #ifdef USE_MSG_FASTOPEN
3502 c->tcp_do_fastopen = 0;
3506 c->dnscrypt_buffer = NULL;
3510 evbits = UB_EV_READ | UB_EV_PERSIST;
3511 /* ub_event stuff */
3512 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3513 comm_point_tcp_accept_callback, c);
3514 if(c->ev->ev == NULL) {
3515 log_err("could not baseset tcpacc event");
3516 comm_point_delete(c);
3519 if (ub_event_add(c->ev->ev, c->timeout) != 0) {
3520 log_err("could not add tcpacc event");
3521 comm_point_delete(c);
3524 /* now prealloc the handlers */
3525 for(i=0; i<num; i++) {
3526 if(port_type == listen_type_tcp ||
3527 port_type == listen_type_ssl ||
3528 port_type == listen_type_tcp_dnscrypt) {
3529 c->tcp_handlers[i] = comm_point_create_tcp_handler(base,
3530 c, bufsize, spoolbuf, callback, callback_arg);
3531 } else if(port_type == listen_type_http) {
3532 c->tcp_handlers[i] = comm_point_create_http_handler(
3533 base, c, bufsize, harden_large_queries,
3534 http_max_streams, http_endpoint,
3535 callback, callback_arg);
3538 log_err("could not create tcp handler, unknown listen "
3542 if(!c->tcp_handlers[i]) {
3543 comm_point_delete(c);
3552 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize,
3553 comm_point_callback_type* callback, void* callback_arg)
3555 struct comm_point* c = (struct comm_point*)calloc(1,
3556 sizeof(struct comm_point));
3560 c->ev = (struct internal_event*)calloc(1,
3561 sizeof(struct internal_event));
3568 c->buffer = sldns_buffer_new(bufsize);
3575 c->tcp_is_reading = 0;
3576 c->tcp_byte_count = 0;
3577 c->tcp_timeout_msec = TCP_QUERY_TIMEOUT;
3578 c->tcp_conn_limit = NULL;
3580 c->tcp_keepalive = 0;
3581 c->tcp_parent = NULL;
3582 c->max_tcp_count = 0;
3583 c->cur_tcp_count = 0;
3584 c->tcp_handlers = NULL;
3587 c->tcp_do_close = 0;
3588 c->do_not_close = 0;
3589 c->tcp_do_toggle_rw = 1;
3590 c->tcp_check_nb_connect = 1;
3591 #ifdef USE_MSG_FASTOPEN
3592 c->tcp_do_fastopen = 1;
3596 c->dnscrypt_buffer = c->buffer;
3599 c->callback = callback;
3600 c->cb_arg = callback_arg;
3601 evbits = UB_EV_PERSIST | UB_EV_WRITE;
3602 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3603 comm_point_tcp_handle_callback, c);
3604 if(c->ev->ev == NULL)
3606 log_err("could not baseset tcpout event");
3607 sldns_buffer_free(c->buffer);
3617 comm_point_create_http_out(struct comm_base *base, size_t bufsize,
3618 comm_point_callback_type* callback, void* callback_arg,
3621 struct comm_point* c = (struct comm_point*)calloc(1,
3622 sizeof(struct comm_point));
3626 c->ev = (struct internal_event*)calloc(1,
3627 sizeof(struct internal_event));
3634 c->buffer = sldns_buffer_new(bufsize);
3641 c->tcp_is_reading = 0;
3642 c->tcp_byte_count = 0;
3643 c->tcp_parent = NULL;
3644 c->max_tcp_count = 0;
3645 c->cur_tcp_count = 0;
3646 c->tcp_handlers = NULL;
3648 c->type = comm_http;
3649 c->tcp_do_close = 0;
3650 c->do_not_close = 0;
3651 c->tcp_do_toggle_rw = 1;
3652 c->tcp_check_nb_connect = 1;
3653 c->http_in_headers = 1;
3654 c->http_in_chunk_headers = 0;
3655 c->http_is_chunked = 0;
3656 c->http_temp = temp;
3657 #ifdef USE_MSG_FASTOPEN
3658 c->tcp_do_fastopen = 1;
3662 c->dnscrypt_buffer = c->buffer;
3665 c->callback = callback;
3666 c->cb_arg = callback_arg;
3667 evbits = UB_EV_PERSIST | UB_EV_WRITE;
3668 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3669 comm_point_http_handle_callback, c);
3670 if(c->ev->ev == NULL)
3672 log_err("could not baseset tcpout event");
3676 sldns_buffer_free(c->buffer);
3686 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize,
3687 comm_point_callback_type* callback, void* callback_arg)
3689 struct comm_point* c = (struct comm_point*)calloc(1,
3690 sizeof(struct comm_point));
3694 c->ev = (struct internal_event*)calloc(1,
3695 sizeof(struct internal_event));
3702 c->buffer = sldns_buffer_new(bufsize);
3709 c->tcp_is_reading = 1;
3710 c->tcp_byte_count = 0;
3711 c->tcp_parent = NULL;
3712 c->max_tcp_count = 0;
3713 c->cur_tcp_count = 0;
3714 c->tcp_handlers = NULL;
3716 c->type = comm_local;
3717 c->tcp_do_close = 0;
3718 c->do_not_close = 1;
3719 c->tcp_do_toggle_rw = 0;
3720 c->tcp_check_nb_connect = 0;
3721 #ifdef USE_MSG_FASTOPEN
3722 c->tcp_do_fastopen = 0;
3726 c->dnscrypt_buffer = c->buffer;
3728 c->callback = callback;
3729 c->cb_arg = callback_arg;
3730 /* ub_event stuff */
3731 evbits = UB_EV_PERSIST | UB_EV_READ;
3732 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3733 comm_point_local_handle_callback, c);
3734 if(c->ev->ev == NULL) {
3735 log_err("could not baseset localhdl event");
3740 if (ub_event_add(c->ev->ev, c->timeout) != 0) {
3741 log_err("could not add localhdl event");
3742 ub_event_free(c->ev->ev);
3751 comm_point_create_raw(struct comm_base* base, int fd, int writing,
3752 comm_point_callback_type* callback, void* callback_arg)
3754 struct comm_point* c = (struct comm_point*)calloc(1,
3755 sizeof(struct comm_point));
3759 c->ev = (struct internal_event*)calloc(1,
3760 sizeof(struct internal_event));
3769 c->tcp_is_reading = 0;
3770 c->tcp_byte_count = 0;
3771 c->tcp_parent = NULL;
3772 c->max_tcp_count = 0;
3773 c->cur_tcp_count = 0;
3774 c->tcp_handlers = NULL;
3777 c->tcp_do_close = 0;
3778 c->do_not_close = 1;
3779 c->tcp_do_toggle_rw = 0;
3780 c->tcp_check_nb_connect = 0;
3781 #ifdef USE_MSG_FASTOPEN
3782 c->tcp_do_fastopen = 0;
3786 c->dnscrypt_buffer = c->buffer;
3788 c->callback = callback;
3789 c->cb_arg = callback_arg;
3790 /* ub_event stuff */
3792 evbits = UB_EV_PERSIST | UB_EV_WRITE;
3793 else evbits = UB_EV_PERSIST | UB_EV_READ;
3794 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3795 comm_point_raw_handle_callback, c);
3796 if(c->ev->ev == NULL) {
3797 log_err("could not baseset rawhdl event");
3802 if (ub_event_add(c->ev->ev, c->timeout) != 0) {
3803 log_err("could not add rawhdl event");
3804 ub_event_free(c->ev->ev);
3813 comm_point_close(struct comm_point* c)
3818 verbose(5, "comm_point_close of %d: event_del", c->fd);
3819 if(ub_event_del(c->ev->ev) != 0) {
3820 log_err("could not event_del on close");
3823 tcl_close_connection(c->tcl_addr);
3825 tcp_req_info_clear(c->tcp_req_info);
3827 http2_session_server_delete(c->h2_session);
3829 /* close fd after removing from event lists, or epoll.. is messed up */
3830 if(c->fd != -1 && !c->do_not_close) {
3831 if(c->type == comm_tcp || c->type == comm_http) {
3832 /* delete sticky events for the fd, it gets closed */
3833 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
3834 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
3836 verbose(VERB_ALGO, "close fd %d", c->fd);
3843 comm_point_delete(struct comm_point* c)
3847 if((c->type == comm_tcp || c->type == comm_http) && c->ssl) {
3849 SSL_shutdown(c->ssl);
3853 if(c->type == comm_http && c->http_endpoint) {
3854 free(c->http_endpoint);
3855 c->http_endpoint = NULL;
3857 comm_point_close(c);
3858 if(c->tcp_handlers) {
3860 for(i=0; i<c->max_tcp_count; i++)
3861 comm_point_delete(c->tcp_handlers[i]);
3862 free(c->tcp_handlers);
3865 if(c->type == comm_tcp || c->type == comm_local || c->type == comm_http) {
3866 sldns_buffer_free(c->buffer);
3868 if(c->dnscrypt && c->dnscrypt_buffer != c->buffer) {
3869 sldns_buffer_free(c->dnscrypt_buffer);
3872 if(c->tcp_req_info) {
3873 tcp_req_info_delete(c->tcp_req_info);
3876 http2_session_delete(c->h2_session);
3879 ub_event_free(c->ev->ev);
3885 comm_point_send_reply(struct comm_reply *repinfo)
3887 struct sldns_buffer* buffer;
3888 log_assert(repinfo && repinfo->c);
3890 buffer = repinfo->c->dnscrypt_buffer;
3891 if(!dnsc_handle_uncurved_request(repinfo)) {
3895 buffer = repinfo->c->buffer;
3897 if(repinfo->c->type == comm_udp) {
3898 if(repinfo->srctype)
3899 comm_point_send_udp_msg_if(repinfo->c,
3900 buffer, (struct sockaddr*)&repinfo->addr,
3901 repinfo->addrlen, repinfo);
3903 comm_point_send_udp_msg(repinfo->c, buffer,
3904 (struct sockaddr*)&repinfo->addr, repinfo->addrlen);
3906 if(repinfo->c->dtenv != NULL &&
3907 repinfo->c->dtenv->log_client_response_messages)
3908 dt_msg_send_client_response(repinfo->c->dtenv,
3909 &repinfo->addr, repinfo->c->type, repinfo->c->buffer);
3913 if(repinfo->c->tcp_parent->dtenv != NULL &&
3914 repinfo->c->tcp_parent->dtenv->log_client_response_messages)
3915 dt_msg_send_client_response(repinfo->c->tcp_parent->dtenv,
3916 &repinfo->addr, repinfo->c->type,
3917 ( repinfo->c->tcp_req_info
3918 ? repinfo->c->tcp_req_info->spool_buffer
3919 : repinfo->c->buffer ));
3921 if(repinfo->c->tcp_req_info) {
3922 tcp_req_info_send_reply(repinfo->c->tcp_req_info);
3923 } else if(repinfo->c->use_h2) {
3924 if(!http2_submit_dns_response(repinfo->c->h2_session)) {
3925 comm_point_drop_reply(repinfo);
3928 repinfo->c->h2_stream = NULL;
3929 repinfo->c->tcp_is_reading = 0;
3930 comm_point_stop_listening(repinfo->c);
3931 comm_point_start_listening(repinfo->c, -1,
3932 repinfo->c->tcp_timeout_msec);
3935 comm_point_start_listening(repinfo->c, -1,
3936 repinfo->c->tcp_timeout_msec);
3942 comm_point_drop_reply(struct comm_reply* repinfo)
3946 log_assert(repinfo->c);
3947 log_assert(repinfo->c->type != comm_tcp_accept);
3948 if(repinfo->c->type == comm_udp)
3950 if(repinfo->c->tcp_req_info)
3951 repinfo->c->tcp_req_info->is_drop = 1;
3952 if(repinfo->c->type == comm_http) {
3953 if(repinfo->c->h2_session) {
3954 repinfo->c->h2_session->is_drop = 1;
3955 if(!repinfo->c->h2_session->postpone_drop)
3956 reclaim_http_handler(repinfo->c);
3959 reclaim_http_handler(repinfo->c);
3962 reclaim_tcp_handler(repinfo->c);
3966 comm_point_stop_listening(struct comm_point* c)
3968 verbose(VERB_ALGO, "comm point stop listening %d", c->fd);
3969 if(ub_event_del(c->ev->ev) != 0) {
3970 log_err("event_del error to stoplisten");
3975 comm_point_start_listening(struct comm_point* c, int newfd, int msec)
3977 verbose(VERB_ALGO, "comm point start listening %d (%d msec)",
3978 c->fd==-1?newfd:c->fd, msec);
3979 if(c->type == comm_tcp_accept && !c->tcp_free) {
3980 /* no use to start listening no free slots. */
3983 if(msec != -1 && msec != 0) {
3985 c->timeout = (struct timeval*)malloc(sizeof(
3988 log_err("cpsl: malloc failed. No net read.");
3992 ub_event_add_bits(c->ev->ev, UB_EV_TIMEOUT);
3993 #ifndef S_SPLINT_S /* splint fails on struct timeval. */
3994 c->timeout->tv_sec = msec/1000;
3995 c->timeout->tv_usec = (msec%1000)*1000;
3996 #endif /* S_SPLINT_S */
3998 if(c->type == comm_tcp || c->type == comm_http) {
3999 ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
4000 if(c->tcp_write_and_read) {
4001 verbose(5, "startlistening %d mode rw", (newfd==-1?c->fd:newfd));
4002 ub_event_add_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
4003 } else if(c->tcp_is_reading) {
4004 verbose(5, "startlistening %d mode r", (newfd==-1?c->fd:newfd));
4005 ub_event_add_bits(c->ev->ev, UB_EV_READ);
4007 verbose(5, "startlistening %d mode w", (newfd==-1?c->fd:newfd));
4008 ub_event_add_bits(c->ev->ev, UB_EV_WRITE);
4012 if(c->fd != -1 && c->fd != newfd) {
4013 verbose(5, "cpsl close of fd %d for %d", c->fd, newfd);
4017 ub_event_set_fd(c->ev->ev, c->fd);
4019 if(ub_event_add(c->ev->ev, msec==0?NULL:c->timeout) != 0) {
4020 log_err("event_add failed. in cpsl.");
4024 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr)
4026 verbose(VERB_ALGO, "comm point listen_for_rw %d %d", c->fd, wr);
4027 if(ub_event_del(c->ev->ev) != 0) {
4028 log_err("event_del error to cplf");
4030 ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
4031 if(rd) ub_event_add_bits(c->ev->ev, UB_EV_READ);
4032 if(wr) ub_event_add_bits(c->ev->ev, UB_EV_WRITE);
4033 if(ub_event_add(c->ev->ev, c->timeout) != 0) {
4034 log_err("event_add failed. in cplf.");
4038 size_t comm_point_get_mem(struct comm_point* c)
4043 s = sizeof(*c) + sizeof(*c->ev);
4045 s += sizeof(*c->timeout);
4046 if(c->type == comm_tcp || c->type == comm_local) {
4047 s += sizeof(*c->buffer) + sldns_buffer_capacity(c->buffer);
4049 s += sizeof(*c->dnscrypt_buffer);
4050 if(c->buffer != c->dnscrypt_buffer) {
4051 s += sldns_buffer_capacity(c->dnscrypt_buffer);
4055 if(c->type == comm_tcp_accept) {
4057 for(i=0; i<c->max_tcp_count; i++)
4058 s += comm_point_get_mem(c->tcp_handlers[i]);
4064 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
4066 struct internal_timer *tm = (struct internal_timer*)calloc(1,
4067 sizeof(struct internal_timer));
4069 log_err("malloc failed");
4072 tm->super.ev_timer = tm;
4074 tm->super.callback = cb;
4075 tm->super.cb_arg = cb_arg;
4076 tm->ev = ub_event_new(base->eb->base, -1, UB_EV_TIMEOUT,
4077 comm_timer_callback, &tm->super);
4078 if(tm->ev == NULL) {
4079 log_err("timer_create: event_base_set failed.");
4087 comm_timer_disable(struct comm_timer* timer)
4091 ub_timer_del(timer->ev_timer->ev);
4092 timer->ev_timer->enabled = 0;
4096 comm_timer_set(struct comm_timer* timer, struct timeval* tv)
4099 if(timer->ev_timer->enabled)
4100 comm_timer_disable(timer);
4101 if(ub_timer_add(timer->ev_timer->ev, timer->ev_timer->base->eb->base,
4102 comm_timer_callback, timer, tv) != 0)
4103 log_err("comm_timer_set: evtimer_add failed.");
4104 timer->ev_timer->enabled = 1;
4108 comm_timer_delete(struct comm_timer* timer)
4112 comm_timer_disable(timer);
4113 /* Free the sub struct timer->ev_timer derived from the super struct timer.
4114 * i.e. assert(timer == timer->ev_timer)
4116 ub_event_free(timer->ev_timer->ev);
4117 free(timer->ev_timer);
4121 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
4123 struct comm_timer* tm = (struct comm_timer*)arg;
4124 if(!(event&UB_EV_TIMEOUT))
4126 ub_comm_base_now(tm->ev_timer->base);
4127 tm->ev_timer->enabled = 0;
4128 fptr_ok(fptr_whitelist_comm_timer(tm->callback));
4129 (*tm->callback)(tm->cb_arg);
4133 comm_timer_is_set(struct comm_timer* timer)
4135 return (int)timer->ev_timer->enabled;
4139 comm_timer_get_mem(struct comm_timer* ATTR_UNUSED(timer))
4141 return sizeof(struct internal_timer);
4145 comm_signal_create(struct comm_base* base,
4146 void (*callback)(int, void*), void* cb_arg)
4148 struct comm_signal* com = (struct comm_signal*)malloc(
4149 sizeof(struct comm_signal));
4151 log_err("malloc failed");
4155 com->callback = callback;
4156 com->cb_arg = cb_arg;
4157 com->ev_signal = NULL;
4162 comm_signal_callback(int sig, short event, void* arg)
4164 struct comm_signal* comsig = (struct comm_signal*)arg;
4165 if(!(event & UB_EV_SIGNAL))
4167 ub_comm_base_now(comsig->base);
4168 fptr_ok(fptr_whitelist_comm_signal(comsig->callback));
4169 (*comsig->callback)(sig, comsig->cb_arg);
4173 comm_signal_bind(struct comm_signal* comsig, int sig)
4175 struct internal_signal* entry = (struct internal_signal*)calloc(1,
4176 sizeof(struct internal_signal));
4178 log_err("malloc failed");
4182 /* add signal event */
4183 entry->ev = ub_signal_new(comsig->base->eb->base, sig,
4184 comm_signal_callback, comsig);
4185 if(entry->ev == NULL) {
4186 log_err("Could not create signal event");
4190 if(ub_signal_add(entry->ev, NULL) != 0) {
4191 log_err("Could not add signal handler");
4192 ub_event_free(entry->ev);
4196 /* link into list */
4197 entry->next = comsig->ev_signal;
4198 comsig->ev_signal = entry;
4203 comm_signal_delete(struct comm_signal* comsig)
4205 struct internal_signal* p, *np;
4208 p=comsig->ev_signal;
4211 ub_signal_del(p->ev);
4212 ub_event_free(p->ev);