]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/unbound/util/netevent.c
MFV: file 5.33
[FreeBSD/FreeBSD.git] / contrib / unbound / util / netevent.c
1 /*
2  * util/netevent.c - event notification
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  * 
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.
18  * 
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.
22  * 
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.
34  */
35
36 /**
37  * \file
38  *
39  * This file contains event notification functions.
40  */
41 #include "config.h"
42 #include "util/netevent.h"
43 #include "util/ub_event.h"
44 #include "util/log.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>
54 #endif
55 #ifdef HAVE_OPENSSL_ERR_H
56 #include <openssl/err.h>
57 #endif
58
59 /* -------- Start of local definitions -------- */
60 /** if CMSG_ALIGN is not defined on this platform, a workaround */
61 #ifndef CMSG_ALIGN
62 #  ifdef __CMSG_ALIGN
63 #    define CMSG_ALIGN(n) __CMSG_ALIGN(n)
64 #  elif defined(CMSG_DATA_ALIGN)
65 #    define CMSG_ALIGN _CMSG_DATA_ALIGN
66 #  else
67 #    define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
68 #  endif
69 #endif
70
71 /** if CMSG_LEN is not defined on this platform, a workaround */
72 #ifndef CMSG_LEN
73 #  define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
74 #endif
75
76 /** if CMSG_SPACE is not defined on this platform, a workaround */
77 #ifndef CMSG_SPACE
78 #  ifdef _CMSG_HDR_ALIGN
79 #    define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)))
80 #  else
81 #    define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr)))
82 #  endif
83 #endif
84
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
89
90 #ifndef NONBLOCKING_IS_BROKEN
91 /** number of UDP reads to perform per read indication from select */
92 #define NUM_UDP_PER_SELECT 100
93 #else
94 #define NUM_UDP_PER_SELECT 1
95 #endif
96
97 /**
98  * The internal event structure for keeping ub_event info for the event.
99  * Possibly other structures (list, tree) this is part of.
100  */
101 struct internal_event {
102         /** the comm base */
103         struct comm_base* base;
104         /** ub_event event type */
105         struct ub_event* ev;
106 };
107
108 /**
109  * Internal base structure, so that every thread has its own events.
110  */
111 struct internal_base {
112         /** ub_event event_base type. */
113         struct ub_event_base* base;
114         /** seconds time pointer points here */
115         time_t secs;
116         /** timeval with current time */
117         struct timeval now;
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;
122 };
123
124 /**
125  * Internal timer structure, to store timer event in.
126  */
127 struct internal_timer {
128         /** the super struct from which derived */
129         struct comm_timer super;
130         /** the comm base */
131         struct comm_base* base;
132         /** ub_event event type */
133         struct ub_event* ev;
134         /** is timer enabled */
135         uint8_t enabled;
136 };
137
138 /**
139  * Internal signal structure, to store signal event in.
140  */
141 struct internal_signal {
142         /** ub_event event type */
143         struct ub_event* ev;
144         /** next in signal list */
145         struct internal_signal* next;
146 };
147
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);
152
153 /* -------- End of local definitions -------- */
154
155 struct comm_base* 
156 comm_base_create(int sigs)
157 {
158         struct comm_base* b = (struct comm_base*)calloc(1,
159                 sizeof(struct comm_base));
160         const char *evnm="event", *evsys="", *evmethod="";
161
162         if(!b)
163                 return NULL;
164         b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
165         if(!b->eb) {
166                 free(b);
167                 return NULL;
168         }
169         b->eb->base = ub_default_event_base(sigs, &b->eb->secs, &b->eb->now);
170         if(!b->eb->base) {
171                 free(b->eb);
172                 free(b);
173                 return NULL;
174         }
175         ub_comm_base_now(b);
176         ub_get_event_sys(b->eb->base, &evnm, &evsys, &evmethod);
177         verbose(VERB_ALGO, "%s %s user %s method.", evnm, evsys, evmethod);
178         return b;
179 }
180
181 struct comm_base*
182 comm_base_create_event(struct ub_event_base* base)
183 {
184         struct comm_base* b = (struct comm_base*)calloc(1,
185                 sizeof(struct comm_base));
186         if(!b)
187                 return NULL;
188         b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
189         if(!b->eb) {
190                 free(b);
191                 return NULL;
192         }
193         b->eb->base = base;
194         ub_comm_base_now(b);
195         return b;
196 }
197
198 void 
199 comm_base_delete(struct comm_base* b)
200 {
201         if(!b)
202                 return;
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");
206                 }
207                 ub_event_free(b->eb->slow_accept);
208         }
209         ub_event_base_free(b->eb->base);
210         b->eb->base = NULL;
211         free(b->eb);
212         free(b);
213 }
214
215 void 
216 comm_base_delete_no_base(struct comm_base* b)
217 {
218         if(!b)
219                 return;
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");
223                 }
224                 ub_event_free(b->eb->slow_accept);
225         }
226         b->eb->base = NULL;
227         free(b->eb);
228         free(b);
229 }
230
231 void 
232 comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv)
233 {
234         *tt = &b->eb->secs;
235         *tv = &b->eb->now;
236 }
237
238 void 
239 comm_base_dispatch(struct comm_base* b)
240 {
241         int retval;
242         retval = ub_event_base_dispatch(b->eb->base);
243         if(retval < 0) {
244                 fatal_exit("event_dispatch returned error %d, "
245                         "errno is %s", retval, strerror(errno));
246         }
247 }
248
249 void comm_base_exit(struct comm_base* b)
250 {
251         if(ub_event_base_loopexit(b->eb->base) != 0) {
252                 log_err("Could not loopexit");
253         }
254 }
255
256 void comm_base_set_slow_accept_handlers(struct comm_base* b,
257         void (*stop_acc)(void*), void (*start_acc)(void*), void* arg)
258 {
259         b->stop_accept = stop_acc;
260         b->start_accept = start_acc;
261         b->cb_arg = arg;
262 }
263
264 struct ub_event_base* comm_base_internal(struct comm_base* b)
265 {
266         return b->eb->base;
267 }
268
269 /** see if errno for udp has to be logged or not uses globals */
270 static int
271 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
272 {
273         /* do not log transient errors (unless high verbosity) */
274 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN)
275         switch(errno) {
276 #  ifdef ENETUNREACH
277                 case ENETUNREACH:
278 #  endif
279 #  ifdef EHOSTDOWN
280                 case EHOSTDOWN:
281 #  endif
282 #  ifdef EHOSTUNREACH
283                 case EHOSTUNREACH:
284 #  endif
285 #  ifdef ENETDOWN
286                 case ENETDOWN:
287 #  endif
288                         if(verbosity < VERB_ALGO)
289                                 return 0;
290                 default:
291                         break;
292         }
293 #endif
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)
300 #  endif
301                 ) && verbosity < VERB_DETAIL)
302                 return 0;
303 #  ifdef EADDRINUSE
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)
307                 return 0;
308 #  endif
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)
314                 return 0;
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)
320                 return 0;
321         return 1;
322 }
323
324 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
325 {
326         return udp_send_errno_needs_log(addr, addrlen);
327 }
328
329 /* send a UDP reply */
330 int
331 comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
332         struct sockaddr* addr, socklen_t addrlen) 
333 {
334         ssize_t sent;
335         log_assert(c->fd != -1);
336 #ifdef UNBOUND_DEBUG
337         if(sldns_buffer_remaining(packet) == 0)
338                 log_err("error: send empty UDP packet");
339 #endif
340         log_assert(addr && addrlen > 0);
341         sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), 
342                 sldns_buffer_remaining(packet), 0,
343                 addr, addrlen);
344         if(sent == -1) {
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. */
348 #ifndef USE_WINSOCK
349                 if(errno == EAGAIN || 
350 #  ifdef EWOULDBLOCK
351                         errno == EWOULDBLOCK ||
352 #  endif
353                         errno == ENOBUFS) {
354 #else
355                 if(WSAGetLastError() == WSAEINPROGRESS ||
356                         WSAGetLastError() == WSAENOBUFS ||
357                         WSAGetLastError() == WSAEWOULDBLOCK) {
358 #endif
359                         int e;
360                         fd_set_block(c->fd);
361                         sent = sendto(c->fd, (void*)sldns_buffer_begin(packet), 
362                                 sldns_buffer_remaining(packet), 0,
363                                 addr, addrlen);
364                         e = errno;
365                         fd_set_nonblock(c->fd);
366                         errno = e;
367                 }
368         }
369         if(sent == -1) {
370                 if(!udp_send_errno_needs_log(addr, addrlen))
371                         return 0;
372 #ifndef USE_WINSOCK
373                 verbose(VERB_OPS, "sendto failed: %s", strerror(errno));
374 #else
375                 verbose(VERB_OPS, "sendto failed: %s", 
376                         wsa_strerror(WSAGetLastError()));
377 #endif
378                 log_addr(VERB_OPS, "remote address is", 
379                         (struct sockaddr_storage*)addr, addrlen);
380                 return 0;
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));
384                 return 0;
385         }
386         return 1;
387 }
388
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)
392 {
393         if(r->srctype != 4 && r->srctype != 6) {
394                 log_info("%s: unknown srctype %d", str, r->srctype);
395                 return;
396         }
397         if(r->srctype == 6) {
398                 char buf[1024];
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));
402                 }
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) {
406 #ifdef IP_PKTINFO
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));
411                 }
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));
417                 }
418                 buf2[sizeof(buf2)-1]=0;
419 #else
420                 buf2[0]=0;
421 #endif
422                 log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex,
423                         buf1, buf2);
424 #elif defined(IP_RECVDSTADDR)
425                 char buf1[1024];
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));
429                 }
430                 buf1[sizeof(buf1)-1]=0;
431                 log_info("%s: %s", str, buf1);
432 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */
433         }
434 }
435 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */
436
437 /** send a UDP reply over specified interface*/
438 static int
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) 
441 {
442 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG)
443         ssize_t sent;
444         struct msghdr msg;
445         struct iovec iov[1];
446         char control[256];
447 #ifndef S_SPLINT_S
448         struct cmsghdr *cmsg;
449 #endif /* S_SPLINT_S */
450
451         log_assert(c->fd != -1);
452 #ifdef UNBOUND_DEBUG
453         if(sldns_buffer_remaining(packet) == 0)
454                 log_err("error: send empty UDP packet");
455 #endif
456         log_assert(addr && addrlen > 0);
457
458         msg.msg_name = addr;
459         msg.msg_namelen = addrlen;
460         iov[0].iov_base = sldns_buffer_begin(packet);
461         iov[0].iov_len = sldns_buffer_remaining(packet);
462         msg.msg_iov = iov;
463         msg.msg_iovlen = 1;
464         msg.msg_control = control;
465 #ifndef S_SPLINT_S
466         msg.msg_controllen = sizeof(control);
467 #endif /* S_SPLINT_S */
468         msg.msg_flags = 0;
469
470 #ifndef S_SPLINT_S
471         cmsg = CMSG_FIRSTHDR(&msg);
472         if(r->srctype == 4) {
473 #ifdef IP_PKTINFO
474                 void* cmsg_data;
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));
493 #else
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) {
498                 void* cmsg_data;
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));
509         } else {
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));
517         }
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);
522         if(sent == -1) {
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. */
526 #ifndef USE_WINSOCK
527                 if(errno == EAGAIN || 
528 #  ifdef EWOULDBLOCK
529                         errno == EWOULDBLOCK ||
530 #  endif
531                         errno == ENOBUFS) {
532 #else
533                 if(WSAGetLastError() == WSAEINPROGRESS ||
534                         WSAGetLastError() == WSAENOBUFS ||
535                         WSAGetLastError() == WSAEWOULDBLOCK) {
536 #endif
537                         int e;
538                         fd_set_block(c->fd);
539                         sent = sendmsg(c->fd, &msg, 0);
540                         e = errno;
541                         fd_set_nonblock(c->fd);
542                         errno = e;
543                 }
544         }
545         if(sent == -1) {
546                 if(!udp_send_errno_needs_log(addr, addrlen))
547                         return 0;
548                 verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno));
549                 log_addr(VERB_OPS, "remote address is", 
550                         (struct sockaddr_storage*)addr, addrlen);
551 #ifdef __NetBSD__
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");
556 #endif
557                 return 0;
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));
561                 return 0;
562         }
563         return 1;
564 #else
565         (void)c;
566         (void)packet;
567         (void)addr;
568         (void)addrlen;
569         (void)r;
570         log_err("sendmsg: IPV6_PKTINFO not supported");
571         return 0;
572 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */
573 }
574
575 void 
576 comm_point_udp_ancil_callback(int fd, short event, void* arg)
577 {
578 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
579         struct comm_reply rep;
580         struct msghdr msg;
581         struct iovec iov[1];
582         ssize_t rcv;
583         char ancil[256];
584         int i;
585 #ifndef S_SPLINT_S
586         struct cmsghdr* cmsg;
587 #endif /* S_SPLINT_S */
588
589         rep.c = (struct comm_point*)arg;
590         log_assert(rep.c->type == comm_udp);
591
592         if(!(event&UB_EV_READ))
593                 return;
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);
605                 msg.msg_iov = iov;
606                 msg.msg_iovlen = 1;
607                 msg.msg_control = ancil;
608 #ifndef S_SPLINT_S
609                 msg.msg_controllen = sizeof(ancil);
610 #endif /* S_SPLINT_S */
611                 msg.msg_flags = 0;
612                 rcv = recvmsg(fd, &msg, 0);
613                 if(rcv == -1) {
614                         if(errno != EAGAIN && errno != EINTR) {
615                                 log_err("recvmsg failed: %s", strerror(errno));
616                         }
617                         return;
618                 }
619                 rep.addrlen = msg.msg_namelen;
620                 sldns_buffer_skip(rep.c->buffer, rcv);
621                 sldns_buffer_flip(rep.c->buffer);
622                 rep.srctype = 0;
623 #ifndef S_SPLINT_S
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) {
628                                 rep.srctype = 6;
629                                 memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg),
630                                         sizeof(struct in6_pktinfo));
631                                 break;
632 #ifdef IP_PKTINFO
633                         } else if( cmsg->cmsg_level == IPPROTO_IP &&
634                                 cmsg->cmsg_type == IP_PKTINFO) {
635                                 rep.srctype = 4;
636                                 memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg),
637                                         sizeof(struct in_pktinfo));
638                                 break;
639 #elif defined(IP_RECVDSTADDR)
640                         } else if( cmsg->cmsg_level == IPPROTO_IP &&
641                                 cmsg->cmsg_type == IP_RECVDSTADDR) {
642                                 rep.srctype = 4;
643                                 memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg),
644                                         sizeof(struct in_addr));
645                                 break;
646 #endif /* IP_PKTINFO or IP_RECVDSTADDR */
647                         }
648                 }
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);
657                 }
658                 if(!rep.c || rep.c->fd == -1) /* commpoint closed */
659                         break;
660         }
661 #else
662         (void)fd;
663         (void)event;
664         (void)arg;
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 */
668 }
669
670 void 
671 comm_point_udp_callback(int fd, short event, void* arg)
672 {
673         struct comm_reply rep;
674         ssize_t rcv;
675         int i;
676         struct sldns_buffer *buffer;
677
678         rep.c = (struct comm_point*)arg;
679         log_assert(rep.c->type == comm_udp);
680
681         if(!(event&UB_EV_READ))
682                 return;
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);
693                 if(rcv == -1) {
694 #ifndef USE_WINSOCK
695                         if(errno != EAGAIN && errno != EINTR)
696                                 log_err("recvfrom %d failed: %s", 
697                                         fd, strerror(errno));
698 #else
699                         if(WSAGetLastError() != WSAEINPROGRESS &&
700                                 WSAGetLastError() != WSAECONNRESET &&
701                                 WSAGetLastError()!= WSAEWOULDBLOCK)
702                                 log_err("recvfrom failed: %s",
703                                         wsa_strerror(WSAGetLastError()));
704 #endif
705                         return;
706                 }
707                 sldns_buffer_skip(rep.c->buffer, rcv);
708                 sldns_buffer_flip(rep.c->buffer);
709                 rep.srctype = 0;
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 */
713 #ifdef USE_DNSCRYPT
714                         buffer = rep.c->dnscrypt_buffer;
715 #else
716                         buffer = rep.c->buffer;
717 #endif
718                         (void)comm_point_send_udp_msg(rep.c, buffer,
719                                 (struct sockaddr*)&rep.addr, rep.addrlen);
720                 }
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. */
723                         break;
724         }
725 }
726
727 /** Use a new tcp handler for new query fd, set to read query */
728 static void
729 setup_tcp_handler(struct comm_point* c, int fd, int cur, int max) 
730 {
731         log_assert(c->type == comm_tcp);
732         log_assert(c->fd == -1);
733         sldns_buffer_clear(c->buffer);
734 #ifdef USE_DNSCRYPT
735         if (c->dnscrypt)
736                 sldns_buffer_clear(c->dnscrypt_buffer);
737 #endif
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 */
744         if(cur > max/2)
745                 c->tcp_timeout_msec = TCP_QUERY_TIMEOUT_FAST;
746         comm_point_start_listening(c, fd, c->tcp_timeout_msec);
747 }
748
749 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
750         short ATTR_UNUSED(event), void* arg)
751 {
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;
759         }
760 }
761
762 int comm_point_perform_accept(struct comm_point* c,
763         struct sockaddr_storage* addr, socklen_t* addrlen)
764 {
765         int new_fd;
766         *addrlen = (socklen_t)sizeof(*addr);
767         new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen);
768         if(new_fd == -1) {
769 #ifndef USE_WINSOCK
770                 /* EINTR is signal interrupt. others are closed connection. */
771                 if(     errno == EINTR || errno == EAGAIN
772 #ifdef EWOULDBLOCK
773                         || errno == EWOULDBLOCK 
774 #endif
775 #ifdef ECONNABORTED
776                         || errno == ECONNABORTED 
777 #endif
778 #ifdef EPROTO
779                         || errno == EPROTO
780 #endif /* EPROTO */
781                         )
782                         return -1;
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;
789                                 struct timeval tv;
790                                 verbose(VERB_ALGO, "out of file descriptors: "
791                                         "slow accept");
792                                 b->eb->slow_accept_enabled = 1;
793                                 fptr_ok(fptr_whitelist_stop_accept(
794                                         b->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,
800                                         -1, UB_EV_TIMEOUT,
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." */
806                                 }
807                                 else if(ub_event_add(b->eb->slow_accept, &tv)
808                                         != 0) {
809                                         /* we do not want to log here,
810                                          * error: "event_add failed." */
811                                 }
812                         }
813                         return -1;
814                 }
815 #endif
816                 log_err_addr("accept failed", strerror(errno), addr, *addrlen);
817 #else /* USE_WINSOCK */
818                 if(WSAGetLastError() == WSAEINPROGRESS ||
819                         WSAGetLastError() == WSAECONNRESET)
820                         return -1;
821                 if(WSAGetLastError() == WSAEWOULDBLOCK) {
822                         ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
823                         return -1;
824                 }
825                 log_err_addr("accept failed", wsa_strerror(WSAGetLastError()),
826                         addr, *addrlen);
827 #endif
828                 return -1;
829         }
830         fd_set_nonblock(new_fd);
831         return new_fd;
832 }
833
834 #ifdef USE_WINSOCK
835 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp),
836         int ATTR_UNUSED(argi), long argl, long retvalue)
837 {
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);
848         }
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);
854         }
855         /* return original return value */
856         return retvalue;
857 }
858
859 /** set win bio callbacks for nonblocking operations */
860 void
861 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl)
862 {
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);
869 }
870 #endif
871
872 void 
873 comm_point_tcp_accept_callback(int fd, short event, void* arg)
874 {
875         struct comm_point* c = (struct comm_point*)arg, *c_hdl;
876         int new_fd;
877         log_assert(c->type == comm_tcp_accept);
878         if(!(event & UB_EV_READ)) {
879                 log_info("ignoring tcp accept event %d", (int)event);
880                 return;
881         }
882         ub_comm_base_now(c->ev->base);
883         /* find free tcp handler. */
884         if(!c->tcp_free) {
885                 log_warn("accepted too many tcp, connections full");
886                 return;
887         }
888         /* accept incoming connection. */
889         c_hdl = c->tcp_free;
890         log_assert(fd != -1);
891         (void)fd;
892         new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.addr,
893                 &c_hdl->repinfo.addrlen);
894         if(new_fd == -1)
895                 return;
896         if(c->ssl) {
897                 c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd);
898                 if(!c_hdl->ssl) {
899                         c_hdl->fd = new_fd;
900                         comm_point_close(c_hdl);
901                         return;
902                 }
903                 c_hdl->ssl_shake_state = comm_ssl_shake_read;
904 #ifdef USE_WINSOCK
905                 comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl);
906 #endif
907         }
908
909         /* grab the tcp handler buffers */
910         c->cur_tcp_count++;
911         c->tcp_free = c_hdl->tcp_free;
912         if(!c->tcp_free) {
913                 /* stop accepting incoming queries for now. */
914                 comm_point_stop_listening(c);
915         }
916         setup_tcp_handler(c_hdl, new_fd, c->cur_tcp_count, c->max_tcp_count);
917 }
918
919 /** Make tcp handler free for next assignment */
920 static void
921 reclaim_tcp_handler(struct comm_point* c)
922 {
923         log_assert(c->type == comm_tcp);
924         if(c->ssl) {
925 #ifdef HAVE_SSL
926                 SSL_shutdown(c->ssl);
927                 SSL_free(c->ssl);
928                 c->ssl = NULL;
929 #endif
930         }
931         comm_point_close(c);
932         if(c->tcp_parent) {
933                 c->tcp_parent->cur_tcp_count--;
934                 c->tcp_free = c->tcp_parent->tcp_free;
935                 c->tcp_parent->tcp_free = c;
936                 if(!c->tcp_free) {
937                         /* re-enable listening on accept socket */
938                         comm_point_start_listening(c->tcp_parent, -1, -1);
939                 }
940         }
941 }
942
943 /** do the callback when writing is done */
944 static void
945 tcp_callback_writer(struct comm_point* c)
946 {
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);
955 }
956
957 /** do the callback when reading is done */
958 static void
959 tcp_callback_reader(struct comm_point* c)
960 {
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);
971         }
972 }
973
974 #ifdef HAVE_SSL
975 /** log certificate details */
976 static void
977 log_cert(unsigned level, const char* str, X509* cert)
978 {
979         BIO* bio;
980         char nul = 0;
981         char* pp = NULL;
982         long len;
983         if(verbosity < level) return;
984         bio = BIO_new(BIO_s_mem());
985         if(!bio) return;
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);
993         if(len != 0 && pp) {
994                 verbose(level, "%s: \n%s", str, pp);
995         }
996         BIO_free(bio);
997 }
998 #endif /* HAVE_SSL */
999
1000 /** continue ssl handshake */
1001 #ifdef HAVE_SSL
1002 static int
1003 ssl_handshake(struct comm_point* c)
1004 {
1005         int r;
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;
1010                 return 1;
1011         }
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;
1016                 return 1;
1017         }
1018
1019         ERR_clear_error();
1020         r = SSL_do_handshake(c->ssl);
1021         if(r != 1) {
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)
1025                                 return 1;
1026                         c->ssl_shake_state = comm_ssl_shake_read;
1027                         comm_point_listen_for_rw(c, 1, 0);
1028                         return 1;
1029                 } else if(want == SSL_ERROR_WANT_WRITE) {
1030                         if(c->ssl_shake_state == comm_ssl_shake_write)
1031                                 return 1;
1032                         c->ssl_shake_state = comm_ssl_shake_write;
1033                         comm_point_listen_for_rw(c, 0, 1);
1034                         return 1;
1035                 } else if(r == 0) {
1036                         return 0; /* closed */
1037                 } else if(want == SSL_ERROR_SYSCALL) {
1038                         /* SYSCALL and errno==0 means closed uncleanly */
1039                         if(errno != 0)
1040                                 log_err("SSL_handshake syscall: %s",
1041                                         strerror(errno));
1042                         return 0;
1043                 } else {
1044                         log_crypto_err("ssl handshake failed");
1045                         log_addr(1, "ssl handshake failed", &c->repinfo.addr,
1046                                 c->repinfo.addrlen);
1047                         return 0;
1048                 }
1049         }
1050         /* this is where peer verification could take place */
1051         if((SSL_get_verify_mode(c->ssl)&SSL_VERIFY_PEER)) {
1052                 /* verification */
1053                 if(SSL_get_verify_result(c->ssl) == X509_V_OK) {
1054                         X509* x = SSL_get_peer_certificate(c->ssl);
1055                         if(!x) {
1056                                 log_addr(VERB_ALGO, "SSL connection failed: "
1057                                         "no certificate",
1058                                         &c->repinfo.addr, c->repinfo.addrlen);
1059                                 return 0;
1060                         }
1061                         log_cert(VERB_ALGO, "peer certificate", x);
1062 #ifdef HAVE_SSL_GET0_PEERNAME
1063                         if(SSL_get0_peername(c->ssl)) {
1064                                 char buf[255];
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);
1070                         } else {
1071 #endif
1072                                 log_addr(VERB_ALGO, "SSL connection "
1073                                         "authenticated", &c->repinfo.addr,
1074                                         c->repinfo.addrlen);
1075 #ifdef HAVE_SSL_GET0_PEERNAME
1076                         }
1077 #endif
1078                         X509_free(x);
1079                 } else {
1080                         X509* x = SSL_get_peer_certificate(c->ssl);
1081                         if(x) {
1082                                 log_cert(VERB_ALGO, "peer certificate", x);
1083                                 X509_free(x);
1084                         }
1085                         log_addr(VERB_ALGO, "SSL connection failed: "
1086                                 "failed to authenticate",
1087                                 &c->repinfo.addr, c->repinfo.addrlen);
1088                         return 0;
1089                 }
1090         } else {
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);
1095         }
1096
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);
1101         } else {
1102                 comm_point_listen_for_rw(c, 1, 1);
1103         }
1104         c->ssl_shake_state = comm_ssl_shake_none;
1105         return 1;
1106 }
1107 #endif /* HAVE_SSL */
1108
1109 /** ssl read callback on TCP */
1110 static int
1111 ssl_handle_read(struct comm_point* c)
1112 {
1113 #ifdef HAVE_SSL
1114         int r;
1115         if(c->ssl_shake_state != comm_ssl_shake_none) {
1116                 if(!ssl_handshake(c))
1117                         return 0;
1118                 if(c->ssl_shake_state != comm_ssl_shake_none)
1119                         return 1;
1120         }
1121         if(c->tcp_byte_count < sizeof(uint16_t)) {
1122                 /* read length bytes */
1123                 ERR_clear_error();
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);
1135                                 return 1;
1136                         } else if(want == SSL_ERROR_SYSCALL) {
1137                                 if(errno != 0)
1138                                         log_err("SSL_read syscall: %s",
1139                                                 strerror(errno));
1140                                 return 0;
1141                         }
1142                         log_crypto_err("could not SSL_read");
1143                         return 0;
1144                 }
1145                 c->tcp_byte_count += r;
1146                 if(c->tcp_byte_count != sizeof(uint16_t))
1147                         return 1;
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");
1151                         return 0;
1152                 }
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.");
1157                         return 0;
1158                 }
1159                 verbose(VERB_ALGO, "Reading ssl tcp query of length %d",
1160                         (int)sldns_buffer_limit(c->buffer));
1161         }
1162         log_assert(sldns_buffer_remaining(c->buffer) > 0);
1163         ERR_clear_error();
1164         r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
1165                 (int)sldns_buffer_remaining(c->buffer));
1166         if(r <= 0) {
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);
1175                         return 1;
1176                 } else if(want == SSL_ERROR_SYSCALL) {
1177                         if(errno != 0)
1178                                 log_err("SSL_read syscall: %s",
1179                                         strerror(errno));
1180                         return 0;
1181                 }
1182                 log_crypto_err("could not SSL_read");
1183                 return 0;
1184         }
1185         sldns_buffer_skip(c->buffer, (ssize_t)r);
1186         if(sldns_buffer_remaining(c->buffer) <= 0) {
1187                 tcp_callback_reader(c);
1188         }
1189         return 1;
1190 #else
1191         (void)c;
1192         return 0;
1193 #endif /* HAVE_SSL */
1194 }
1195
1196 /** ssl write callback on TCP */
1197 static int
1198 ssl_handle_write(struct comm_point* c)
1199 {
1200 #ifdef HAVE_SSL
1201         int r;
1202         if(c->ssl_shake_state != comm_ssl_shake_none) {
1203                 if(!ssl_handshake(c))
1204                         return 0;
1205                 if(c->ssl_shake_state != comm_ssl_shake_none)
1206                         return 1;
1207         }
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));
1212                 ERR_clear_error();
1213                 if(sizeof(uint16_t)+sldns_buffer_remaining(c->buffer) <
1214                         LDNS_RR_BUF_SIZE) {
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));
1226                 } else {
1227                         r = SSL_write(c->ssl,
1228                                 (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1229                                 (int)(sizeof(uint16_t)-c->tcp_byte_count));
1230                 }
1231                 if(r <= 0) {
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) {
1242                                 if(errno != 0)
1243                                         log_err("SSL_write syscall: %s",
1244                                                 strerror(errno));
1245                                 return 0;
1246                         }
1247                         log_crypto_err("could not SSL_write");
1248                         return 0;
1249                 }
1250                 c->tcp_byte_count += r;
1251                 if(c->tcp_byte_count < sizeof(uint16_t))
1252                         return 1;
1253                 sldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1254                         sizeof(uint16_t));
1255                 if(sldns_buffer_remaining(c->buffer) == 0) {
1256                         tcp_callback_writer(c);
1257                         return 1;
1258                 }
1259         }
1260         log_assert(sldns_buffer_remaining(c->buffer) > 0);
1261         ERR_clear_error();
1262         r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
1263                 (int)sldns_buffer_remaining(c->buffer));
1264         if(r <= 0) {
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) {
1275                         if(errno != 0)
1276                                 log_err("SSL_write syscall: %s",
1277                                         strerror(errno));
1278                         return 0;
1279                 }
1280                 log_crypto_err("could not SSL_write");
1281                 return 0;
1282         }
1283         sldns_buffer_skip(c->buffer, (ssize_t)r);
1284
1285         if(sldns_buffer_remaining(c->buffer) == 0) {
1286                 tcp_callback_writer(c);
1287         }
1288         return 1;
1289 #else
1290         (void)c;
1291         return 0;
1292 #endif /* HAVE_SSL */
1293 }
1294
1295 /** handle ssl tcp connection with dns contents */
1296 static int
1297 ssl_handle_it(struct comm_point* c)
1298 {
1299         if(c->tcp_is_reading)
1300                 return ssl_handle_read(c);
1301         return ssl_handle_write(c);
1302 }
1303
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 
1309  */
1310 static int
1311 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok)
1312 {
1313         ssize_t r;
1314         log_assert(c->type == comm_tcp || c->type == comm_local);
1315         if(c->ssl)
1316                 return ssl_handle_it(c);
1317         if(!c->tcp_is_reading)
1318                 return 0;
1319
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);
1325                 if(r == 0)
1326                         return 0;
1327                 else if(r == -1) {
1328 #ifndef USE_WINSOCK
1329                         if(errno == EINTR || errno == EAGAIN)
1330                                 return 1;
1331 #ifdef ECONNRESET
1332                         if(errno == ECONNRESET && verbosity < 2)
1333                                 return 0; /* silence reset by peer */
1334 #endif
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)
1339                                 return 0;
1340                         if(WSAGetLastError() == WSAEINPROGRESS)
1341                                 return 1;
1342                         if(WSAGetLastError() == WSAEWOULDBLOCK) {
1343                                 ub_winsock_tcp_wouldblock(c->ev->ev,
1344                                         UB_EV_READ);
1345                                 return 1;
1346                         }
1347                         log_err_addr("read (in tcp s)", 
1348                                 wsa_strerror(WSAGetLastError()),
1349                                 &c->repinfo.addr, c->repinfo.addrlen);
1350 #endif
1351                         return 0;
1352                 } 
1353                 c->tcp_byte_count += r;
1354                 if(c->tcp_byte_count != sizeof(uint16_t))
1355                         return 1;
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");
1359                         return 0;
1360                 }
1361                 sldns_buffer_set_limit(c->buffer, 
1362                         sldns_buffer_read_u16_at(c->buffer, 0));
1363                 if(!short_ok && 
1364                         sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1365                         verbose(VERB_QUERY, "tcp: dropped bogus too short.");
1366                         return 0;
1367                 }
1368                 verbose(VERB_ALGO, "Reading tcp query of length %d", 
1369                         (int)sldns_buffer_limit(c->buffer));
1370         }
1371
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);
1375         if(r == 0) {
1376                 return 0;
1377         } else if(r == -1) {
1378 #ifndef USE_WINSOCK
1379                 if(errno == EINTR || errno == EAGAIN)
1380                         return 1;
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)
1385                         return 0;
1386                 if(WSAGetLastError() == WSAEINPROGRESS)
1387                         return 1;
1388                 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1389                         ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1390                         return 1;
1391                 }
1392                 log_err_addr("read (in tcp r)",
1393                         wsa_strerror(WSAGetLastError()),
1394                         &c->repinfo.addr, c->repinfo.addrlen);
1395 #endif
1396                 return 0;
1397         }
1398         sldns_buffer_skip(c->buffer, r);
1399         if(sldns_buffer_remaining(c->buffer) <= 0) {
1400                 tcp_callback_reader(c);
1401         }
1402         return 1;
1403 }
1404
1405 /** 
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
1410  */
1411 static int
1412 comm_point_tcp_handle_write(int fd, struct comm_point* c)
1413 {
1414         ssize_t r;
1415         struct sldns_buffer *buffer;
1416         log_assert(c->type == comm_tcp);
1417 #ifdef USE_DNSCRYPT
1418         buffer = c->dnscrypt_buffer;
1419 #else
1420         buffer = c->buffer;
1421 #endif
1422         if(c->tcp_is_reading && !c->ssl)
1423                 return 0;
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*/
1428                 int error = 0;
1429                 socklen_t len = (socklen_t)sizeof(error);
1430                 if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error, 
1431                         &len) < 0){
1432 #ifndef USE_WINSOCK
1433                         error = errno; /* on solaris errno is error */
1434 #else /* USE_WINSOCK */
1435                         error = WSAGetLastError();
1436 #endif
1437                 }
1438 #ifndef USE_WINSOCK
1439 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1440                 if(error == EINPROGRESS || error == EWOULDBLOCK)
1441                         return 1; /* try again later */
1442                 else
1443 #endif
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 */
1450                 /* examine error */
1451                 if(error == WSAEINPROGRESS)
1452                         return 1;
1453                 else if(error == WSAEWOULDBLOCK) {
1454                         ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1455                         return 1;
1456                 } else if(error != 0 && verbosity < 2)
1457                         return 0;
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 */
1462                         return 0;
1463                 }
1464         }
1465         if(c->ssl)
1466                 return ssl_handle_it(c);
1467
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));
1476                 struct msghdr msg;
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;
1488                 msg.msg_iov = iov;
1489                 msg.msg_iovlen = 2;
1490                 r = sendmsg(fd, &msg, MSG_FASTOPEN);
1491                 if (r == -1) {
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)
1496                                 return 1;
1497 #endif
1498                         if(errno == EINTR || errno == EAGAIN)
1499                                 return 1;
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);
1506                                 return 0;
1507                         }
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) {
1512 #ifdef EINPROGRESS
1513                                 if(errno == EINPROGRESS)
1514                                         return 1; /* wait until connect done*/
1515 #endif
1516 #ifdef USE_WINSOCK
1517                                 if(WSAGetLastError() == WSAEINPROGRESS ||
1518                                         WSAGetLastError() == WSAEWOULDBLOCK)
1519                                         return 1; /* wait until connect done*/
1520 #endif
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);
1525                                 }
1526                                 return 0;
1527                         }
1528
1529                 } else {
1530                         c->tcp_byte_count += r;
1531                         if(c->tcp_byte_count < sizeof(uint16_t))
1532                                 return 1;
1533                         sldns_buffer_set_position(buffer, c->tcp_byte_count - 
1534                                 sizeof(uint16_t));
1535                         if(sldns_buffer_remaining(buffer) == 0) {
1536                                 tcp_callback_writer(c);
1537                                 return 1;
1538                         }
1539                 }
1540         }
1541 #endif /* USE_MSG_FASTOPEN */
1542
1543         if(c->tcp_byte_count < sizeof(uint16_t)) {
1544                 uint16_t len = htons(sldns_buffer_limit(buffer));
1545 #ifdef HAVE_WRITEV
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 */
1558                 if(r == -1) {
1559 #ifndef USE_WINSOCK
1560 #  ifdef EPIPE
1561                         if(errno == EPIPE && verbosity < 2)
1562                                 return 0; /* silence 'broken pipe' */
1563   #endif
1564                         if(errno == EINTR || errno == EAGAIN)
1565                                 return 1;
1566 #  ifdef HAVE_WRITEV
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 */
1573 #else
1574                         if(WSAGetLastError() == WSAENOTCONN)
1575                                 return 1;
1576                         if(WSAGetLastError() == WSAEINPROGRESS)
1577                                 return 1;
1578                         if(WSAGetLastError() == WSAEWOULDBLOCK) {
1579                                 ub_winsock_tcp_wouldblock(c->ev->ev,
1580                                         UB_EV_WRITE);
1581                                 return 1; 
1582                         }
1583                         log_err_addr("tcp send s",
1584                                 wsa_strerror(WSAGetLastError()),
1585                                 &c->repinfo.addr, c->repinfo.addrlen);
1586 #endif
1587                         return 0;
1588                 }
1589                 c->tcp_byte_count += r;
1590                 if(c->tcp_byte_count < sizeof(uint16_t))
1591                         return 1;
1592                 sldns_buffer_set_position(buffer, c->tcp_byte_count - 
1593                         sizeof(uint16_t));
1594                 if(sldns_buffer_remaining(buffer) == 0) {
1595                         tcp_callback_writer(c);
1596                         return 1;
1597                 }
1598         }
1599         log_assert(sldns_buffer_remaining(buffer) > 0);
1600         r = send(fd, (void*)sldns_buffer_current(buffer), 
1601                 sldns_buffer_remaining(buffer), 0);
1602         if(r == -1) {
1603 #ifndef USE_WINSOCK
1604                 if(errno == EINTR || errno == EAGAIN)
1605                         return 1;
1606                 log_err_addr("tcp send r", strerror(errno),
1607                         &c->repinfo.addr, c->repinfo.addrlen);
1608 #else
1609                 if(WSAGetLastError() == WSAEINPROGRESS)
1610                         return 1;
1611                 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1612                         ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
1613                         return 1; 
1614                 }
1615                 log_err_addr("tcp send r", wsa_strerror(WSAGetLastError()),
1616                         &c->repinfo.addr, c->repinfo.addrlen);
1617 #endif
1618                 return 0;
1619         }
1620         sldns_buffer_skip(buffer, r);
1621
1622         if(sldns_buffer_remaining(buffer) == 0) {
1623                 tcp_callback_writer(c);
1624         }
1625         
1626         return 1;
1627 }
1628
1629 void 
1630 comm_point_tcp_handle_callback(int fd, short event, void* arg)
1631 {
1632         struct comm_point* c = (struct comm_point*)arg;
1633         log_assert(c->type == comm_tcp);
1634         ub_comm_base_now(c->ev->base);
1635
1636 #ifdef USE_DNSCRYPT
1637         /* Initialize if this is a dnscrypt socket */
1638         if(c->tcp_parent) {
1639                 c->dnscrypt = c->tcp_parent->dnscrypt;
1640         }
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(
1648                                         c->callback));
1649                                 (void)(*c->callback)(c, c->cb_arg, 
1650                                         NETEVENT_CLOSED, NULL);
1651                         }
1652                         return;
1653                 }
1654         }
1655 #endif
1656
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(
1662                                         c->callback));
1663                                 (void)(*c->callback)(c, c->cb_arg, 
1664                                         NETEVENT_CLOSED, NULL);
1665                         }
1666                 }
1667                 return;
1668         }
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(
1674                                         c->callback));
1675                                 (void)(*c->callback)(c, c->cb_arg, 
1676                                         NETEVENT_CLOSED, NULL);
1677                         }
1678                 }
1679                 return;
1680         }
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);
1688                 }
1689                 return;
1690         }
1691         log_err("Ignored event %d for tcphdl.", event);
1692 }
1693
1694 /** Make http handler free for next assignment */
1695 static void
1696 reclaim_http_handler(struct comm_point* c)
1697 {
1698         log_assert(c->type == comm_http);
1699         if(c->ssl) {
1700 #ifdef HAVE_SSL
1701                 SSL_shutdown(c->ssl);
1702                 SSL_free(c->ssl);
1703                 c->ssl = NULL;
1704 #endif
1705         }
1706         comm_point_close(c);
1707         if(c->tcp_parent) {
1708                 c->tcp_parent->cur_tcp_count--;
1709                 c->tcp_free = c->tcp_parent->tcp_free;
1710                 c->tcp_parent->tcp_free = c;
1711                 if(!c->tcp_free) {
1712                         /* re-enable listening on accept socket */
1713                         comm_point_start_listening(c->tcp_parent, -1, -1);
1714                 }
1715         }
1716 }
1717
1718 /** read more data for http (with ssl) */
1719 static int
1720 ssl_http_read_more(struct comm_point* c)
1721 {
1722 #ifdef HAVE_SSL
1723         int r;
1724         log_assert(sldns_buffer_remaining(c->buffer) > 0);
1725         ERR_clear_error();
1726         r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
1727                 (int)sldns_buffer_remaining(c->buffer));
1728         if(r <= 0) {
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);
1737                         return 1;
1738                 } else if(want == SSL_ERROR_SYSCALL) {
1739                         if(errno != 0)
1740                                 log_err("SSL_read syscall: %s",
1741                                         strerror(errno));
1742                         return 0;
1743                 }
1744                 log_crypto_err("could not SSL_read");
1745                 return 0;
1746         }
1747         sldns_buffer_skip(c->buffer, (ssize_t)r);
1748         return 1;
1749 #else
1750         (void)c;
1751         return 0;
1752 #endif /* HAVE_SSL */
1753 }
1754
1755 /** read more data for http */
1756 static int
1757 http_read_more(int fd, struct comm_point* c)
1758 {
1759         ssize_t r;
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);
1763         if(r == 0) {
1764                 return 0;
1765         } else if(r == -1) {
1766 #ifndef USE_WINSOCK
1767                 if(errno == EINTR || errno == EAGAIN)
1768                         return 1;
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)
1773                         return 0;
1774                 if(WSAGetLastError() == WSAEINPROGRESS)
1775                         return 1;
1776                 if(WSAGetLastError() == WSAEWOULDBLOCK) {
1777                         ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1778                         return 1;
1779                 }
1780                 log_err_addr("read (in http r)",
1781                         wsa_strerror(WSAGetLastError()),
1782                         &c->repinfo.addr, c->repinfo.addrlen);
1783 #endif
1784                 return 0;
1785         }
1786         sldns_buffer_skip(c->buffer, r);
1787         return 1;
1788 }
1789
1790 /** return true if http header has been read (one line complete) */
1791 static int
1792 http_header_done(sldns_buffer* buf)
1793 {
1794         size_t i;
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')
1798                         return 1;
1799         }
1800         return 0;
1801 }
1802
1803 /** return character string into buffer for header line, moves buffer
1804  * past that line and puts zero terminator into linefeed-newline */
1805 static char*
1806 http_header_line(sldns_buffer* buf)
1807 {
1808         char* result = (char*)sldns_buffer_current(buf);
1809         size_t i;
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);
1818                         return result;
1819                 }
1820         }
1821         return NULL;
1822 }
1823
1824 /** move unread buffer to start and clear rest for putting the rest into it */
1825 static void
1826 http_moveover_buffer(sldns_buffer* buf)
1827 {
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);
1833 }
1834
1835 /** a http header is complete, process it */
1836 static int
1837 http_process_initial_header(struct comm_point* c)
1838 {
1839         char* line = http_header_line(c->buffer);
1840         if(!line) return 1;
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);
1846                         return 0;
1847                 }
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);
1865                 return 1;
1866         }
1867         /* ignore other headers */
1868         return 1;
1869 }
1870
1871 /** a chunk header is complete, process it, return 0=fail, 1=continue next
1872  * header line, 2=done with chunked transfer*/
1873 static int
1874 http_process_chunk_header(struct comm_point* c)
1875 {
1876         char* line = http_header_line(c->buffer);
1877         if(!line) return 1;
1878         if(c->http_in_chunk_headers == 3) {
1879                 verbose(VERB_ALGO, "http chunk trailer: %s", line);
1880                 /* are we done ? */
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 */
1889                         return 2;
1890                 }
1891                 if(line[0] == 0) {
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);
1897                         return 1;
1898                 }
1899                 /* ignore further trail headers */
1900                 return 1;
1901         }
1902         verbose(VERB_ALGO, "http chunk header: %s", line);
1903         if(c->http_in_chunk_headers == 1) {
1904                 /* read chunked start line */
1905                 char* end = NULL;
1906                 c->tcp_byte_count = (size_t)strtol(line, &end, 16);
1907                 if(end == line)
1908                         return 0;
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;
1916                 }
1917                 return 1;
1918         }
1919         /* ignore other headers */
1920         return 1;
1921 }
1922
1923 /** handle nonchunked data segment */
1924 static int
1925 http_nonchunk_segment(struct comm_point* c)
1926 {
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 */
1935                 c->http_stored = 0;
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);
1939                 return 1;
1940         }
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 */
1953                 return 1;
1954         }
1955         /* call callback with this data amount, then
1956          * wait for more */
1957         c->http_stored = 0;
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 */
1963         return 1;
1964 }
1965
1966 /** handle nonchunked data segment, return 0=fail, 1=wait, 2=process more */
1967 static int
1968 http_chunked_segment(struct comm_point* c)
1969 {
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.
1973          */
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 */
1980                 size_t fraglen;
1981                 struct comm_reply repinfo;
1982                 c->http_stored = 0;
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);
1989
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(). */
1998
1999                 /* is commpoint deleted? */
2000                 if(!repinfo.c) {
2001                         return 1;
2002                 }
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
2010                  * an empty line */
2011                 c->http_in_chunk_headers = 3;
2012                 /* process more data in buffer (if any) */
2013                 return 2;
2014         }
2015         c->tcp_byte_count -= got_now;
2016
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 */
2028                 return 1;
2029         }
2030         
2031         /* callback of http reader for a new part of the data */
2032         c->http_stored = 0;
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 */
2038         return 1;
2039 }
2040
2041 /**
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 
2046  */
2047 static int
2048 comm_point_http_handle_read(int fd, struct comm_point* c)
2049 {
2050         log_assert(c->type == comm_http);
2051         log_assert(fd != -1);
2052
2053         /* if we are in ssl handshake, handle SSL handshake */
2054 #ifdef HAVE_SSL
2055         if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
2056                 if(!ssl_handshake(c))
2057                         return 0;
2058                 if(c->ssl_shake_state != comm_ssl_shake_none)
2059                         return 1;
2060         }
2061 #endif /* HAVE_SSL */
2062
2063         if(!c->tcp_is_reading)
2064                 return 1;
2065         /* read more data */
2066         if(c->ssl) {
2067                 if(!ssl_http_read_more(c))
2068                         return 0;
2069         } else {
2070                 if(!http_read_more(fd, c))
2071                         return 0;
2072         }
2073
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 */
2084                                 return 1;
2085                         }
2086                         if(!c->http_in_chunk_headers) {
2087                                 /* process initial headers */
2088                                 if(!http_process_initial_header(c))
2089                                         return 0;
2090                         } else {
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 */
2096                         }
2097                         /* see if we have more to process */
2098                         continue;
2099                 }
2100
2101                 if(!c->http_is_chunked) {
2102                         /* if we are reading nonchunks, process that*/
2103                         return http_nonchunk_segment(c);
2104                 } else {
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;
2109                         continue;
2110                 }
2111         }
2112         /* broke out of the loop; could not process header instead need
2113          * to read more */
2114         /* moveover any remaining data and read more data */
2115         http_moveover_buffer(c->buffer);
2116         /* return and wait to read more */
2117         return 1;
2118 }
2119
2120 /** check pending connect for http */
2121 static int
2122 http_check_connect(int fd, struct comm_point* c)
2123 {
2124         /* check for pending error from nonblocking connect */
2125         /* from Stevens, unix network programming, vol1, 3rd ed, p450*/
2126         int error = 0;
2127         socklen_t len = (socklen_t)sizeof(error);
2128         if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error, 
2129                 &len) < 0){
2130 #ifndef USE_WINSOCK
2131                 error = errno; /* on solaris errno is error */
2132 #else /* USE_WINSOCK */
2133                 error = WSAGetLastError();
2134 #endif
2135         }
2136 #ifndef USE_WINSOCK
2137 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
2138         if(error == EINPROGRESS || error == EWOULDBLOCK)
2139                 return 1; /* try again later */
2140         else
2141 #endif
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 */
2148         /* examine error */
2149         if(error == WSAEINPROGRESS)
2150                 return 1;
2151         else if(error == WSAEWOULDBLOCK) {
2152                 ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2153                 return 1;
2154         } else if(error != 0 && verbosity < 2)
2155                 return 0;
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 */
2160                 return 0;
2161         }
2162         /* keep on processing this socket */
2163         return 2;
2164 }
2165
2166 /** write more data for http (with ssl) */
2167 static int
2168 ssl_http_write_more(struct comm_point* c)
2169 {
2170 #ifdef HAVE_SSL
2171         int r;
2172         log_assert(sldns_buffer_remaining(c->buffer) > 0);
2173         ERR_clear_error();
2174         r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
2175                 (int)sldns_buffer_remaining(c->buffer));
2176         if(r <= 0) {
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) {
2187                         if(errno != 0)
2188                                 log_err("SSL_write syscall: %s",
2189                                         strerror(errno));
2190                         return 0;
2191                 }
2192                 log_crypto_err("could not SSL_write");
2193                 return 0;
2194         }
2195         sldns_buffer_skip(c->buffer, (ssize_t)r);
2196         return 1;
2197 #else
2198         (void)c;
2199         return 0;
2200 #endif /* HAVE_SSL */
2201 }
2202
2203 /** write more data for http */
2204 static int
2205 http_write_more(int fd, struct comm_point* c)
2206 {
2207         ssize_t r;
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);
2211         if(r == -1) {
2212 #ifndef USE_WINSOCK
2213                 if(errno == EINTR || errno == EAGAIN)
2214                         return 1;
2215                 log_err_addr("http send r", strerror(errno),
2216                         &c->repinfo.addr, c->repinfo.addrlen);
2217 #else
2218                 if(WSAGetLastError() == WSAEINPROGRESS)
2219                         return 1;
2220                 if(WSAGetLastError() == WSAEWOULDBLOCK) {
2221                         ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2222                         return 1; 
2223                 }
2224                 log_err_addr("http send r", wsa_strerror(WSAGetLastError()),
2225                         &c->repinfo.addr, c->repinfo.addrlen);
2226 #endif
2227                 return 0;
2228         }
2229         sldns_buffer_skip(c->buffer, r);
2230         return 1;
2231 }
2232
2233 /** 
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
2238  */
2239 static int
2240 comm_point_http_handle_write(int fd, struct comm_point* c)
2241 {
2242         log_assert(c->type == comm_http);
2243         log_assert(fd != -1);
2244
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;
2252         }
2253         /* if we are in ssl handshake, handle SSL handshake */
2254 #ifdef HAVE_SSL
2255         if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
2256                 if(!ssl_handshake(c))
2257                         return 0;
2258                 if(c->ssl_shake_state != comm_ssl_shake_none)
2259                         return 1;
2260         }
2261 #endif /* HAVE_SSL */
2262         if(c->tcp_is_reading)
2263                 return 1;
2264         /* if we are writing, write more */
2265         if(c->ssl) {
2266                 if(!ssl_http_write_more(c))
2267                         return 0;
2268         } else {
2269                 if(!http_write_more(fd, c))
2270                         return 0;
2271         }
2272
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);
2284         }
2285         return 1;
2286 }
2287
2288 void 
2289 comm_point_http_handle_callback(int fd, short event, void* arg)
2290 {
2291         struct comm_point* c = (struct comm_point*)arg;
2292         log_assert(c->type == comm_http);
2293         ub_comm_base_now(c->ev->base);
2294
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(
2300                                         c->callback));
2301                                 (void)(*c->callback)(c, c->cb_arg, 
2302                                         NETEVENT_CLOSED, NULL);
2303                         }
2304                 }
2305                 return;
2306         }
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(
2312                                         c->callback));
2313                                 (void)(*c->callback)(c, c->cb_arg, 
2314                                         NETEVENT_CLOSED, NULL);
2315                         }
2316                 }
2317                 return;
2318         }
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);
2326                 }
2327                 return;
2328         }
2329         log_err("Ignored event %d for httphdl.", event);
2330 }
2331
2332 void comm_point_local_handle_callback(int fd, short event, void* arg)
2333 {
2334         struct comm_point* c = (struct comm_point*)arg;
2335         log_assert(c->type == comm_local);
2336         ub_comm_base_now(c->ev->base);
2337
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, 
2342                                 NULL);
2343                 }
2344                 return;
2345         }
2346         log_err("Ignored event %d for localhdl.", event);
2347 }
2348
2349 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd), 
2350         short event, void* arg)
2351 {
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);
2356         
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);
2361 }
2362
2363 struct comm_point* 
2364 comm_point_create_udp(struct comm_base *base, int fd, sldns_buffer* buffer,
2365         comm_point_callback_type* callback, void* callback_arg)
2366 {
2367         struct comm_point* c = (struct comm_point*)calloc(1,
2368                 sizeof(struct comm_point));
2369         short evbits;
2370         if(!c)
2371                 return NULL;
2372         c->ev = (struct internal_event*)calloc(1,
2373                 sizeof(struct internal_event));
2374         if(!c->ev) {
2375                 free(c);
2376                 return NULL;
2377         }
2378         c->ev->base = base;
2379         c->fd = fd;
2380         c->buffer = buffer;
2381         c->timeout = NULL;
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;
2388         c->tcp_free = NULL;
2389         c->type = comm_udp;
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;
2396 #endif
2397 #ifdef USE_DNSCRYPT
2398         c->dnscrypt = 0;
2399         c->dnscrypt_buffer = buffer;
2400 #endif
2401         c->inuse = 0;
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);
2411                 return NULL;
2412         }
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);
2416                 return NULL;
2417         }
2418         return c;
2419 }
2420
2421 struct comm_point* 
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)
2425 {
2426         struct comm_point* c = (struct comm_point*)calloc(1,
2427                 sizeof(struct comm_point));
2428         short evbits;
2429         if(!c)
2430                 return NULL;
2431         c->ev = (struct internal_event*)calloc(1,
2432                 sizeof(struct internal_event));
2433         if(!c->ev) {
2434                 free(c);
2435                 return NULL;
2436         }
2437         c->ev->base = base;
2438         c->fd = fd;
2439         c->buffer = buffer;
2440         c->timeout = NULL;
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;
2447         c->tcp_free = NULL;
2448         c->type = comm_udp;
2449         c->tcp_do_close = 0;
2450         c->do_not_close = 0;
2451 #ifdef USE_DNSCRYPT
2452         c->dnscrypt = 0;
2453         c->dnscrypt_buffer = buffer;
2454 #endif
2455         c->inuse = 0;
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;
2460 #endif
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);
2470                 return NULL;
2471         }
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);
2475                 return NULL;
2476         }
2477         return c;
2478 }
2479
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)
2484 {
2485         struct comm_point* c = (struct comm_point*)calloc(1,
2486                 sizeof(struct comm_point));
2487         short evbits;
2488         if(!c)
2489                 return NULL;
2490         c->ev = (struct internal_event*)calloc(1,
2491                 sizeof(struct internal_event));
2492         if(!c->ev) {
2493                 free(c);
2494                 return NULL;
2495         }
2496         c->ev->base = base;
2497         c->fd = -1;
2498         c->buffer = sldns_buffer_new(bufsize);
2499         if(!c->buffer) {
2500                 free(c->ev);
2501                 free(c);
2502                 return NULL;
2503         }
2504         c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
2505         if(!c->timeout) {
2506                 sldns_buffer_free(c->buffer);
2507                 free(c->ev);
2508                 free(c);
2509                 return NULL;
2510         }
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;
2517         c->tcp_free = NULL;
2518         c->type = comm_tcp;
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;
2525 #endif
2526 #ifdef USE_DNSCRYPT
2527         c->dnscrypt = 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;
2531 #endif
2532         c->repinfo.c = c;
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)
2543         {
2544                 log_err("could not basetset tcphdl event");
2545                 parent->tcp_free = c->tcp_free;
2546                 free(c->ev);
2547                 free(c);
2548                 return NULL;
2549         }
2550         return c;
2551 }
2552
2553 struct comm_point* 
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)
2556 {
2557         struct comm_point* c = (struct comm_point*)calloc(1,
2558                 sizeof(struct comm_point));
2559         short evbits;
2560         int i;
2561         /* first allocate the TCP accept listener */
2562         if(!c)
2563                 return NULL;
2564         c->ev = (struct internal_event*)calloc(1,
2565                 sizeof(struct internal_event));
2566         if(!c->ev) {
2567                 free(c);
2568                 return NULL;
2569         }
2570         c->ev->base = base;
2571         c->fd = fd;
2572         c->buffer = NULL;
2573         c->timeout = NULL;
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) {
2582                 free(c->ev);
2583                 free(c);
2584                 return NULL;
2585         }
2586         c->tcp_free = NULL;
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;
2594 #endif
2595 #ifdef USE_DNSCRYPT
2596         c->dnscrypt = 0;
2597         c->dnscrypt_buffer = NULL;
2598 #endif
2599         c->callback = NULL;
2600         c->cb_arg = 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);
2608                 return NULL;
2609         }
2610         if (ub_event_add(c->ev->ev, c->timeout) != 0) {
2611                 log_err("could not add tcpacc event");
2612                 comm_point_delete(c);
2613                 return NULL;
2614         }
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);
2621                         return NULL;
2622                 }
2623         }
2624         
2625         return c;
2626 }
2627
2628 struct comm_point* 
2629 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize,
2630         comm_point_callback_type* callback, void* callback_arg)
2631 {
2632         struct comm_point* c = (struct comm_point*)calloc(1,
2633                 sizeof(struct comm_point));
2634         short evbits;
2635         if(!c)
2636                 return NULL;
2637         c->ev = (struct internal_event*)calloc(1,
2638                 sizeof(struct internal_event));
2639         if(!c->ev) {
2640                 free(c);
2641                 return NULL;
2642         }
2643         c->ev->base = base;
2644         c->fd = -1;
2645         c->buffer = sldns_buffer_new(bufsize);
2646         if(!c->buffer) {
2647                 free(c->ev);
2648                 free(c);
2649                 return NULL;
2650         }
2651         c->timeout = NULL;
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;
2658         c->tcp_free = NULL;
2659         c->type = comm_tcp;
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;
2666 #endif
2667 #ifdef USE_DNSCRYPT
2668         c->dnscrypt = 0;
2669         c->dnscrypt_buffer = c->buffer;
2670 #endif
2671         c->repinfo.c = c;
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)
2678         {
2679                 log_err("could not baseset tcpout event");
2680                 sldns_buffer_free(c->buffer);
2681                 free(c->ev);
2682                 free(c);
2683                 return NULL;
2684         }
2685
2686         return c;
2687 }
2688
2689 struct comm_point* 
2690 comm_point_create_http_out(struct comm_base *base, size_t bufsize,
2691         comm_point_callback_type* callback, void* callback_arg,
2692         sldns_buffer* temp)
2693 {
2694         struct comm_point* c = (struct comm_point*)calloc(1,
2695                 sizeof(struct comm_point));
2696         short evbits;
2697         if(!c)
2698                 return NULL;
2699         c->ev = (struct internal_event*)calloc(1,
2700                 sizeof(struct internal_event));
2701         if(!c->ev) {
2702                 free(c);
2703                 return NULL;
2704         }
2705         c->ev->base = base;
2706         c->fd = -1;
2707         c->buffer = sldns_buffer_new(bufsize);
2708         if(!c->buffer) {
2709                 free(c->ev);
2710                 free(c);
2711                 return NULL;
2712         }
2713         c->timeout = NULL;
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;
2720         c->tcp_free = 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;
2732 #endif
2733 #ifdef USE_DNSCRYPT
2734         c->dnscrypt = 0;
2735         c->dnscrypt_buffer = c->buffer;
2736 #endif
2737         c->repinfo.c = c;
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)
2744         {
2745                 log_err("could not baseset tcpout event");
2746 #ifdef HAVE_SSL
2747                 SSL_free(c->ssl);
2748 #endif
2749                 sldns_buffer_free(c->buffer);
2750                 free(c->ev);
2751                 free(c);
2752                 return NULL;
2753         }
2754
2755         return c;
2756 }
2757
2758 struct comm_point* 
2759 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize,
2760         comm_point_callback_type* callback, void* callback_arg)
2761 {
2762         struct comm_point* c = (struct comm_point*)calloc(1,
2763                 sizeof(struct comm_point));
2764         short evbits;
2765         if(!c)
2766                 return NULL;
2767         c->ev = (struct internal_event*)calloc(1,
2768                 sizeof(struct internal_event));
2769         if(!c->ev) {
2770                 free(c);
2771                 return NULL;
2772         }
2773         c->ev->base = base;
2774         c->fd = fd;
2775         c->buffer = sldns_buffer_new(bufsize);
2776         if(!c->buffer) {
2777                 free(c->ev);
2778                 free(c);
2779                 return NULL;
2780         }
2781         c->timeout = NULL;
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;
2788         c->tcp_free = 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;
2796 #endif
2797 #ifdef USE_DNSCRYPT
2798         c->dnscrypt = 0;
2799         c->dnscrypt_buffer = c->buffer;
2800 #endif
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");
2809                 free(c->ev);
2810                 free(c);
2811                 return NULL;
2812         }
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);
2816                 free(c->ev);
2817                 free(c);
2818                 return NULL;
2819         }
2820         return c;
2821 }
2822
2823 struct comm_point* 
2824 comm_point_create_raw(struct comm_base* base, int fd, int writing, 
2825         comm_point_callback_type* callback, void* callback_arg)
2826 {
2827         struct comm_point* c = (struct comm_point*)calloc(1,
2828                 sizeof(struct comm_point));
2829         short evbits;
2830         if(!c)
2831                 return NULL;
2832         c->ev = (struct internal_event*)calloc(1,
2833                 sizeof(struct internal_event));
2834         if(!c->ev) {
2835                 free(c);
2836                 return NULL;
2837         }
2838         c->ev->base = base;
2839         c->fd = fd;
2840         c->buffer = NULL;
2841         c->timeout = NULL;
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;
2848         c->tcp_free = NULL;
2849         c->type = comm_raw;
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;
2856 #endif
2857 #ifdef USE_DNSCRYPT
2858         c->dnscrypt = 0;
2859         c->dnscrypt_buffer = c->buffer;
2860 #endif
2861         c->callback = callback;
2862         c->cb_arg = callback_arg;
2863         /* ub_event stuff */
2864         if(writing)
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");
2871                 free(c->ev);
2872                 free(c);
2873                 return NULL;
2874         }
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);
2878                 free(c->ev);
2879                 free(c);
2880                 return NULL;
2881         }
2882         return c;
2883 }
2884
2885 void 
2886 comm_point_close(struct comm_point* c)
2887 {
2888         if(!c)
2889                 return;
2890         if(c->fd != -1)
2891                 if(ub_event_del(c->ev->ev) != 0) {
2892                         log_err("could not event_del on close");
2893                 }
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);
2897 #ifndef USE_WINSOCK
2898                 close(c->fd);
2899 #else
2900                 closesocket(c->fd);
2901 #endif
2902         }
2903         c->fd = -1;
2904 }
2905
2906 void 
2907 comm_point_delete(struct comm_point* c)
2908 {
2909         if(!c) 
2910                 return;
2911         if((c->type == comm_tcp || c->type == comm_http) && c->ssl) {
2912 #ifdef HAVE_SSL
2913                 SSL_shutdown(c->ssl);
2914                 SSL_free(c->ssl);
2915 #endif
2916         }
2917         comm_point_close(c);
2918         if(c->tcp_handlers) {
2919                 int i;
2920                 for(i=0; i<c->max_tcp_count; i++)
2921                         comm_point_delete(c->tcp_handlers[i]);
2922                 free(c->tcp_handlers);
2923         }
2924         free(c->timeout);
2925         if(c->type == comm_tcp || c->type == comm_local || c->type == comm_http) {
2926                 sldns_buffer_free(c->buffer);
2927 #ifdef USE_DNSCRYPT
2928                 if(c->dnscrypt && c->dnscrypt_buffer != c->buffer) {
2929                         sldns_buffer_free(c->dnscrypt_buffer);
2930                 }
2931 #endif
2932         }
2933         ub_event_free(c->ev->ev);
2934         free(c->ev);
2935         free(c);
2936 }
2937
2938 void 
2939 comm_point_send_reply(struct comm_reply *repinfo)
2940 {
2941         struct sldns_buffer* buffer;
2942         log_assert(repinfo && repinfo->c);
2943 #ifdef USE_DNSCRYPT
2944         buffer = repinfo->c->dnscrypt_buffer;
2945         if(!dnsc_handle_uncurved_request(repinfo)) {
2946                 return;
2947         }
2948 #else
2949         buffer = repinfo->c->buffer;
2950 #endif
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);
2956                 else
2957                         comm_point_send_udp_msg(repinfo->c, buffer,
2958                         (struct sockaddr*)&repinfo->addr, repinfo->addrlen);
2959 #ifdef USE_DNSTAP
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);
2964 #endif
2965         } else {
2966 #ifdef USE_DNSTAP
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);
2971 #endif
2972                 comm_point_start_listening(repinfo->c, -1,
2973                         repinfo->c->tcp_timeout_msec);
2974         }
2975 }
2976
2977 void 
2978 comm_point_drop_reply(struct comm_reply* repinfo)
2979 {
2980         if(!repinfo)
2981                 return;
2982         log_assert(repinfo && repinfo->c);
2983         log_assert(repinfo->c->type != comm_tcp_accept);
2984         if(repinfo->c->type == comm_udp)
2985                 return;
2986         reclaim_tcp_handler(repinfo->c);
2987 }
2988
2989 void 
2990 comm_point_stop_listening(struct comm_point* c)
2991 {
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");
2995         }
2996 }
2997
2998 void 
2999 comm_point_start_listening(struct comm_point* c, int newfd, int msec)
3000 {
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. */
3005                 return;
3006         }
3007         if(msec != -1 && msec != 0) {
3008                 if(!c->timeout) {
3009                         c->timeout = (struct timeval*)malloc(sizeof(
3010                                 struct timeval));
3011                         if(!c->timeout) {
3012                                 log_err("cpsl: malloc failed. No net read.");
3013                                 return;
3014                         }
3015                 }
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 */
3021         }
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);
3027         }
3028         if(newfd != -1) {
3029                 if(c->fd != -1) {
3030 #ifndef USE_WINSOCK
3031                         close(c->fd);
3032 #else
3033                         closesocket(c->fd);
3034 #endif
3035                 }
3036                 c->fd = newfd;
3037                 ub_event_set_fd(c->ev->ev, c->fd);
3038         }
3039         if(ub_event_add(c->ev->ev, msec==0?NULL:c->timeout) != 0) {
3040                 log_err("event_add failed. in cpsl.");
3041         }
3042 }
3043
3044 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr)
3045 {
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");
3049         }
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.");
3055         }
3056 }
3057
3058 size_t comm_point_get_mem(struct comm_point* c)
3059 {
3060         size_t s;
3061         if(!c) 
3062                 return 0;
3063         s = sizeof(*c) + sizeof(*c->ev);
3064         if(c->timeout) 
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);
3068 #ifdef USE_DNSCRYPT
3069                 s += sizeof(*c->dnscrypt_buffer);
3070                 if(c->buffer != c->dnscrypt_buffer) {
3071                         s += sldns_buffer_capacity(c->dnscrypt_buffer);
3072                 }
3073 #endif
3074         }
3075         if(c->type == comm_tcp_accept) {
3076                 int i;
3077                 for(i=0; i<c->max_tcp_count; i++)
3078                         s += comm_point_get_mem(c->tcp_handlers[i]);
3079         }
3080         return s;
3081 }
3082
3083 struct comm_timer* 
3084 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
3085 {
3086         struct internal_timer *tm = (struct internal_timer*)calloc(1,
3087                 sizeof(struct internal_timer));
3088         if(!tm) {
3089                 log_err("malloc failed");
3090                 return NULL;
3091         }
3092         tm->super.ev_timer = tm;
3093         tm->base = base;
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.");
3100                 free(tm);
3101                 return NULL;
3102         }
3103         return &tm->super;
3104 }
3105
3106 void 
3107 comm_timer_disable(struct comm_timer* timer)
3108 {
3109         if(!timer)
3110                 return;
3111         ub_timer_del(timer->ev_timer->ev);
3112         timer->ev_timer->enabled = 0;
3113 }
3114
3115 void 
3116 comm_timer_set(struct comm_timer* timer, struct timeval* tv)
3117 {
3118         log_assert(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;
3125 }
3126
3127 void 
3128 comm_timer_delete(struct comm_timer* timer)
3129 {
3130         if(!timer)
3131                 return;
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)
3135          */
3136         ub_event_free(timer->ev_timer->ev);
3137         free(timer->ev_timer);
3138 }
3139
3140 void 
3141 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
3142 {
3143         struct comm_timer* tm = (struct comm_timer*)arg;
3144         if(!(event&UB_EV_TIMEOUT))
3145                 return;
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);
3150 }
3151
3152 int 
3153 comm_timer_is_set(struct comm_timer* timer)
3154 {
3155         return (int)timer->ev_timer->enabled;
3156 }
3157
3158 size_t 
3159 comm_timer_get_mem(struct comm_timer* ATTR_UNUSED(timer))
3160 {
3161         return sizeof(struct internal_timer);
3162 }
3163
3164 struct comm_signal* 
3165 comm_signal_create(struct comm_base* base,
3166         void (*callback)(int, void*), void* cb_arg)
3167 {
3168         struct comm_signal* com = (struct comm_signal*)malloc(
3169                 sizeof(struct comm_signal));
3170         if(!com) {
3171                 log_err("malloc failed");
3172                 return NULL;
3173         }
3174         com->base = base;
3175         com->callback = callback;
3176         com->cb_arg = cb_arg;
3177         com->ev_signal = NULL;
3178         return com;
3179 }
3180
3181 void 
3182 comm_signal_callback(int sig, short event, void* arg)
3183 {
3184         struct comm_signal* comsig = (struct comm_signal*)arg;
3185         if(!(event & UB_EV_SIGNAL))
3186                 return;
3187         ub_comm_base_now(comsig->base);
3188         fptr_ok(fptr_whitelist_comm_signal(comsig->callback));
3189         (*comsig->callback)(sig, comsig->cb_arg);
3190 }
3191
3192 int 
3193 comm_signal_bind(struct comm_signal* comsig, int sig)
3194 {
3195         struct internal_signal* entry = (struct internal_signal*)calloc(1, 
3196                 sizeof(struct internal_signal));
3197         if(!entry) {
3198                 log_err("malloc failed");
3199                 return 0;
3200         }
3201         log_assert(comsig);
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");
3207                 free(entry);
3208                 return 0;
3209         }
3210         if(ub_signal_add(entry->ev, NULL) != 0) {
3211                 log_err("Could not add signal handler");
3212                 ub_event_free(entry->ev);
3213                 free(entry);
3214                 return 0;
3215         }
3216         /* link into list */
3217         entry->next = comsig->ev_signal;
3218         comsig->ev_signal = entry;
3219         return 1;
3220 }
3221
3222 void 
3223 comm_signal_delete(struct comm_signal* comsig)
3224 {
3225         struct internal_signal* p, *np;
3226         if(!comsig)
3227                 return;
3228         p=comsig->ev_signal;
3229         while(p) {
3230                 np = p->next;
3231                 ub_signal_del(p->ev);
3232                 ub_event_free(p->ev);
3233                 free(p);
3234                 p = np;
3235         }
3236         free(comsig);
3237 }