xref: /openbsd-src/usr.sbin/unbound/util/netevent.c (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains event notification functions.
40  */
41 #include "config.h"
42 #include <ldns/wire2host.h>
43 #include "util/netevent.h"
44 #include "util/log.h"
45 #include "util/net_help.h"
46 #include "util/fptr_wlist.h"
47 #include <openssl/ssl.h>
48 #include <openssl/err.h>
49 
50 /* -------- Start of local definitions -------- */
51 /** if CMSG_ALIGN is not defined on this platform, a workaround */
52 #ifndef CMSG_ALIGN
53 #  ifdef _CMSG_DATA_ALIGN
54 #    define CMSG_ALIGN _CMSG_DATA_ALIGN
55 #  else
56 #    define CMSG_ALIGN(len) (((len)+sizeof(long)-1) & ~(sizeof(long)-1))
57 #  endif
58 #endif
59 
60 /** if CMSG_LEN is not defined on this platform, a workaround */
61 #ifndef CMSG_LEN
62 #  define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr))+(len))
63 #endif
64 
65 /** if CMSG_SPACE is not defined on this platform, a workaround */
66 #ifndef CMSG_SPACE
67 #  ifdef _CMSG_HDR_ALIGN
68 #    define CMSG_SPACE(l) (CMSG_ALIGN(l)+_CMSG_HDR_ALIGN(sizeof(struct cmsghdr)))
69 #  else
70 #    define CMSG_SPACE(l) (CMSG_ALIGN(l)+CMSG_ALIGN(sizeof(struct cmsghdr)))
71 #  endif
72 #endif
73 
74 /** The TCP reading or writing query timeout in seconds */
75 #define TCP_QUERY_TIMEOUT 120
76 
77 #ifndef NONBLOCKING_IS_BROKEN
78 /** number of UDP reads to perform per read indication from select */
79 #define NUM_UDP_PER_SELECT 100
80 #else
81 #define NUM_UDP_PER_SELECT 1
82 #endif
83 
84 /* We define libevent structures here to hide the libevent stuff. */
85 
86 #ifdef USE_MINI_EVENT
87 #  ifdef USE_WINSOCK
88 #    include "util/winsock_event.h"
89 #  else
90 #    include "util/mini_event.h"
91 #  endif /* USE_WINSOCK */
92 #else /* USE_MINI_EVENT */
93    /* we use libevent */
94 #  include <event.h>
95 #endif /* USE_MINI_EVENT */
96 
97 /**
98  * The internal event structure for keeping libevent info for the event.
99  * Possibly other structures (list, tree) this is part of.
100  */
101 struct internal_event {
102 	/** the comm base */
103 	struct comm_base* base;
104 	/** libevent event type, alloced here */
105 	struct event ev;
106 };
107 
108 /**
109  * Internal base structure, so that every thread has its own events.
110  */
111 struct internal_base {
112 	/** libevent event_base type. */
113 	struct event_base* base;
114 	/** seconds time pointer points here */
115 	uint32_t secs;
116 	/** timeval with current time */
117 	struct timeval now;
118 	/** the event used for slow_accept timeouts */
119 	struct event slow_accept;
120 	/** true if slow_accept is enabled */
121 	int slow_accept_enabled;
122 };
123 
124 /**
125  * Internal timer structure, to store timer event in.
126  */
127 struct internal_timer {
128 	/** the comm base */
129 	struct comm_base* base;
130 	/** libevent event type, alloced here */
131 	struct event ev;
132 	/** is timer enabled */
133 	uint8_t enabled;
134 };
135 
136 /**
137  * Internal signal structure, to store signal event in.
138  */
139 struct internal_signal {
140 	/** libevent event type, alloced here */
141 	struct event ev;
142 	/** next in signal list */
143 	struct internal_signal* next;
144 };
145 
146 /** create a tcp handler with a parent */
147 static struct comm_point* comm_point_create_tcp_handler(
148 	struct comm_base *base, struct comm_point* parent, size_t bufsize,
149         comm_point_callback_t* callback, void* callback_arg);
150 
151 /* -------- End of local definitions -------- */
152 
153 #ifdef USE_MINI_EVENT
154 /** minievent updates the time when it blocks. */
155 #define comm_base_now(x) /* nothing to do */
156 #else /* !USE_MINI_EVENT */
157 /** fillup the time values in the event base */
158 static void
159 comm_base_now(struct comm_base* b)
160 {
161 	if(gettimeofday(&b->eb->now, NULL) < 0) {
162 		log_err("gettimeofday: %s", strerror(errno));
163 	}
164 	b->eb->secs = (uint32_t)b->eb->now.tv_sec;
165 }
166 #endif /* USE_MINI_EVENT */
167 
168 struct comm_base*
169 comm_base_create(int sigs)
170 {
171 	struct comm_base* b = (struct comm_base*)calloc(1,
172 		sizeof(struct comm_base));
173 	if(!b)
174 		return NULL;
175 	b->eb = (struct internal_base*)calloc(1, sizeof(struct internal_base));
176 	if(!b->eb) {
177 		free(b);
178 		return NULL;
179 	}
180 #ifdef USE_MINI_EVENT
181 	(void)sigs;
182 	/* use mini event time-sharing feature */
183 	b->eb->base = event_init(&b->eb->secs, &b->eb->now);
184 #else
185 #  if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
186 	/* libev */
187 	if(sigs)
188 		b->eb->base=(struct event_base *)ev_default_loop(EVFLAG_AUTO);
189 	else
190 		b->eb->base=(struct event_base *)ev_loop_new(EVFLAG_AUTO);
191 #  else
192 	(void)sigs;
193 #    ifdef HAVE_EVENT_BASE_NEW
194 	b->eb->base = event_base_new();
195 #    else
196 	b->eb->base = event_init();
197 #    endif
198 #  endif
199 #endif
200 	if(!b->eb->base) {
201 		free(b->eb);
202 		free(b);
203 		return NULL;
204 	}
205 	comm_base_now(b);
206 	/* avoid event_get_method call which causes crashes even when
207 	 * not printing, because its result is passed */
208 	verbose(VERB_ALGO,
209 #if defined(HAVE_EV_LOOP) || defined(HAVE_EV_DEFAULT_LOOP)
210 		"libev"
211 #elif defined(USE_MINI_EVENT)
212 		"event "
213 #else
214 		"libevent "
215 #endif
216 		"%s uses %s method.",
217 		event_get_version(),
218 #ifdef HAVE_EVENT_BASE_GET_METHOD
219 		event_base_get_method(b->eb->base)
220 #else
221 		"not_obtainable"
222 #endif
223 	);
224 	return b;
225 }
226 
227 void
228 comm_base_delete(struct comm_base* b)
229 {
230 	if(!b)
231 		return;
232 	if(b->eb->slow_accept_enabled) {
233 		if(event_del(&b->eb->slow_accept) != 0) {
234 			log_err("could not event_del slow_accept");
235 		}
236 	}
237 #ifdef USE_MINI_EVENT
238 	event_base_free(b->eb->base);
239 #elif defined(HAVE_EVENT_BASE_FREE) && defined(HAVE_EVENT_BASE_ONCE)
240 	/* only libevent 1.2+ has it, but in 1.2 it is broken -
241 	   assertion fails on signal handling ev that is not deleted
242  	   in libevent 1.3c (event_base_once appears) this is fixed. */
243 	event_base_free(b->eb->base);
244 #endif /* HAVE_EVENT_BASE_FREE and HAVE_EVENT_BASE_ONCE */
245 	b->eb->base = NULL;
246 	free(b->eb);
247 	free(b);
248 }
249 
250 void
251 comm_base_timept(struct comm_base* b, uint32_t** tt, struct timeval** tv)
252 {
253 	*tt = &b->eb->secs;
254 	*tv = &b->eb->now;
255 }
256 
257 void
258 comm_base_dispatch(struct comm_base* b)
259 {
260 	int retval;
261 	retval = event_base_dispatch(b->eb->base);
262 	if(retval != 0) {
263 		fatal_exit("event_dispatch returned error %d, "
264 			"errno is %s", retval, strerror(errno));
265 	}
266 }
267 
268 void comm_base_exit(struct comm_base* b)
269 {
270 	if(event_base_loopexit(b->eb->base, NULL) != 0) {
271 		log_err("Could not loopexit");
272 	}
273 }
274 
275 void comm_base_set_slow_accept_handlers(struct comm_base* b,
276 	void (*stop_acc)(void*), void (*start_acc)(void*), void* arg)
277 {
278 	b->stop_accept = stop_acc;
279 	b->start_accept = start_acc;
280 	b->cb_arg = arg;
281 }
282 
283 struct event_base* comm_base_internal(struct comm_base* b)
284 {
285 	return b->eb->base;
286 }
287 
288 /** see if errno for udp has to be logged or not uses globals */
289 static int
290 udp_send_errno_needs_log(struct sockaddr* addr, socklen_t addrlen)
291 {
292 	/* do not log transient errors (unless high verbosity) */
293 #if defined(ENETUNREACH) || defined(EHOSTDOWN) || defined(EHOSTUNREACH) || defined(ENETDOWN)
294 	switch(errno) {
295 #  ifdef ENETUNREACH
296 		case ENETUNREACH:
297 #  endif
298 #  ifdef EHOSTDOWN
299 		case EHOSTDOWN:
300 #  endif
301 #  ifdef EHOSTUNREACH
302 		case EHOSTUNREACH:
303 #  endif
304 #  ifdef ENETDOWN
305 		case ENETDOWN:
306 #  endif
307 			if(verbosity < VERB_ALGO)
308 				return 0;
309 		default:
310 			break;
311 	}
312 #endif
313 	/* squelch errors where people deploy AAAA ::ffff:bla for
314 	 * authority servers, which we try for intranets. */
315 	if(errno == EINVAL && addr_is_ip4mapped(
316 		(struct sockaddr_storage*)addr, addrlen) &&
317 		verbosity < VERB_DETAIL)
318 		return 0;
319 	/* SO_BROADCAST sockopt can give access to 255.255.255.255,
320 	 * but a dns cache does not need it. */
321 	if(errno == EACCES && addr_is_broadcast(
322 		(struct sockaddr_storage*)addr, addrlen) &&
323 		verbosity < VERB_DETAIL)
324 		return 0;
325 	return 1;
326 }
327 
328 /* send a UDP reply */
329 int
330 comm_point_send_udp_msg(struct comm_point *c, ldns_buffer* packet,
331 	struct sockaddr* addr, socklen_t addrlen)
332 {
333 	ssize_t sent;
334 	log_assert(c->fd != -1);
335 #ifdef UNBOUND_DEBUG
336 	if(ldns_buffer_remaining(packet) == 0)
337 		log_err("error: send empty UDP packet");
338 #endif
339 	log_assert(addr && addrlen > 0);
340 	sent = sendto(c->fd, (void*)ldns_buffer_begin(packet),
341 		ldns_buffer_remaining(packet), 0,
342 		addr, addrlen);
343 	if(sent == -1) {
344 		if(!udp_send_errno_needs_log(addr, addrlen))
345 			return 0;
346 #ifndef USE_WINSOCK
347 		verbose(VERB_OPS, "sendto failed: %s", strerror(errno));
348 #else
349 		verbose(VERB_OPS, "sendto failed: %s",
350 			wsa_strerror(WSAGetLastError()));
351 #endif
352 		log_addr(VERB_OPS, "remote address is",
353 			(struct sockaddr_storage*)addr, addrlen);
354 		return 0;
355 	} else if((size_t)sent != ldns_buffer_remaining(packet)) {
356 		log_err("sent %d in place of %d bytes",
357 			(int)sent, (int)ldns_buffer_remaining(packet));
358 		return 0;
359 	}
360 	return 1;
361 }
362 
363 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && (defined(HAVE_RECVMSG) || defined(HAVE_SENDMSG))
364 /** print debug ancillary info */
365 static void p_ancil(const char* str, struct comm_reply* r)
366 {
367 	if(r->srctype != 4 && r->srctype != 6) {
368 		log_info("%s: unknown srctype %d", str, r->srctype);
369 		return;
370 	}
371 	if(r->srctype == 6) {
372 		char buf[1024];
373 		if(inet_ntop(AF_INET6, &r->pktinfo.v6info.ipi6_addr,
374 			buf, (socklen_t)sizeof(buf)) == 0) {
375 			strncpy(buf, "(inet_ntop error)", sizeof(buf));
376 		}
377 		buf[sizeof(buf)-1]=0;
378 		log_info("%s: %s %d", str, buf, r->pktinfo.v6info.ipi6_ifindex);
379 	} else if(r->srctype == 4) {
380 #ifdef IP_PKTINFO
381 		char buf1[1024], buf2[1024];
382 		if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_addr,
383 			buf1, (socklen_t)sizeof(buf1)) == 0) {
384 			strncpy(buf1, "(inet_ntop error)", sizeof(buf1));
385 		}
386 		buf1[sizeof(buf1)-1]=0;
387 		if(inet_ntop(AF_INET, &r->pktinfo.v4info.ipi_spec_dst,
388 			buf2, (socklen_t)sizeof(buf2)) == 0) {
389 			strncpy(buf2, "(inet_ntop error)", sizeof(buf2));
390 		}
391 		buf2[sizeof(buf2)-1]=0;
392 		log_info("%s: %d %s %s", str, r->pktinfo.v4info.ipi_ifindex,
393 			buf1, buf2);
394 #elif defined(IP_RECVDSTADDR)
395 		char buf1[1024];
396 		if(inet_ntop(AF_INET, &r->pktinfo.v4addr,
397 			buf1, (socklen_t)sizeof(buf1)) == 0) {
398 			strncpy(buf1, "(inet_ntop error)", sizeof(buf1));
399 		}
400 		buf1[sizeof(buf1)-1]=0;
401 		log_info("%s: %s", str, buf1);
402 #endif /* IP_PKTINFO or PI_RECVDSTDADDR */
403 	}
404 }
405 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG||HAVE_SENDMSG */
406 
407 /** send a UDP reply over specified interface*/
408 static int
409 comm_point_send_udp_msg_if(struct comm_point *c, ldns_buffer* packet,
410 	struct sockaddr* addr, socklen_t addrlen, struct comm_reply* r)
411 {
412 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_SENDMSG)
413 	ssize_t sent;
414 	struct msghdr msg;
415 	struct iovec iov[1];
416 	char control[256];
417 #ifndef S_SPLINT_S
418 	struct cmsghdr *cmsg;
419 #endif /* S_SPLINT_S */
420 
421 	log_assert(c->fd != -1);
422 #ifdef UNBOUND_DEBUG
423 	if(ldns_buffer_remaining(packet) == 0)
424 		log_err("error: send empty UDP packet");
425 #endif
426 	log_assert(addr && addrlen > 0);
427 
428 	msg.msg_name = addr;
429 	msg.msg_namelen = addrlen;
430 	iov[0].iov_base = ldns_buffer_begin(packet);
431 	iov[0].iov_len = ldns_buffer_remaining(packet);
432 	msg.msg_iov = iov;
433 	msg.msg_iovlen = 1;
434 	msg.msg_control = control;
435 #ifndef S_SPLINT_S
436 	msg.msg_controllen = sizeof(control);
437 #endif /* S_SPLINT_S */
438 	msg.msg_flags = 0;
439 
440 #ifndef S_SPLINT_S
441 	cmsg = CMSG_FIRSTHDR(&msg);
442 	if(r->srctype == 4) {
443 #ifdef IP_PKTINFO
444 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in_pktinfo));
445 		log_assert(msg.msg_controllen <= sizeof(control));
446 		cmsg->cmsg_level = IPPROTO_IP;
447 		cmsg->cmsg_type = IP_PKTINFO;
448 		memmove(CMSG_DATA(cmsg), &r->pktinfo.v4info,
449 			sizeof(struct in_pktinfo));
450 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
451 #elif defined(IP_SENDSRCADDR)
452 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in_addr));
453 		log_assert(msg.msg_controllen <= sizeof(control));
454 		cmsg->cmsg_level = IPPROTO_IP;
455 		cmsg->cmsg_type = IP_SENDSRCADDR;
456 		memmove(CMSG_DATA(cmsg), &r->pktinfo.v4addr,
457 			sizeof(struct in_addr));
458 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in_addr));
459 #else
460 		verbose(VERB_ALGO, "no IP_PKTINFO or IP_SENDSRCADDR");
461 		msg.msg_control = NULL;
462 #endif /* IP_PKTINFO or IP_SENDSRCADDR */
463 	} else if(r->srctype == 6) {
464 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
465 		log_assert(msg.msg_controllen <= sizeof(control));
466 		cmsg->cmsg_level = IPPROTO_IPV6;
467 		cmsg->cmsg_type = IPV6_PKTINFO;
468 		memmove(CMSG_DATA(cmsg), &r->pktinfo.v6info,
469 			sizeof(struct in6_pktinfo));
470 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
471 	} else {
472 		/* try to pass all 0 to use default route */
473 		msg.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo));
474 		log_assert(msg.msg_controllen <= sizeof(control));
475 		cmsg->cmsg_level = IPPROTO_IPV6;
476 		cmsg->cmsg_type = IPV6_PKTINFO;
477 		memset(CMSG_DATA(cmsg), 0, sizeof(struct in6_pktinfo));
478 		cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
479 	}
480 #endif /* S_SPLINT_S */
481 	if(verbosity >= VERB_ALGO)
482 		p_ancil("send_udp over interface", r);
483 	sent = sendmsg(c->fd, &msg, 0);
484 	if(sent == -1) {
485 		if(!udp_send_errno_needs_log(addr, addrlen))
486 			return 0;
487 		verbose(VERB_OPS, "sendmsg failed: %s", strerror(errno));
488 		log_addr(VERB_OPS, "remote address is",
489 			(struct sockaddr_storage*)addr, addrlen);
490 		return 0;
491 	} else if((size_t)sent != ldns_buffer_remaining(packet)) {
492 		log_err("sent %d in place of %d bytes",
493 			(int)sent, (int)ldns_buffer_remaining(packet));
494 		return 0;
495 	}
496 	return 1;
497 #else
498 	(void)c;
499 	(void)packet;
500 	(void)addr;
501 	(void)addrlen;
502 	(void)r;
503 	log_err("sendmsg: IPV6_PKTINFO not supported");
504 	return 0;
505 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_SENDMSG */
506 }
507 
508 void
509 comm_point_udp_ancil_callback(int fd, short event, void* arg)
510 {
511 #if defined(AF_INET6) && defined(IPV6_PKTINFO) && defined(HAVE_RECVMSG)
512 	struct comm_reply rep;
513 	struct msghdr msg;
514 	struct iovec iov[1];
515 	ssize_t rcv;
516 	char ancil[256];
517 	int i;
518 #ifndef S_SPLINT_S
519 	struct cmsghdr* cmsg;
520 #endif /* S_SPLINT_S */
521 
522 	rep.c = (struct comm_point*)arg;
523 	log_assert(rep.c->type == comm_udp);
524 
525 	if(!(event&EV_READ))
526 		return;
527 	log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
528 	comm_base_now(rep.c->ev->base);
529 	for(i=0; i<NUM_UDP_PER_SELECT; i++) {
530 		ldns_buffer_clear(rep.c->buffer);
531 		rep.addrlen = (socklen_t)sizeof(rep.addr);
532 		log_assert(fd != -1);
533 		log_assert(ldns_buffer_remaining(rep.c->buffer) > 0);
534 		msg.msg_name = &rep.addr;
535 		msg.msg_namelen = (socklen_t)sizeof(rep.addr);
536 		iov[0].iov_base = ldns_buffer_begin(rep.c->buffer);
537 		iov[0].iov_len = ldns_buffer_remaining(rep.c->buffer);
538 		msg.msg_iov = iov;
539 		msg.msg_iovlen = 1;
540 		msg.msg_control = ancil;
541 #ifndef S_SPLINT_S
542 		msg.msg_controllen = sizeof(ancil);
543 #endif /* S_SPLINT_S */
544 		msg.msg_flags = 0;
545 		rcv = recvmsg(fd, &msg, 0);
546 		if(rcv == -1) {
547 			if(errno != EAGAIN && errno != EINTR) {
548 				log_err("recvmsg failed: %s", strerror(errno));
549 			}
550 			return;
551 		}
552 		rep.addrlen = msg.msg_namelen;
553 		ldns_buffer_skip(rep.c->buffer, rcv);
554 		ldns_buffer_flip(rep.c->buffer);
555 		rep.srctype = 0;
556 #ifndef S_SPLINT_S
557 		for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
558 			cmsg = CMSG_NXTHDR(&msg, cmsg)) {
559 			if( cmsg->cmsg_level == IPPROTO_IPV6 &&
560 				cmsg->cmsg_type == IPV6_PKTINFO) {
561 				rep.srctype = 6;
562 				memmove(&rep.pktinfo.v6info, CMSG_DATA(cmsg),
563 					sizeof(struct in6_pktinfo));
564 				break;
565 #ifdef IP_PKTINFO
566 			} else if( cmsg->cmsg_level == IPPROTO_IP &&
567 				cmsg->cmsg_type == IP_PKTINFO) {
568 				rep.srctype = 4;
569 				memmove(&rep.pktinfo.v4info, CMSG_DATA(cmsg),
570 					sizeof(struct in_pktinfo));
571 				break;
572 #elif defined(IP_RECVDSTADDR)
573 			} else if( cmsg->cmsg_level == IPPROTO_IP &&
574 				cmsg->cmsg_type == IP_RECVDSTADDR) {
575 				rep.srctype = 4;
576 				memmove(&rep.pktinfo.v4addr, CMSG_DATA(cmsg),
577 					sizeof(struct in_addr));
578 				break;
579 #endif /* IP_PKTINFO or IP_RECVDSTADDR */
580 			}
581 		}
582 		if(verbosity >= VERB_ALGO)
583 			p_ancil("receive_udp on interface", &rep);
584 #endif /* S_SPLINT_S */
585 		fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
586 		if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
587 			/* send back immediate reply */
588 			(void)comm_point_send_udp_msg_if(rep.c, rep.c->buffer,
589 				(struct sockaddr*)&rep.addr, rep.addrlen, &rep);
590 		}
591 		if(rep.c->fd == -1) /* commpoint closed */
592 			break;
593 	}
594 #else
595 	(void)fd;
596 	(void)event;
597 	(void)arg;
598 	fatal_exit("recvmsg: No support for IPV6_PKTINFO. "
599 		"Please disable interface-automatic");
600 #endif /* AF_INET6 && IPV6_PKTINFO && HAVE_RECVMSG */
601 }
602 
603 void
604 comm_point_udp_callback(int fd, short event, void* arg)
605 {
606 	struct comm_reply rep;
607 	ssize_t rcv;
608 	int i;
609 
610 	rep.c = (struct comm_point*)arg;
611 	log_assert(rep.c->type == comm_udp);
612 
613 	if(!(event&EV_READ))
614 		return;
615 	log_assert(rep.c && rep.c->buffer && rep.c->fd == fd);
616 	comm_base_now(rep.c->ev->base);
617 	for(i=0; i<NUM_UDP_PER_SELECT; i++) {
618 		ldns_buffer_clear(rep.c->buffer);
619 		rep.addrlen = (socklen_t)sizeof(rep.addr);
620 		log_assert(fd != -1);
621 		log_assert(ldns_buffer_remaining(rep.c->buffer) > 0);
622 		rcv = recvfrom(fd, (void*)ldns_buffer_begin(rep.c->buffer),
623 			ldns_buffer_remaining(rep.c->buffer), 0,
624 			(struct sockaddr*)&rep.addr, &rep.addrlen);
625 		if(rcv == -1) {
626 #ifndef USE_WINSOCK
627 			if(errno != EAGAIN && errno != EINTR)
628 				log_err("recvfrom %d failed: %s",
629 					fd, strerror(errno));
630 #else
631 			if(WSAGetLastError() != WSAEINPROGRESS &&
632 				WSAGetLastError() != WSAECONNRESET &&
633 				WSAGetLastError()!= WSAEWOULDBLOCK)
634 				log_err("recvfrom failed: %s",
635 					wsa_strerror(WSAGetLastError()));
636 #endif
637 			return;
638 		}
639 		ldns_buffer_skip(rep.c->buffer, rcv);
640 		ldns_buffer_flip(rep.c->buffer);
641 		rep.srctype = 0;
642 		fptr_ok(fptr_whitelist_comm_point(rep.c->callback));
643 		if((*rep.c->callback)(rep.c, rep.c->cb_arg, NETEVENT_NOERROR, &rep)) {
644 			/* send back immediate reply */
645 			(void)comm_point_send_udp_msg(rep.c, rep.c->buffer,
646 				(struct sockaddr*)&rep.addr, rep.addrlen);
647 		}
648 		if(rep.c->fd != fd) /* commpoint closed to -1 or reused for
649 		another UDP port. Note rep.c cannot be reused with TCP fd. */
650 			break;
651 	}
652 }
653 
654 /** Use a new tcp handler for new query fd, set to read query */
655 static void
656 setup_tcp_handler(struct comm_point* c, int fd)
657 {
658 	log_assert(c->type == comm_tcp);
659 	log_assert(c->fd == -1);
660 	ldns_buffer_clear(c->buffer);
661 	c->tcp_is_reading = 1;
662 	c->tcp_byte_count = 0;
663 	comm_point_start_listening(c, fd, TCP_QUERY_TIMEOUT);
664 }
665 
666 void comm_base_handle_slow_accept(int fd, short event, void* arg)
667 {
668 	struct comm_base* b = (struct comm_base*)arg;
669 	/* timeout for the slow accept, re-enable accepts again */
670 	if(b->start_accept) {
671 		verbose(VERB_ALGO, "wait is over, slow accept disabled");
672 		fptr_ok(fptr_whitelist_start_accept(b->start_accept));
673 		(*b->start_accept)(b->cb_arg);
674 		b->eb->slow_accept_enabled = 0;
675 	}
676 }
677 
678 int comm_point_perform_accept(struct comm_point* c,
679 	struct sockaddr_storage* addr, socklen_t* addrlen)
680 {
681 	int new_fd;
682 	*addrlen = (socklen_t)sizeof(*addr);
683 	new_fd = accept(c->fd, (struct sockaddr*)addr, addrlen);
684 	if(new_fd == -1) {
685 #ifndef USE_WINSOCK
686 		/* EINTR is signal interrupt. others are closed connection. */
687 		if(	errno == EINTR || errno == EAGAIN
688 #ifdef EWOULDBLOCK
689 			|| errno == EWOULDBLOCK
690 #endif
691 #ifdef ECONNABORTED
692 			|| errno == ECONNABORTED
693 #endif
694 #ifdef EPROTO
695 			|| errno == EPROTO
696 #endif /* EPROTO */
697 			)
698 			return -1;
699 #if defined(ENFILE) && defined(EMFILE)
700 		if(errno == ENFILE || errno == EMFILE) {
701 			/* out of file descriptors, likely outside of our
702 			 * control. stop accept() calls for some time */
703 			if(c->ev->base->stop_accept) {
704 				struct comm_base* b = c->ev->base;
705 				struct timeval tv;
706 				verbose(VERB_ALGO, "out of file descriptors: "
707 					"slow accept");
708 				b->eb->slow_accept_enabled = 1;
709 				fptr_ok(fptr_whitelist_stop_accept(
710 					b->stop_accept));
711 				(*b->stop_accept)(b->cb_arg);
712 				/* set timeout, no mallocs */
713 				tv.tv_sec = NETEVENT_SLOW_ACCEPT_TIME/1000;
714 				tv.tv_usec = NETEVENT_SLOW_ACCEPT_TIME%1000;
715 				event_set(&b->eb->slow_accept, -1, EV_TIMEOUT,
716 					comm_base_handle_slow_accept, b);
717 				if(event_base_set(b->eb->base,
718 					&b->eb->slow_accept) != 0) {
719 					/* we do not want to log here, because
720 					 * that would spam the logfiles.
721 					 * error: "event_base_set failed." */
722 				}
723 				if(event_add(&b->eb->slow_accept, &tv) != 0) {
724 					/* we do not want to log here,
725 					 * error: "event_add failed." */
726 				}
727 			}
728 			return -1;
729 		}
730 #endif
731 		log_err("accept failed: %s", strerror(errno));
732 #else /* USE_WINSOCK */
733 		if(WSAGetLastError() == WSAEINPROGRESS ||
734 			WSAGetLastError() == WSAECONNRESET)
735 			return -1;
736 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
737 			winsock_tcp_wouldblock(&c->ev->ev, EV_READ);
738 			return -1;
739 		}
740 		log_err("accept failed: %s", wsa_strerror(WSAGetLastError()));
741 #endif
742 		log_addr(0, "remote address is", addr, *addrlen);
743 		return -1;
744 	}
745 	fd_set_nonblock(new_fd);
746 	return new_fd;
747 }
748 
749 #ifdef USE_WINSOCK
750 static long win_bio_cb(BIO *b, int oper, const char* ATTR_UNUSED(argp),
751         int ATTR_UNUSED(argi), long argl, long retvalue)
752 {
753 	verbose(VERB_ALGO, "bio_cb %d, %s %s %s", oper,
754 		(oper&BIO_CB_RETURN)?"return":"before",
755 		(oper&BIO_CB_READ)?"read":((oper&BIO_CB_WRITE)?"write":"other"),
756 		WSAGetLastError()==WSAEWOULDBLOCK?"wsawb":"");
757 	/* on windows, check if previous operation caused EWOULDBLOCK */
758 	if( (oper == (BIO_CB_READ|BIO_CB_RETURN) && argl == 0) ||
759 		(oper == (BIO_CB_GETS|BIO_CB_RETURN) && argl == 0)) {
760 		if(WSAGetLastError() == WSAEWOULDBLOCK)
761 			winsock_tcp_wouldblock((struct event*)
762 				BIO_get_callback_arg(b), EV_READ);
763 	}
764 	if( (oper == (BIO_CB_WRITE|BIO_CB_RETURN) && argl == 0) ||
765 		(oper == (BIO_CB_PUTS|BIO_CB_RETURN) && argl == 0)) {
766 		if(WSAGetLastError() == WSAEWOULDBLOCK)
767 			winsock_tcp_wouldblock((struct event*)
768 				BIO_get_callback_arg(b), EV_WRITE);
769 	}
770 	/* return original return value */
771 	return retvalue;
772 }
773 
774 /** set win bio callbacks for nonblocking operations */
775 void
776 comm_point_tcp_win_bio_cb(struct comm_point* c, void* thessl)
777 {
778 	SSL* ssl = (SSL*)thessl;
779 	/* set them both just in case, but usually they are the same BIO */
780 	BIO_set_callback(SSL_get_rbio(ssl), &win_bio_cb);
781 	BIO_set_callback_arg(SSL_get_rbio(ssl), (char*)&c->ev->ev);
782 	BIO_set_callback(SSL_get_wbio(ssl), &win_bio_cb);
783 	BIO_set_callback_arg(SSL_get_wbio(ssl), (char*)&c->ev->ev);
784 }
785 #endif
786 
787 void
788 comm_point_tcp_accept_callback(int fd, short event, void* arg)
789 {
790 	struct comm_point* c = (struct comm_point*)arg, *c_hdl;
791 	int new_fd;
792 	log_assert(c->type == comm_tcp_accept);
793 	if(!(event & EV_READ)) {
794 		log_info("ignoring tcp accept event %d", (int)event);
795 		return;
796 	}
797 	comm_base_now(c->ev->base);
798 	/* find free tcp handler. */
799 	if(!c->tcp_free) {
800 		log_warn("accepted too many tcp, connections full");
801 		return;
802 	}
803 	/* accept incoming connection. */
804 	c_hdl = c->tcp_free;
805 	log_assert(fd != -1);
806 	new_fd = comm_point_perform_accept(c, &c_hdl->repinfo.addr,
807 		&c_hdl->repinfo.addrlen);
808 	if(new_fd == -1)
809 		return;
810 	if(c->ssl) {
811 		c_hdl->ssl = incoming_ssl_fd(c->ssl, new_fd);
812 		if(!c_hdl->ssl) {
813 			c_hdl->fd = new_fd;
814 			comm_point_close(c_hdl);
815 			return;
816 		}
817 		c_hdl->ssl_shake_state = comm_ssl_shake_read;
818 #ifdef USE_WINSOCK
819 		comm_point_tcp_win_bio_cb(c_hdl, c_hdl->ssl);
820 #endif
821 	}
822 
823 	/* grab the tcp handler buffers */
824 	c->tcp_free = c_hdl->tcp_free;
825 	if(!c->tcp_free) {
826 		/* stop accepting incoming queries for now. */
827 		comm_point_stop_listening(c);
828 	}
829 	/* addr is dropped. Not needed for tcp reply. */
830 	setup_tcp_handler(c_hdl, new_fd);
831 }
832 
833 /** Make tcp handler free for next assignment */
834 static void
835 reclaim_tcp_handler(struct comm_point* c)
836 {
837 	log_assert(c->type == comm_tcp);
838 	if(c->ssl) {
839 		SSL_shutdown(c->ssl);
840 		SSL_free(c->ssl);
841 		c->ssl = NULL;
842 	}
843 	comm_point_close(c);
844 	if(c->tcp_parent) {
845 		c->tcp_free = c->tcp_parent->tcp_free;
846 		c->tcp_parent->tcp_free = c;
847 		if(!c->tcp_free) {
848 			/* re-enable listening on accept socket */
849 			comm_point_start_listening(c->tcp_parent, -1, -1);
850 		}
851 	}
852 }
853 
854 /** do the callback when writing is done */
855 static void
856 tcp_callback_writer(struct comm_point* c)
857 {
858 	log_assert(c->type == comm_tcp);
859 	ldns_buffer_clear(c->buffer);
860 	if(c->tcp_do_toggle_rw)
861 		c->tcp_is_reading = 1;
862 	c->tcp_byte_count = 0;
863 	/* switch from listening(write) to listening(read) */
864 	comm_point_stop_listening(c);
865 	comm_point_start_listening(c, -1, -1);
866 }
867 
868 /** do the callback when reading is done */
869 static void
870 tcp_callback_reader(struct comm_point* c)
871 {
872 	log_assert(c->type == comm_tcp || c->type == comm_local);
873 	ldns_buffer_flip(c->buffer);
874 	if(c->tcp_do_toggle_rw)
875 		c->tcp_is_reading = 0;
876 	c->tcp_byte_count = 0;
877 	if(c->type == comm_tcp)
878 		comm_point_stop_listening(c);
879 	fptr_ok(fptr_whitelist_comm_point(c->callback));
880 	if( (*c->callback)(c, c->cb_arg, NETEVENT_NOERROR, &c->repinfo) ) {
881 		comm_point_start_listening(c, -1, TCP_QUERY_TIMEOUT);
882 	}
883 }
884 
885 /** continue ssl handshake */
886 static int
887 ssl_handshake(struct comm_point* c)
888 {
889 	int r;
890 	if(c->ssl_shake_state == comm_ssl_shake_hs_read) {
891 		/* read condition satisfied back to writing */
892 		comm_point_listen_for_rw(c, 1, 1);
893 		c->ssl_shake_state = comm_ssl_shake_none;
894 		return 1;
895 	}
896 	if(c->ssl_shake_state == comm_ssl_shake_hs_write) {
897 		/* write condition satisfied, back to reading */
898 		comm_point_listen_for_rw(c, 1, 0);
899 		c->ssl_shake_state = comm_ssl_shake_none;
900 		return 1;
901 	}
902 
903 	ERR_clear_error();
904 	r = SSL_do_handshake(c->ssl);
905 	if(r != 1) {
906 		int want = SSL_get_error(c->ssl, r);
907 		if(want == SSL_ERROR_WANT_READ) {
908 			if(c->ssl_shake_state == comm_ssl_shake_read)
909 				return 1;
910 			c->ssl_shake_state = comm_ssl_shake_read;
911 			comm_point_listen_for_rw(c, 1, 0);
912 			return 1;
913 		} else if(want == SSL_ERROR_WANT_WRITE) {
914 			if(c->ssl_shake_state == comm_ssl_shake_write)
915 				return 1;
916 			c->ssl_shake_state = comm_ssl_shake_write;
917 			comm_point_listen_for_rw(c, 0, 1);
918 			return 1;
919 		} else if(r == 0) {
920 			return 0; /* closed */
921 		} else if(want == SSL_ERROR_SYSCALL) {
922 			/* SYSCALL and errno==0 means closed uncleanly */
923 			if(errno != 0)
924 				log_err("SSL_handshake syscall: %s",
925 					strerror(errno));
926 			return 0;
927 		} else {
928 			log_crypto_err("ssl handshake failed");
929 			log_addr(1, "ssl handshake failed", &c->repinfo.addr,
930 				c->repinfo.addrlen);
931 			return 0;
932 		}
933 	}
934 	/* this is where peer verification could take place */
935 	log_addr(VERB_ALGO, "SSL DNS connection", &c->repinfo.addr,
936 		c->repinfo.addrlen);
937 
938 	/* setup listen rw correctly */
939 	if(c->tcp_is_reading) {
940 		if(c->ssl_shake_state != comm_ssl_shake_read)
941 			comm_point_listen_for_rw(c, 1, 0);
942 	} else {
943 		comm_point_listen_for_rw(c, 1, 1);
944 	}
945 	c->ssl_shake_state = comm_ssl_shake_none;
946 	return 1;
947 }
948 
949 /** ssl read callback on TCP */
950 static int
951 ssl_handle_read(struct comm_point* c)
952 {
953 	int r;
954 	if(c->ssl_shake_state != comm_ssl_shake_none) {
955 		if(!ssl_handshake(c))
956 			return 0;
957 		if(c->ssl_shake_state != comm_ssl_shake_none)
958 			return 1;
959 	}
960 	if(c->tcp_byte_count < sizeof(uint16_t)) {
961 		/* read length bytes */
962 		ERR_clear_error();
963 		if((r=SSL_read(c->ssl, (void*)ldns_buffer_at(c->buffer,
964 			c->tcp_byte_count), (int)(sizeof(uint16_t) -
965 			c->tcp_byte_count))) <= 0) {
966 			int want = SSL_get_error(c->ssl, r);
967 			if(want == SSL_ERROR_ZERO_RETURN) {
968 				return 0; /* shutdown, closed */
969 			} else if(want == SSL_ERROR_WANT_READ) {
970 				return 1; /* read more later */
971 			} else if(want == SSL_ERROR_WANT_WRITE) {
972 				c->ssl_shake_state = comm_ssl_shake_hs_write;
973 				comm_point_listen_for_rw(c, 0, 1);
974 				return 1;
975 			} else if(want == SSL_ERROR_SYSCALL) {
976 				if(errno != 0)
977 					log_err("SSL_read syscall: %s",
978 						strerror(errno));
979 				return 0;
980 			}
981 			log_crypto_err("could not SSL_read");
982 			return 0;
983 		}
984 		c->tcp_byte_count += r;
985 		if(c->tcp_byte_count != sizeof(uint16_t))
986 			return 1;
987 		if(ldns_buffer_read_u16_at(c->buffer, 0) >
988 			ldns_buffer_capacity(c->buffer)) {
989 			verbose(VERB_QUERY, "ssl: dropped larger than buffer");
990 			return 0;
991 		}
992 		ldns_buffer_set_limit(c->buffer,
993 			ldns_buffer_read_u16_at(c->buffer, 0));
994 		if(ldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
995 			verbose(VERB_QUERY, "ssl: dropped bogus too short.");
996 			return 0;
997 		}
998 		verbose(VERB_ALGO, "Reading ssl tcp query of length %d",
999 			(int)ldns_buffer_limit(c->buffer));
1000 	}
1001 	log_assert(ldns_buffer_remaining(c->buffer) > 0);
1002 	ERR_clear_error();
1003 	r = SSL_read(c->ssl, (void*)ldns_buffer_current(c->buffer),
1004 		(int)ldns_buffer_remaining(c->buffer));
1005 	if(r <= 0) {
1006 		int want = SSL_get_error(c->ssl, r);
1007 		if(want == SSL_ERROR_ZERO_RETURN) {
1008 			return 0; /* shutdown, closed */
1009 		} else if(want == SSL_ERROR_WANT_READ) {
1010 			return 1; /* read more later */
1011 		} else if(want == SSL_ERROR_WANT_WRITE) {
1012 			c->ssl_shake_state = comm_ssl_shake_hs_write;
1013 			comm_point_listen_for_rw(c, 0, 1);
1014 			return 1;
1015 		} else if(want == SSL_ERROR_SYSCALL) {
1016 			if(errno != 0)
1017 				log_err("SSL_read syscall: %s",
1018 					strerror(errno));
1019 			return 0;
1020 		}
1021 		log_crypto_err("could not SSL_read");
1022 		return 0;
1023 	}
1024 	ldns_buffer_skip(c->buffer, (ssize_t)r);
1025 	if(ldns_buffer_remaining(c->buffer) <= 0) {
1026 		tcp_callback_reader(c);
1027 	}
1028 	return 1;
1029 }
1030 
1031 /** ssl write callback on TCP */
1032 static int
1033 ssl_handle_write(struct comm_point* c)
1034 {
1035 	int r;
1036 	if(c->ssl_shake_state != comm_ssl_shake_none) {
1037 		if(!ssl_handshake(c))
1038 			return 0;
1039 		if(c->ssl_shake_state != comm_ssl_shake_none)
1040 			return 1;
1041 	}
1042 	/* ignore return, if fails we may simply block */
1043 	(void)SSL_set_mode(c->ssl, SSL_MODE_ENABLE_PARTIAL_WRITE);
1044 	if(c->tcp_byte_count < sizeof(uint16_t)) {
1045 		uint16_t len = htons(ldns_buffer_limit(c->buffer));
1046 		ERR_clear_error();
1047 		r = SSL_write(c->ssl,
1048 			(void*)(((uint8_t*)&len)+c->tcp_byte_count),
1049 			(int)(sizeof(uint16_t)-c->tcp_byte_count));
1050 		if(r <= 0) {
1051 			int want = SSL_get_error(c->ssl, r);
1052 			if(want == SSL_ERROR_ZERO_RETURN) {
1053 				return 0; /* closed */
1054 			} else if(want == SSL_ERROR_WANT_READ) {
1055 				c->ssl_shake_state = comm_ssl_shake_read;
1056 				comm_point_listen_for_rw(c, 1, 0);
1057 				return 1; /* wait for read condition */
1058 			} else if(want == SSL_ERROR_WANT_WRITE) {
1059 				return 1; /* write more later */
1060 			} else if(want == SSL_ERROR_SYSCALL) {
1061 				if(errno != 0)
1062 					log_err("SSL_write syscall: %s",
1063 						strerror(errno));
1064 				return 0;
1065 			}
1066 			log_crypto_err("could not SSL_write");
1067 			return 0;
1068 		}
1069 		c->tcp_byte_count += r;
1070 		if(c->tcp_byte_count < sizeof(uint16_t))
1071 			return 1;
1072 		ldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1073 			sizeof(uint16_t));
1074 		if(ldns_buffer_remaining(c->buffer) == 0) {
1075 			tcp_callback_writer(c);
1076 			return 1;
1077 		}
1078 	}
1079 	log_assert(ldns_buffer_remaining(c->buffer) > 0);
1080 	ERR_clear_error();
1081 	r = SSL_write(c->ssl, (void*)ldns_buffer_current(c->buffer),
1082 		(int)ldns_buffer_remaining(c->buffer));
1083 	if(r <= 0) {
1084 		int want = SSL_get_error(c->ssl, r);
1085 		if(want == SSL_ERROR_ZERO_RETURN) {
1086 			return 0; /* closed */
1087 		} else if(want == SSL_ERROR_WANT_READ) {
1088 			c->ssl_shake_state = comm_ssl_shake_read;
1089 			comm_point_listen_for_rw(c, 1, 0);
1090 			return 1; /* wait for read condition */
1091 		} else if(want == SSL_ERROR_WANT_WRITE) {
1092 			return 1; /* write more later */
1093 		} else if(want == SSL_ERROR_SYSCALL) {
1094 			if(errno != 0)
1095 				log_err("SSL_write syscall: %s",
1096 					strerror(errno));
1097 			return 0;
1098 		}
1099 		log_crypto_err("could not SSL_write");
1100 		return 0;
1101 	}
1102 	ldns_buffer_skip(c->buffer, (ssize_t)r);
1103 
1104 	if(ldns_buffer_remaining(c->buffer) == 0) {
1105 		tcp_callback_writer(c);
1106 	}
1107 	return 1;
1108 }
1109 
1110 /** handle ssl tcp connection with dns contents */
1111 static int
1112 ssl_handle_it(struct comm_point* c)
1113 {
1114 	if(c->tcp_is_reading)
1115 		return ssl_handle_read(c);
1116 	return ssl_handle_write(c);
1117 }
1118 
1119 /** Handle tcp reading callback.
1120  * @param fd: file descriptor of socket.
1121  * @param c: comm point to read from into buffer.
1122  * @param short_ok: if true, very short packets are OK (for comm_local).
1123  * @return: 0 on error
1124  */
1125 static int
1126 comm_point_tcp_handle_read(int fd, struct comm_point* c, int short_ok)
1127 {
1128 	ssize_t r;
1129 	log_assert(c->type == comm_tcp || c->type == comm_local);
1130 	if(c->ssl)
1131 		return ssl_handle_it(c);
1132 	if(!c->tcp_is_reading)
1133 		return 0;
1134 
1135 	log_assert(fd != -1);
1136 	if(c->tcp_byte_count < sizeof(uint16_t)) {
1137 		/* read length bytes */
1138 		r = recv(fd,(void*)ldns_buffer_at(c->buffer,c->tcp_byte_count),
1139 			sizeof(uint16_t)-c->tcp_byte_count, 0);
1140 		if(r == 0)
1141 			return 0;
1142 		else if(r == -1) {
1143 #ifndef USE_WINSOCK
1144 			if(errno == EINTR || errno == EAGAIN)
1145 				return 1;
1146 #ifdef ECONNRESET
1147 			if(errno == ECONNRESET && verbosity < 2)
1148 				return 0; /* silence reset by peer */
1149 #endif
1150 			log_err("read (in tcp s): %s", strerror(errno));
1151 #else /* USE_WINSOCK */
1152 			if(WSAGetLastError() == WSAECONNRESET)
1153 				return 0;
1154 			if(WSAGetLastError() == WSAEINPROGRESS)
1155 				return 1;
1156 			if(WSAGetLastError() == WSAEWOULDBLOCK) {
1157 				winsock_tcp_wouldblock(&c->ev->ev, EV_READ);
1158 				return 1;
1159 			}
1160 			log_err("read (in tcp s): %s",
1161 				wsa_strerror(WSAGetLastError()));
1162 #endif
1163 			log_addr(0, "remote address is", &c->repinfo.addr,
1164 				c->repinfo.addrlen);
1165 			return 0;
1166 		}
1167 		c->tcp_byte_count += r;
1168 		if(c->tcp_byte_count != sizeof(uint16_t))
1169 			return 1;
1170 		if(ldns_buffer_read_u16_at(c->buffer, 0) >
1171 			ldns_buffer_capacity(c->buffer)) {
1172 			verbose(VERB_QUERY, "tcp: dropped larger than buffer");
1173 			return 0;
1174 		}
1175 		ldns_buffer_set_limit(c->buffer,
1176 			ldns_buffer_read_u16_at(c->buffer, 0));
1177 		if(!short_ok &&
1178 			ldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
1179 			verbose(VERB_QUERY, "tcp: dropped bogus too short.");
1180 			return 0;
1181 		}
1182 		verbose(VERB_ALGO, "Reading tcp query of length %d",
1183 			(int)ldns_buffer_limit(c->buffer));
1184 	}
1185 
1186 	log_assert(ldns_buffer_remaining(c->buffer) > 0);
1187 	r = recv(fd, (void*)ldns_buffer_current(c->buffer),
1188 		ldns_buffer_remaining(c->buffer), 0);
1189 	if(r == 0) {
1190 		return 0;
1191 	} else if(r == -1) {
1192 #ifndef USE_WINSOCK
1193 		if(errno == EINTR || errno == EAGAIN)
1194 			return 1;
1195 		log_err("read (in tcp r): %s", strerror(errno));
1196 #else /* USE_WINSOCK */
1197 		if(WSAGetLastError() == WSAECONNRESET)
1198 			return 0;
1199 		if(WSAGetLastError() == WSAEINPROGRESS)
1200 			return 1;
1201 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
1202 			winsock_tcp_wouldblock(&c->ev->ev, EV_READ);
1203 			return 1;
1204 		}
1205 		log_err("read (in tcp r): %s",
1206 			wsa_strerror(WSAGetLastError()));
1207 #endif
1208 		log_addr(0, "remote address is", &c->repinfo.addr,
1209 			c->repinfo.addrlen);
1210 		return 0;
1211 	}
1212 	ldns_buffer_skip(c->buffer, r);
1213 	if(ldns_buffer_remaining(c->buffer) <= 0) {
1214 		tcp_callback_reader(c);
1215 	}
1216 	return 1;
1217 }
1218 
1219 /**
1220  * Handle tcp writing callback.
1221  * @param fd: file descriptor of socket.
1222  * @param c: comm point to write buffer out of.
1223  * @return: 0 on error
1224  */
1225 static int
1226 comm_point_tcp_handle_write(int fd, struct comm_point* c)
1227 {
1228 	ssize_t r;
1229 	log_assert(c->type == comm_tcp);
1230 	if(c->tcp_is_reading && !c->ssl)
1231 		return 0;
1232 	log_assert(fd != -1);
1233 	if(c->tcp_byte_count == 0 && c->tcp_check_nb_connect) {
1234 		/* check for pending error from nonblocking connect */
1235 		/* from Stevens, unix network programming, vol1, 3rd ed, p450*/
1236 		int error = 0;
1237 		socklen_t len = (socklen_t)sizeof(error);
1238 		if(getsockopt(fd, SOL_SOCKET, SO_ERROR, (void*)&error,
1239 			&len) < 0){
1240 #ifndef USE_WINSOCK
1241 			error = errno; /* on solaris errno is error */
1242 #else /* USE_WINSOCK */
1243 			error = WSAGetLastError();
1244 #endif
1245 		}
1246 #ifndef USE_WINSOCK
1247 #if defined(EINPROGRESS) && defined(EWOULDBLOCK)
1248 		if(error == EINPROGRESS || error == EWOULDBLOCK)
1249 			return 1; /* try again later */
1250 #endif
1251 		else if(error != 0 && verbosity < 2)
1252 			return 0; /* silence lots of chatter in the logs */
1253                 else if(error != 0) {
1254 			log_err("tcp connect: %s", strerror(error));
1255 #else /* USE_WINSOCK */
1256 		/* examine error */
1257 		if(error == WSAEINPROGRESS)
1258 			return 1;
1259 		else if(error == WSAEWOULDBLOCK) {
1260 			winsock_tcp_wouldblock(&c->ev->ev, EV_WRITE);
1261 			return 1;
1262 		} else if(error != 0 && verbosity < 2)
1263 			return 0;
1264 		else if(error != 0) {
1265 			log_err("tcp connect: %s", wsa_strerror(error));
1266 #endif /* USE_WINSOCK */
1267 			log_addr(0, "remote address is", &c->repinfo.addr,
1268 				c->repinfo.addrlen);
1269 			return 0;
1270 		}
1271 	}
1272 	if(c->ssl)
1273 		return ssl_handle_it(c);
1274 
1275 	if(c->tcp_byte_count < sizeof(uint16_t)) {
1276 		uint16_t len = htons(ldns_buffer_limit(c->buffer));
1277 #ifdef HAVE_WRITEV
1278 		struct iovec iov[2];
1279 		iov[0].iov_base = (uint8_t*)&len + c->tcp_byte_count;
1280 		iov[0].iov_len = sizeof(uint16_t) - c->tcp_byte_count;
1281 		iov[1].iov_base = ldns_buffer_begin(c->buffer);
1282 		iov[1].iov_len = ldns_buffer_limit(c->buffer);
1283 		log_assert(iov[0].iov_len > 0);
1284 		log_assert(iov[1].iov_len > 0);
1285 		r = writev(fd, iov, 2);
1286 #else /* HAVE_WRITEV */
1287 		r = send(fd, (void*)(((uint8_t*)&len)+c->tcp_byte_count),
1288 			sizeof(uint16_t)-c->tcp_byte_count, 0);
1289 #endif /* HAVE_WRITEV */
1290 		if(r == -1) {
1291 #ifndef USE_WINSOCK
1292 #ifdef EPIPE
1293                 	if(errno == EPIPE && verbosity < 2)
1294                         	return 0; /* silence 'broken pipe' */
1295 #endif
1296 			if(errno == EINTR || errno == EAGAIN)
1297 				return 1;
1298 			log_err("tcp writev: %s", strerror(errno));
1299 #else
1300 			if(WSAGetLastError() == WSAENOTCONN)
1301 				return 1;
1302 			if(WSAGetLastError() == WSAEINPROGRESS)
1303 				return 1;
1304 			if(WSAGetLastError() == WSAEWOULDBLOCK) {
1305 				winsock_tcp_wouldblock(&c->ev->ev, EV_WRITE);
1306 				return 1;
1307 			}
1308 			log_err("tcp send s: %s",
1309 				wsa_strerror(WSAGetLastError()));
1310 #endif
1311 			log_addr(0, "remote address is", &c->repinfo.addr,
1312 				c->repinfo.addrlen);
1313 			return 0;
1314 		}
1315 		c->tcp_byte_count += r;
1316 		if(c->tcp_byte_count < sizeof(uint16_t))
1317 			return 1;
1318 		ldns_buffer_set_position(c->buffer, c->tcp_byte_count -
1319 			sizeof(uint16_t));
1320 		if(ldns_buffer_remaining(c->buffer) == 0) {
1321 			tcp_callback_writer(c);
1322 			return 1;
1323 		}
1324 	}
1325 	log_assert(ldns_buffer_remaining(c->buffer) > 0);
1326 	r = send(fd, (void*)ldns_buffer_current(c->buffer),
1327 		ldns_buffer_remaining(c->buffer), 0);
1328 	if(r == -1) {
1329 #ifndef USE_WINSOCK
1330 		if(errno == EINTR || errno == EAGAIN)
1331 			return 1;
1332 		log_err("tcp send r: %s", strerror(errno));
1333 #else
1334 		if(WSAGetLastError() == WSAEINPROGRESS)
1335 			return 1;
1336 		if(WSAGetLastError() == WSAEWOULDBLOCK) {
1337 			winsock_tcp_wouldblock(&c->ev->ev, EV_WRITE);
1338 			return 1;
1339 		}
1340 		log_err("tcp send r: %s",
1341 			wsa_strerror(WSAGetLastError()));
1342 #endif
1343 		log_addr(0, "remote address is", &c->repinfo.addr,
1344 			c->repinfo.addrlen);
1345 		return 0;
1346 	}
1347 	ldns_buffer_skip(c->buffer, r);
1348 
1349 	if(ldns_buffer_remaining(c->buffer) == 0) {
1350 		tcp_callback_writer(c);
1351 	}
1352 
1353 	return 1;
1354 }
1355 
1356 void
1357 comm_point_tcp_handle_callback(int fd, short event, void* arg)
1358 {
1359 	struct comm_point* c = (struct comm_point*)arg;
1360 	log_assert(c->type == comm_tcp);
1361 	comm_base_now(c->ev->base);
1362 
1363 	if(event&EV_READ) {
1364 		if(!comm_point_tcp_handle_read(fd, c, 0)) {
1365 			reclaim_tcp_handler(c);
1366 			if(!c->tcp_do_close) {
1367 				fptr_ok(fptr_whitelist_comm_point(
1368 					c->callback));
1369 				(void)(*c->callback)(c, c->cb_arg,
1370 					NETEVENT_CLOSED, NULL);
1371 			}
1372 		}
1373 		return;
1374 	}
1375 	if(event&EV_WRITE) {
1376 		if(!comm_point_tcp_handle_write(fd, c)) {
1377 			reclaim_tcp_handler(c);
1378 			if(!c->tcp_do_close) {
1379 				fptr_ok(fptr_whitelist_comm_point(
1380 					c->callback));
1381 				(void)(*c->callback)(c, c->cb_arg,
1382 					NETEVENT_CLOSED, NULL);
1383 			}
1384 		}
1385 		return;
1386 	}
1387 	if(event&EV_TIMEOUT) {
1388 		verbose(VERB_QUERY, "tcp took too long, dropped");
1389 		reclaim_tcp_handler(c);
1390 		if(!c->tcp_do_close) {
1391 			fptr_ok(fptr_whitelist_comm_point(c->callback));
1392 			(void)(*c->callback)(c, c->cb_arg,
1393 				NETEVENT_TIMEOUT, NULL);
1394 		}
1395 		return;
1396 	}
1397 	log_err("Ignored event %d for tcphdl.", event);
1398 }
1399 
1400 void comm_point_local_handle_callback(int fd, short event, void* arg)
1401 {
1402 	struct comm_point* c = (struct comm_point*)arg;
1403 	log_assert(c->type == comm_local);
1404 	comm_base_now(c->ev->base);
1405 
1406 	if(event&EV_READ) {
1407 		if(!comm_point_tcp_handle_read(fd, c, 1)) {
1408 			fptr_ok(fptr_whitelist_comm_point(c->callback));
1409 			(void)(*c->callback)(c, c->cb_arg, NETEVENT_CLOSED,
1410 				NULL);
1411 		}
1412 		return;
1413 	}
1414 	log_err("Ignored event %d for localhdl.", event);
1415 }
1416 
1417 void comm_point_raw_handle_callback(int ATTR_UNUSED(fd),
1418 	short event, void* arg)
1419 {
1420 	struct comm_point* c = (struct comm_point*)arg;
1421 	int err = NETEVENT_NOERROR;
1422 	log_assert(c->type == comm_raw);
1423 	comm_base_now(c->ev->base);
1424 
1425 	if(event&EV_TIMEOUT)
1426 		err = NETEVENT_TIMEOUT;
1427 	fptr_ok(fptr_whitelist_comm_point_raw(c->callback));
1428 	(void)(*c->callback)(c, c->cb_arg, err, NULL);
1429 }
1430 
1431 struct comm_point*
1432 comm_point_create_udp(struct comm_base *base, int fd, ldns_buffer* buffer,
1433 	comm_point_callback_t* callback, void* callback_arg)
1434 {
1435 	struct comm_point* c = (struct comm_point*)calloc(1,
1436 		sizeof(struct comm_point));
1437 	short evbits;
1438 	if(!c)
1439 		return NULL;
1440 	c->ev = (struct internal_event*)calloc(1,
1441 		sizeof(struct internal_event));
1442 	if(!c->ev) {
1443 		free(c);
1444 		return NULL;
1445 	}
1446 	c->ev->base = base;
1447 	c->fd = fd;
1448 	c->buffer = buffer;
1449 	c->timeout = NULL;
1450 	c->tcp_is_reading = 0;
1451 	c->tcp_byte_count = 0;
1452 	c->tcp_parent = NULL;
1453 	c->max_tcp_count = 0;
1454 	c->tcp_handlers = NULL;
1455 	c->tcp_free = NULL;
1456 	c->type = comm_udp;
1457 	c->tcp_do_close = 0;
1458 	c->do_not_close = 0;
1459 	c->tcp_do_toggle_rw = 0;
1460 	c->tcp_check_nb_connect = 0;
1461 	c->inuse = 0;
1462 	c->callback = callback;
1463 	c->cb_arg = callback_arg;
1464 	evbits = EV_READ | EV_PERSIST;
1465 	/* libevent stuff */
1466 	event_set(&c->ev->ev, c->fd, evbits, comm_point_udp_callback, c);
1467 	if(event_base_set(base->eb->base, &c->ev->ev) != 0) {
1468 		log_err("could not baseset udp event");
1469 		comm_point_delete(c);
1470 		return NULL;
1471 	}
1472 	if(fd!=-1 && event_add(&c->ev->ev, c->timeout) != 0 ) {
1473 		log_err("could not add udp event");
1474 		comm_point_delete(c);
1475 		return NULL;
1476 	}
1477 	return c;
1478 }
1479 
1480 struct comm_point*
1481 comm_point_create_udp_ancil(struct comm_base *base, int fd,
1482 	ldns_buffer* buffer,
1483 	comm_point_callback_t* callback, void* callback_arg)
1484 {
1485 	struct comm_point* c = (struct comm_point*)calloc(1,
1486 		sizeof(struct comm_point));
1487 	short evbits;
1488 	if(!c)
1489 		return NULL;
1490 	c->ev = (struct internal_event*)calloc(1,
1491 		sizeof(struct internal_event));
1492 	if(!c->ev) {
1493 		free(c);
1494 		return NULL;
1495 	}
1496 	c->ev->base = base;
1497 	c->fd = fd;
1498 	c->buffer = buffer;
1499 	c->timeout = NULL;
1500 	c->tcp_is_reading = 0;
1501 	c->tcp_byte_count = 0;
1502 	c->tcp_parent = NULL;
1503 	c->max_tcp_count = 0;
1504 	c->tcp_handlers = NULL;
1505 	c->tcp_free = NULL;
1506 	c->type = comm_udp;
1507 	c->tcp_do_close = 0;
1508 	c->do_not_close = 0;
1509 	c->inuse = 0;
1510 	c->tcp_do_toggle_rw = 0;
1511 	c->tcp_check_nb_connect = 0;
1512 	c->callback = callback;
1513 	c->cb_arg = callback_arg;
1514 	evbits = EV_READ | EV_PERSIST;
1515 	/* libevent stuff */
1516 	event_set(&c->ev->ev, c->fd, evbits, comm_point_udp_ancil_callback, c);
1517 	if(event_base_set(base->eb->base, &c->ev->ev) != 0) {
1518 		log_err("could not baseset udp event");
1519 		comm_point_delete(c);
1520 		return NULL;
1521 	}
1522 	if(fd!=-1 && event_add(&c->ev->ev, c->timeout) != 0 ) {
1523 		log_err("could not add udp event");
1524 		comm_point_delete(c);
1525 		return NULL;
1526 	}
1527 	return c;
1528 }
1529 
1530 static struct comm_point*
1531 comm_point_create_tcp_handler(struct comm_base *base,
1532 	struct comm_point* parent, size_t bufsize,
1533         comm_point_callback_t* callback, void* callback_arg)
1534 {
1535 	struct comm_point* c = (struct comm_point*)calloc(1,
1536 		sizeof(struct comm_point));
1537 	short evbits;
1538 	if(!c)
1539 		return NULL;
1540 	c->ev = (struct internal_event*)calloc(1,
1541 		sizeof(struct internal_event));
1542 	if(!c->ev) {
1543 		free(c);
1544 		return NULL;
1545 	}
1546 	c->ev->base = base;
1547 	c->fd = -1;
1548 	c->buffer = ldns_buffer_new(bufsize);
1549 	if(!c->buffer) {
1550 		free(c->ev);
1551 		free(c);
1552 		return NULL;
1553 	}
1554 	c->timeout = (struct timeval*)malloc(sizeof(struct timeval));
1555 	if(!c->timeout) {
1556 		ldns_buffer_free(c->buffer);
1557 		free(c->ev);
1558 		free(c);
1559 		return NULL;
1560 	}
1561 	c->tcp_is_reading = 0;
1562 	c->tcp_byte_count = 0;
1563 	c->tcp_parent = parent;
1564 	c->max_tcp_count = 0;
1565 	c->tcp_handlers = NULL;
1566 	c->tcp_free = NULL;
1567 	c->type = comm_tcp;
1568 	c->tcp_do_close = 0;
1569 	c->do_not_close = 0;
1570 	c->tcp_do_toggle_rw = 1;
1571 	c->tcp_check_nb_connect = 0;
1572 	c->repinfo.c = c;
1573 	c->callback = callback;
1574 	c->cb_arg = callback_arg;
1575 	/* add to parent free list */
1576 	c->tcp_free = parent->tcp_free;
1577 	parent->tcp_free = c;
1578 	/* libevent stuff */
1579 	evbits = EV_PERSIST | EV_READ | EV_TIMEOUT;
1580 	event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_handle_callback, c);
1581 	if(event_base_set(base->eb->base, &c->ev->ev) != 0)
1582 	{
1583 		log_err("could not basetset tcphdl event");
1584 		parent->tcp_free = c->tcp_free;
1585 		free(c->ev);
1586 		free(c);
1587 		return NULL;
1588 	}
1589 	return c;
1590 }
1591 
1592 struct comm_point*
1593 comm_point_create_tcp(struct comm_base *base, int fd, int num, size_t bufsize,
1594         comm_point_callback_t* callback, void* callback_arg)
1595 {
1596 	struct comm_point* c = (struct comm_point*)calloc(1,
1597 		sizeof(struct comm_point));
1598 	short evbits;
1599 	int i;
1600 	/* first allocate the TCP accept listener */
1601 	if(!c)
1602 		return NULL;
1603 	c->ev = (struct internal_event*)calloc(1,
1604 		sizeof(struct internal_event));
1605 	if(!c->ev) {
1606 		free(c);
1607 		return NULL;
1608 	}
1609 	c->ev->base = base;
1610 	c->fd = fd;
1611 	c->buffer = NULL;
1612 	c->timeout = NULL;
1613 	c->tcp_is_reading = 0;
1614 	c->tcp_byte_count = 0;
1615 	c->tcp_parent = NULL;
1616 	c->max_tcp_count = num;
1617 	c->tcp_handlers = (struct comm_point**)calloc((size_t)num,
1618 		sizeof(struct comm_point*));
1619 	if(!c->tcp_handlers) {
1620 		free(c->ev);
1621 		free(c);
1622 		return NULL;
1623 	}
1624 	c->tcp_free = NULL;
1625 	c->type = comm_tcp_accept;
1626 	c->tcp_do_close = 0;
1627 	c->do_not_close = 0;
1628 	c->tcp_do_toggle_rw = 0;
1629 	c->tcp_check_nb_connect = 0;
1630 	c->callback = NULL;
1631 	c->cb_arg = NULL;
1632 	evbits = EV_READ | EV_PERSIST;
1633 	/* libevent stuff */
1634 	event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_accept_callback, c);
1635 	if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
1636 		event_add(&c->ev->ev, c->timeout) != 0 )
1637 	{
1638 		log_err("could not add tcpacc event");
1639 		comm_point_delete(c);
1640 		return NULL;
1641 	}
1642 
1643 	/* now prealloc the tcp handlers */
1644 	for(i=0; i<num; i++) {
1645 		c->tcp_handlers[i] = comm_point_create_tcp_handler(base,
1646 			c, bufsize, callback, callback_arg);
1647 		if(!c->tcp_handlers[i]) {
1648 			comm_point_delete(c);
1649 			return NULL;
1650 		}
1651 	}
1652 
1653 	return c;
1654 }
1655 
1656 struct comm_point*
1657 comm_point_create_tcp_out(struct comm_base *base, size_t bufsize,
1658         comm_point_callback_t* callback, void* callback_arg)
1659 {
1660 	struct comm_point* c = (struct comm_point*)calloc(1,
1661 		sizeof(struct comm_point));
1662 	short evbits;
1663 	if(!c)
1664 		return NULL;
1665 	c->ev = (struct internal_event*)calloc(1,
1666 		sizeof(struct internal_event));
1667 	if(!c->ev) {
1668 		free(c);
1669 		return NULL;
1670 	}
1671 	c->ev->base = base;
1672 	c->fd = -1;
1673 	c->buffer = ldns_buffer_new(bufsize);
1674 	if(!c->buffer) {
1675 		free(c->ev);
1676 		free(c);
1677 		return NULL;
1678 	}
1679 	c->timeout = NULL;
1680 	c->tcp_is_reading = 0;
1681 	c->tcp_byte_count = 0;
1682 	c->tcp_parent = NULL;
1683 	c->max_tcp_count = 0;
1684 	c->tcp_handlers = NULL;
1685 	c->tcp_free = NULL;
1686 	c->type = comm_tcp;
1687 	c->tcp_do_close = 0;
1688 	c->do_not_close = 0;
1689 	c->tcp_do_toggle_rw = 1;
1690 	c->tcp_check_nb_connect = 1;
1691 	c->repinfo.c = c;
1692 	c->callback = callback;
1693 	c->cb_arg = callback_arg;
1694 	evbits = EV_PERSIST | EV_WRITE;
1695 	event_set(&c->ev->ev, c->fd, evbits, comm_point_tcp_handle_callback, c);
1696 	if(event_base_set(base->eb->base, &c->ev->ev) != 0)
1697 	{
1698 		log_err("could not basetset tcpout event");
1699 		ldns_buffer_free(c->buffer);
1700 		free(c->ev);
1701 		free(c);
1702 		return NULL;
1703 	}
1704 
1705 	return c;
1706 }
1707 
1708 struct comm_point*
1709 comm_point_create_local(struct comm_base *base, int fd, size_t bufsize,
1710         comm_point_callback_t* callback, void* callback_arg)
1711 {
1712 	struct comm_point* c = (struct comm_point*)calloc(1,
1713 		sizeof(struct comm_point));
1714 	short evbits;
1715 	if(!c)
1716 		return NULL;
1717 	c->ev = (struct internal_event*)calloc(1,
1718 		sizeof(struct internal_event));
1719 	if(!c->ev) {
1720 		free(c);
1721 		return NULL;
1722 	}
1723 	c->ev->base = base;
1724 	c->fd = fd;
1725 	c->buffer = ldns_buffer_new(bufsize);
1726 	if(!c->buffer) {
1727 		free(c->ev);
1728 		free(c);
1729 		return NULL;
1730 	}
1731 	c->timeout = NULL;
1732 	c->tcp_is_reading = 1;
1733 	c->tcp_byte_count = 0;
1734 	c->tcp_parent = NULL;
1735 	c->max_tcp_count = 0;
1736 	c->tcp_handlers = NULL;
1737 	c->tcp_free = NULL;
1738 	c->type = comm_local;
1739 	c->tcp_do_close = 0;
1740 	c->do_not_close = 1;
1741 	c->tcp_do_toggle_rw = 0;
1742 	c->tcp_check_nb_connect = 0;
1743 	c->callback = callback;
1744 	c->cb_arg = callback_arg;
1745 	/* libevent stuff */
1746 	evbits = EV_PERSIST | EV_READ;
1747 	event_set(&c->ev->ev, c->fd, evbits, comm_point_local_handle_callback,
1748 		c);
1749 	if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
1750 		event_add(&c->ev->ev, c->timeout) != 0 )
1751 	{
1752 		log_err("could not add localhdl event");
1753 		free(c->ev);
1754 		free(c);
1755 		return NULL;
1756 	}
1757 	return c;
1758 }
1759 
1760 struct comm_point*
1761 comm_point_create_raw(struct comm_base* base, int fd, int writing,
1762 	comm_point_callback_t* callback, void* callback_arg)
1763 {
1764 	struct comm_point* c = (struct comm_point*)calloc(1,
1765 		sizeof(struct comm_point));
1766 	short evbits;
1767 	if(!c)
1768 		return NULL;
1769 	c->ev = (struct internal_event*)calloc(1,
1770 		sizeof(struct internal_event));
1771 	if(!c->ev) {
1772 		free(c);
1773 		return NULL;
1774 	}
1775 	c->ev->base = base;
1776 	c->fd = fd;
1777 	c->buffer = NULL;
1778 	c->timeout = NULL;
1779 	c->tcp_is_reading = 0;
1780 	c->tcp_byte_count = 0;
1781 	c->tcp_parent = NULL;
1782 	c->max_tcp_count = 0;
1783 	c->tcp_handlers = NULL;
1784 	c->tcp_free = NULL;
1785 	c->type = comm_raw;
1786 	c->tcp_do_close = 0;
1787 	c->do_not_close = 1;
1788 	c->tcp_do_toggle_rw = 0;
1789 	c->tcp_check_nb_connect = 0;
1790 	c->callback = callback;
1791 	c->cb_arg = callback_arg;
1792 	/* libevent stuff */
1793 	if(writing)
1794 		evbits = EV_PERSIST | EV_WRITE;
1795 	else 	evbits = EV_PERSIST | EV_READ;
1796 	event_set(&c->ev->ev, c->fd, evbits, comm_point_raw_handle_callback,
1797 		c);
1798 	if(event_base_set(base->eb->base, &c->ev->ev) != 0 ||
1799 		event_add(&c->ev->ev, c->timeout) != 0 )
1800 	{
1801 		log_err("could not add rawhdl event");
1802 		free(c->ev);
1803 		free(c);
1804 		return NULL;
1805 	}
1806 	return c;
1807 }
1808 
1809 void
1810 comm_point_close(struct comm_point* c)
1811 {
1812 	if(!c)
1813 		return;
1814 	if(c->fd != -1)
1815 		if(event_del(&c->ev->ev) != 0) {
1816 			log_err("could not event_del on close");
1817 		}
1818 	/* close fd after removing from event lists, or epoll.. is messed up */
1819 	if(c->fd != -1 && !c->do_not_close) {
1820 		verbose(VERB_ALGO, "close fd %d", c->fd);
1821 #ifndef USE_WINSOCK
1822 		close(c->fd);
1823 #else
1824 		closesocket(c->fd);
1825 #endif
1826 	}
1827 	c->fd = -1;
1828 }
1829 
1830 void
1831 comm_point_delete(struct comm_point* c)
1832 {
1833 	if(!c)
1834 		return;
1835 	if(c->type == comm_tcp && c->ssl) {
1836 		SSL_shutdown(c->ssl);
1837 		SSL_free(c->ssl);
1838 	}
1839 	comm_point_close(c);
1840 	if(c->tcp_handlers) {
1841 		int i;
1842 		for(i=0; i<c->max_tcp_count; i++)
1843 			comm_point_delete(c->tcp_handlers[i]);
1844 		free(c->tcp_handlers);
1845 	}
1846 	free(c->timeout);
1847 	if(c->type == comm_tcp || c->type == comm_local)
1848 		ldns_buffer_free(c->buffer);
1849 	free(c->ev);
1850 	free(c);
1851 }
1852 
1853 void
1854 comm_point_send_reply(struct comm_reply *repinfo)
1855 {
1856 	log_assert(repinfo && repinfo->c);
1857 	if(repinfo->c->type == comm_udp) {
1858 		if(repinfo->srctype)
1859 			comm_point_send_udp_msg_if(repinfo->c,
1860 			repinfo->c->buffer, (struct sockaddr*)&repinfo->addr,
1861 			repinfo->addrlen, repinfo);
1862 		else
1863 			comm_point_send_udp_msg(repinfo->c, repinfo->c->buffer,
1864 			(struct sockaddr*)&repinfo->addr, repinfo->addrlen);
1865 	} else {
1866 		comm_point_start_listening(repinfo->c, -1, TCP_QUERY_TIMEOUT);
1867 	}
1868 }
1869 
1870 void
1871 comm_point_drop_reply(struct comm_reply* repinfo)
1872 {
1873 	if(!repinfo)
1874 		return;
1875 	log_assert(repinfo && repinfo->c);
1876 	log_assert(repinfo->c->type != comm_tcp_accept);
1877 	if(repinfo->c->type == comm_udp)
1878 		return;
1879 	reclaim_tcp_handler(repinfo->c);
1880 }
1881 
1882 void
1883 comm_point_stop_listening(struct comm_point* c)
1884 {
1885 	verbose(VERB_ALGO, "comm point stop listening %d", c->fd);
1886 	if(event_del(&c->ev->ev) != 0) {
1887 		log_err("event_del error to stoplisten");
1888 	}
1889 }
1890 
1891 void
1892 comm_point_start_listening(struct comm_point* c, int newfd, int sec)
1893 {
1894 	verbose(VERB_ALGO, "comm point start listening %d",
1895 		c->fd==-1?newfd:c->fd);
1896 	if(c->type == comm_tcp_accept && !c->tcp_free) {
1897 		/* no use to start listening no free slots. */
1898 		return;
1899 	}
1900 	if(sec != -1 && sec != 0) {
1901 		if(!c->timeout) {
1902 			c->timeout = (struct timeval*)malloc(sizeof(
1903 				struct timeval));
1904 			if(!c->timeout) {
1905 				log_err("cpsl: malloc failed. No net read.");
1906 				return;
1907 			}
1908 		}
1909 		c->ev->ev.ev_events |= EV_TIMEOUT;
1910 #ifndef S_SPLINT_S /* splint fails on struct timeval. */
1911 		c->timeout->tv_sec = sec;
1912 		c->timeout->tv_usec = 0;
1913 #endif /* S_SPLINT_S */
1914 	}
1915 	if(c->type == comm_tcp) {
1916 		c->ev->ev.ev_events &= ~(EV_READ|EV_WRITE);
1917 		if(c->tcp_is_reading)
1918 			c->ev->ev.ev_events |= EV_READ;
1919 		else	c->ev->ev.ev_events |= EV_WRITE;
1920 	}
1921 	if(newfd != -1) {
1922 		if(c->fd != -1) {
1923 #ifndef USE_WINSOCK
1924 			close(c->fd);
1925 #else
1926 			closesocket(c->fd);
1927 #endif
1928 		}
1929 		c->fd = newfd;
1930 		c->ev->ev.ev_fd = c->fd;
1931 	}
1932 	if(event_add(&c->ev->ev, sec==0?NULL:c->timeout) != 0) {
1933 		log_err("event_add failed. in cpsl.");
1934 	}
1935 }
1936 
1937 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr)
1938 {
1939 	verbose(VERB_ALGO, "comm point listen_for_rw %d %d", c->fd, wr);
1940 	if(event_del(&c->ev->ev) != 0) {
1941 		log_err("event_del error to cplf");
1942 	}
1943 	c->ev->ev.ev_events &= ~(EV_READ|EV_WRITE);
1944 	if(rd) c->ev->ev.ev_events |= EV_READ;
1945 	if(wr) c->ev->ev.ev_events |= EV_WRITE;
1946 	if(event_add(&c->ev->ev, c->timeout) != 0) {
1947 		log_err("event_add failed. in cplf.");
1948 	}
1949 }
1950 
1951 size_t comm_point_get_mem(struct comm_point* c)
1952 {
1953 	size_t s;
1954 	if(!c)
1955 		return 0;
1956 	s = sizeof(*c) + sizeof(*c->ev);
1957 	if(c->timeout)
1958 		s += sizeof(*c->timeout);
1959 	if(c->type == comm_tcp || c->type == comm_local)
1960 		s += sizeof(*c->buffer) + ldns_buffer_capacity(c->buffer);
1961 	if(c->type == comm_tcp_accept) {
1962 		int i;
1963 		for(i=0; i<c->max_tcp_count; i++)
1964 			s += comm_point_get_mem(c->tcp_handlers[i]);
1965 	}
1966 	return s;
1967 }
1968 
1969 struct comm_timer*
1970 comm_timer_create(struct comm_base* base, void (*cb)(void*), void* cb_arg)
1971 {
1972 	struct comm_timer *tm = (struct comm_timer*)calloc(1,
1973 		sizeof(struct comm_timer));
1974 	if(!tm)
1975 		return NULL;
1976 	tm->ev_timer = (struct internal_timer*)calloc(1,
1977 		sizeof(struct internal_timer));
1978 	if(!tm->ev_timer) {
1979 		log_err("malloc failed");
1980 		free(tm);
1981 		return NULL;
1982 	}
1983 	tm->ev_timer->base = base;
1984 	tm->callback = cb;
1985 	tm->cb_arg = cb_arg;
1986 	event_set(&tm->ev_timer->ev, -1, EV_TIMEOUT,
1987 		comm_timer_callback, tm);
1988 	if(event_base_set(base->eb->base, &tm->ev_timer->ev) != 0) {
1989 		log_err("timer_create: event_base_set failed.");
1990 		free(tm->ev_timer);
1991 		free(tm);
1992 		return NULL;
1993 	}
1994 	return tm;
1995 }
1996 
1997 void
1998 comm_timer_disable(struct comm_timer* timer)
1999 {
2000 	if(!timer)
2001 		return;
2002 	evtimer_del(&timer->ev_timer->ev);
2003 	timer->ev_timer->enabled = 0;
2004 }
2005 
2006 void
2007 comm_timer_set(struct comm_timer* timer, struct timeval* tv)
2008 {
2009 	log_assert(tv);
2010 	if(timer->ev_timer->enabled)
2011 		comm_timer_disable(timer);
2012 	event_set(&timer->ev_timer->ev, -1, EV_TIMEOUT,
2013 		comm_timer_callback, timer);
2014 	if(event_base_set(timer->ev_timer->base->eb->base,
2015 		&timer->ev_timer->ev) != 0)
2016 		log_err("comm_timer_set: set_base failed.");
2017 	if(evtimer_add(&timer->ev_timer->ev, tv) != 0)
2018 		log_err("comm_timer_set: evtimer_add failed.");
2019 	timer->ev_timer->enabled = 1;
2020 }
2021 
2022 void
2023 comm_timer_delete(struct comm_timer* timer)
2024 {
2025 	if(!timer)
2026 		return;
2027 	comm_timer_disable(timer);
2028 	free(timer->ev_timer);
2029 	free(timer);
2030 }
2031 
2032 void
2033 comm_timer_callback(int ATTR_UNUSED(fd), short event, void* arg)
2034 {
2035 	struct comm_timer* tm = (struct comm_timer*)arg;
2036 	if(!(event&EV_TIMEOUT))
2037 		return;
2038 	comm_base_now(tm->ev_timer->base);
2039 	tm->ev_timer->enabled = 0;
2040 	fptr_ok(fptr_whitelist_comm_timer(tm->callback));
2041 	(*tm->callback)(tm->cb_arg);
2042 }
2043 
2044 int
2045 comm_timer_is_set(struct comm_timer* timer)
2046 {
2047 	return (int)timer->ev_timer->enabled;
2048 }
2049 
2050 size_t
2051 comm_timer_get_mem(struct comm_timer* timer)
2052 {
2053 	return sizeof(*timer) + sizeof(struct internal_timer);
2054 }
2055 
2056 struct comm_signal*
2057 comm_signal_create(struct comm_base* base,
2058         void (*callback)(int, void*), void* cb_arg)
2059 {
2060 	struct comm_signal* com = (struct comm_signal*)malloc(
2061 		sizeof(struct comm_signal));
2062 	if(!com) {
2063 		log_err("malloc failed");
2064 		return NULL;
2065 	}
2066 	com->base = base;
2067 	com->callback = callback;
2068 	com->cb_arg = cb_arg;
2069 	com->ev_signal = NULL;
2070 	return com;
2071 }
2072 
2073 void
2074 comm_signal_callback(int sig, short event, void* arg)
2075 {
2076 	struct comm_signal* comsig = (struct comm_signal*)arg;
2077 	if(!(event & EV_SIGNAL))
2078 		return;
2079 	comm_base_now(comsig->base);
2080 	fptr_ok(fptr_whitelist_comm_signal(comsig->callback));
2081 	(*comsig->callback)(sig, comsig->cb_arg);
2082 }
2083 
2084 int
2085 comm_signal_bind(struct comm_signal* comsig, int sig)
2086 {
2087 	struct internal_signal* entry = (struct internal_signal*)calloc(1,
2088 		sizeof(struct internal_signal));
2089 	if(!entry) {
2090 		log_err("malloc failed");
2091 		return 0;
2092 	}
2093 	log_assert(comsig);
2094 	/* add signal event */
2095 	signal_set(&entry->ev, sig, comm_signal_callback, comsig);
2096 	if(event_base_set(comsig->base->eb->base, &entry->ev) != 0) {
2097 		log_err("Could not set signal base");
2098 		free(entry);
2099 		return 0;
2100 	}
2101 	if(signal_add(&entry->ev, NULL) != 0) {
2102 		log_err("Could not add signal handler");
2103 		free(entry);
2104 		return 0;
2105 	}
2106 	/* link into list */
2107 	entry->next = comsig->ev_signal;
2108 	comsig->ev_signal = entry;
2109 	return 1;
2110 }
2111 
2112 void
2113 comm_signal_delete(struct comm_signal* comsig)
2114 {
2115 	struct internal_signal* p, *np;
2116 	if(!comsig)
2117 		return;
2118 	p=comsig->ev_signal;
2119 	while(p) {
2120 		np = p->next;
2121 		signal_del(&p->ev);
2122 		free(p);
2123 		p = np;
2124 	}
2125 	free(comsig);
2126 }
2127