1 /*
2  * services/outside_network.c - implement sending of queries and wait answer.
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 has functions to send queries to authoritative servers and
40  * wait for the pending answer events.
41  */
42 #include "config.h"
43 #include <ctype.h>
44 #ifdef HAVE_SYS_TYPES_H
45 #  include <sys/types.h>
46 #endif
47 #include <sys/time.h>
48 #include "services/outside_network.h"
49 #include "services/listen_dnsport.h"
50 #include "services/cache/infra.h"
51 #include "util/data/msgparse.h"
52 #include "util/data/msgreply.h"
53 #include "util/data/msgencode.h"
54 #include "util/data/dname.h"
55 #include "util/netevent.h"
56 #include "util/log.h"
57 #include "util/net_help.h"
58 #include "util/random.h"
59 #include "util/fptr_wlist.h"
60 #include "sldns/sbuffer.h"
61 #include "dnstap/dnstap.h"
62 #ifdef HAVE_OPENSSL_SSL_H
63 #include <openssl/ssl.h>
64 #endif
65 
66 #ifdef HAVE_NETDB_H
67 #include <netdb.h>
68 #endif
69 #include <fcntl.h>
70 
71 /** number of times to retry making a random ID that is unique. */
72 #define MAX_ID_RETRY 1000
73 /** number of times to retry finding interface, port that can be opened. */
74 #define MAX_PORT_RETRY 10000
75 /** number of retries on outgoing UDP queries */
76 #define OUTBOUND_UDP_RETRY 1
77 
78 /** initiate TCP transaction for serviced query */
79 static void serviced_tcp_initiate(struct serviced_query* sq, sldns_buffer* buff);
80 /** with a fd available, randomize and send UDP */
81 static int randomize_and_send_udp(struct pending* pend, sldns_buffer* packet,
82 	int timeout);
83 
84 /** remove waiting tcp from the outnet waiting list */
85 static void waiting_list_remove(struct outside_network* outnet,
86 	struct waiting_tcp* w);
87 
88 int
89 pending_cmp(const void* key1, const void* key2)
90 {
91 	struct pending *p1 = (struct pending*)key1;
92 	struct pending *p2 = (struct pending*)key2;
93 	if(p1->id < p2->id)
94 		return -1;
95 	if(p1->id > p2->id)
96 		return 1;
97 	log_assert(p1->id == p2->id);
98 	return sockaddr_cmp(&p1->addr, p1->addrlen, &p2->addr, p2->addrlen);
99 }
100 
101 int
102 serviced_cmp(const void* key1, const void* key2)
103 {
104 	struct serviced_query* q1 = (struct serviced_query*)key1;
105 	struct serviced_query* q2 = (struct serviced_query*)key2;
106 	int r;
107 	if(q1->qbuflen < q2->qbuflen)
108 		return -1;
109 	if(q1->qbuflen > q2->qbuflen)
110 		return 1;
111 	log_assert(q1->qbuflen == q2->qbuflen);
112 	log_assert(q1->qbuflen >= 15 /* 10 header, root, type, class */);
113 	/* alternate casing of qname is still the same query */
114 	if((r = memcmp(q1->qbuf, q2->qbuf, 10)) != 0)
115 		return r;
116 	if((r = memcmp(q1->qbuf+q1->qbuflen-4, q2->qbuf+q2->qbuflen-4, 4)) != 0)
117 		return r;
118 	if(q1->dnssec != q2->dnssec) {
119 		if(q1->dnssec < q2->dnssec)
120 			return -1;
121 		return 1;
122 	}
123 	if((r = query_dname_compare(q1->qbuf+10, q2->qbuf+10)) != 0)
124 		return r;
125 	if((r = edns_opt_list_compare(q1->opt_list, q2->opt_list)) != 0)
126 		return r;
127 	return sockaddr_cmp(&q1->addr, q1->addrlen, &q2->addr, q2->addrlen);
128 }
129 
130 /** delete waiting_tcp entry. Does not unlink from waiting list.
131  * @param w: to delete.
132  */
133 static void
134 waiting_tcp_delete(struct waiting_tcp* w)
135 {
136 	if(!w) return;
137 	if(w->timer)
138 		comm_timer_delete(w->timer);
139 	free(w);
140 }
141 
142 /**
143  * Pick random outgoing-interface of that family, and bind it.
144  * port set to 0 so OS picks a port number for us.
145  * if it is the ANY address, do not bind.
146  * @param w: tcp structure with destination address.
147  * @param s: socket fd.
148  * @return false on error, socket closed.
149  */
150 static int
151 pick_outgoing_tcp(struct waiting_tcp* w, int s)
152 {
153 	struct port_if* pi = NULL;
154 	int num;
155 #ifdef INET6
156 	if(addr_is_ip6(&w->addr, w->addrlen))
157 		num = w->outnet->num_ip6;
158 	else
159 #endif
160 		num = w->outnet->num_ip4;
161 	if(num == 0) {
162 		log_err("no TCP outgoing interfaces of family");
163 		log_addr(VERB_OPS, "for addr", &w->addr, w->addrlen);
164 #ifndef USE_WINSOCK
165 		close(s);
166 #else
167 		closesocket(s);
168 #endif
169 		return 0;
170 	}
171 #ifdef INET6
172 	if(addr_is_ip6(&w->addr, w->addrlen))
173 		pi = &w->outnet->ip6_ifs[ub_random_max(w->outnet->rnd, num)];
174 	else
175 #endif
176 		pi = &w->outnet->ip4_ifs[ub_random_max(w->outnet->rnd, num)];
177 	log_assert(pi);
178 	if(addr_is_any(&pi->addr, pi->addrlen)) {
179 		/* binding to the ANY interface is for listening sockets */
180 		return 1;
181 	}
182 	/* set port to 0 */
183 	if(addr_is_ip6(&pi->addr, pi->addrlen))
184 		((struct sockaddr_in6*)&pi->addr)->sin6_port = 0;
185 	else	((struct sockaddr_in*)&pi->addr)->sin_port = 0;
186 	if(bind(s, (struct sockaddr*)&pi->addr, pi->addrlen) != 0) {
187 #ifndef USE_WINSOCK
188 		log_err("outgoing tcp: bind: %s", strerror(errno));
189 		close(s);
190 #else
191 		log_err("outgoing tcp: bind: %s",
192 			wsa_strerror(WSAGetLastError()));
193 		closesocket(s);
194 #endif
195 		return 0;
196 	}
197 	log_addr(VERB_ALGO, "tcp bound to src", &pi->addr, pi->addrlen);
198 	return 1;
199 }
200 
201 /** get TCP file descriptor for address, returns -1 on failure,
202  * tcp_mss is 0 or maxseg size to set for TCP packets. */
203 int
204 outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen, int tcp_mss)
205 {
206 	int s;
207 #ifdef SO_REUSEADDR
208 	int on = 1;
209 #endif
210 #ifdef INET6
211 	if(addr_is_ip6(addr, addrlen))
212 		s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
213 	else
214 #endif
215 		s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
216 	if(s == -1) {
217 #ifndef USE_WINSOCK
218 		log_err_addr("outgoing tcp: socket", strerror(errno),
219 			addr, addrlen);
220 #else
221 		log_err_addr("outgoing tcp: socket",
222 			wsa_strerror(WSAGetLastError()), addr, addrlen);
223 #endif
224 		return -1;
225 	}
226 
227 #ifdef SO_REUSEADDR
228 	if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void*)&on,
229 		(socklen_t)sizeof(on)) < 0) {
230 		verbose(VERB_ALGO, "outgoing tcp:"
231 			" setsockopt(.. SO_REUSEADDR ..) failed");
232 	}
233 #endif
234 
235 	if(tcp_mss > 0) {
236 #if defined(IPPROTO_TCP) && defined(TCP_MAXSEG)
237 		if(setsockopt(s, IPPROTO_TCP, TCP_MAXSEG,
238 			(void*)&tcp_mss, (socklen_t)sizeof(tcp_mss)) < 0) {
239 			verbose(VERB_ALGO, "outgoing tcp:"
240 				" setsockopt(.. TCP_MAXSEG ..) failed");
241 		}
242 #else
243 		verbose(VERB_ALGO, "outgoing tcp:"
244 			" setsockopt(TCP_MAXSEG) unsupported");
245 #endif /* defined(IPPROTO_TCP) && defined(TCP_MAXSEG) */
246 	}
247 
248 	return s;
249 }
250 
251 /** connect tcp connection to addr, 0 on failure */
252 int
253 outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen)
254 {
255 	if(connect(s, (struct sockaddr*)addr, addrlen) == -1) {
256 #ifndef USE_WINSOCK
257 #ifdef EINPROGRESS
258 		if(errno != EINPROGRESS) {
259 #endif
260 			if(tcp_connect_errno_needs_log(
261 				(struct sockaddr*)addr, addrlen))
262 				log_err_addr("outgoing tcp: connect",
263 					strerror(errno), addr, addrlen);
264 			close(s);
265 			return 0;
266 #ifdef EINPROGRESS
267 		}
268 #endif
269 #else /* USE_WINSOCK */
270 		if(WSAGetLastError() != WSAEINPROGRESS &&
271 			WSAGetLastError() != WSAEWOULDBLOCK) {
272 			closesocket(s);
273 			return 0;
274 		}
275 #endif
276 	}
277 	return 1;
278 }
279 
280 /** use next free buffer to service a tcp query */
281 static int
282 outnet_tcp_take_into_use(struct waiting_tcp* w, uint8_t* pkt, size_t pkt_len)
283 {
284 	struct pending_tcp* pend = w->outnet->tcp_free;
285 	int s;
286 	log_assert(pend);
287 	log_assert(pkt);
288 	log_assert(w->addrlen > 0);
289 	/* open socket */
290 	s = outnet_get_tcp_fd(&w->addr, w->addrlen, w->outnet->tcp_mss);
291 
292 	if(!pick_outgoing_tcp(w, s))
293 		return 0;
294 
295 	fd_set_nonblock(s);
296 #ifdef USE_OSX_MSG_FASTOPEN
297 	/* API for fast open is different here. We use a connectx() function and
298 	   then writes can happen as normal even using SSL.*/
299 	/* connectx requires that the len be set in the sockaddr struct*/
300 	struct sockaddr_in *addr_in = (struct sockaddr_in *)&w->addr;
301 	addr_in->sin_len = w->addrlen;
302 	sa_endpoints_t endpoints;
303 	endpoints.sae_srcif = 0;
304 	endpoints.sae_srcaddr = NULL;
305 	endpoints.sae_srcaddrlen = 0;
306 	endpoints.sae_dstaddr = (struct sockaddr *)&w->addr;
307 	endpoints.sae_dstaddrlen = w->addrlen;
308 	if (connectx(s, &endpoints, SAE_ASSOCID_ANY,
309 	             CONNECT_DATA_IDEMPOTENT | CONNECT_RESUME_ON_READ_WRITE,
310 	             NULL, 0, NULL, NULL) == -1) {
311 		/* if fails, failover to connect for OSX 10.10 */
312 #ifdef EINPROGRESS
313 		if(errno != EINPROGRESS) {
314 #else
315 		if(1) {
316 #endif
317 			if(connect(s, (struct sockaddr*)&w->addr, w->addrlen) == -1) {
318 #else /* USE_OSX_MSG_FASTOPEN*/
319 #ifdef USE_MSG_FASTOPEN
320 	pend->c->tcp_do_fastopen = 1;
321 	/* Only do TFO for TCP in which case no connect() is required here.
322 	   Don't combine client TFO with SSL, since OpenSSL can't
323 	   currently support doing a handshake on fd that already isn't connected*/
324 	if (w->outnet->sslctx && w->ssl_upstream) {
325 		if(connect(s, (struct sockaddr*)&w->addr, w->addrlen) == -1) {
326 #else /* USE_MSG_FASTOPEN*/
327 	if(connect(s, (struct sockaddr*)&w->addr, w->addrlen) == -1) {
328 #endif /* USE_MSG_FASTOPEN*/
329 #endif /* USE_OSX_MSG_FASTOPEN*/
330 #ifndef USE_WINSOCK
331 #ifdef EINPROGRESS
332 		if(errno != EINPROGRESS) {
333 #else
334 		if(1) {
335 #endif
336 			if(tcp_connect_errno_needs_log(
337 				(struct sockaddr*)&w->addr, w->addrlen))
338 				log_err_addr("outgoing tcp: connect",
339 					strerror(errno), &w->addr, w->addrlen);
340 			close(s);
341 #else /* USE_WINSOCK */
342 		if(WSAGetLastError() != WSAEINPROGRESS &&
343 			WSAGetLastError() != WSAEWOULDBLOCK) {
344 			closesocket(s);
345 #endif
346 			return 0;
347 		}
348 	}
349 #ifdef USE_MSG_FASTOPEN
350 	}
351 #endif /* USE_MSG_FASTOPEN */
352 #ifdef USE_OSX_MSG_FASTOPEN
353 		}
354 	}
355 #endif /* USE_OSX_MSG_FASTOPEN */
356 	if(w->outnet->sslctx && w->ssl_upstream) {
357 		pend->c->ssl = outgoing_ssl_fd(w->outnet->sslctx, s);
358 		if(!pend->c->ssl) {
359 			pend->c->fd = s;
360 			comm_point_close(pend->c);
361 			return 0;
362 		}
363 #ifdef USE_WINSOCK
364 		comm_point_tcp_win_bio_cb(pend->c, pend->c->ssl);
365 #endif
366 		pend->c->ssl_shake_state = comm_ssl_shake_write;
367 #ifdef HAVE_SSL_SET1_HOST
368 		if(w->tls_auth_name) {
369 			SSL_set_verify(pend->c->ssl, SSL_VERIFY_PEER, NULL);
370 			/* setting the hostname makes openssl verify the
371                          * host name in the x509 certificate in the
372                          * SSL connection*/
373                         if(!SSL_set1_host(pend->c->ssl, w->tls_auth_name)) {
374                                 log_err("SSL_set1_host failed");
375 				pend->c->fd = s;
376 				comm_point_close(pend->c);
377 				return 0;
378 			}
379 		}
380 #endif /* HAVE_SSL_SET1_HOST */
381 	}
382 	w->pkt = NULL;
383 	w->next_waiting = (void*)pend;
384 	pend->id = LDNS_ID_WIRE(pkt);
385 	w->outnet->num_tcp_outgoing++;
386 	w->outnet->tcp_free = pend->next_free;
387 	pend->next_free = NULL;
388 	pend->query = w;
389 	pend->c->repinfo.addrlen = w->addrlen;
390 	memcpy(&pend->c->repinfo.addr, &w->addr, w->addrlen);
391 	sldns_buffer_clear(pend->c->buffer);
392 	sldns_buffer_write(pend->c->buffer, pkt, pkt_len);
393 	sldns_buffer_flip(pend->c->buffer);
394 	pend->c->tcp_is_reading = 0;
395 	pend->c->tcp_byte_count = 0;
396 	comm_point_start_listening(pend->c, s, -1);
397 	return 1;
398 }
399 
400 /** see if buffers can be used to service TCP queries */
401 static void
402 use_free_buffer(struct outside_network* outnet)
403 {
404 	struct waiting_tcp* w;
405 	while(outnet->tcp_free && outnet->tcp_wait_first
406 		&& !outnet->want_to_quit) {
407 		w = outnet->tcp_wait_first;
408 		outnet->tcp_wait_first = w->next_waiting;
409 		if(outnet->tcp_wait_last == w)
410 			outnet->tcp_wait_last = NULL;
411 		if(!outnet_tcp_take_into_use(w, w->pkt, w->pkt_len)) {
412 			comm_point_callback_type* cb = w->cb;
413 			void* cb_arg = w->cb_arg;
414 			waiting_tcp_delete(w);
415 			fptr_ok(fptr_whitelist_pending_tcp(cb));
416 			(void)(*cb)(NULL, cb_arg, NETEVENT_CLOSED, NULL);
417 		}
418 	}
419 }
420 
421 /** decommission a tcp buffer, closes commpoint and frees waiting_tcp entry */
422 static void
423 decommission_pending_tcp(struct outside_network* outnet,
424 	struct pending_tcp* pend)
425 {
426 	if(pend->c->ssl) {
427 #ifdef HAVE_SSL
428 		SSL_shutdown(pend->c->ssl);
429 		SSL_free(pend->c->ssl);
430 		pend->c->ssl = NULL;
431 #endif
432 	}
433 	comm_point_close(pend->c);
434 	pend->next_free = outnet->tcp_free;
435 	outnet->tcp_free = pend;
436 	waiting_tcp_delete(pend->query);
437 	pend->query = NULL;
438 	use_free_buffer(outnet);
439 }
440 
441 int
442 outnet_tcp_cb(struct comm_point* c, void* arg, int error,
443 	struct comm_reply *reply_info)
444 {
445 	struct pending_tcp* pend = (struct pending_tcp*)arg;
446 	struct outside_network* outnet = pend->query->outnet;
447 	verbose(VERB_ALGO, "outnettcp cb");
448 	if(error != NETEVENT_NOERROR) {
449 		verbose(VERB_QUERY, "outnettcp got tcp error %d", error);
450 		/* pass error below and exit */
451 	} else {
452 		/* check ID */
453 		if(sldns_buffer_limit(c->buffer) < sizeof(uint16_t) ||
454 			LDNS_ID_WIRE(sldns_buffer_begin(c->buffer))!=pend->id) {
455 			log_addr(VERB_QUERY,
456 				"outnettcp: bad ID in reply, from:",
457 				&pend->query->addr, pend->query->addrlen);
458 			error = NETEVENT_CLOSED;
459 		}
460 	}
461 	fptr_ok(fptr_whitelist_pending_tcp(pend->query->cb));
462 	(void)(*pend->query->cb)(c, pend->query->cb_arg, error, reply_info);
463 	decommission_pending_tcp(outnet, pend);
464 	return 0;
465 }
466 
467 /** lower use count on pc, see if it can be closed */
468 static void
469 portcomm_loweruse(struct outside_network* outnet, struct port_comm* pc)
470 {
471 	struct port_if* pif;
472 	pc->num_outstanding--;
473 	if(pc->num_outstanding > 0) {
474 		return;
475 	}
476 	/* close it and replace in unused list */
477 	verbose(VERB_ALGO, "close of port %d", pc->number);
478 	comm_point_close(pc->cp);
479 	pif = pc->pif;
480 	log_assert(pif->inuse > 0);
481 	pif->avail_ports[pif->avail_total - pif->inuse] = pc->number;
482 	pif->inuse--;
483 	pif->out[pc->index] = pif->out[pif->inuse];
484 	pif->out[pc->index]->index = pc->index;
485 	pc->next = outnet->unused_fds;
486 	outnet->unused_fds = pc;
487 }
488 
489 /** try to send waiting UDP queries */
490 static void
491 outnet_send_wait_udp(struct outside_network* outnet)
492 {
493 	struct pending* pend;
494 	/* process waiting queries */
495 	while(outnet->udp_wait_first && outnet->unused_fds
496 		&& !outnet->want_to_quit) {
497 		pend = outnet->udp_wait_first;
498 		outnet->udp_wait_first = pend->next_waiting;
499 		if(!pend->next_waiting) outnet->udp_wait_last = NULL;
500 		sldns_buffer_clear(outnet->udp_buff);
501 		sldns_buffer_write(outnet->udp_buff, pend->pkt, pend->pkt_len);
502 		sldns_buffer_flip(outnet->udp_buff);
503 		free(pend->pkt); /* freeing now makes get_mem correct */
504 		pend->pkt = NULL;
505 		pend->pkt_len = 0;
506 		if(!randomize_and_send_udp(pend, outnet->udp_buff,
507 			pend->timeout)) {
508 			/* callback error on pending */
509 			if(pend->cb) {
510 				fptr_ok(fptr_whitelist_pending_udp(pend->cb));
511 				(void)(*pend->cb)(outnet->unused_fds->cp, pend->cb_arg,
512 					NETEVENT_CLOSED, NULL);
513 			}
514 			pending_delete(outnet, pend);
515 		}
516 	}
517 }
518 
519 int
520 outnet_udp_cb(struct comm_point* c, void* arg, int error,
521 	struct comm_reply *reply_info)
522 {
523 	struct outside_network* outnet = (struct outside_network*)arg;
524 	struct pending key;
525 	struct pending* p;
526 	verbose(VERB_ALGO, "answer cb");
527 
528 	if(error != NETEVENT_NOERROR) {
529 		verbose(VERB_QUERY, "outnetudp got udp error %d", error);
530 		return 0;
531 	}
532 	if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
533 		verbose(VERB_QUERY, "outnetudp udp too short");
534 		return 0;
535 	}
536 	log_assert(reply_info);
537 
538 	/* setup lookup key */
539 	key.id = (unsigned)LDNS_ID_WIRE(sldns_buffer_begin(c->buffer));
540 	memcpy(&key.addr, &reply_info->addr, reply_info->addrlen);
541 	key.addrlen = reply_info->addrlen;
542 	verbose(VERB_ALGO, "Incoming reply id = %4.4x", key.id);
543 	log_addr(VERB_ALGO, "Incoming reply addr =",
544 		&reply_info->addr, reply_info->addrlen);
545 
546 	/* find it, see if this thing is a valid query response */
547 	verbose(VERB_ALGO, "lookup size is %d entries", (int)outnet->pending->count);
548 	p = (struct pending*)rbtree_search(outnet->pending, &key);
549 	if(!p) {
550 		verbose(VERB_QUERY, "received unwanted or unsolicited udp reply dropped.");
551 		log_buf(VERB_ALGO, "dropped message", c->buffer);
552 		outnet->unwanted_replies++;
553 		if(outnet->unwanted_threshold && ++outnet->unwanted_total
554 			>= outnet->unwanted_threshold) {
555 			log_warn("unwanted reply total reached threshold (%u)"
556 				" you may be under attack."
557 				" defensive action: clearing the cache",
558 				(unsigned)outnet->unwanted_threshold);
559 			fptr_ok(fptr_whitelist_alloc_cleanup(
560 				outnet->unwanted_action));
561 			(*outnet->unwanted_action)(outnet->unwanted_param);
562 			outnet->unwanted_total = 0;
563 		}
564 		return 0;
565 	}
566 
567 	verbose(VERB_ALGO, "received udp reply.");
568 	log_buf(VERB_ALGO, "udp message", c->buffer);
569 	if(p->pc->cp != c) {
570 		verbose(VERB_QUERY, "received reply id,addr on wrong port. "
571 			"dropped.");
572 		outnet->unwanted_replies++;
573 		if(outnet->unwanted_threshold && ++outnet->unwanted_total
574 			>= outnet->unwanted_threshold) {
575 			log_warn("unwanted reply total reached threshold (%u)"
576 				" you may be under attack."
577 				" defensive action: clearing the cache",
578 				(unsigned)outnet->unwanted_threshold);
579 			fptr_ok(fptr_whitelist_alloc_cleanup(
580 				outnet->unwanted_action));
581 			(*outnet->unwanted_action)(outnet->unwanted_param);
582 			outnet->unwanted_total = 0;
583 		}
584 		return 0;
585 	}
586 	comm_timer_disable(p->timer);
587 	verbose(VERB_ALGO, "outnet handle udp reply");
588 	/* delete from tree first in case callback creates a retry */
589 	(void)rbtree_delete(outnet->pending, p->node.key);
590 	if(p->cb) {
591 		fptr_ok(fptr_whitelist_pending_udp(p->cb));
592 		(void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_NOERROR, reply_info);
593 	}
594 	portcomm_loweruse(outnet, p->pc);
595 	pending_delete(NULL, p);
596 	outnet_send_wait_udp(outnet);
597 	return 0;
598 }
599 
600 /** calculate number of ip4 and ip6 interfaces*/
601 static void
602 calc_num46(char** ifs, int num_ifs, int do_ip4, int do_ip6,
603 	int* num_ip4, int* num_ip6)
604 {
605 	int i;
606 	*num_ip4 = 0;
607 	*num_ip6 = 0;
608 	if(num_ifs <= 0) {
609 		if(do_ip4)
610 			*num_ip4 = 1;
611 		if(do_ip6)
612 			*num_ip6 = 1;
613 		return;
614 	}
615 	for(i=0; i<num_ifs; i++)
616 	{
617 		if(str_is_ip6(ifs[i])) {
618 			if(do_ip6)
619 				(*num_ip6)++;
620 		} else {
621 			if(do_ip4)
622 				(*num_ip4)++;
623 		}
624 	}
625 
626 }
627 
628 void
629 pending_udp_timer_delay_cb(void* arg)
630 {
631 	struct pending* p = (struct pending*)arg;
632 	struct outside_network* outnet = p->outnet;
633 	verbose(VERB_ALGO, "timeout udp with delay");
634 	portcomm_loweruse(outnet, p->pc);
635 	pending_delete(outnet, p);
636 	outnet_send_wait_udp(outnet);
637 }
638 
639 void
640 pending_udp_timer_cb(void *arg)
641 {
642 	struct pending* p = (struct pending*)arg;
643 	struct outside_network* outnet = p->outnet;
644 	/* it timed out */
645 	verbose(VERB_ALGO, "timeout udp");
646 	if(p->cb) {
647 		fptr_ok(fptr_whitelist_pending_udp(p->cb));
648 		(void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_TIMEOUT, NULL);
649 	}
650 	/* if delayclose, keep port open for a longer time.
651 	 * But if the udpwaitlist exists, then we are struggling to
652 	 * keep up with demand for sockets, so do not wait, but service
653 	 * the customer (customer service more important than portICMPs) */
654 	if(outnet->delayclose && !outnet->udp_wait_first) {
655 		p->cb = NULL;
656 		p->timer->callback = &pending_udp_timer_delay_cb;
657 		comm_timer_set(p->timer, &outnet->delay_tv);
658 		return;
659 	}
660 	portcomm_loweruse(outnet, p->pc);
661 	pending_delete(outnet, p);
662 	outnet_send_wait_udp(outnet);
663 }
664 
665 /** create pending_tcp buffers */
666 static int
667 create_pending_tcp(struct outside_network* outnet, size_t bufsize)
668 {
669 	size_t i;
670 	if(outnet->num_tcp == 0)
671 		return 1; /* no tcp needed, nothing to do */
672 	if(!(outnet->tcp_conns = (struct pending_tcp **)calloc(
673 			outnet->num_tcp, sizeof(struct pending_tcp*))))
674 		return 0;
675 	for(i=0; i<outnet->num_tcp; i++) {
676 		if(!(outnet->tcp_conns[i] = (struct pending_tcp*)calloc(1,
677 			sizeof(struct pending_tcp))))
678 			return 0;
679 		outnet->tcp_conns[i]->next_free = outnet->tcp_free;
680 		outnet->tcp_free = outnet->tcp_conns[i];
681 		outnet->tcp_conns[i]->c = comm_point_create_tcp_out(
682 			outnet->base, bufsize, outnet_tcp_cb,
683 			outnet->tcp_conns[i]);
684 		if(!outnet->tcp_conns[i]->c)
685 			return 0;
686 	}
687 	return 1;
688 }
689 
690 /** setup an outgoing interface, ready address */
691 static int setup_if(struct port_if* pif, const char* addrstr,
692 	int* avail, int numavail, size_t numfd)
693 {
694 	pif->avail_total = numavail;
695 	pif->avail_ports = (int*)memdup(avail, (size_t)numavail*sizeof(int));
696 	if(!pif->avail_ports)
697 		return 0;
698 	if(!ipstrtoaddr(addrstr, UNBOUND_DNS_PORT, &pif->addr, &pif->addrlen) &&
699 	   !netblockstrtoaddr(addrstr, UNBOUND_DNS_PORT,
700 			      &pif->addr, &pif->addrlen, &pif->pfxlen))
701 		return 0;
702 	pif->maxout = (int)numfd;
703 	pif->inuse = 0;
704 	pif->out = (struct port_comm**)calloc(numfd,
705 		sizeof(struct port_comm*));
706 	if(!pif->out)
707 		return 0;
708 	return 1;
709 }
710 
711 struct outside_network*
712 outside_network_create(struct comm_base *base, size_t bufsize,
713 	size_t num_ports, char** ifs, int num_ifs, int do_ip4,
714 	int do_ip6, size_t num_tcp, struct infra_cache* infra,
715 	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
716 	int numavailports, size_t unwanted_threshold, int tcp_mss,
717 	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
718 	void* sslctx, int delayclose, struct dt_env* dtenv)
719 {
720 	struct outside_network* outnet = (struct outside_network*)
721 		calloc(1, sizeof(struct outside_network));
722 	size_t k;
723 	if(!outnet) {
724 		log_err("malloc failed");
725 		return NULL;
726 	}
727 	comm_base_timept(base, &outnet->now_secs, &outnet->now_tv);
728 	outnet->base = base;
729 	outnet->num_tcp = num_tcp;
730 	outnet->num_tcp_outgoing = 0;
731 	outnet->infra = infra;
732 	outnet->rnd = rnd;
733 	outnet->sslctx = sslctx;
734 #ifdef USE_DNSTAP
735 	outnet->dtenv = dtenv;
736 #else
737 	(void)dtenv;
738 #endif
739 	outnet->svcd_overhead = 0;
740 	outnet->want_to_quit = 0;
741 	outnet->unwanted_threshold = unwanted_threshold;
742 	outnet->unwanted_action = unwanted_action;
743 	outnet->unwanted_param = unwanted_param;
744 	outnet->use_caps_for_id = use_caps_for_id;
745 	outnet->do_udp = do_udp;
746 	outnet->tcp_mss = tcp_mss;
747 #ifndef S_SPLINT_S
748 	if(delayclose) {
749 		outnet->delayclose = 1;
750 		outnet->delay_tv.tv_sec = delayclose/1000;
751 		outnet->delay_tv.tv_usec = (delayclose%1000)*1000;
752 	}
753 #endif
754 	if(numavailports == 0) {
755 		log_err("no outgoing ports available");
756 		outside_network_delete(outnet);
757 		return NULL;
758 	}
759 #ifndef INET6
760 	do_ip6 = 0;
761 #endif
762 	calc_num46(ifs, num_ifs, do_ip4, do_ip6,
763 		&outnet->num_ip4, &outnet->num_ip6);
764 	if(outnet->num_ip4 != 0) {
765 		if(!(outnet->ip4_ifs = (struct port_if*)calloc(
766 			(size_t)outnet->num_ip4, sizeof(struct port_if)))) {
767 			log_err("malloc failed");
768 			outside_network_delete(outnet);
769 			return NULL;
770 		}
771 	}
772 	if(outnet->num_ip6 != 0) {
773 		if(!(outnet->ip6_ifs = (struct port_if*)calloc(
774 			(size_t)outnet->num_ip6, sizeof(struct port_if)))) {
775 			log_err("malloc failed");
776 			outside_network_delete(outnet);
777 			return NULL;
778 		}
779 	}
780 	if(	!(outnet->udp_buff = sldns_buffer_new(bufsize)) ||
781 		!(outnet->pending = rbtree_create(pending_cmp)) ||
782 		!(outnet->serviced = rbtree_create(serviced_cmp)) ||
783 		!create_pending_tcp(outnet, bufsize)) {
784 		log_err("malloc failed");
785 		outside_network_delete(outnet);
786 		return NULL;
787 	}
788 
789 	/* allocate commpoints */
790 	for(k=0; k<num_ports; k++) {
791 		struct port_comm* pc;
792 		pc = (struct port_comm*)calloc(1, sizeof(*pc));
793 		if(!pc) {
794 			log_err("malloc failed");
795 			outside_network_delete(outnet);
796 			return NULL;
797 		}
798 		pc->cp = comm_point_create_udp(outnet->base, -1,
799 			outnet->udp_buff, outnet_udp_cb, outnet);
800 		if(!pc->cp) {
801 			log_err("malloc failed");
802 			free(pc);
803 			outside_network_delete(outnet);
804 			return NULL;
805 		}
806 		pc->next = outnet->unused_fds;
807 		outnet->unused_fds = pc;
808 	}
809 
810 	/* allocate interfaces */
811 	if(num_ifs == 0) {
812 		if(do_ip4 && !setup_if(&outnet->ip4_ifs[0], "0.0.0.0",
813 			availports, numavailports, num_ports)) {
814 			log_err("malloc failed");
815 			outside_network_delete(outnet);
816 			return NULL;
817 		}
818 		if(do_ip6 && !setup_if(&outnet->ip6_ifs[0], "::",
819 			availports, numavailports, num_ports)) {
820 			log_err("malloc failed");
821 			outside_network_delete(outnet);
822 			return NULL;
823 		}
824 	} else {
825 		size_t done_4 = 0, done_6 = 0;
826 		int i;
827 		for(i=0; i<num_ifs; i++) {
828 			if(str_is_ip6(ifs[i]) && do_ip6) {
829 				if(!setup_if(&outnet->ip6_ifs[done_6], ifs[i],
830 					availports, numavailports, num_ports)){
831 					log_err("malloc failed");
832 					outside_network_delete(outnet);
833 					return NULL;
834 				}
835 				done_6++;
836 			}
837 			if(!str_is_ip6(ifs[i]) && do_ip4) {
838 				if(!setup_if(&outnet->ip4_ifs[done_4], ifs[i],
839 					availports, numavailports, num_ports)){
840 					log_err("malloc failed");
841 					outside_network_delete(outnet);
842 					return NULL;
843 				}
844 				done_4++;
845 			}
846 		}
847 	}
848 	return outnet;
849 }
850 
851 /** helper pending delete */
852 static void
853 pending_node_del(rbnode_type* node, void* arg)
854 {
855 	struct pending* pend = (struct pending*)node;
856 	struct outside_network* outnet = (struct outside_network*)arg;
857 	pending_delete(outnet, pend);
858 }
859 
860 /** helper serviced delete */
861 static void
862 serviced_node_del(rbnode_type* node, void* ATTR_UNUSED(arg))
863 {
864 	struct serviced_query* sq = (struct serviced_query*)node;
865 	struct service_callback* p = sq->cblist, *np;
866 	free(sq->qbuf);
867 	free(sq->zone);
868 	free(sq->tls_auth_name);
869 	edns_opt_list_free(sq->opt_list);
870 	while(p) {
871 		np = p->next;
872 		free(p);
873 		p = np;
874 	}
875 	free(sq);
876 }
877 
878 void
879 outside_network_quit_prepare(struct outside_network* outnet)
880 {
881 	if(!outnet)
882 		return;
883 	/* prevent queued items from being sent */
884 	outnet->want_to_quit = 1;
885 }
886 
887 void
888 outside_network_delete(struct outside_network* outnet)
889 {
890 	if(!outnet)
891 		return;
892 	outnet->want_to_quit = 1;
893 	/* check every element, since we can be called on malloc error */
894 	if(outnet->pending) {
895 		/* free pending elements, but do no unlink from tree. */
896 		traverse_postorder(outnet->pending, pending_node_del, NULL);
897 		free(outnet->pending);
898 	}
899 	if(outnet->serviced) {
900 		traverse_postorder(outnet->serviced, serviced_node_del, NULL);
901 		free(outnet->serviced);
902 	}
903 	if(outnet->udp_buff)
904 		sldns_buffer_free(outnet->udp_buff);
905 	if(outnet->unused_fds) {
906 		struct port_comm* p = outnet->unused_fds, *np;
907 		while(p) {
908 			np = p->next;
909 			comm_point_delete(p->cp);
910 			free(p);
911 			p = np;
912 		}
913 		outnet->unused_fds = NULL;
914 	}
915 	if(outnet->ip4_ifs) {
916 		int i, k;
917 		for(i=0; i<outnet->num_ip4; i++) {
918 			for(k=0; k<outnet->ip4_ifs[i].inuse; k++) {
919 				struct port_comm* pc = outnet->ip4_ifs[i].
920 					out[k];
921 				comm_point_delete(pc->cp);
922 				free(pc);
923 			}
924 			free(outnet->ip4_ifs[i].avail_ports);
925 			free(outnet->ip4_ifs[i].out);
926 		}
927 		free(outnet->ip4_ifs);
928 	}
929 	if(outnet->ip6_ifs) {
930 		int i, k;
931 		for(i=0; i<outnet->num_ip6; i++) {
932 			for(k=0; k<outnet->ip6_ifs[i].inuse; k++) {
933 				struct port_comm* pc = outnet->ip6_ifs[i].
934 					out[k];
935 				comm_point_delete(pc->cp);
936 				free(pc);
937 			}
938 			free(outnet->ip6_ifs[i].avail_ports);
939 			free(outnet->ip6_ifs[i].out);
940 		}
941 		free(outnet->ip6_ifs);
942 	}
943 	if(outnet->tcp_conns) {
944 		size_t i;
945 		for(i=0; i<outnet->num_tcp; i++)
946 			if(outnet->tcp_conns[i]) {
947 				comm_point_delete(outnet->tcp_conns[i]->c);
948 				waiting_tcp_delete(outnet->tcp_conns[i]->query);
949 				free(outnet->tcp_conns[i]);
950 			}
951 		free(outnet->tcp_conns);
952 	}
953 	if(outnet->tcp_wait_first) {
954 		struct waiting_tcp* p = outnet->tcp_wait_first, *np;
955 		while(p) {
956 			np = p->next_waiting;
957 			waiting_tcp_delete(p);
958 			p = np;
959 		}
960 	}
961 	if(outnet->udp_wait_first) {
962 		struct pending* p = outnet->udp_wait_first, *np;
963 		while(p) {
964 			np = p->next_waiting;
965 			pending_delete(NULL, p);
966 			p = np;
967 		}
968 	}
969 	free(outnet);
970 }
971 
972 void
973 pending_delete(struct outside_network* outnet, struct pending* p)
974 {
975 	if(!p)
976 		return;
977 	if(outnet && outnet->udp_wait_first &&
978 		(p->next_waiting || p == outnet->udp_wait_last) ) {
979 		/* delete from waiting list, if it is in the waiting list */
980 		struct pending* prev = NULL, *x = outnet->udp_wait_first;
981 		while(x && x != p) {
982 			prev = x;
983 			x = x->next_waiting;
984 		}
985 		if(x) {
986 			log_assert(x == p);
987 			if(prev)
988 				prev->next_waiting = p->next_waiting;
989 			else	outnet->udp_wait_first = p->next_waiting;
990 			if(outnet->udp_wait_last == p)
991 				outnet->udp_wait_last = prev;
992 		}
993 	}
994 	if(outnet) {
995 		(void)rbtree_delete(outnet->pending, p->node.key);
996 	}
997 	if(p->timer)
998 		comm_timer_delete(p->timer);
999 	free(p->pkt);
1000 	free(p);
1001 }
1002 
1003 static void
1004 sai6_putrandom(struct sockaddr_in6 *sa, int pfxlen, struct ub_randstate *rnd)
1005 {
1006 	int i, last;
1007 	if(!(pfxlen > 0 && pfxlen < 128))
1008 		return;
1009 	for(i = 0; i < (128 - pfxlen) / 8; i++) {
1010 		sa->sin6_addr.s6_addr[15-i] = (uint8_t)ub_random_max(rnd, 256);
1011 	}
1012 	last = pfxlen & 7;
1013 	if(last != 0) {
1014 		sa->sin6_addr.s6_addr[15-i] |=
1015 			((0xFF >> last) & ub_random_max(rnd, 256));
1016 	}
1017 }
1018 
1019 /**
1020  * Try to open a UDP socket for outgoing communication.
1021  * Sets sockets options as needed.
1022  * @param addr: socket address.
1023  * @param addrlen: length of address.
1024  * @param pfxlen: length of network prefix (for address randomisation).
1025  * @param port: port override for addr.
1026  * @param inuse: if -1 is returned, this bool means the port was in use.
1027  * @param rnd: random state (for address randomisation).
1028  * @return fd or -1
1029  */
1030 static int
1031 udp_sockport(struct sockaddr_storage* addr, socklen_t addrlen, int pfxlen,
1032 	int port, int* inuse, struct ub_randstate* rnd)
1033 {
1034 	int fd, noproto;
1035 	if(addr_is_ip6(addr, addrlen)) {
1036 		int freebind = 0;
1037 		struct sockaddr_in6 sa = *(struct sockaddr_in6*)addr;
1038 		sa.sin6_port = (in_port_t)htons((uint16_t)port);
1039 		if(pfxlen != 0) {
1040 			freebind = 1;
1041 			sai6_putrandom(&sa, pfxlen, rnd);
1042 		}
1043 		fd = create_udp_sock(AF_INET6, SOCK_DGRAM,
1044 			(struct sockaddr*)&sa, addrlen, 1, inuse, &noproto,
1045 			0, 0, 0, NULL, 0, freebind, 0);
1046 	} else {
1047 		struct sockaddr_in* sa = (struct sockaddr_in*)addr;
1048 		sa->sin_port = (in_port_t)htons((uint16_t)port);
1049 		fd = create_udp_sock(AF_INET, SOCK_DGRAM,
1050 			(struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
1051 			0, 0, 0, NULL, 0, 0, 0);
1052 	}
1053 	return fd;
1054 }
1055 
1056 /** Select random ID */
1057 static int
1058 select_id(struct outside_network* outnet, struct pending* pend,
1059 	sldns_buffer* packet)
1060 {
1061 	int id_tries = 0;
1062 	pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
1063 	LDNS_ID_SET(sldns_buffer_begin(packet), pend->id);
1064 
1065 	/* insert in tree */
1066 	pend->node.key = pend;
1067 	while(!rbtree_insert(outnet->pending, &pend->node)) {
1068 		/* change ID to avoid collision */
1069 		pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
1070 		LDNS_ID_SET(sldns_buffer_begin(packet), pend->id);
1071 		id_tries++;
1072 		if(id_tries == MAX_ID_RETRY) {
1073 			pend->id=99999; /* non existant ID */
1074 			log_err("failed to generate unique ID, drop msg");
1075 			return 0;
1076 		}
1077 	}
1078 	verbose(VERB_ALGO, "inserted new pending reply id=%4.4x", pend->id);
1079 	return 1;
1080 }
1081 
1082 /** Select random interface and port */
1083 static int
1084 select_ifport(struct outside_network* outnet, struct pending* pend,
1085 	int num_if, struct port_if* ifs)
1086 {
1087 	int my_if, my_port, fd, portno, inuse, tries=0;
1088 	struct port_if* pif;
1089 	/* randomly select interface and port */
1090 	if(num_if == 0) {
1091 		verbose(VERB_QUERY, "Need to send query but have no "
1092 			"outgoing interfaces of that family");
1093 		return 0;
1094 	}
1095 	log_assert(outnet->unused_fds);
1096 	tries = 0;
1097 	while(1) {
1098 		my_if = ub_random_max(outnet->rnd, num_if);
1099 		pif = &ifs[my_if];
1100 		my_port = ub_random_max(outnet->rnd, pif->avail_total);
1101 		if(my_port < pif->inuse) {
1102 			/* port already open */
1103 			pend->pc = pif->out[my_port];
1104 			verbose(VERB_ALGO, "using UDP if=%d port=%d",
1105 				my_if, pend->pc->number);
1106 			break;
1107 		}
1108 		/* try to open new port, if fails, loop to try again */
1109 		log_assert(pif->inuse < pif->maxout);
1110 		portno = pif->avail_ports[my_port - pif->inuse];
1111 		fd = udp_sockport(&pif->addr, pif->addrlen, pif->pfxlen,
1112 			portno, &inuse, outnet->rnd);
1113 		if(fd == -1 && !inuse) {
1114 			/* nonrecoverable error making socket */
1115 			return 0;
1116 		}
1117 		if(fd != -1) {
1118 			verbose(VERB_ALGO, "opened UDP if=%d port=%d",
1119 				my_if, portno);
1120 			/* grab fd */
1121 			pend->pc = outnet->unused_fds;
1122 			outnet->unused_fds = pend->pc->next;
1123 
1124 			/* setup portcomm */
1125 			pend->pc->next = NULL;
1126 			pend->pc->number = portno;
1127 			pend->pc->pif = pif;
1128 			pend->pc->index = pif->inuse;
1129 			pend->pc->num_outstanding = 0;
1130 			comm_point_start_listening(pend->pc->cp, fd, -1);
1131 
1132 			/* grab port in interface */
1133 			pif->out[pif->inuse] = pend->pc;
1134 			pif->avail_ports[my_port - pif->inuse] =
1135 				pif->avail_ports[pif->avail_total-pif->inuse-1];
1136 			pif->inuse++;
1137 			break;
1138 		}
1139 		/* failed, already in use */
1140 		verbose(VERB_QUERY, "port %d in use, trying another", portno);
1141 		tries++;
1142 		if(tries == MAX_PORT_RETRY) {
1143 			log_err("failed to find an open port, drop msg");
1144 			return 0;
1145 		}
1146 	}
1147 	log_assert(pend->pc);
1148 	pend->pc->num_outstanding++;
1149 
1150 	return 1;
1151 }
1152 
1153 static int
1154 randomize_and_send_udp(struct pending* pend, sldns_buffer* packet, int timeout)
1155 {
1156 	struct timeval tv;
1157 	struct outside_network* outnet = pend->sq->outnet;
1158 
1159 	/* select id */
1160 	if(!select_id(outnet, pend, packet)) {
1161 		return 0;
1162 	}
1163 
1164 	/* select src_if, port */
1165 	if(addr_is_ip6(&pend->addr, pend->addrlen)) {
1166 		if(!select_ifport(outnet, pend,
1167 			outnet->num_ip6, outnet->ip6_ifs))
1168 			return 0;
1169 	} else {
1170 		if(!select_ifport(outnet, pend,
1171 			outnet->num_ip4, outnet->ip4_ifs))
1172 			return 0;
1173 	}
1174 	log_assert(pend->pc && pend->pc->cp);
1175 
1176 	/* send it over the commlink */
1177 	if(!comm_point_send_udp_msg(pend->pc->cp, packet,
1178 		(struct sockaddr*)&pend->addr, pend->addrlen)) {
1179 		portcomm_loweruse(outnet, pend->pc);
1180 		return 0;
1181 	}
1182 
1183 	/* system calls to set timeout after sending UDP to make roundtrip
1184 	   smaller. */
1185 #ifndef S_SPLINT_S
1186 	tv.tv_sec = timeout/1000;
1187 	tv.tv_usec = (timeout%1000)*1000;
1188 #endif
1189 	comm_timer_set(pend->timer, &tv);
1190 
1191 #ifdef USE_DNSTAP
1192 	if(outnet->dtenv &&
1193 	   (outnet->dtenv->log_resolver_query_messages ||
1194 	    outnet->dtenv->log_forwarder_query_messages))
1195 		dt_msg_send_outside_query(outnet->dtenv, &pend->addr, comm_udp,
1196 		pend->sq->zone, pend->sq->zonelen, packet);
1197 #endif
1198 	return 1;
1199 }
1200 
1201 struct pending*
1202 pending_udp_query(struct serviced_query* sq, struct sldns_buffer* packet,
1203 	int timeout, comm_point_callback_type* cb, void* cb_arg)
1204 {
1205 	struct pending* pend = (struct pending*)calloc(1, sizeof(*pend));
1206 	if(!pend) return NULL;
1207 	pend->outnet = sq->outnet;
1208 	pend->sq = sq;
1209 	pend->addrlen = sq->addrlen;
1210 	memmove(&pend->addr, &sq->addr, sq->addrlen);
1211 	pend->cb = cb;
1212 	pend->cb_arg = cb_arg;
1213 	pend->node.key = pend;
1214 	pend->timer = comm_timer_create(sq->outnet->base, pending_udp_timer_cb,
1215 		pend);
1216 	if(!pend->timer) {
1217 		free(pend);
1218 		return NULL;
1219 	}
1220 
1221 	if(sq->outnet->unused_fds == NULL) {
1222 		/* no unused fd, cannot create a new port (randomly) */
1223 		verbose(VERB_ALGO, "no fds available, udp query waiting");
1224 		pend->timeout = timeout;
1225 		pend->pkt_len = sldns_buffer_limit(packet);
1226 		pend->pkt = (uint8_t*)memdup(sldns_buffer_begin(packet),
1227 			pend->pkt_len);
1228 		if(!pend->pkt) {
1229 			comm_timer_delete(pend->timer);
1230 			free(pend);
1231 			return NULL;
1232 		}
1233 		/* put at end of waiting list */
1234 		if(sq->outnet->udp_wait_last)
1235 			sq->outnet->udp_wait_last->next_waiting = pend;
1236 		else
1237 			sq->outnet->udp_wait_first = pend;
1238 		sq->outnet->udp_wait_last = pend;
1239 		return pend;
1240 	}
1241 	if(!randomize_and_send_udp(pend, packet, timeout)) {
1242 		pending_delete(sq->outnet, pend);
1243 		return NULL;
1244 	}
1245 	return pend;
1246 }
1247 
1248 void
1249 outnet_tcptimer(void* arg)
1250 {
1251 	struct waiting_tcp* w = (struct waiting_tcp*)arg;
1252 	struct outside_network* outnet = w->outnet;
1253 	comm_point_callback_type* cb;
1254 	void* cb_arg;
1255 	if(w->pkt) {
1256 		/* it is on the waiting list */
1257 		waiting_list_remove(outnet, w);
1258 	} else {
1259 		/* it was in use */
1260 		struct pending_tcp* pend=(struct pending_tcp*)w->next_waiting;
1261 		comm_point_close(pend->c);
1262 		pend->query = NULL;
1263 		pend->next_free = outnet->tcp_free;
1264 		outnet->tcp_free = pend;
1265 	}
1266 	cb = w->cb;
1267 	cb_arg = w->cb_arg;
1268 	waiting_tcp_delete(w);
1269 	fptr_ok(fptr_whitelist_pending_tcp(cb));
1270 	(void)(*cb)(NULL, cb_arg, NETEVENT_TIMEOUT, NULL);
1271 	use_free_buffer(outnet);
1272 }
1273 
1274 struct waiting_tcp*
1275 pending_tcp_query(struct serviced_query* sq, sldns_buffer* packet,
1276 	int timeout, comm_point_callback_type* callback, void* callback_arg)
1277 {
1278 	struct pending_tcp* pend = sq->outnet->tcp_free;
1279 	struct waiting_tcp* w;
1280 	struct timeval tv;
1281 	uint16_t id;
1282 	/* if no buffer is free allocate space to store query */
1283 	w = (struct waiting_tcp*)malloc(sizeof(struct waiting_tcp)
1284 		+ (pend?0:sldns_buffer_limit(packet)));
1285 	if(!w) {
1286 		return NULL;
1287 	}
1288 	if(!(w->timer = comm_timer_create(sq->outnet->base, outnet_tcptimer, w))) {
1289 		free(w);
1290 		return NULL;
1291 	}
1292 	w->pkt = NULL;
1293 	w->pkt_len = 0;
1294 	id = ((unsigned)ub_random(sq->outnet->rnd)>>8) & 0xffff;
1295 	LDNS_ID_SET(sldns_buffer_begin(packet), id);
1296 	memcpy(&w->addr, &sq->addr, sq->addrlen);
1297 	w->addrlen = sq->addrlen;
1298 	w->outnet = sq->outnet;
1299 	w->cb = callback;
1300 	w->cb_arg = callback_arg;
1301 	w->ssl_upstream = sq->ssl_upstream;
1302 	w->tls_auth_name = sq->tls_auth_name;
1303 #ifndef S_SPLINT_S
1304 	tv.tv_sec = timeout/1000;
1305 	tv.tv_usec = (timeout%1000)*1000;
1306 #endif
1307 	comm_timer_set(w->timer, &tv);
1308 	if(pend) {
1309 		/* we have a buffer available right now */
1310 		if(!outnet_tcp_take_into_use(w, sldns_buffer_begin(packet),
1311 			sldns_buffer_limit(packet))) {
1312 			waiting_tcp_delete(w);
1313 			return NULL;
1314 		}
1315 #ifdef USE_DNSTAP
1316 		if(sq->outnet->dtenv &&
1317 		   (sq->outnet->dtenv->log_resolver_query_messages ||
1318 		    sq->outnet->dtenv->log_forwarder_query_messages))
1319 		dt_msg_send_outside_query(sq->outnet->dtenv, &sq->addr,
1320 		comm_tcp, sq->zone, sq->zonelen, packet);
1321 #endif
1322 	} else {
1323 		/* queue up */
1324 		w->pkt = (uint8_t*)w + sizeof(struct waiting_tcp);
1325 		w->pkt_len = sldns_buffer_limit(packet);
1326 		memmove(w->pkt, sldns_buffer_begin(packet), w->pkt_len);
1327 		w->next_waiting = NULL;
1328 		if(sq->outnet->tcp_wait_last)
1329 			sq->outnet->tcp_wait_last->next_waiting = w;
1330 		else	sq->outnet->tcp_wait_first = w;
1331 		sq->outnet->tcp_wait_last = w;
1332 	}
1333 	return w;
1334 }
1335 
1336 /** create query for serviced queries */
1337 static void
1338 serviced_gen_query(sldns_buffer* buff, uint8_t* qname, size_t qnamelen,
1339 	uint16_t qtype, uint16_t qclass, uint16_t flags)
1340 {
1341 	sldns_buffer_clear(buff);
1342 	/* skip id */
1343 	sldns_buffer_write_u16(buff, flags);
1344 	sldns_buffer_write_u16(buff, 1); /* qdcount */
1345 	sldns_buffer_write_u16(buff, 0); /* ancount */
1346 	sldns_buffer_write_u16(buff, 0); /* nscount */
1347 	sldns_buffer_write_u16(buff, 0); /* arcount */
1348 	sldns_buffer_write(buff, qname, qnamelen);
1349 	sldns_buffer_write_u16(buff, qtype);
1350 	sldns_buffer_write_u16(buff, qclass);
1351 	sldns_buffer_flip(buff);
1352 }
1353 
1354 /** lookup serviced query in serviced query rbtree */
1355 static struct serviced_query*
1356 lookup_serviced(struct outside_network* outnet, sldns_buffer* buff, int dnssec,
1357 	struct sockaddr_storage* addr, socklen_t addrlen,
1358 	struct edns_option* opt_list)
1359 {
1360 	struct serviced_query key;
1361 	key.node.key = &key;
1362 	key.qbuf = sldns_buffer_begin(buff);
1363 	key.qbuflen = sldns_buffer_limit(buff);
1364 	key.dnssec = dnssec;
1365 	memcpy(&key.addr, addr, addrlen);
1366 	key.addrlen = addrlen;
1367 	key.outnet = outnet;
1368 	key.opt_list = opt_list;
1369 	return (struct serviced_query*)rbtree_search(outnet->serviced, &key);
1370 }
1371 
1372 /** Create new serviced entry */
1373 static struct serviced_query*
1374 serviced_create(struct outside_network* outnet, sldns_buffer* buff, int dnssec,
1375 	int want_dnssec, int nocaps, int tcp_upstream, int ssl_upstream,
1376 	char* tls_auth_name, struct sockaddr_storage* addr, socklen_t addrlen,
1377 	uint8_t* zone, size_t zonelen, int qtype, struct edns_option* opt_list)
1378 {
1379 	struct serviced_query* sq = (struct serviced_query*)malloc(sizeof(*sq));
1380 #ifdef UNBOUND_DEBUG
1381 	rbnode_type* ins;
1382 #endif
1383 	if(!sq)
1384 		return NULL;
1385 	sq->node.key = sq;
1386 	sq->qbuf = memdup(sldns_buffer_begin(buff), sldns_buffer_limit(buff));
1387 	if(!sq->qbuf) {
1388 		free(sq);
1389 		return NULL;
1390 	}
1391 	sq->qbuflen = sldns_buffer_limit(buff);
1392 	sq->zone = memdup(zone, zonelen);
1393 	if(!sq->zone) {
1394 		free(sq->qbuf);
1395 		free(sq);
1396 		return NULL;
1397 	}
1398 	sq->zonelen = zonelen;
1399 	sq->qtype = qtype;
1400 	sq->dnssec = dnssec;
1401 	sq->want_dnssec = want_dnssec;
1402 	sq->nocaps = nocaps;
1403 	sq->tcp_upstream = tcp_upstream;
1404 	sq->ssl_upstream = ssl_upstream;
1405 	if(tls_auth_name) {
1406 		sq->tls_auth_name = strdup(tls_auth_name);
1407 		if(!sq->tls_auth_name) {
1408 			free(sq->zone);
1409 			free(sq->qbuf);
1410 			free(sq);
1411 			return NULL;
1412 		}
1413 	} else {
1414 		sq->tls_auth_name = NULL;
1415 	}
1416 	memcpy(&sq->addr, addr, addrlen);
1417 	sq->addrlen = addrlen;
1418 	sq->opt_list = NULL;
1419 	if(opt_list) {
1420 		sq->opt_list = edns_opt_copy_alloc(opt_list);
1421 		if(!sq->opt_list) {
1422 			free(sq->tls_auth_name);
1423 			free(sq->zone);
1424 			free(sq->qbuf);
1425 			free(sq);
1426 			return NULL;
1427 		}
1428 	}
1429 	sq->outnet = outnet;
1430 	sq->cblist = NULL;
1431 	sq->pending = NULL;
1432 	sq->status = serviced_initial;
1433 	sq->retry = 0;
1434 	sq->to_be_deleted = 0;
1435 #ifdef UNBOUND_DEBUG
1436 	ins =
1437 #else
1438 	(void)
1439 #endif
1440 	rbtree_insert(outnet->serviced, &sq->node);
1441 	log_assert(ins != NULL); /* must not be already present */
1442 	return sq;
1443 }
1444 
1445 /** remove waiting tcp from the outnet waiting list */
1446 static void
1447 waiting_list_remove(struct outside_network* outnet, struct waiting_tcp* w)
1448 {
1449 	struct waiting_tcp* p = outnet->tcp_wait_first, *prev = NULL;
1450 	while(p) {
1451 		if(p == w) {
1452 			/* remove w */
1453 			if(prev)
1454 				prev->next_waiting = w->next_waiting;
1455 			else	outnet->tcp_wait_first = w->next_waiting;
1456 			if(outnet->tcp_wait_last == w)
1457 				outnet->tcp_wait_last = prev;
1458 			return;
1459 		}
1460 		prev = p;
1461 		p = p->next_waiting;
1462 	}
1463 }
1464 
1465 /** cleanup serviced query entry */
1466 static void
1467 serviced_delete(struct serviced_query* sq)
1468 {
1469 	if(sq->pending) {
1470 		/* clear up the pending query */
1471 		if(sq->status == serviced_query_UDP_EDNS ||
1472 			sq->status == serviced_query_UDP ||
1473 			sq->status == serviced_query_PROBE_EDNS ||
1474 			sq->status == serviced_query_UDP_EDNS_FRAG ||
1475 			sq->status == serviced_query_UDP_EDNS_fallback) {
1476 			struct pending* p = (struct pending*)sq->pending;
1477 			if(p->pc)
1478 				portcomm_loweruse(sq->outnet, p->pc);
1479 			pending_delete(sq->outnet, p);
1480 			/* this call can cause reentrant calls back into the
1481 			 * mesh */
1482 			outnet_send_wait_udp(sq->outnet);
1483 		} else {
1484 			struct waiting_tcp* p = (struct waiting_tcp*)
1485 				sq->pending;
1486 			if(p->pkt == NULL) {
1487 				decommission_pending_tcp(sq->outnet,
1488 					(struct pending_tcp*)p->next_waiting);
1489 			} else {
1490 				waiting_list_remove(sq->outnet, p);
1491 				waiting_tcp_delete(p);
1492 			}
1493 		}
1494 	}
1495 	/* does not delete from tree, caller has to do that */
1496 	serviced_node_del(&sq->node, NULL);
1497 }
1498 
1499 /** perturb a dname capitalization randomly */
1500 static void
1501 serviced_perturb_qname(struct ub_randstate* rnd, uint8_t* qbuf, size_t len)
1502 {
1503 	uint8_t lablen;
1504 	uint8_t* d = qbuf + 10;
1505 	long int random = 0;
1506 	int bits = 0;
1507 	log_assert(len >= 10 + 5 /* offset qname, root, qtype, qclass */);
1508 	(void)len;
1509 	lablen = *d++;
1510 	while(lablen) {
1511 		while(lablen--) {
1512 			/* only perturb A-Z, a-z */
1513 			if(isalpha((unsigned char)*d)) {
1514 				/* get a random bit */
1515 				if(bits == 0) {
1516 					random = ub_random(rnd);
1517 					bits = 30;
1518 				}
1519 				if(random & 0x1) {
1520 					*d = (uint8_t)toupper((unsigned char)*d);
1521 				} else {
1522 					*d = (uint8_t)tolower((unsigned char)*d);
1523 				}
1524 				random >>= 1;
1525 				bits--;
1526 			}
1527 			d++;
1528 		}
1529 		lablen = *d++;
1530 	}
1531 	if(verbosity >= VERB_ALGO) {
1532 		char buf[LDNS_MAX_DOMAINLEN+1];
1533 		dname_str(qbuf+10, buf);
1534 		verbose(VERB_ALGO, "qname perturbed to %s", buf);
1535 	}
1536 }
1537 
1538 /** put serviced query into a buffer */
1539 static void
1540 serviced_encode(struct serviced_query* sq, sldns_buffer* buff, int with_edns)
1541 {
1542 	/* if we are using 0x20 bits for ID randomness, perturb them */
1543 	if(sq->outnet->use_caps_for_id && !sq->nocaps) {
1544 		serviced_perturb_qname(sq->outnet->rnd, sq->qbuf, sq->qbuflen);
1545 	}
1546 	/* generate query */
1547 	sldns_buffer_clear(buff);
1548 	sldns_buffer_write_u16(buff, 0); /* id placeholder */
1549 	sldns_buffer_write(buff, sq->qbuf, sq->qbuflen);
1550 	sldns_buffer_flip(buff);
1551 	if(with_edns) {
1552 		/* add edns section */
1553 		struct edns_data edns;
1554 		edns.edns_present = 1;
1555 		edns.ext_rcode = 0;
1556 		edns.edns_version = EDNS_ADVERTISED_VERSION;
1557 		edns.opt_list = sq->opt_list;
1558 		if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1559 			if(addr_is_ip6(&sq->addr, sq->addrlen)) {
1560 				if(EDNS_FRAG_SIZE_IP6 < EDNS_ADVERTISED_SIZE)
1561 					edns.udp_size = EDNS_FRAG_SIZE_IP6;
1562 				else	edns.udp_size = EDNS_ADVERTISED_SIZE;
1563 			} else {
1564 				if(EDNS_FRAG_SIZE_IP4 < EDNS_ADVERTISED_SIZE)
1565 					edns.udp_size = EDNS_FRAG_SIZE_IP4;
1566 				else	edns.udp_size = EDNS_ADVERTISED_SIZE;
1567 			}
1568 		} else {
1569 			edns.udp_size = EDNS_ADVERTISED_SIZE;
1570 		}
1571 		edns.bits = 0;
1572 		if(sq->dnssec & EDNS_DO)
1573 			edns.bits = EDNS_DO;
1574 		if(sq->dnssec & BIT_CD)
1575 			LDNS_CD_SET(sldns_buffer_begin(buff));
1576 		attach_edns_record(buff, &edns);
1577 	}
1578 }
1579 
1580 /**
1581  * Perform serviced query UDP sending operation.
1582  * Sends UDP with EDNS, unless infra host marked non EDNS.
1583  * @param sq: query to send.
1584  * @param buff: buffer scratch space.
1585  * @return 0 on error.
1586  */
1587 static int
1588 serviced_udp_send(struct serviced_query* sq, sldns_buffer* buff)
1589 {
1590 	int rtt, vs;
1591 	uint8_t edns_lame_known;
1592 	time_t now = *sq->outnet->now_secs;
1593 
1594 	if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1595 		sq->zonelen, now, &vs, &edns_lame_known, &rtt))
1596 		return 0;
1597 	sq->last_rtt = rtt;
1598 	verbose(VERB_ALGO, "EDNS lookup known=%d vs=%d", edns_lame_known, vs);
1599 	if(sq->status == serviced_initial) {
1600 		if(edns_lame_known == 0 && rtt > 5000 && rtt < 10001) {
1601 			/* perform EDNS lame probe - check if server is
1602 			 * EDNS lame (EDNS queries to it are dropped) */
1603 			verbose(VERB_ALGO, "serviced query: send probe to see "
1604 				" if use of EDNS causes timeouts");
1605 			/* even 700 msec may be too small */
1606 			rtt = 1000;
1607 			sq->status = serviced_query_PROBE_EDNS;
1608 		} else if(vs != -1) {
1609 			sq->status = serviced_query_UDP_EDNS;
1610 		} else {
1611 			sq->status = serviced_query_UDP;
1612 		}
1613 	}
1614 	serviced_encode(sq, buff, (sq->status == serviced_query_UDP_EDNS) ||
1615 		(sq->status == serviced_query_UDP_EDNS_FRAG));
1616 	sq->last_sent_time = *sq->outnet->now_tv;
1617 	sq->edns_lame_known = (int)edns_lame_known;
1618 	verbose(VERB_ALGO, "serviced query UDP timeout=%d msec", rtt);
1619 	sq->pending = pending_udp_query(sq, buff, rtt,
1620 		serviced_udp_callback, sq);
1621 	if(!sq->pending)
1622 		return 0;
1623 	return 1;
1624 }
1625 
1626 /** check that perturbed qname is identical */
1627 static int
1628 serviced_check_qname(sldns_buffer* pkt, uint8_t* qbuf, size_t qbuflen)
1629 {
1630 	uint8_t* d1 = sldns_buffer_begin(pkt)+12;
1631 	uint8_t* d2 = qbuf+10;
1632 	uint8_t len1, len2;
1633 	int count = 0;
1634 	if(sldns_buffer_limit(pkt) < 12+1+4) /* packet too small for qname */
1635 		return 0;
1636 	log_assert(qbuflen >= 15 /* 10 header, root, type, class */);
1637 	len1 = *d1++;
1638 	len2 = *d2++;
1639 	while(len1 != 0 || len2 != 0) {
1640 		if(LABEL_IS_PTR(len1)) {
1641 			/* check if we can read *d1 with compression ptr rest */
1642 			if(d1 >= sldns_buffer_at(pkt, sldns_buffer_limit(pkt)))
1643 				return 0;
1644 			d1 = sldns_buffer_begin(pkt)+PTR_OFFSET(len1, *d1);
1645 			/* check if we can read the destination *d1 */
1646 			if(d1 >= sldns_buffer_at(pkt, sldns_buffer_limit(pkt)))
1647 				return 0;
1648 			len1 = *d1++;
1649 			if(count++ > MAX_COMPRESS_PTRS)
1650 				return 0;
1651 			continue;
1652 		}
1653 		if(d2 > qbuf+qbuflen)
1654 			return 0;
1655 		if(len1 != len2)
1656 			return 0;
1657 		if(len1 > LDNS_MAX_LABELLEN)
1658 			return 0;
1659 		/* check len1 + 1(next length) are okay to read */
1660 		if(d1+len1 >= sldns_buffer_at(pkt, sldns_buffer_limit(pkt)))
1661 			return 0;
1662 		log_assert(len1 <= LDNS_MAX_LABELLEN);
1663 		log_assert(len2 <= LDNS_MAX_LABELLEN);
1664 		log_assert(len1 == len2 && len1 != 0);
1665 		/* compare the labels - bitwise identical */
1666 		if(memcmp(d1, d2, len1) != 0)
1667 			return 0;
1668 		d1 += len1;
1669 		d2 += len2;
1670 		len1 = *d1++;
1671 		len2 = *d2++;
1672 	}
1673 	return 1;
1674 }
1675 
1676 /** call the callbacks for a serviced query */
1677 static void
1678 serviced_callbacks(struct serviced_query* sq, int error, struct comm_point* c,
1679 	struct comm_reply* rep)
1680 {
1681 	struct service_callback* p;
1682 	int dobackup = (sq->cblist && sq->cblist->next); /* >1 cb*/
1683 	uint8_t *backup_p = NULL;
1684 	size_t backlen = 0;
1685 #ifdef UNBOUND_DEBUG
1686 	rbnode_type* rem =
1687 #else
1688 	(void)
1689 #endif
1690 	/* remove from tree, and schedule for deletion, so that callbacks
1691 	 * can safely deregister themselves and even create new serviced
1692 	 * queries that are identical to this one. */
1693 	rbtree_delete(sq->outnet->serviced, sq);
1694 	log_assert(rem); /* should have been present */
1695 	sq->to_be_deleted = 1;
1696 	verbose(VERB_ALGO, "svcd callbacks start");
1697 	if(sq->outnet->use_caps_for_id && error == NETEVENT_NOERROR && c &&
1698 		!sq->nocaps && sq->qtype != LDNS_RR_TYPE_PTR) {
1699 		/* for type PTR do not check perturbed name in answer,
1700 		 * compatibility with cisco dns guard boxes that mess up
1701 		 * reverse queries 0x20 contents */
1702 		/* noerror and nxdomain must have a qname in reply */
1703 		if(sldns_buffer_read_u16_at(c->buffer, 4) == 0 &&
1704 			(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1705 				== LDNS_RCODE_NOERROR ||
1706 			 LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1707 				== LDNS_RCODE_NXDOMAIN)) {
1708 			verbose(VERB_DETAIL, "no qname in reply to check 0x20ID");
1709 			log_addr(VERB_DETAIL, "from server",
1710 				&sq->addr, sq->addrlen);
1711 			log_buf(VERB_DETAIL, "for packet", c->buffer);
1712 			error = NETEVENT_CLOSED;
1713 			c = NULL;
1714 		} else if(sldns_buffer_read_u16_at(c->buffer, 4) > 0 &&
1715 			!serviced_check_qname(c->buffer, sq->qbuf,
1716 			sq->qbuflen)) {
1717 			verbose(VERB_DETAIL, "wrong 0x20-ID in reply qname");
1718 			log_addr(VERB_DETAIL, "from server",
1719 				&sq->addr, sq->addrlen);
1720 			log_buf(VERB_DETAIL, "for packet", c->buffer);
1721 			error = NETEVENT_CAPSFAIL;
1722 			/* and cleanup too */
1723 			pkt_dname_tolower(c->buffer,
1724 				sldns_buffer_at(c->buffer, 12));
1725 		} else {
1726 			verbose(VERB_ALGO, "good 0x20-ID in reply qname");
1727 			/* cleanup caps, prettier cache contents. */
1728 			pkt_dname_tolower(c->buffer,
1729 				sldns_buffer_at(c->buffer, 12));
1730 		}
1731 	}
1732 	if(dobackup && c) {
1733 		/* make a backup of the query, since the querystate processing
1734 		 * may send outgoing queries that overwrite the buffer.
1735 		 * use secondary buffer to store the query.
1736 		 * This is a data copy, but faster than packet to server */
1737 		backlen = sldns_buffer_limit(c->buffer);
1738 		backup_p = memdup(sldns_buffer_begin(c->buffer), backlen);
1739 		if(!backup_p) {
1740 			log_err("malloc failure in serviced query callbacks");
1741 			error = NETEVENT_CLOSED;
1742 			c = NULL;
1743 		}
1744 		sq->outnet->svcd_overhead = backlen;
1745 	}
1746 	/* test the actual sq->cblist, because the next elem could be deleted*/
1747 	while((p=sq->cblist) != NULL) {
1748 		sq->cblist = p->next; /* remove this element */
1749 		if(dobackup && c) {
1750 			sldns_buffer_clear(c->buffer);
1751 			sldns_buffer_write(c->buffer, backup_p, backlen);
1752 			sldns_buffer_flip(c->buffer);
1753 		}
1754 		fptr_ok(fptr_whitelist_serviced_query(p->cb));
1755 		(void)(*p->cb)(c, p->cb_arg, error, rep);
1756 		free(p);
1757 	}
1758 	if(backup_p) {
1759 		free(backup_p);
1760 		sq->outnet->svcd_overhead = 0;
1761 	}
1762 	verbose(VERB_ALGO, "svcd callbacks end");
1763 	log_assert(sq->cblist == NULL);
1764 	serviced_delete(sq);
1765 }
1766 
1767 int
1768 serviced_tcp_callback(struct comm_point* c, void* arg, int error,
1769         struct comm_reply* rep)
1770 {
1771 	struct serviced_query* sq = (struct serviced_query*)arg;
1772 	struct comm_reply r2;
1773 	sq->pending = NULL; /* removed after this callback */
1774 	if(error != NETEVENT_NOERROR)
1775 		log_addr(VERB_QUERY, "tcp error for address",
1776 			&sq->addr, sq->addrlen);
1777 	if(error==NETEVENT_NOERROR)
1778 		infra_update_tcp_works(sq->outnet->infra, &sq->addr,
1779 			sq->addrlen, sq->zone, sq->zonelen);
1780 #ifdef USE_DNSTAP
1781 	if(error==NETEVENT_NOERROR && sq->outnet->dtenv &&
1782 	   (sq->outnet->dtenv->log_resolver_response_messages ||
1783 	    sq->outnet->dtenv->log_forwarder_response_messages))
1784 		dt_msg_send_outside_response(sq->outnet->dtenv, &sq->addr,
1785 		c->type, sq->zone, sq->zonelen, sq->qbuf, sq->qbuflen,
1786 		&sq->last_sent_time, sq->outnet->now_tv, c->buffer);
1787 #endif
1788 	if(error==NETEVENT_NOERROR && sq->status == serviced_query_TCP_EDNS &&
1789 		(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1790 		LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(sldns_buffer_begin(
1791 		c->buffer)) == LDNS_RCODE_NOTIMPL) ) {
1792 		/* attempt to fallback to nonEDNS */
1793 		sq->status = serviced_query_TCP_EDNS_fallback;
1794 		serviced_tcp_initiate(sq, c->buffer);
1795 		return 0;
1796 	} else if(error==NETEVENT_NOERROR &&
1797 		sq->status == serviced_query_TCP_EDNS_fallback &&
1798 			(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1799 			LDNS_RCODE_NOERROR || LDNS_RCODE_WIRE(
1800 			sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NXDOMAIN
1801 			|| LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1802 			== LDNS_RCODE_YXDOMAIN)) {
1803 		/* the fallback produced a result that looks promising, note
1804 		 * that this server should be approached without EDNS */
1805 		/* only store noEDNS in cache if domain is noDNSSEC */
1806 		if(!sq->want_dnssec)
1807 		  if(!infra_edns_update(sq->outnet->infra, &sq->addr,
1808 			sq->addrlen, sq->zone, sq->zonelen, -1,
1809 			*sq->outnet->now_secs))
1810 			log_err("Out of memory caching no edns for host");
1811 		sq->status = serviced_query_TCP;
1812 	}
1813 	if(sq->tcp_upstream || sq->ssl_upstream) {
1814 	    struct timeval now = *sq->outnet->now_tv;
1815 	    if(error!=NETEVENT_NOERROR) {
1816 	        if(!infra_rtt_update(sq->outnet->infra, &sq->addr,
1817 		    sq->addrlen, sq->zone, sq->zonelen, sq->qtype,
1818 		    -1, sq->last_rtt, (time_t)now.tv_sec))
1819 		    log_err("out of memory in TCP exponential backoff.");
1820 	    } else if(now.tv_sec > sq->last_sent_time.tv_sec ||
1821 		(now.tv_sec == sq->last_sent_time.tv_sec &&
1822 		now.tv_usec > sq->last_sent_time.tv_usec)) {
1823 		/* convert from microseconds to milliseconds */
1824 		int roundtime = ((int)(now.tv_sec - sq->last_sent_time.tv_sec))*1000
1825 		  + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1826 		verbose(VERB_ALGO, "measured TCP-time at %d msec", roundtime);
1827 		log_assert(roundtime >= 0);
1828 		/* only store if less then AUTH_TIMEOUT seconds, it could be
1829 		 * huge due to system-hibernated and we woke up */
1830 		if(roundtime < 60000) {
1831 		    if(!infra_rtt_update(sq->outnet->infra, &sq->addr,
1832 			sq->addrlen, sq->zone, sq->zonelen, sq->qtype,
1833 			roundtime, sq->last_rtt, (time_t)now.tv_sec))
1834 			log_err("out of memory noting rtt.");
1835 		}
1836 	    }
1837 	}
1838 	/* insert address into reply info */
1839 	if(!rep) {
1840 		/* create one if there isn't (on errors) */
1841 		rep = &r2;
1842 		r2.c = c;
1843 	}
1844 	memcpy(&rep->addr, &sq->addr, sq->addrlen);
1845 	rep->addrlen = sq->addrlen;
1846 	serviced_callbacks(sq, error, c, rep);
1847 	return 0;
1848 }
1849 
1850 static void
1851 serviced_tcp_initiate(struct serviced_query* sq, sldns_buffer* buff)
1852 {
1853 	verbose(VERB_ALGO, "initiate TCP query %s",
1854 		sq->status==serviced_query_TCP_EDNS?"EDNS":"");
1855 	serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1856 	sq->last_sent_time = *sq->outnet->now_tv;
1857 	sq->pending = pending_tcp_query(sq, buff, TCP_AUTH_QUERY_TIMEOUT,
1858 		serviced_tcp_callback, sq);
1859 	if(!sq->pending) {
1860 		/* delete from tree so that a retry by above layer does not
1861 		 * clash with this entry */
1862 		log_err("serviced_tcp_initiate: failed to send tcp query");
1863 		serviced_callbacks(sq, NETEVENT_CLOSED, NULL, NULL);
1864 	}
1865 }
1866 
1867 /** Send serviced query over TCP return false on initial failure */
1868 static int
1869 serviced_tcp_send(struct serviced_query* sq, sldns_buffer* buff)
1870 {
1871 	int vs, rtt, timeout;
1872 	uint8_t edns_lame_known;
1873 	if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1874 		sq->zonelen, *sq->outnet->now_secs, &vs, &edns_lame_known,
1875 		&rtt))
1876 		return 0;
1877 	sq->last_rtt = rtt;
1878 	if(vs != -1)
1879 		sq->status = serviced_query_TCP_EDNS;
1880 	else 	sq->status = serviced_query_TCP;
1881 	serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1882 	sq->last_sent_time = *sq->outnet->now_tv;
1883 	if(sq->tcp_upstream || sq->ssl_upstream) {
1884 		timeout = rtt;
1885 		if(rtt >= 376 && rtt < TCP_AUTH_QUERY_TIMEOUT)
1886 			timeout = TCP_AUTH_QUERY_TIMEOUT;
1887 	} else {
1888 		timeout = TCP_AUTH_QUERY_TIMEOUT;
1889 	}
1890 	sq->pending = pending_tcp_query(sq, buff, timeout,
1891 		serviced_tcp_callback, sq);
1892 	return sq->pending != NULL;
1893 }
1894 
1895 /* see if packet is edns malformed; got zeroes at start.
1896  * This is from servers that return malformed packets to EDNS0 queries,
1897  * but they return good packets for nonEDNS0 queries.
1898  * We try to detect their output; without resorting to a full parse or
1899  * check for too many bytes after the end of the packet. */
1900 static int
1901 packet_edns_malformed(struct sldns_buffer* buf, int qtype)
1902 {
1903 	size_t len;
1904 	if(sldns_buffer_limit(buf) < LDNS_HEADER_SIZE)
1905 		return 1; /* malformed */
1906 	/* they have NOERROR rcode, 1 answer. */
1907 	if(LDNS_RCODE_WIRE(sldns_buffer_begin(buf)) != LDNS_RCODE_NOERROR)
1908 		return 0;
1909 	/* one query (to skip) and answer records */
1910 	if(LDNS_QDCOUNT(sldns_buffer_begin(buf)) != 1 ||
1911 		LDNS_ANCOUNT(sldns_buffer_begin(buf)) == 0)
1912 		return 0;
1913 	/* skip qname */
1914 	len = dname_valid(sldns_buffer_at(buf, LDNS_HEADER_SIZE),
1915 		sldns_buffer_limit(buf)-LDNS_HEADER_SIZE);
1916 	if(len == 0)
1917 		return 0;
1918 	if(len == 1 && qtype == 0)
1919 		return 0; /* we asked for '.' and type 0 */
1920 	/* and then 4 bytes (type and class of query) */
1921 	if(sldns_buffer_limit(buf) < LDNS_HEADER_SIZE + len + 4 + 3)
1922 		return 0;
1923 
1924 	/* and start with 11 zeroes as the answer RR */
1925 	/* so check the qtype of the answer record, qname=0, type=0 */
1926 	if(sldns_buffer_at(buf, LDNS_HEADER_SIZE+len+4)[0] == 0 &&
1927 	   sldns_buffer_at(buf, LDNS_HEADER_SIZE+len+4)[1] == 0 &&
1928 	   sldns_buffer_at(buf, LDNS_HEADER_SIZE+len+4)[2] == 0)
1929 		return 1;
1930 	return 0;
1931 }
1932 
1933 int
1934 serviced_udp_callback(struct comm_point* c, void* arg, int error,
1935         struct comm_reply* rep)
1936 {
1937 	struct serviced_query* sq = (struct serviced_query*)arg;
1938 	struct outside_network* outnet = sq->outnet;
1939 	struct timeval now = *sq->outnet->now_tv;
1940 	int fallback_tcp = 0;
1941 
1942 	sq->pending = NULL; /* removed after callback */
1943 	if(error == NETEVENT_TIMEOUT) {
1944 		int rto = 0;
1945 		if(sq->status == serviced_query_PROBE_EDNS) {
1946 			/* non-EDNS probe failed; we do not know its status,
1947 			 * keep trying with EDNS, timeout may not be caused
1948 			 * by EDNS. */
1949 			sq->status = serviced_query_UDP_EDNS;
1950 		}
1951 		if(sq->status == serviced_query_UDP_EDNS && sq->last_rtt < 5000) {
1952 			/* fallback to 1480/1280 */
1953 			sq->status = serviced_query_UDP_EDNS_FRAG;
1954 			log_name_addr(VERB_ALGO, "try edns1xx0", sq->qbuf+10,
1955 				&sq->addr, sq->addrlen);
1956 			if(!serviced_udp_send(sq, c->buffer)) {
1957 				serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1958 			}
1959 			return 0;
1960 		}
1961 		if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1962 			/* fragmentation size did not fix it */
1963 			sq->status = serviced_query_UDP_EDNS;
1964 		}
1965 		sq->retry++;
1966 		if(!(rto=infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1967 			sq->zone, sq->zonelen, sq->qtype, -1, sq->last_rtt,
1968 			(time_t)now.tv_sec)))
1969 			log_err("out of memory in UDP exponential backoff");
1970 		if(sq->retry < OUTBOUND_UDP_RETRY) {
1971 			log_name_addr(VERB_ALGO, "retry query", sq->qbuf+10,
1972 				&sq->addr, sq->addrlen);
1973 			if(!serviced_udp_send(sq, c->buffer)) {
1974 				serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1975 			}
1976 			return 0;
1977 		}
1978 		if(rto >= RTT_MAX_TIMEOUT) {
1979 			fallback_tcp = 1;
1980 			/* UDP does not work, fallback to TCP below */
1981 		} else {
1982 			serviced_callbacks(sq, NETEVENT_TIMEOUT, c, rep);
1983 			return 0;
1984 		}
1985 	} else if(error != NETEVENT_NOERROR) {
1986 		/* udp returns error (due to no ID or interface available) */
1987 		serviced_callbacks(sq, error, c, rep);
1988 		return 0;
1989 	}
1990 #ifdef USE_DNSTAP
1991 	if(error == NETEVENT_NOERROR && outnet->dtenv &&
1992 	   (outnet->dtenv->log_resolver_response_messages ||
1993 	    outnet->dtenv->log_forwarder_response_messages))
1994 		dt_msg_send_outside_response(outnet->dtenv, &sq->addr, c->type,
1995 		sq->zone, sq->zonelen, sq->qbuf, sq->qbuflen,
1996 		&sq->last_sent_time, sq->outnet->now_tv, c->buffer);
1997 #endif
1998 	if(!fallback_tcp) {
1999 	    if( (sq->status == serviced_query_UDP_EDNS
2000 	        ||sq->status == serviced_query_UDP_EDNS_FRAG)
2001 		&& (LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
2002 			== LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(
2003 			sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOTIMPL
2004 		    || packet_edns_malformed(c->buffer, sq->qtype)
2005 			)) {
2006 		/* try to get an answer by falling back without EDNS */
2007 		verbose(VERB_ALGO, "serviced query: attempt without EDNS");
2008 		sq->status = serviced_query_UDP_EDNS_fallback;
2009 		sq->retry = 0;
2010 		if(!serviced_udp_send(sq, c->buffer)) {
2011 			serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
2012 		}
2013 		return 0;
2014 	    } else if(sq->status == serviced_query_PROBE_EDNS) {
2015 		/* probe without EDNS succeeds, so we conclude that this
2016 		 * host likely has EDNS packets dropped */
2017 		log_addr(VERB_DETAIL, "timeouts, concluded that connection to "
2018 			"host drops EDNS packets", &sq->addr, sq->addrlen);
2019 		/* only store noEDNS in cache if domain is noDNSSEC */
2020 		if(!sq->want_dnssec)
2021 		  if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
2022 			sq->zone, sq->zonelen, -1, (time_t)now.tv_sec)) {
2023 			log_err("Out of memory caching no edns for host");
2024 		  }
2025 		sq->status = serviced_query_UDP;
2026 	    } else if(sq->status == serviced_query_UDP_EDNS &&
2027 		!sq->edns_lame_known) {
2028 		/* now we know that edns queries received answers store that */
2029 		log_addr(VERB_ALGO, "serviced query: EDNS works for",
2030 			&sq->addr, sq->addrlen);
2031 		if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
2032 			sq->zone, sq->zonelen, 0, (time_t)now.tv_sec)) {
2033 			log_err("Out of memory caching edns works");
2034 		}
2035 		sq->edns_lame_known = 1;
2036 	    } else if(sq->status == serviced_query_UDP_EDNS_fallback &&
2037 		!sq->edns_lame_known && (LDNS_RCODE_WIRE(
2038 		sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOERROR ||
2039 		LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
2040 		LDNS_RCODE_NXDOMAIN || LDNS_RCODE_WIRE(sldns_buffer_begin(
2041 		c->buffer)) == LDNS_RCODE_YXDOMAIN)) {
2042 		/* the fallback produced a result that looks promising, note
2043 		 * that this server should be approached without EDNS */
2044 		/* only store noEDNS in cache if domain is noDNSSEC */
2045 		if(!sq->want_dnssec) {
2046 		  log_addr(VERB_ALGO, "serviced query: EDNS fails for",
2047 			&sq->addr, sq->addrlen);
2048 		  if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
2049 			sq->zone, sq->zonelen, -1, (time_t)now.tv_sec)) {
2050 			log_err("Out of memory caching no edns for host");
2051 		  }
2052 		} else {
2053 		  log_addr(VERB_ALGO, "serviced query: EDNS fails, but "
2054 		  	"not stored because need DNSSEC for", &sq->addr,
2055 			sq->addrlen);
2056 		}
2057 		sq->status = serviced_query_UDP;
2058 	    }
2059 	    if(now.tv_sec > sq->last_sent_time.tv_sec ||
2060 		(now.tv_sec == sq->last_sent_time.tv_sec &&
2061 		now.tv_usec > sq->last_sent_time.tv_usec)) {
2062 		/* convert from microseconds to milliseconds */
2063 		int roundtime = ((int)(now.tv_sec - sq->last_sent_time.tv_sec))*1000
2064 		  + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
2065 		verbose(VERB_ALGO, "measured roundtrip at %d msec", roundtime);
2066 		log_assert(roundtime >= 0);
2067 		/* in case the system hibernated, do not enter a huge value,
2068 		 * above this value gives trouble with server selection */
2069 		if(roundtime < 60000) {
2070 		    if(!infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
2071 			sq->zone, sq->zonelen, sq->qtype, roundtime,
2072 			sq->last_rtt, (time_t)now.tv_sec))
2073 			log_err("out of memory noting rtt.");
2074 		}
2075 	    }
2076 	} /* end of if_!fallback_tcp */
2077 	/* perform TC flag check and TCP fallback after updating our
2078 	 * cache entries for EDNS status and RTT times */
2079 	if(LDNS_TC_WIRE(sldns_buffer_begin(c->buffer)) || fallback_tcp) {
2080 		/* fallback to TCP */
2081 		/* this discards partial UDP contents */
2082 		if(sq->status == serviced_query_UDP_EDNS ||
2083 			sq->status == serviced_query_UDP_EDNS_FRAG ||
2084 			sq->status == serviced_query_UDP_EDNS_fallback)
2085 			/* if we have unfinished EDNS_fallback, start again */
2086 			sq->status = serviced_query_TCP_EDNS;
2087 		else	sq->status = serviced_query_TCP;
2088 		serviced_tcp_initiate(sq, c->buffer);
2089 		return 0;
2090 	}
2091 	/* yay! an answer */
2092 	serviced_callbacks(sq, error, c, rep);
2093 	return 0;
2094 }
2095 
2096 struct serviced_query*
2097 outnet_serviced_query(struct outside_network* outnet,
2098 	struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec,
2099 	int nocaps, int tcp_upstream, int ssl_upstream, char* tls_auth_name,
2100 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
2101 	size_t zonelen, struct module_qstate* qstate,
2102 	comm_point_callback_type* callback, void* callback_arg, sldns_buffer* buff,
2103 	struct module_env* env)
2104 {
2105 	struct serviced_query* sq;
2106 	struct service_callback* cb;
2107 	if(!inplace_cb_query_call(env, qinfo, flags, addr, addrlen, zone, zonelen,
2108 		qstate, qstate->region))
2109 			return NULL;
2110 	serviced_gen_query(buff, qinfo->qname, qinfo->qname_len, qinfo->qtype,
2111 		qinfo->qclass, flags);
2112 	sq = lookup_serviced(outnet, buff, dnssec, addr, addrlen,
2113 		qstate->edns_opts_back_out);
2114 	/* duplicate entries are included in the callback list, because
2115 	 * there is a counterpart registration by our caller that needs to
2116 	 * be doubly-removed (with callbacks perhaps). */
2117 	if(!(cb = (struct service_callback*)malloc(sizeof(*cb))))
2118 		return NULL;
2119 	if(!sq) {
2120 		/* make new serviced query entry */
2121 		sq = serviced_create(outnet, buff, dnssec, want_dnssec, nocaps,
2122 			tcp_upstream, ssl_upstream, tls_auth_name, addr,
2123 			addrlen, zone, zonelen, (int)qinfo->qtype,
2124 			qstate->edns_opts_back_out);
2125 		if(!sq) {
2126 			free(cb);
2127 			return NULL;
2128 		}
2129 		/* perform first network action */
2130 		if(outnet->do_udp && !(tcp_upstream || ssl_upstream)) {
2131 			if(!serviced_udp_send(sq, buff)) {
2132 				(void)rbtree_delete(outnet->serviced, sq);
2133 				free(sq->qbuf);
2134 				free(sq->zone);
2135 				free(sq);
2136 				free(cb);
2137 				return NULL;
2138 			}
2139 		} else {
2140 			if(!serviced_tcp_send(sq, buff)) {
2141 				(void)rbtree_delete(outnet->serviced, sq);
2142 				free(sq->qbuf);
2143 				free(sq->zone);
2144 				free(sq);
2145 				free(cb);
2146 				return NULL;
2147 			}
2148 		}
2149 	}
2150 	/* add callback to list of callbacks */
2151 	cb->cb = callback;
2152 	cb->cb_arg = callback_arg;
2153 	cb->next = sq->cblist;
2154 	sq->cblist = cb;
2155 	return sq;
2156 }
2157 
2158 /** remove callback from list */
2159 static void
2160 callback_list_remove(struct serviced_query* sq, void* cb_arg)
2161 {
2162 	struct service_callback** pp = &sq->cblist;
2163 	while(*pp) {
2164 		if((*pp)->cb_arg == cb_arg) {
2165 			struct service_callback* del = *pp;
2166 			*pp = del->next;
2167 			free(del);
2168 			return;
2169 		}
2170 		pp = &(*pp)->next;
2171 	}
2172 }
2173 
2174 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
2175 {
2176 	if(!sq)
2177 		return;
2178 	callback_list_remove(sq, cb_arg);
2179 	/* if callbacks() routine scheduled deletion, let it do that */
2180 	if(!sq->cblist && !sq->to_be_deleted) {
2181 		(void)rbtree_delete(sq->outnet->serviced, sq);
2182 		serviced_delete(sq);
2183 	}
2184 }
2185 
2186 /** create fd to send to this destination */
2187 static int
2188 fd_for_dest(struct outside_network* outnet, struct sockaddr_storage* to_addr,
2189 	socklen_t to_addrlen)
2190 {
2191 	struct sockaddr_storage* addr;
2192 	socklen_t addrlen;
2193 	int i;
2194 	int try;
2195 
2196 	/* select interface */
2197 	if(addr_is_ip6(to_addr, to_addrlen)) {
2198 		if(outnet->num_ip6 == 0) {
2199 			char to[64];
2200 			addr_to_str(to_addr, to_addrlen, to, sizeof(to));
2201 			verbose(VERB_QUERY, "need ipv6 to send, but no ipv6 outgoing interfaces, for %s", to);
2202 			return -1;
2203 		}
2204 		i = ub_random_max(outnet->rnd, outnet->num_ip6);
2205 		addr = &outnet->ip6_ifs[i].addr;
2206 		addrlen = outnet->ip6_ifs[i].addrlen;
2207 	} else {
2208 		if(outnet->num_ip4 == 0) {
2209 			char to[64];
2210 			addr_to_str(to_addr, to_addrlen, to, sizeof(to));
2211 			verbose(VERB_QUERY, "need ipv4 to send, but no ipv4 outgoing interfaces, for %s", to);
2212 			return -1;
2213 		}
2214 		i = ub_random_max(outnet->rnd, outnet->num_ip4);
2215 		addr = &outnet->ip4_ifs[i].addr;
2216 		addrlen = outnet->ip4_ifs[i].addrlen;
2217 	}
2218 
2219 	/* create fd */
2220 	for(try = 0; try<1000; try++) {
2221 		int freebind = 0;
2222 		int noproto = 0;
2223 		int inuse = 0;
2224 		int port = ub_random(outnet->rnd)&0xffff;
2225 		int fd = -1;
2226 		if(addr_is_ip6(to_addr, to_addrlen)) {
2227 			struct sockaddr_in6 sa = *(struct sockaddr_in6*)addr;
2228 			sa.sin6_port = (in_port_t)htons((uint16_t)port);
2229 			fd = create_udp_sock(AF_INET6, SOCK_DGRAM,
2230 				(struct sockaddr*)&sa, addrlen, 1, &inuse, &noproto,
2231 				0, 0, 0, NULL, 0, freebind, 0);
2232 		} else {
2233 			struct sockaddr_in* sa = (struct sockaddr_in*)addr;
2234 			sa->sin_port = (in_port_t)htons((uint16_t)port);
2235 			fd = create_udp_sock(AF_INET, SOCK_DGRAM,
2236 				(struct sockaddr*)addr, addrlen, 1, &inuse, &noproto,
2237 				0, 0, 0, NULL, 0, freebind, 0);
2238 		}
2239 		if(fd != -1) {
2240 			return fd;
2241 		}
2242 		if(!inuse) {
2243 			return -1;
2244 		}
2245 	}
2246 	/* too many tries */
2247 	log_err("cannot send probe, ports are in use");
2248 	return -1;
2249 }
2250 
2251 struct comm_point*
2252 outnet_comm_point_for_udp(struct outside_network* outnet,
2253 	comm_point_callback_type* cb, void* cb_arg,
2254 	struct sockaddr_storage* to_addr, socklen_t to_addrlen)
2255 {
2256 	struct comm_point* cp;
2257 	int fd = fd_for_dest(outnet, to_addr, to_addrlen);
2258 	if(fd == -1) {
2259 		return NULL;
2260 	}
2261 	cp = comm_point_create_udp(outnet->base, fd, outnet->udp_buff,
2262 		cb, cb_arg);
2263 	if(!cp) {
2264 		log_err("malloc failure");
2265 		close(fd);
2266 		return NULL;
2267 	}
2268 	return cp;
2269 }
2270 
2271 struct comm_point*
2272 outnet_comm_point_for_tcp(struct outside_network* outnet,
2273 	comm_point_callback_type* cb, void* cb_arg,
2274 	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
2275 	sldns_buffer* query, int timeout)
2276 {
2277 	struct comm_point* cp;
2278 	int fd = outnet_get_tcp_fd(to_addr, to_addrlen, outnet->tcp_mss);
2279 	if(fd == -1) {
2280 		return 0;
2281 	}
2282 	fd_set_nonblock(fd);
2283 	if(!outnet_tcp_connect(fd, to_addr, to_addrlen)) {
2284 		/* outnet_tcp_connect has closed fd on error for us */
2285 		return 0;
2286 	}
2287 	cp = comm_point_create_tcp_out(outnet->base, 65552, cb, cb_arg);
2288 	if(!cp) {
2289 		log_err("malloc failure");
2290 		close(fd);
2291 		return 0;
2292 	}
2293 	cp->repinfo.addrlen = to_addrlen;
2294 	memcpy(&cp->repinfo.addr, to_addr, to_addrlen);
2295 	/* set timeout on TCP connection */
2296 	comm_point_start_listening(cp, fd, timeout);
2297 	/* copy scratch buffer to cp->buffer */
2298 	sldns_buffer_copy(cp->buffer, query);
2299 	return cp;
2300 }
2301 
2302 /** setup http request headers in buffer for sending query to destination */
2303 static int
2304 setup_http_request(sldns_buffer* buf, char* host, char* path)
2305 {
2306 	sldns_buffer_clear(buf);
2307 	sldns_buffer_printf(buf, "GET /%s HTTP/1.1\r\n", path);
2308 	sldns_buffer_printf(buf, "Host: %s\r\n", host);
2309 	sldns_buffer_printf(buf, "User-Agent: unbound/%s\r\n",
2310 		PACKAGE_VERSION);
2311 	/* We do not really do multiple queries per connection,
2312 	 * but this header setting is also not needed.
2313 	 * sldns_buffer_printf(buf, "Connection: close\r\n") */
2314 	sldns_buffer_printf(buf, "\r\n");
2315 	if(sldns_buffer_position(buf)+10 > sldns_buffer_capacity(buf))
2316 		return 0; /* somehow buffer too short, but it is about 60K
2317 		and the request is only a couple bytes long. */
2318 	sldns_buffer_flip(buf);
2319 	return 1;
2320 }
2321 
2322 struct comm_point*
2323 outnet_comm_point_for_http(struct outside_network* outnet,
2324 	comm_point_callback_type* cb, void* cb_arg,
2325 	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
2326 	int ssl, char* host, char* path)
2327 {
2328 	/* cp calls cb with err=NETEVENT_DONE when transfer is done */
2329 	struct comm_point* cp;
2330 	int fd = outnet_get_tcp_fd(to_addr, to_addrlen, outnet->tcp_mss);
2331 	if(fd == -1) {
2332 		return 0;
2333 	}
2334 	fd_set_nonblock(fd);
2335 	if(!outnet_tcp_connect(fd, to_addr, to_addrlen)) {
2336 		/* outnet_tcp_connect has closed fd on error for us */
2337 		return 0;
2338 	}
2339 	cp = comm_point_create_http_out(outnet->base, 65552, cb, cb_arg,
2340 		outnet->udp_buff);
2341 	if(!cp) {
2342 		log_err("malloc failure");
2343 		close(fd);
2344 		return 0;
2345 	}
2346 	cp->repinfo.addrlen = to_addrlen;
2347 	memcpy(&cp->repinfo.addr, to_addr, to_addrlen);
2348 
2349 	/* setup for SSL (if needed) */
2350 	if(ssl) {
2351 		cp->ssl = outgoing_ssl_fd(outnet->sslctx, fd);
2352 		if(!cp->ssl) {
2353 			log_err("cannot setup https");
2354 			comm_point_delete(cp);
2355 			return NULL;
2356 		}
2357 #ifdef USE_WINSOCK
2358 		comm_point_tcp_win_bio_cb(cp, cp->ssl);
2359 #endif
2360 		cp->ssl_shake_state = comm_ssl_shake_write;
2361 		/* https verification */
2362 #ifdef HAVE_SSL_SET1_HOST
2363 		if((SSL_CTX_get_verify_mode(outnet->sslctx)&SSL_VERIFY_PEER)) {
2364 			/* because we set SSL_VERIFY_PEER, in netevent in
2365 			 * ssl_handshake, it'll check if the certificate
2366 			 * verification has succeeded */
2367 			/* SSL_VERIFY_PEER is set on the sslctx */
2368 			/* and the certificates to verify with are loaded into
2369 			 * it with SSL_load_verify_locations or
2370 			 * SSL_CTX_set_default_verify_paths */
2371 			/* setting the hostname makes openssl verify the
2372 			 * host name in the x509 certificate in the
2373 			 * SSL connection*/
2374 		 	if(!SSL_set1_host(cp->ssl, host)) {
2375 				log_err("SSL_set1_host failed");
2376 				comm_point_delete(cp);
2377 				return NULL;
2378 			}
2379 		}
2380 #endif /* HAVE_SSL_SET1_HOST */
2381 	}
2382 
2383 	/* set timeout on TCP connection */
2384 	comm_point_start_listening(cp, fd, timeout);
2385 
2386 	/* setup http request in cp->buffer */
2387 	if(!setup_http_request(cp->buffer, host, path)) {
2388 		log_err("error setting up http request");
2389 		comm_point_delete(cp);
2390 		return NULL;
2391 	}
2392 	return cp;
2393 }
2394 
2395 /** get memory used by waiting tcp entry (in use or not) */
2396 static size_t
2397 waiting_tcp_get_mem(struct waiting_tcp* w)
2398 {
2399 	size_t s;
2400 	if(!w) return 0;
2401 	s = sizeof(*w) + w->pkt_len;
2402 	if(w->timer)
2403 		s += comm_timer_get_mem(w->timer);
2404 	return s;
2405 }
2406 
2407 /** get memory used by port if */
2408 static size_t
2409 if_get_mem(struct port_if* pif)
2410 {
2411 	size_t s;
2412 	int i;
2413 	s = sizeof(*pif) + sizeof(int)*pif->avail_total +
2414 		sizeof(struct port_comm*)*pif->maxout;
2415 	for(i=0; i<pif->inuse; i++)
2416 		s += sizeof(*pif->out[i]) +
2417 			comm_point_get_mem(pif->out[i]->cp);
2418 	return s;
2419 }
2420 
2421 /** get memory used by waiting udp */
2422 static size_t
2423 waiting_udp_get_mem(struct pending* w)
2424 {
2425 	size_t s;
2426 	s = sizeof(*w) + comm_timer_get_mem(w->timer) + w->pkt_len;
2427 	return s;
2428 }
2429 
2430 size_t outnet_get_mem(struct outside_network* outnet)
2431 {
2432 	size_t i;
2433 	int k;
2434 	struct waiting_tcp* w;
2435 	struct pending* u;
2436 	struct serviced_query* sq;
2437 	struct service_callback* sb;
2438 	struct port_comm* pc;
2439 	size_t s = sizeof(*outnet) + sizeof(*outnet->base) +
2440 		sizeof(*outnet->udp_buff) +
2441 		sldns_buffer_capacity(outnet->udp_buff);
2442 	/* second buffer is not ours */
2443 	for(pc = outnet->unused_fds; pc; pc = pc->next) {
2444 		s += sizeof(*pc) + comm_point_get_mem(pc->cp);
2445 	}
2446 	for(k=0; k<outnet->num_ip4; k++)
2447 		s += if_get_mem(&outnet->ip4_ifs[k]);
2448 	for(k=0; k<outnet->num_ip6; k++)
2449 		s += if_get_mem(&outnet->ip6_ifs[k]);
2450 	for(u=outnet->udp_wait_first; u; u=u->next_waiting)
2451 		s += waiting_udp_get_mem(u);
2452 
2453 	s += sizeof(struct pending_tcp*)*outnet->num_tcp;
2454 	for(i=0; i<outnet->num_tcp; i++) {
2455 		s += sizeof(struct pending_tcp);
2456 		s += comm_point_get_mem(outnet->tcp_conns[i]->c);
2457 		if(outnet->tcp_conns[i]->query)
2458 			s += waiting_tcp_get_mem(outnet->tcp_conns[i]->query);
2459 	}
2460 	for(w=outnet->tcp_wait_first; w; w = w->next_waiting)
2461 		s += waiting_tcp_get_mem(w);
2462 	s += sizeof(*outnet->pending);
2463 	s += (sizeof(struct pending) + comm_timer_get_mem(NULL)) *
2464 		outnet->pending->count;
2465 	s += sizeof(*outnet->serviced);
2466 	s += outnet->svcd_overhead;
2467 	RBTREE_FOR(sq, struct serviced_query*, outnet->serviced) {
2468 		s += sizeof(*sq) + sq->qbuflen;
2469 		for(sb = sq->cblist; sb; sb = sb->next)
2470 			s += sizeof(*sb);
2471 	}
2472 	return s;
2473 }
2474 
2475 size_t
2476 serviced_get_mem(struct serviced_query* sq)
2477 {
2478 	struct service_callback* sb;
2479 	size_t s;
2480 	s = sizeof(*sq) + sq->qbuflen;
2481 	for(sb = sq->cblist; sb; sb = sb->next)
2482 		s += sizeof(*sb);
2483 	if(sq->status == serviced_query_UDP_EDNS ||
2484 		sq->status == serviced_query_UDP ||
2485 		sq->status == serviced_query_PROBE_EDNS ||
2486 		sq->status == serviced_query_UDP_EDNS_FRAG ||
2487 		sq->status == serviced_query_UDP_EDNS_fallback) {
2488 		s += sizeof(struct pending);
2489 		s += comm_timer_get_mem(NULL);
2490 	} else {
2491 		/* does not have size of the pkt pointer */
2492 		/* always has a timer except on malloc failures */
2493 
2494 		/* these sizes are part of the main outside network mem */
2495 		/*
2496 		s += sizeof(struct waiting_tcp);
2497 		s += comm_timer_get_mem(NULL);
2498 		*/
2499 	}
2500 	return s;
2501 }
2502 
2503