xref: /freebsd/contrib/unbound/util/netevent.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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/tcp_conn_limit.h"
47 #include "util/fptr_wlist.h"
48 #include "util/proxy_protocol.h"
49 #include "util/timeval_func.h"
50 #include "sldns/pkthdr.h"
51 #include "sldns/sbuffer.h"
52 #include "sldns/str2wire.h"
53 #include "dnstap/dnstap.h"
54 #include "dnscrypt/dnscrypt.h"
55 #include "services/listen_dnsport.h"
56 #ifdef HAVE_SYS_TYPES_H
57 #include <sys/types.h>
58 #endif
59 #ifdef HAVE_SYS_SOCKET_H
60 #include <sys/socket.h>
61 #endif
62 #ifdef HAVE_NETDB_H
63 #include <netdb.h>
64 #endif
65 #ifdef HAVE_POLL_H
66 #include <poll.h>
67 #endif
68 
69 #ifdef HAVE_OPENSSL_SSL_H
70 #include <openssl/ssl.h>
71 #endif
72 #ifdef HAVE_OPENSSL_ERR_H
73 #include <openssl/err.h>
74 #endif
75 #ifdef HAVE_LINUX_NET_TSTAMP_H
76 #include <linux/net_tstamp.h>
77 #endif
78 /* -------- Start of local definitions -------- */
79 /** if CMSG_ALIGN is not defined on this platform, a workaround */
80 #ifndef CMSG_ALIGN
81 #  ifdef __CMSG_ALIGN
82 #    define CMSG_ALIGN(n) __CMSG_ALIGN(n)
83 #  elif defined(CMSG_DATA_ALIGN)
84 #    define CMSG_ALIGN _CMSG_DATA_ALIGN
85 #  else
86 #    define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
87 #  endif
88 #endif
89 
90 /** if CMSG_LEN is not defined on this platform, a workaround */
91 #ifndef CMSG_LEN
92 #  define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
93 #endif
94 
95 /** if CMSG_SPACE is not defined on this platform, a workaround */
96 #ifndef CMSG_SPACE
97 #  ifdef _CMSG_HDR_ALIGN
98 #    define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)))
99 #  else
100 #    define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr)))
101 #  endif
102 #endif
103 
104 /** The TCP writing query timeout in milliseconds */
105 #define TCP_QUERY_TIMEOUT 120000
106 /** The minimum actual TCP timeout to use, regardless of what we advertise,
107  * in msec */
108 #define TCP_QUERY_TIMEOUT_MINIMUM 200
109 
110 #ifndef NONBLOCKING_IS_BROKEN
111 /** number of UDP reads to perform per read indication from select */
112 #define NUM_UDP_PER_SELECT 100
113 #else
114 #define NUM_UDP_PER_SELECT 1
115 #endif
116 
117 /** timeout in millisec to wait for write to unblock, packets dropped after.*/
118 #define SEND_BLOCKED_WAIT_TIMEOUT 200
119 /** max number of times to wait for write to unblock, packets dropped after.*/
120 #define SEND_BLOCKED_MAX_RETRY 5
121 
122 /** Let's make timestamping code cleaner and redefine SO_TIMESTAMP* */
123 #ifndef SO_TIMESTAMP
124 #define SO_TIMESTAMP 29
125 #endif
126 #ifndef SO_TIMESTAMPNS
127 #define SO_TIMESTAMPNS 35
128 #endif
129 #ifndef SO_TIMESTAMPING
130 #define SO_TIMESTAMPING 37
131 #endif
132 /**
133  * The internal event structure for keeping ub_event info for the event.
134  * Possibly other structures (list, tree) this is part of.
135  */
136 struct internal_event {
137 	/** the comm base */
138 	struct comm_base* base;
139 	/** ub_event event type */
140 	struct ub_event* ev;
141 };
142 
143 /**
144  * Internal base structure, so that every thread has its own events.
145  */
146 struct internal_base {
147 	/** ub_event event_base type. */
148 	struct ub_event_base* base;
149 	/** seconds time pointer points here */
150 	time_t secs;
151 	/** timeval with current time */
152 	struct timeval now;
153 	/** the event used for slow_accept timeouts */
154 	struct ub_event* slow_accept;
155 	/** true if slow_accept is enabled */
156 	int slow_accept_enabled;
157 	/** last log time for slow logging of file descriptor errors */
158 	time_t last_slow_log;
159 	/** last log time for slow logging of write wait failures */
160 	time_t last_writewait_log;
161 };
162 
163 /**
164  * Internal timer structure, to store timer event in.
165  */
166 struct internal_timer {
167 	/** the super struct from which derived */
168 	struct comm_timer super;
169 	/** the comm base */
170 	struct comm_base* base;
171 	/** ub_event event type */
172 	struct ub_event* ev;
173 	/** is timer enabled */
174 	uint8_t enabled;
175 };
176 
177 /**
178  * Internal signal structure, to store signal event in.
179  */
180 struct internal_signal {
181 	/** ub_event event type */
182 	struct ub_event* ev;
183 	/** next in signal list */
184 	struct internal_signal* next;
185 };
186 
187 /** create a tcp handler with a parent */
188 static struct comm_point* comm_point_create_tcp_handler(
189 	struct comm_base *base, struct comm_point* parent, size_t bufsize,
190 	struct sldns_buffer* spoolbuf, comm_point_callback_type* callback,
191 	void* callback_arg, struct unbound_socket* socket);
192 
193 /* -------- End of local definitions -------- */
194 
195 struct comm_base*
196 comm_base_create(int sigs)
197 {
198 	struct comm_base* b = (struct comm_base*)calloc(1,
199 		sizeof(struct comm_base));
200 	const char *evnm="event", *evsys="", *evmethod="";
201 
202 	if(!b)
203 		return NULL;
204 	b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
205 	if(!b->eb) {
206 		free(b);
207 		return NULL;
208 	}
209 	b->eb->base = ub_default_event_base(sigs, &b->eb->secs, &b->eb->now);
210 	if(!b->eb->base) {
211 		free(b->eb);
212 		free(b);
213 		return NULL;
214 	}
215 	ub_comm_base_now(b);
216 	ub_get_event_sys(b->eb->base, &evnm, &evsys, &evmethod);
217 	verbose(VERB_ALGO, "%s %s uses %s method.", evnm, evsys, evmethod);
218 	return b;
219 }
220 
221 struct comm_base*
222 comm_base_create_event(struct ub_event_base* base)
223 {
224 	struct comm_base* b = (struct comm_base*)calloc(1,
225 		sizeof(struct comm_base));
226 	if(!b)
227 		return NULL;
228 	b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
229 	if(!b->eb) {
230 		free(b);
231 		return NULL;
232 	}
233 	b->eb->base = base;
234 	ub_comm_base_now(b);
235 	return b;
236 }
237 
238 void
239 comm_base_delete(struct comm_base* b)
240 {
241 	if(!b)
242 		return;
243 	if(b->eb->slow_accept_enabled) {
244 		if(ub_event_del(b->eb->slow_accept) != 0) {
245 			log_err("could not event_del slow_accept");
246 		}
247 		ub_event_free(b->eb->slow_accept);
248 	}
249 	ub_event_base_free(b->eb->base);
250 	b->eb->base = NULL;
251 	free(b->eb);
252 	free(b);
253 }
254 
255 void
256 comm_base_delete_no_base(struct comm_base* b)
257 {
258 	if(!b)
259 		return;
260 	if(b->eb->slow_accept_enabled) {
261 		if(ub_event_del(b->eb->slow_accept) != 0) {
262 			log_err("could not event_del slow_accept");
263 		}
264 		ub_event_free(b->eb->slow_accept);
265 	}
266 	b->eb->base = NULL;
267 	free(b->eb);
268 	free(b);
269 }
270 
271 void
272 comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv)
273 {
274 	*tt = &b->eb->secs;
275 	*tv = &b->eb->now;
276 }
277 
278 void
279 comm_base_dispatch(struct comm_base* b)
280 {
281 	int retval;
282 	retval = ub_event_base_dispatch(b->eb->base);
283 	if(retval < 0) {
284 		fatal_exit("event_dispatch returned error %d, "
285 			"errno is %s", retval, strerror(errno));
286 	}
287 }
288 
289 void comm_base_exit(struct comm_base* b)
290 {
291 	if(ub_event_base_loopexit(b->eb->base) != 0) {
292 		log_err("Could not loopexit");
293 	}
294 }
295 
296 void comm_base_set_slow_accept_handlers(struct comm_base* b,
297 	void (*stop_acc)(void*), void (*start_acc)(void*), void* arg)
298 {
299 	b->stop_accept = stop_acc;
300 	b->start_accept = start_acc;
301 	b->cb_arg = arg;
302 }
303 
304 struct ub_event_base* comm_base_internal(struct comm_base* b)
305 {
306 	return b->eb->base;
307 }
308 
309 /** see if errno for udp has to be logged or not uses globals */
310 static int
311 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
312 {
313 	/* do not log transient errors (unless high verbosity) */
314 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN)
315 	switch(errno) {
316 #  ifdef ENETUNREACH
317 		case ENETUNREACH:
318 #  endif
319 #  ifdef EHOSTDOWN
320 		case EHOSTDOWN:
321 #  endif
322 #  ifdef EHOSTUNREACH
323 		case EHOSTUNREACH:
324 #  endif
325 #  ifdef ENETDOWN
326 		case ENETDOWN:
327 #  endif
328 		case EPERM:
329 		case EACCES:
330 			if(verbosity < VERB_ALGO)
331 				return 0;
332 		default:
333 			break;
334 	}
335 #endif
336 	/* permission denied is gotten for every send if the
337 	 * network is disconnected (on some OS), squelch it */
338 	if( ((errno == EPERM)
339 #  ifdef EADDRNOTAVAIL
340 		/* 'Cannot assign requested address' also when disconnected */
341 		|| (errno == EADDRNOTAVAIL)
342 #  endif
343 		) && verbosity < VERB_ALGO)
344 		return 0;
345 #  ifdef EADDRINUSE
346 	/* If SO_REUSEADDR is set, we could try to connect to the same server
347 	 * from the same source port twice. */
348 	if(errno == EADDRINUSE && verbosity < VERB_DETAIL)
349 		return 0;
350 #  endif
351 	/* squelch errors where people deploy AAAA ::ffff:bla for
352 	 * authority servers, which we try for intranets. */
353 	if(errno == EINVAL && addr_is_ip4mapped(
354 		(struct sockaddr_storage*)addr, addrlen) &&
355 		verbosity < VERB_DETAIL)
356 		return 0;
357 	/* SO_BROADCAST sockopt can give access to 255.255.255.255,
358 	 * but a dns cache does not need it. */
359 	if(errno == EACCES && addr_is_broadcast(
360 		(struct sockaddr_storage*)addr, addrlen) &&
361 		verbosity < VERB_DETAIL)
362 		return 0;
363 	return 1;
364 }
365 
366 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
367 {
368 	return udp_send_errno_needs_log(addr, addrlen);
369 }
370 
371 /* send a UDP reply */
372 int
373 comm_point_send_udp_msg(struct comm_point *c, sldns_buffer* packet,
374 	struct sockaddr* addr, socklen_t addrlen, int is_connected)
375 {
376 	ssize_t sent;
377 	log_assert(c->fd != -1);
378 #ifdef UNBOUND_DEBUG
379 	if(sldns_buffer_remaining(packet) == 0)
380 		log_err("error: send empty UDP packet");
381 #endif
382 	log_assert(addr && addrlen > 0);
383 	if(!is_connected) {
384 		sent = sendto(c->fd, (void*)sldns_buffer_begin(packet),
385 			sldns_buffer_remaining(packet), 0,
386 			addr, addrlen);
387 	} else {
388 		sent = send(c->fd, (void*)sldns_buffer_begin(packet),
389 			sldns_buffer_remaining(packet), 0);
390 	}
391 	if(sent == -1) {
392 		/* try again and block, waiting for IO to complete,
393 		 * we want to send the answer, and we will wait for
394 		 * the ethernet interface buffer to have space. */
395 #ifndef USE_WINSOCK
396 		if(errno == EAGAIN || errno == EINTR ||
397 #  ifdef EWOULDBLOCK
398 			errno == EWOULDBLOCK ||
399 #  endif
400 			errno == ENOBUFS) {
401 #else
402 		if(WSAGetLastError() == WSAEINPROGRESS ||
403 			WSAGetLastError() == WSAEINTR ||
404 			WSAGetLastError() == WSAENOBUFS ||
405 			WSAGetLastError() == WSAEWOULDBLOCK) {
406 #endif
407 			int retries = 0;
408 			/* if we set the fd blocking, other threads suddenly
409 			 * have a blocking fd that they operate on */
410 			while(sent == -1 && retries < SEND_BLOCKED_MAX_RETRY && (
411 #ifndef USE_WINSOCK
412 				errno == EAGAIN || errno == EINTR ||
413 #  ifdef EWOULDBLOCK
414 				errno == EWOULDBLOCK ||
415 #  endif
416 				errno == ENOBUFS
417 #else
418 				WSAGetLastError() == WSAEINPROGRESS ||
419 				WSAGetLastError() == WSAEINTR ||
420 				WSAGetLastError() == WSAENOBUFS ||
421 				WSAGetLastError() == WSAEWOULDBLOCK
422 #endif
423 			)) {
424 #if defined(HAVE_POLL) || defined(USE_WINSOCK)
425 				int send_nobufs = (
426 #ifndef USE_WINSOCK
427 					errno == ENOBUFS
428 #else
429 					WSAGetLastError() == WSAENOBUFS
430 #endif
431 				);
432 				struct pollfd p;
433 				int pret;
434 				memset(&p, 0, sizeof(p));
435 				p.fd = c->fd;
436 				p.events = POLLOUT | POLLERR | POLLHUP;
437 #  ifndef USE_WINSOCK
438 				pret = poll(&p, 1, SEND_BLOCKED_WAIT_TIMEOUT);
439 #  else
440 				pret = WSAPoll(&p, 1,
441 					SEND_BLOCKED_WAIT_TIMEOUT);
442 #  endif
443 				if(pret == 0) {
444 					/* timer expired */
445 					struct comm_base* b = c->ev->base;
446 					if(b->eb->last_writewait_log+SLOW_LOG_TIME <=
447 						b->eb->secs) {
448 						b->eb->last_writewait_log = b->eb->secs;
449 						verbose(VERB_OPS, "send udp blocked "
450 							"for long, dropping packet.");
451 					}
452 					return 0;
453 				} else if(pret < 0 &&
454 #ifndef USE_WINSOCK
455 					errno != EAGAIN && errno != EINTR &&
456 #  ifdef EWOULDBLOCK
457 					errno != EWOULDBLOCK &&
458 #  endif
459 					errno != ENOBUFS
460 #else
461 					WSAGetLastError() != WSAEINPROGRESS &&
462 					WSAGetLastError() != WSAEINTR &&
463 					WSAGetLastError() != WSAENOBUFS &&
464 					WSAGetLastError() != WSAEWOULDBLOCK
465 #endif
466 					) {
467 					log_err("poll udp out failed: %s",
468 						sock_strerror(errno));
469 					return 0;
470 				} else if((pret < 0 &&
471 #ifndef USE_WINSOCK
472 					errno == ENOBUFS
473 #else
474 					WSAGetLastError() == WSAENOBUFS
475 #endif
476 					) || (send_nobufs && retries > 0)) {
477 					/* ENOBUFS, and poll returned without
478 					 * a timeout. Or the retried send call
479 					 * returned ENOBUFS. It is good to
480 					 * wait a bit for the error to clear. */
481 					/* The timeout is 20*(2^(retries+1)),
482 					 * it increases exponentially, starting
483 					 * at 40 msec. After 5 tries, 1240 msec
484 					 * have passed in total, when poll
485 					 * returned the error, and 1200 msec
486 					 * when send returned the errors. */
487 #ifndef USE_WINSOCK
488 					pret = poll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1));
489 #else
490 					pret = WSAPoll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1));
491 #endif
492 					if(pret < 0 &&
493 #ifndef USE_WINSOCK
494 						errno != EAGAIN && errno != EINTR &&
495 #  ifdef EWOULDBLOCK
496 						errno != EWOULDBLOCK &&
497 #  endif
498 						errno != ENOBUFS
499 #else
500 						WSAGetLastError() != WSAEINPROGRESS &&
501 						WSAGetLastError() != WSAEINTR &&
502 						WSAGetLastError() != WSAENOBUFS &&
503 						WSAGetLastError() != WSAEWOULDBLOCK
504 #endif
505 					) {
506 						log_err("poll udp out timer failed: %s",
507 							sock_strerror(errno));
508 					}
509 				}
510 #endif /* defined(HAVE_POLL) || defined(USE_WINSOCK) */
511 				retries++;
512 				if (!is_connected) {
513 					sent = sendto(c->fd, (void*)sldns_buffer_begin(packet),
514 						sldns_buffer_remaining(packet), 0,
515 						addr, addrlen);
516 				} else {
517 					sent = send(c->fd, (void*)sldns_buffer_begin(packet),
518 						sldns_buffer_remaining(packet), 0);
519 				}
520 			}
521 		}
522 	}
523 	if(sent == -1) {
524 		if(!udp_send_errno_needs_log(addr, addrlen))
525 			return 0;
526 		if (!is_connected) {
527 			verbose(VERB_OPS, "sendto failed: %s", sock_strerror(errno));
528 		} else {
529 			verbose(VERB_OPS, "send failed: %s", sock_strerror(errno));
530 		}
531 		if(addr)
532 			log_addr(VERB_OPS, "remote address is",
533 				(struct sockaddr_storage*)addr, addrlen);
534 		return 0;
535 	} else if((size_t)sent != sldns_buffer_remaining(packet)) {
536 		log_err("sent %d in place of %d bytes",
537 			(int)sent, (int)sldns_buffer_remaining(packet));
538 		return 0;
539 	}
540 	return 1;
541 }
542 
543 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && (defined(HAVE_RECVMSG) || defined(HAVE_SENDMSG))
544 /** print debug ancillary info */
545 static void p_ancil(const char* str, struct comm_reply* r)
546 {
547 	if(r->srctype != 4 && r->srctype != 6) {
548 		log_info("%s: unknown srctype %d", str, r->srctype);
549 		return;
550 	}
551 
552 	if(r->srctype == 6) {
553 #ifdef IPV6_PKTINFO
554 		char buf[1024];
555 		if(inet_ntop(AF_INET6, &r->pktinfo.v6info.ipi6_addr,
556 			buf, (socklen_t)sizeof(buf)) == 0) {
557 			(void)strlcpy(buf, "(inet_ntop error)", sizeof(buf));
558 		}
559 		buf[sizeof(buf)-1]=0;
560 		log_info("%s: %s %d", str, buf, r->pktinfo.v6info.ipi6_ifindex);
561 #endif
562 	} else if(r->srctype == 4) {
563 #ifdef IP_PKTINFO
564 		char buf1[1024], buf2[1024];
565 		if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_addr,
566 			buf1, (socklen_t)sizeof(buf1)) == 0) {
567 			(void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1));
568 		}
569 		buf1[sizeof(buf1)-1]=0;
570 #ifdef HAVE_STRUCT_IN_PKTINFO_IPI_SPEC_DST
571 		if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_spec_dst,
572 			buf2, (socklen_t)sizeof(buf2)) == 0) {
573 			(void)strlcpy(buf2, "(inet_ntop error)", sizeof(buf2));
574 		}
575 		buf2[sizeof(buf2)-1]=0;
576 #else
577 		buf2[0]=0;
578 #endif
579 		log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex,
580 			buf1, buf2);
581 #elif defined(IP_RECVDSTADDR)
582 		char buf1[1024];
583 		if(inet_ntop(AF_INET, &r->pktinfo.v4addr,
584 			buf1, (socklen_t)sizeof(buf1)) == 0) {
585 			(void)strlcpy(buf1, "(inet_ntop error)", sizeof(buf1));
586 		}
587 		buf1[sizeof(buf1)-1]=0;
588 		log_info("%s: %s", str, buf1);
589 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */
590 	}
591 }
592 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */
593 
594 /** send a UDP reply over specified interface*/
595 static int
596 comm_point_send_udp_msg_if(struct comm_point *c, sldns_buffer* packet,
597 	struct sockaddr* addr, socklen_t addrlen, struct comm_reply* r)
598 {
599 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG)
600 	ssize_t sent;
601 	struct msghdr msg;
602 	struct iovec iov[1];
603 	union {
604 		struct cmsghdr hdr;
605 		char buf[256];
606 	} control;
607 #ifndef S_SPLINT_S
608 	struct cmsghdr *cmsg;
609 #endif /* S_SPLINT_S */
610 
611 	log_assert(c->fd != -1);
612 #ifdef UNBOUND_DEBUG
613 	if(sldns_buffer_remaining(packet) == 0)
614 		log_err("error: send empty UDP packet");
615 #endif
616 	log_assert(addr && addrlen > 0);
617 
618 	msg.msg_name = addr;
619 	msg.msg_namelen = addrlen;
620 	iov[0].iov_base = sldns_buffer_begin(packet);
621 	iov[0].iov_len = sldns_buffer_remaining(packet);
622 	msg.msg_iov = iov;
623 	msg.msg_iovlen = 1;
624 	msg.msg_control = control.buf;
625 #ifndef S_SPLINT_S
626 	msg.msg_controllen = sizeof(control.buf);
627 #endif /* S_SPLINT_S */
628 	msg.msg_flags = 0;
629 
630 #ifndef S_SPLINT_S
631 	cmsg = CMSG_FIRSTHDR(&msg);
632 	if(r->srctype == 4) {
633 #ifdef IP_PKTINFO
634 		void* cmsg_data;
635 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
636 		log_assert(msg.msg_controllen <= sizeof(control.buf));
637 		cmsg->cmsg_level = IPPROTO_IP;
638 		cmsg->cmsg_type = IP_PKTINFO;
639 		memmove(CMSG_DATA(cmsg), &r->pktinfo.v4info,
640 			sizeof(struct in_pktinfo));
641 		/* unset the ifindex to not bypass the routing tables */
642 		cmsg_data = CMSG_DATA(cmsg);
643 		((struct in_pktinfo *) cmsg_data)->ipi_ifindex = 0;
644 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
645 		/* zero the padding bytes inserted by the CMSG_LEN */
646 		if(sizeof(struct in_pktinfo) < cmsg->cmsg_len)
647 			memset(((uint8_t*)(CMSG_DATA(cmsg))) +
648 				sizeof(struct in_pktinfo), 0, cmsg->cmsg_len
649 				- sizeof(struct in_pktinfo));
650 #elif defined(IP_SENDSRCADDR)
651 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
652 		log_assert(msg.msg_controllen <= sizeof(control.buf));
653 		cmsg->cmsg_level = IPPROTO_IP;
654 		cmsg->cmsg_type = IP_SENDSRCADDR;
655 		memmove(CMSG_DATA(cmsg), &r->pktinfo.v4addr,
656 			sizeof(struct in_addr));
657 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
658 		/* zero the padding bytes inserted by the CMSG_LEN */
659 		if(sizeof(struct in_addr) < cmsg->cmsg_len)
660 			memset(((uint8_t*)(CMSG_DATA(cmsg))) +
661 				sizeof(struct in_addr), 0, cmsg->cmsg_len
662 				- sizeof(struct in_addr));
663 #else
664 		verbose(VERB_ALGO, "no IP_PKTINFO or IP_SENDSRCADDR");
665 		msg.msg_control = NULL;
666 #endif /* IP_PKTINFO or IP_SENDSRCADDR */
667 	} else if(r->srctype == 6) {
668 		void* cmsg_data;
669 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
670 		log_assert(msg.msg_controllen <= sizeof(control.buf));
671 		cmsg->cmsg_level = IPPROTO_IPV6;
672 		cmsg->cmsg_type = IPV6_PKTINFO;
673 		memmove(CMSG_DATA(cmsg), &r->pktinfo.v6info,
674 			sizeof(struct in6_pktinfo));
675 		/* unset the ifindex to not bypass the routing tables */
676 		cmsg_data = CMSG_DATA(cmsg);
677 		((struct in6_pktinfo *) cmsg_data)->ipi6_ifindex = 0;
678 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
679 		/* zero the padding bytes inserted by the CMSG_LEN */
680 		if(sizeof(struct in6_pktinfo) < cmsg->cmsg_len)
681 			memset(((uint8_t*)(CMSG_DATA(cmsg))) +
682 				sizeof(struct in6_pktinfo), 0, cmsg->cmsg_len
683 				- sizeof(struct in6_pktinfo));
684 	} else {
685 		/* try to pass all 0 to use default route */
686 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
687 		log_assert(msg.msg_controllen <= sizeof(control.buf));
688 		cmsg->cmsg_level = IPPROTO_IPV6;
689 		cmsg->cmsg_type = IPV6_PKTINFO;
690 		memset(CMSG_DATA(cmsg), 0, sizeof(struct in6_pktinfo));
691 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
692 		/* zero the padding bytes inserted by the CMSG_LEN */
693 		if(sizeof(struct in6_pktinfo) < cmsg->cmsg_len)
694 			memset(((uint8_t*)(CMSG_DATA(cmsg))) +
695 				sizeof(struct in6_pktinfo), 0, cmsg->cmsg_len
696 				- sizeof(struct in6_pktinfo));
697 	}
698 #endif /* S_SPLINT_S */
699 	if(verbosity >= VERB_ALGO && r->srctype != 0)
700 		p_ancil("send_udp over interface", r);
701 	sent = sendmsg(c->fd, &msg, 0);
702 	if(sent == -1) {
703 		/* try again and block, waiting for IO to complete,
704 		 * we want to send the answer, and we will wait for
705 		 * the ethernet interface buffer to have space. */
706 #ifndef USE_WINSOCK
707 		if(errno == EAGAIN || errno == EINTR ||
708 #  ifdef EWOULDBLOCK
709 			errno == EWOULDBLOCK ||
710 #  endif
711 			errno == ENOBUFS) {
712 #else
713 		if(WSAGetLastError() == WSAEINPROGRESS ||
714 			WSAGetLastError() == WSAEINTR ||
715 			WSAGetLastError() == WSAENOBUFS ||
716 			WSAGetLastError() == WSAEWOULDBLOCK) {
717 #endif
718 			int retries = 0;
719 			while(sent == -1 && retries < SEND_BLOCKED_MAX_RETRY && (
720 #ifndef USE_WINSOCK
721 				errno == EAGAIN || errno == EINTR ||
722 #  ifdef EWOULDBLOCK
723 				errno == EWOULDBLOCK ||
724 #  endif
725 				errno == ENOBUFS
726 #else
727 				WSAGetLastError() == WSAEINPROGRESS ||
728 				WSAGetLastError() == WSAEINTR ||
729 				WSAGetLastError() == WSAENOBUFS ||
730 				WSAGetLastError() == WSAEWOULDBLOCK
731 #endif
732 			)) {
733 #if defined(HAVE_POLL) || defined(USE_WINSOCK)
734 				int send_nobufs = (
735 #ifndef USE_WINSOCK
736 					errno == ENOBUFS
737 #else
738 					WSAGetLastError() == WSAENOBUFS
739 #endif
740 				);
741 				struct pollfd p;
742 				int pret;
743 				memset(&p, 0, sizeof(p));
744 				p.fd = c->fd;
745 				p.events = POLLOUT | POLLERR | POLLHUP;
746 #  ifndef USE_WINSOCK
747 				pret = poll(&p, 1, SEND_BLOCKED_WAIT_TIMEOUT);
748 #  else
749 				pret = WSAPoll(&p, 1,
750 					SEND_BLOCKED_WAIT_TIMEOUT);
751 #  endif
752 				if(pret == 0) {
753 					/* timer expired */
754 					struct comm_base* b = c->ev->base;
755 					if(b->eb->last_writewait_log+SLOW_LOG_TIME <=
756 						b->eb->secs) {
757 						b->eb->last_writewait_log = b->eb->secs;
758 						verbose(VERB_OPS, "send udp blocked "
759 							"for long, dropping packet.");
760 					}
761 					return 0;
762 				} else if(pret < 0 &&
763 #ifndef USE_WINSOCK
764 					errno != EAGAIN && errno != EINTR &&
765 #  ifdef EWOULDBLOCK
766 					errno != EWOULDBLOCK &&
767 #  endif
768 					errno != ENOBUFS
769 #else
770 					WSAGetLastError() != WSAEINPROGRESS &&
771 					WSAGetLastError() != WSAEINTR &&
772 					WSAGetLastError() != WSAENOBUFS &&
773 					WSAGetLastError() != WSAEWOULDBLOCK
774 #endif
775 					) {
776 					log_err("poll udp out failed: %s",
777 						sock_strerror(errno));
778 					return 0;
779 				} else if((pret < 0 &&
780 #ifndef USE_WINSOCK
781 					errno == ENOBUFS
782 #else
783 					WSAGetLastError() == WSAENOBUFS
784 #endif
785 					) || (send_nobufs && retries > 0)) {
786 					/* ENOBUFS, and poll returned without
787 					 * a timeout. Or the retried send call
788 					 * returned ENOBUFS. It is good to
789 					 * wait a bit for the error to clear. */
790 					/* The timeout is 20*(2^(retries+1)),
791 					 * it increases exponentially, starting
792 					 * at 40 msec. After 5 tries, 1240 msec
793 					 * have passed in total, when poll
794 					 * returned the error, and 1200 msec
795 					 * when send returned the errors. */
796 #ifndef USE_WINSOCK
797 					pret = poll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1));
798 #else
799 					pret = WSAPoll(NULL, 0, (SEND_BLOCKED_WAIT_TIMEOUT/10)<<(retries+1));
800 #endif
801 					if(pret < 0 &&
802 #ifndef USE_WINSOCK
803 						errno != EAGAIN && errno != EINTR &&
804 #  ifdef EWOULDBLOCK
805 						errno != EWOULDBLOCK &&
806 #  endif
807 						errno != ENOBUFS
808 #else
809 						WSAGetLastError() != WSAEINPROGRESS &&
810 						WSAGetLastError() != WSAEINTR &&
811 						WSAGetLastError() != WSAENOBUFS &&
812 						WSAGetLastError() != WSAEWOULDBLOCK
813 #endif
814 					) {
815 						log_err("poll udp out timer failed: %s",
816 							sock_strerror(errno));
817 					}
818 				}
819 #endif /* defined(HAVE_POLL) || defined(USE_WINSOCK) */
820 				retries++;
821 				sent = sendmsg(c->fd, &msg, 0);
822 			}
823 		}
824 	}
825 	if(sent == -1) {
826 		if(!udp_send_errno_needs_log(addr, addrlen))
827 			return 0;
828 		verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno));
829 		log_addr(VERB_OPS, "remote address is",
830 			(struct sockaddr_storage*)addr, addrlen);
831 #ifdef __NetBSD__
832 		/* netbsd 7 has IP_PKTINFO for recv but not send */
833 		if(errno == EINVAL && r->srctype == 4)
834 			log_err("sendmsg: No support for sendmsg(IP_PKTINFO). "
835 				"Please disable interface-automatic");
836 #endif
837 		return 0;
838 	} else if((size_t)sent != sldns_buffer_remaining(packet)) {
839 		log_err("sent %d in place of %d bytes",
840 			(int)sent, (int)sldns_buffer_remaining(packet));
841 		return 0;
842 	}
843 	return 1;
844 #else
845 	(void)c;
846 	(void)packet;
847 	(void)addr;
848 	(void)addrlen;
849 	(void)r;
850 	log_err("sendmsg: IPV6_PKTINFO not supported");
851 	return 0;
852 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */
853 }
854 
855 /** return true is UDP receive error needs to be logged */
856 static int udp_recv_needs_log(int err)
857 {
858 	switch(err) {
859 	case EACCES: /* some hosts send ICMP 'Permission Denied' */
860 #ifndef USE_WINSOCK
861 	case ECONNREFUSED:
862 #  ifdef ENETUNREACH
863 	case ENETUNREACH:
864 #  endif
865 #  ifdef EHOSTDOWN
866 	case EHOSTDOWN:
867 #  endif
868 #  ifdef EHOSTUNREACH
869 	case EHOSTUNREACH:
870 #  endif
871 #  ifdef ENETDOWN
872 	case ENETDOWN:
873 #  endif
874 #else /* USE_WINSOCK */
875 	case WSAECONNREFUSED:
876 	case WSAENETUNREACH:
877 	case WSAEHOSTDOWN:
878 	case WSAEHOSTUNREACH:
879 	case WSAENETDOWN:
880 #endif
881 		if(verbosity >= VERB_ALGO)
882 			return 1;
883 		return 0;
884 	default:
885 		break;
886 	}
887 	return 1;
888 }
889 
890 /** Parses the PROXYv2 header from buf and updates the comm_reply struct.
891  *  Returns 1 on success, 0 on failure. */
892 static int consume_pp2_header(struct sldns_buffer* buf, struct comm_reply* rep,
893 	int stream) {
894 	size_t size;
895 	struct pp2_header *header = pp2_read_header(buf);
896 	if(header == NULL) return 0;
897 	size = PP2_HEADER_SIZE + ntohs(header->len);
898 	if((header->ver_cmd & 0xF) == PP2_CMD_LOCAL) {
899 		/* A connection from the proxy itself.
900 		 * No need to do anything with addresses. */
901 		goto done;
902 	}
903 	if(header->fam_prot == 0x00) {
904 		/* Unspecified family and protocol. This could be used for
905 		 * health checks by proxies.
906 		 * No need to do anything with addresses. */
907 		goto done;
908 	}
909 	/* Read the proxied address */
910 	switch(header->fam_prot) {
911 		case 0x11: /* AF_INET|STREAM */
912 		case 0x12: /* AF_INET|DGRAM */
913 			{
914 			struct sockaddr_in* addr =
915 				(struct sockaddr_in*)&rep->client_addr;
916 			addr->sin_family = AF_INET;
917 			addr->sin_addr.s_addr = header->addr.addr4.src_addr;
918 			addr->sin_port = header->addr.addr4.src_port;
919 			rep->client_addrlen = (socklen_t)sizeof(struct sockaddr_in);
920 			}
921 			/* Ignore the destination address; it should be us. */
922 			break;
923 		case 0x21: /* AF_INET6|STREAM */
924 		case 0x22: /* AF_INET6|DGRAM */
925 			{
926 			struct sockaddr_in6* addr =
927 				(struct sockaddr_in6*)&rep->client_addr;
928 			memset(addr, 0, sizeof(*addr));
929 			addr->sin6_family = AF_INET6;
930 			memcpy(&addr->sin6_addr,
931 				header->addr.addr6.src_addr, 16);
932 			addr->sin6_port = header->addr.addr6.src_port;
933 			rep->client_addrlen = (socklen_t)sizeof(struct sockaddr_in6);
934 			}
935 			/* Ignore the destination address; it should be us. */
936 			break;
937 	}
938 	rep->is_proxied = 1;
939 done:
940 	if(!stream) {
941 		/* We are reading a whole packet;
942 		 * Move the rest of the data to overwrite the PROXYv2 header */
943 		/* XXX can we do better to avoid memmove? */
944 		memmove(header, ((char*)header)+size,
945 			sldns_buffer_limit(buf)-size);
946 		sldns_buffer_set_limit(buf, sldns_buffer_limit(buf)-size);
947 	}
948 	return 1;
949 }
950 
951 void
952 comm_point_udp_ancil_callback(int fd, short event, void* arg)
953 {
954 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
955 	struct comm_reply rep;
956 	struct msghdr msg;
957 	struct iovec iov[1];
958 	ssize_t rcv;
959 	union {
960 		struct cmsghdr hdr;
961 		char buf[256];
962 	} ancil;
963 	int i;
964 #ifndef S_SPLINT_S
965 	struct cmsghdr* cmsg;
966 #endif /* S_SPLINT_S */
967 #ifdef HAVE_LINUX_NET_TSTAMP_H
968 	struct timespec *ts;
969 #endif /* HAVE_LINUX_NET_TSTAMP_H */
970 
971 	rep.c = (struct comm_point*)arg;
972 	log_assert(rep.c->type == comm_udp);
973 
974 	if(!(event&UB_EV_READ))
975 		return;
976 	log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
977 	ub_comm_base_now(rep.c->ev->base);
978 	for(i=0; i<NUM_UDP_PER_SELECT; i++) {
979 		sldns_buffer_clear(rep.c->buffer);
980 		timeval_clear(&rep.c->recv_tv);
981 		rep.remote_addrlen = (socklen_t)sizeof(rep.remote_addr);
982 		log_assert(fd != -1);
983 		log_assert(sldns_buffer_remaining(rep.c->buffer) > 0);
984 		msg.msg_name = &rep.remote_addr;
985 		msg.msg_namelen = (socklen_t)sizeof(rep.remote_addr);
986 		iov[0].iov_base = sldns_buffer_begin(rep.c->buffer);
987 		iov[0].iov_len = sldns_buffer_remaining(rep.c->buffer);
988 		msg.msg_iov = iov;
989 		msg.msg_iovlen = 1;
990 		msg.msg_control = ancil.buf;
991 #ifndef S_SPLINT_S
992 		msg.msg_controllen = sizeof(ancil.buf);
993 #endif /* S_SPLINT_S */
994 		msg.msg_flags = 0;
995 		rcv = recvmsg(fd, &msg, MSG_DONTWAIT);
996 		if(rcv == -1) {
997 			if(errno != EAGAIN && errno != EINTR
998 				&& udp_recv_needs_log(errno)) {
999 				log_err("recvmsg failed: %s", strerror(errno));
1000 			}
1001 			return;
1002 		}
1003 		rep.remote_addrlen = msg.msg_namelen;
1004 		sldns_buffer_skip(rep.c->buffer, rcv);
1005 		sldns_buffer_flip(rep.c->buffer);
1006 		rep.srctype = 0;
1007 		rep.is_proxied = 0;
1008 #ifndef S_SPLINT_S
1009 		for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
1010 			cmsg = CMSG_NXTHDR(&msg, cmsg)) {
1011 			if( cmsg->cmsg_level == IPPROTO_IPV6 &&
1012 				cmsg->cmsg_type == IPV6_PKTINFO) {
1013 				rep.srctype = 6;
1014 				memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg),
1015 					sizeof(struct in6_pktinfo));
1016 				break;
1017 #ifdef IP_PKTINFO
1018 			} else if( cmsg->cmsg_level == IPPROTO_IP &&
1019 				cmsg->cmsg_type == IP_PKTINFO) {
1020 				rep.srctype = 4;
1021 				memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg),
1022 					sizeof(struct in_pktinfo));
1023 				break;
1024 #elif defined(IP_RECVDSTADDR)
1025 			} else if( cmsg->cmsg_level == IPPROTO_IP &&
1026 				cmsg->cmsg_type == IP_RECVDSTADDR) {
1027 				rep.srctype = 4;
1028 				memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg),
1029 					sizeof(struct in_addr));
1030 				break;
1031 #endif /* IP_PKTINFO or IP_RECVDSTADDR */
1032 #ifdef HAVE_LINUX_NET_TSTAMP_H
1033 			} else if( cmsg->cmsg_level == SOL_SOCKET &&
1034 				cmsg->cmsg_type == SO_TIMESTAMPNS) {
1035 				ts = (struct timespec *)CMSG_DATA(cmsg);
1036 				TIMESPEC_TO_TIMEVAL(&rep.c->recv_tv, ts);
1037 			} else if( cmsg->cmsg_level == SOL_SOCKET &&
1038 				cmsg->cmsg_type == SO_TIMESTAMPING) {
1039 				ts = (struct timespec *)CMSG_DATA(cmsg);
1040 				TIMESPEC_TO_TIMEVAL(&rep.c->recv_tv, ts);
1041 			} else if( cmsg->cmsg_level == SOL_SOCKET &&
1042 				cmsg->cmsg_type == SO_TIMESTAMP) {
1043 				memmove(&rep.c->recv_tv, CMSG_DATA(cmsg), sizeof(struct timeval));
1044 #endif /* HAVE_LINUX_NET_TSTAMP_H */
1045 			}
1046 		}
1047 
1048 		if(verbosity >= VERB_ALGO && rep.srctype != 0)
1049 			p_ancil("receive_udp on interface", &rep);
1050 #endif /* S_SPLINT_S */
1051 
1052 		if(rep.c->pp2_enabled && !consume_pp2_header(rep.c->buffer,
1053 			&rep, 0)) {
1054 			log_err("proxy_protocol: could not consume PROXYv2 header");
1055 			return;
1056 		}
1057 		if(!rep.is_proxied) {
1058 			rep.client_addrlen = rep.remote_addrlen;
1059 			memmove(&rep.client_addr, &rep.remote_addr,
1060 				rep.remote_addrlen);
1061 		}
1062 
1063 		fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
1064 		if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
1065 			/* send back immediate reply */
1066 			(void)comm_point_send_udp_msg_if(rep.c, rep.c->buffer,
1067 				(struct sockaddr*)&rep.remote_addr,
1068 				rep.remote_addrlen, &rep);
1069 		}
1070 		if(!rep.c || rep.c->fd == -1) /* commpoint closed */
1071 			break;
1072 	}
1073 #else
1074 	(void)fd;
1075 	(void)event;
1076 	(void)arg;
1077 	fatal_exit("recvmsg: No support for IPV6_PKTINFO; IP_PKTINFO or IP_RECVDSTADDR. "
1078 		"Please disable interface-automatic");
1079 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG */
1080 }
1081 
1082 void
1083 comm_point_udp_callback(int fd, short event, void* arg)
1084 {
1085 	struct comm_reply rep;
1086 	ssize_t rcv;
1087 	int i;
1088 	struct sldns_buffer *buffer;
1089 
1090 	rep.c = (struct comm_point*)arg;
1091 	log_assert(rep.c->type == comm_udp);
1092 
1093 	if(!(event&UB_EV_READ))
1094 		return;
1095 	log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
1096 	ub_comm_base_now(rep.c->ev->base);
1097 	for(i=0; i<NUM_UDP_PER_SELECT; i++) {
1098 		sldns_buffer_clear(rep.c->buffer);
1099 		rep.remote_addrlen = (socklen_t)sizeof(rep.remote_addr);
1100 		log_assert(fd != -1);
1101 		log_assert(sldns_buffer_remaining(rep.c->buffer) > 0);
1102 		rcv = recvfrom(fd, (void*)sldns_buffer_begin(rep.c->buffer),
1103 			sldns_buffer_remaining(rep.c->buffer), MSG_DONTWAIT,
1104 			(struct sockaddr*)&rep.remote_addr, &rep.remote_addrlen);
1105 		if(rcv == -1) {
1106 #ifndef USE_WINSOCK
1107 			if(errno != EAGAIN && errno != EINTR
1108 				&& udp_recv_needs_log(errno))
1109 				log_err("recvfrom %d failed: %s",
1110 					fd, strerror(errno));
1111 #else
1112 			if(WSAGetLastError() != WSAEINPROGRESS &&
1113 				WSAGetLastError() != WSAECONNRESET &&
1114 				WSAGetLastError()!= WSAEWOULDBLOCK &&
1115 				udp_recv_needs_log(WSAGetLastError()))
1116 				log_err("recvfrom failed: %s",
1117 					wsa_strerror(WSAGetLastError()));
1118 #endif
1119 			return;
1120 		}
1121 		sldns_buffer_skip(rep.c->buffer, rcv);
1122 		sldns_buffer_flip(rep.c->buffer);
1123 		rep.srctype = 0;
1124 		rep.is_proxied = 0;
1125 
1126 		if(rep.c->pp2_enabled && !consume_pp2_header(rep.c->buffer,
1127 			&rep, 0)) {
1128 			log_err("proxy_protocol: could not consume PROXYv2 header");
1129 			return;
1130 		}
1131 		if(!rep.is_proxied) {
1132 			rep.client_addrlen = rep.remote_addrlen;
1133 			memmove(&rep.client_addr, &rep.remote_addr,
1134 				rep.remote_addrlen);
1135 		}
1136 
1137 		fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
1138 		if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
1139 			/* send back immediate reply */
1140 #ifdef USE_DNSCRYPT
1141 			buffer = rep.c->dnscrypt_buffer;
1142 #else
1143 			buffer = rep.c->buffer;
1144 #endif
1145 			(void)comm_point_send_udp_msg(rep.c, buffer,
1146 				(struct sockaddr*)&rep.remote_addr,
1147 				rep.remote_addrlen, 0);
1148 		}
1149 		if(!rep.c || rep.c->fd != fd) /* commpoint closed to -1 or reused for
1150 		another UDP port. Note rep.c cannot be reused with TCP fd. */
1151 			break;
1152 	}
1153 }
1154 
1155 int adjusted_tcp_timeout(struct comm_point* c)
1156 {
1157 	if(c->tcp_timeout_msec < TCP_QUERY_TIMEOUT_MINIMUM)
1158 		return TCP_QUERY_TIMEOUT_MINIMUM;
1159 	return c->tcp_timeout_msec;
1160 }
1161 
1162 /** Use a new tcp handler for new query fd, set to read query */
1163 static void
1164 setup_tcp_handler(struct comm_point* c, int fd, int cur, int max)
1165 {
1166 	int handler_usage;
1167 	log_assert(c->type == comm_tcp || c->type == comm_http);
1168 	log_assert(c->fd == -1);
1169 	sldns_buffer_clear(c->buffer);
1170 #ifdef USE_DNSCRYPT
1171 	if (c->dnscrypt)
1172 		sldns_buffer_clear(c->dnscrypt_buffer);
1173 #endif
1174 	c->tcp_is_reading = 1;
1175 	c->tcp_byte_count = 0;
1176 	c->tcp_keepalive = 0;
1177 	/* if more than half the tcp handlers are in use, use a shorter
1178 	 * timeout for this TCP connection, we need to make space for
1179 	 * other connections to be able to get attention */
1180 	/* If > 50% TCP handler structures in use, set timeout to 1/100th
1181 	 * 	configured value.
1182 	 * If > 65%TCP handler structures in use, set to 1/500th configured
1183 	 * 	value.
1184 	 * If > 80% TCP handler structures in use, set to 0.
1185 	 *
1186 	 * If the timeout to use falls below 200 milliseconds, an actual
1187 	 * timeout of 200ms is used.
1188 	 */
1189 	handler_usage = (cur * 100) / max;
1190 	if(handler_usage > 50 && handler_usage <= 65)
1191 		c->tcp_timeout_msec /= 100;
1192 	else if (handler_usage > 65 && handler_usage <= 80)
1193 		c->tcp_timeout_msec /= 500;
1194 	else if (handler_usage > 80)
1195 		c->tcp_timeout_msec = 0;
1196 	comm_point_start_listening(c, fd, adjusted_tcp_timeout(c));
1197 }
1198 
1199 void comm_base_handle_slow_accept(int ATTR_UNUSED(fd),
1200 	short ATTR_UNUSED(event), void* arg)
1201 {
1202 	struct comm_base* b = (struct comm_base*)arg;
1203 	/* timeout for the slow accept, re-enable accepts again */
1204 	if(b->start_accept) {
1205 		verbose(VERB_ALGO, "wait is over, slow accept disabled");
1206 		fptr_ok(fptr_whitelist_start_accept(b->start_accept));
1207 		(*b->start_accept)(b->cb_arg);
1208 		b->eb->slow_accept_enabled = 0;
1209 	}
1210 }
1211 
1212 int comm_point_perform_accept(struct comm_point* c,
1213 	struct sockaddr_storage* addr, socklen_t* addrlen)
1214 {
1215 	int new_fd;
1216 	*addrlen = (socklen_t)sizeof(*addr);
1217 #ifndef HAVE_ACCEPT4
1218 	new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen);
1219 #else
1220 	/* SOCK_NONBLOCK saves extra calls to fcntl for the same result */
1221 	new_fd = accept4(c->fd, (struct sockaddr*)addr, addrlen, SOCK_NONBLOCK);
1222 #endif
1223 	if(new_fd == -1) {
1224 #ifndef USE_WINSOCK
1225 		/* EINTR is signal interrupt. others are closed connection. */
1226 		if(	errno == EINTR || errno == EAGAIN
1227 #ifdef EWOULDBLOCK
1228 			|| errno == EWOULDBLOCK
1229 #endif
1230 #ifdef ECONNABORTED
1231 			|| errno == ECONNABORTED
1232 #endif
1233 #ifdef EPROTO
1234 			|| errno == EPROTO
1235 #endif /* EPROTO */
1236 			)
1237 			return -1;
1238 #if defined(ENFILE) && defined(EMFILE)
1239 		if(errno == ENFILE || errno == EMFILE) {
1240 			/* out of file descriptors, likely outside of our
1241 			 * control. stop accept() calls for some time */
1242 			if(c->ev->base->stop_accept) {
1243 				struct comm_base* b = c->ev->base;
1244 				struct timeval tv;
1245 				verbose(VERB_ALGO, "out of file descriptors: "
1246 					"slow accept");
1247 				ub_comm_base_now(b);
1248 				if(b->eb->last_slow_log+SLOW_LOG_TIME <=
1249 					b->eb->secs) {
1250 					b->eb->last_slow_log = b->eb->secs;
1251 					verbose(VERB_OPS, "accept failed, "
1252 						"slow down accept for %d "
1253 						"msec: %s",
1254 						NETEVENT_SLOW_ACCEPT_TIME,
1255 						sock_strerror(errno));
1256 				}
1257 				b->eb->slow_accept_enabled = 1;
1258 				fptr_ok(fptr_whitelist_stop_accept(
1259 					b->stop_accept));
1260 				(*b->stop_accept)(b->cb_arg);
1261 				/* set timeout, no mallocs */
1262 				tv.tv_sec = NETEVENT_SLOW_ACCEPT_TIME/1000;
1263 				tv.tv_usec = (NETEVENT_SLOW_ACCEPT_TIME%1000)*1000;
1264 				b->eb->slow_accept = ub_event_new(b->eb->base,
1265 					-1, UB_EV_TIMEOUT,
1266 					comm_base_handle_slow_accept, b);
1267 				if(b->eb->slow_accept == NULL) {
1268 					/* we do not want to log here, because
1269 					 * that would spam the logfiles.
1270 					 * error: "event_base_set failed." */
1271 				}
1272 				else if(ub_event_add(b->eb->slow_accept, &tv)
1273 					!= 0) {
1274 					/* we do not want to log here,
1275 					 * error: "event_add failed." */
1276 				}
1277 			} else {
1278 				log_err("accept, with no slow down, "
1279 					"failed: %s", sock_strerror(errno));
1280 			}
1281 			return -1;
1282 		}
1283 #endif
1284 #else /* USE_WINSOCK */
1285 		if(WSAGetLastError() == WSAEINPROGRESS ||
1286 			WSAGetLastError() == WSAECONNRESET)
1287 			return -1;
1288 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
1289 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1290 			return -1;
1291 		}
1292 #endif
1293 		log_err_addr("accept failed", sock_strerror(errno), addr,
1294 			*addrlen);
1295 		return -1;
1296 	}
1297 	if(c->tcp_conn_limit && c->type == comm_tcp_accept) {
1298 		c->tcl_addr = tcl_addr_lookup(c->tcp_conn_limit, addr, *addrlen);
1299 		if(!tcl_new_connection(c->tcl_addr)) {
1300 			if(verbosity >= 3)
1301 				log_err_addr("accept rejected",
1302 				"connection limit exceeded", addr, *addrlen);
1303 			close(new_fd);
1304 			return -1;
1305 		}
1306 	}
1307 #ifndef HAVE_ACCEPT4
1308 	fd_set_nonblock(new_fd);
1309 #endif
1310 	return new_fd;
1311 }
1312 
1313 #ifdef USE_WINSOCK
1314 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp),
1315 #ifdef HAVE_BIO_SET_CALLBACK_EX
1316 	size_t ATTR_UNUSED(len),
1317 #endif
1318         int ATTR_UNUSED(argi), long argl,
1319 #ifndef HAVE_BIO_SET_CALLBACK_EX
1320 	long retvalue
1321 #else
1322 	int retvalue, size_t* ATTR_UNUSED(processed)
1323 #endif
1324 	)
1325 {
1326 	int wsa_err = WSAGetLastError(); /* store errcode before it is gone */
1327 	verbose(VERB_ALGO, "bio_cb %d, %s %s %s", oper,
1328 		(oper&BIO_CB_RETURN)?"return":"before",
1329 		(oper&BIO_CB_READ)?"read":((oper&BIO_CB_WRITE)?"write":"other"),
1330 		wsa_err==WSAEWOULDBLOCK?"wsawb":"");
1331 	/* on windows, check if previous operation caused EWOULDBLOCK */
1332 	if( (oper == (BIO_CB_READ|BIO_CB_RETURN) && argl == 0) ||
1333 		(oper == (BIO_CB_GETS|BIO_CB_RETURN) && argl == 0)) {
1334 		if(wsa_err == WSAEWOULDBLOCK)
1335 			ub_winsock_tcp_wouldblock((struct ub_event*)
1336 				BIO_get_callback_arg(b), UB_EV_READ);
1337 	}
1338 	if( (oper == (BIO_CB_WRITE|BIO_CB_RETURN) && argl == 0) ||
1339 		(oper == (BIO_CB_PUTS|BIO_CB_RETURN) && argl == 0)) {
1340 		if(wsa_err == WSAEWOULDBLOCK)
1341 			ub_winsock_tcp_wouldblock((struct ub_event*)
1342 				BIO_get_callback_arg(b), UB_EV_WRITE);
1343 	}
1344 	/* return original return value */
1345 	return retvalue;
1346 }
1347 
1348 /** set win bio callbacks for nonblocking operations */
1349 void
1350 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl)
1351 {
1352 	SSL* ssl = (SSL*)thessl;
1353 	/* set them both just in case, but usually they are the same BIO */
1354 #ifdef HAVE_BIO_SET_CALLBACK_EX
1355 	BIO_set_callback_ex(SSL_get_rbio(ssl), &win_bio_cb);
1356 #else
1357 	BIO_set_callback(SSL_get_rbio(ssl), &win_bio_cb);
1358 #endif
1359 	BIO_set_callback_arg(SSL_get_rbio(ssl), (char*)c->ev->ev);
1360 #ifdef HAVE_BIO_SET_CALLBACK_EX
1361 	BIO_set_callback_ex(SSL_get_wbio(ssl), &win_bio_cb);
1362 #else
1363 	BIO_set_callback(SSL_get_wbio(ssl), &win_bio_cb);
1364 #endif
1365 	BIO_set_callback_arg(SSL_get_wbio(ssl), (char*)c->ev->ev);
1366 }
1367 #endif
1368 
1369 #ifdef HAVE_NGHTTP2
1370 /** Create http2 session server.  Per connection, after TCP accepted.*/
1371 static int http2_session_server_create(struct http2_session* h2_session)
1372 {
1373 	log_assert(h2_session->callbacks);
1374 	h2_session->is_drop = 0;
1375 	if(nghttp2_session_server_new(&h2_session->session,
1376 			h2_session->callbacks,
1377 		h2_session) == NGHTTP2_ERR_NOMEM) {
1378 		log_err("failed to create nghttp2 session server");
1379 		return 0;
1380 	}
1381 
1382 	return 1;
1383 }
1384 
1385 /** Submit http2 setting to session. Once per session. */
1386 static int http2_submit_settings(struct http2_session* h2_session)
1387 {
1388 	int ret;
1389 	nghttp2_settings_entry settings[1] = {
1390 		{NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS,
1391 		 h2_session->c->http2_max_streams}};
1392 
1393 	ret = nghttp2_submit_settings(h2_session->session, NGHTTP2_FLAG_NONE,
1394 		settings, 1);
1395 	if(ret) {
1396 		verbose(VERB_QUERY, "http2: submit_settings failed, "
1397 			"error: %s", nghttp2_strerror(ret));
1398 		return 0;
1399 	}
1400 	return 1;
1401 }
1402 #endif /* HAVE_NGHTTP2 */
1403 
1404 
1405 void
1406 comm_point_tcp_accept_callback(int fd, short event, void* arg)
1407 {
1408 	struct comm_point* c = (struct comm_point*)arg, *c_hdl;
1409 	int new_fd;
1410 	log_assert(c->type == comm_tcp_accept);
1411 	if(!(event & UB_EV_READ)) {
1412 		log_info("ignoring tcp accept event %d", (int)event);
1413 		return;
1414 	}
1415 	ub_comm_base_now(c->ev->base);
1416 	/* find free tcp handler. */
1417 	if(!c->tcp_free) {
1418 		log_warn("accepted too many tcp, connections full");
1419 		return;
1420 	}
1421 	/* accept incoming connection. */
1422 	c_hdl = c->tcp_free;
1423 	/* clear leftover flags from previous use, and then set the
1424 	 * correct event base for the event structure for libevent */
1425 	ub_event_free(c_hdl->ev->ev);
1426 	c_hdl->ev->ev = NULL;
1427 	if((c_hdl->type == comm_tcp && c_hdl->tcp_req_info) ||
1428 		c_hdl->type == comm_local || c_hdl->type == comm_raw)
1429 		c_hdl->tcp_do_toggle_rw = 0;
1430 	else	c_hdl->tcp_do_toggle_rw = 1;
1431 
1432 	if(c_hdl->type == comm_http) {
1433 #ifdef HAVE_NGHTTP2
1434 		if(!c_hdl->h2_session ||
1435 			!http2_session_server_create(c_hdl->h2_session)) {
1436 			log_warn("failed to create nghttp2");
1437 			return;
1438 		}
1439 		if(!c_hdl->h2_session ||
1440 			!http2_submit_settings(c_hdl->h2_session)) {
1441 			log_warn("failed to submit http2 settings");
1442 			return;
1443 		}
1444 		if(!c->ssl) {
1445 			c_hdl->tcp_do_toggle_rw = 0;
1446 			c_hdl->use_h2 = 1;
1447 		}
1448 #endif
1449 		c_hdl->ev->ev = ub_event_new(c_hdl->ev->base->eb->base, -1,
1450 			UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT,
1451 			comm_point_http_handle_callback, c_hdl);
1452 	} else {
1453 		c_hdl->ev->ev = ub_event_new(c_hdl->ev->base->eb->base, -1,
1454 			UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT,
1455 			comm_point_tcp_handle_callback, c_hdl);
1456 	}
1457 	if(!c_hdl->ev->ev) {
1458 		log_warn("could not ub_event_new, dropped tcp");
1459 		return;
1460 	}
1461 	log_assert(fd != -1);
1462 	(void)fd;
1463 	new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.remote_addr,
1464 		&c_hdl->repinfo.remote_addrlen);
1465 	if(new_fd == -1)
1466 		return;
1467 	/* Copy remote_address to client_address.
1468 	 * Simplest way/time for streams to do that. */
1469 	c_hdl->repinfo.client_addrlen = c_hdl->repinfo.remote_addrlen;
1470 	memmove(&c_hdl->repinfo.client_addr,
1471 		&c_hdl->repinfo.remote_addr,
1472 		c_hdl->repinfo.remote_addrlen);
1473 	if(c->ssl) {
1474 		c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd);
1475 		if(!c_hdl->ssl) {
1476 			c_hdl->fd = new_fd;
1477 			comm_point_close(c_hdl);
1478 			return;
1479 		}
1480 		c_hdl->ssl_shake_state = comm_ssl_shake_read;
1481 #ifdef USE_WINSOCK
1482 		comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl);
1483 #endif
1484 	}
1485 
1486 	/* grab the tcp handler buffers */
1487 	c->cur_tcp_count++;
1488 	c->tcp_free = c_hdl->tcp_free;
1489 	c_hdl->tcp_free = NULL;
1490 	if(!c->tcp_free) {
1491 		/* stop accepting incoming queries for now. */
1492 		comm_point_stop_listening(c);
1493 	}
1494 	setup_tcp_handler(c_hdl, new_fd, c->cur_tcp_count, c->max_tcp_count);
1495 }
1496 
1497 /** Make tcp handler free for next assignment */
1498 static void
1499 reclaim_tcp_handler(struct comm_point* c)
1500 {
1501 	log_assert(c->type == comm_tcp);
1502 	if(c->ssl) {
1503 #ifdef HAVE_SSL
1504 		SSL_shutdown(c->ssl);
1505 		SSL_free(c->ssl);
1506 		c->ssl = NULL;
1507 #endif
1508 	}
1509 	comm_point_close(c);
1510 	if(c->tcp_parent) {
1511 		if(c != c->tcp_parent->tcp_free) {
1512 			c->tcp_parent->cur_tcp_count--;
1513 			c->tcp_free = c->tcp_parent->tcp_free;
1514 			c->tcp_parent->tcp_free = c;
1515 		}
1516 		if(!c->tcp_free) {
1517 			/* re-enable listening on accept socket */
1518 			comm_point_start_listening(c->tcp_parent, -1, -1);
1519 		}
1520 	}
1521 	c->tcp_more_read_again = NULL;
1522 	c->tcp_more_write_again = NULL;
1523 	c->tcp_byte_count = 0;
1524 	c->pp2_header_state = pp2_header_none;
1525 	sldns_buffer_clear(c->buffer);
1526 }
1527 
1528 /** do the callback when writing is done */
1529 static void
1530 tcp_callback_writer(struct comm_point* c)
1531 {
1532 	log_assert(c->type == comm_tcp);
1533 	if(!c->tcp_write_and_read) {
1534 		sldns_buffer_clear(c->buffer);
1535 		c->tcp_byte_count = 0;
1536 	}
1537 	if(c->tcp_do_toggle_rw)
1538 		c->tcp_is_reading = 1;
1539 	/* switch from listening(write) to listening(read) */
1540 	if(c->tcp_req_info) {
1541 		tcp_req_info_handle_writedone(c->tcp_req_info);
1542 	} else {
1543 		comm_point_stop_listening(c);
1544 		if(c->tcp_write_and_read) {
1545 			fptr_ok(fptr_whitelist_comm_point(c->callback));
1546 			if( (*c->callback)(c, c->cb_arg, NETEVENT_PKT_WRITTEN,
1547 				&c->repinfo) ) {
1548 				comm_point_start_listening(c, -1,
1549 					adjusted_tcp_timeout(c));
1550 			}
1551 		} else {
1552 			comm_point_start_listening(c, -1,
1553 					adjusted_tcp_timeout(c));
1554 		}
1555 	}
1556 }
1557 
1558 /** do the callback when reading is done */
1559 static void
1560 tcp_callback_reader(struct comm_point* c)
1561 {
1562 	log_assert(c->type == comm_tcp || c->type == comm_local);
1563 	sldns_buffer_flip(c->buffer);
1564 	if(c->tcp_do_toggle_rw)
1565 		c->tcp_is_reading = 0;
1566 	c->tcp_byte_count = 0;
1567 	if(c->tcp_req_info) {
1568 		tcp_req_info_handle_readdone(c->tcp_req_info);
1569 	} else {
1570 		if(c->type == comm_tcp)
1571 			comm_point_stop_listening(c);
1572 		fptr_ok(fptr_whitelist_comm_point(c->callback));
1573 		if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) {
1574 			comm_point_start_listening(c, -1,
1575 					adjusted_tcp_timeout(c));
1576 		}
1577 	}
1578 }
1579 
1580 #ifdef HAVE_SSL
1581 /** true if the ssl handshake error has to be squelched from the logs */
1582 int
1583 squelch_err_ssl_handshake(unsigned long err)
1584 {
1585 	if(verbosity >= VERB_QUERY)
1586 		return 0; /* only squelch on low verbosity */
1587 	if(ERR_GET_LIB(err) == ERR_LIB_SSL &&
1588 		(ERR_GET_REASON(err) == SSL_R_HTTPS_PROXY_REQUEST ||
1589 		 ERR_GET_REASON(err) == SSL_R_HTTP_REQUEST ||
1590 		 ERR_GET_REASON(err) == SSL_R_WRONG_VERSION_NUMBER ||
1591 		 ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_BAD_CERTIFICATE
1592 #ifdef SSL_F_TLS_POST_PROCESS_CLIENT_HELLO
1593 		 || ERR_GET_REASON(err) == SSL_R_NO_SHARED_CIPHER
1594 #endif
1595 #ifdef SSL_F_TLS_EARLY_POST_PROCESS_CLIENT_HELLO
1596 		 || ERR_GET_REASON(err) == SSL_R_UNKNOWN_PROTOCOL
1597 		 || ERR_GET_REASON(err) == SSL_R_UNSUPPORTED_PROTOCOL
1598 #  ifdef SSL_R_VERSION_TOO_LOW
1599 		 || ERR_GET_REASON(err) == SSL_R_VERSION_TOO_LOW
1600 #  endif
1601 #endif
1602 		))
1603 		return 1;
1604 	return 0;
1605 }
1606 #endif /* HAVE_SSL */
1607 
1608 /** continue ssl handshake */
1609 #ifdef HAVE_SSL
1610 static int
1611 ssl_handshake(struct comm_point* c)
1612 {
1613 	int r;
1614 	if(c->ssl_shake_state == comm_ssl_shake_hs_read) {
1615 		/* read condition satisfied back to writing */
1616 		comm_point_listen_for_rw(c, 0, 1);
1617 		c->ssl_shake_state = comm_ssl_shake_none;
1618 		return 1;
1619 	}
1620 	if(c->ssl_shake_state == comm_ssl_shake_hs_write) {
1621 		/* write condition satisfied, back to reading */
1622 		comm_point_listen_for_rw(c, 1, 0);
1623 		c->ssl_shake_state = comm_ssl_shake_none;
1624 		return 1;
1625 	}
1626 
1627 	ERR_clear_error();
1628 	r = SSL_do_handshake(c->ssl);
1629 	if(r != 1) {
1630 		int want = SSL_get_error(c->ssl, r);
1631 		if(want == SSL_ERROR_WANT_READ) {
1632 			if(c->ssl_shake_state == comm_ssl_shake_read)
1633 				return 1;
1634 			c->ssl_shake_state = comm_ssl_shake_read;
1635 			comm_point_listen_for_rw(c, 1, 0);
1636 			return 1;
1637 		} else if(want == SSL_ERROR_WANT_WRITE) {
1638 			if(c->ssl_shake_state == comm_ssl_shake_write)
1639 				return 1;
1640 			c->ssl_shake_state = comm_ssl_shake_write;
1641 			comm_point_listen_for_rw(c, 0, 1);
1642 			return 1;
1643 		} else if(r == 0) {
1644 			return 0; /* closed */
1645 		} else if(want == SSL_ERROR_SYSCALL) {
1646 			/* SYSCALL and errno==0 means closed uncleanly */
1647 #ifdef EPIPE
1648 			if(errno == EPIPE && verbosity < 2)
1649 				return 0; /* silence 'broken pipe' */
1650 #endif
1651 #ifdef ECONNRESET
1652 			if(errno == ECONNRESET && verbosity < 2)
1653 				return 0; /* silence reset by peer */
1654 #endif
1655 			if(!tcp_connect_errno_needs_log(
1656 				(struct sockaddr*)&c->repinfo.remote_addr,
1657 				c->repinfo.remote_addrlen))
1658 				return 0; /* silence connect failures that
1659 				show up because after connect this is the
1660 				first system call that accesses the socket */
1661 			if(errno != 0)
1662 				log_err("SSL_handshake syscall: %s",
1663 					strerror(errno));
1664 			return 0;
1665 		} else {
1666 			unsigned long err = ERR_get_error();
1667 			if(!squelch_err_ssl_handshake(err)) {
1668 				log_crypto_err_code("ssl handshake failed", err);
1669 				log_addr(VERB_OPS, "ssl handshake failed",
1670 					&c->repinfo.remote_addr,
1671 					c->repinfo.remote_addrlen);
1672 			}
1673 			return 0;
1674 		}
1675 	}
1676 	/* this is where peer verification could take place */
1677 	if((SSL_get_verify_mode(c->ssl)&SSL_VERIFY_PEER)) {
1678 		/* verification */
1679 		if(SSL_get_verify_result(c->ssl) == X509_V_OK) {
1680 #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE
1681 			X509* x = SSL_get1_peer_certificate(c->ssl);
1682 #else
1683 			X509* x = SSL_get_peer_certificate(c->ssl);
1684 #endif
1685 			if(!x) {
1686 				log_addr(VERB_ALGO, "SSL connection failed: "
1687 					"no certificate",
1688 					&c->repinfo.remote_addr,
1689 					c->repinfo.remote_addrlen);
1690 				return 0;
1691 			}
1692 			log_cert(VERB_ALGO, "peer certificate", x);
1693 #ifdef HAVE_SSL_GET0_PEERNAME
1694 			if(SSL_get0_peername(c->ssl)) {
1695 				char buf[255];
1696 				snprintf(buf, sizeof(buf), "SSL connection "
1697 					"to %s authenticated",
1698 					SSL_get0_peername(c->ssl));
1699 				log_addr(VERB_ALGO, buf, &c->repinfo.remote_addr,
1700 					c->repinfo.remote_addrlen);
1701 			} else {
1702 #endif
1703 				log_addr(VERB_ALGO, "SSL connection "
1704 					"authenticated", &c->repinfo.remote_addr,
1705 					c->repinfo.remote_addrlen);
1706 #ifdef HAVE_SSL_GET0_PEERNAME
1707 			}
1708 #endif
1709 			X509_free(x);
1710 		} else {
1711 #ifdef HAVE_SSL_GET1_PEER_CERTIFICATE
1712 			X509* x = SSL_get1_peer_certificate(c->ssl);
1713 #else
1714 			X509* x = SSL_get_peer_certificate(c->ssl);
1715 #endif
1716 			if(x) {
1717 				log_cert(VERB_ALGO, "peer certificate", x);
1718 				X509_free(x);
1719 			}
1720 			log_addr(VERB_ALGO, "SSL connection failed: "
1721 				"failed to authenticate",
1722 				&c->repinfo.remote_addr,
1723 				c->repinfo.remote_addrlen);
1724 			return 0;
1725 		}
1726 	} else {
1727 		/* unauthenticated, the verify peer flag was not set
1728 		 * in c->ssl when the ssl object was created from ssl_ctx */
1729 		log_addr(VERB_ALGO, "SSL connection", &c->repinfo.remote_addr,
1730 			c->repinfo.remote_addrlen);
1731 	}
1732 
1733 #ifdef HAVE_SSL_GET0_ALPN_SELECTED
1734 	/* check if http2 use is negotiated */
1735 	if(c->type == comm_http && c->h2_session) {
1736 		const unsigned char *alpn;
1737 		unsigned int alpnlen = 0;
1738 		SSL_get0_alpn_selected(c->ssl, &alpn, &alpnlen);
1739 		if(alpnlen == 2 && memcmp("h2", alpn, 2) == 0) {
1740 			/* connection upgraded to HTTP2 */
1741 			c->tcp_do_toggle_rw = 0;
1742 			c->use_h2 = 1;
1743 		}
1744 	}
1745 #endif
1746 
1747 	/* setup listen rw correctly */
1748 	if(c->tcp_is_reading) {
1749 		if(c->ssl_shake_state != comm_ssl_shake_read)
1750 			comm_point_listen_for_rw(c, 1, 0);
1751 	} else {
1752 		comm_point_listen_for_rw(c, 0, 1);
1753 	}
1754 	c->ssl_shake_state = comm_ssl_shake_none;
1755 	return 1;
1756 }
1757 #endif /* HAVE_SSL */
1758 
1759 /** ssl read callback on TCP */
1760 static int
1761 ssl_handle_read(struct comm_point* c)
1762 {
1763 #ifdef HAVE_SSL
1764 	int r;
1765 	if(c->ssl_shake_state != comm_ssl_shake_none) {
1766 		if(!ssl_handshake(c))
1767 			return 0;
1768 		if(c->ssl_shake_state != comm_ssl_shake_none)
1769 			return 1;
1770 	}
1771 	if(c->pp2_enabled && c->pp2_header_state != pp2_header_done) {
1772 		struct pp2_header* header = NULL;
1773 		size_t want_read_size = 0;
1774 		size_t current_read_size = 0;
1775 		if(c->pp2_header_state == pp2_header_none) {
1776 			want_read_size = PP2_HEADER_SIZE;
1777 			if(sldns_buffer_remaining(c->buffer)<want_read_size) {
1778 				log_err_addr("proxy_protocol: not enough "
1779 					"buffer size to read PROXYv2 header", "",
1780 					&c->repinfo.remote_addr,
1781 					c->repinfo.remote_addrlen);
1782 				return 0;
1783 			}
1784 			verbose(VERB_ALGO, "proxy_protocol: reading fixed "
1785 				"part of PROXYv2 header (len %lu)",
1786 				(unsigned long)want_read_size);
1787 			current_read_size = want_read_size;
1788 			if(c->tcp_byte_count < current_read_size) {
1789 				ERR_clear_error();
1790 				if((r=SSL_read(c->ssl, (void*)sldns_buffer_at(
1791 					c->buffer, c->tcp_byte_count),
1792 					current_read_size -
1793 					c->tcp_byte_count)) <= 0) {
1794 					int want = SSL_get_error(c->ssl, r);
1795 					if(want == SSL_ERROR_ZERO_RETURN) {
1796 						if(c->tcp_req_info)
1797 							return tcp_req_info_handle_read_close(c->tcp_req_info);
1798 						return 0; /* shutdown, closed */
1799 					} else if(want == SSL_ERROR_WANT_READ) {
1800 #ifdef USE_WINSOCK
1801 						ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1802 #endif
1803 						return 1; /* read more later */
1804 					} else if(want == SSL_ERROR_WANT_WRITE) {
1805 						c->ssl_shake_state = comm_ssl_shake_hs_write;
1806 						comm_point_listen_for_rw(c, 0, 1);
1807 						return 1;
1808 					} else if(want == SSL_ERROR_SYSCALL) {
1809 #ifdef ECONNRESET
1810 						if(errno == ECONNRESET && verbosity < 2)
1811 							return 0; /* silence reset by peer */
1812 #endif
1813 						if(errno != 0)
1814 							log_err("SSL_read syscall: %s",
1815 								strerror(errno));
1816 						return 0;
1817 					}
1818 					log_crypto_err("could not SSL_read");
1819 					return 0;
1820 				}
1821 				c->tcp_byte_count += r;
1822 				if(c->tcp_byte_count != current_read_size) return 1;
1823 				c->pp2_header_state = pp2_header_init;
1824 			}
1825 		}
1826 		if(c->pp2_header_state == pp2_header_init) {
1827 			header = pp2_read_header(c->buffer);
1828 			if(!header) {
1829 				log_err("proxy_protocol: could not parse "
1830 					"PROXYv2 header");
1831 				return 0;
1832 			}
1833 			want_read_size = ntohs(header->len);
1834 			if(sldns_buffer_remaining(c->buffer) <
1835 				PP2_HEADER_SIZE + want_read_size) {
1836 				log_err_addr("proxy_protocol: not enough "
1837 					"buffer size to read PROXYv2 header", "",
1838 					&c->repinfo.remote_addr,
1839 					c->repinfo.remote_addrlen);
1840 				return 0;
1841 			}
1842 			verbose(VERB_ALGO, "proxy_protocol: reading variable "
1843 				"part of PROXYv2 header (len %lu)",
1844 				(unsigned long)want_read_size);
1845 			current_read_size = PP2_HEADER_SIZE + want_read_size;
1846 			if(want_read_size == 0) {
1847 				/* nothing more to read; header is complete */
1848 				c->pp2_header_state = pp2_header_done;
1849 			} else if(c->tcp_byte_count < current_read_size) {
1850 				ERR_clear_error();
1851 				if((r=SSL_read(c->ssl, (void*)sldns_buffer_at(
1852 					c->buffer, c->tcp_byte_count),
1853 					current_read_size -
1854 					c->tcp_byte_count)) <= 0) {
1855 					int want = SSL_get_error(c->ssl, r);
1856 					if(want == SSL_ERROR_ZERO_RETURN) {
1857 						if(c->tcp_req_info)
1858 							return tcp_req_info_handle_read_close(c->tcp_req_info);
1859 						return 0; /* shutdown, closed */
1860 					} else if(want == SSL_ERROR_WANT_READ) {
1861 #ifdef USE_WINSOCK
1862 						ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1863 #endif
1864 						return 1; /* read more later */
1865 					} else if(want == SSL_ERROR_WANT_WRITE) {
1866 						c->ssl_shake_state = comm_ssl_shake_hs_write;
1867 						comm_point_listen_for_rw(c, 0, 1);
1868 						return 1;
1869 					} else if(want == SSL_ERROR_SYSCALL) {
1870 #ifdef ECONNRESET
1871 						if(errno == ECONNRESET && verbosity < 2)
1872 							return 0; /* silence reset by peer */
1873 #endif
1874 						if(errno != 0)
1875 							log_err("SSL_read syscall: %s",
1876 								strerror(errno));
1877 						return 0;
1878 					}
1879 					log_crypto_err("could not SSL_read");
1880 					return 0;
1881 				}
1882 				c->tcp_byte_count += r;
1883 				if(c->tcp_byte_count != current_read_size) return 1;
1884 				c->pp2_header_state = pp2_header_done;
1885 			}
1886 		}
1887 		if(c->pp2_header_state != pp2_header_done || !header) {
1888 			log_err_addr("proxy_protocol: wrong state for the "
1889 				"PROXYv2 header", "", &c->repinfo.remote_addr,
1890 				c->repinfo.remote_addrlen);
1891 			return 0;
1892 		}
1893 		if(!consume_pp2_header(c->buffer, &c->repinfo, 1)) {
1894 			log_err_addr("proxy_protocol: could not consume "
1895 				"PROXYv2 header", "", &c->repinfo.remote_addr,
1896 				c->repinfo.remote_addrlen);
1897 			return 0;
1898 		}
1899 		verbose(VERB_ALGO, "proxy_protocol: successful read of "
1900 			"PROXYv2 header");
1901 		/* Clear and reset the buffer to read the following
1902 		 * DNS packet(s). */
1903 		sldns_buffer_clear(c->buffer);
1904 		c->tcp_byte_count = 0;
1905 		return 1;
1906 	}
1907 	if(c->tcp_byte_count < sizeof(uint16_t)) {
1908 		/* read length bytes */
1909 		ERR_clear_error();
1910 		if((r=SSL_read(c->ssl, (void*)sldns_buffer_at(c->buffer,
1911 			c->tcp_byte_count), (int)(sizeof(uint16_t) -
1912 			c->tcp_byte_count))) <= 0) {
1913 			int want = SSL_get_error(c->ssl, r);
1914 			if(want == SSL_ERROR_ZERO_RETURN) {
1915 				if(c->tcp_req_info)
1916 					return tcp_req_info_handle_read_close(c->tcp_req_info);
1917 				return 0; /* shutdown, closed */
1918 			} else if(want == SSL_ERROR_WANT_READ) {
1919 #ifdef USE_WINSOCK
1920 				ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1921 #endif
1922 				return 1; /* read more later */
1923 			} else if(want == SSL_ERROR_WANT_WRITE) {
1924 				c->ssl_shake_state = comm_ssl_shake_hs_write;
1925 				comm_point_listen_for_rw(c, 0, 1);
1926 				return 1;
1927 			} else if(want == SSL_ERROR_SYSCALL) {
1928 #ifdef ECONNRESET
1929 				if(errno == ECONNRESET && verbosity < 2)
1930 					return 0; /* silence reset by peer */
1931 #endif
1932 				if(errno != 0)
1933 					log_err("SSL_read syscall: %s",
1934 						strerror(errno));
1935 				return 0;
1936 			}
1937 			log_crypto_err("could not SSL_read");
1938 			return 0;
1939 		}
1940 		c->tcp_byte_count += r;
1941 		if(c->tcp_byte_count < sizeof(uint16_t))
1942 			return 1;
1943 		if(sldns_buffer_read_u16_at(c->buffer, 0) >
1944 			sldns_buffer_capacity(c->buffer)) {
1945 			verbose(VERB_QUERY, "ssl: dropped larger than buffer");
1946 			return 0;
1947 		}
1948 		sldns_buffer_set_limit(c->buffer,
1949 			sldns_buffer_read_u16_at(c->buffer, 0));
1950 		if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1951 			verbose(VERB_QUERY, "ssl: dropped bogus too short.");
1952 			return 0;
1953 		}
1954 		sldns_buffer_skip(c->buffer, (ssize_t)(c->tcp_byte_count-sizeof(uint16_t)));
1955 		verbose(VERB_ALGO, "Reading ssl tcp query of length %d",
1956 			(int)sldns_buffer_limit(c->buffer));
1957 	}
1958 	if(sldns_buffer_remaining(c->buffer) > 0) {
1959 		ERR_clear_error();
1960 		r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
1961 			(int)sldns_buffer_remaining(c->buffer));
1962 		if(r <= 0) {
1963 			int want = SSL_get_error(c->ssl, r);
1964 			if(want == SSL_ERROR_ZERO_RETURN) {
1965 				if(c->tcp_req_info)
1966 					return tcp_req_info_handle_read_close(c->tcp_req_info);
1967 				return 0; /* shutdown, closed */
1968 			} else if(want == SSL_ERROR_WANT_READ) {
1969 #ifdef USE_WINSOCK
1970 				ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
1971 #endif
1972 				return 1; /* read more later */
1973 			} else if(want == SSL_ERROR_WANT_WRITE) {
1974 				c->ssl_shake_state = comm_ssl_shake_hs_write;
1975 				comm_point_listen_for_rw(c, 0, 1);
1976 				return 1;
1977 			} else if(want == SSL_ERROR_SYSCALL) {
1978 #ifdef ECONNRESET
1979 				if(errno == ECONNRESET && verbosity < 2)
1980 					return 0; /* silence reset by peer */
1981 #endif
1982 				if(errno != 0)
1983 					log_err("SSL_read syscall: %s",
1984 						strerror(errno));
1985 				return 0;
1986 			}
1987 			log_crypto_err("could not SSL_read");
1988 			return 0;
1989 		}
1990 		sldns_buffer_skip(c->buffer, (ssize_t)r);
1991 	}
1992 	if(sldns_buffer_remaining(c->buffer) <= 0) {
1993 		tcp_callback_reader(c);
1994 	}
1995 	return 1;
1996 #else
1997 	(void)c;
1998 	return 0;
1999 #endif /* HAVE_SSL */
2000 }
2001 
2002 /** ssl write callback on TCP */
2003 static int
2004 ssl_handle_write(struct comm_point* c)
2005 {
2006 #ifdef HAVE_SSL
2007 	int r;
2008 	if(c->ssl_shake_state != comm_ssl_shake_none) {
2009 		if(!ssl_handshake(c))
2010 			return 0;
2011 		if(c->ssl_shake_state != comm_ssl_shake_none)
2012 			return 1;
2013 	}
2014 	/* ignore return, if fails we may simply block */
2015 	(void)SSL_set_mode(c->ssl, (long)SSL_MODE_ENABLE_PARTIAL_WRITE);
2016 	if((c->tcp_write_and_read?c->tcp_write_byte_count:c->tcp_byte_count) < sizeof(uint16_t)) {
2017 		uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(c->buffer));
2018 		ERR_clear_error();
2019 		if(c->tcp_write_and_read) {
2020 			if(c->tcp_write_pkt_len + 2 < LDNS_RR_BUF_SIZE) {
2021 				/* combine the tcp length and the query for
2022 				 * write, this emulates writev */
2023 				uint8_t buf[LDNS_RR_BUF_SIZE];
2024 				memmove(buf, &len, sizeof(uint16_t));
2025 				memmove(buf+sizeof(uint16_t),
2026 					c->tcp_write_pkt,
2027 					c->tcp_write_pkt_len);
2028 				r = SSL_write(c->ssl,
2029 					(void*)(buf+c->tcp_write_byte_count),
2030 					c->tcp_write_pkt_len + 2 -
2031 					c->tcp_write_byte_count);
2032 			} else {
2033 				r = SSL_write(c->ssl,
2034 					(void*)(((uint8_t*)&len)+c->tcp_write_byte_count),
2035 					(int)(sizeof(uint16_t)-c->tcp_write_byte_count));
2036 			}
2037 		} else if(sizeof(uint16_t)+sldns_buffer_remaining(c->buffer) <
2038 			LDNS_RR_BUF_SIZE) {
2039 			/* combine the tcp length and the query for write,
2040 			 * this emulates writev */
2041 			uint8_t buf[LDNS_RR_BUF_SIZE];
2042 			memmove(buf, &len, sizeof(uint16_t));
2043 			memmove(buf+sizeof(uint16_t),
2044 				sldns_buffer_current(c->buffer),
2045 				sldns_buffer_remaining(c->buffer));
2046 			r = SSL_write(c->ssl, (void*)(buf+c->tcp_byte_count),
2047 				(int)(sizeof(uint16_t)+
2048 				sldns_buffer_remaining(c->buffer)
2049 				- c->tcp_byte_count));
2050 		} else {
2051 			r = SSL_write(c->ssl,
2052 				(void*)(((uint8_t*)&len)+c->tcp_byte_count),
2053 				(int)(sizeof(uint16_t)-c->tcp_byte_count));
2054 		}
2055 		if(r <= 0) {
2056 			int want = SSL_get_error(c->ssl, r);
2057 			if(want == SSL_ERROR_ZERO_RETURN) {
2058 				return 0; /* closed */
2059 			} else if(want == SSL_ERROR_WANT_READ) {
2060 				c->ssl_shake_state = comm_ssl_shake_hs_read;
2061 				comm_point_listen_for_rw(c, 1, 0);
2062 				return 1; /* wait for read condition */
2063 			} else if(want == SSL_ERROR_WANT_WRITE) {
2064 #ifdef USE_WINSOCK
2065 				ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2066 #endif
2067 				return 1; /* write more later */
2068 			} else if(want == SSL_ERROR_SYSCALL) {
2069 #ifdef EPIPE
2070 				if(errno == EPIPE && verbosity < 2)
2071 					return 0; /* silence 'broken pipe' */
2072 #endif
2073 				if(errno != 0)
2074 					log_err("SSL_write syscall: %s",
2075 						strerror(errno));
2076 				return 0;
2077 			}
2078 			log_crypto_err("could not SSL_write");
2079 			return 0;
2080 		}
2081 		if(c->tcp_write_and_read) {
2082 			c->tcp_write_byte_count += r;
2083 			if(c->tcp_write_byte_count < sizeof(uint16_t))
2084 				return 1;
2085 		} else {
2086 			c->tcp_byte_count += r;
2087 			if(c->tcp_byte_count < sizeof(uint16_t))
2088 				return 1;
2089 			sldns_buffer_set_position(c->buffer, c->tcp_byte_count -
2090 				sizeof(uint16_t));
2091 		}
2092 		if((!c->tcp_write_and_read && sldns_buffer_remaining(c->buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
2093 			tcp_callback_writer(c);
2094 			return 1;
2095 		}
2096 	}
2097 	log_assert(c->tcp_write_and_read || sldns_buffer_remaining(c->buffer) > 0);
2098 	log_assert(!c->tcp_write_and_read || c->tcp_write_byte_count < c->tcp_write_pkt_len + 2);
2099 	ERR_clear_error();
2100 	if(c->tcp_write_and_read) {
2101 		r = SSL_write(c->ssl, (void*)(c->tcp_write_pkt + c->tcp_write_byte_count - 2),
2102 			(int)(c->tcp_write_pkt_len + 2 - c->tcp_write_byte_count));
2103 	} else {
2104 		r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
2105 			(int)sldns_buffer_remaining(c->buffer));
2106 	}
2107 	if(r <= 0) {
2108 		int want = SSL_get_error(c->ssl, r);
2109 		if(want == SSL_ERROR_ZERO_RETURN) {
2110 			return 0; /* closed */
2111 		} else if(want == SSL_ERROR_WANT_READ) {
2112 			c->ssl_shake_state = comm_ssl_shake_hs_read;
2113 			comm_point_listen_for_rw(c, 1, 0);
2114 			return 1; /* wait for read condition */
2115 		} else if(want == SSL_ERROR_WANT_WRITE) {
2116 #ifdef USE_WINSOCK
2117 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2118 #endif
2119 			return 1; /* write more later */
2120 		} else if(want == SSL_ERROR_SYSCALL) {
2121 #ifdef EPIPE
2122 			if(errno == EPIPE && verbosity < 2)
2123 				return 0; /* silence 'broken pipe' */
2124 #endif
2125 			if(errno != 0)
2126 				log_err("SSL_write syscall: %s",
2127 					strerror(errno));
2128 			return 0;
2129 		}
2130 		log_crypto_err("could not SSL_write");
2131 		return 0;
2132 	}
2133 	if(c->tcp_write_and_read) {
2134 		c->tcp_write_byte_count += r;
2135 	} else {
2136 		sldns_buffer_skip(c->buffer, (ssize_t)r);
2137 	}
2138 
2139 	if((!c->tcp_write_and_read && sldns_buffer_remaining(c->buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
2140 		tcp_callback_writer(c);
2141 	}
2142 	return 1;
2143 #else
2144 	(void)c;
2145 	return 0;
2146 #endif /* HAVE_SSL */
2147 }
2148 
2149 /** handle ssl tcp connection with dns contents */
2150 static int
2151 ssl_handle_it(struct comm_point* c, int is_write)
2152 {
2153 	/* handle case where renegotiation wants read during write call
2154 	 * or write during read calls */
2155 	if(is_write && c->ssl_shake_state == comm_ssl_shake_hs_write)
2156 		return ssl_handle_read(c);
2157 	else if(!is_write && c->ssl_shake_state == comm_ssl_shake_hs_read)
2158 		return ssl_handle_write(c);
2159 	/* handle read events for read operation and write events for a
2160 	 * write operation */
2161 	else if(!is_write)
2162 		return ssl_handle_read(c);
2163 	return ssl_handle_write(c);
2164 }
2165 
2166 /**
2167  * Handle tcp reading callback.
2168  * @param fd: file descriptor of socket.
2169  * @param c: comm point to read from into buffer.
2170  * @param short_ok: if true, very short packets are OK (for comm_local).
2171  * @return: 0 on error
2172  */
2173 static int
2174 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok)
2175 {
2176 	ssize_t r;
2177 	int recv_initial = 0;
2178 	log_assert(c->type == comm_tcp || c->type == comm_local);
2179 	if(c->ssl)
2180 		return ssl_handle_it(c, 0);
2181 	if(!c->tcp_is_reading && !c->tcp_write_and_read)
2182 		return 0;
2183 
2184 	log_assert(fd != -1);
2185 	if(c->pp2_enabled && c->pp2_header_state != pp2_header_done) {
2186 		struct pp2_header* header = NULL;
2187 		size_t want_read_size = 0;
2188 		size_t current_read_size = 0;
2189 		if(c->pp2_header_state == pp2_header_none) {
2190 			want_read_size = PP2_HEADER_SIZE;
2191 			if(sldns_buffer_remaining(c->buffer)<want_read_size) {
2192 				log_err_addr("proxy_protocol: not enough "
2193 					"buffer size to read PROXYv2 header", "",
2194 					&c->repinfo.remote_addr,
2195 					c->repinfo.remote_addrlen);
2196 				return 0;
2197 			}
2198 			verbose(VERB_ALGO, "proxy_protocol: reading fixed "
2199 				"part of PROXYv2 header (len %lu)",
2200 				(unsigned long)want_read_size);
2201 			current_read_size = want_read_size;
2202 			if(c->tcp_byte_count < current_read_size) {
2203 				r = recv(fd, (void*)sldns_buffer_at(c->buffer,
2204 					c->tcp_byte_count),
2205 					current_read_size-c->tcp_byte_count, MSG_DONTWAIT);
2206 				if(r == 0) {
2207 					if(c->tcp_req_info)
2208 						return tcp_req_info_handle_read_close(c->tcp_req_info);
2209 					return 0;
2210 				} else if(r == -1) {
2211 					goto recv_error_initial;
2212 				}
2213 				c->tcp_byte_count += r;
2214 				if(c->tcp_byte_count != current_read_size) return 1;
2215 				c->pp2_header_state = pp2_header_init;
2216 			}
2217 		}
2218 		if(c->pp2_header_state == pp2_header_init) {
2219 			header = pp2_read_header(c->buffer);
2220 			if(!header) {
2221 				log_err("proxy_protocol: could not parse "
2222 					"PROXYv2 header");
2223 				return 0;
2224 			}
2225 			want_read_size = ntohs(header->len);
2226 			if(sldns_buffer_remaining(c->buffer) <
2227 				PP2_HEADER_SIZE + want_read_size) {
2228 				log_err_addr("proxy_protocol: not enough "
2229 					"buffer size to read PROXYv2 header", "",
2230 					&c->repinfo.remote_addr,
2231 					c->repinfo.remote_addrlen);
2232 				return 0;
2233 			}
2234 			verbose(VERB_ALGO, "proxy_protocol: reading variable "
2235 				"part of PROXYv2 header (len %lu)",
2236 				(unsigned long)want_read_size);
2237 			current_read_size = PP2_HEADER_SIZE + want_read_size;
2238 			if(want_read_size == 0) {
2239 				/* nothing more to read; header is complete */
2240 				c->pp2_header_state = pp2_header_done;
2241 			} else if(c->tcp_byte_count < current_read_size) {
2242 				r = recv(fd, (void*)sldns_buffer_at(c->buffer,
2243 					c->tcp_byte_count),
2244 					current_read_size-c->tcp_byte_count, MSG_DONTWAIT);
2245 				if(r == 0) {
2246 					if(c->tcp_req_info)
2247 						return tcp_req_info_handle_read_close(c->tcp_req_info);
2248 					return 0;
2249 				} else if(r == -1) {
2250 					goto recv_error;
2251 				}
2252 				c->tcp_byte_count += r;
2253 				if(c->tcp_byte_count != current_read_size) return 1;
2254 				c->pp2_header_state = pp2_header_done;
2255 			}
2256 		}
2257 		if(c->pp2_header_state != pp2_header_done || !header) {
2258 			log_err_addr("proxy_protocol: wrong state for the "
2259 				"PROXYv2 header", "", &c->repinfo.remote_addr,
2260 				c->repinfo.remote_addrlen);
2261 			return 0;
2262 		}
2263 		if(!consume_pp2_header(c->buffer, &c->repinfo, 1)) {
2264 			log_err_addr("proxy_protocol: could not consume "
2265 				"PROXYv2 header", "", &c->repinfo.remote_addr,
2266 				c->repinfo.remote_addrlen);
2267 			return 0;
2268 		}
2269 		verbose(VERB_ALGO, "proxy_protocol: successful read of "
2270 			"PROXYv2 header");
2271 		/* Clear and reset the buffer to read the following
2272 		    * DNS packet(s). */
2273 		sldns_buffer_clear(c->buffer);
2274 		c->tcp_byte_count = 0;
2275 		return 1;
2276 	}
2277 
2278 	if(c->tcp_byte_count < sizeof(uint16_t)) {
2279 		/* read length bytes */
2280 		r = recv(fd,(void*)sldns_buffer_at(c->buffer,c->tcp_byte_count),
2281 			sizeof(uint16_t)-c->tcp_byte_count, MSG_DONTWAIT);
2282 		if(r == 0) {
2283 			if(c->tcp_req_info)
2284 				return tcp_req_info_handle_read_close(c->tcp_req_info);
2285 			return 0;
2286 		} else if(r == -1) {
2287 			if(c->pp2_enabled) goto recv_error;
2288 			goto recv_error_initial;
2289 		}
2290 		c->tcp_byte_count += r;
2291 		if(c->tcp_byte_count != sizeof(uint16_t))
2292 			return 1;
2293 		if(sldns_buffer_read_u16_at(c->buffer, 0) >
2294 			sldns_buffer_capacity(c->buffer)) {
2295 			verbose(VERB_QUERY, "tcp: dropped larger than buffer");
2296 			return 0;
2297 		}
2298 		sldns_buffer_set_limit(c->buffer,
2299 			sldns_buffer_read_u16_at(c->buffer, 0));
2300 		if(!short_ok &&
2301 			sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
2302 			verbose(VERB_QUERY, "tcp: dropped bogus too short.");
2303 			return 0;
2304 		}
2305 		verbose(VERB_ALGO, "Reading tcp query of length %d",
2306 			(int)sldns_buffer_limit(c->buffer));
2307 	}
2308 
2309 	if(sldns_buffer_remaining(c->buffer) == 0)
2310 		log_err("in comm_point_tcp_handle_read buffer_remaining is "
2311 			"not > 0 as expected, continuing with (harmless) 0 "
2312 			"length recv");
2313 	r = recv(fd, (void*)sldns_buffer_current(c->buffer),
2314 		sldns_buffer_remaining(c->buffer), MSG_DONTWAIT);
2315 	if(r == 0) {
2316 		if(c->tcp_req_info)
2317 			return tcp_req_info_handle_read_close(c->tcp_req_info);
2318 		return 0;
2319 	} else if(r == -1) {
2320 		goto recv_error;
2321 	}
2322 	sldns_buffer_skip(c->buffer, r);
2323 	if(sldns_buffer_remaining(c->buffer) <= 0) {
2324 		tcp_callback_reader(c);
2325 	}
2326 	return 1;
2327 
2328 recv_error_initial:
2329 	recv_initial = 1;
2330 recv_error:
2331 #ifndef USE_WINSOCK
2332 	if(errno == EINTR || errno == EAGAIN)
2333 		return 1;
2334 	if(recv_initial) {
2335 #ifdef ECONNRESET
2336 		if(errno == ECONNRESET && verbosity < 2)
2337 			return 0; /* silence reset by peer */
2338 #endif
2339 #ifdef ECONNREFUSED
2340 		if(errno == ECONNREFUSED && verbosity < 2)
2341 			return 0; /* silence reset by peer */
2342 #endif
2343 #ifdef ENETUNREACH
2344 		if(errno == ENETUNREACH && verbosity < 2)
2345 			return 0; /* silence it */
2346 #endif
2347 #ifdef EHOSTDOWN
2348 		if(errno == EHOSTDOWN && verbosity < 2)
2349 			return 0; /* silence it */
2350 #endif
2351 #ifdef EHOSTUNREACH
2352 		if(errno == EHOSTUNREACH && verbosity < 2)
2353 			return 0; /* silence it */
2354 #endif
2355 #ifdef ENETDOWN
2356 		if(errno == ENETDOWN && verbosity < 2)
2357 			return 0; /* silence it */
2358 #endif
2359 #ifdef EACCES
2360 		if(errno == EACCES && verbosity < 2)
2361 			return 0; /* silence it */
2362 #endif
2363 #ifdef ENOTCONN
2364 		if(errno == ENOTCONN) {
2365 			log_err_addr("read (in tcp s) failed and this "
2366 				"could be because TCP Fast Open is "
2367 				"enabled [--disable-tfo-client "
2368 				"--disable-tfo-server] but does not "
2369 				"work", sock_strerror(errno),
2370 				&c->repinfo.remote_addr,
2371 				c->repinfo.remote_addrlen);
2372 			return 0;
2373 		}
2374 #endif
2375 	}
2376 #else /* USE_WINSOCK */
2377 	if(recv_initial) {
2378 		if(WSAGetLastError() == WSAECONNREFUSED && verbosity < 2)
2379 			return 0;
2380 		if(WSAGetLastError() == WSAEHOSTDOWN && verbosity < 2)
2381 			return 0;
2382 		if(WSAGetLastError() == WSAEHOSTUNREACH && verbosity < 2)
2383 			return 0;
2384 		if(WSAGetLastError() == WSAENETDOWN && verbosity < 2)
2385 			return 0;
2386 		if(WSAGetLastError() == WSAENETUNREACH && verbosity < 2)
2387 			return 0;
2388 	}
2389 	if(WSAGetLastError() == WSAECONNRESET)
2390 		return 0;
2391 	if(WSAGetLastError() == WSAEINPROGRESS)
2392 		return 1;
2393 	if(WSAGetLastError() == WSAEWOULDBLOCK) {
2394 		ub_winsock_tcp_wouldblock(c->ev->ev,
2395 			UB_EV_READ);
2396 		return 1;
2397 	}
2398 #endif
2399 	log_err_addr("read (in tcp s)", sock_strerror(errno),
2400 		&c->repinfo.remote_addr, c->repinfo.remote_addrlen);
2401 	return 0;
2402 }
2403 
2404 /**
2405  * Handle tcp writing callback.
2406  * @param fd: file descriptor of socket.
2407  * @param c: comm point to write buffer out of.
2408  * @return: 0 on error
2409  */
2410 static int
2411 comm_point_tcp_handle_write(int fd, struct comm_point* c)
2412 {
2413 	ssize_t r;
2414 	struct sldns_buffer *buffer;
2415 	log_assert(c->type == comm_tcp);
2416 #ifdef USE_DNSCRYPT
2417 	buffer = c->dnscrypt_buffer;
2418 #else
2419 	buffer = c->buffer;
2420 #endif
2421 	if(c->tcp_is_reading && !c->ssl && !c->tcp_write_and_read)
2422 		return 0;
2423 	log_assert(fd != -1);
2424 	if(((!c->tcp_write_and_read && c->tcp_byte_count == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == 0)) && c->tcp_check_nb_connect) {
2425 		/* check for pending error from nonblocking connect */
2426 		/* from Stevens, unix network programming, vol1, 3rd ed, p450*/
2427 		int error = 0;
2428 		socklen_t len = (socklen_t)sizeof(error);
2429 		if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
2430 			&len) < 0){
2431 #ifndef USE_WINSOCK
2432 			error = errno; /* on solaris errno is error */
2433 #else /* USE_WINSOCK */
2434 			error = WSAGetLastError();
2435 #endif
2436 		}
2437 #ifndef USE_WINSOCK
2438 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
2439 		if(error == EINPROGRESS || error == EWOULDBLOCK)
2440 			return 1; /* try again later */
2441 		else
2442 #endif
2443 		if(error != 0 && verbosity < 2)
2444 			return 0; /* silence lots of chatter in the logs */
2445                 else if(error != 0) {
2446 			log_err_addr("tcp connect", strerror(error),
2447 				&c->repinfo.remote_addr,
2448 				c->repinfo.remote_addrlen);
2449 #else /* USE_WINSOCK */
2450 		/* examine error */
2451 		if(error == WSAEINPROGRESS)
2452 			return 1;
2453 		else if(error == WSAEWOULDBLOCK) {
2454 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2455 			return 1;
2456 		} else if(error != 0 && verbosity < 2)
2457 			return 0;
2458 		else if(error != 0) {
2459 			log_err_addr("tcp connect", wsa_strerror(error),
2460 				&c->repinfo.remote_addr,
2461 				c->repinfo.remote_addrlen);
2462 #endif /* USE_WINSOCK */
2463 			return 0;
2464 		}
2465 	}
2466 	if(c->ssl)
2467 		return ssl_handle_it(c, 1);
2468 
2469 #ifdef USE_MSG_FASTOPEN
2470 	/* Only try this on first use of a connection that uses tfo,
2471 	   otherwise fall through to normal write */
2472 	/* Also, TFO support on WINDOWS not implemented at the moment */
2473 	if(c->tcp_do_fastopen == 1) {
2474 		/* this form of sendmsg() does both a connect() and send() so need to
2475 		   look for various flavours of error*/
2476 		uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(buffer));
2477 		struct msghdr msg;
2478 		struct iovec iov[2];
2479 		c->tcp_do_fastopen = 0;
2480 		memset(&msg, 0, sizeof(msg));
2481 		if(c->tcp_write_and_read) {
2482 			iov[0].iov_base = (uint8_t*)&len + c->tcp_write_byte_count;
2483 			iov[0].iov_len = sizeof(uint16_t) - c->tcp_write_byte_count;
2484 			iov[1].iov_base = c->tcp_write_pkt;
2485 			iov[1].iov_len = c->tcp_write_pkt_len;
2486 		} else {
2487 			iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
2488 			iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
2489 			iov[1].iov_base = sldns_buffer_begin(buffer);
2490 			iov[1].iov_len = sldns_buffer_limit(buffer);
2491 		}
2492 		log_assert(iov[0].iov_len > 0);
2493 		msg.msg_name = &c->repinfo.remote_addr;
2494 		msg.msg_namelen = c->repinfo.remote_addrlen;
2495 		msg.msg_iov = iov;
2496 		msg.msg_iovlen = 2;
2497 		r = sendmsg(fd, &msg, MSG_FASTOPEN);
2498 		if (r == -1) {
2499 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
2500 			/* Handshake is underway, maybe because no TFO cookie available.
2501 			   Come back to write the message*/
2502 			if(errno == EINPROGRESS || errno == EWOULDBLOCK)
2503 				return 1;
2504 #endif
2505 			if(errno == EINTR || errno == EAGAIN)
2506 				return 1;
2507 			/* Not handling EISCONN here as shouldn't ever hit that case.*/
2508 			if(errno != EPIPE
2509 #ifdef EOPNOTSUPP
2510 				/* if /proc/sys/net/ipv4/tcp_fastopen is
2511 				 * disabled on Linux, sendmsg may return
2512 				 * 'Operation not supported', if so
2513 				 * fallthrough to ordinary connect. */
2514 				&& errno != EOPNOTSUPP
2515 #endif
2516 				&& errno != 0) {
2517 				if(verbosity < 2)
2518 					return 0; /* silence lots of chatter in the logs */
2519 				log_err_addr("tcp sendmsg", strerror(errno),
2520 					&c->repinfo.remote_addr,
2521 					c->repinfo.remote_addrlen);
2522 				return 0;
2523 			}
2524 			verbose(VERB_ALGO, "tcp sendmsg for fastopen failed (with %s), try normal connect", strerror(errno));
2525 			/* fallthrough to nonFASTOPEN
2526 			 * (MSG_FASTOPEN on Linux 3 produces EPIPE)
2527 			 * we need to perform connect() */
2528 			if(connect(fd, (struct sockaddr *)&c->repinfo.remote_addr,
2529 				c->repinfo.remote_addrlen) == -1) {
2530 #ifdef EINPROGRESS
2531 				if(errno == EINPROGRESS)
2532 					return 1; /* wait until connect done*/
2533 #endif
2534 #ifdef USE_WINSOCK
2535 				if(WSAGetLastError() == WSAEINPROGRESS ||
2536 					WSAGetLastError() == WSAEWOULDBLOCK)
2537 					return 1; /* wait until connect done*/
2538 #endif
2539 				if(tcp_connect_errno_needs_log(
2540 					(struct sockaddr *)&c->repinfo.remote_addr,
2541 					c->repinfo.remote_addrlen)) {
2542 					log_err_addr("outgoing tcp: connect after EPIPE for fastopen",
2543 						strerror(errno),
2544 						&c->repinfo.remote_addr,
2545 						c->repinfo.remote_addrlen);
2546 				}
2547 				return 0;
2548 			}
2549 
2550 		} else {
2551 			if(c->tcp_write_and_read) {
2552 				c->tcp_write_byte_count += r;
2553 				if(c->tcp_write_byte_count < sizeof(uint16_t))
2554 					return 1;
2555 			} else {
2556 				c->tcp_byte_count += r;
2557 				if(c->tcp_byte_count < sizeof(uint16_t))
2558 					return 1;
2559 				sldns_buffer_set_position(buffer, c->tcp_byte_count -
2560 					sizeof(uint16_t));
2561 			}
2562 			if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
2563 				tcp_callback_writer(c);
2564 				return 1;
2565 			}
2566 		}
2567 	}
2568 #endif /* USE_MSG_FASTOPEN */
2569 
2570 	if((c->tcp_write_and_read?c->tcp_write_byte_count:c->tcp_byte_count) < sizeof(uint16_t)) {
2571 		uint16_t len = htons(c->tcp_write_and_read?c->tcp_write_pkt_len:sldns_buffer_limit(buffer));
2572 #ifdef HAVE_WRITEV
2573 		struct iovec iov[2];
2574 		if(c->tcp_write_and_read) {
2575 			iov[0].iov_base = (uint8_t*)&len + c->tcp_write_byte_count;
2576 			iov[0].iov_len = sizeof(uint16_t) - c->tcp_write_byte_count;
2577 			iov[1].iov_base = c->tcp_write_pkt;
2578 			iov[1].iov_len = c->tcp_write_pkt_len;
2579 		} else {
2580 			iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
2581 			iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
2582 			iov[1].iov_base = sldns_buffer_begin(buffer);
2583 			iov[1].iov_len = sldns_buffer_limit(buffer);
2584 		}
2585 		log_assert(iov[0].iov_len > 0);
2586 		r = writev(fd, iov, 2);
2587 #else /* HAVE_WRITEV */
2588 		if(c->tcp_write_and_read) {
2589 			r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_write_byte_count),
2590 				sizeof(uint16_t)-c->tcp_write_byte_count, 0);
2591 		} else {
2592 			r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_byte_count),
2593 				sizeof(uint16_t)-c->tcp_byte_count, 0);
2594 		}
2595 #endif /* HAVE_WRITEV */
2596 		if(r == -1) {
2597 #ifndef USE_WINSOCK
2598 #  ifdef EPIPE
2599                 	if(errno == EPIPE && verbosity < 2)
2600                         	return 0; /* silence 'broken pipe' */
2601   #endif
2602 			if(errno == EINTR || errno == EAGAIN)
2603 				return 1;
2604 #ifdef ECONNRESET
2605 			if(errno == ECONNRESET && verbosity < 2)
2606 				return 0; /* silence reset by peer */
2607 #endif
2608 #  ifdef HAVE_WRITEV
2609 			log_err_addr("tcp writev", strerror(errno),
2610 				&c->repinfo.remote_addr,
2611 				c->repinfo.remote_addrlen);
2612 #  else /* HAVE_WRITEV */
2613 			log_err_addr("tcp send s", strerror(errno),
2614 				&c->repinfo.remote_addr,
2615 				c->repinfo.remote_addrlen);
2616 #  endif /* HAVE_WRITEV */
2617 #else
2618 			if(WSAGetLastError() == WSAENOTCONN)
2619 				return 1;
2620 			if(WSAGetLastError() == WSAEINPROGRESS)
2621 				return 1;
2622 			if(WSAGetLastError() == WSAEWOULDBLOCK) {
2623 				ub_winsock_tcp_wouldblock(c->ev->ev,
2624 					UB_EV_WRITE);
2625 				return 1;
2626 			}
2627 			if(WSAGetLastError() == WSAECONNRESET && verbosity < 2)
2628 				return 0; /* silence reset by peer */
2629 			log_err_addr("tcp send s",
2630 				wsa_strerror(WSAGetLastError()),
2631 				&c->repinfo.remote_addr,
2632 				c->repinfo.remote_addrlen);
2633 #endif
2634 			return 0;
2635 		}
2636 		if(c->tcp_write_and_read) {
2637 			c->tcp_write_byte_count += r;
2638 			if(c->tcp_write_byte_count < sizeof(uint16_t))
2639 				return 1;
2640 		} else {
2641 			c->tcp_byte_count += r;
2642 			if(c->tcp_byte_count < sizeof(uint16_t))
2643 				return 1;
2644 			sldns_buffer_set_position(buffer, c->tcp_byte_count -
2645 				sizeof(uint16_t));
2646 		}
2647 		if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
2648 			tcp_callback_writer(c);
2649 			return 1;
2650 		}
2651 	}
2652 	log_assert(c->tcp_write_and_read || sldns_buffer_remaining(buffer) > 0);
2653 	log_assert(!c->tcp_write_and_read || c->tcp_write_byte_count < c->tcp_write_pkt_len + 2);
2654 	if(c->tcp_write_and_read) {
2655 		r = send(fd, (void*)(c->tcp_write_pkt + c->tcp_write_byte_count - 2),
2656 			c->tcp_write_pkt_len + 2 - c->tcp_write_byte_count, 0);
2657 	} else {
2658 		r = send(fd, (void*)sldns_buffer_current(buffer),
2659 			sldns_buffer_remaining(buffer), 0);
2660 	}
2661 	if(r == -1) {
2662 #ifndef USE_WINSOCK
2663 		if(errno == EINTR || errno == EAGAIN)
2664 			return 1;
2665 #ifdef ECONNRESET
2666 		if(errno == ECONNRESET && verbosity < 2)
2667 			return 0; /* silence reset by peer */
2668 #endif
2669 #else
2670 		if(WSAGetLastError() == WSAEINPROGRESS)
2671 			return 1;
2672 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
2673 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
2674 			return 1;
2675 		}
2676 		if(WSAGetLastError() == WSAECONNRESET && verbosity < 2)
2677 			return 0; /* silence reset by peer */
2678 #endif
2679 		log_err_addr("tcp send r", sock_strerror(errno),
2680 			&c->repinfo.remote_addr,
2681 			c->repinfo.remote_addrlen);
2682 		return 0;
2683 	}
2684 	if(c->tcp_write_and_read) {
2685 		c->tcp_write_byte_count += r;
2686 	} else {
2687 		sldns_buffer_skip(buffer, r);
2688 	}
2689 
2690 	if((!c->tcp_write_and_read && sldns_buffer_remaining(buffer) == 0) || (c->tcp_write_and_read && c->tcp_write_byte_count == c->tcp_write_pkt_len + 2)) {
2691 		tcp_callback_writer(c);
2692 	}
2693 
2694 	return 1;
2695 }
2696 
2697 /** read again to drain buffers when there could be more to read, returns 0
2698  * on failure which means the comm point is closed. */
2699 static int
2700 tcp_req_info_read_again(int fd, struct comm_point* c)
2701 {
2702 	while(c->tcp_req_info->read_again) {
2703 		int r;
2704 		c->tcp_req_info->read_again = 0;
2705 		if(c->tcp_is_reading)
2706 			r = comm_point_tcp_handle_read(fd, c, 0);
2707 		else 	r = comm_point_tcp_handle_write(fd, c);
2708 		if(!r) {
2709 			reclaim_tcp_handler(c);
2710 			if(!c->tcp_do_close) {
2711 				fptr_ok(fptr_whitelist_comm_point(
2712 					c->callback));
2713 				(void)(*c->callback)(c, c->cb_arg,
2714 					NETEVENT_CLOSED, NULL);
2715 			}
2716 			return 0;
2717 		}
2718 	}
2719 	return 1;
2720 }
2721 
2722 /** read again to drain buffers when there could be more to read */
2723 static void
2724 tcp_more_read_again(int fd, struct comm_point* c)
2725 {
2726 	/* if the packet is done, but another one could be waiting on
2727 	 * the connection, the callback signals this, and we try again */
2728 	/* this continues until the read routines get EAGAIN or so,
2729 	 * and thus does not call the callback, and the bool is 0 */
2730 	int* moreread = c->tcp_more_read_again;
2731 	while(moreread && *moreread) {
2732 		*moreread = 0;
2733 		if(!comm_point_tcp_handle_read(fd, c, 0)) {
2734 			reclaim_tcp_handler(c);
2735 			if(!c->tcp_do_close) {
2736 				fptr_ok(fptr_whitelist_comm_point(
2737 					c->callback));
2738 				(void)(*c->callback)(c, c->cb_arg,
2739 					NETEVENT_CLOSED, NULL);
2740 			}
2741 			return;
2742 		}
2743 	}
2744 }
2745 
2746 /** write again to fill up when there could be more to write */
2747 static void
2748 tcp_more_write_again(int fd, struct comm_point* c)
2749 {
2750 	/* if the packet is done, but another is waiting to be written,
2751 	 * the callback signals it and we try again. */
2752 	/* this continues until the write routines get EAGAIN or so,
2753 	 * and thus does not call the callback, and the bool is 0 */
2754 	int* morewrite = c->tcp_more_write_again;
2755 	while(morewrite && *morewrite) {
2756 		*morewrite = 0;
2757 		if(!comm_point_tcp_handle_write(fd, c)) {
2758 			reclaim_tcp_handler(c);
2759 			if(!c->tcp_do_close) {
2760 				fptr_ok(fptr_whitelist_comm_point(
2761 					c->callback));
2762 				(void)(*c->callback)(c, c->cb_arg,
2763 					NETEVENT_CLOSED, NULL);
2764 			}
2765 			return;
2766 		}
2767 	}
2768 }
2769 
2770 void
2771 comm_point_tcp_handle_callback(int fd, short event, void* arg)
2772 {
2773 	struct comm_point* c = (struct comm_point*)arg;
2774 	log_assert(c->type == comm_tcp);
2775 	ub_comm_base_now(c->ev->base);
2776 
2777 	if(c->fd == -1 || c->fd != fd)
2778 		return; /* duplicate event, but commpoint closed. */
2779 
2780 #ifdef USE_DNSCRYPT
2781 	/* Initialize if this is a dnscrypt socket */
2782 	if(c->tcp_parent) {
2783 		c->dnscrypt = c->tcp_parent->dnscrypt;
2784 	}
2785 	if(c->dnscrypt && c->dnscrypt_buffer == c->buffer) {
2786 		c->dnscrypt_buffer = sldns_buffer_new(sldns_buffer_capacity(c->buffer));
2787 		if(!c->dnscrypt_buffer) {
2788 			log_err("Could not allocate dnscrypt buffer");
2789 			reclaim_tcp_handler(c);
2790 			if(!c->tcp_do_close) {
2791 				fptr_ok(fptr_whitelist_comm_point(
2792 					c->callback));
2793 				(void)(*c->callback)(c, c->cb_arg,
2794 					NETEVENT_CLOSED, NULL);
2795 			}
2796 			return;
2797 		}
2798 	}
2799 #endif
2800 
2801 	if(event&UB_EV_TIMEOUT) {
2802 		verbose(VERB_QUERY, "tcp took too long, dropped");
2803 		reclaim_tcp_handler(c);
2804 		if(!c->tcp_do_close) {
2805 			fptr_ok(fptr_whitelist_comm_point(c->callback));
2806 			(void)(*c->callback)(c, c->cb_arg,
2807 				NETEVENT_TIMEOUT, NULL);
2808 		}
2809 		return;
2810 	}
2811 	if(event&UB_EV_READ
2812 #ifdef USE_MSG_FASTOPEN
2813 		&& !(c->tcp_do_fastopen && (event&UB_EV_WRITE))
2814 #endif
2815 		) {
2816 		int has_tcpq = (c->tcp_req_info != NULL);
2817 		int* moreread = c->tcp_more_read_again;
2818 		if(!comm_point_tcp_handle_read(fd, c, 0)) {
2819 			reclaim_tcp_handler(c);
2820 			if(!c->tcp_do_close) {
2821 				fptr_ok(fptr_whitelist_comm_point(
2822 					c->callback));
2823 				(void)(*c->callback)(c, c->cb_arg,
2824 					NETEVENT_CLOSED, NULL);
2825 			}
2826 			return;
2827 		}
2828 		if(has_tcpq && c->tcp_req_info && c->tcp_req_info->read_again) {
2829 			if(!tcp_req_info_read_again(fd, c))
2830 				return;
2831 		}
2832 		if(moreread && *moreread)
2833 			tcp_more_read_again(fd, c);
2834 		return;
2835 	}
2836 	if(event&UB_EV_WRITE) {
2837 		int has_tcpq = (c->tcp_req_info != NULL);
2838 		int* morewrite = c->tcp_more_write_again;
2839 		if(!comm_point_tcp_handle_write(fd, c)) {
2840 			reclaim_tcp_handler(c);
2841 			if(!c->tcp_do_close) {
2842 				fptr_ok(fptr_whitelist_comm_point(
2843 					c->callback));
2844 				(void)(*c->callback)(c, c->cb_arg,
2845 					NETEVENT_CLOSED, NULL);
2846 			}
2847 			return;
2848 		}
2849 		if(has_tcpq && c->tcp_req_info && c->tcp_req_info->read_again) {
2850 			if(!tcp_req_info_read_again(fd, c))
2851 				return;
2852 		}
2853 		if(morewrite && *morewrite)
2854 			tcp_more_write_again(fd, c);
2855 		return;
2856 	}
2857 	log_err("Ignored event %d for tcphdl.", event);
2858 }
2859 
2860 /** Make http handler free for next assignment */
2861 static void
2862 reclaim_http_handler(struct comm_point* c)
2863 {
2864 	log_assert(c->type == comm_http);
2865 	if(c->ssl) {
2866 #ifdef HAVE_SSL
2867 		SSL_shutdown(c->ssl);
2868 		SSL_free(c->ssl);
2869 		c->ssl = NULL;
2870 #endif
2871 	}
2872 	comm_point_close(c);
2873 	if(c->tcp_parent) {
2874 		if(c != c->tcp_parent->tcp_free) {
2875 			c->tcp_parent->cur_tcp_count--;
2876 			c->tcp_free = c->tcp_parent->tcp_free;
2877 			c->tcp_parent->tcp_free = c;
2878 		}
2879 		if(!c->tcp_free) {
2880 			/* re-enable listening on accept socket */
2881 			comm_point_start_listening(c->tcp_parent, -1, -1);
2882 		}
2883 	}
2884 }
2885 
2886 /** read more data for http (with ssl) */
2887 static int
2888 ssl_http_read_more(struct comm_point* c)
2889 {
2890 #ifdef HAVE_SSL
2891 	int r;
2892 	log_assert(sldns_buffer_remaining(c->buffer) > 0);
2893 	ERR_clear_error();
2894 	r = SSL_read(c->ssl, (void*)sldns_buffer_current(c->buffer),
2895 		(int)sldns_buffer_remaining(c->buffer));
2896 	if(r <= 0) {
2897 		int want = SSL_get_error(c->ssl, r);
2898 		if(want == SSL_ERROR_ZERO_RETURN) {
2899 			return 0; /* shutdown, closed */
2900 		} else if(want == SSL_ERROR_WANT_READ) {
2901 			return 1; /* read more later */
2902 		} else if(want == SSL_ERROR_WANT_WRITE) {
2903 			c->ssl_shake_state = comm_ssl_shake_hs_write;
2904 			comm_point_listen_for_rw(c, 0, 1);
2905 			return 1;
2906 		} else if(want == SSL_ERROR_SYSCALL) {
2907 #ifdef ECONNRESET
2908 			if(errno == ECONNRESET && verbosity < 2)
2909 				return 0; /* silence reset by peer */
2910 #endif
2911 			if(errno != 0)
2912 				log_err("SSL_read syscall: %s",
2913 					strerror(errno));
2914 			return 0;
2915 		}
2916 		log_crypto_err("could not SSL_read");
2917 		return 0;
2918 	}
2919 	verbose(VERB_ALGO, "ssl http read more skip to %d + %d",
2920 		(int)sldns_buffer_position(c->buffer), (int)r);
2921 	sldns_buffer_skip(c->buffer, (ssize_t)r);
2922 	return 1;
2923 #else
2924 	(void)c;
2925 	return 0;
2926 #endif /* HAVE_SSL */
2927 }
2928 
2929 /** read more data for http */
2930 static int
2931 http_read_more(int fd, struct comm_point* c)
2932 {
2933 	ssize_t r;
2934 	log_assert(sldns_buffer_remaining(c->buffer) > 0);
2935 	r = recv(fd, (void*)sldns_buffer_current(c->buffer),
2936 		sldns_buffer_remaining(c->buffer), MSG_DONTWAIT);
2937 	if(r == 0) {
2938 		return 0;
2939 	} else if(r == -1) {
2940 #ifndef USE_WINSOCK
2941 		if(errno == EINTR || errno == EAGAIN)
2942 			return 1;
2943 #else /* USE_WINSOCK */
2944 		if(WSAGetLastError() == WSAECONNRESET)
2945 			return 0;
2946 		if(WSAGetLastError() == WSAEINPROGRESS)
2947 			return 1;
2948 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
2949 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
2950 			return 1;
2951 		}
2952 #endif
2953 		log_err_addr("read (in http r)", sock_strerror(errno),
2954 			&c->repinfo.remote_addr, c->repinfo.remote_addrlen);
2955 		return 0;
2956 	}
2957 	verbose(VERB_ALGO, "http read more skip to %d + %d",
2958 		(int)sldns_buffer_position(c->buffer), (int)r);
2959 	sldns_buffer_skip(c->buffer, r);
2960 	return 1;
2961 }
2962 
2963 /** return true if http header has been read (one line complete) */
2964 static int
2965 http_header_done(sldns_buffer* buf)
2966 {
2967 	size_t i;
2968 	for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) {
2969 		/* there was a \r before the \n, but we ignore that */
2970 		if((char)sldns_buffer_read_u8_at(buf, i) == '\n')
2971 			return 1;
2972 	}
2973 	return 0;
2974 }
2975 
2976 /** return character string into buffer for header line, moves buffer
2977  * past that line and puts zero terminator into linefeed-newline */
2978 static char*
2979 http_header_line(sldns_buffer* buf)
2980 {
2981 	char* result = (char*)sldns_buffer_current(buf);
2982 	size_t i;
2983 	for(i=sldns_buffer_position(buf); i<sldns_buffer_limit(buf); i++) {
2984 		/* terminate the string on the \r */
2985 		if((char)sldns_buffer_read_u8_at(buf, i) == '\r')
2986 			sldns_buffer_write_u8_at(buf, i, 0);
2987 		/* terminate on the \n and skip past the it and done */
2988 		if((char)sldns_buffer_read_u8_at(buf, i) == '\n') {
2989 			sldns_buffer_write_u8_at(buf, i, 0);
2990 			sldns_buffer_set_position(buf, i+1);
2991 			return result;
2992 		}
2993 	}
2994 	return NULL;
2995 }
2996 
2997 /** move unread buffer to start and clear rest for putting the rest into it */
2998 static void
2999 http_moveover_buffer(sldns_buffer* buf)
3000 {
3001 	size_t pos = sldns_buffer_position(buf);
3002 	size_t len = sldns_buffer_remaining(buf);
3003 	sldns_buffer_clear(buf);
3004 	memmove(sldns_buffer_begin(buf), sldns_buffer_at(buf, pos), len);
3005 	sldns_buffer_set_position(buf, len);
3006 }
3007 
3008 /** a http header is complete, process it */
3009 static int
3010 http_process_initial_header(struct comm_point* c)
3011 {
3012 	char* line = http_header_line(c->buffer);
3013 	if(!line) return 1;
3014 	verbose(VERB_ALGO, "http header: %s", line);
3015 	if(strncasecmp(line, "HTTP/1.1 ", 9) == 0) {
3016 		/* check returncode */
3017 		if(line[9] != '2') {
3018 			verbose(VERB_ALGO, "http bad status %s", line+9);
3019 			return 0;
3020 		}
3021 	} else if(strncasecmp(line, "Content-Length: ", 16) == 0) {
3022 		if(!c->http_is_chunked)
3023 			c->tcp_byte_count = (size_t)atoi(line+16);
3024 	} else if(strncasecmp(line, "Transfer-Encoding: chunked", 19+7) == 0) {
3025 		c->tcp_byte_count = 0;
3026 		c->http_is_chunked = 1;
3027 	} else if(line[0] == 0) {
3028 		/* end of initial headers */
3029 		c->http_in_headers = 0;
3030 		if(c->http_is_chunked)
3031 			c->http_in_chunk_headers = 1;
3032 		/* remove header text from front of buffer
3033 		 * the buffer is going to be used to return the data segment
3034 		 * itself and we don't want the header to get returned
3035 		 * prepended with it */
3036 		http_moveover_buffer(c->buffer);
3037 		sldns_buffer_flip(c->buffer);
3038 		return 1;
3039 	}
3040 	/* ignore other headers */
3041 	return 1;
3042 }
3043 
3044 /** a chunk header is complete, process it, return 0=fail, 1=continue next
3045  * header line, 2=done with chunked transfer*/
3046 static int
3047 http_process_chunk_header(struct comm_point* c)
3048 {
3049 	char* line = http_header_line(c->buffer);
3050 	if(!line) return 1;
3051 	if(c->http_in_chunk_headers == 3) {
3052 		verbose(VERB_ALGO, "http chunk trailer: %s", line);
3053 		/* are we done ? */
3054 		if(line[0] == 0 && c->tcp_byte_count == 0) {
3055 			/* callback of http reader when NETEVENT_DONE,
3056 			 * end of data, with no data in buffer */
3057 			sldns_buffer_set_position(c->buffer, 0);
3058 			sldns_buffer_set_limit(c->buffer, 0);
3059 			fptr_ok(fptr_whitelist_comm_point(c->callback));
3060 			(void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL);
3061 			/* return that we are done */
3062 			return 2;
3063 		}
3064 		if(line[0] == 0) {
3065 			/* continue with header of the next chunk */
3066 			c->http_in_chunk_headers = 1;
3067 			/* remove header text from front of buffer */
3068 			http_moveover_buffer(c->buffer);
3069 			sldns_buffer_flip(c->buffer);
3070 			return 1;
3071 		}
3072 		/* ignore further trail headers */
3073 		return 1;
3074 	}
3075 	verbose(VERB_ALGO, "http chunk header: %s", line);
3076 	if(c->http_in_chunk_headers == 1) {
3077 		/* read chunked start line */
3078 		char* end = NULL;
3079 		c->tcp_byte_count = (size_t)strtol(line, &end, 16);
3080 		if(end == line)
3081 			return 0;
3082 		c->http_in_chunk_headers = 0;
3083 		/* remove header text from front of buffer */
3084 		http_moveover_buffer(c->buffer);
3085 		sldns_buffer_flip(c->buffer);
3086 		if(c->tcp_byte_count == 0) {
3087 			/* done with chunks, process chunk_trailer lines */
3088 			c->http_in_chunk_headers = 3;
3089 		}
3090 		return 1;
3091 	}
3092 	/* ignore other headers */
3093 	return 1;
3094 }
3095 
3096 /** handle nonchunked data segment, 0=fail, 1=wait */
3097 static int
3098 http_nonchunk_segment(struct comm_point* c)
3099 {
3100 	/* c->buffer at position..limit has new data we read in.
3101 	 * the buffer itself is full of nonchunked data.
3102 	 * we are looking to read tcp_byte_count more data
3103 	 * and then the transfer is done. */
3104 	size_t remainbufferlen;
3105 	size_t got_now = sldns_buffer_limit(c->buffer);
3106 	if(c->tcp_byte_count <= got_now) {
3107 		/* done, this is the last data fragment */
3108 		c->http_stored = 0;
3109 		sldns_buffer_set_position(c->buffer, 0);
3110 		fptr_ok(fptr_whitelist_comm_point(c->callback));
3111 		(void)(*c->callback)(c, c->cb_arg, NETEVENT_DONE, NULL);
3112 		return 1;
3113 	}
3114 	/* if we have the buffer space,
3115 	 * read more data collected into the buffer */
3116 	remainbufferlen = sldns_buffer_capacity(c->buffer) -
3117 		sldns_buffer_limit(c->buffer);
3118 	if(remainbufferlen+got_now >= c->tcp_byte_count ||
3119 		remainbufferlen >= (size_t)(c->ssl?16384:2048)) {
3120 		size_t total = sldns_buffer_limit(c->buffer);
3121 		sldns_buffer_clear(c->buffer);
3122 		sldns_buffer_set_position(c->buffer, total);
3123 		c->http_stored = total;
3124 		/* return and wait to read more */
3125 		return 1;
3126 	}
3127 	/* call callback with this data amount, then
3128 	 * wait for more */
3129 	c->tcp_byte_count -= got_now;
3130 	c->http_stored = 0;
3131 	sldns_buffer_set_position(c->buffer, 0);
3132 	fptr_ok(fptr_whitelist_comm_point(c->callback));
3133 	(void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL);
3134 	/* c->callback has to buffer_clear(c->buffer). */
3135 	/* return and wait to read more */
3136 	return 1;
3137 }
3138 
3139 /** handle chunked data segment, return 0=fail, 1=wait, 2=process more */
3140 static int
3141 http_chunked_segment(struct comm_point* c)
3142 {
3143 	/* the c->buffer has from position..limit new data we read. */
3144 	/* the current chunk has length tcp_byte_count.
3145 	 * once we read that read more chunk headers.
3146 	 */
3147 	size_t remainbufferlen;
3148 	size_t got_now = sldns_buffer_limit(c->buffer) - c->http_stored;
3149 	verbose(VERB_ALGO, "http_chunked_segment: got now %d, tcpbytcount %d, http_stored %d, buffer pos %d, buffer limit %d", (int)got_now, (int)c->tcp_byte_count, (int)c->http_stored, (int)sldns_buffer_position(c->buffer), (int)sldns_buffer_limit(c->buffer));
3150 	if(c->tcp_byte_count <= got_now) {
3151 		/* the chunk has completed (with perhaps some extra data
3152 		 * from next chunk header and next chunk) */
3153 		/* save too much info into temp buffer */
3154 		size_t fraglen;
3155 		struct comm_reply repinfo;
3156 		c->http_stored = 0;
3157 		sldns_buffer_skip(c->buffer, (ssize_t)c->tcp_byte_count);
3158 		sldns_buffer_clear(c->http_temp);
3159 		sldns_buffer_write(c->http_temp,
3160 			sldns_buffer_current(c->buffer),
3161 			sldns_buffer_remaining(c->buffer));
3162 		sldns_buffer_flip(c->http_temp);
3163 
3164 		/* callback with this fragment */
3165 		fraglen = sldns_buffer_position(c->buffer);
3166 		sldns_buffer_set_position(c->buffer, 0);
3167 		sldns_buffer_set_limit(c->buffer, fraglen);
3168 		repinfo = c->repinfo;
3169 		fptr_ok(fptr_whitelist_comm_point(c->callback));
3170 		(void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &repinfo);
3171 		/* c->callback has to buffer_clear(). */
3172 
3173 		/* is commpoint deleted? */
3174 		if(!repinfo.c) {
3175 			return 1;
3176 		}
3177 		/* copy waiting info */
3178 		sldns_buffer_clear(c->buffer);
3179 		sldns_buffer_write(c->buffer,
3180 			sldns_buffer_begin(c->http_temp),
3181 			sldns_buffer_remaining(c->http_temp));
3182 		sldns_buffer_flip(c->buffer);
3183 		/* process end of chunk trailer header lines, until
3184 		 * an empty line */
3185 		c->http_in_chunk_headers = 3;
3186 		/* process more data in buffer (if any) */
3187 		return 2;
3188 	}
3189 	c->tcp_byte_count -= got_now;
3190 
3191 	/* if we have the buffer space,
3192 	 * read more data collected into the buffer */
3193 	remainbufferlen = sldns_buffer_capacity(c->buffer) -
3194 		sldns_buffer_limit(c->buffer);
3195 	if(remainbufferlen >= c->tcp_byte_count ||
3196 		remainbufferlen >= 2048) {
3197 		size_t total = sldns_buffer_limit(c->buffer);
3198 		sldns_buffer_clear(c->buffer);
3199 		sldns_buffer_set_position(c->buffer, total);
3200 		c->http_stored = total;
3201 		/* return and wait to read more */
3202 		return 1;
3203 	}
3204 
3205 	/* callback of http reader for a new part of the data */
3206 	c->http_stored = 0;
3207 	sldns_buffer_set_position(c->buffer, 0);
3208 	fptr_ok(fptr_whitelist_comm_point(c->callback));
3209 	(void)(*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, NULL);
3210 	/* c->callback has to buffer_clear(c->buffer). */
3211 	/* return and wait to read more */
3212 	return 1;
3213 }
3214 
3215 #ifdef HAVE_NGHTTP2
3216 /** Create new http2 session. Called when creating handling comm point. */
3217 static struct http2_session* http2_session_create(struct comm_point* c)
3218 {
3219 	struct http2_session* session = calloc(1, sizeof(*session));
3220 	if(!session) {
3221 		log_err("malloc failure while creating http2 session");
3222 		return NULL;
3223 	}
3224 	session->c = c;
3225 
3226 	return session;
3227 }
3228 #endif
3229 
3230 /** Delete http2 session. After closing connection or on error */
3231 static void http2_session_delete(struct http2_session* h2_session)
3232 {
3233 #ifdef HAVE_NGHTTP2
3234 	if(h2_session->callbacks)
3235 		nghttp2_session_callbacks_del(h2_session->callbacks);
3236 	free(h2_session);
3237 #else
3238 	(void)h2_session;
3239 #endif
3240 }
3241 
3242 #ifdef HAVE_NGHTTP2
3243 struct http2_stream* http2_stream_create(int32_t stream_id)
3244 {
3245 	struct http2_stream* h2_stream = calloc(1, sizeof(*h2_stream));
3246 	if(!h2_stream) {
3247 		log_err("malloc failure while creating http2 stream");
3248 		return NULL;
3249 	}
3250 	h2_stream->stream_id = stream_id;
3251 	return h2_stream;
3252 }
3253 
3254 /** Delete http2 stream. After session delete or stream close callback */
3255 static void http2_stream_delete(struct http2_session* h2_session,
3256 	struct http2_stream* h2_stream)
3257 {
3258 	if(h2_stream->mesh_state) {
3259 		mesh_state_remove_reply(h2_stream->mesh, h2_stream->mesh_state,
3260 			h2_session->c);
3261 		h2_stream->mesh_state = NULL;
3262 	}
3263 	http2_req_stream_clear(h2_stream);
3264 	free(h2_stream);
3265 }
3266 #endif
3267 
3268 void http2_stream_add_meshstate(struct http2_stream* h2_stream,
3269 	struct mesh_area* mesh, struct mesh_state* m)
3270 {
3271 	h2_stream->mesh = mesh;
3272 	h2_stream->mesh_state = m;
3273 }
3274 
3275 /** delete http2 session server. After closing connection. */
3276 static void http2_session_server_delete(struct http2_session* h2_session)
3277 {
3278 #ifdef HAVE_NGHTTP2
3279 	struct http2_stream* h2_stream, *next;
3280 	nghttp2_session_del(h2_session->session); /* NULL input is fine */
3281 	h2_session->session = NULL;
3282 	for(h2_stream = h2_session->first_stream; h2_stream;) {
3283 		next = h2_stream->next;
3284 		http2_stream_delete(h2_session, h2_stream);
3285 		h2_stream = next;
3286 	}
3287 	h2_session->first_stream = NULL;
3288 	h2_session->is_drop = 0;
3289 	h2_session->postpone_drop = 0;
3290 	h2_session->c->h2_stream = NULL;
3291 #endif
3292 	(void)h2_session;
3293 }
3294 
3295 #ifdef HAVE_NGHTTP2
3296 void http2_session_add_stream(struct http2_session* h2_session,
3297 	struct http2_stream* h2_stream)
3298 {
3299 	if(h2_session->first_stream)
3300 		h2_session->first_stream->prev = h2_stream;
3301 	h2_stream->next = h2_session->first_stream;
3302 	h2_session->first_stream = h2_stream;
3303 }
3304 
3305 /** remove stream from session linked list. After stream close callback or
3306  * closing connection */
3307 static void http2_session_remove_stream(struct http2_session* h2_session,
3308 	struct http2_stream* h2_stream)
3309 {
3310 	if(h2_stream->prev)
3311 		h2_stream->prev->next = h2_stream->next;
3312 	else
3313 		h2_session->first_stream = h2_stream->next;
3314 	if(h2_stream->next)
3315 		h2_stream->next->prev = h2_stream->prev;
3316 
3317 }
3318 
3319 int http2_stream_close_cb(nghttp2_session* ATTR_UNUSED(session),
3320 	int32_t stream_id, uint32_t ATTR_UNUSED(error_code), void* cb_arg)
3321 {
3322 	struct http2_stream* h2_stream;
3323 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
3324 	if(!(h2_stream = nghttp2_session_get_stream_user_data(
3325 		h2_session->session, stream_id))) {
3326 		return 0;
3327 	}
3328 	http2_session_remove_stream(h2_session, h2_stream);
3329 	http2_stream_delete(h2_session, h2_stream);
3330 	return 0;
3331 }
3332 
3333 ssize_t http2_recv_cb(nghttp2_session* ATTR_UNUSED(session), uint8_t* buf,
3334 	size_t len, int ATTR_UNUSED(flags), void* cb_arg)
3335 {
3336 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
3337 	ssize_t ret;
3338 
3339 	log_assert(h2_session->c->type == comm_http);
3340 	log_assert(h2_session->c->h2_session);
3341 
3342 #ifdef HAVE_SSL
3343 	if(h2_session->c->ssl) {
3344 		int r;
3345 		ERR_clear_error();
3346 		r = SSL_read(h2_session->c->ssl, buf, len);
3347 		if(r <= 0) {
3348 			int want = SSL_get_error(h2_session->c->ssl, r);
3349 			if(want == SSL_ERROR_ZERO_RETURN) {
3350 				return NGHTTP2_ERR_EOF;
3351 			} else if(want == SSL_ERROR_WANT_READ) {
3352 				return NGHTTP2_ERR_WOULDBLOCK;
3353 			} else if(want == SSL_ERROR_WANT_WRITE) {
3354 				h2_session->c->ssl_shake_state = comm_ssl_shake_hs_write;
3355 				comm_point_listen_for_rw(h2_session->c, 0, 1);
3356 				return NGHTTP2_ERR_WOULDBLOCK;
3357 			} else if(want == SSL_ERROR_SYSCALL) {
3358 #ifdef ECONNRESET
3359 				if(errno == ECONNRESET && verbosity < 2)
3360 					return NGHTTP2_ERR_CALLBACK_FAILURE;
3361 #endif
3362 				if(errno != 0)
3363 					log_err("SSL_read syscall: %s",
3364 						strerror(errno));
3365 				return NGHTTP2_ERR_CALLBACK_FAILURE;
3366 			}
3367 			log_crypto_err("could not SSL_read");
3368 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3369 		}
3370 		return r;
3371 	}
3372 #endif /* HAVE_SSL */
3373 
3374 	ret = recv(h2_session->c->fd, buf, len, MSG_DONTWAIT);
3375 	if(ret == 0) {
3376 		return NGHTTP2_ERR_EOF;
3377 	} else if(ret < 0) {
3378 #ifndef USE_WINSOCK
3379 		if(errno == EINTR || errno == EAGAIN)
3380 			return NGHTTP2_ERR_WOULDBLOCK;
3381 #ifdef ECONNRESET
3382 		if(errno == ECONNRESET && verbosity < 2)
3383 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3384 #endif
3385 		log_err_addr("could not http2 recv: %s", strerror(errno),
3386 			&h2_session->c->repinfo.remote_addr,
3387 			h2_session->c->repinfo.remote_addrlen);
3388 #else /* USE_WINSOCK */
3389 		if(WSAGetLastError() == WSAECONNRESET)
3390 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3391 		if(WSAGetLastError() == WSAEINPROGRESS)
3392 			return NGHTTP2_ERR_WOULDBLOCK;
3393 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
3394 			ub_winsock_tcp_wouldblock(h2_session->c->ev->ev,
3395 				UB_EV_READ);
3396 			return NGHTTP2_ERR_WOULDBLOCK;
3397 		}
3398 		log_err_addr("could not http2 recv: %s",
3399 			wsa_strerror(WSAGetLastError()),
3400 			&h2_session->c->repinfo.remote_addr,
3401 			h2_session->c->repinfo.remote_addrlen);
3402 #endif
3403 		return NGHTTP2_ERR_CALLBACK_FAILURE;
3404 	}
3405 	return ret;
3406 }
3407 #endif /* HAVE_NGHTTP2 */
3408 
3409 /** Handle http2 read */
3410 static int
3411 comm_point_http2_handle_read(int ATTR_UNUSED(fd), struct comm_point* c)
3412 {
3413 #ifdef HAVE_NGHTTP2
3414 	int ret;
3415 	log_assert(c->h2_session);
3416 
3417 	/* reading until recv cb returns NGHTTP2_ERR_WOULDBLOCK */
3418 	ret = nghttp2_session_recv(c->h2_session->session);
3419 	if(ret) {
3420 		if(ret != NGHTTP2_ERR_EOF &&
3421 			ret != NGHTTP2_ERR_CALLBACK_FAILURE) {
3422 			char a[256];
3423 			addr_to_str(&c->repinfo.remote_addr,
3424 				c->repinfo.remote_addrlen, a, sizeof(a));
3425 			verbose(VERB_QUERY, "http2: session_recv from %s failed, "
3426 				"error: %s", a, nghttp2_strerror(ret));
3427 		}
3428 		return 0;
3429 	}
3430 	if(nghttp2_session_want_write(c->h2_session->session)) {
3431 		c->tcp_is_reading = 0;
3432 		comm_point_stop_listening(c);
3433 		comm_point_start_listening(c, -1, adjusted_tcp_timeout(c));
3434 	} else if(!nghttp2_session_want_read(c->h2_session->session))
3435 		return 0; /* connection can be closed */
3436 	return 1;
3437 #else
3438 	(void)c;
3439 	return 0;
3440 #endif
3441 }
3442 
3443 /**
3444  * Handle http reading callback.
3445  * @param fd: file descriptor of socket.
3446  * @param c: comm point to read from into buffer.
3447  * @return: 0 on error
3448  */
3449 static int
3450 comm_point_http_handle_read(int fd, struct comm_point* c)
3451 {
3452 	log_assert(c->type == comm_http);
3453 	log_assert(fd != -1);
3454 
3455 	/* if we are in ssl handshake, handle SSL handshake */
3456 #ifdef HAVE_SSL
3457 	if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
3458 		if(!ssl_handshake(c))
3459 			return 0;
3460 		if(c->ssl_shake_state != comm_ssl_shake_none)
3461 			return 1;
3462 	}
3463 #endif /* HAVE_SSL */
3464 
3465 	if(!c->tcp_is_reading)
3466 		return 1;
3467 
3468 	if(c->use_h2) {
3469 		return comm_point_http2_handle_read(fd, c);
3470 	}
3471 
3472 	/* http version is <= http/1.1 */
3473 
3474 	if(c->http_min_version >= http_version_2) {
3475 		/* HTTP/2 failed, not allowed to use lower version. */
3476 		return 0;
3477 	}
3478 
3479 	/* read more data */
3480 	if(c->ssl) {
3481 		if(!ssl_http_read_more(c))
3482 			return 0;
3483 	} else {
3484 		if(!http_read_more(fd, c))
3485 			return 0;
3486 	}
3487 
3488 	if(c->http_stored >= sldns_buffer_position(c->buffer)) {
3489 		/* read did not work but we wanted more data, there is
3490 		 * no bytes to process now. */
3491 		return 1;
3492 	}
3493 	sldns_buffer_flip(c->buffer);
3494 	/* if we are partway in a segment of data, position us at the point
3495 	 * where we left off previously */
3496 	if(c->http_stored < sldns_buffer_limit(c->buffer))
3497 		sldns_buffer_set_position(c->buffer, c->http_stored);
3498 	else	sldns_buffer_set_position(c->buffer, sldns_buffer_limit(c->buffer));
3499 
3500 	while(sldns_buffer_remaining(c->buffer) > 0) {
3501 		/* Handle HTTP/1.x data */
3502 		/* if we are reading headers, read more headers */
3503 		if(c->http_in_headers || c->http_in_chunk_headers) {
3504 			/* if header is done, process the header */
3505 			if(!http_header_done(c->buffer)) {
3506 				/* copy remaining data to front of buffer
3507 				 * and set rest for writing into it */
3508 				http_moveover_buffer(c->buffer);
3509 				/* return and wait to read more */
3510 				return 1;
3511 			}
3512 			if(!c->http_in_chunk_headers) {
3513 				/* process initial headers */
3514 				if(!http_process_initial_header(c))
3515 					return 0;
3516 			} else {
3517 				/* process chunk headers */
3518 				int r = http_process_chunk_header(c);
3519 				if(r == 0) return 0;
3520 				if(r == 2) return 1; /* done */
3521 				/* r == 1, continue */
3522 			}
3523 			/* see if we have more to process */
3524 			continue;
3525 		}
3526 
3527 		if(!c->http_is_chunked) {
3528 			/* if we are reading nonchunks, process that*/
3529 			return http_nonchunk_segment(c);
3530 		} else {
3531 			/* if we are reading chunks, read the chunk */
3532 			int r = http_chunked_segment(c);
3533 			if(r == 0) return 0;
3534 			if(r == 1) return 1;
3535 			continue;
3536 		}
3537 	}
3538 	/* broke out of the loop; could not process header instead need
3539 	 * to read more */
3540 	/* moveover any remaining data and read more data */
3541 	http_moveover_buffer(c->buffer);
3542 	/* return and wait to read more */
3543 	return 1;
3544 }
3545 
3546 /** check pending connect for http */
3547 static int
3548 http_check_connect(int fd, struct comm_point* c)
3549 {
3550 	/* check for pending error from nonblocking connect */
3551 	/* from Stevens, unix network programming, vol1, 3rd ed, p450*/
3552 	int error = 0;
3553 	socklen_t len = (socklen_t)sizeof(error);
3554 	if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
3555 		&len) < 0){
3556 #ifndef USE_WINSOCK
3557 		error = errno; /* on solaris errno is error */
3558 #else /* USE_WINSOCK */
3559 		error = WSAGetLastError();
3560 #endif
3561 	}
3562 #ifndef USE_WINSOCK
3563 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
3564 	if(error == EINPROGRESS || error == EWOULDBLOCK)
3565 		return 1; /* try again later */
3566 	else
3567 #endif
3568 	if(error != 0 && verbosity < 2)
3569 		return 0; /* silence lots of chatter in the logs */
3570 	else if(error != 0) {
3571 		log_err_addr("http connect", strerror(error),
3572 			&c->repinfo.remote_addr, c->repinfo.remote_addrlen);
3573 #else /* USE_WINSOCK */
3574 	/* examine error */
3575 	if(error == WSAEINPROGRESS)
3576 		return 1;
3577 	else if(error == WSAEWOULDBLOCK) {
3578 		ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
3579 		return 1;
3580 	} else if(error != 0 && verbosity < 2)
3581 		return 0;
3582 	else if(error != 0) {
3583 		log_err_addr("http connect", wsa_strerror(error),
3584 			&c->repinfo.remote_addr, c->repinfo.remote_addrlen);
3585 #endif /* USE_WINSOCK */
3586 		return 0;
3587 	}
3588 	/* keep on processing this socket */
3589 	return 2;
3590 }
3591 
3592 /** write more data for http (with ssl) */
3593 static int
3594 ssl_http_write_more(struct comm_point* c)
3595 {
3596 #ifdef HAVE_SSL
3597 	int r;
3598 	log_assert(sldns_buffer_remaining(c->buffer) > 0);
3599 	ERR_clear_error();
3600 	r = SSL_write(c->ssl, (void*)sldns_buffer_current(c->buffer),
3601 		(int)sldns_buffer_remaining(c->buffer));
3602 	if(r <= 0) {
3603 		int want = SSL_get_error(c->ssl, r);
3604 		if(want == SSL_ERROR_ZERO_RETURN) {
3605 			return 0; /* closed */
3606 		} else if(want == SSL_ERROR_WANT_READ) {
3607 			c->ssl_shake_state = comm_ssl_shake_hs_read;
3608 			comm_point_listen_for_rw(c, 1, 0);
3609 			return 1; /* wait for read condition */
3610 		} else if(want == SSL_ERROR_WANT_WRITE) {
3611 			return 1; /* write more later */
3612 		} else if(want == SSL_ERROR_SYSCALL) {
3613 #ifdef EPIPE
3614 			if(errno == EPIPE && verbosity < 2)
3615 				return 0; /* silence 'broken pipe' */
3616 #endif
3617 			if(errno != 0)
3618 				log_err("SSL_write syscall: %s",
3619 					strerror(errno));
3620 			return 0;
3621 		}
3622 		log_crypto_err("could not SSL_write");
3623 		return 0;
3624 	}
3625 	sldns_buffer_skip(c->buffer, (ssize_t)r);
3626 	return 1;
3627 #else
3628 	(void)c;
3629 	return 0;
3630 #endif /* HAVE_SSL */
3631 }
3632 
3633 /** write more data for http */
3634 static int
3635 http_write_more(int fd, struct comm_point* c)
3636 {
3637 	ssize_t r;
3638 	log_assert(sldns_buffer_remaining(c->buffer) > 0);
3639 	r = send(fd, (void*)sldns_buffer_current(c->buffer),
3640 		sldns_buffer_remaining(c->buffer), 0);
3641 	if(r == -1) {
3642 #ifndef USE_WINSOCK
3643 		if(errno == EINTR || errno == EAGAIN)
3644 			return 1;
3645 #else
3646 		if(WSAGetLastError() == WSAEINPROGRESS)
3647 			return 1;
3648 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
3649 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
3650 			return 1;
3651 		}
3652 #endif
3653 		log_err_addr("http send r", sock_strerror(errno),
3654 			&c->repinfo.remote_addr, c->repinfo.remote_addrlen);
3655 		return 0;
3656 	}
3657 	sldns_buffer_skip(c->buffer, r);
3658 	return 1;
3659 }
3660 
3661 #ifdef HAVE_NGHTTP2
3662 ssize_t http2_send_cb(nghttp2_session* ATTR_UNUSED(session), const uint8_t* buf,
3663 	size_t len, int ATTR_UNUSED(flags), void* cb_arg)
3664 {
3665 	ssize_t ret;
3666 	struct http2_session* h2_session = (struct http2_session*)cb_arg;
3667 	log_assert(h2_session->c->type == comm_http);
3668 	log_assert(h2_session->c->h2_session);
3669 
3670 #ifdef HAVE_SSL
3671 	if(h2_session->c->ssl) {
3672 		int r;
3673 		ERR_clear_error();
3674 		r = SSL_write(h2_session->c->ssl, buf, len);
3675 		if(r <= 0) {
3676 			int want = SSL_get_error(h2_session->c->ssl, r);
3677 			if(want == SSL_ERROR_ZERO_RETURN) {
3678 				return NGHTTP2_ERR_CALLBACK_FAILURE;
3679 			} else if(want == SSL_ERROR_WANT_READ) {
3680 				h2_session->c->ssl_shake_state = comm_ssl_shake_hs_read;
3681 				comm_point_listen_for_rw(h2_session->c, 1, 0);
3682 				return NGHTTP2_ERR_WOULDBLOCK;
3683 			} else if(want == SSL_ERROR_WANT_WRITE) {
3684 				return NGHTTP2_ERR_WOULDBLOCK;
3685 			} else if(want == SSL_ERROR_SYSCALL) {
3686 #ifdef EPIPE
3687 				if(errno == EPIPE && verbosity < 2)
3688 					return NGHTTP2_ERR_CALLBACK_FAILURE;
3689 #endif
3690 				if(errno != 0)
3691 					log_err("SSL_write syscall: %s",
3692 						strerror(errno));
3693 				return NGHTTP2_ERR_CALLBACK_FAILURE;
3694 			}
3695 			log_crypto_err("could not SSL_write");
3696 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3697 		}
3698 		return r;
3699 	}
3700 #endif /* HAVE_SSL */
3701 
3702 	ret = send(h2_session->c->fd, buf, len, 0);
3703 	if(ret == 0) {
3704 		return NGHTTP2_ERR_CALLBACK_FAILURE;
3705 	} else if(ret < 0) {
3706 #ifndef USE_WINSOCK
3707 		if(errno == EINTR || errno == EAGAIN)
3708 			return NGHTTP2_ERR_WOULDBLOCK;
3709 #ifdef EPIPE
3710 		if(errno == EPIPE && verbosity < 2)
3711 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3712 #endif
3713 #ifdef ECONNRESET
3714 		if(errno == ECONNRESET && verbosity < 2)
3715 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3716 #endif
3717 		log_err_addr("could not http2 write: %s", strerror(errno),
3718 			&h2_session->c->repinfo.remote_addr,
3719 			h2_session->c->repinfo.remote_addrlen);
3720 #else /* USE_WINSOCK */
3721 		if(WSAGetLastError() == WSAENOTCONN)
3722 			return NGHTTP2_ERR_WOULDBLOCK;
3723 		if(WSAGetLastError() == WSAEINPROGRESS)
3724 			return NGHTTP2_ERR_WOULDBLOCK;
3725 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
3726 			ub_winsock_tcp_wouldblock(h2_session->c->ev->ev,
3727 				UB_EV_WRITE);
3728 			return NGHTTP2_ERR_WOULDBLOCK;
3729 		}
3730 		if(WSAGetLastError() == WSAECONNRESET && verbosity < 2)
3731 			return NGHTTP2_ERR_CALLBACK_FAILURE;
3732 		log_err_addr("could not http2 write: %s",
3733 			wsa_strerror(WSAGetLastError()),
3734 			&h2_session->c->repinfo.remote_addr,
3735 			h2_session->c->repinfo.remote_addrlen);
3736 #endif
3737 		return NGHTTP2_ERR_CALLBACK_FAILURE;
3738 	}
3739 	return ret;
3740 }
3741 #endif /* HAVE_NGHTTP2 */
3742 
3743 /** Handle http2 writing */
3744 static int
3745 comm_point_http2_handle_write(int ATTR_UNUSED(fd), struct comm_point* c)
3746 {
3747 #ifdef HAVE_NGHTTP2
3748 	int ret;
3749 	log_assert(c->h2_session);
3750 
3751 	ret = nghttp2_session_send(c->h2_session->session);
3752 	if(ret) {
3753 		verbose(VERB_QUERY, "http2: session_send failed, "
3754 			"error: %s", nghttp2_strerror(ret));
3755 		return 0;
3756 	}
3757 
3758 	if(nghttp2_session_want_read(c->h2_session->session)) {
3759 		c->tcp_is_reading = 1;
3760 		comm_point_stop_listening(c);
3761 		comm_point_start_listening(c, -1, adjusted_tcp_timeout(c));
3762 	} else if(!nghttp2_session_want_write(c->h2_session->session))
3763 		return 0; /* connection can be closed */
3764 	return 1;
3765 #else
3766 	(void)c;
3767 	return 0;
3768 #endif
3769 }
3770 
3771 /**
3772  * Handle http writing callback.
3773  * @param fd: file descriptor of socket.
3774  * @param c: comm point to write buffer out of.
3775  * @return: 0 on error
3776  */
3777 static int
3778 comm_point_http_handle_write(int fd, struct comm_point* c)
3779 {
3780 	log_assert(c->type == comm_http);
3781 	log_assert(fd != -1);
3782 
3783 	/* check pending connect errors, if that fails, we wait for more,
3784 	 * or we can continue to write contents */
3785 	if(c->tcp_check_nb_connect) {
3786 		int r = http_check_connect(fd, c);
3787 		if(r == 0) return 0;
3788 		if(r == 1) return 1;
3789 		c->tcp_check_nb_connect = 0;
3790 	}
3791 	/* if we are in ssl handshake, handle SSL handshake */
3792 #ifdef HAVE_SSL
3793 	if(c->ssl && c->ssl_shake_state != comm_ssl_shake_none) {
3794 		if(!ssl_handshake(c))
3795 			return 0;
3796 		if(c->ssl_shake_state != comm_ssl_shake_none)
3797 			return 1;
3798 	}
3799 #endif /* HAVE_SSL */
3800 	if(c->tcp_is_reading)
3801 		return 1;
3802 
3803 	if(c->use_h2) {
3804 		return comm_point_http2_handle_write(fd, c);
3805 	}
3806 
3807 	/* http version is <= http/1.1 */
3808 
3809 	if(c->http_min_version >= http_version_2) {
3810 		/* HTTP/2 failed, not allowed to use lower version. */
3811 		return 0;
3812 	}
3813 
3814 	/* if we are writing, write more */
3815 	if(c->ssl) {
3816 		if(!ssl_http_write_more(c))
3817 			return 0;
3818 	} else {
3819 		if(!http_write_more(fd, c))
3820 			return 0;
3821 	}
3822 
3823 	/* we write a single buffer contents, that can contain
3824 	 * the http request, and then flip to read the results */
3825 	/* see if write is done */
3826 	if(sldns_buffer_remaining(c->buffer) == 0) {
3827 		sldns_buffer_clear(c->buffer);
3828 		if(c->tcp_do_toggle_rw)
3829 			c->tcp_is_reading = 1;
3830 		c->tcp_byte_count = 0;
3831 		/* switch from listening(write) to listening(read) */
3832 		comm_point_stop_listening(c);
3833 		comm_point_start_listening(c, -1, -1);
3834 	}
3835 	return 1;
3836 }
3837 
3838 void
3839 comm_point_http_handle_callback(int fd, short event, void* arg)
3840 {
3841 	struct comm_point* c = (struct comm_point*)arg;
3842 	log_assert(c->type == comm_http);
3843 	ub_comm_base_now(c->ev->base);
3844 
3845 	if(event&UB_EV_TIMEOUT) {
3846 		verbose(VERB_QUERY, "http took too long, dropped");
3847 		reclaim_http_handler(c);
3848 		if(!c->tcp_do_close) {
3849 			fptr_ok(fptr_whitelist_comm_point(c->callback));
3850 			(void)(*c->callback)(c, c->cb_arg,
3851 				NETEVENT_TIMEOUT, NULL);
3852 		}
3853 		return;
3854 	}
3855 	if(event&UB_EV_READ) {
3856 		if(!comm_point_http_handle_read(fd, c)) {
3857 			reclaim_http_handler(c);
3858 			if(!c->tcp_do_close) {
3859 				fptr_ok(fptr_whitelist_comm_point(
3860 					c->callback));
3861 				(void)(*c->callback)(c, c->cb_arg,
3862 					NETEVENT_CLOSED, NULL);
3863 			}
3864 		}
3865 		return;
3866 	}
3867 	if(event&UB_EV_WRITE) {
3868 		if(!comm_point_http_handle_write(fd, c)) {
3869 			reclaim_http_handler(c);
3870 			if(!c->tcp_do_close) {
3871 				fptr_ok(fptr_whitelist_comm_point(
3872 					c->callback));
3873 				(void)(*c->callback)(c, c->cb_arg,
3874 					NETEVENT_CLOSED, NULL);
3875 			}
3876 		}
3877 		return;
3878 	}
3879 	log_err("Ignored event %d for httphdl.", event);
3880 }
3881 
3882 void comm_point_local_handle_callback(int fd, short event, void* arg)
3883 {
3884 	struct comm_point* c = (struct comm_point*)arg;
3885 	log_assert(c->type == comm_local);
3886 	ub_comm_base_now(c->ev->base);
3887 
3888 	if(event&UB_EV_READ) {
3889 		if(!comm_point_tcp_handle_read(fd, c, 1)) {
3890 			fptr_ok(fptr_whitelist_comm_point(c->callback));
3891 			(void)(*c->callback)(c, c->cb_arg, NETEVENT_CLOSED,
3892 				NULL);
3893 		}
3894 		return;
3895 	}
3896 	log_err("Ignored event %d for localhdl.", event);
3897 }
3898 
3899 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
3900 	short event, void* arg)
3901 {
3902 	struct comm_point* c = (struct comm_point*)arg;
3903 	int err = NETEVENT_NOERROR;
3904 	log_assert(c->type == comm_raw);
3905 	ub_comm_base_now(c->ev->base);
3906 
3907 	if(event&UB_EV_TIMEOUT)
3908 		err = NETEVENT_TIMEOUT;
3909 	fptr_ok(fptr_whitelist_comm_point_raw(c->callback));
3910 	(void)(*c->callback)(c, c->cb_arg, err, NULL);
3911 }
3912 
3913 struct comm_point*
3914 comm_point_create_udp(struct comm_base *base, int fd, sldns_buffer* buffer,
3915 	int pp2_enabled, comm_point_callback_type* callback,
3916 	void* callback_arg, struct unbound_socket* socket)
3917 {
3918 	struct comm_point* c = (struct comm_point*)calloc(1,
3919 		sizeof(struct comm_point));
3920 	short evbits;
3921 	if(!c)
3922 		return NULL;
3923 	c->ev = (struct internal_event*)calloc(1,
3924 		sizeof(struct internal_event));
3925 	if(!c->ev) {
3926 		free(c);
3927 		return NULL;
3928 	}
3929 	c->ev->base = base;
3930 	c->fd = fd;
3931 	c->buffer = buffer;
3932 	c->timeout = NULL;
3933 	c->tcp_is_reading = 0;
3934 	c->tcp_byte_count = 0;
3935 	c->tcp_parent = NULL;
3936 	c->max_tcp_count = 0;
3937 	c->cur_tcp_count = 0;
3938 	c->tcp_handlers = NULL;
3939 	c->tcp_free = NULL;
3940 	c->type = comm_udp;
3941 	c->tcp_do_close = 0;
3942 	c->do_not_close = 0;
3943 	c->tcp_do_toggle_rw = 0;
3944 	c->tcp_check_nb_connect = 0;
3945 #ifdef USE_MSG_FASTOPEN
3946 	c->tcp_do_fastopen = 0;
3947 #endif
3948 #ifdef USE_DNSCRYPT
3949 	c->dnscrypt = 0;
3950 	c->dnscrypt_buffer = buffer;
3951 #endif
3952 	c->inuse = 0;
3953 	c->callback = callback;
3954 	c->cb_arg = callback_arg;
3955 	c->socket = socket;
3956 	c->pp2_enabled = pp2_enabled;
3957 	c->pp2_header_state = pp2_header_none;
3958 	evbits = UB_EV_READ | UB_EV_PERSIST;
3959 	/* ub_event stuff */
3960 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
3961 #ifdef USE_WINSOCK
3962 		comm_point_udp_callback, c);
3963 #else
3964 		comm_point_udp_ancil_callback, c);
3965 #endif
3966 	if(c->ev->ev == NULL) {
3967 		log_err("could not baseset udp event");
3968 		comm_point_delete(c);
3969 		return NULL;
3970 	}
3971 	if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) {
3972 		log_err("could not add udp event");
3973 		comm_point_delete(c);
3974 		return NULL;
3975 	}
3976 	c->event_added = 1;
3977 	return c;
3978 }
3979 
3980 struct comm_point*
3981 comm_point_create_udp_ancil(struct comm_base *base, int fd,
3982 	sldns_buffer* buffer, int pp2_enabled,
3983 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket)
3984 {
3985 	struct comm_point* c = (struct comm_point*)calloc(1,
3986 		sizeof(struct comm_point));
3987 	short evbits;
3988 	if(!c)
3989 		return NULL;
3990 	c->ev = (struct internal_event*)calloc(1,
3991 		sizeof(struct internal_event));
3992 	if(!c->ev) {
3993 		free(c);
3994 		return NULL;
3995 	}
3996 	c->ev->base = base;
3997 	c->fd = fd;
3998 	c->buffer = buffer;
3999 	c->timeout = NULL;
4000 	c->tcp_is_reading = 0;
4001 	c->tcp_byte_count = 0;
4002 	c->tcp_parent = NULL;
4003 	c->max_tcp_count = 0;
4004 	c->cur_tcp_count = 0;
4005 	c->tcp_handlers = NULL;
4006 	c->tcp_free = NULL;
4007 	c->type = comm_udp;
4008 	c->tcp_do_close = 0;
4009 	c->do_not_close = 0;
4010 #ifdef USE_DNSCRYPT
4011 	c->dnscrypt = 0;
4012 	c->dnscrypt_buffer = buffer;
4013 #endif
4014 	c->inuse = 0;
4015 	c->tcp_do_toggle_rw = 0;
4016 	c->tcp_check_nb_connect = 0;
4017 #ifdef USE_MSG_FASTOPEN
4018 	c->tcp_do_fastopen = 0;
4019 #endif
4020 	c->callback = callback;
4021 	c->cb_arg = callback_arg;
4022 	c->socket = socket;
4023 	c->pp2_enabled = pp2_enabled;
4024 	c->pp2_header_state = pp2_header_none;
4025 	evbits = UB_EV_READ | UB_EV_PERSIST;
4026 	/* ub_event stuff */
4027 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4028 		comm_point_udp_ancil_callback, c);
4029 	if(c->ev->ev == NULL) {
4030 		log_err("could not baseset udp event");
4031 		comm_point_delete(c);
4032 		return NULL;
4033 	}
4034 	if(fd!=-1 && ub_event_add(c->ev->ev, c->timeout) != 0 ) {
4035 		log_err("could not add udp event");
4036 		comm_point_delete(c);
4037 		return NULL;
4038 	}
4039 	c->event_added = 1;
4040 	return c;
4041 }
4042 
4043 static struct comm_point*
4044 comm_point_create_tcp_handler(struct comm_base *base,
4045 	struct comm_point* parent, size_t bufsize,
4046 	struct sldns_buffer* spoolbuf, comm_point_callback_type* callback,
4047 	void* callback_arg, struct unbound_socket* socket)
4048 {
4049 	struct comm_point* c = (struct comm_point*)calloc(1,
4050 		sizeof(struct comm_point));
4051 	short evbits;
4052 	if(!c)
4053 		return NULL;
4054 	c->ev = (struct internal_event*)calloc(1,
4055 		sizeof(struct internal_event));
4056 	if(!c->ev) {
4057 		free(c);
4058 		return NULL;
4059 	}
4060 	c->ev->base = base;
4061 	c->fd = -1;
4062 	c->buffer = sldns_buffer_new(bufsize);
4063 	if(!c->buffer) {
4064 		free(c->ev);
4065 		free(c);
4066 		return NULL;
4067 	}
4068 	c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
4069 	if(!c->timeout) {
4070 		sldns_buffer_free(c->buffer);
4071 		free(c->ev);
4072 		free(c);
4073 		return NULL;
4074 	}
4075 	c->tcp_is_reading = 0;
4076 	c->tcp_byte_count = 0;
4077 	c->tcp_parent = parent;
4078 	c->tcp_timeout_msec = parent->tcp_timeout_msec;
4079 	c->tcp_conn_limit = parent->tcp_conn_limit;
4080 	c->tcl_addr = NULL;
4081 	c->tcp_keepalive = 0;
4082 	c->max_tcp_count = 0;
4083 	c->cur_tcp_count = 0;
4084 	c->tcp_handlers = NULL;
4085 	c->tcp_free = NULL;
4086 	c->type = comm_tcp;
4087 	c->tcp_do_close = 0;
4088 	c->do_not_close = 0;
4089 	c->tcp_do_toggle_rw = 1;
4090 	c->tcp_check_nb_connect = 0;
4091 #ifdef USE_MSG_FASTOPEN
4092 	c->tcp_do_fastopen = 0;
4093 #endif
4094 #ifdef USE_DNSCRYPT
4095 	c->dnscrypt = 0;
4096 	/* We don't know just yet if this is a dnscrypt channel. Allocation
4097 	 * will be done when handling the callback. */
4098 	c->dnscrypt_buffer = c->buffer;
4099 #endif
4100 	c->repinfo.c = c;
4101 	c->callback = callback;
4102 	c->cb_arg = callback_arg;
4103 	c->socket = socket;
4104 	c->pp2_enabled = parent->pp2_enabled;
4105 	c->pp2_header_state = pp2_header_none;
4106 	if(spoolbuf) {
4107 		c->tcp_req_info = tcp_req_info_create(spoolbuf);
4108 		if(!c->tcp_req_info) {
4109 			log_err("could not create tcp commpoint");
4110 			sldns_buffer_free(c->buffer);
4111 			free(c->timeout);
4112 			free(c->ev);
4113 			free(c);
4114 			return NULL;
4115 		}
4116 		c->tcp_req_info->cp = c;
4117 		c->tcp_do_close = 1;
4118 		c->tcp_do_toggle_rw = 0;
4119 	}
4120 	/* add to parent free list */
4121 	c->tcp_free = parent->tcp_free;
4122 	parent->tcp_free = c;
4123 	/* ub_event stuff */
4124 	evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT;
4125 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4126 		comm_point_tcp_handle_callback, c);
4127 	if(c->ev->ev == NULL)
4128 	{
4129 		log_err("could not basetset tcphdl event");
4130 		parent->tcp_free = c->tcp_free;
4131 		tcp_req_info_delete(c->tcp_req_info);
4132 		sldns_buffer_free(c->buffer);
4133 		free(c->timeout);
4134 		free(c->ev);
4135 		free(c);
4136 		return NULL;
4137 	}
4138 	return c;
4139 }
4140 
4141 static struct comm_point*
4142 comm_point_create_http_handler(struct comm_base *base,
4143 	struct comm_point* parent, size_t bufsize, int harden_large_queries,
4144 	uint32_t http_max_streams, char* http_endpoint,
4145 	comm_point_callback_type* callback, void* callback_arg,
4146 	struct unbound_socket* socket)
4147 {
4148 	struct comm_point* c = (struct comm_point*)calloc(1,
4149 		sizeof(struct comm_point));
4150 	short evbits;
4151 	if(!c)
4152 		return NULL;
4153 	c->ev = (struct internal_event*)calloc(1,
4154 		sizeof(struct internal_event));
4155 	if(!c->ev) {
4156 		free(c);
4157 		return NULL;
4158 	}
4159 	c->ev->base = base;
4160 	c->fd = -1;
4161 	c->buffer = sldns_buffer_new(bufsize);
4162 	if(!c->buffer) {
4163 		free(c->ev);
4164 		free(c);
4165 		return NULL;
4166 	}
4167 	c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
4168 	if(!c->timeout) {
4169 		sldns_buffer_free(c->buffer);
4170 		free(c->ev);
4171 		free(c);
4172 		return NULL;
4173 	}
4174 	c->tcp_is_reading = 0;
4175 	c->tcp_byte_count = 0;
4176 	c->tcp_parent = parent;
4177 	c->tcp_timeout_msec = parent->tcp_timeout_msec;
4178 	c->tcp_conn_limit = parent->tcp_conn_limit;
4179 	c->tcl_addr = NULL;
4180 	c->tcp_keepalive = 0;
4181 	c->max_tcp_count = 0;
4182 	c->cur_tcp_count = 0;
4183 	c->tcp_handlers = NULL;
4184 	c->tcp_free = NULL;
4185 	c->type = comm_http;
4186 	c->tcp_do_close = 1;
4187 	c->do_not_close = 0;
4188 	c->tcp_do_toggle_rw = 1; /* will be set to 0 after http2 upgrade */
4189 	c->tcp_check_nb_connect = 0;
4190 #ifdef USE_MSG_FASTOPEN
4191 	c->tcp_do_fastopen = 0;
4192 #endif
4193 #ifdef USE_DNSCRYPT
4194 	c->dnscrypt = 0;
4195 	c->dnscrypt_buffer = NULL;
4196 #endif
4197 	c->repinfo.c = c;
4198 	c->callback = callback;
4199 	c->cb_arg = callback_arg;
4200 	c->socket = socket;
4201 	c->pp2_enabled = 0;
4202 	c->pp2_header_state = pp2_header_none;
4203 
4204 	c->http_min_version = http_version_2;
4205 	c->http2_stream_max_qbuffer_size = bufsize;
4206 	if(harden_large_queries && bufsize > 512)
4207 		c->http2_stream_max_qbuffer_size = 512;
4208 	c->http2_max_streams = http_max_streams;
4209 	if(!(c->http_endpoint = strdup(http_endpoint))) {
4210 		log_err("could not strdup http_endpoint");
4211 		sldns_buffer_free(c->buffer);
4212 		free(c->timeout);
4213 		free(c->ev);
4214 		free(c);
4215 		return NULL;
4216 	}
4217 	c->use_h2 = 0;
4218 #ifdef HAVE_NGHTTP2
4219 	if(!(c->h2_session = http2_session_create(c))) {
4220 		log_err("could not create http2 session");
4221 		free(c->http_endpoint);
4222 		sldns_buffer_free(c->buffer);
4223 		free(c->timeout);
4224 		free(c->ev);
4225 		free(c);
4226 		return NULL;
4227 	}
4228 	if(!(c->h2_session->callbacks = http2_req_callbacks_create())) {
4229 		log_err("could not create http2 callbacks");
4230 		http2_session_delete(c->h2_session);
4231 		free(c->http_endpoint);
4232 		sldns_buffer_free(c->buffer);
4233 		free(c->timeout);
4234 		free(c->ev);
4235 		free(c);
4236 		return NULL;
4237 	}
4238 #endif
4239 
4240 	/* add to parent free list */
4241 	c->tcp_free = parent->tcp_free;
4242 	parent->tcp_free = c;
4243 	/* ub_event stuff */
4244 	evbits = UB_EV_PERSIST | UB_EV_READ | UB_EV_TIMEOUT;
4245 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4246 		comm_point_http_handle_callback, c);
4247 	if(c->ev->ev == NULL)
4248 	{
4249 		log_err("could not set http handler event");
4250 		parent->tcp_free = c->tcp_free;
4251 		http2_session_delete(c->h2_session);
4252 		sldns_buffer_free(c->buffer);
4253 		free(c->timeout);
4254 		free(c->ev);
4255 		free(c);
4256 		return NULL;
4257 	}
4258 	return c;
4259 }
4260 
4261 struct comm_point*
4262 comm_point_create_tcp(struct comm_base *base, int fd, int num,
4263 	int idle_timeout, int harden_large_queries,
4264 	uint32_t http_max_streams, char* http_endpoint,
4265 	struct tcl_list* tcp_conn_limit, size_t bufsize,
4266 	struct sldns_buffer* spoolbuf, enum listen_type port_type,
4267 	int pp2_enabled, comm_point_callback_type* callback,
4268 	void* callback_arg, struct unbound_socket* socket)
4269 {
4270 	struct comm_point* c = (struct comm_point*)calloc(1,
4271 		sizeof(struct comm_point));
4272 	short evbits;
4273 	int i;
4274 	/* first allocate the TCP accept listener */
4275 	if(!c)
4276 		return NULL;
4277 	c->ev = (struct internal_event*)calloc(1,
4278 		sizeof(struct internal_event));
4279 	if(!c->ev) {
4280 		free(c);
4281 		return NULL;
4282 	}
4283 	c->ev->base = base;
4284 	c->fd = fd;
4285 	c->buffer = NULL;
4286 	c->timeout = NULL;
4287 	c->tcp_is_reading = 0;
4288 	c->tcp_byte_count = 0;
4289 	c->tcp_timeout_msec = idle_timeout;
4290 	c->tcp_conn_limit = tcp_conn_limit;
4291 	c->tcl_addr = NULL;
4292 	c->tcp_keepalive = 0;
4293 	c->tcp_parent = NULL;
4294 	c->max_tcp_count = num;
4295 	c->cur_tcp_count = 0;
4296 	c->tcp_handlers = (struct comm_point**)calloc((size_t)num,
4297 		sizeof(struct comm_point*));
4298 	if(!c->tcp_handlers) {
4299 		free(c->ev);
4300 		free(c);
4301 		return NULL;
4302 	}
4303 	c->tcp_free = NULL;
4304 	c->type = comm_tcp_accept;
4305 	c->tcp_do_close = 0;
4306 	c->do_not_close = 0;
4307 	c->tcp_do_toggle_rw = 0;
4308 	c->tcp_check_nb_connect = 0;
4309 #ifdef USE_MSG_FASTOPEN
4310 	c->tcp_do_fastopen = 0;
4311 #endif
4312 #ifdef USE_DNSCRYPT
4313 	c->dnscrypt = 0;
4314 	c->dnscrypt_buffer = NULL;
4315 #endif
4316 	c->callback = NULL;
4317 	c->cb_arg = NULL;
4318 	c->socket = socket;
4319 	c->pp2_enabled = (port_type==listen_type_http?0:pp2_enabled);
4320 	c->pp2_header_state = pp2_header_none;
4321 	evbits = UB_EV_READ | UB_EV_PERSIST;
4322 	/* ub_event stuff */
4323 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4324 		comm_point_tcp_accept_callback, c);
4325 	if(c->ev->ev == NULL) {
4326 		log_err("could not baseset tcpacc event");
4327 		comm_point_delete(c);
4328 		return NULL;
4329 	}
4330 	if (ub_event_add(c->ev->ev, c->timeout) != 0) {
4331 		log_err("could not add tcpacc event");
4332 		comm_point_delete(c);
4333 		return NULL;
4334 	}
4335 	c->event_added = 1;
4336 	/* now prealloc the handlers */
4337 	for(i=0; i<num; i++) {
4338 		if(port_type == listen_type_tcp ||
4339 			port_type == listen_type_ssl ||
4340 			port_type == listen_type_tcp_dnscrypt) {
4341 			c->tcp_handlers[i] = comm_point_create_tcp_handler(base,
4342 				c, bufsize, spoolbuf, callback, callback_arg, socket);
4343 		} else if(port_type == listen_type_http) {
4344 			c->tcp_handlers[i] = comm_point_create_http_handler(
4345 				base, c, bufsize, harden_large_queries,
4346 				http_max_streams, http_endpoint,
4347 				callback, callback_arg, socket);
4348 		}
4349 		else {
4350 			log_err("could not create tcp handler, unknown listen "
4351 				"type");
4352 			return NULL;
4353 		}
4354 		if(!c->tcp_handlers[i]) {
4355 			comm_point_delete(c);
4356 			return NULL;
4357 		}
4358 	}
4359 
4360 	return c;
4361 }
4362 
4363 struct comm_point*
4364 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize,
4365         comm_point_callback_type* callback, void* callback_arg)
4366 {
4367 	struct comm_point* c = (struct comm_point*)calloc(1,
4368 		sizeof(struct comm_point));
4369 	short evbits;
4370 	if(!c)
4371 		return NULL;
4372 	c->ev = (struct internal_event*)calloc(1,
4373 		sizeof(struct internal_event));
4374 	if(!c->ev) {
4375 		free(c);
4376 		return NULL;
4377 	}
4378 	c->ev->base = base;
4379 	c->fd = -1;
4380 	c->buffer = sldns_buffer_new(bufsize);
4381 	if(!c->buffer) {
4382 		free(c->ev);
4383 		free(c);
4384 		return NULL;
4385 	}
4386 	c->timeout = NULL;
4387 	c->tcp_is_reading = 0;
4388 	c->tcp_byte_count = 0;
4389 	c->tcp_timeout_msec = TCP_QUERY_TIMEOUT;
4390 	c->tcp_conn_limit = NULL;
4391 	c->tcl_addr = NULL;
4392 	c->tcp_keepalive = 0;
4393 	c->tcp_parent = NULL;
4394 	c->max_tcp_count = 0;
4395 	c->cur_tcp_count = 0;
4396 	c->tcp_handlers = NULL;
4397 	c->tcp_free = NULL;
4398 	c->type = comm_tcp;
4399 	c->tcp_do_close = 0;
4400 	c->do_not_close = 0;
4401 	c->tcp_do_toggle_rw = 1;
4402 	c->tcp_check_nb_connect = 1;
4403 #ifdef USE_MSG_FASTOPEN
4404 	c->tcp_do_fastopen = 1;
4405 #endif
4406 #ifdef USE_DNSCRYPT
4407 	c->dnscrypt = 0;
4408 	c->dnscrypt_buffer = c->buffer;
4409 #endif
4410 	c->repinfo.c = c;
4411 	c->callback = callback;
4412 	c->cb_arg = callback_arg;
4413 	c->pp2_enabled = 0;
4414 	c->pp2_header_state = pp2_header_none;
4415 	evbits = UB_EV_PERSIST | UB_EV_WRITE;
4416 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4417 		comm_point_tcp_handle_callback, c);
4418 	if(c->ev->ev == NULL)
4419 	{
4420 		log_err("could not baseset tcpout event");
4421 		sldns_buffer_free(c->buffer);
4422 		free(c->ev);
4423 		free(c);
4424 		return NULL;
4425 	}
4426 
4427 	return c;
4428 }
4429 
4430 struct comm_point*
4431 comm_point_create_http_out(struct comm_base *base, size_t bufsize,
4432         comm_point_callback_type* callback, void* callback_arg,
4433 	sldns_buffer* temp)
4434 {
4435 	struct comm_point* c = (struct comm_point*)calloc(1,
4436 		sizeof(struct comm_point));
4437 	short evbits;
4438 	if(!c)
4439 		return NULL;
4440 	c->ev = (struct internal_event*)calloc(1,
4441 		sizeof(struct internal_event));
4442 	if(!c->ev) {
4443 		free(c);
4444 		return NULL;
4445 	}
4446 	c->ev->base = base;
4447 	c->fd = -1;
4448 	c->buffer = sldns_buffer_new(bufsize);
4449 	if(!c->buffer) {
4450 		free(c->ev);
4451 		free(c);
4452 		return NULL;
4453 	}
4454 	c->timeout = NULL;
4455 	c->tcp_is_reading = 0;
4456 	c->tcp_byte_count = 0;
4457 	c->tcp_parent = NULL;
4458 	c->max_tcp_count = 0;
4459 	c->cur_tcp_count = 0;
4460 	c->tcp_handlers = NULL;
4461 	c->tcp_free = NULL;
4462 	c->type = comm_http;
4463 	c->tcp_do_close = 0;
4464 	c->do_not_close = 0;
4465 	c->tcp_do_toggle_rw = 1;
4466 	c->tcp_check_nb_connect = 1;
4467 	c->http_in_headers = 1;
4468 	c->http_in_chunk_headers = 0;
4469 	c->http_is_chunked = 0;
4470 	c->http_temp = temp;
4471 #ifdef USE_MSG_FASTOPEN
4472 	c->tcp_do_fastopen = 1;
4473 #endif
4474 #ifdef USE_DNSCRYPT
4475 	c->dnscrypt = 0;
4476 	c->dnscrypt_buffer = c->buffer;
4477 #endif
4478 	c->repinfo.c = c;
4479 	c->callback = callback;
4480 	c->cb_arg = callback_arg;
4481 	c->pp2_enabled = 0;
4482 	c->pp2_header_state = pp2_header_none;
4483 	evbits = UB_EV_PERSIST | UB_EV_WRITE;
4484 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4485 		comm_point_http_handle_callback, c);
4486 	if(c->ev->ev == NULL)
4487 	{
4488 		log_err("could not baseset tcpout event");
4489 #ifdef HAVE_SSL
4490 		SSL_free(c->ssl);
4491 #endif
4492 		sldns_buffer_free(c->buffer);
4493 		free(c->ev);
4494 		free(c);
4495 		return NULL;
4496 	}
4497 
4498 	return c;
4499 }
4500 
4501 struct comm_point*
4502 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize,
4503         comm_point_callback_type* callback, void* callback_arg)
4504 {
4505 	struct comm_point* c = (struct comm_point*)calloc(1,
4506 		sizeof(struct comm_point));
4507 	short evbits;
4508 	if(!c)
4509 		return NULL;
4510 	c->ev = (struct internal_event*)calloc(1,
4511 		sizeof(struct internal_event));
4512 	if(!c->ev) {
4513 		free(c);
4514 		return NULL;
4515 	}
4516 	c->ev->base = base;
4517 	c->fd = fd;
4518 	c->buffer = sldns_buffer_new(bufsize);
4519 	if(!c->buffer) {
4520 		free(c->ev);
4521 		free(c);
4522 		return NULL;
4523 	}
4524 	c->timeout = NULL;
4525 	c->tcp_is_reading = 1;
4526 	c->tcp_byte_count = 0;
4527 	c->tcp_parent = NULL;
4528 	c->max_tcp_count = 0;
4529 	c->cur_tcp_count = 0;
4530 	c->tcp_handlers = NULL;
4531 	c->tcp_free = NULL;
4532 	c->type = comm_local;
4533 	c->tcp_do_close = 0;
4534 	c->do_not_close = 1;
4535 	c->tcp_do_toggle_rw = 0;
4536 	c->tcp_check_nb_connect = 0;
4537 #ifdef USE_MSG_FASTOPEN
4538 	c->tcp_do_fastopen = 0;
4539 #endif
4540 #ifdef USE_DNSCRYPT
4541 	c->dnscrypt = 0;
4542 	c->dnscrypt_buffer = c->buffer;
4543 #endif
4544 	c->callback = callback;
4545 	c->cb_arg = callback_arg;
4546 	c->pp2_enabled = 0;
4547 	c->pp2_header_state = pp2_header_none;
4548 	/* ub_event stuff */
4549 	evbits = UB_EV_PERSIST | UB_EV_READ;
4550 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4551 		comm_point_local_handle_callback, c);
4552 	if(c->ev->ev == NULL) {
4553 		log_err("could not baseset localhdl event");
4554 		free(c->ev);
4555 		free(c);
4556 		return NULL;
4557 	}
4558 	if (ub_event_add(c->ev->ev, c->timeout) != 0) {
4559 		log_err("could not add localhdl event");
4560 		ub_event_free(c->ev->ev);
4561 		free(c->ev);
4562 		free(c);
4563 		return NULL;
4564 	}
4565 	c->event_added = 1;
4566 	return c;
4567 }
4568 
4569 struct comm_point*
4570 comm_point_create_raw(struct comm_base* base, int fd, int writing,
4571 	comm_point_callback_type* callback, void* callback_arg)
4572 {
4573 	struct comm_point* c = (struct comm_point*)calloc(1,
4574 		sizeof(struct comm_point));
4575 	short evbits;
4576 	if(!c)
4577 		return NULL;
4578 	c->ev = (struct internal_event*)calloc(1,
4579 		sizeof(struct internal_event));
4580 	if(!c->ev) {
4581 		free(c);
4582 		return NULL;
4583 	}
4584 	c->ev->base = base;
4585 	c->fd = fd;
4586 	c->buffer = NULL;
4587 	c->timeout = NULL;
4588 	c->tcp_is_reading = 0;
4589 	c->tcp_byte_count = 0;
4590 	c->tcp_parent = NULL;
4591 	c->max_tcp_count = 0;
4592 	c->cur_tcp_count = 0;
4593 	c->tcp_handlers = NULL;
4594 	c->tcp_free = NULL;
4595 	c->type = comm_raw;
4596 	c->tcp_do_close = 0;
4597 	c->do_not_close = 1;
4598 	c->tcp_do_toggle_rw = 0;
4599 	c->tcp_check_nb_connect = 0;
4600 #ifdef USE_MSG_FASTOPEN
4601 	c->tcp_do_fastopen = 0;
4602 #endif
4603 #ifdef USE_DNSCRYPT
4604 	c->dnscrypt = 0;
4605 	c->dnscrypt_buffer = c->buffer;
4606 #endif
4607 	c->callback = callback;
4608 	c->cb_arg = callback_arg;
4609 	c->pp2_enabled = 0;
4610 	c->pp2_header_state = pp2_header_none;
4611 	/* ub_event stuff */
4612 	if(writing)
4613 		evbits = UB_EV_PERSIST | UB_EV_WRITE;
4614 	else 	evbits = UB_EV_PERSIST | UB_EV_READ;
4615 	c->ev->ev = ub_event_new(base->eb->base, c->fd, evbits,
4616 		comm_point_raw_handle_callback, c);
4617 	if(c->ev->ev == NULL) {
4618 		log_err("could not baseset rawhdl event");
4619 		free(c->ev);
4620 		free(c);
4621 		return NULL;
4622 	}
4623 	if (ub_event_add(c->ev->ev, c->timeout) != 0) {
4624 		log_err("could not add rawhdl event");
4625 		ub_event_free(c->ev->ev);
4626 		free(c->ev);
4627 		free(c);
4628 		return NULL;
4629 	}
4630 	c->event_added = 1;
4631 	return c;
4632 }
4633 
4634 void
4635 comm_point_close(struct comm_point* c)
4636 {
4637 	if(!c)
4638 		return;
4639 	if(c->fd != -1) {
4640 		verbose(5, "comm_point_close of %d: event_del", c->fd);
4641 		if(c->event_added) {
4642 			if(ub_event_del(c->ev->ev) != 0) {
4643 				log_err("could not event_del on close");
4644 			}
4645 			c->event_added = 0;
4646 		}
4647 	}
4648 	tcl_close_connection(c->tcl_addr);
4649 	if(c->tcp_req_info)
4650 		tcp_req_info_clear(c->tcp_req_info);
4651 	if(c->h2_session)
4652 		http2_session_server_delete(c->h2_session);
4653 	/* stop the comm point from reading or writing after it is closed. */
4654 	if(c->tcp_more_read_again && *c->tcp_more_read_again)
4655 		*c->tcp_more_read_again = 0;
4656 	if(c->tcp_more_write_again && *c->tcp_more_write_again)
4657 		*c->tcp_more_write_again = 0;
4658 
4659 	/* close fd after removing from event lists, or epoll.. is messed up */
4660 	if(c->fd != -1 && !c->do_not_close) {
4661 #ifdef USE_WINSOCK
4662 		if(c->type == comm_tcp || c->type == comm_http) {
4663 			/* delete sticky events for the fd, it gets closed */
4664 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_READ);
4665 			ub_winsock_tcp_wouldblock(c->ev->ev, UB_EV_WRITE);
4666 		}
4667 #endif
4668 		verbose(VERB_ALGO, "close fd %d", c->fd);
4669 		sock_close(c->fd);
4670 	}
4671 	c->fd = -1;
4672 }
4673 
4674 void
4675 comm_point_delete(struct comm_point* c)
4676 {
4677 	if(!c)
4678 		return;
4679 	if((c->type == comm_tcp || c->type == comm_http) && c->ssl) {
4680 #ifdef HAVE_SSL
4681 		SSL_shutdown(c->ssl);
4682 		SSL_free(c->ssl);
4683 #endif
4684 	}
4685 	if(c->type == comm_http && c->http_endpoint) {
4686 		free(c->http_endpoint);
4687 		c->http_endpoint = NULL;
4688 	}
4689 	comm_point_close(c);
4690 	if(c->tcp_handlers) {
4691 		int i;
4692 		for(i=0; i<c->max_tcp_count; i++)
4693 			comm_point_delete(c->tcp_handlers[i]);
4694 		free(c->tcp_handlers);
4695 	}
4696 	free(c->timeout);
4697 	if(c->type == comm_tcp || c->type == comm_local || c->type == comm_http) {
4698 		sldns_buffer_free(c->buffer);
4699 #ifdef USE_DNSCRYPT
4700 		if(c->dnscrypt && c->dnscrypt_buffer != c->buffer) {
4701 			sldns_buffer_free(c->dnscrypt_buffer);
4702 		}
4703 #endif
4704 		if(c->tcp_req_info) {
4705 			tcp_req_info_delete(c->tcp_req_info);
4706 		}
4707 		if(c->h2_session) {
4708 			http2_session_delete(c->h2_session);
4709 		}
4710 	}
4711 	ub_event_free(c->ev->ev);
4712 	free(c->ev);
4713 	free(c);
4714 }
4715 
4716 void
4717 comm_point_send_reply(struct comm_reply *repinfo)
4718 {
4719 	struct sldns_buffer* buffer;
4720 	log_assert(repinfo && repinfo->c);
4721 #ifdef USE_DNSCRYPT
4722 	buffer = repinfo->c->dnscrypt_buffer;
4723 	if(!dnsc_handle_uncurved_request(repinfo)) {
4724 		return;
4725 	}
4726 #else
4727 	buffer = repinfo->c->buffer;
4728 #endif
4729 	if(repinfo->c->type == comm_udp) {
4730 		if(repinfo->srctype)
4731 			comm_point_send_udp_msg_if(repinfo->c, buffer,
4732 				(struct sockaddr*)&repinfo->remote_addr,
4733 				repinfo->remote_addrlen, repinfo);
4734 		else
4735 			comm_point_send_udp_msg(repinfo->c, buffer,
4736 				(struct sockaddr*)&repinfo->remote_addr,
4737 				repinfo->remote_addrlen, 0);
4738 #ifdef USE_DNSTAP
4739 		/*
4740 		 * sending src (client)/dst (local service) addresses over DNSTAP from udp callback
4741 		 */
4742 		if(repinfo->c->dtenv != NULL && repinfo->c->dtenv->log_client_response_messages) {
4743 			log_addr(VERB_ALGO, "from local addr", (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->socket->addr->ai_addrlen);
4744 			log_addr(VERB_ALGO, "response to client", &repinfo->client_addr, repinfo->client_addrlen);
4745 			dt_msg_send_client_response(repinfo->c->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->type, repinfo->c->buffer);
4746 		}
4747 #endif
4748 	} else {
4749 #ifdef USE_DNSTAP
4750 		/*
4751 		 * sending src (client)/dst (local service) addresses over DNSTAP from TCP callback
4752 		 */
4753 		if(repinfo->c->tcp_parent->dtenv != NULL && repinfo->c->tcp_parent->dtenv->log_client_response_messages) {
4754 			log_addr(VERB_ALGO, "from local addr", (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->socket->addr->ai_addrlen);
4755 			log_addr(VERB_ALGO, "response to client", &repinfo->client_addr, repinfo->client_addrlen);
4756 			dt_msg_send_client_response(repinfo->c->tcp_parent->dtenv, &repinfo->client_addr, (void*)repinfo->c->socket->addr->ai_addr, repinfo->c->type,
4757 				( repinfo->c->tcp_req_info? repinfo->c->tcp_req_info->spool_buffer: repinfo->c->buffer ));
4758 		}
4759 #endif
4760 		if(repinfo->c->tcp_req_info) {
4761 			tcp_req_info_send_reply(repinfo->c->tcp_req_info);
4762 		} else if(repinfo->c->use_h2) {
4763 			if(!http2_submit_dns_response(repinfo->c->h2_session)) {
4764 				comm_point_drop_reply(repinfo);
4765 				return;
4766 			}
4767 			repinfo->c->h2_stream = NULL;
4768 			repinfo->c->tcp_is_reading = 0;
4769 			comm_point_stop_listening(repinfo->c);
4770 			comm_point_start_listening(repinfo->c, -1,
4771 				adjusted_tcp_timeout(repinfo->c));
4772 			return;
4773 		} else {
4774 			comm_point_start_listening(repinfo->c, -1,
4775 				adjusted_tcp_timeout(repinfo->c));
4776 		}
4777 	}
4778 }
4779 
4780 void
4781 comm_point_drop_reply(struct comm_reply* repinfo)
4782 {
4783 	if(!repinfo)
4784 		return;
4785 	log_assert(repinfo->c);
4786 	log_assert(repinfo->c->type != comm_tcp_accept);
4787 	if(repinfo->c->type == comm_udp)
4788 		return;
4789 	if(repinfo->c->tcp_req_info)
4790 		repinfo->c->tcp_req_info->is_drop = 1;
4791 	if(repinfo->c->type == comm_http) {
4792 		if(repinfo->c->h2_session) {
4793 			repinfo->c->h2_session->is_drop = 1;
4794 			if(!repinfo->c->h2_session->postpone_drop)
4795 				reclaim_http_handler(repinfo->c);
4796 			return;
4797 		}
4798 		reclaim_http_handler(repinfo->c);
4799 		return;
4800 	}
4801 	reclaim_tcp_handler(repinfo->c);
4802 }
4803 
4804 void
4805 comm_point_stop_listening(struct comm_point* c)
4806 {
4807 	verbose(VERB_ALGO, "comm point stop listening %d", c->fd);
4808 	if(c->event_added) {
4809 		if(ub_event_del(c->ev->ev) != 0) {
4810 			log_err("event_del error to stoplisten");
4811 		}
4812 		c->event_added = 0;
4813 	}
4814 }
4815 
4816 void
4817 comm_point_start_listening(struct comm_point* c, int newfd, int msec)
4818 {
4819 	verbose(VERB_ALGO, "comm point start listening %d (%d msec)",
4820 		c->fd==-1?newfd:c->fd, msec);
4821 	if(c->type == comm_tcp_accept && !c->tcp_free) {
4822 		/* no use to start listening no free slots. */
4823 		return;
4824 	}
4825 	if(c->event_added) {
4826 		if(ub_event_del(c->ev->ev) != 0) {
4827 			log_err("event_del error to startlisten");
4828 		}
4829 		c->event_added = 0;
4830 	}
4831 	if(msec != -1 && msec != 0) {
4832 		if(!c->timeout) {
4833 			c->timeout = (struct timeval*)malloc(sizeof(
4834 				struct timeval));
4835 			if(!c->timeout) {
4836 				log_err("cpsl: malloc failed. No net read.");
4837 				return;
4838 			}
4839 		}
4840 		ub_event_add_bits(c->ev->ev, UB_EV_TIMEOUT);
4841 #ifndef S_SPLINT_S /* splint fails on struct timeval. */
4842 		c->timeout->tv_sec = msec/1000;
4843 		c->timeout->tv_usec = (msec%1000)*1000;
4844 #endif /* S_SPLINT_S */
4845 	} else {
4846 		if(msec == 0 || !c->timeout) {
4847 			ub_event_del_bits(c->ev->ev, UB_EV_TIMEOUT);
4848 		}
4849 	}
4850 	if(c->type == comm_tcp || c->type == comm_http) {
4851 		ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
4852 		if(c->tcp_write_and_read) {
4853 			verbose(5, "startlistening %d mode rw", (newfd==-1?c->fd:newfd));
4854 			ub_event_add_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
4855 		} else if(c->tcp_is_reading) {
4856 			verbose(5, "startlistening %d mode r", (newfd==-1?c->fd:newfd));
4857 			ub_event_add_bits(c->ev->ev, UB_EV_READ);
4858 		} else	{
4859 			verbose(5, "startlistening %d mode w", (newfd==-1?c->fd:newfd));
4860 			ub_event_add_bits(c->ev->ev, UB_EV_WRITE);
4861 		}
4862 	}
4863 	if(newfd != -1) {
4864 		if(c->fd != -1 && c->fd != newfd) {
4865 			verbose(5, "cpsl close of fd %d for %d", c->fd, newfd);
4866 			sock_close(c->fd);
4867 		}
4868 		c->fd = newfd;
4869 		ub_event_set_fd(c->ev->ev, c->fd);
4870 	}
4871 	if(ub_event_add(c->ev->ev, msec==0?NULL:c->timeout) != 0) {
4872 		log_err("event_add failed. in cpsl.");
4873 		return;
4874 	}
4875 	c->event_added = 1;
4876 }
4877 
4878 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr)
4879 {
4880 	verbose(VERB_ALGO, "comm point listen_for_rw %d %d", c->fd, wr);
4881 	if(c->event_added) {
4882 		if(ub_event_del(c->ev->ev) != 0) {
4883 			log_err("event_del error to cplf");
4884 		}
4885 		c->event_added = 0;
4886 	}
4887 	if(!c->timeout) {
4888 		ub_event_del_bits(c->ev->ev, UB_EV_TIMEOUT);
4889 	}
4890 	ub_event_del_bits(c->ev->ev, UB_EV_READ|UB_EV_WRITE);
4891 	if(rd) ub_event_add_bits(c->ev->ev, UB_EV_READ);
4892 	if(wr) ub_event_add_bits(c->ev->ev, UB_EV_WRITE);
4893 	if(ub_event_add(c->ev->ev, c->timeout) != 0) {
4894 		log_err("event_add failed. in cplf.");
4895 		return;
4896 	}
4897 	c->event_added = 1;
4898 }
4899 
4900 size_t comm_point_get_mem(struct comm_point* c)
4901 {
4902 	size_t s;
4903 	if(!c)
4904 		return 0;
4905 	s = sizeof(*c) + sizeof(*c->ev);
4906 	if(c->timeout)
4907 		s += sizeof(*c->timeout);
4908 	if(c->type == comm_tcp || c->type == comm_local) {
4909 		s += sizeof(*c->buffer) + sldns_buffer_capacity(c->buffer);
4910 #ifdef USE_DNSCRYPT
4911 		s += sizeof(*c->dnscrypt_buffer);
4912 		if(c->buffer != c->dnscrypt_buffer) {
4913 			s += sldns_buffer_capacity(c->dnscrypt_buffer);
4914 		}
4915 #endif
4916 	}
4917 	if(c->type == comm_tcp_accept) {
4918 		int i;
4919 		for(i=0; i<c->max_tcp_count; i++)
4920 			s += comm_point_get_mem(c->tcp_handlers[i]);
4921 	}
4922 	return s;
4923 }
4924 
4925 struct comm_timer*
4926 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
4927 {
4928 	struct internal_timer *tm = (struct internal_timer*)calloc(1,
4929 		sizeof(struct internal_timer));
4930 	if(!tm) {
4931 		log_err("malloc failed");
4932 		return NULL;
4933 	}
4934 	tm->super.ev_timer = tm;
4935 	tm->base = base;
4936 	tm->super.callback = cb;
4937 	tm->super.cb_arg = cb_arg;
4938 	tm->ev = ub_event_new(base->eb->base, -1, UB_EV_TIMEOUT,
4939 		comm_timer_callback, &tm->super);
4940 	if(tm->ev == NULL) {
4941 		log_err("timer_create: event_base_set failed.");
4942 		free(tm);
4943 		return NULL;
4944 	}
4945 	return &tm->super;
4946 }
4947 
4948 void
4949 comm_timer_disable(struct comm_timer* timer)
4950 {
4951 	if(!timer)
4952 		return;
4953 	ub_timer_del(timer->ev_timer->ev);
4954 	timer->ev_timer->enabled = 0;
4955 }
4956 
4957 void
4958 comm_timer_set(struct comm_timer* timer, struct timeval* tv)
4959 {
4960 	log_assert(tv);
4961 	if(timer->ev_timer->enabled)
4962 		comm_timer_disable(timer);
4963 	if(ub_timer_add(timer->ev_timer->ev, timer->ev_timer->base->eb->base,
4964 		comm_timer_callback, timer, tv) != 0)
4965 		log_err("comm_timer_set: evtimer_add failed.");
4966 	timer->ev_timer->enabled = 1;
4967 }
4968 
4969 void
4970 comm_timer_delete(struct comm_timer* timer)
4971 {
4972 	if(!timer)
4973 		return;
4974 	comm_timer_disable(timer);
4975 	/* Free the sub struct timer->ev_timer derived from the super struct timer.
4976 	 * i.e. assert(timer == timer->ev_timer)
4977 	 */
4978 	ub_event_free(timer->ev_timer->ev);
4979 	free(timer->ev_timer);
4980 }
4981 
4982 void
4983 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
4984 {
4985 	struct comm_timer* tm = (struct comm_timer*)arg;
4986 	if(!(event&UB_EV_TIMEOUT))
4987 		return;
4988 	ub_comm_base_now(tm->ev_timer->base);
4989 	tm->ev_timer->enabled = 0;
4990 	fptr_ok(fptr_whitelist_comm_timer(tm->callback));
4991 	(*tm->callback)(tm->cb_arg);
4992 }
4993 
4994 int
4995 comm_timer_is_set(struct comm_timer* timer)
4996 {
4997 	return (int)timer->ev_timer->enabled;
4998 }
4999 
5000 size_t
5001 comm_timer_get_mem(struct comm_timer* ATTR_UNUSED(timer))
5002 {
5003 	return sizeof(struct internal_timer);
5004 }
5005 
5006 struct comm_signal*
5007 comm_signal_create(struct comm_base* base,
5008         void (*callback)(int, void*), void* cb_arg)
5009 {
5010 	struct comm_signal* com = (struct comm_signal*)malloc(
5011 		sizeof(struct comm_signal));
5012 	if(!com) {
5013 		log_err("malloc failed");
5014 		return NULL;
5015 	}
5016 	com->base = base;
5017 	com->callback = callback;
5018 	com->cb_arg = cb_arg;
5019 	com->ev_signal = NULL;
5020 	return com;
5021 }
5022 
5023 void
5024 comm_signal_callback(int sig, short event, void* arg)
5025 {
5026 	struct comm_signal* comsig = (struct comm_signal*)arg;
5027 	if(!(event & UB_EV_SIGNAL))
5028 		return;
5029 	ub_comm_base_now(comsig->base);
5030 	fptr_ok(fptr_whitelist_comm_signal(comsig->callback));
5031 	(*comsig->callback)(sig, comsig->cb_arg);
5032 }
5033 
5034 int
5035 comm_signal_bind(struct comm_signal* comsig, int sig)
5036 {
5037 	struct internal_signal* entry = (struct internal_signal*)calloc(1,
5038 		sizeof(struct internal_signal));
5039 	if(!entry) {
5040 		log_err("malloc failed");
5041 		return 0;
5042 	}
5043 	log_assert(comsig);
5044 	/* add signal event */
5045 	entry->ev = ub_signal_new(comsig->base->eb->base, sig,
5046 		comm_signal_callback, comsig);
5047 	if(entry->ev == NULL) {
5048 		log_err("Could not create signal event");
5049 		free(entry);
5050 		return 0;
5051 	}
5052 	if(ub_signal_add(entry->ev, NULL) != 0) {
5053 		log_err("Could not add signal handler");
5054 		ub_event_free(entry->ev);
5055 		free(entry);
5056 		return 0;
5057 	}
5058 	/* link into list */
5059 	entry->next = comsig->ev_signal;
5060 	comsig->ev_signal = entry;
5061 	return 1;
5062 }
5063 
5064 void
5065 comm_signal_delete(struct comm_signal* comsig)
5066 {
5067 	struct internal_signal* p, *np;
5068 	if(!comsig)
5069 		return;
5070 	p=comsig->ev_signal;
5071 	while(p) {
5072 		np = p->next;
5073 		ub_signal_del(p->ev);
5074 		ub_event_free(p->ev);
5075 		free(p);
5076 		p = np;
5077 	}
5078 	free(comsig);
5079 }
5080