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/fptr_wlist.h"
47 #include "sldns/pkthdr.h"
48 #include "sldns/sbuffer.h"
49 #include "sldns/str2wire.h"
50 #include "dnstap/dnstap.h"
51 #include "dnscrypt/dnscrypt.h"
52 #ifdef HAVE_OPENSSL_SSL_H
53 #include <openssl/ssl.h>
55 #ifdef HAVE_OPENSSL_ERR_H
56 #include <openssl/err.h>
59 /* -------- Start of local definitions -------- */
60 /** if CMSG_ALIGN is not defined on this platform, a workaround */
63 # define CMSG_ALIGN(n) __CMSG_ALIGN(n)
64 # elif defined(CMSG_DATA_ALIGN)
65 # define CMSG_ALIGN _CMSG_DATA_ALIGN
67 # define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
71 /** if CMSG_LEN is not defined on this platform, a workaround */
73 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
76 /** if CMSG_SPACE is not defined on this platform, a workaround */
78 # ifdef _CMSG_HDR_ALIGN
79 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)))
81 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr)))
85 /** The TCP reading or writing query timeout in milliseconds */
86 #define TCP_QUERY_TIMEOUT 120000
87 /** The TCP timeout in msec for fast queries, above half are used */
88 #define TCP_QUERY_TIMEOUT_FAST 200
90 #ifndef NONBLOCKING_IS_BROKEN
91 /** number of UDP reads to perform per read indication from select */
92 #define NUM_UDP_PER_SELECT 100
94 #define NUM_UDP_PER_SELECT 1
98 * The internal event structure for keeping ub_event info for the event.
99 * Possibly other structures (list, tree) this is part of.
101 struct internal_event {
103 struct comm_base* base;
104 /** ub_event event type */
109 * Internal base structure, so that every thread has its own events.
111 struct internal_base {
112 /** ub_event event_base type. */
113 struct ub_event_base* base;
114 /** seconds time pointer points here */
116 /** timeval with current time */
118 /** the event used for slow_accept timeouts */
119 struct ub_event* slow_accept;
120 /** true if slow_accept is enabled */
121 int slow_accept_enabled;
125 * Internal timer structure, to store timer event in.
127 struct internal_timer {
128 /** the super struct from which derived */
129 struct comm_timer super;
131 struct comm_base* base;
132 /** ub_event event type */
134 /** is timer enabled */
139 * Internal signal structure, to store signal event in.
141 struct internal_signal {
142 /** ub_event event type */
144 /** next in signal list */
145 struct internal_signal* next;
148 /** create a tcp handler with a parent */
149 static struct comm_point* comm_point_create_tcp_handler(
150 struct comm_base *base, struct comm_point* parent, size_t bufsize,
151 comm_point_callback_type* callback, void* callback_arg);
153 /* -------- End of local definitions -------- */
156 comm_base_create(int sigs)
158 struct comm_base* b = (struct comm_base*)calloc(1,
159 sizeof(struct comm_base));
160 const char *evnm="event", *evsys="", *evmethod="";
164 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
169 b->eb->base = ub_default_event_base(sigs, &b->eb->secs, &b->eb->now);
176 ub_get_event_sys(b->eb->base, &evnm, &evsys, &evmethod);
177 verbose(VERB_ALGO, "%s %s user %s method.", evnm, evsys, evmethod);
182 comm_base_create_event(struct ub_event_base* base)
184 struct comm_base* b = (struct comm_base*)calloc(1,
185 sizeof(struct comm_base));
188 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
199 comm_base_delete(struct comm_base* b)
203 if(b->eb->slow_accept_enabled) {
204 if(ub_event_del(b->eb->slow_accept) != 0) {
205 log_err("could not event_del slow_accept");
207 ub_event_free(b->eb->slow_accept);
209 ub_event_base_free(b->eb->base);
216 comm_base_delete_no_base(struct comm_base* b)
220 if(b->eb->slow_accept_enabled) {
221 if(ub_event_del(b->eb->slow_accept) != 0) {
222 log_err("could not event_del slow_accept");
224 ub_event_free(b->eb->slow_accept);
232 comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv)
239 comm_base_dispatch(struct comm_base* b)
242 retval = ub_event_base_dispatch(b->eb->base);
244 fatal_exit("event_dispatch returned error %d, "
245 "errno is %s", retval, strerror(errno));
249 void comm_base_exit(struct comm_base* b)
251 if(ub_event_base_loopexit(b->eb->base) != 0) {
252 log_err("Could not loopexit");
256 void comm_base_set_slow_accept_handlers(struct comm_base* b,
257 void (*stop_acc)(void*), void (*start_acc)(void*), void* arg)
259 b->stop_accept = stop_acc;
260 b->start_accept = start_acc;
264 struct ub_event_base* comm_base_internal(struct comm_base* b)
269 /** see if errno for udp has to be logged or not uses globals */
271 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
273 /* do not log transient errors (unless high verbosity) */
274 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN)
288 if(verbosity < VERB_ALGO)
294 /* permission denied is gotten for every send if the
295 * network is disconnected (on some OS), squelch it */
296 if( ((errno == EPERM)
297 # ifdef EADDRNOTAVAIL
298 /* 'Cannot assign requested address' also when disconnected */
299 || (errno == EADDRNOTAVAIL)
301 ) && verbosity < VERB_DETAIL)
304 /* If SO_REUSEADDR is set, we could try to connect to the same server
305 * from the same source port twice. */
306 if(errno == EADDRINUSE && verbosity < VERB_DETAIL)
309 /* squelch errors where people deploy AAAA ::ffff:bla for
310 * authority servers, which we try for intranets. */
311 if(errno == EINVAL && addr_is_ip4mapped(
312 (struct sockaddr_storage*)addr, addrlen) &&
313 verbosity < VERB_DETAIL)
315 /* SO_BROADCAST sockopt can give access to 255.255.255.255,
316 * but a dns cache does not need it. */
317 if(errno == EACCES && addr_is_broadcast(
318 (struct sockaddr_storage*)addr, addrlen) &&
319 verbosity < VERB_DETAIL)
324 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
326 return udp_send_errno_needs_log(addr, addrlen);
329 /* send a UDP reply */
331 comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
332 struct sockaddr* addr, socklen_t addrlen)
335 log_assert(c->fd != -1);
337 if(sldns_buffer_remaining(packet) == 0)
338 log_err("error: send empty UDP packet");
340 log_assert(addr && addrlen > 0);
341 sent = sendto(c->fd, (void*)sldns_buffer_begin(packet),
342 sldns_buffer_remaining(packet), 0,
345 /* try again and block, waiting for IO to complete,
346 * we want to send the answer, and we will wait for
347 * the ethernet interface buffer to have space. */
349 if(errno == EAGAIN ||
351 errno == EWOULDBLOCK ||
355 if(WSAGetLastError() == WSAEINPROGRESS ||
356 WSAGetLastError() == WSAENOBUFS ||
357 WSAGetLastError() == WSAEWOULDBLOCK) {
361 sent = sendto(c->fd, (void*)sldns_buffer_begin(packet),
362 sldns_buffer_remaining(packet), 0,
365 fd_set_nonblock(c->fd);
370 if(!udp_send_errno_needs_log(addr, addrlen))
373 verbose(VERB_OPS, "sendto failed: %s", strerror(errno));
375 verbose(VERB_OPS, "sendto failed: %s",
376 wsa_strerror(WSAGetLastError()));
378 log_addr(VERB_OPS, "remote address is",
379 (struct sockaddr_storage*)addr, addrlen);
381 } else if((size_t)sent != sldns_buffer_remaining(packet)) {
382 log_err("sent %d in place of %d bytes",
383 (int)sent, (int)sldns_buffer_remaining(packet));
389 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && (defined(HAVE_RECVMSG) || defined(HAVE_SENDMSG))
390 /** print debug ancillary info */
391 static void p_ancil(const char* str, struct comm_reply* r)
393 if(r->srctype != 4 && r->srctype != 6) {
394 log_info("%s: unknown srctype %d", str, r->srctype);
397 if(r->srctype == 6) {
399 if(inet_ntop(AF_INET6, &r->pktinfo.v6info.ipi6_addr,
400 buf, (socklen_t)sizeof(buf)) == 0) {
401 (void)strlcpy(buf, "(inet_ntop error)", sizeof(buf));
403 buf[sizeof(buf)-1]=0;
404 log_info("%s: %s %d", str, buf, r->pktinfo.v6info.ipi6_ifindex);
405 } else if(r->srctype == 4) {
407 char buf1[1024], buf2[1024];
408 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_addr,
409 buf1, (socklen_t)sizeof(buf1)) == 0) {
410 (void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1));
412 buf1[sizeof(buf1)-1]=0;
413 #ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST
414 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_spec_dst,
415 buf2, (socklen_t)sizeof(buf2)) == 0) {
416 (void)strlcpy(buf2, "(inet_ntop error)", sizeof(buf2));
418 buf2[sizeof(buf2)-1]=0;
422 log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex,
424 #elif defined(IP_RECVDSTADDR)
426 if(inet_ntop(AF_INET, &r->pktinfo.v4addr,
427 buf1, (socklen_t)sizeof(buf1)) == 0) {
428 (void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1));
430 buf1[sizeof(buf1)-1]=0;
431 log_info("%s: %s", str, buf1);
432 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */
435 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */
437 /** send a UDP reply over specified interface*/
439 comm_point_send_udp_msg_if(struct comm_point *c, sldns_buffer* packet,
440 struct sockaddr* addr, socklen_t addrlen, struct comm_reply* r)
442 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG)
448 struct cmsghdr *cmsg;
449 #endif /* S_SPLINT_S */
451 log_assert(c->fd != -1);
453 if(sldns_buffer_remaining(packet) == 0)
454 log_err("error: send empty UDP packet");
456 log_assert(addr && addrlen > 0);
459 msg.msg_namelen = addrlen;
460 iov[0].iov_base = sldns_buffer_begin(packet);
461 iov[0].iov_len = sldns_buffer_remaining(packet);
464 msg.msg_control = control;
466 msg.msg_controllen = sizeof(control);
467 #endif /* S_SPLINT_S */
471 cmsg = CMSG_FIRSTHDR(&msg);
472 if(r->srctype == 4) {
475 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
476 log_assert(msg.msg_controllen <= sizeof(control));
477 cmsg->cmsg_level = IPPROTO_IP;
478 cmsg->cmsg_type = IP_PKTINFO;
479 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4info,
480 sizeof(struct in_pktinfo));
481 /* unset the ifindex to not bypass the routing tables */
482 cmsg_data = CMSG_DATA(cmsg);
483 ((struct in_pktinfo *) cmsg_data)->ipi_ifindex = 0;
484 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
485 #elif defined(IP_SENDSRCADDR)
486 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
487 log_assert(msg.msg_controllen <= sizeof(control));
488 cmsg->cmsg_level = IPPROTO_IP;
489 cmsg->cmsg_type = IP_SENDSRCADDR;
490 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4addr,
491 sizeof(struct in_addr));
492 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
494 verbose(VERB_ALGO, "no IP_PKTINFO or IP_SENDSRCADDR");
495 msg.msg_control = NULL;
496 #endif /* IP_PKTINFO or IP_SENDSRCADDR */
497 } else if(r->srctype == 6) {
499 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
500 log_assert(msg.msg_controllen <= sizeof(control));
501 cmsg->cmsg_level = IPPROTO_IPV6;
502 cmsg->cmsg_type = IPV6_PKTINFO;
503 memmove(CMSG_DATA(cmsg), &r->pktinfo.v6info,
504 sizeof(struct in6_pktinfo));
505 /* unset the ifindex to not bypass the routing tables */
506 cmsg_data = CMSG_DATA(cmsg);
507 ((struct in6_pktinfo *) cmsg_data)->ipi6_ifindex = 0;
508 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
510 /* try to pass all 0 to use default route */
511 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
512 log_assert(msg.msg_controllen <= sizeof(control));
513 cmsg->cmsg_level = IPPROTO_IPV6;
514 cmsg->cmsg_type = IPV6_PKTINFO;
515 memset(CMSG_DATA(cmsg), 0, sizeof(struct in6_pktinfo));
516 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
518 #endif /* S_SPLINT_S */
519 if(verbosity >= VERB_ALGO)
520 p_ancil("send_udp over interface", r);
521 sent = sendmsg(c->fd, &msg, 0);
523 /* try again and block, waiting for IO to complete,
524 * we want to send the answer, and we will wait for
525 * the ethernet interface buffer to have space. */
527 if(errno == EAGAIN ||
529 errno == EWOULDBLOCK ||
533 if(WSAGetLastError() == WSAEINPROGRESS ||
534 WSAGetLastError() == WSAENOBUFS ||
535 WSAGetLastError() == WSAEWOULDBLOCK) {
539 sent = sendmsg(c->fd, &msg, 0);
541 fd_set_nonblock(c->fd);
546 if(!udp_send_errno_needs_log(addr, addrlen))
548 verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno));
549 log_addr(VERB_OPS, "remote address is",
550 (struct sockaddr_storage*)addr, addrlen);
552 /* netbsd 7 has IP_PKTINFO for recv but not send */
553 if(errno == EINVAL && r->srctype == 4)
554 log_err("sendmsg: No support for sendmsg(IP_PKTINFO). "
555 "Please disable interface-automatic");
558 } else if((size_t)sent != sldns_buffer_remaining(packet)) {
559 log_err("sent %d in place of %d bytes",
560 (int)sent, (int)sldns_buffer_remaining(packet));
570 log_err("sendmsg: IPV6_PKTINFO not supported");
572 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */
576 comm_point_udp_ancil_callback(int fd, short event, void* arg)
578 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
579 struct comm_reply rep;
586 struct cmsghdr* cmsg;
587 #endif /* S_SPLINT_S */
589 rep.c = (struct comm_point*)arg;
590 log_assert(rep.c->type == comm_udp);
592 if(!(event&UB_EV_READ))
594 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
595 ub_comm_base_now(rep.c->ev->base);
596 for(i=0; i<NUM_UDP_PER_SELECT; i++) {
597 sldns_buffer_clear(rep.c->buffer);
598 rep.addrlen = (socklen_t)sizeof(rep.addr);
599 log_assert(fd != -1);
600 log_assert(sldns_buffer_remaining(rep.c->buffer) > 0);
601 msg.msg_name = &rep.addr;
602 msg.msg_namelen = (socklen_t)sizeof(rep.addr);
603 iov[0].iov_base = sldns_buffer_begin(rep.c->buffer);
604 iov[0].iov_len = sldns_buffer_remaining(rep.c->buffer);
607 msg.msg_control = ancil;
609 msg.msg_controllen = sizeof(ancil);
610 #endif /* S_SPLINT_S */
612 rcv = recvmsg(fd, &msg, 0);
614 if(errno != EAGAIN && errno != EINTR) {
615 log_err("recvmsg failed: %s", strerror(errno));
619 rep.addrlen = msg.msg_namelen;
620 sldns_buffer_skip(rep.c->buffer, rcv);
621 sldns_buffer_flip(rep.c->buffer);
624 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
625 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
626 if( cmsg->cmsg_level == IPPROTO_IPV6 &&
627 cmsg->cmsg_type == IPV6_PKTINFO) {
629 memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg),
630 sizeof(struct in6_pktinfo));
633 } else if( cmsg->cmsg_level == IPPROTO_IP &&
634 cmsg->cmsg_type == IP_PKTINFO) {
636 memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg),
637 sizeof(struct in_pktinfo));
639 #elif defined(IP_RECVDSTADDR)
640 } else if( cmsg->cmsg_level == IPPROTO_IP &&
641 cmsg->cmsg_type == IP_RECVDSTADDR) {
643 memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg),
644 sizeof(struct in_addr));
646 #endif /* IP_PKTINFO or IP_RECVDSTADDR */
649 if(verbosity >= VERB_ALGO)
650 p_ancil("receive_udp on interface", &rep);
651 #endif /* S_SPLINT_S */
652 fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
653 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
654 /* send back immediate reply */
655 (void)comm_point_send_udp_msg_if(rep.c, rep.c->buffer,
656 (struct sockaddr*)&rep.addr, rep.addrlen, &rep);
658 if(!rep.c || rep.c->fd == -1) /* commpoint closed */
665 fatal_exit("recvmsg: No support for IPV6_PKTINFO; IP_PKTINFO or IP_RECVDSTADDR. "
666 "Please disable interface-automatic");
667 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG */
671 comm_point_udp_callback(int fd, short event, void* arg)
673 struct comm_reply rep;
676 struct sldns_buffer *buffer;
678 rep.c = (struct comm_point*)arg;
679 log_assert(rep.c->type == comm_udp);
681 if(!(event&UB_EV_READ))
683 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
684 ub_comm_base_now(rep.c->ev->base);
685 for(i=0; i<NUM_UDP_PER_SELECT; i++) {
686 sldns_buffer_clear(rep.c->buffer);
687 rep.addrlen = (socklen_t)sizeof(rep.addr);
688 log_assert(fd != -1);
689 log_assert(sldns_buffer_remaining(rep.c->buffer) > 0);
690 rcv = recvfrom(fd, (void*)sldns_buffer_begin(rep.c->buffer),
691 sldns_buffer_remaining(rep.c->buffer), 0,
692 (struct sockaddr*)&rep.addr, &rep.addrlen);
695 if(errno != EAGAIN && errno != EINTR)
696 log_err("recvfrom %d failed: %s",
697 fd, strerror(errno));
699 if(WSAGetLastError() != WSAEINPROGRESS &&
700 WSAGetLastError() != WSAECONNRESET &&
701 WSAGetLastError()!= WSAEWOULDBLOCK)
702 log_err("recvfrom failed: %s",
703 wsa_strerror(WSAGetLastError()));
707 sldns_buffer_skip(rep.c->buffer, rcv);
708 sldns_buffer_flip(rep.c->buffer);
710 fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
711 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
712 /* send back immediate reply */
714 buffer = rep.c->dnscrypt_buffer;
716 buffer = rep.c->buffer;
718 (void)comm_point_send_udp_msg(rep.c, buffer,
719 (struct sockaddr*)&rep.addr, rep.addrlen);
721 if(!rep.c || rep.c->fd != fd) /* commpoint closed to -1 or reused for
722 another UDP port. Note rep.c cannot be reused with TCP fd. */
727 /** Use a new tcp handler for new query fd, set to read query */
729 setup_tcp_handler(struct comm_point* c, int fd, int cur, int max)
731 log_assert(c->type == comm_tcp);
732 log_assert(c->fd == -1);
733 sldns_buffer_clear(c->buffer);
736 sldns_buffer_clear(c->dnscrypt_buffer);
738 c->tcp_is_reading = 1;
739 c->tcp_byte_count = 0;
740 c->tcp_timeout_msec = TCP_QUERY_TIMEOUT;
741 /* if more than half the tcp handlers are in use, use a shorter
742 * timeout for this TCP connection, we need to make space for
743 * other connections to be able to get attention */
745 c->tcp_timeout_msec = TCP_QUERY_TIMEOUT_FAST;
746 comm_point_start_listening(c, fd, c->tcp_timeout_msec);
749 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
750 short ATTR_UNUSED(event), void* arg)
752 struct comm_base* b = (struct comm_base*)arg;
753 /* timeout for the slow accept, re-enable accepts again */
754 if(b->start_accept) {
755 verbose(VERB_ALGO, "wait is over, slow accept disabled");
756 fptr_ok(fptr_whitelist_start_accept(b->start_accept));
757 (*b->start_accept)(b->cb_arg);
758 b->eb->slow_accept_enabled = 0;
762 int comm_point_perform_accept(struct comm_point* c,
763 struct sockaddr_storage* addr, socklen_t* addrlen)
766 *addrlen = (socklen_t)sizeof(*addr);
767 new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen);
770 /* EINTR is signal interrupt. others are closed connection. */
771 if( errno == EINTR || errno == EAGAIN
773 || errno == EWOULDBLOCK
776 || errno == ECONNABORTED
783 #if defined(ENFILE) && defined(EMFILE)
784 if(errno == ENFILE || errno == EMFILE) {
785 /* out of file descriptors, likely outside of our
786 * control. stop accept() calls for some time */
787 if(c->ev->base->stop_accept) {
788 struct comm_base* b = c->ev->base;
790 verbose(VERB_ALGO, "out of file descriptors: "
792 b->eb->slow_accept_enabled = 1;
793 fptr_ok(fptr_whitelist_stop_accept(
795 (*b->stop_accept)(b->cb_arg);
796 /* set timeout, no mallocs */
797 tv.tv_sec = NETEVENT_SLOW_ACCEPT_TIME/1000;
798 tv.tv_usec = (NETEVENT_SLOW_ACCEPT_TIME%1000)*1000;
799 b->eb->slow_accept = ub_event_new(b->eb->base,
801 comm_base_handle_slow_accept, b);
802 if(b->eb->slow_accept == NULL) {
803 /* we do not want to log here, because
804 * that would spam the logfiles.
805 * error: "event_base_set failed." */
807 else if(ub_event_add(b->eb->slow_accept, &tv)
809 /* we do not want to log here,
810 * error: "event_add failed." */
816 log_err_addr("accept failed", strerror(errno), addr, *addrlen);
817 #else /* USE_WINSOCK */
818 if(WSAGetLastError() == WSAEINPROGRESS ||
819 WSAGetLastError() == WSAECONNRESET)
821 if(WSAGetLastError() == WSAEWOULDBLOCK) {
822 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
825 log_err_addr("accept failed", wsa_strerror(WSAGetLastError()),
830 fd_set_nonblock(new_fd);
835 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp),
836 int ATTR_UNUSED(argi), long argl, long retvalue)
838 verbose(VERB_ALGO, "bio_cb %d, %s %s %s", oper,
839 (oper&BIO_CB_RETURN)?"return":"before",
840 (oper&BIO_CB_READ)?"read":((oper&BIO_CB_WRITE)?"write":"other"),
841 WSAGetLastError()==WSAEWOULDBLOCK?"wsawb":"");
842 /* on windows, check if previous operation caused EWOULDBLOCK */
843 if( (oper == (BIO_CB_READ|BIO_CB_RETURN) && argl == 0) ||
844 (oper == (BIO_CB_GETS|BIO_CB_RETURN) && argl == 0)) {
845 if(WSAGetLastError() == WSAEWOULDBLOCK)
846 ub_winsock_tcp_wouldblock((struct ub_event*)
847 BIO_get_callback_arg(b), UB_EV_READ);
849 if( (oper == (BIO_CB_WRITE|BIO_CB_RETURN) && argl == 0) ||
850 (oper == (BIO_CB_PUTS|BIO_CB_RETURN) && argl == 0)) {
851 if(WSAGetLastError() == WSAEWOULDBLOCK)
852 ub_winsock_tcp_wouldblock((struct ub_event*)
853 BIO_get_callback_arg(b), UB_EV_WRITE);
855 /* return original return value */
859 /** set win bio callbacks for nonblocking operations */
861 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl)
863 SSL* ssl = (SSL*)thessl;
864 /* set them both just in case, but usually they are the same BIO */
865 BIO_set_callback(SSL_get_rbio(ssl), &win_bio_cb);
866 BIO_set_callback_arg(SSL_get_rbio(ssl), (char*)c->ev->ev);
867 BIO_set_callback(SSL_get_wbio(ssl), &win_bio_cb);
868 BIO_set_callback_arg(SSL_get_wbio(ssl), (char*)c->ev->ev);
873 comm_point_tcp_accept_callback(int fd, short event, void* arg)
875 struct comm_point* c = (struct comm_point*)arg, *c_hdl;
877 log_assert(c->type == comm_tcp_accept);
878 if(!(event & UB_EV_READ)) {
879 log_info("ignoring tcp accept event %d", (int)event);
882 ub_comm_base_now(c->ev->base);
883 /* find free tcp handler. */
885 log_warn("accepted too many tcp, connections full");
888 /* accept incoming connection. */
890 log_assert(fd != -1);
892 new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.addr,
893 &c_hdl->repinfo.addrlen);
897 c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd);
900 comm_point_close(c_hdl);
903 c_hdl->ssl_shake_state = comm_ssl_shake_read;
905 comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl);
909 /* grab the tcp handler buffers */
911 c->tcp_free = c_hdl->tcp_free;
913 /* stop accepting incoming queries for now. */
914 comm_point_stop_listening(c);
916 setup_tcp_handler(c_hdl, new_fd, c->cur_tcp_count, c->max_tcp_count);
919 /** Make tcp handler free for next assignment */
921 reclaim_tcp_handler(struct comm_point* c)
923 log_assert(c->type == comm_tcp);
926 SSL_shutdown(c->ssl);
933 c->tcp_parent->cur_tcp_count--;
934 c->tcp_free = c->tcp_parent->tcp_free;
935 c->tcp_parent->tcp_free = c;
937 /* re-enable listening on accept socket */
938 comm_point_start_listening(c->tcp_parent, -1, -1);
943 /** do the callback when writing is done */
945 tcp_callback_writer(struct comm_point* c)
947 log_assert(c->type == comm_tcp);
948 sldns_buffer_clear(c->buffer);
949 if(c->tcp_do_toggle_rw)
950 c->tcp_is_reading = 1;
951 c->tcp_byte_count = 0;
952 /* switch from listening(write) to listening(read) */
953 comm_point_stop_listening(c);
954 comm_point_start_listening(c, -1, -1);
957 /** do the callback when reading is done */
959 tcp_callback_reader(struct comm_point* c)
961 log_assert(c->type == comm_tcp || c->type == comm_local);
962 sldns_buffer_flip(c->buffer);
963 if(c->tcp_do_toggle_rw)
964 c->tcp_is_reading = 0;
965 c->tcp_byte_count = 0;
966 if(c->type == comm_tcp)
967 comm_point_stop_listening(c);
968 fptr_ok(fptr_whitelist_comm_point(c->callback));
969 if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) {
970 comm_point_start_listening(c, -1, c->tcp_timeout_msec);
975 /** log certificate details */
977 log_cert(unsigned level, const char* str, X509* cert)
983 if(verbosity < level) return;
984 bio = BIO_new(BIO_s_mem());
986 X509_print_ex(bio, cert, 0, (unsigned long)-1
987 ^(X509_FLAG_NO_SUBJECT
988 |X509_FLAG_NO_ISSUER|X509_FLAG_NO_VALIDITY
989 |X509_FLAG_NO_EXTENSIONS|X509_FLAG_NO_AUX
990 |X509_FLAG_NO_ATTRIBUTES));
991 BIO_write(bio, &nul, (int)sizeof(nul));
992 len = BIO_get_mem_data(bio, &pp);
994 verbose(level, "%s: \n%s", str, pp);
998 #endif /* HAVE_SSL */
1000 /** continue ssl handshake */
1003 ssl_handshake(struct comm_point* c)
1006 if(c->ssl_shake_state == comm_ssl_shake_hs_read) {
1007 /* read condition satisfied back to writing */
1008 comm_point_listen_for_rw(c, 1, 1);
1009 c->ssl_shake_state = comm_ssl_shake_none;
1012 if(c->ssl_shake_state == comm_ssl_shake_hs_write) {
1013 /* write condition satisfied, back to reading */
1014 comm_point_listen_for_rw(c, 1, 0);
1015 c->ssl_shake_state = comm_ssl_shake_none;
1020 r = SSL_do_handshake(c->ssl);
1022 int want = SSL_get_error(c->ssl, r);
1023 if(want == SSL_ERROR_WANT_READ) {
1024 if(c->ssl_shake_state == comm_ssl_shake_read)
1026 c->ssl_shake_state = comm_ssl_shake_read;
1027 comm_point_listen_for_rw(c, 1, 0);
1029 } else if(want == SSL_ERROR_WANT_WRITE) {
1030 if(c->ssl_shake_state == comm_ssl_shake_write)
1032 c->ssl_shake_state = comm_ssl_shake_write;
1033 comm_point_listen_for_rw(c, 0, 1);
1036 return 0; /* closed */
1037 } else if(want == SSL_ERROR_SYSCALL) {
1038 /* SYSCALL and errno==0 means closed uncleanly */
1040 log_err("SSL_handshake syscall: %s",
1044 log_crypto_err("ssl handshake failed");
1045 log_addr(1, "ssl handshake failed", &c->repinfo.addr,
1046 c->repinfo.addrlen);
1050 /* this is where peer verification could take place */
1051 if((SSL_get_verify_mode(c->ssl)&SSL_VERIFY_PEER)) {
1053 if(SSL_get_verify_result(c->ssl) == X509_V_OK) {
1054 X509* x = SSL_get_peer_certificate(c->ssl);
1056 log_addr(VERB_ALGO, "SSL connection failed: "
1058 &c->repinfo.addr, c->repinfo.addrlen);
1061 log_cert(VERB_ALGO, "peer certificate", x);
1062 #ifdef HAVE_SSL_GET0_PEERNAME
1063 if(SSL_get0_peername(c->ssl)) {
1065 snprintf(buf, sizeof(buf), "SSL connection "
1066 "to %s authenticated",
1067 SSL_get0_peername(c->ssl));
1068 log_addr(VERB_ALGO, buf, &c->repinfo.addr,
1069 c->repinfo.addrlen);
1072 log_addr(VERB_ALGO, "SSL connection "
1073 "authenticated", &c->repinfo.addr,
1074 c->repinfo.addrlen);
1075 #ifdef HAVE_SSL_GET0_PEERNAME
1080 X509* x = SSL_get_peer_certificate(c->ssl);
1082 log_cert(VERB_ALGO, "peer certificate", x);
1085 log_addr(VERB_ALGO, "SSL connection failed: "
1086 "failed to authenticate",
1087 &c->repinfo.addr, c->repinfo.addrlen);
1091 /* unauthenticated, the verify peer flag was not set
1092 * in c->ssl when the ssl object was created from ssl_ctx */
1093 log_addr(VERB_ALGO, "SSL connection", &c->repinfo.addr,
1094 c->repinfo.addrlen);
1097 /* setup listen rw correctly */
1098 if(c->tcp_is_reading) {
1099 if(c->ssl_shake_state != comm_ssl_shake_read)
1100 comm_point_listen_for_rw(c, 1, 0);
1102 comm_point_listen_for_rw(c, 1, 1);
1104 c->ssl_shake_state = comm_ssl_shake_none;
1107 #endif /* HAVE_SSL */
1109 /** ssl read callback on TCP */
1111 ssl_handle_read(struct comm_point* c)
1115 if(c->ssl_shake_state != comm_ssl_shake_none) {
1116 if(!ssl_handshake(c))
1118 if(c->ssl_shake_state != comm_ssl_shake_none)
1121 if(c->tcp_byte_count < sizeof(uint16_t)) {
1122 /* read length bytes */
1124 if((r=SSL_read(c->ssl, (void*)sldns_buffer_at(c->buffer,
1125 c->tcp_byte_count), (int)(sizeof(uint16_t) -
1126 c->tcp_byte_count))) <= 0) {
1127 int want = SSL_get_error(c->ssl, r);
1128 if(want == SSL_ERROR_ZERO_RETURN) {
1129 return 0; /* shutdown, closed */
1130 } else if(want == SSL_ERROR_WANT_READ) {
1131 return 1; /* read more later */
1132 } else if(want == SSL_ERROR_WANT_WRITE) {
1133 c->ssl_shake_state = comm_ssl_shake_hs_write;
1134 comm_point_listen_for_rw(c, 0, 1);
1136 } else if(want == SSL_ERROR_SYSCALL) {
1138 log_err("SSL_read syscall: %s",
1142 log_crypto_err("could not SSL_read");
1145 c->tcp_byte_count += r;
1146 if(c->tcp_byte_count != sizeof(uint16_t))
1148 if(sldns_buffer_read_u16_at(c->buffer, 0) >
1149 sldns_buffer_capacity(c->buffer)) {
1150 verbose(VERB_QUERY, "ssl: dropped larger than buffer");
1153 sldns_buffer_set_limit(c->buffer,
1154 sldns_buffer_read_u16_at(c->buffer, 0));
1155 if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1156 verbose(VERB_QUERY, "ssl: dropped bogus too short.");
1159 verbose(VERB_ALGO, "Reading ssl tcp query of length %d",
1160 (int)sldns_buffer_limit(c->buffer));
1162 log_assert(sldns_buffer_remaining(c->buffer) > 0);
1164 r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
1165 (int)sldns_buffer_remaining(c->buffer));
1167 int want = SSL_get_error(c->ssl, r);
1168 if(want == SSL_ERROR_ZERO_RETURN) {
1169 return 0; /* shutdown, closed */
1170 } else if(want == SSL_ERROR_WANT_READ) {
1171 return 1; /* read more later */
1172 } else if(want == SSL_ERROR_WANT_WRITE) {
1173 c->ssl_shake_state = comm_ssl_shake_hs_write;
1174 comm_point_listen_for_rw(c, 0, 1);
1176 } else if(want == SSL_ERROR_SYSCALL) {
1178 log_err("SSL_read syscall: %s",
1182 log_crypto_err("could not SSL_read");
1185 sldns_buffer_skip(c->buffer, (ssize_t)r);
1186 if(sldns_buffer_remaining(c->buffer) <= 0) {
1187 tcp_callback_reader(c);
1193 #endif /* HAVE_SSL */
1196 /** ssl write callback on TCP */
1198 ssl_handle_write(struct comm_point* c)
1202 if(c->ssl_shake_state != comm_ssl_shake_none) {
1203 if(!ssl_handshake(c))
1205 if(c->ssl_shake_state != comm_ssl_shake_none)
1208 /* ignore return, if fails we may simply block */
1209 (void)SSL_set_mode(c->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
1210 if(c->tcp_byte_count < sizeof(uint16_t)) {
1211 uint16_t len = htons(sldns_buffer_limit(c->buffer));
1213 if(sizeof(uint16_t)+sldns_buffer_remaining(c->buffer) <
1215 /* combine the tcp length and the query for write,
1216 * this emulates writev */
1217 uint8_t buf[LDNS_RR_BUF_SIZE];
1218 memmove(buf, &len, sizeof(uint16_t));
1219 memmove(buf+sizeof(uint16_t),
1220 sldns_buffer_current(c->buffer),
1221 sldns_buffer_remaining(c->buffer));
1222 r = SSL_write(c->ssl, (void*)(buf+c->tcp_byte_count),
1223 (int)(sizeof(uint16_t)+
1224 sldns_buffer_remaining(c->buffer)
1225 - c->tcp_byte_count));
1227 r = SSL_write(c->ssl,
1228 (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1229 (int)(sizeof(uint16_t)-c->tcp_byte_count));
1232 int want = SSL_get_error(c->ssl, r);
1233 if(want == SSL_ERROR_ZERO_RETURN) {
1234 return 0; /* closed */
1235 } else if(want == SSL_ERROR_WANT_READ) {
1236 c->ssl_shake_state = comm_ssl_shake_read;
1237 comm_point_listen_for_rw(c, 1, 0);
1238 return 1; /* wait for read condition */
1239 } else if(want == SSL_ERROR_WANT_WRITE) {
1240 return 1; /* write more later */
1241 } else if(want == SSL_ERROR_SYSCALL) {
1243 log_err("SSL_write syscall: %s",
1247 log_crypto_err("could not SSL_write");
1250 c->tcp_byte_count += r;
1251 if(c->tcp_byte_count < sizeof(uint16_t))
1253 sldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1255 if(sldns_buffer_remaining(c->buffer) == 0) {
1256 tcp_callback_writer(c);
1260 log_assert(sldns_buffer_remaining(c->buffer) > 0);
1262 r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
1263 (int)sldns_buffer_remaining(c->buffer));
1265 int want = SSL_get_error(c->ssl, r);
1266 if(want == SSL_ERROR_ZERO_RETURN) {
1267 return 0; /* closed */
1268 } else if(want == SSL_ERROR_WANT_READ) {
1269 c->ssl_shake_state = comm_ssl_shake_read;
1270 comm_point_listen_for_rw(c, 1, 0);
1271 return 1; /* wait for read condition */
1272 } else if(want == SSL_ERROR_WANT_WRITE) {
1273 return 1; /* write more later */
1274 } else if(want == SSL_ERROR_SYSCALL) {
1276 log_err("SSL_write syscall: %s",
1280 log_crypto_err("could not SSL_write");
1283 sldns_buffer_skip(c->buffer, (ssize_t)r);
1285 if(sldns_buffer_remaining(c->buffer) == 0) {
1286 tcp_callback_writer(c);
1292 #endif /* HAVE_SSL */
1295 /** handle ssl tcp connection with dns contents */
1297 ssl_handle_it(struct comm_point* c)
1299 if(c->tcp_is_reading)
1300 return ssl_handle_read(c);
1301 return ssl_handle_write(c);
1304 /** Handle tcp reading callback.
1305 * @param fd: file descriptor of socket.
1306 * @param c: comm point to read from into buffer.
1307 * @param short_ok: if true, very short packets are OK (for comm_local).
1308 * @return: 0 on error
1311 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok)
1314 log_assert(c->type == comm_tcp || c->type == comm_local);
1316 return ssl_handle_it(c);
1317 if(!c->tcp_is_reading)
1320 log_assert(fd != -1);
1321 if(c->tcp_byte_count < sizeof(uint16_t)) {
1322 /* read length bytes */
1323 r = recv(fd,(void*)sldns_buffer_at(c->buffer,c->tcp_byte_count),
1324 sizeof(uint16_t)-c->tcp_byte_count, 0);
1329 if(errno == EINTR || errno == EAGAIN)
1332 if(errno == ECONNRESET && verbosity < 2)
1333 return 0; /* silence reset by peer */
1335 log_err_addr("read (in tcp s)", strerror(errno),
1336 &c->repinfo.addr, c->repinfo.addrlen);
1337 #else /* USE_WINSOCK */
1338 if(WSAGetLastError() == WSAECONNRESET)
1340 if(WSAGetLastError() == WSAEINPROGRESS)
1342 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1343 ub_winsock_tcp_wouldblock(c->ev->ev,
1347 log_err_addr("read (in tcp s)",
1348 wsa_strerror(WSAGetLastError()),
1349 &c->repinfo.addr, c->repinfo.addrlen);
1353 c->tcp_byte_count += r;
1354 if(c->tcp_byte_count != sizeof(uint16_t))
1356 if(sldns_buffer_read_u16_at(c->buffer, 0) >
1357 sldns_buffer_capacity(c->buffer)) {
1358 verbose(VERB_QUERY, "tcp: dropped larger than buffer");
1361 sldns_buffer_set_limit(c->buffer,
1362 sldns_buffer_read_u16_at(c->buffer, 0));
1364 sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1365 verbose(VERB_QUERY, "tcp: dropped bogus too short.");
1368 verbose(VERB_ALGO, "Reading tcp query of length %d",
1369 (int)sldns_buffer_limit(c->buffer));
1372 log_assert(sldns_buffer_remaining(c->buffer) > 0);
1373 r = recv(fd, (void*)sldns_buffer_current(c->buffer),
1374 sldns_buffer_remaining(c->buffer), 0);
1377 } else if(r == -1) {
1379 if(errno == EINTR || errno == EAGAIN)
1381 log_err_addr("read (in tcp r)", strerror(errno),
1382 &c->repinfo.addr, c->repinfo.addrlen);
1383 #else /* USE_WINSOCK */
1384 if(WSAGetLastError() == WSAECONNRESET)
1386 if(WSAGetLastError() == WSAEINPROGRESS)
1388 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1389 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1392 log_err_addr("read (in tcp r)",
1393 wsa_strerror(WSAGetLastError()),
1394 &c->repinfo.addr, c->repinfo.addrlen);
1398 sldns_buffer_skip(c->buffer, r);
1399 if(sldns_buffer_remaining(c->buffer) <= 0) {
1400 tcp_callback_reader(c);
1406 * Handle tcp writing callback.
1407 * @param fd: file descriptor of socket.
1408 * @param c: comm point to write buffer out of.
1409 * @return: 0 on error
1412 comm_point_tcp_handle_write(int fd, struct comm_point* c)
1415 struct sldns_buffer *buffer;
1416 log_assert(c->type == comm_tcp);
1418 buffer = c->dnscrypt_buffer;
1422 if(c->tcp_is_reading && !c->ssl)
1424 log_assert(fd != -1);
1425 if(c->tcp_byte_count == 0 && c->tcp_check_nb_connect) {
1426 /* check for pending error from nonblocking connect */
1427 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/
1429 socklen_t len = (socklen_t)sizeof(error);
1430 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
1433 error = errno; /* on solaris errno is error */
1434 #else /* USE_WINSOCK */
1435 error = WSAGetLastError();
1439 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1440 if(error == EINPROGRESS || error == EWOULDBLOCK)
1441 return 1; /* try again later */
1444 if(error != 0 && verbosity < 2)
1445 return 0; /* silence lots of chatter in the logs */
1446 else if(error != 0) {
1447 log_err_addr("tcp connect", strerror(error),
1448 &c->repinfo.addr, c->repinfo.addrlen);
1449 #else /* USE_WINSOCK */
1451 if(error == WSAEINPROGRESS)
1453 else if(error == WSAEWOULDBLOCK) {
1454 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1456 } else if(error != 0 && verbosity < 2)
1458 else if(error != 0) {
1459 log_err_addr("tcp connect", wsa_strerror(error),
1460 &c->repinfo.addr, c->repinfo.addrlen);
1461 #endif /* USE_WINSOCK */
1466 return ssl_handle_it(c);
1468 #ifdef USE_MSG_FASTOPEN
1469 /* Only try this on first use of a connection that uses tfo,
1470 otherwise fall through to normal write */
1471 /* Also, TFO support on WINDOWS not implemented at the moment */
1472 if(c->tcp_do_fastopen == 1) {
1473 /* this form of sendmsg() does both a connect() and send() so need to
1474 look for various flavours of error*/
1475 uint16_t len = htons(sldns_buffer_limit(buffer));
1477 struct iovec iov[2];
1478 c->tcp_do_fastopen = 0;
1479 memset(&msg, 0, sizeof(msg));
1480 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
1481 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
1482 iov[1].iov_base = sldns_buffer_begin(buffer);
1483 iov[1].iov_len = sldns_buffer_limit(buffer);
1484 log_assert(iov[0].iov_len > 0);
1485 log_assert(iov[1].iov_len > 0);
1486 msg.msg_name = &c->repinfo.addr;
1487 msg.msg_namelen = c->repinfo.addrlen;
1490 r = sendmsg(fd, &msg, MSG_FASTOPEN);
1492 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1493 /* Handshake is underway, maybe because no TFO cookie available.
1494 Come back to write the message*/
1495 if(errno == EINPROGRESS || errno == EWOULDBLOCK)
1498 if(errno == EINTR || errno == EAGAIN)
1500 /* Not handling EISCONN here as shouldn't ever hit that case.*/
1501 if(errno != EPIPE && errno != 0 && verbosity < 2)
1502 return 0; /* silence lots of chatter in the logs */
1503 if(errno != EPIPE && errno != 0) {
1504 log_err_addr("tcp sendmsg", strerror(errno),
1505 &c->repinfo.addr, c->repinfo.addrlen);
1508 /* fallthrough to nonFASTOPEN
1509 * (MSG_FASTOPEN on Linux 3 produces EPIPE)
1510 * we need to perform connect() */
1511 if(connect(fd, (struct sockaddr *)&c->repinfo.addr, c->repinfo.addrlen) == -1) {
1513 if(errno == EINPROGRESS)
1514 return 1; /* wait until connect done*/
1517 if(WSAGetLastError() == WSAEINPROGRESS ||
1518 WSAGetLastError() == WSAEWOULDBLOCK)
1519 return 1; /* wait until connect done*/
1521 if(tcp_connect_errno_needs_log(
1522 (struct sockaddr *)&c->repinfo.addr, c->repinfo.addrlen)) {
1523 log_err_addr("outgoing tcp: connect after EPIPE for fastopen",
1524 strerror(errno), &c->repinfo.addr, c->repinfo.addrlen);
1530 c->tcp_byte_count += r;
1531 if(c->tcp_byte_count < sizeof(uint16_t))
1533 sldns_buffer_set_position(buffer, c->tcp_byte_count -
1535 if(sldns_buffer_remaining(buffer) == 0) {
1536 tcp_callback_writer(c);
1541 #endif /* USE_MSG_FASTOPEN */
1543 if(c->tcp_byte_count < sizeof(uint16_t)) {
1544 uint16_t len = htons(sldns_buffer_limit(buffer));
1546 struct iovec iov[2];
1547 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
1548 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
1549 iov[1].iov_base = sldns_buffer_begin(buffer);
1550 iov[1].iov_len = sldns_buffer_limit(buffer);
1551 log_assert(iov[0].iov_len > 0);
1552 log_assert(iov[1].iov_len > 0);
1553 r = writev(fd, iov, 2);
1554 #else /* HAVE_WRITEV */
1555 r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1556 sizeof(uint16_t)-c->tcp_byte_count, 0);
1557 #endif /* HAVE_WRITEV */
1561 if(errno == EPIPE && verbosity < 2)
1562 return 0; /* silence 'broken pipe' */
1564 if(errno == EINTR || errno == EAGAIN)
1567 log_err_addr("tcp writev", strerror(errno),
1568 &c->repinfo.addr, c->repinfo.addrlen);
1569 # else /* HAVE_WRITEV */
1570 log_err_addr("tcp send s", strerror(errno),
1571 &c->repinfo.addr, c->repinfo.addrlen);
1572 # endif /* HAVE_WRITEV */
1574 if(WSAGetLastError() == WSAENOTCONN)
1576 if(WSAGetLastError() == WSAEINPROGRESS)
1578 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1579 ub_winsock_tcp_wouldblock(c->ev->ev,
1583 log_err_addr("tcp send s",
1584 wsa_strerror(WSAGetLastError()),
1585 &c->repinfo.addr, c->repinfo.addrlen);
1589 c->tcp_byte_count += r;
1590 if(c->tcp_byte_count < sizeof(uint16_t))
1592 sldns_buffer_set_position(buffer, c->tcp_byte_count -
1594 if(sldns_buffer_remaining(buffer) == 0) {
1595 tcp_callback_writer(c);
1599 log_assert(sldns_buffer_remaining(buffer) > 0);
1600 r = send(fd, (void*)sldns_buffer_current(buffer),
1601 sldns_buffer_remaining(buffer), 0);
1604 if(errno == EINTR || errno == EAGAIN)
1606 log_err_addr("tcp send r", strerror(errno),
1607 &c->repinfo.addr, c->repinfo.addrlen);
1609 if(WSAGetLastError() == WSAEINPROGRESS)
1611 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1612 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1615 log_err_addr("tcp send r", wsa_strerror(WSAGetLastError()),
1616 &c->repinfo.addr, c->repinfo.addrlen);
1620 sldns_buffer_skip(buffer, r);
1622 if(sldns_buffer_remaining(buffer) == 0) {
1623 tcp_callback_writer(c);
1630 comm_point_tcp_handle_callback(int fd, short event, void* arg)
1632 struct comm_point* c = (struct comm_point*)arg;
1633 log_assert(c->type == comm_tcp);
1634 ub_comm_base_now(c->ev->base);
1637 /* Initialize if this is a dnscrypt socket */
1639 c->dnscrypt = c->tcp_parent->dnscrypt;
1641 if(c->dnscrypt && c->dnscrypt_buffer == c->buffer) {
1642 c->dnscrypt_buffer = sldns_buffer_new(sldns_buffer_capacity(c->buffer));
1643 if(!c->dnscrypt_buffer) {
1644 log_err("Could not allocate dnscrypt buffer");
1645 reclaim_tcp_handler(c);
1646 if(!c->tcp_do_close) {
1647 fptr_ok(fptr_whitelist_comm_point(
1649 (void)(*c->callback)(c, c->cb_arg,
1650 NETEVENT_CLOSED, NULL);
1657 if(event&UB_EV_READ) {
1658 if(!comm_point_tcp_handle_read(fd, c, 0)) {
1659 reclaim_tcp_handler(c);
1660 if(!c->tcp_do_close) {
1661 fptr_ok(fptr_whitelist_comm_point(
1663 (void)(*c->callback)(c, c->cb_arg,
1664 NETEVENT_CLOSED, NULL);
1669 if(event&UB_EV_WRITE) {
1670 if(!comm_point_tcp_handle_write(fd, c)) {
1671 reclaim_tcp_handler(c);
1672 if(!c->tcp_do_close) {
1673 fptr_ok(fptr_whitelist_comm_point(
1675 (void)(*c->callback)(c, c->cb_arg,
1676 NETEVENT_CLOSED, NULL);
1681 if(event&UB_EV_TIMEOUT) {
1682 verbose(VERB_QUERY, "tcp took too long, dropped");
1683 reclaim_tcp_handler(c);
1684 if(!c->tcp_do_close) {
1685 fptr_ok(fptr_whitelist_comm_point(c->callback));
1686 (void)(*c->callback)(c, c->cb_arg,
1687 NETEVENT_TIMEOUT, NULL);
1691 log_err("Ignored event %d for tcphdl.", event);
1694 /** Make http handler free for next assignment */
1696 reclaim_http_handler(struct comm_point* c)
1698 log_assert(c->type == comm_http);
1701 SSL_shutdown(c->ssl);
1706 comm_point_close(c);
1708 c->tcp_parent->cur_tcp_count--;
1709 c->tcp_free = c->tcp_parent->tcp_free;
1710 c->tcp_parent->tcp_free = c;
1712 /* re-enable listening on accept socket */
1713 comm_point_start_listening(c->tcp_parent, -1, -1);
1718 /** read more data for http (with ssl) */
1720 ssl_http_read_more(struct comm_point* c)
1724 log_assert(sldns_buffer_remaining(c->buffer) > 0);
1726 r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
1727 (int)sldns_buffer_remaining(c->buffer));
1729 int want = SSL_get_error(c->ssl, r);
1730 if(want == SSL_ERROR_ZERO_RETURN) {
1731 return 0; /* shutdown, closed */
1732 } else if(want == SSL_ERROR_WANT_READ) {
1733 return 1; /* read more later */
1734 } else if(want == SSL_ERROR_WANT_WRITE) {
1735 c->ssl_shake_state = comm_ssl_shake_hs_write;
1736 comm_point_listen_for_rw(c, 0, 1);
1738 } else if(want == SSL_ERROR_SYSCALL) {
1740 log_err("SSL_read syscall: %s",
1744 log_crypto_err("could not SSL_read");
1747 sldns_buffer_skip(c->buffer, (ssize_t)r);
1752 #endif /* HAVE_SSL */
1755 /** read more data for http */
1757 http_read_more(int fd, struct comm_point* c)
1760 log_assert(sldns_buffer_remaining(c->buffer) > 0);
1761 r = recv(fd, (void*)sldns_buffer_current(c->buffer),
1762 sldns_buffer_remaining(c->buffer), 0);
1765 } else if(r == -1) {
1767 if(errno == EINTR || errno == EAGAIN)
1769 log_err_addr("read (in http r)", strerror(errno),
1770 &c->repinfo.addr, c->repinfo.addrlen);
1771 #else /* USE_WINSOCK */
1772 if(WSAGetLastError() == WSAECONNRESET)
1774 if(WSAGetLastError() == WSAEINPROGRESS)
1776 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1777 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1780 log_err_addr("read (in http r)",
1781 wsa_strerror(WSAGetLastError()),
1782 &c->repinfo.addr, c->repinfo.addrlen);
1786 sldns_buffer_skip(c->buffer, r);
1790 /** return true if http header has been read (one line complete) */
1792 http_header_done(sldns_buffer* buf)
1795 for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) {
1796 /* there was a \r before the \n, but we ignore that */
1797 if((char)sldns_buffer_read_u8_at(buf, i) == '\n')
1803 /** return character string into buffer for header line, moves buffer
1804 * past that line and puts zero terminator into linefeed-newline */
1806 http_header_line(sldns_buffer* buf)
1808 char* result = (char*)sldns_buffer_current(buf);
1810 for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) {
1811 /* terminate the string on the \r */
1812 if((char)sldns_buffer_read_u8_at(buf, i) == '\r')
1813 sldns_buffer_write_u8_at(buf, i, 0);
1814 /* terminate on the \n and skip past the it and done */
1815 if((char)sldns_buffer_read_u8_at(buf, i) == '\n') {
1816 sldns_buffer_write_u8_at(buf, i, 0);
1817 sldns_buffer_set_position(buf, i+1);
1824 /** move unread buffer to start and clear rest for putting the rest into it */
1826 http_moveover_buffer(sldns_buffer* buf)
1828 size_t pos = sldns_buffer_position(buf);
1829 size_t len = sldns_buffer_remaining(buf);
1830 sldns_buffer_clear(buf);
1831 memmove(sldns_buffer_begin(buf), sldns_buffer_at(buf, pos), len);
1832 sldns_buffer_set_position(buf, len);
1835 /** a http header is complete, process it */
1837 http_process_initial_header(struct comm_point* c)
1839 char* line = http_header_line(c->buffer);
1841 verbose(VERB_ALGO, "http header: %s", line);
1842 if(strncasecmp(line, "HTTP/1.1 ", 9) == 0) {
1843 /* check returncode */
1844 if(line[9] != '2') {
1845 verbose(VERB_ALGO, "http bad status %s", line+9);
1848 } else if(strncasecmp(line, "Content-Length: ", 16) == 0) {
1849 if(!c->http_is_chunked)
1850 c->tcp_byte_count = (size_t)atoi(line+16);
1851 } else if(strncasecmp(line, "Transfer-Encoding: chunked", 19+7) == 0) {
1852 c->tcp_byte_count = 0;
1853 c->http_is_chunked = 1;
1854 } else if(line[0] == 0) {
1855 /* end of initial headers */
1856 c->http_in_headers = 0;
1857 if(c->http_is_chunked)
1858 c->http_in_chunk_headers = 1;
1859 /* remove header text from front of buffer
1860 * the buffer is going to be used to return the data segment
1861 * itself and we don't want the header to get returned
1862 * prepended with it */
1863 http_moveover_buffer(c->buffer);
1864 sldns_buffer_flip(c->buffer);
1867 /* ignore other headers */
1871 /** a chunk header is complete, process it, return 0=fail, 1=continue next
1872 * header line, 2=done with chunked transfer*/
1874 http_process_chunk_header(struct comm_point* c)
1876 char* line = http_header_line(c->buffer);
1878 if(c->http_in_chunk_headers == 3) {
1879 verbose(VERB_ALGO, "http chunk trailer: %s", line);
1881 if(line[0] == 0 && c->tcp_byte_count == 0) {
1882 /* callback of http reader when NETEVENT_DONE,
1883 * end of data, with no data in buffer */
1884 sldns_buffer_set_position(c->buffer, 0);
1885 sldns_buffer_set_limit(c->buffer, 0);
1886 fptr_ok(fptr_whitelist_comm_point(c->callback));
1887 (void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL);
1888 /* return that we are done */
1892 /* continue with header of the next chunk */
1893 c->http_in_chunk_headers = 1;
1894 /* remove header text from front of buffer */
1895 http_moveover_buffer(c->buffer);
1896 sldns_buffer_flip(c->buffer);
1899 /* ignore further trail headers */
1902 verbose(VERB_ALGO, "http chunk header: %s", line);
1903 if(c->http_in_chunk_headers == 1) {
1904 /* read chunked start line */
1906 c->tcp_byte_count = (size_t)strtol(line, &end, 16);
1909 c->http_in_chunk_headers = 0;
1910 /* remove header text from front of buffer */
1911 http_moveover_buffer(c->buffer);
1912 sldns_buffer_flip(c->buffer);
1913 if(c->tcp_byte_count == 0) {
1914 /* done with chunks, process chunk_trailer lines */
1915 c->http_in_chunk_headers = 3;
1919 /* ignore other headers */
1923 /** handle nonchunked data segment */
1925 http_nonchunk_segment(struct comm_point* c)
1927 /* c->buffer at position..limit has new data we read in.
1928 * the buffer itself is full of nonchunked data.
1929 * we are looking to read tcp_byte_count more data
1930 * and then the transfer is done. */
1931 size_t remainbufferlen;
1932 size_t got_now = sldns_buffer_limit(c->buffer) - c->http_stored;
1933 if(c->tcp_byte_count <= got_now) {
1934 /* done, this is the last data fragment */
1936 sldns_buffer_set_position(c->buffer, 0);
1937 fptr_ok(fptr_whitelist_comm_point(c->callback));
1938 (void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL);
1941 c->tcp_byte_count -= got_now;
1942 /* if we have the buffer space,
1943 * read more data collected into the buffer */
1944 remainbufferlen = sldns_buffer_capacity(c->buffer) -
1945 sldns_buffer_limit(c->buffer);
1946 if(remainbufferlen >= c->tcp_byte_count ||
1947 remainbufferlen >= 2048) {
1948 size_t total = sldns_buffer_limit(c->buffer);
1949 sldns_buffer_clear(c->buffer);
1950 sldns_buffer_set_position(c->buffer, total);
1951 c->http_stored = total;
1952 /* return and wait to read more */
1955 /* call callback with this data amount, then
1958 sldns_buffer_set_position(c->buffer, 0);
1959 fptr_ok(fptr_whitelist_comm_point(c->callback));
1960 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL);
1961 /* c->callback has to buffer_clear(c->buffer). */
1962 /* return and wait to read more */
1966 /** handle nonchunked data segment, return 0=fail, 1=wait, 2=process more */
1968 http_chunked_segment(struct comm_point* c)
1970 /* the c->buffer has from position..limit new data we read. */
1971 /* the current chunk has length tcp_byte_count.
1972 * once we read that read more chunk headers.
1974 size_t remainbufferlen;
1975 size_t got_now = sldns_buffer_limit(c->buffer) - c->http_stored;
1976 if(c->tcp_byte_count <= got_now) {
1977 /* the chunk has completed (with perhaps some extra data
1978 * from next chunk header and next chunk) */
1979 /* save too much info into temp buffer */
1981 struct comm_reply repinfo;
1983 sldns_buffer_skip(c->buffer, (ssize_t)c->tcp_byte_count);
1984 sldns_buffer_clear(c->http_temp);
1985 sldns_buffer_write(c->http_temp,
1986 sldns_buffer_current(c->buffer),
1987 sldns_buffer_remaining(c->buffer));
1988 sldns_buffer_flip(c->http_temp);
1990 /* callback with this fragment */
1991 fraglen = sldns_buffer_position(c->buffer);
1992 sldns_buffer_set_position(c->buffer, 0);
1993 sldns_buffer_set_limit(c->buffer, fraglen);
1994 repinfo = c->repinfo;
1995 fptr_ok(fptr_whitelist_comm_point(c->callback));
1996 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &repinfo);
1997 /* c->callback has to buffer_clear(). */
1999 /* is commpoint deleted? */
2003 /* copy waiting info */
2004 sldns_buffer_clear(c->buffer);
2005 sldns_buffer_write(c->buffer,
2006 sldns_buffer_begin(c->http_temp),
2007 sldns_buffer_remaining(c->http_temp));
2008 sldns_buffer_flip(c->buffer);
2009 /* process end of chunk trailer header lines, until
2011 c->http_in_chunk_headers = 3;
2012 /* process more data in buffer (if any) */
2015 c->tcp_byte_count -= got_now;
2017 /* if we have the buffer space,
2018 * read more data collected into the buffer */
2019 remainbufferlen = sldns_buffer_capacity(c->buffer) -
2020 sldns_buffer_limit(c->buffer);
2021 if(remainbufferlen >= c->tcp_byte_count ||
2022 remainbufferlen >= 2048) {
2023 size_t total = sldns_buffer_limit(c->buffer);
2024 sldns_buffer_clear(c->buffer);
2025 sldns_buffer_set_position(c->buffer, total);
2026 c->http_stored = total;
2027 /* return and wait to read more */
2031 /* callback of http reader for a new part of the data */
2033 sldns_buffer_set_position(c->buffer, 0);
2034 fptr_ok(fptr_whitelist_comm_point(c->callback));
2035 (void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL);
2036 /* c->callback has to buffer_clear(c->buffer). */
2037 /* return and wait to read more */
2042 * Handle http reading callback.
2043 * @param fd: file descriptor of socket.
2044 * @param c: comm point to read from into buffer.
2045 * @return: 0 on error
2048 comm_point_http_handle_read(int fd, struct comm_point* c)
2050 log_assert(c->type == comm_http);
2051 log_assert(fd != -1);
2053 /* if we are in ssl handshake, handle SSL handshake */
2055 if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
2056 if(!ssl_handshake(c))
2058 if(c->ssl_shake_state != comm_ssl_shake_none)
2061 #endif /* HAVE_SSL */
2063 if(!c->tcp_is_reading)
2065 /* read more data */
2067 if(!ssl_http_read_more(c))
2070 if(!http_read_more(fd, c))
2074 sldns_buffer_flip(c->buffer);
2075 while(sldns_buffer_remaining(c->buffer) > 0) {
2076 /* if we are reading headers, read more headers */
2077 if(c->http_in_headers || c->http_in_chunk_headers) {
2078 /* if header is done, process the header */
2079 if(!http_header_done(c->buffer)) {
2080 /* copy remaining data to front of buffer
2081 * and set rest for writing into it */
2082 http_moveover_buffer(c->buffer);
2083 /* return and wait to read more */
2086 if(!c->http_in_chunk_headers) {
2087 /* process initial headers */
2088 if(!http_process_initial_header(c))
2091 /* process chunk headers */
2092 int r = http_process_chunk_header(c);
2093 if(r == 0) return 0;
2094 if(r == 2) return 1; /* done */
2095 /* r == 1, continue */
2097 /* see if we have more to process */
2101 if(!c->http_is_chunked) {
2102 /* if we are reading nonchunks, process that*/
2103 return http_nonchunk_segment(c);
2105 /* if we are reading chunks, read the chunk */
2106 int r = http_chunked_segment(c);
2107 if(r == 0) return 0;
2108 if(r == 1) return 1;
2112 /* broke out of the loop; could not process header instead need
2114 /* moveover any remaining data and read more data */
2115 http_moveover_buffer(c->buffer);
2116 /* return and wait to read more */
2120 /** check pending connect for http */
2122 http_check_connect(int fd, struct comm_point* c)
2124 /* check for pending error from nonblocking connect */
2125 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/
2127 socklen_t len = (socklen_t)sizeof(error);
2128 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
2131 error = errno; /* on solaris errno is error */
2132 #else /* USE_WINSOCK */
2133 error = WSAGetLastError();
2137 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
2138 if(error == EINPROGRESS || error == EWOULDBLOCK)
2139 return 1; /* try again later */
2142 if(error != 0 && verbosity < 2)
2143 return 0; /* silence lots of chatter in the logs */
2144 else if(error != 0) {
2145 log_err_addr("http connect", strerror(error),
2146 &c->repinfo.addr, c->repinfo.addrlen);
2147 #else /* USE_WINSOCK */
2149 if(error == WSAEINPROGRESS)
2151 else if(error == WSAEWOULDBLOCK) {
2152 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2154 } else if(error != 0 && verbosity < 2)
2156 else if(error != 0) {
2157 log_err_addr("http connect", wsa_strerror(error),
2158 &c->repinfo.addr, c->repinfo.addrlen);
2159 #endif /* USE_WINSOCK */
2162 /* keep on processing this socket */
2166 /** write more data for http (with ssl) */
2168 ssl_http_write_more(struct comm_point* c)
2172 log_assert(sldns_buffer_remaining(c->buffer) > 0);
2174 r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
2175 (int)sldns_buffer_remaining(c->buffer));
2177 int want = SSL_get_error(c->ssl, r);
2178 if(want == SSL_ERROR_ZERO_RETURN) {
2179 return 0; /* closed */
2180 } else if(want == SSL_ERROR_WANT_READ) {
2181 c->ssl_shake_state = comm_ssl_shake_read;
2182 comm_point_listen_for_rw(c, 1, 0);
2183 return 1; /* wait for read condition */
2184 } else if(want == SSL_ERROR_WANT_WRITE) {
2185 return 1; /* write more later */
2186 } else if(want == SSL_ERROR_SYSCALL) {
2188 log_err("SSL_write syscall: %s",
2192 log_crypto_err("could not SSL_write");
2195 sldns_buffer_skip(c->buffer, (ssize_t)r);
2200 #endif /* HAVE_SSL */
2203 /** write more data for http */
2205 http_write_more(int fd, struct comm_point* c)
2208 log_assert(sldns_buffer_remaining(c->buffer) > 0);
2209 r = send(fd, (void*)sldns_buffer_current(c->buffer),
2210 sldns_buffer_remaining(c->buffer), 0);
2213 if(errno == EINTR || errno == EAGAIN)
2215 log_err_addr("http send r", strerror(errno),
2216 &c->repinfo.addr, c->repinfo.addrlen);
2218 if(WSAGetLastError() == WSAEINPROGRESS)
2220 if(WSAGetLastError() == WSAEWOULDBLOCK) {
2221 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2224 log_err_addr("http send r", wsa_strerror(WSAGetLastError()),
2225 &c->repinfo.addr, c->repinfo.addrlen);
2229 sldns_buffer_skip(c->buffer, r);
2234 * Handle http writing callback.
2235 * @param fd: file descriptor of socket.
2236 * @param c: comm point to write buffer out of.
2237 * @return: 0 on error
2240 comm_point_http_handle_write(int fd, struct comm_point* c)
2242 log_assert(c->type == comm_http);
2243 log_assert(fd != -1);
2245 /* check pending connect errors, if that fails, we wait for more,
2246 * or we can continue to write contents */
2247 if(c->tcp_check_nb_connect) {
2248 int r = http_check_connect(fd, c);
2249 if(r == 0) return 0;
2250 if(r == 1) return 1;
2251 c->tcp_check_nb_connect = 0;
2253 /* if we are in ssl handshake, handle SSL handshake */
2255 if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
2256 if(!ssl_handshake(c))
2258 if(c->ssl_shake_state != comm_ssl_shake_none)
2261 #endif /* HAVE_SSL */
2262 if(c->tcp_is_reading)
2264 /* if we are writing, write more */
2266 if(!ssl_http_write_more(c))
2269 if(!http_write_more(fd, c))
2273 /* we write a single buffer contents, that can contain
2274 * the http request, and then flip to read the results */
2275 /* see if write is done */
2276 if(sldns_buffer_remaining(c->buffer) == 0) {
2277 sldns_buffer_clear(c->buffer);
2278 if(c->tcp_do_toggle_rw)
2279 c->tcp_is_reading = 1;
2280 c->tcp_byte_count = 0;
2281 /* switch from listening(write) to listening(read) */
2282 comm_point_stop_listening(c);
2283 comm_point_start_listening(c, -1, -1);
2289 comm_point_http_handle_callback(int fd, short event, void* arg)
2291 struct comm_point* c = (struct comm_point*)arg;
2292 log_assert(c->type == comm_http);
2293 ub_comm_base_now(c->ev->base);
2295 if(event&UB_EV_READ) {
2296 if(!comm_point_http_handle_read(fd, c)) {
2297 reclaim_http_handler(c);
2298 if(!c->tcp_do_close) {
2299 fptr_ok(fptr_whitelist_comm_point(
2301 (void)(*c->callback)(c, c->cb_arg,
2302 NETEVENT_CLOSED, NULL);
2307 if(event&UB_EV_WRITE) {
2308 if(!comm_point_http_handle_write(fd, c)) {
2309 reclaim_http_handler(c);
2310 if(!c->tcp_do_close) {
2311 fptr_ok(fptr_whitelist_comm_point(
2313 (void)(*c->callback)(c, c->cb_arg,
2314 NETEVENT_CLOSED, NULL);
2319 if(event&UB_EV_TIMEOUT) {
2320 verbose(VERB_QUERY, "http took too long, dropped");
2321 reclaim_http_handler(c);
2322 if(!c->tcp_do_close) {
2323 fptr_ok(fptr_whitelist_comm_point(c->callback));
2324 (void)(*c->callback)(c, c->cb_arg,
2325 NETEVENT_TIMEOUT, NULL);
2329 log_err("Ignored event %d for httphdl.", event);
2332 void comm_point_local_handle_callback(int fd, short event, void* arg)
2334 struct comm_point* c = (struct comm_point*)arg;
2335 log_assert(c->type == comm_local);
2336 ub_comm_base_now(c->ev->base);
2338 if(event&UB_EV_READ) {
2339 if(!comm_point_tcp_handle_read(fd, c, 1)) {
2340 fptr_ok(fptr_whitelist_comm_point(c->callback));
2341 (void)(*c->callback)(c, c->cb_arg, NETEVENT_CLOSED,
2346 log_err("Ignored event %d for localhdl.", event);
2349 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
2350 short event, void* arg)
2352 struct comm_point* c = (struct comm_point*)arg;
2353 int err = NETEVENT_NOERROR;
2354 log_assert(c->type == comm_raw);
2355 ub_comm_base_now(c->ev->base);
2357 if(event&UB_EV_TIMEOUT)
2358 err = NETEVENT_TIMEOUT;
2359 fptr_ok(fptr_whitelist_comm_point_raw(c->callback));
2360 (void)(*c->callback)(c, c->cb_arg, err, NULL);
2364 comm_point_create_udp(struct comm_base *base, int fd, sldns_buffer* buffer,
2365 comm_point_callback_type* callback, void* callback_arg)
2367 struct comm_point* c = (struct comm_point*)calloc(1,
2368 sizeof(struct comm_point));
2372 c->ev = (struct internal_event*)calloc(1,
2373 sizeof(struct internal_event));
2382 c->tcp_is_reading = 0;
2383 c->tcp_byte_count = 0;
2384 c->tcp_parent = NULL;
2385 c->max_tcp_count = 0;
2386 c->cur_tcp_count = 0;
2387 c->tcp_handlers = NULL;
2390 c->tcp_do_close = 0;
2391 c->do_not_close = 0;
2392 c->tcp_do_toggle_rw = 0;
2393 c->tcp_check_nb_connect = 0;
2394 #ifdef USE_MSG_FASTOPEN
2395 c->tcp_do_fastopen = 0;
2399 c->dnscrypt_buffer = buffer;
2402 c->callback = callback;
2403 c->cb_arg = callback_arg;
2404 evbits = UB_EV_READ | UB_EV_PERSIST;
2405 /* ub_event stuff */
2406 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2407 comm_point_udp_callback, c);
2408 if(c->ev->ev == NULL) {
2409 log_err("could not baseset udp event");
2410 comm_point_delete(c);
2413 if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) {
2414 log_err("could not add udp event");
2415 comm_point_delete(c);
2422 comm_point_create_udp_ancil(struct comm_base *base, int fd,
2423 sldns_buffer* buffer,
2424 comm_point_callback_type* callback, void* callback_arg)
2426 struct comm_point* c = (struct comm_point*)calloc(1,
2427 sizeof(struct comm_point));
2431 c->ev = (struct internal_event*)calloc(1,
2432 sizeof(struct internal_event));
2441 c->tcp_is_reading = 0;
2442 c->tcp_byte_count = 0;
2443 c->tcp_parent = NULL;
2444 c->max_tcp_count = 0;
2445 c->cur_tcp_count = 0;
2446 c->tcp_handlers = NULL;
2449 c->tcp_do_close = 0;
2450 c->do_not_close = 0;
2453 c->dnscrypt_buffer = buffer;
2456 c->tcp_do_toggle_rw = 0;
2457 c->tcp_check_nb_connect = 0;
2458 #ifdef USE_MSG_FASTOPEN
2459 c->tcp_do_fastopen = 0;
2461 c->callback = callback;
2462 c->cb_arg = callback_arg;
2463 evbits = UB_EV_READ | UB_EV_PERSIST;
2464 /* ub_event stuff */
2465 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2466 comm_point_udp_ancil_callback, c);
2467 if(c->ev->ev == NULL) {
2468 log_err("could not baseset udp event");
2469 comm_point_delete(c);
2472 if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) {
2473 log_err("could not add udp event");
2474 comm_point_delete(c);
2480 static struct comm_point*
2481 comm_point_create_tcp_handler(struct comm_base *base,
2482 struct comm_point* parent, size_t bufsize,
2483 comm_point_callback_type* callback, void* callback_arg)
2485 struct comm_point* c = (struct comm_point*)calloc(1,
2486 sizeof(struct comm_point));
2490 c->ev = (struct internal_event*)calloc(1,
2491 sizeof(struct internal_event));
2498 c->buffer = sldns_buffer_new(bufsize);
2504 c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
2506 sldns_buffer_free(c->buffer);
2511 c->tcp_is_reading = 0;
2512 c->tcp_byte_count = 0;
2513 c->tcp_parent = parent;
2514 c->max_tcp_count = 0;
2515 c->cur_tcp_count = 0;
2516 c->tcp_handlers = NULL;
2519 c->tcp_do_close = 0;
2520 c->do_not_close = 0;
2521 c->tcp_do_toggle_rw = 1;
2522 c->tcp_check_nb_connect = 0;
2523 #ifdef USE_MSG_FASTOPEN
2524 c->tcp_do_fastopen = 0;
2528 /* We don't know just yet if this is a dnscrypt channel. Allocation
2529 * will be done when handling the callback. */
2530 c->dnscrypt_buffer = c->buffer;
2533 c->callback = callback;
2534 c->cb_arg = callback_arg;
2535 /* add to parent free list */
2536 c->tcp_free = parent->tcp_free;
2537 parent->tcp_free = c;
2538 /* ub_event stuff */
2539 evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT;
2540 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2541 comm_point_tcp_handle_callback, c);
2542 if(c->ev->ev == NULL)
2544 log_err("could not basetset tcphdl event");
2545 parent->tcp_free = c->tcp_free;
2554 comm_point_create_tcp(struct comm_base *base, int fd, int num, size_t bufsize,
2555 comm_point_callback_type* callback, void* callback_arg)
2557 struct comm_point* c = (struct comm_point*)calloc(1,
2558 sizeof(struct comm_point));
2561 /* first allocate the TCP accept listener */
2564 c->ev = (struct internal_event*)calloc(1,
2565 sizeof(struct internal_event));
2574 c->tcp_is_reading = 0;
2575 c->tcp_byte_count = 0;
2576 c->tcp_parent = NULL;
2577 c->max_tcp_count = num;
2578 c->cur_tcp_count = 0;
2579 c->tcp_handlers = (struct comm_point**)calloc((size_t)num,
2580 sizeof(struct comm_point*));
2581 if(!c->tcp_handlers) {
2587 c->type = comm_tcp_accept;
2588 c->tcp_do_close = 0;
2589 c->do_not_close = 0;
2590 c->tcp_do_toggle_rw = 0;
2591 c->tcp_check_nb_connect = 0;
2592 #ifdef USE_MSG_FASTOPEN
2593 c->tcp_do_fastopen = 0;
2597 c->dnscrypt_buffer = NULL;
2601 evbits = UB_EV_READ | UB_EV_PERSIST;
2602 /* ub_event stuff */
2603 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2604 comm_point_tcp_accept_callback, c);
2605 if(c->ev->ev == NULL) {
2606 log_err("could not baseset tcpacc event");
2607 comm_point_delete(c);
2610 if (ub_event_add(c->ev->ev, c->timeout) != 0) {
2611 log_err("could not add tcpacc event");
2612 comm_point_delete(c);
2615 /* now prealloc the tcp handlers */
2616 for(i=0; i<num; i++) {
2617 c->tcp_handlers[i] = comm_point_create_tcp_handler(base,
2618 c, bufsize, callback, callback_arg);
2619 if(!c->tcp_handlers[i]) {
2620 comm_point_delete(c);
2629 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize,
2630 comm_point_callback_type* callback, void* callback_arg)
2632 struct comm_point* c = (struct comm_point*)calloc(1,
2633 sizeof(struct comm_point));
2637 c->ev = (struct internal_event*)calloc(1,
2638 sizeof(struct internal_event));
2645 c->buffer = sldns_buffer_new(bufsize);
2652 c->tcp_is_reading = 0;
2653 c->tcp_byte_count = 0;
2654 c->tcp_parent = NULL;
2655 c->max_tcp_count = 0;
2656 c->cur_tcp_count = 0;
2657 c->tcp_handlers = NULL;
2660 c->tcp_do_close = 0;
2661 c->do_not_close = 0;
2662 c->tcp_do_toggle_rw = 1;
2663 c->tcp_check_nb_connect = 1;
2664 #ifdef USE_MSG_FASTOPEN
2665 c->tcp_do_fastopen = 1;
2669 c->dnscrypt_buffer = c->buffer;
2672 c->callback = callback;
2673 c->cb_arg = callback_arg;
2674 evbits = UB_EV_PERSIST | UB_EV_WRITE;
2675 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2676 comm_point_tcp_handle_callback, c);
2677 if(c->ev->ev == NULL)
2679 log_err("could not baseset tcpout event");
2680 sldns_buffer_free(c->buffer);
2690 comm_point_create_http_out(struct comm_base *base, size_t bufsize,
2691 comm_point_callback_type* callback, void* callback_arg,
2694 struct comm_point* c = (struct comm_point*)calloc(1,
2695 sizeof(struct comm_point));
2699 c->ev = (struct internal_event*)calloc(1,
2700 sizeof(struct internal_event));
2707 c->buffer = sldns_buffer_new(bufsize);
2714 c->tcp_is_reading = 0;
2715 c->tcp_byte_count = 0;
2716 c->tcp_parent = NULL;
2717 c->max_tcp_count = 0;
2718 c->cur_tcp_count = 0;
2719 c->tcp_handlers = NULL;
2721 c->type = comm_http;
2722 c->tcp_do_close = 0;
2723 c->do_not_close = 0;
2724 c->tcp_do_toggle_rw = 1;
2725 c->tcp_check_nb_connect = 1;
2726 c->http_in_headers = 1;
2727 c->http_in_chunk_headers = 0;
2728 c->http_is_chunked = 0;
2729 c->http_temp = temp;
2730 #ifdef USE_MSG_FASTOPEN
2731 c->tcp_do_fastopen = 1;
2735 c->dnscrypt_buffer = c->buffer;
2738 c->callback = callback;
2739 c->cb_arg = callback_arg;
2740 evbits = UB_EV_PERSIST | UB_EV_WRITE;
2741 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2742 comm_point_http_handle_callback, c);
2743 if(c->ev->ev == NULL)
2745 log_err("could not baseset tcpout event");
2749 sldns_buffer_free(c->buffer);
2759 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize,
2760 comm_point_callback_type* callback, void* callback_arg)
2762 struct comm_point* c = (struct comm_point*)calloc(1,
2763 sizeof(struct comm_point));
2767 c->ev = (struct internal_event*)calloc(1,
2768 sizeof(struct internal_event));
2775 c->buffer = sldns_buffer_new(bufsize);
2782 c->tcp_is_reading = 1;
2783 c->tcp_byte_count = 0;
2784 c->tcp_parent = NULL;
2785 c->max_tcp_count = 0;
2786 c->cur_tcp_count = 0;
2787 c->tcp_handlers = NULL;
2789 c->type = comm_local;
2790 c->tcp_do_close = 0;
2791 c->do_not_close = 1;
2792 c->tcp_do_toggle_rw = 0;
2793 c->tcp_check_nb_connect = 0;
2794 #ifdef USE_MSG_FASTOPEN
2795 c->tcp_do_fastopen = 0;
2799 c->dnscrypt_buffer = c->buffer;
2801 c->callback = callback;
2802 c->cb_arg = callback_arg;
2803 /* ub_event stuff */
2804 evbits = UB_EV_PERSIST | UB_EV_READ;
2805 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2806 comm_point_local_handle_callback, c);
2807 if(c->ev->ev == NULL) {
2808 log_err("could not baseset localhdl event");
2813 if (ub_event_add(c->ev->ev, c->timeout) != 0) {
2814 log_err("could not add localhdl event");
2815 ub_event_free(c->ev->ev);
2824 comm_point_create_raw(struct comm_base* base, int fd, int writing,
2825 comm_point_callback_type* callback, void* callback_arg)
2827 struct comm_point* c = (struct comm_point*)calloc(1,
2828 sizeof(struct comm_point));
2832 c->ev = (struct internal_event*)calloc(1,
2833 sizeof(struct internal_event));
2842 c->tcp_is_reading = 0;
2843 c->tcp_byte_count = 0;
2844 c->tcp_parent = NULL;
2845 c->max_tcp_count = 0;
2846 c->cur_tcp_count = 0;
2847 c->tcp_handlers = NULL;
2850 c->tcp_do_close = 0;
2851 c->do_not_close = 1;
2852 c->tcp_do_toggle_rw = 0;
2853 c->tcp_check_nb_connect = 0;
2854 #ifdef USE_MSG_FASTOPEN
2855 c->tcp_do_fastopen = 0;
2859 c->dnscrypt_buffer = c->buffer;
2861 c->callback = callback;
2862 c->cb_arg = callback_arg;
2863 /* ub_event stuff */
2865 evbits = UB_EV_PERSIST | UB_EV_WRITE;
2866 else evbits = UB_EV_PERSIST | UB_EV_READ;
2867 c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
2868 comm_point_raw_handle_callback, c);
2869 if(c->ev->ev == NULL) {
2870 log_err("could not baseset rawhdl event");
2875 if (ub_event_add(c->ev->ev, c->timeout) != 0) {
2876 log_err("could not add rawhdl event");
2877 ub_event_free(c->ev->ev);
2886 comm_point_close(struct comm_point* c)
2891 if(ub_event_del(c->ev->ev) != 0) {
2892 log_err("could not event_del on close");
2894 /* close fd after removing from event lists, or epoll.. is messed up */
2895 if(c->fd != -1 && !c->do_not_close) {
2896 verbose(VERB_ALGO, "close fd %d", c->fd);
2907 comm_point_delete(struct comm_point* c)
2911 if((c->type == comm_tcp || c->type == comm_http) && c->ssl) {
2913 SSL_shutdown(c->ssl);
2917 comm_point_close(c);
2918 if(c->tcp_handlers) {
2920 for(i=0; i<c->max_tcp_count; i++)
2921 comm_point_delete(c->tcp_handlers[i]);
2922 free(c->tcp_handlers);
2925 if(c->type == comm_tcp || c->type == comm_local || c->type == comm_http) {
2926 sldns_buffer_free(c->buffer);
2928 if(c->dnscrypt && c->dnscrypt_buffer != c->buffer) {
2929 sldns_buffer_free(c->dnscrypt_buffer);
2933 ub_event_free(c->ev->ev);
2939 comm_point_send_reply(struct comm_reply *repinfo)
2941 struct sldns_buffer* buffer;
2942 log_assert(repinfo && repinfo->c);
2944 buffer = repinfo->c->dnscrypt_buffer;
2945 if(!dnsc_handle_uncurved_request(repinfo)) {
2949 buffer = repinfo->c->buffer;
2951 if(repinfo->c->type == comm_udp) {
2952 if(repinfo->srctype)
2953 comm_point_send_udp_msg_if(repinfo->c,
2954 buffer, (struct sockaddr*)&repinfo->addr,
2955 repinfo->addrlen, repinfo);
2957 comm_point_send_udp_msg(repinfo->c, buffer,
2958 (struct sockaddr*)&repinfo->addr, repinfo->addrlen);
2960 if(repinfo->c->dtenv != NULL &&
2961 repinfo->c->dtenv->log_client_response_messages)
2962 dt_msg_send_client_response(repinfo->c->dtenv,
2963 &repinfo->addr, repinfo->c->type, repinfo->c->buffer);
2967 if(repinfo->c->tcp_parent->dtenv != NULL &&
2968 repinfo->c->tcp_parent->dtenv->log_client_response_messages)
2969 dt_msg_send_client_response(repinfo->c->tcp_parent->dtenv,
2970 &repinfo->addr, repinfo->c->type, repinfo->c->buffer);
2972 comm_point_start_listening(repinfo->c, -1,
2973 repinfo->c->tcp_timeout_msec);
2978 comm_point_drop_reply(struct comm_reply* repinfo)
2982 log_assert(repinfo && repinfo->c);
2983 log_assert(repinfo->c->type != comm_tcp_accept);
2984 if(repinfo->c->type == comm_udp)
2986 reclaim_tcp_handler(repinfo->c);
2990 comm_point_stop_listening(struct comm_point* c)
2992 verbose(VERB_ALGO, "comm point stop listening %d", c->fd);
2993 if(ub_event_del(c->ev->ev) != 0) {
2994 log_err("event_del error to stoplisten");
2999 comm_point_start_listening(struct comm_point* c, int newfd, int msec)
3001 verbose(VERB_ALGO, "comm point start listening %d",
3002 c->fd==-1?newfd:c->fd);
3003 if(c->type == comm_tcp_accept && !c->tcp_free) {
3004 /* no use to start listening no free slots. */
3007 if(msec != -1 && msec != 0) {
3009 c->timeout = (struct timeval*)malloc(sizeof(
3012 log_err("cpsl: malloc failed. No net read.");
3016 ub_event_add_bits(c->ev->ev, UB_EV_TIMEOUT);
3017 #ifndef S_SPLINT_S /* splint fails on struct timeval. */
3018 c->timeout->tv_sec = msec/1000;
3019 c->timeout->tv_usec = (msec%1000)*1000;
3020 #endif /* S_SPLINT_S */
3022 if(c->type == comm_tcp || c->type == comm_http) {
3023 ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
3024 if(c->tcp_is_reading)
3025 ub_event_add_bits(c->ev->ev, UB_EV_READ);
3026 else ub_event_add_bits(c->ev->ev, UB_EV_WRITE);
3037 ub_event_set_fd(c->ev->ev, c->fd);
3039 if(ub_event_add(c->ev->ev, msec==0?NULL:c->timeout) != 0) {
3040 log_err("event_add failed. in cpsl.");
3044 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr)
3046 verbose(VERB_ALGO, "comm point listen_for_rw %d %d", c->fd, wr);
3047 if(ub_event_del(c->ev->ev) != 0) {
3048 log_err("event_del error to cplf");
3050 ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
3051 if(rd) ub_event_add_bits(c->ev->ev, UB_EV_READ);
3052 if(wr) ub_event_add_bits(c->ev->ev, UB_EV_WRITE);
3053 if(ub_event_add(c->ev->ev, c->timeout) != 0) {
3054 log_err("event_add failed. in cplf.");
3058 size_t comm_point_get_mem(struct comm_point* c)
3063 s = sizeof(*c) + sizeof(*c->ev);
3065 s += sizeof(*c->timeout);
3066 if(c->type == comm_tcp || c->type == comm_local) {
3067 s += sizeof(*c->buffer) + sldns_buffer_capacity(c->buffer);
3069 s += sizeof(*c->dnscrypt_buffer);
3070 if(c->buffer != c->dnscrypt_buffer) {
3071 s += sldns_buffer_capacity(c->dnscrypt_buffer);
3075 if(c->type == comm_tcp_accept) {
3077 for(i=0; i<c->max_tcp_count; i++)
3078 s += comm_point_get_mem(c->tcp_handlers[i]);
3084 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
3086 struct internal_timer *tm = (struct internal_timer*)calloc(1,
3087 sizeof(struct internal_timer));
3089 log_err("malloc failed");
3092 tm->super.ev_timer = tm;
3094 tm->super.callback = cb;
3095 tm->super.cb_arg = cb_arg;
3096 tm->ev = ub_event_new(base->eb->base, -1, UB_EV_TIMEOUT,
3097 comm_timer_callback, &tm->super);
3098 if(tm->ev == NULL) {
3099 log_err("timer_create: event_base_set failed.");
3107 comm_timer_disable(struct comm_timer* timer)
3111 ub_timer_del(timer->ev_timer->ev);
3112 timer->ev_timer->enabled = 0;
3116 comm_timer_set(struct comm_timer* timer, struct timeval* tv)
3119 if(timer->ev_timer->enabled)
3120 comm_timer_disable(timer);
3121 if(ub_timer_add(timer->ev_timer->ev, timer->ev_timer->base->eb->base,
3122 comm_timer_callback, timer, tv) != 0)
3123 log_err("comm_timer_set: evtimer_add failed.");
3124 timer->ev_timer->enabled = 1;
3128 comm_timer_delete(struct comm_timer* timer)
3132 comm_timer_disable(timer);
3133 /* Free the sub struct timer->ev_timer derived from the super struct timer.
3134 * i.e. assert(timer == timer->ev_timer)
3136 ub_event_free(timer->ev_timer->ev);
3137 free(timer->ev_timer);
3141 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
3143 struct comm_timer* tm = (struct comm_timer*)arg;
3144 if(!(event&UB_EV_TIMEOUT))
3146 ub_comm_base_now(tm->ev_timer->base);
3147 tm->ev_timer->enabled = 0;
3148 fptr_ok(fptr_whitelist_comm_timer(tm->callback));
3149 (*tm->callback)(tm->cb_arg);
3153 comm_timer_is_set(struct comm_timer* timer)
3155 return (int)timer->ev_timer->enabled;
3159 comm_timer_get_mem(struct comm_timer* ATTR_UNUSED(timer))
3161 return sizeof(struct internal_timer);
3165 comm_signal_create(struct comm_base* base,
3166 void (*callback)(int, void*), void* cb_arg)
3168 struct comm_signal* com = (struct comm_signal*)malloc(
3169 sizeof(struct comm_signal));
3171 log_err("malloc failed");
3175 com->callback = callback;
3176 com->cb_arg = cb_arg;
3177 com->ev_signal = NULL;
3182 comm_signal_callback(int sig, short event, void* arg)
3184 struct comm_signal* comsig = (struct comm_signal*)arg;
3185 if(!(event & UB_EV_SIGNAL))
3187 ub_comm_base_now(comsig->base);
3188 fptr_ok(fptr_whitelist_comm_signal(comsig->callback));
3189 (*comsig->callback)(sig, comsig->cb_arg);
3193 comm_signal_bind(struct comm_signal* comsig, int sig)
3195 struct internal_signal* entry = (struct internal_signal*)calloc(1,
3196 sizeof(struct internal_signal));
3198 log_err("malloc failed");
3202 /* add signal event */
3203 entry->ev = ub_signal_new(comsig->base->eb->base, sig,
3204 comm_signal_callback, comsig);
3205 if(entry->ev == NULL) {
3206 log_err("Could not create signal event");
3210 if(ub_signal_add(entry->ev, NULL) != 0) {
3211 log_err("Could not add signal handler");
3212 ub_event_free(entry->ev);
3216 /* link into list */
3217 entry->next = comsig->ev_signal;
3218 comsig->ev_signal = entry;
3223 comm_signal_delete(struct comm_signal* comsig)
3225 struct internal_signal* p, *np;
3228 p=comsig->ev_signal;
3231 ub_signal_del(p->ev);
3232 ub_event_free(p->ev);