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 LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
39 * This file contains event notification functions.
42 #include <ldns/wire2host.h>
43 #include "util/netevent.h"
45 #include "util/net_help.h"
46 #include "util/fptr_wlist.h"
47 #ifdef HAVE_OPENSSL_SSL_H
48 #include <openssl/ssl.h>
50 #ifdef HAVE_OPENSSL_ERR_H
51 #include <openssl/err.h>
54 /* -------- Start of local definitions -------- */
55 /** if CMSG_ALIGN is not defined on this platform, a workaround */
57 # ifdef _CMSG_DATA_ALIGN
58 # define CMSG_ALIGN _CMSG_DATA_ALIGN
60 # define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
64 /** if CMSG_LEN is not defined on this platform, a workaround */
66 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
69 /** if CMSG_SPACE is not defined on this platform, a workaround */
71 # ifdef _CMSG_HDR_ALIGN
72 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)))
74 # define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr)))
78 /** The TCP reading or writing query timeout in seconds */
79 #define TCP_QUERY_TIMEOUT 120
81 #ifndef NONBLOCKING_IS_BROKEN
82 /** number of UDP reads to perform per read indication from select */
83 #define NUM_UDP_PER_SELECT 100
85 #define NUM_UDP_PER_SELECT 1
88 /* We define libevent structures here to hide the libevent stuff. */
92 # include "util/winsock_event.h"
94 # include "util/mini_event.h"
95 # endif /* USE_WINSOCK */
96 #else /* USE_MINI_EVENT */
101 # include "event2/event.h"
102 # include "event2/event_struct.h"
103 # include "event2/event_compat.h"
105 #endif /* USE_MINI_EVENT */
108 * The internal event structure for keeping libevent info for the event.
109 * Possibly other structures (list, tree) this is part of.
111 struct internal_event {
113 struct comm_base* base;
114 /** libevent event type, alloced here */
119 * Internal base structure, so that every thread has its own events.
121 struct internal_base {
122 /** libevent event_base type. */
123 struct event_base* base;
124 /** seconds time pointer points here */
126 /** timeval with current time */
128 /** the event used for slow_accept timeouts */
129 struct event slow_accept;
130 /** true if slow_accept is enabled */
131 int slow_accept_enabled;
135 * Internal timer structure, to store timer event in.
137 struct internal_timer {
139 struct comm_base* base;
140 /** libevent event type, alloced here */
142 /** is timer enabled */
147 * Internal signal structure, to store signal event in.
149 struct internal_signal {
150 /** libevent event type, alloced here */
152 /** next in signal list */
153 struct internal_signal* next;
156 /** create a tcp handler with a parent */
157 static struct comm_point* comm_point_create_tcp_handler(
158 struct comm_base *base, struct comm_point* parent, size_t bufsize,
159 comm_point_callback_t* callback, void* callback_arg);
161 /* -------- End of local definitions -------- */
163 #ifdef USE_MINI_EVENT
164 /** minievent updates the time when it blocks. */
165 #define comm_base_now(x) /* nothing to do */
166 #else /* !USE_MINI_EVENT */
167 /** fillup the time values in the event base */
169 comm_base_now(struct comm_base* b)
171 if(gettimeofday(&b->eb->now, NULL) < 0) {
172 log_err("gettimeofday: %s", strerror(errno));
174 b->eb->secs = (uint32_t)b->eb->now.tv_sec;
176 #endif /* USE_MINI_EVENT */
179 comm_base_create(int sigs)
181 struct comm_base* b = (struct comm_base*)calloc(1,
182 sizeof(struct comm_base));
185 b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
190 #ifdef USE_MINI_EVENT
192 /* use mini event time-sharing feature */
193 b->eb->base = event_init(&b->eb->secs, &b->eb->now);
195 # if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
198 b->eb->base=(struct event_base *)ev_default_loop(EVFLAG_AUTO);
200 b->eb->base=(struct event_base *)ev_loop_new(EVFLAG_AUTO);
203 # ifdef HAVE_EVENT_BASE_NEW
204 b->eb->base = event_base_new();
206 b->eb->base = event_init();
216 /* avoid event_get_method call which causes crashes even when
217 * not printing, because its result is passed */
219 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
221 #elif defined(USE_MINI_EVENT)
226 "%s uses %s method.",
228 #ifdef HAVE_EVENT_BASE_GET_METHOD
229 event_base_get_method(b->eb->base)
238 comm_base_delete(struct comm_base* b)
242 if(b->eb->slow_accept_enabled) {
243 if(event_del(&b->eb->slow_accept) != 0) {
244 log_err("could not event_del slow_accept");
247 #ifdef USE_MINI_EVENT
248 event_base_free(b->eb->base);
249 #elif defined(HAVE_EVENT_BASE_FREE) && defined(HAVE_EVENT_BASE_ONCE)
250 /* only libevent 1.2+ has it, but in 1.2 it is broken -
251 assertion fails on signal handling ev that is not deleted
252 in libevent 1.3c (event_base_once appears) this is fixed. */
253 event_base_free(b->eb->base);
254 #endif /* HAVE_EVENT_BASE_FREE and HAVE_EVENT_BASE_ONCE */
261 comm_base_timept(struct comm_base* b, uint32_t** tt, struct timeval** tv)
268 comm_base_dispatch(struct comm_base* b)
271 retval = event_base_dispatch(b->eb->base);
273 fatal_exit("event_dispatch returned error %d, "
274 "errno is %s", retval, strerror(errno));
278 void comm_base_exit(struct comm_base* b)
280 if(event_base_loopexit(b->eb->base, NULL) != 0) {
281 log_err("Could not loopexit");
285 void comm_base_set_slow_accept_handlers(struct comm_base* b,
286 void (*stop_acc)(void*), void (*start_acc)(void*), void* arg)
288 b->stop_accept = stop_acc;
289 b->start_accept = start_acc;
293 struct event_base* comm_base_internal(struct comm_base* b)
298 /** see if errno for udp has to be logged or not uses globals */
300 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
302 /* do not log transient errors (unless high verbosity) */
303 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN)
317 if(verbosity < VERB_ALGO)
323 /* squelch errors where people deploy AAAA ::ffff:bla for
324 * authority servers, which we try for intranets. */
325 if(errno == EINVAL && addr_is_ip4mapped(
326 (struct sockaddr_storage*)addr, addrlen) &&
327 verbosity < VERB_DETAIL)
329 /* SO_BROADCAST sockopt can give access to 255.255.255.255,
330 * but a dns cache does not need it. */
331 if(errno == EACCES && addr_is_broadcast(
332 (struct sockaddr_storage*)addr, addrlen) &&
333 verbosity < VERB_DETAIL)
338 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
340 return udp_send_errno_needs_log(addr, addrlen);
343 /* send a UDP reply */
345 comm_point_send_udp_msg(struct comm_point *c, ldns_buffer* packet,
346 struct sockaddr* addr, socklen_t addrlen)
349 log_assert(c->fd != -1);
351 if(ldns_buffer_remaining(packet) == 0)
352 log_err("error: send empty UDP packet");
354 log_assert(addr && addrlen > 0);
355 sent = sendto(c->fd, (void*)ldns_buffer_begin(packet),
356 ldns_buffer_remaining(packet), 0,
359 if(!udp_send_errno_needs_log(addr, addrlen))
362 verbose(VERB_OPS, "sendto failed: %s", strerror(errno));
364 verbose(VERB_OPS, "sendto failed: %s",
365 wsa_strerror(WSAGetLastError()));
367 log_addr(VERB_OPS, "remote address is",
368 (struct sockaddr_storage*)addr, addrlen);
370 } else if((size_t)sent != ldns_buffer_remaining(packet)) {
371 log_err("sent %d in place of %d bytes",
372 (int)sent, (int)ldns_buffer_remaining(packet));
378 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && (defined(HAVE_RECVMSG) || defined(HAVE_SENDMSG))
379 /** print debug ancillary info */
380 static void p_ancil(const char* str, struct comm_reply* r)
382 if(r->srctype != 4 && r->srctype != 6) {
383 log_info("%s: unknown srctype %d", str, r->srctype);
386 if(r->srctype == 6) {
388 if(inet_ntop(AF_INET6, &r->pktinfo.v6info.ipi6_addr,
389 buf, (socklen_t)sizeof(buf)) == 0) {
390 strncpy(buf, "(inet_ntop error)", sizeof(buf));
392 buf[sizeof(buf)-1]=0;
393 log_info("%s: %s %d", str, buf, r->pktinfo.v6info.ipi6_ifindex);
394 } else if(r->srctype == 4) {
396 char buf1[1024], buf2[1024];
397 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_addr,
398 buf1, (socklen_t)sizeof(buf1)) == 0) {
399 strncpy(buf1, "(inet_ntop error)", sizeof(buf1));
401 buf1[sizeof(buf1)-1]=0;
402 #ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST
403 if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_spec_dst,
404 buf2, (socklen_t)sizeof(buf2)) == 0) {
405 strncpy(buf2, "(inet_ntop error)", sizeof(buf2));
407 buf2[sizeof(buf2)-1]=0;
411 log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex,
413 #elif defined(IP_RECVDSTADDR)
415 if(inet_ntop(AF_INET, &r->pktinfo.v4addr,
416 buf1, (socklen_t)sizeof(buf1)) == 0) {
417 strncpy(buf1, "(inet_ntop error)", sizeof(buf1));
419 buf1[sizeof(buf1)-1]=0;
420 log_info("%s: %s", str, buf1);
421 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */
424 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */
426 /** send a UDP reply over specified interface*/
428 comm_point_send_udp_msg_if(struct comm_point *c, ldns_buffer* packet,
429 struct sockaddr* addr, socklen_t addrlen, struct comm_reply* r)
431 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG)
437 struct cmsghdr *cmsg;
438 #endif /* S_SPLINT_S */
440 log_assert(c->fd != -1);
442 if(ldns_buffer_remaining(packet) == 0)
443 log_err("error: send empty UDP packet");
445 log_assert(addr && addrlen > 0);
448 msg.msg_namelen = addrlen;
449 iov[0].iov_base = ldns_buffer_begin(packet);
450 iov[0].iov_len = ldns_buffer_remaining(packet);
453 msg.msg_control = control;
455 msg.msg_controllen = sizeof(control);
456 #endif /* S_SPLINT_S */
460 cmsg = CMSG_FIRSTHDR(&msg);
461 if(r->srctype == 4) {
463 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
464 log_assert(msg.msg_controllen <= sizeof(control));
465 cmsg->cmsg_level = IPPROTO_IP;
466 cmsg->cmsg_type = IP_PKTINFO;
467 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4info,
468 sizeof(struct in_pktinfo));
469 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
470 #elif defined(IP_SENDSRCADDR)
471 msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
472 log_assert(msg.msg_controllen <= sizeof(control));
473 cmsg->cmsg_level = IPPROTO_IP;
474 cmsg->cmsg_type = IP_SENDSRCADDR;
475 memmove(CMSG_DATA(cmsg), &r->pktinfo.v4addr,
476 sizeof(struct in_addr));
477 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
479 verbose(VERB_ALGO, "no IP_PKTINFO or IP_SENDSRCADDR");
480 msg.msg_control = NULL;
481 #endif /* IP_PKTINFO or IP_SENDSRCADDR */
482 } else if(r->srctype == 6) {
483 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
484 log_assert(msg.msg_controllen <= sizeof(control));
485 cmsg->cmsg_level = IPPROTO_IPV6;
486 cmsg->cmsg_type = IPV6_PKTINFO;
487 memmove(CMSG_DATA(cmsg), &r->pktinfo.v6info,
488 sizeof(struct in6_pktinfo));
489 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
491 /* try to pass all 0 to use default route */
492 msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
493 log_assert(msg.msg_controllen <= sizeof(control));
494 cmsg->cmsg_level = IPPROTO_IPV6;
495 cmsg->cmsg_type = IPV6_PKTINFO;
496 memset(CMSG_DATA(cmsg), 0, sizeof(struct in6_pktinfo));
497 cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
499 #endif /* S_SPLINT_S */
500 if(verbosity >= VERB_ALGO)
501 p_ancil("send_udp over interface", r);
502 sent = sendmsg(c->fd, &msg, 0);
504 if(!udp_send_errno_needs_log(addr, addrlen))
506 verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno));
507 log_addr(VERB_OPS, "remote address is",
508 (struct sockaddr_storage*)addr, addrlen);
510 } else if((size_t)sent != ldns_buffer_remaining(packet)) {
511 log_err("sent %d in place of %d bytes",
512 (int)sent, (int)ldns_buffer_remaining(packet));
522 log_err("sendmsg: IPV6_PKTINFO not supported");
524 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */
528 comm_point_udp_ancil_callback(int fd, short event, void* arg)
530 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
531 struct comm_reply rep;
538 struct cmsghdr* cmsg;
539 #endif /* S_SPLINT_S */
541 rep.c = (struct comm_point*)arg;
542 log_assert(rep.c->type == comm_udp);
546 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
547 comm_base_now(rep.c->ev->base);
548 for(i=0; i<NUM_UDP_PER_SELECT; i++) {
549 ldns_buffer_clear(rep.c->buffer);
550 rep.addrlen = (socklen_t)sizeof(rep.addr);
551 log_assert(fd != -1);
552 log_assert(ldns_buffer_remaining(rep.c->buffer) > 0);
553 msg.msg_name = &rep.addr;
554 msg.msg_namelen = (socklen_t)sizeof(rep.addr);
555 iov[0].iov_base = ldns_buffer_begin(rep.c->buffer);
556 iov[0].iov_len = ldns_buffer_remaining(rep.c->buffer);
559 msg.msg_control = ancil;
561 msg.msg_controllen = sizeof(ancil);
562 #endif /* S_SPLINT_S */
564 rcv = recvmsg(fd, &msg, 0);
566 if(errno != EAGAIN && errno != EINTR) {
567 log_err("recvmsg failed: %s", strerror(errno));
571 rep.addrlen = msg.msg_namelen;
572 ldns_buffer_skip(rep.c->buffer, rcv);
573 ldns_buffer_flip(rep.c->buffer);
576 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
577 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
578 if( cmsg->cmsg_level == IPPROTO_IPV6 &&
579 cmsg->cmsg_type == IPV6_PKTINFO) {
581 memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg),
582 sizeof(struct in6_pktinfo));
585 } else if( cmsg->cmsg_level == IPPROTO_IP &&
586 cmsg->cmsg_type == IP_PKTINFO) {
588 memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg),
589 sizeof(struct in_pktinfo));
591 #elif defined(IP_RECVDSTADDR)
592 } else if( cmsg->cmsg_level == IPPROTO_IP &&
593 cmsg->cmsg_type == IP_RECVDSTADDR) {
595 memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg),
596 sizeof(struct in_addr));
598 #endif /* IP_PKTINFO or IP_RECVDSTADDR */
601 if(verbosity >= VERB_ALGO)
602 p_ancil("receive_udp on interface", &rep);
603 #endif /* S_SPLINT_S */
604 fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
605 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
606 /* send back immediate reply */
607 (void)comm_point_send_udp_msg_if(rep.c, rep.c->buffer,
608 (struct sockaddr*)&rep.addr, rep.addrlen, &rep);
610 if(rep.c->fd == -1) /* commpoint closed */
617 fatal_exit("recvmsg: No support for IPV6_PKTINFO. "
618 "Please disable interface-automatic");
619 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG */
623 comm_point_udp_callback(int fd, short event, void* arg)
625 struct comm_reply rep;
629 rep.c = (struct comm_point*)arg;
630 log_assert(rep.c->type == comm_udp);
634 log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
635 comm_base_now(rep.c->ev->base);
636 for(i=0; i<NUM_UDP_PER_SELECT; i++) {
637 ldns_buffer_clear(rep.c->buffer);
638 rep.addrlen = (socklen_t)sizeof(rep.addr);
639 log_assert(fd != -1);
640 log_assert(ldns_buffer_remaining(rep.c->buffer) > 0);
641 rcv = recvfrom(fd, (void*)ldns_buffer_begin(rep.c->buffer),
642 ldns_buffer_remaining(rep.c->buffer), 0,
643 (struct sockaddr*)&rep.addr, &rep.addrlen);
646 if(errno != EAGAIN && errno != EINTR)
647 log_err("recvfrom %d failed: %s",
648 fd, strerror(errno));
650 if(WSAGetLastError() != WSAEINPROGRESS &&
651 WSAGetLastError() != WSAECONNRESET &&
652 WSAGetLastError()!= WSAEWOULDBLOCK)
653 log_err("recvfrom failed: %s",
654 wsa_strerror(WSAGetLastError()));
658 ldns_buffer_skip(rep.c->buffer, rcv);
659 ldns_buffer_flip(rep.c->buffer);
661 fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
662 if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
663 /* send back immediate reply */
664 (void)comm_point_send_udp_msg(rep.c, rep.c->buffer,
665 (struct sockaddr*)&rep.addr, rep.addrlen);
667 if(rep.c->fd != fd) /* commpoint closed to -1 or reused for
668 another UDP port. Note rep.c cannot be reused with TCP fd. */
673 /** Use a new tcp handler for new query fd, set to read query */
675 setup_tcp_handler(struct comm_point* c, int fd)
677 log_assert(c->type == comm_tcp);
678 log_assert(c->fd == -1);
679 ldns_buffer_clear(c->buffer);
680 c->tcp_is_reading = 1;
681 c->tcp_byte_count = 0;
682 comm_point_start_listening(c, fd, TCP_QUERY_TIMEOUT);
685 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
686 short ATTR_UNUSED(event), void* arg)
688 struct comm_base* b = (struct comm_base*)arg;
689 /* timeout for the slow accept, re-enable accepts again */
690 if(b->start_accept) {
691 verbose(VERB_ALGO, "wait is over, slow accept disabled");
692 fptr_ok(fptr_whitelist_start_accept(b->start_accept));
693 (*b->start_accept)(b->cb_arg);
694 b->eb->slow_accept_enabled = 0;
698 int comm_point_perform_accept(struct comm_point* c,
699 struct sockaddr_storage* addr, socklen_t* addrlen)
702 *addrlen = (socklen_t)sizeof(*addr);
703 new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen);
706 /* EINTR is signal interrupt. others are closed connection. */
707 if( errno == EINTR || errno == EAGAIN
709 || errno == EWOULDBLOCK
712 || errno == ECONNABORTED
719 #if defined(ENFILE) && defined(EMFILE)
720 if(errno == ENFILE || errno == EMFILE) {
721 /* out of file descriptors, likely outside of our
722 * control. stop accept() calls for some time */
723 if(c->ev->base->stop_accept) {
724 struct comm_base* b = c->ev->base;
726 verbose(VERB_ALGO, "out of file descriptors: "
728 b->eb->slow_accept_enabled = 1;
729 fptr_ok(fptr_whitelist_stop_accept(
731 (*b->stop_accept)(b->cb_arg);
732 /* set timeout, no mallocs */
733 tv.tv_sec = NETEVENT_SLOW_ACCEPT_TIME/1000;
734 tv.tv_usec = NETEVENT_SLOW_ACCEPT_TIME%1000;
735 event_set(&b->eb->slow_accept, -1, EV_TIMEOUT,
736 comm_base_handle_slow_accept, b);
737 if(event_base_set(b->eb->base,
738 &b->eb->slow_accept) != 0) {
739 /* we do not want to log here, because
740 * that would spam the logfiles.
741 * error: "event_base_set failed." */
743 if(event_add(&b->eb->slow_accept, &tv) != 0) {
744 /* we do not want to log here,
745 * error: "event_add failed." */
751 log_err("accept failed: %s", strerror(errno));
752 #else /* USE_WINSOCK */
753 if(WSAGetLastError() == WSAEINPROGRESS ||
754 WSAGetLastError() == WSAECONNRESET)
756 if(WSAGetLastError() == WSAEWOULDBLOCK) {
757 winsock_tcp_wouldblock(&c->ev->ev, EV_READ);
760 log_err("accept failed: %s", wsa_strerror(WSAGetLastError()));
762 log_addr(0, "remote address is", addr, *addrlen);
765 fd_set_nonblock(new_fd);
770 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp),
771 int ATTR_UNUSED(argi), long argl, long retvalue)
773 verbose(VERB_ALGO, "bio_cb %d, %s %s %s", oper,
774 (oper&BIO_CB_RETURN)?"return":"before",
775 (oper&BIO_CB_READ)?"read":((oper&BIO_CB_WRITE)?"write":"other"),
776 WSAGetLastError()==WSAEWOULDBLOCK?"wsawb":"");
777 /* on windows, check if previous operation caused EWOULDBLOCK */
778 if( (oper == (BIO_CB_READ|BIO_CB_RETURN) && argl == 0) ||
779 (oper == (BIO_CB_GETS|BIO_CB_RETURN) && argl == 0)) {
780 if(WSAGetLastError() == WSAEWOULDBLOCK)
781 winsock_tcp_wouldblock((struct event*)
782 BIO_get_callback_arg(b), EV_READ);
784 if( (oper == (BIO_CB_WRITE|BIO_CB_RETURN) && argl == 0) ||
785 (oper == (BIO_CB_PUTS|BIO_CB_RETURN) && argl == 0)) {
786 if(WSAGetLastError() == WSAEWOULDBLOCK)
787 winsock_tcp_wouldblock((struct event*)
788 BIO_get_callback_arg(b), EV_WRITE);
790 /* return original return value */
794 /** set win bio callbacks for nonblocking operations */
796 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl)
798 SSL* ssl = (SSL*)thessl;
799 /* set them both just in case, but usually they are the same BIO */
800 BIO_set_callback(SSL_get_rbio(ssl), &win_bio_cb);
801 BIO_set_callback_arg(SSL_get_rbio(ssl), (char*)&c->ev->ev);
802 BIO_set_callback(SSL_get_wbio(ssl), &win_bio_cb);
803 BIO_set_callback_arg(SSL_get_wbio(ssl), (char*)&c->ev->ev);
808 comm_point_tcp_accept_callback(int fd, short event, void* arg)
810 struct comm_point* c = (struct comm_point*)arg, *c_hdl;
812 log_assert(c->type == comm_tcp_accept);
813 if(!(event & EV_READ)) {
814 log_info("ignoring tcp accept event %d", (int)event);
817 comm_base_now(c->ev->base);
818 /* find free tcp handler. */
820 log_warn("accepted too many tcp, connections full");
823 /* accept incoming connection. */
825 log_assert(fd != -1);
826 new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.addr,
827 &c_hdl->repinfo.addrlen);
831 c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd);
834 comm_point_close(c_hdl);
837 c_hdl->ssl_shake_state = comm_ssl_shake_read;
839 comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl);
843 /* grab the tcp handler buffers */
844 c->tcp_free = c_hdl->tcp_free;
846 /* stop accepting incoming queries for now. */
847 comm_point_stop_listening(c);
849 /* addr is dropped. Not needed for tcp reply. */
850 setup_tcp_handler(c_hdl, new_fd);
853 /** Make tcp handler free for next assignment */
855 reclaim_tcp_handler(struct comm_point* c)
857 log_assert(c->type == comm_tcp);
860 SSL_shutdown(c->ssl);
867 c->tcp_free = c->tcp_parent->tcp_free;
868 c->tcp_parent->tcp_free = c;
870 /* re-enable listening on accept socket */
871 comm_point_start_listening(c->tcp_parent, -1, -1);
876 /** do the callback when writing is done */
878 tcp_callback_writer(struct comm_point* c)
880 log_assert(c->type == comm_tcp);
881 ldns_buffer_clear(c->buffer);
882 if(c->tcp_do_toggle_rw)
883 c->tcp_is_reading = 1;
884 c->tcp_byte_count = 0;
885 /* switch from listening(write) to listening(read) */
886 comm_point_stop_listening(c);
887 comm_point_start_listening(c, -1, -1);
890 /** do the callback when reading is done */
892 tcp_callback_reader(struct comm_point* c)
894 log_assert(c->type == comm_tcp || c->type == comm_local);
895 ldns_buffer_flip(c->buffer);
896 if(c->tcp_do_toggle_rw)
897 c->tcp_is_reading = 0;
898 c->tcp_byte_count = 0;
899 if(c->type == comm_tcp)
900 comm_point_stop_listening(c);
901 fptr_ok(fptr_whitelist_comm_point(c->callback));
902 if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) {
903 comm_point_start_listening(c, -1, TCP_QUERY_TIMEOUT);
907 /** continue ssl handshake */
910 ssl_handshake(struct comm_point* c)
913 if(c->ssl_shake_state == comm_ssl_shake_hs_read) {
914 /* read condition satisfied back to writing */
915 comm_point_listen_for_rw(c, 1, 1);
916 c->ssl_shake_state = comm_ssl_shake_none;
919 if(c->ssl_shake_state == comm_ssl_shake_hs_write) {
920 /* write condition satisfied, back to reading */
921 comm_point_listen_for_rw(c, 1, 0);
922 c->ssl_shake_state = comm_ssl_shake_none;
927 r = SSL_do_handshake(c->ssl);
929 int want = SSL_get_error(c->ssl, r);
930 if(want == SSL_ERROR_WANT_READ) {
931 if(c->ssl_shake_state == comm_ssl_shake_read)
933 c->ssl_shake_state = comm_ssl_shake_read;
934 comm_point_listen_for_rw(c, 1, 0);
936 } else if(want == SSL_ERROR_WANT_WRITE) {
937 if(c->ssl_shake_state == comm_ssl_shake_write)
939 c->ssl_shake_state = comm_ssl_shake_write;
940 comm_point_listen_for_rw(c, 0, 1);
943 return 0; /* closed */
944 } else if(want == SSL_ERROR_SYSCALL) {
945 /* SYSCALL and errno==0 means closed uncleanly */
947 log_err("SSL_handshake syscall: %s",
951 log_crypto_err("ssl handshake failed");
952 log_addr(1, "ssl handshake failed", &c->repinfo.addr,
957 /* this is where peer verification could take place */
958 log_addr(VERB_ALGO, "SSL DNS connection", &c->repinfo.addr,
961 /* setup listen rw correctly */
962 if(c->tcp_is_reading) {
963 if(c->ssl_shake_state != comm_ssl_shake_read)
964 comm_point_listen_for_rw(c, 1, 0);
966 comm_point_listen_for_rw(c, 1, 1);
968 c->ssl_shake_state = comm_ssl_shake_none;
971 #endif /* HAVE_SSL */
973 /** ssl read callback on TCP */
975 ssl_handle_read(struct comm_point* c)
979 if(c->ssl_shake_state != comm_ssl_shake_none) {
980 if(!ssl_handshake(c))
982 if(c->ssl_shake_state != comm_ssl_shake_none)
985 if(c->tcp_byte_count < sizeof(uint16_t)) {
986 /* read length bytes */
988 if((r=SSL_read(c->ssl, (void*)ldns_buffer_at(c->buffer,
989 c->tcp_byte_count), (int)(sizeof(uint16_t) -
990 c->tcp_byte_count))) <= 0) {
991 int want = SSL_get_error(c->ssl, r);
992 if(want == SSL_ERROR_ZERO_RETURN) {
993 return 0; /* shutdown, closed */
994 } else if(want == SSL_ERROR_WANT_READ) {
995 return 1; /* read more later */
996 } else if(want == SSL_ERROR_WANT_WRITE) {
997 c->ssl_shake_state = comm_ssl_shake_hs_write;
998 comm_point_listen_for_rw(c, 0, 1);
1000 } else if(want == SSL_ERROR_SYSCALL) {
1002 log_err("SSL_read syscall: %s",
1006 log_crypto_err("could not SSL_read");
1009 c->tcp_byte_count += r;
1010 if(c->tcp_byte_count != sizeof(uint16_t))
1012 if(ldns_buffer_read_u16_at(c->buffer, 0) >
1013 ldns_buffer_capacity(c->buffer)) {
1014 verbose(VERB_QUERY, "ssl: dropped larger than buffer");
1017 ldns_buffer_set_limit(c->buffer,
1018 ldns_buffer_read_u16_at(c->buffer, 0));
1019 if(ldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1020 verbose(VERB_QUERY, "ssl: dropped bogus too short.");
1023 verbose(VERB_ALGO, "Reading ssl tcp query of length %d",
1024 (int)ldns_buffer_limit(c->buffer));
1026 log_assert(ldns_buffer_remaining(c->buffer) > 0);
1028 r = SSL_read(c->ssl, (void*)ldns_buffer_current(c->buffer),
1029 (int)ldns_buffer_remaining(c->buffer));
1031 int want = SSL_get_error(c->ssl, r);
1032 if(want == SSL_ERROR_ZERO_RETURN) {
1033 return 0; /* shutdown, closed */
1034 } else if(want == SSL_ERROR_WANT_READ) {
1035 return 1; /* read more later */
1036 } else if(want == SSL_ERROR_WANT_WRITE) {
1037 c->ssl_shake_state = comm_ssl_shake_hs_write;
1038 comm_point_listen_for_rw(c, 0, 1);
1040 } else if(want == SSL_ERROR_SYSCALL) {
1042 log_err("SSL_read syscall: %s",
1046 log_crypto_err("could not SSL_read");
1049 ldns_buffer_skip(c->buffer, (ssize_t)r);
1050 if(ldns_buffer_remaining(c->buffer) <= 0) {
1051 tcp_callback_reader(c);
1057 #endif /* HAVE_SSL */
1060 /** ssl write callback on TCP */
1062 ssl_handle_write(struct comm_point* c)
1066 if(c->ssl_shake_state != comm_ssl_shake_none) {
1067 if(!ssl_handshake(c))
1069 if(c->ssl_shake_state != comm_ssl_shake_none)
1072 /* ignore return, if fails we may simply block */
1073 (void)SSL_set_mode(c->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
1074 if(c->tcp_byte_count < sizeof(uint16_t)) {
1075 uint16_t len = htons(ldns_buffer_limit(c->buffer));
1077 r = SSL_write(c->ssl,
1078 (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1079 (int)(sizeof(uint16_t)-c->tcp_byte_count));
1081 int want = SSL_get_error(c->ssl, r);
1082 if(want == SSL_ERROR_ZERO_RETURN) {
1083 return 0; /* closed */
1084 } else if(want == SSL_ERROR_WANT_READ) {
1085 c->ssl_shake_state = comm_ssl_shake_read;
1086 comm_point_listen_for_rw(c, 1, 0);
1087 return 1; /* wait for read condition */
1088 } else if(want == SSL_ERROR_WANT_WRITE) {
1089 return 1; /* write more later */
1090 } else if(want == SSL_ERROR_SYSCALL) {
1092 log_err("SSL_write syscall: %s",
1096 log_crypto_err("could not SSL_write");
1099 c->tcp_byte_count += r;
1100 if(c->tcp_byte_count < sizeof(uint16_t))
1102 ldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1104 if(ldns_buffer_remaining(c->buffer) == 0) {
1105 tcp_callback_writer(c);
1109 log_assert(ldns_buffer_remaining(c->buffer) > 0);
1111 r = SSL_write(c->ssl, (void*)ldns_buffer_current(c->buffer),
1112 (int)ldns_buffer_remaining(c->buffer));
1114 int want = SSL_get_error(c->ssl, r);
1115 if(want == SSL_ERROR_ZERO_RETURN) {
1116 return 0; /* closed */
1117 } else if(want == SSL_ERROR_WANT_READ) {
1118 c->ssl_shake_state = comm_ssl_shake_read;
1119 comm_point_listen_for_rw(c, 1, 0);
1120 return 1; /* wait for read condition */
1121 } else if(want == SSL_ERROR_WANT_WRITE) {
1122 return 1; /* write more later */
1123 } else if(want == SSL_ERROR_SYSCALL) {
1125 log_err("SSL_write syscall: %s",
1129 log_crypto_err("could not SSL_write");
1132 ldns_buffer_skip(c->buffer, (ssize_t)r);
1134 if(ldns_buffer_remaining(c->buffer) == 0) {
1135 tcp_callback_writer(c);
1141 #endif /* HAVE_SSL */
1144 /** handle ssl tcp connection with dns contents */
1146 ssl_handle_it(struct comm_point* c)
1148 if(c->tcp_is_reading)
1149 return ssl_handle_read(c);
1150 return ssl_handle_write(c);
1153 /** Handle tcp reading callback.
1154 * @param fd: file descriptor of socket.
1155 * @param c: comm point to read from into buffer.
1156 * @param short_ok: if true, very short packets are OK (for comm_local).
1157 * @return: 0 on error
1160 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok)
1163 log_assert(c->type == comm_tcp || c->type == comm_local);
1165 return ssl_handle_it(c);
1166 if(!c->tcp_is_reading)
1169 log_assert(fd != -1);
1170 if(c->tcp_byte_count < sizeof(uint16_t)) {
1171 /* read length bytes */
1172 r = recv(fd,(void*)ldns_buffer_at(c->buffer,c->tcp_byte_count),
1173 sizeof(uint16_t)-c->tcp_byte_count, 0);
1178 if(errno == EINTR || errno == EAGAIN)
1181 if(errno == ECONNRESET && verbosity < 2)
1182 return 0; /* silence reset by peer */
1184 log_err("read (in tcp s): %s", strerror(errno));
1185 #else /* USE_WINSOCK */
1186 if(WSAGetLastError() == WSAECONNRESET)
1188 if(WSAGetLastError() == WSAEINPROGRESS)
1190 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1191 winsock_tcp_wouldblock(&c->ev->ev, EV_READ);
1194 log_err("read (in tcp s): %s",
1195 wsa_strerror(WSAGetLastError()));
1197 log_addr(0, "remote address is", &c->repinfo.addr,
1198 c->repinfo.addrlen);
1201 c->tcp_byte_count += r;
1202 if(c->tcp_byte_count != sizeof(uint16_t))
1204 if(ldns_buffer_read_u16_at(c->buffer, 0) >
1205 ldns_buffer_capacity(c->buffer)) {
1206 verbose(VERB_QUERY, "tcp: dropped larger than buffer");
1209 ldns_buffer_set_limit(c->buffer,
1210 ldns_buffer_read_u16_at(c->buffer, 0));
1212 ldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1213 verbose(VERB_QUERY, "tcp: dropped bogus too short.");
1216 verbose(VERB_ALGO, "Reading tcp query of length %d",
1217 (int)ldns_buffer_limit(c->buffer));
1220 log_assert(ldns_buffer_remaining(c->buffer) > 0);
1221 r = recv(fd, (void*)ldns_buffer_current(c->buffer),
1222 ldns_buffer_remaining(c->buffer), 0);
1225 } else if(r == -1) {
1227 if(errno == EINTR || errno == EAGAIN)
1229 log_err("read (in tcp r): %s", strerror(errno));
1230 #else /* USE_WINSOCK */
1231 if(WSAGetLastError() == WSAECONNRESET)
1233 if(WSAGetLastError() == WSAEINPROGRESS)
1235 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1236 winsock_tcp_wouldblock(&c->ev->ev, EV_READ);
1239 log_err("read (in tcp r): %s",
1240 wsa_strerror(WSAGetLastError()));
1242 log_addr(0, "remote address is", &c->repinfo.addr,
1243 c->repinfo.addrlen);
1246 ldns_buffer_skip(c->buffer, r);
1247 if(ldns_buffer_remaining(c->buffer) <= 0) {
1248 tcp_callback_reader(c);
1254 * Handle tcp writing callback.
1255 * @param fd: file descriptor of socket.
1256 * @param c: comm point to write buffer out of.
1257 * @return: 0 on error
1260 comm_point_tcp_handle_write(int fd, struct comm_point* c)
1263 log_assert(c->type == comm_tcp);
1264 if(c->tcp_is_reading && !c->ssl)
1266 log_assert(fd != -1);
1267 if(c->tcp_byte_count == 0 && c->tcp_check_nb_connect) {
1268 /* check for pending error from nonblocking connect */
1269 /* from Stevens, unix network programming, vol1, 3rd ed, p450*/
1271 socklen_t len = (socklen_t)sizeof(error);
1272 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
1275 error = errno; /* on solaris errno is error */
1276 #else /* USE_WINSOCK */
1277 error = WSAGetLastError();
1281 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1282 if(error == EINPROGRESS || error == EWOULDBLOCK)
1283 return 1; /* try again later */
1286 if(error != 0 && verbosity < 2)
1287 return 0; /* silence lots of chatter in the logs */
1288 else if(error != 0) {
1289 log_err("tcp connect: %s", strerror(error));
1290 #else /* USE_WINSOCK */
1292 if(error == WSAEINPROGRESS)
1294 else if(error == WSAEWOULDBLOCK) {
1295 winsock_tcp_wouldblock(&c->ev->ev, EV_WRITE);
1297 } else if(error != 0 && verbosity < 2)
1299 else if(error != 0) {
1300 log_err("tcp connect: %s", wsa_strerror(error));
1301 #endif /* USE_WINSOCK */
1302 log_addr(0, "remote address is", &c->repinfo.addr,
1303 c->repinfo.addrlen);
1308 return ssl_handle_it(c);
1310 if(c->tcp_byte_count < sizeof(uint16_t)) {
1311 uint16_t len = htons(ldns_buffer_limit(c->buffer));
1313 struct iovec iov[2];
1314 iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
1315 iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
1316 iov[1].iov_base = ldns_buffer_begin(c->buffer);
1317 iov[1].iov_len = ldns_buffer_limit(c->buffer);
1318 log_assert(iov[0].iov_len > 0);
1319 log_assert(iov[1].iov_len > 0);
1320 r = writev(fd, iov, 2);
1321 #else /* HAVE_WRITEV */
1322 r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1323 sizeof(uint16_t)-c->tcp_byte_count, 0);
1324 #endif /* HAVE_WRITEV */
1328 if(errno == EPIPE && verbosity < 2)
1329 return 0; /* silence 'broken pipe' */
1331 if(errno == EINTR || errno == EAGAIN)
1333 log_err("tcp writev: %s", strerror(errno));
1335 if(WSAGetLastError() == WSAENOTCONN)
1337 if(WSAGetLastError() == WSAEINPROGRESS)
1339 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1340 winsock_tcp_wouldblock(&c->ev->ev, EV_WRITE);
1343 log_err("tcp send s: %s",
1344 wsa_strerror(WSAGetLastError()));
1346 log_addr(0, "remote address is", &c->repinfo.addr,
1347 c->repinfo.addrlen);
1350 c->tcp_byte_count += r;
1351 if(c->tcp_byte_count < sizeof(uint16_t))
1353 ldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1355 if(ldns_buffer_remaining(c->buffer) == 0) {
1356 tcp_callback_writer(c);
1360 log_assert(ldns_buffer_remaining(c->buffer) > 0);
1361 r = send(fd, (void*)ldns_buffer_current(c->buffer),
1362 ldns_buffer_remaining(c->buffer), 0);
1365 if(errno == EINTR || errno == EAGAIN)
1367 log_err("tcp send r: %s", strerror(errno));
1369 if(WSAGetLastError() == WSAEINPROGRESS)
1371 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1372 winsock_tcp_wouldblock(&c->ev->ev, EV_WRITE);
1375 log_err("tcp send r: %s",
1376 wsa_strerror(WSAGetLastError()));
1378 log_addr(0, "remote address is", &c->repinfo.addr,
1379 c->repinfo.addrlen);
1382 ldns_buffer_skip(c->buffer, r);
1384 if(ldns_buffer_remaining(c->buffer) == 0) {
1385 tcp_callback_writer(c);
1392 comm_point_tcp_handle_callback(int fd, short event, void* arg)
1394 struct comm_point* c = (struct comm_point*)arg;
1395 log_assert(c->type == comm_tcp);
1396 comm_base_now(c->ev->base);
1399 if(!comm_point_tcp_handle_read(fd, c, 0)) {
1400 reclaim_tcp_handler(c);
1401 if(!c->tcp_do_close) {
1402 fptr_ok(fptr_whitelist_comm_point(
1404 (void)(*c->callback)(c, c->cb_arg,
1405 NETEVENT_CLOSED, NULL);
1410 if(event&EV_WRITE) {
1411 if(!comm_point_tcp_handle_write(fd, c)) {
1412 reclaim_tcp_handler(c);
1413 if(!c->tcp_do_close) {
1414 fptr_ok(fptr_whitelist_comm_point(
1416 (void)(*c->callback)(c, c->cb_arg,
1417 NETEVENT_CLOSED, NULL);
1422 if(event&EV_TIMEOUT) {
1423 verbose(VERB_QUERY, "tcp took too long, dropped");
1424 reclaim_tcp_handler(c);
1425 if(!c->tcp_do_close) {
1426 fptr_ok(fptr_whitelist_comm_point(c->callback));
1427 (void)(*c->callback)(c, c->cb_arg,
1428 NETEVENT_TIMEOUT, NULL);
1432 log_err("Ignored event %d for tcphdl.", event);
1435 void comm_point_local_handle_callback(int fd, short event, void* arg)
1437 struct comm_point* c = (struct comm_point*)arg;
1438 log_assert(c->type == comm_local);
1439 comm_base_now(c->ev->base);
1442 if(!comm_point_tcp_handle_read(fd, c, 1)) {
1443 fptr_ok(fptr_whitelist_comm_point(c->callback));
1444 (void)(*c->callback)(c, c->cb_arg, NETEVENT_CLOSED,
1449 log_err("Ignored event %d for localhdl.", event);
1452 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
1453 short event, void* arg)
1455 struct comm_point* c = (struct comm_point*)arg;
1456 int err = NETEVENT_NOERROR;
1457 log_assert(c->type == comm_raw);
1458 comm_base_now(c->ev->base);
1460 if(event&EV_TIMEOUT)
1461 err = NETEVENT_TIMEOUT;
1462 fptr_ok(fptr_whitelist_comm_point_raw(c->callback));
1463 (void)(*c->callback)(c, c->cb_arg, err, NULL);
1467 comm_point_create_udp(struct comm_base *base, int fd, ldns_buffer* buffer,
1468 comm_point_callback_t* callback, void* callback_arg)
1470 struct comm_point* c = (struct comm_point*)calloc(1,
1471 sizeof(struct comm_point));
1475 c->ev = (struct internal_event*)calloc(1,
1476 sizeof(struct internal_event));
1485 c->tcp_is_reading = 0;
1486 c->tcp_byte_count = 0;
1487 c->tcp_parent = NULL;
1488 c->max_tcp_count = 0;
1489 c->tcp_handlers = NULL;
1492 c->tcp_do_close = 0;
1493 c->do_not_close = 0;
1494 c->tcp_do_toggle_rw = 0;
1495 c->tcp_check_nb_connect = 0;
1497 c->callback = callback;
1498 c->cb_arg = callback_arg;
1499 evbits = EV_READ | EV_PERSIST;
1500 /* libevent stuff */
1501 event_set(&c->ev->ev, c->fd, evbits, comm_point_udp_callback, c);
1502 if(event_base_set(base->eb->base, &c->ev->ev) != 0) {
1503 log_err("could not baseset udp event");
1504 comm_point_delete(c);
1507 if(fd!=-1 && event_add(&c->ev->ev, c->timeout) != 0 ) {
1508 log_err("could not add udp event");
1509 comm_point_delete(c);
1516 comm_point_create_udp_ancil(struct comm_base *base, int fd,
1517 ldns_buffer* buffer,
1518 comm_point_callback_t* callback, void* callback_arg)
1520 struct comm_point* c = (struct comm_point*)calloc(1,
1521 sizeof(struct comm_point));
1525 c->ev = (struct internal_event*)calloc(1,
1526 sizeof(struct internal_event));
1535 c->tcp_is_reading = 0;
1536 c->tcp_byte_count = 0;
1537 c->tcp_parent = NULL;
1538 c->max_tcp_count = 0;
1539 c->tcp_handlers = NULL;
1542 c->tcp_do_close = 0;
1543 c->do_not_close = 0;
1545 c->tcp_do_toggle_rw = 0;
1546 c->tcp_check_nb_connect = 0;
1547 c->callback = callback;
1548 c->cb_arg = callback_arg;
1549 evbits = EV_READ | EV_PERSIST;
1550 /* libevent stuff */
1551 event_set(&c->ev->ev, c->fd, evbits, comm_point_udp_ancil_callback, c);
1552 if(event_base_set(base->eb->base, &c->ev->ev) != 0) {
1553 log_err("could not baseset udp event");
1554 comm_point_delete(c);
1557 if(fd!=-1 && event_add(&c->ev->ev, c->timeout) != 0 ) {
1558 log_err("could not add udp event");
1559 comm_point_delete(c);
1565 static struct comm_point*
1566 comm_point_create_tcp_handler(struct comm_base *base,
1567 struct comm_point* parent, size_t bufsize,
1568 comm_point_callback_t* callback, void* callback_arg)
1570 struct comm_point* c = (struct comm_point*)calloc(1,
1571 sizeof(struct comm_point));
1575 c->ev = (struct internal_event*)calloc(1,
1576 sizeof(struct internal_event));
1583 c->buffer = ldns_buffer_new(bufsize);
1589 c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
1591 ldns_buffer_free(c->buffer);
1596 c->tcp_is_reading = 0;
1597 c->tcp_byte_count = 0;
1598 c->tcp_parent = parent;
1599 c->max_tcp_count = 0;
1600 c->tcp_handlers = NULL;
1603 c->tcp_do_close = 0;
1604 c->do_not_close = 0;
1605 c->tcp_do_toggle_rw = 1;
1606 c->tcp_check_nb_connect = 0;
1608 c->callback = callback;
1609 c->cb_arg = callback_arg;
1610 /* add to parent free list */
1611 c->tcp_free = parent->tcp_free;
1612 parent->tcp_free = c;
1613 /* libevent stuff */
1614 evbits = EV_PERSIST | EV_READ | EV_TIMEOUT;
1615 event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_handle_callback, c);
1616 if(event_base_set(base->eb->base, &c->ev->ev) != 0)
1618 log_err("could not basetset tcphdl event");
1619 parent->tcp_free = c->tcp_free;
1628 comm_point_create_tcp(struct comm_base *base, int fd, int num, size_t bufsize,
1629 comm_point_callback_t* callback, void* callback_arg)
1631 struct comm_point* c = (struct comm_point*)calloc(1,
1632 sizeof(struct comm_point));
1635 /* first allocate the TCP accept listener */
1638 c->ev = (struct internal_event*)calloc(1,
1639 sizeof(struct internal_event));
1648 c->tcp_is_reading = 0;
1649 c->tcp_byte_count = 0;
1650 c->tcp_parent = NULL;
1651 c->max_tcp_count = num;
1652 c->tcp_handlers = (struct comm_point**)calloc((size_t)num,
1653 sizeof(struct comm_point*));
1654 if(!c->tcp_handlers) {
1660 c->type = comm_tcp_accept;
1661 c->tcp_do_close = 0;
1662 c->do_not_close = 0;
1663 c->tcp_do_toggle_rw = 0;
1664 c->tcp_check_nb_connect = 0;
1667 evbits = EV_READ | EV_PERSIST;
1668 /* libevent stuff */
1669 event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_accept_callback, c);
1670 if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
1671 event_add(&c->ev->ev, c->timeout) != 0 )
1673 log_err("could not add tcpacc event");
1674 comm_point_delete(c);
1678 /* now prealloc the tcp handlers */
1679 for(i=0; i<num; i++) {
1680 c->tcp_handlers[i] = comm_point_create_tcp_handler(base,
1681 c, bufsize, callback, callback_arg);
1682 if(!c->tcp_handlers[i]) {
1683 comm_point_delete(c);
1692 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize,
1693 comm_point_callback_t* callback, void* callback_arg)
1695 struct comm_point* c = (struct comm_point*)calloc(1,
1696 sizeof(struct comm_point));
1700 c->ev = (struct internal_event*)calloc(1,
1701 sizeof(struct internal_event));
1708 c->buffer = ldns_buffer_new(bufsize);
1715 c->tcp_is_reading = 0;
1716 c->tcp_byte_count = 0;
1717 c->tcp_parent = NULL;
1718 c->max_tcp_count = 0;
1719 c->tcp_handlers = NULL;
1722 c->tcp_do_close = 0;
1723 c->do_not_close = 0;
1724 c->tcp_do_toggle_rw = 1;
1725 c->tcp_check_nb_connect = 1;
1727 c->callback = callback;
1728 c->cb_arg = callback_arg;
1729 evbits = EV_PERSIST | EV_WRITE;
1730 event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_handle_callback, c);
1731 if(event_base_set(base->eb->base, &c->ev->ev) != 0)
1733 log_err("could not basetset tcpout event");
1734 ldns_buffer_free(c->buffer);
1744 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize,
1745 comm_point_callback_t* callback, void* callback_arg)
1747 struct comm_point* c = (struct comm_point*)calloc(1,
1748 sizeof(struct comm_point));
1752 c->ev = (struct internal_event*)calloc(1,
1753 sizeof(struct internal_event));
1760 c->buffer = ldns_buffer_new(bufsize);
1767 c->tcp_is_reading = 1;
1768 c->tcp_byte_count = 0;
1769 c->tcp_parent = NULL;
1770 c->max_tcp_count = 0;
1771 c->tcp_handlers = NULL;
1773 c->type = comm_local;
1774 c->tcp_do_close = 0;
1775 c->do_not_close = 1;
1776 c->tcp_do_toggle_rw = 0;
1777 c->tcp_check_nb_connect = 0;
1778 c->callback = callback;
1779 c->cb_arg = callback_arg;
1780 /* libevent stuff */
1781 evbits = EV_PERSIST | EV_READ;
1782 event_set(&c->ev->ev, c->fd, evbits, comm_point_local_handle_callback,
1784 if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
1785 event_add(&c->ev->ev, c->timeout) != 0 )
1787 log_err("could not add localhdl event");
1796 comm_point_create_raw(struct comm_base* base, int fd, int writing,
1797 comm_point_callback_t* callback, void* callback_arg)
1799 struct comm_point* c = (struct comm_point*)calloc(1,
1800 sizeof(struct comm_point));
1804 c->ev = (struct internal_event*)calloc(1,
1805 sizeof(struct internal_event));
1814 c->tcp_is_reading = 0;
1815 c->tcp_byte_count = 0;
1816 c->tcp_parent = NULL;
1817 c->max_tcp_count = 0;
1818 c->tcp_handlers = NULL;
1821 c->tcp_do_close = 0;
1822 c->do_not_close = 1;
1823 c->tcp_do_toggle_rw = 0;
1824 c->tcp_check_nb_connect = 0;
1825 c->callback = callback;
1826 c->cb_arg = callback_arg;
1827 /* libevent stuff */
1829 evbits = EV_PERSIST | EV_WRITE;
1830 else evbits = EV_PERSIST | EV_READ;
1831 event_set(&c->ev->ev, c->fd, evbits, comm_point_raw_handle_callback,
1833 if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
1834 event_add(&c->ev->ev, c->timeout) != 0 )
1836 log_err("could not add rawhdl event");
1845 comm_point_close(struct comm_point* c)
1850 if(event_del(&c->ev->ev) != 0) {
1851 log_err("could not event_del on close");
1853 /* close fd after removing from event lists, or epoll.. is messed up */
1854 if(c->fd != -1 && !c->do_not_close) {
1855 verbose(VERB_ALGO, "close fd %d", c->fd);
1866 comm_point_delete(struct comm_point* c)
1870 if(c->type == comm_tcp && c->ssl) {
1872 SSL_shutdown(c->ssl);
1876 comm_point_close(c);
1877 if(c->tcp_handlers) {
1879 for(i=0; i<c->max_tcp_count; i++)
1880 comm_point_delete(c->tcp_handlers[i]);
1881 free(c->tcp_handlers);
1884 if(c->type == comm_tcp || c->type == comm_local)
1885 ldns_buffer_free(c->buffer);
1891 comm_point_send_reply(struct comm_reply *repinfo)
1893 log_assert(repinfo && repinfo->c);
1894 if(repinfo->c->type == comm_udp) {
1895 if(repinfo->srctype)
1896 comm_point_send_udp_msg_if(repinfo->c,
1897 repinfo->c->buffer, (struct sockaddr*)&repinfo->addr,
1898 repinfo->addrlen, repinfo);
1900 comm_point_send_udp_msg(repinfo->c, repinfo->c->buffer,
1901 (struct sockaddr*)&repinfo->addr, repinfo->addrlen);
1903 comm_point_start_listening(repinfo->c, -1, TCP_QUERY_TIMEOUT);
1908 comm_point_drop_reply(struct comm_reply* repinfo)
1912 log_assert(repinfo && repinfo->c);
1913 log_assert(repinfo->c->type != comm_tcp_accept);
1914 if(repinfo->c->type == comm_udp)
1916 reclaim_tcp_handler(repinfo->c);
1920 comm_point_stop_listening(struct comm_point* c)
1922 verbose(VERB_ALGO, "comm point stop listening %d", c->fd);
1923 if(event_del(&c->ev->ev) != 0) {
1924 log_err("event_del error to stoplisten");
1929 comm_point_start_listening(struct comm_point* c, int newfd, int sec)
1931 verbose(VERB_ALGO, "comm point start listening %d",
1932 c->fd==-1?newfd:c->fd);
1933 if(c->type == comm_tcp_accept && !c->tcp_free) {
1934 /* no use to start listening no free slots. */
1937 if(sec != -1 && sec != 0) {
1939 c->timeout = (struct timeval*)malloc(sizeof(
1942 log_err("cpsl: malloc failed. No net read.");
1946 c->ev->ev.ev_events |= EV_TIMEOUT;
1947 #ifndef S_SPLINT_S /* splint fails on struct timeval. */
1948 c->timeout->tv_sec = sec;
1949 c->timeout->tv_usec = 0;
1950 #endif /* S_SPLINT_S */
1952 if(c->type == comm_tcp) {
1953 c->ev->ev.ev_events &= ~(EV_READ|EV_WRITE);
1954 if(c->tcp_is_reading)
1955 c->ev->ev.ev_events |= EV_READ;
1956 else c->ev->ev.ev_events |= EV_WRITE;
1967 c->ev->ev.ev_fd = c->fd;
1969 if(event_add(&c->ev->ev, sec==0?NULL:c->timeout) != 0) {
1970 log_err("event_add failed. in cpsl.");
1974 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr)
1976 verbose(VERB_ALGO, "comm point listen_for_rw %d %d", c->fd, wr);
1977 if(event_del(&c->ev->ev) != 0) {
1978 log_err("event_del error to cplf");
1980 c->ev->ev.ev_events &= ~(EV_READ|EV_WRITE);
1981 if(rd) c->ev->ev.ev_events |= EV_READ;
1982 if(wr) c->ev->ev.ev_events |= EV_WRITE;
1983 if(event_add(&c->ev->ev, c->timeout) != 0) {
1984 log_err("event_add failed. in cplf.");
1988 size_t comm_point_get_mem(struct comm_point* c)
1993 s = sizeof(*c) + sizeof(*c->ev);
1995 s += sizeof(*c->timeout);
1996 if(c->type == comm_tcp || c->type == comm_local)
1997 s += sizeof(*c->buffer) + ldns_buffer_capacity(c->buffer);
1998 if(c->type == comm_tcp_accept) {
2000 for(i=0; i<c->max_tcp_count; i++)
2001 s += comm_point_get_mem(c->tcp_handlers[i]);
2007 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
2009 struct comm_timer *tm = (struct comm_timer*)calloc(1,
2010 sizeof(struct comm_timer));
2013 tm->ev_timer = (struct internal_timer*)calloc(1,
2014 sizeof(struct internal_timer));
2016 log_err("malloc failed");
2020 tm->ev_timer->base = base;
2022 tm->cb_arg = cb_arg;
2023 event_set(&tm->ev_timer->ev, -1, EV_TIMEOUT,
2024 comm_timer_callback, tm);
2025 if(event_base_set(base->eb->base, &tm->ev_timer->ev) != 0) {
2026 log_err("timer_create: event_base_set failed.");
2035 comm_timer_disable(struct comm_timer* timer)
2039 evtimer_del(&timer->ev_timer->ev);
2040 timer->ev_timer->enabled = 0;
2044 comm_timer_set(struct comm_timer* timer, struct timeval* tv)
2047 if(timer->ev_timer->enabled)
2048 comm_timer_disable(timer);
2049 event_set(&timer->ev_timer->ev, -1, EV_TIMEOUT,
2050 comm_timer_callback, timer);
2051 if(event_base_set(timer->ev_timer->base->eb->base,
2052 &timer->ev_timer->ev) != 0)
2053 log_err("comm_timer_set: set_base failed.");
2054 if(evtimer_add(&timer->ev_timer->ev, tv) != 0)
2055 log_err("comm_timer_set: evtimer_add failed.");
2056 timer->ev_timer->enabled = 1;
2060 comm_timer_delete(struct comm_timer* timer)
2064 comm_timer_disable(timer);
2065 free(timer->ev_timer);
2070 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
2072 struct comm_timer* tm = (struct comm_timer*)arg;
2073 if(!(event&EV_TIMEOUT))
2075 comm_base_now(tm->ev_timer->base);
2076 tm->ev_timer->enabled = 0;
2077 fptr_ok(fptr_whitelist_comm_timer(tm->callback));
2078 (*tm->callback)(tm->cb_arg);
2082 comm_timer_is_set(struct comm_timer* timer)
2084 return (int)timer->ev_timer->enabled;
2088 comm_timer_get_mem(struct comm_timer* timer)
2090 return sizeof(*timer) + sizeof(struct internal_timer);
2094 comm_signal_create(struct comm_base* base,
2095 void (*callback)(int, void*), void* cb_arg)
2097 struct comm_signal* com = (struct comm_signal*)malloc(
2098 sizeof(struct comm_signal));
2100 log_err("malloc failed");
2104 com->callback = callback;
2105 com->cb_arg = cb_arg;
2106 com->ev_signal = NULL;
2111 comm_signal_callback(int sig, short event, void* arg)
2113 struct comm_signal* comsig = (struct comm_signal*)arg;
2114 if(!(event & EV_SIGNAL))
2116 comm_base_now(comsig->base);
2117 fptr_ok(fptr_whitelist_comm_signal(comsig->callback));
2118 (*comsig->callback)(sig, comsig->cb_arg);
2122 comm_signal_bind(struct comm_signal* comsig, int sig)
2124 struct internal_signal* entry = (struct internal_signal*)calloc(1,
2125 sizeof(struct internal_signal));
2127 log_err("malloc failed");
2131 /* add signal event */
2132 signal_set(&entry->ev, sig, comm_signal_callback, comsig);
2133 if(event_base_set(comsig->base->eb->base, &entry->ev) != 0) {
2134 log_err("Could not set signal base");
2138 if(signal_add(&entry->ev, NULL) != 0) {
2139 log_err("Could not add signal handler");
2143 /* link into list */
2144 entry->next = comsig->ev_signal;
2145 comsig->ev_signal = entry;
2150 comm_signal_delete(struct comm_signal* comsig)
2152 struct internal_signal* p, *np;
2155 p=comsig->ev_signal;