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