outside_network.c revision 269257
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 "ldns/sbuffer.h"
61#ifdef HAVE_OPENSSL_SSL_H
62#include <openssl/ssl.h>
63#endif
64
65#ifdef HAVE_NETDB_H
66#include <netdb.h>
67#endif
68#include <fcntl.h>
69
70/** number of times to retry making a random ID that is unique. */
71#define MAX_ID_RETRY 1000
72/** number of times to retry finding interface, port that can be opened. */
73#define MAX_PORT_RETRY 10000
74/** number of retries on outgoing UDP queries */
75#define OUTBOUND_UDP_RETRY 1
76
77/** initiate TCP transaction for serviced query */
78static void serviced_tcp_initiate(struct outside_network* outnet,
79	struct serviced_query* sq, sldns_buffer* buff);
80/** with a fd available, randomize and send UDP */
81static int randomize_and_send_udp(struct outside_network* outnet,
82	struct pending* pend, sldns_buffer* packet, int timeout);
83
84int
85pending_cmp(const void* key1, const void* key2)
86{
87	struct pending *p1 = (struct pending*)key1;
88	struct pending *p2 = (struct pending*)key2;
89	if(p1->id < p2->id)
90		return -1;
91	if(p1->id > p2->id)
92		return 1;
93	log_assert(p1->id == p2->id);
94	return sockaddr_cmp(&p1->addr, p1->addrlen, &p2->addr, p2->addrlen);
95}
96
97int
98serviced_cmp(const void* key1, const void* key2)
99{
100	struct serviced_query* q1 = (struct serviced_query*)key1;
101	struct serviced_query* q2 = (struct serviced_query*)key2;
102	int r;
103	if(q1->qbuflen < q2->qbuflen)
104		return -1;
105	if(q1->qbuflen > q2->qbuflen)
106		return 1;
107	log_assert(q1->qbuflen == q2->qbuflen);
108	log_assert(q1->qbuflen >= 15 /* 10 header, root, type, class */);
109	/* alternate casing of qname is still the same query */
110	if((r = memcmp(q1->qbuf, q2->qbuf, 10)) != 0)
111		return r;
112	if((r = memcmp(q1->qbuf+q1->qbuflen-4, q2->qbuf+q2->qbuflen-4, 4)) != 0)
113		return r;
114	if(q1->dnssec != q2->dnssec) {
115		if(q1->dnssec < q2->dnssec)
116			return -1;
117		return 1;
118	}
119	if((r = query_dname_compare(q1->qbuf+10, q2->qbuf+10)) != 0)
120		return r;
121	return sockaddr_cmp(&q1->addr, q1->addrlen, &q2->addr, q2->addrlen);
122}
123
124/** delete waiting_tcp entry. Does not unlink from waiting list.
125 * @param w: to delete.
126 */
127static void
128waiting_tcp_delete(struct waiting_tcp* w)
129{
130	if(!w) return;
131	if(w->timer)
132		comm_timer_delete(w->timer);
133	free(w);
134}
135
136/**
137 * Pick random outgoing-interface of that family, and bind it.
138 * port set to 0 so OS picks a port number for us.
139 * if it is the ANY address, do not bind.
140 * @param w: tcp structure with destination address.
141 * @param s: socket fd.
142 * @return false on error, socket closed.
143 */
144static int
145pick_outgoing_tcp(struct waiting_tcp* w, int s)
146{
147	struct port_if* pi = NULL;
148	int num;
149#ifdef INET6
150	if(addr_is_ip6(&w->addr, w->addrlen))
151		num = w->outnet->num_ip6;
152	else
153#endif
154		num = w->outnet->num_ip4;
155	if(num == 0) {
156		log_err("no TCP outgoing interfaces of family");
157		log_addr(VERB_OPS, "for addr", &w->addr, w->addrlen);
158#ifndef USE_WINSOCK
159		close(s);
160#else
161		closesocket(s);
162#endif
163		return 0;
164	}
165#ifdef INET6
166	if(addr_is_ip6(&w->addr, w->addrlen))
167		pi = &w->outnet->ip6_ifs[ub_random_max(w->outnet->rnd, num)];
168	else
169#endif
170		pi = &w->outnet->ip4_ifs[ub_random_max(w->outnet->rnd, num)];
171	log_assert(pi);
172	if(addr_is_any(&pi->addr, pi->addrlen)) {
173		/* binding to the ANY interface is for listening sockets */
174		return 1;
175	}
176	/* set port to 0 */
177	if(addr_is_ip6(&pi->addr, pi->addrlen))
178		((struct sockaddr_in6*)&pi->addr)->sin6_port = 0;
179	else	((struct sockaddr_in*)&pi->addr)->sin_port = 0;
180	if(bind(s, (struct sockaddr*)&pi->addr, pi->addrlen) != 0) {
181#ifndef USE_WINSOCK
182		log_err("outgoing tcp: bind: %s", strerror(errno));
183		close(s);
184#else
185		log_err("outgoing tcp: bind: %s",
186			wsa_strerror(WSAGetLastError()));
187		closesocket(s);
188#endif
189		return 0;
190	}
191	log_addr(VERB_ALGO, "tcp bound to src", &pi->addr, pi->addrlen);
192	return 1;
193}
194
195/** use next free buffer to service a tcp query */
196static int
197outnet_tcp_take_into_use(struct waiting_tcp* w, uint8_t* pkt, size_t pkt_len)
198{
199	struct pending_tcp* pend = w->outnet->tcp_free;
200	int s;
201	log_assert(pend);
202	log_assert(pkt);
203	log_assert(w->addrlen > 0);
204	/* open socket */
205#ifdef INET6
206	if(addr_is_ip6(&w->addr, w->addrlen))
207		s = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP);
208	else
209#endif
210		s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
211	if(s == -1) {
212#ifndef USE_WINSOCK
213		log_err("outgoing tcp: socket: %s", strerror(errno));
214#else
215		log_err("outgoing tcp: socket: %s",
216			wsa_strerror(WSAGetLastError()));
217#endif
218		log_addr(0, "failed address", &w->addr, w->addrlen);
219		return 0;
220	}
221	if(!pick_outgoing_tcp(w, s))
222		return 0;
223
224	fd_set_nonblock(s);
225	if(connect(s, (struct sockaddr*)&w->addr, w->addrlen) == -1) {
226#ifndef USE_WINSOCK
227#ifdef EINPROGRESS
228		if(errno != EINPROGRESS) {
229#else
230		if(1) {
231#endif
232			if(tcp_connect_errno_needs_log(
233				(struct sockaddr*)&w->addr, w->addrlen))
234				log_err("outgoing tcp: connect: %s",
235					strerror(errno));
236			close(s);
237#else /* USE_WINSOCK */
238		if(WSAGetLastError() != WSAEINPROGRESS &&
239			WSAGetLastError() != WSAEWOULDBLOCK) {
240			closesocket(s);
241#endif
242			log_addr(0, "failed address", &w->addr, w->addrlen);
243			return 0;
244		}
245	}
246	if(w->outnet->sslctx && w->ssl_upstream) {
247		pend->c->ssl = outgoing_ssl_fd(w->outnet->sslctx, s);
248		if(!pend->c->ssl) {
249			pend->c->fd = s;
250			comm_point_close(pend->c);
251			return 0;
252		}
253#ifdef USE_WINSOCK
254		comm_point_tcp_win_bio_cb(pend->c, pend->c->ssl);
255#endif
256		pend->c->ssl_shake_state = comm_ssl_shake_write;
257	}
258	w->pkt = NULL;
259	w->next_waiting = (void*)pend;
260	pend->id = LDNS_ID_WIRE(pkt);
261	w->outnet->tcp_free = pend->next_free;
262	pend->next_free = NULL;
263	pend->query = w;
264	pend->c->repinfo.addrlen = w->addrlen;
265	memcpy(&pend->c->repinfo.addr, &w->addr, w->addrlen);
266	sldns_buffer_clear(pend->c->buffer);
267	sldns_buffer_write(pend->c->buffer, pkt, pkt_len);
268	sldns_buffer_flip(pend->c->buffer);
269	pend->c->tcp_is_reading = 0;
270	pend->c->tcp_byte_count = 0;
271	comm_point_start_listening(pend->c, s, -1);
272	return 1;
273}
274
275/** see if buffers can be used to service TCP queries */
276static void
277use_free_buffer(struct outside_network* outnet)
278{
279	struct waiting_tcp* w;
280	while(outnet->tcp_free && outnet->tcp_wait_first
281		&& !outnet->want_to_quit) {
282		w = outnet->tcp_wait_first;
283		outnet->tcp_wait_first = w->next_waiting;
284		if(outnet->tcp_wait_last == w)
285			outnet->tcp_wait_last = NULL;
286		if(!outnet_tcp_take_into_use(w, w->pkt, w->pkt_len)) {
287			comm_point_callback_t* cb = w->cb;
288			void* cb_arg = w->cb_arg;
289			waiting_tcp_delete(w);
290			fptr_ok(fptr_whitelist_pending_tcp(cb));
291			(void)(*cb)(NULL, cb_arg, NETEVENT_CLOSED, NULL);
292		}
293	}
294}
295
296/** decomission a tcp buffer, closes commpoint and frees waiting_tcp entry */
297static void
298decomission_pending_tcp(struct outside_network* outnet,
299	struct pending_tcp* pend)
300{
301	if(pend->c->ssl) {
302#ifdef HAVE_SSL
303		SSL_shutdown(pend->c->ssl);
304		SSL_free(pend->c->ssl);
305		pend->c->ssl = NULL;
306#endif
307	}
308	comm_point_close(pend->c);
309	pend->next_free = outnet->tcp_free;
310	outnet->tcp_free = pend;
311	waiting_tcp_delete(pend->query);
312	pend->query = NULL;
313	use_free_buffer(outnet);
314}
315
316int
317outnet_tcp_cb(struct comm_point* c, void* arg, int error,
318	struct comm_reply *reply_info)
319{
320	struct pending_tcp* pend = (struct pending_tcp*)arg;
321	struct outside_network* outnet = pend->query->outnet;
322	verbose(VERB_ALGO, "outnettcp cb");
323	if(error != NETEVENT_NOERROR) {
324		verbose(VERB_QUERY, "outnettcp got tcp error %d", error);
325		/* pass error below and exit */
326	} else {
327		/* check ID */
328		if(sldns_buffer_limit(c->buffer) < sizeof(uint16_t) ||
329			LDNS_ID_WIRE(sldns_buffer_begin(c->buffer))!=pend->id) {
330			log_addr(VERB_QUERY,
331				"outnettcp: bad ID in reply, from:",
332				&pend->query->addr, pend->query->addrlen);
333			error = NETEVENT_CLOSED;
334		}
335	}
336	fptr_ok(fptr_whitelist_pending_tcp(pend->query->cb));
337	(void)(*pend->query->cb)(c, pend->query->cb_arg, error, reply_info);
338	decomission_pending_tcp(outnet, pend);
339	return 0;
340}
341
342/** lower use count on pc, see if it can be closed */
343static void
344portcomm_loweruse(struct outside_network* outnet, struct port_comm* pc)
345{
346	struct port_if* pif;
347	pc->num_outstanding--;
348	if(pc->num_outstanding > 0) {
349		return;
350	}
351	/* close it and replace in unused list */
352	verbose(VERB_ALGO, "close of port %d", pc->number);
353	comm_point_close(pc->cp);
354	pif = pc->pif;
355	log_assert(pif->inuse > 0);
356	pif->avail_ports[pif->avail_total - pif->inuse] = pc->number;
357	pif->inuse--;
358	pif->out[pc->index] = pif->out[pif->inuse];
359	pif->out[pc->index]->index = pc->index;
360	pc->next = outnet->unused_fds;
361	outnet->unused_fds = pc;
362}
363
364/** try to send waiting UDP queries */
365static void
366outnet_send_wait_udp(struct outside_network* outnet)
367{
368	struct pending* pend;
369	/* process waiting queries */
370	while(outnet->udp_wait_first && outnet->unused_fds
371		&& !outnet->want_to_quit) {
372		pend = outnet->udp_wait_first;
373		outnet->udp_wait_first = pend->next_waiting;
374		if(!pend->next_waiting) outnet->udp_wait_last = NULL;
375		sldns_buffer_clear(outnet->udp_buff);
376		sldns_buffer_write(outnet->udp_buff, pend->pkt, pend->pkt_len);
377		sldns_buffer_flip(outnet->udp_buff);
378		free(pend->pkt); /* freeing now makes get_mem correct */
379		pend->pkt = NULL;
380		pend->pkt_len = 0;
381		if(!randomize_and_send_udp(outnet, pend, outnet->udp_buff,
382			pend->timeout)) {
383			/* callback error on pending */
384			if(pend->cb) {
385				fptr_ok(fptr_whitelist_pending_udp(pend->cb));
386				(void)(*pend->cb)(outnet->unused_fds->cp, pend->cb_arg,
387					NETEVENT_CLOSED, NULL);
388			}
389			pending_delete(outnet, pend);
390		}
391	}
392}
393
394int
395outnet_udp_cb(struct comm_point* c, void* arg, int error,
396	struct comm_reply *reply_info)
397{
398	struct outside_network* outnet = (struct outside_network*)arg;
399	struct pending key;
400	struct pending* p;
401	verbose(VERB_ALGO, "answer cb");
402
403	if(error != NETEVENT_NOERROR) {
404		verbose(VERB_QUERY, "outnetudp got udp error %d", error);
405		return 0;
406	}
407	if(sldns_buffer_limit(c->buffer) < LDNS_HEADER_SIZE) {
408		verbose(VERB_QUERY, "outnetudp udp too short");
409		return 0;
410	}
411	log_assert(reply_info);
412
413	/* setup lookup key */
414	key.id = (unsigned)LDNS_ID_WIRE(sldns_buffer_begin(c->buffer));
415	memcpy(&key.addr, &reply_info->addr, reply_info->addrlen);
416	key.addrlen = reply_info->addrlen;
417	verbose(VERB_ALGO, "Incoming reply id = %4.4x", key.id);
418	log_addr(VERB_ALGO, "Incoming reply addr =",
419		&reply_info->addr, reply_info->addrlen);
420
421	/* find it, see if this thing is a valid query response */
422	verbose(VERB_ALGO, "lookup size is %d entries", (int)outnet->pending->count);
423	p = (struct pending*)rbtree_search(outnet->pending, &key);
424	if(!p) {
425		verbose(VERB_QUERY, "received unwanted or unsolicited udp reply dropped.");
426		log_buf(VERB_ALGO, "dropped message", c->buffer);
427		outnet->unwanted_replies++;
428		if(outnet->unwanted_threshold && ++outnet->unwanted_total
429			>= outnet->unwanted_threshold) {
430			log_warn("unwanted reply total reached threshold (%u)"
431				" you may be under attack."
432				" defensive action: clearing the cache",
433				(unsigned)outnet->unwanted_threshold);
434			fptr_ok(fptr_whitelist_alloc_cleanup(
435				outnet->unwanted_action));
436			(*outnet->unwanted_action)(outnet->unwanted_param);
437			outnet->unwanted_total = 0;
438		}
439		return 0;
440	}
441
442	verbose(VERB_ALGO, "received udp reply.");
443	log_buf(VERB_ALGO, "udp message", c->buffer);
444	if(p->pc->cp != c) {
445		verbose(VERB_QUERY, "received reply id,addr on wrong port. "
446			"dropped.");
447		outnet->unwanted_replies++;
448		if(outnet->unwanted_threshold && ++outnet->unwanted_total
449			>= outnet->unwanted_threshold) {
450			log_warn("unwanted reply total reached threshold (%u)"
451				" you may be under attack."
452				" defensive action: clearing the cache",
453				(unsigned)outnet->unwanted_threshold);
454			fptr_ok(fptr_whitelist_alloc_cleanup(
455				outnet->unwanted_action));
456			(*outnet->unwanted_action)(outnet->unwanted_param);
457			outnet->unwanted_total = 0;
458		}
459		return 0;
460	}
461	comm_timer_disable(p->timer);
462	verbose(VERB_ALGO, "outnet handle udp reply");
463	/* delete from tree first in case callback creates a retry */
464	(void)rbtree_delete(outnet->pending, p->node.key);
465	if(p->cb) {
466		fptr_ok(fptr_whitelist_pending_udp(p->cb));
467		(void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_NOERROR, reply_info);
468	}
469	portcomm_loweruse(outnet, p->pc);
470	pending_delete(NULL, p);
471	outnet_send_wait_udp(outnet);
472	return 0;
473}
474
475/** calculate number of ip4 and ip6 interfaces*/
476static void
477calc_num46(char** ifs, int num_ifs, int do_ip4, int do_ip6,
478	int* num_ip4, int* num_ip6)
479{
480	int i;
481	*num_ip4 = 0;
482	*num_ip6 = 0;
483	if(num_ifs <= 0) {
484		if(do_ip4)
485			*num_ip4 = 1;
486		if(do_ip6)
487			*num_ip6 = 1;
488		return;
489	}
490	for(i=0; i<num_ifs; i++)
491	{
492		if(str_is_ip6(ifs[i])) {
493			if(do_ip6)
494				(*num_ip6)++;
495		} else {
496			if(do_ip4)
497				(*num_ip4)++;
498		}
499	}
500
501}
502
503void
504pending_udp_timer_delay_cb(void* arg)
505{
506	struct pending* p = (struct pending*)arg;
507	struct outside_network* outnet = p->outnet;
508	verbose(VERB_ALGO, "timeout udp with delay");
509	portcomm_loweruse(outnet, p->pc);
510	pending_delete(outnet, p);
511	outnet_send_wait_udp(outnet);
512}
513
514void
515pending_udp_timer_cb(void *arg)
516{
517	struct pending* p = (struct pending*)arg;
518	struct outside_network* outnet = p->outnet;
519	/* it timed out */
520	verbose(VERB_ALGO, "timeout udp");
521	if(p->cb) {
522		fptr_ok(fptr_whitelist_pending_udp(p->cb));
523		(void)(*p->cb)(p->pc->cp, p->cb_arg, NETEVENT_TIMEOUT, NULL);
524	}
525	/* if delayclose, keep port open for a longer time.
526	 * But if the udpwaitlist exists, then we are struggling to
527	 * keep up with demand for sockets, so do not wait, but service
528	 * the customer (customer service more important than portICMPs) */
529	if(outnet->delayclose && !outnet->udp_wait_first) {
530		p->cb = NULL;
531		p->timer->callback = &pending_udp_timer_delay_cb;
532		comm_timer_set(p->timer, &outnet->delay_tv);
533		return;
534	}
535	portcomm_loweruse(outnet, p->pc);
536	pending_delete(outnet, p);
537	outnet_send_wait_udp(outnet);
538}
539
540/** create pending_tcp buffers */
541static int
542create_pending_tcp(struct outside_network* outnet, size_t bufsize)
543{
544	size_t i;
545	if(outnet->num_tcp == 0)
546		return 1; /* no tcp needed, nothing to do */
547	if(!(outnet->tcp_conns = (struct pending_tcp **)calloc(
548			outnet->num_tcp, sizeof(struct pending_tcp*))))
549		return 0;
550	for(i=0; i<outnet->num_tcp; i++) {
551		if(!(outnet->tcp_conns[i] = (struct pending_tcp*)calloc(1,
552			sizeof(struct pending_tcp))))
553			return 0;
554		outnet->tcp_conns[i]->next_free = outnet->tcp_free;
555		outnet->tcp_free = outnet->tcp_conns[i];
556		outnet->tcp_conns[i]->c = comm_point_create_tcp_out(
557			outnet->base, bufsize, outnet_tcp_cb,
558			outnet->tcp_conns[i]);
559		if(!outnet->tcp_conns[i]->c)
560			return 0;
561	}
562	return 1;
563}
564
565/** setup an outgoing interface, ready address */
566static int setup_if(struct port_if* pif, const char* addrstr,
567	int* avail, int numavail, size_t numfd)
568{
569	pif->avail_total = numavail;
570	pif->avail_ports = (int*)memdup(avail, (size_t)numavail*sizeof(int));
571	if(!pif->avail_ports)
572		return 0;
573	if(!ipstrtoaddr(addrstr, UNBOUND_DNS_PORT, &pif->addr, &pif->addrlen))
574		return 0;
575	pif->maxout = (int)numfd;
576	pif->inuse = 0;
577	pif->out = (struct port_comm**)calloc(numfd,
578		sizeof(struct port_comm*));
579	if(!pif->out)
580		return 0;
581	return 1;
582}
583
584struct outside_network*
585outside_network_create(struct comm_base *base, size_t bufsize,
586	size_t num_ports, char** ifs, int num_ifs, int do_ip4,
587	int do_ip6, size_t num_tcp, struct infra_cache* infra,
588	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
589	int numavailports, size_t unwanted_threshold,
590	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
591	void* sslctx, int delayclose)
592{
593	struct outside_network* outnet = (struct outside_network*)
594		calloc(1, sizeof(struct outside_network));
595	size_t k;
596	if(!outnet) {
597		log_err("malloc failed");
598		return NULL;
599	}
600	comm_base_timept(base, &outnet->now_secs, &outnet->now_tv);
601	outnet->base = base;
602	outnet->num_tcp = num_tcp;
603	outnet->infra = infra;
604	outnet->rnd = rnd;
605	outnet->sslctx = sslctx;
606	outnet->svcd_overhead = 0;
607	outnet->want_to_quit = 0;
608	outnet->unwanted_threshold = unwanted_threshold;
609	outnet->unwanted_action = unwanted_action;
610	outnet->unwanted_param = unwanted_param;
611	outnet->use_caps_for_id = use_caps_for_id;
612	outnet->do_udp = do_udp;
613#ifndef S_SPLINT_S
614	if(delayclose) {
615		outnet->delayclose = 1;
616		outnet->delay_tv.tv_sec = delayclose/1000;
617		outnet->delay_tv.tv_usec = (delayclose%1000)*1000;
618	}
619#endif
620	if(numavailports == 0) {
621		log_err("no outgoing ports available");
622		outside_network_delete(outnet);
623		return NULL;
624	}
625#ifndef INET6
626	do_ip6 = 0;
627#endif
628	calc_num46(ifs, num_ifs, do_ip4, do_ip6,
629		&outnet->num_ip4, &outnet->num_ip6);
630	if(outnet->num_ip4 != 0) {
631		if(!(outnet->ip4_ifs = (struct port_if*)calloc(
632			(size_t)outnet->num_ip4, sizeof(struct port_if)))) {
633			log_err("malloc failed");
634			outside_network_delete(outnet);
635			return NULL;
636		}
637	}
638	if(outnet->num_ip6 != 0) {
639		if(!(outnet->ip6_ifs = (struct port_if*)calloc(
640			(size_t)outnet->num_ip6, sizeof(struct port_if)))) {
641			log_err("malloc failed");
642			outside_network_delete(outnet);
643			return NULL;
644		}
645	}
646	if(	!(outnet->udp_buff = sldns_buffer_new(bufsize)) ||
647		!(outnet->pending = rbtree_create(pending_cmp)) ||
648		!(outnet->serviced = rbtree_create(serviced_cmp)) ||
649		!create_pending_tcp(outnet, bufsize)) {
650		log_err("malloc failed");
651		outside_network_delete(outnet);
652		return NULL;
653	}
654
655	/* allocate commpoints */
656	for(k=0; k<num_ports; k++) {
657		struct port_comm* pc;
658		pc = (struct port_comm*)calloc(1, sizeof(*pc));
659		if(!pc) {
660			log_err("malloc failed");
661			outside_network_delete(outnet);
662			return NULL;
663		}
664		pc->cp = comm_point_create_udp(outnet->base, -1,
665			outnet->udp_buff, outnet_udp_cb, outnet);
666		if(!pc->cp) {
667			log_err("malloc failed");
668			free(pc);
669			outside_network_delete(outnet);
670			return NULL;
671		}
672		pc->next = outnet->unused_fds;
673		outnet->unused_fds = pc;
674	}
675
676	/* allocate interfaces */
677	if(num_ifs == 0) {
678		if(do_ip4 && !setup_if(&outnet->ip4_ifs[0], "0.0.0.0",
679			availports, numavailports, num_ports)) {
680			log_err("malloc failed");
681			outside_network_delete(outnet);
682			return NULL;
683		}
684		if(do_ip6 && !setup_if(&outnet->ip6_ifs[0], "::",
685			availports, numavailports, num_ports)) {
686			log_err("malloc failed");
687			outside_network_delete(outnet);
688			return NULL;
689		}
690	} else {
691		size_t done_4 = 0, done_6 = 0;
692		int i;
693		for(i=0; i<num_ifs; i++) {
694			if(str_is_ip6(ifs[i]) && do_ip6) {
695				if(!setup_if(&outnet->ip6_ifs[done_6], ifs[i],
696					availports, numavailports, num_ports)){
697					log_err("malloc failed");
698					outside_network_delete(outnet);
699					return NULL;
700				}
701				done_6++;
702			}
703			if(!str_is_ip6(ifs[i]) && do_ip4) {
704				if(!setup_if(&outnet->ip4_ifs[done_4], ifs[i],
705					availports, numavailports, num_ports)){
706					log_err("malloc failed");
707					outside_network_delete(outnet);
708					return NULL;
709				}
710				done_4++;
711			}
712		}
713	}
714	return outnet;
715}
716
717/** helper pending delete */
718static void
719pending_node_del(rbnode_t* node, void* arg)
720{
721	struct pending* pend = (struct pending*)node;
722	struct outside_network* outnet = (struct outside_network*)arg;
723	pending_delete(outnet, pend);
724}
725
726/** helper serviced delete */
727static void
728serviced_node_del(rbnode_t* node, void* ATTR_UNUSED(arg))
729{
730	struct serviced_query* sq = (struct serviced_query*)node;
731	struct service_callback* p = sq->cblist, *np;
732	free(sq->qbuf);
733	free(sq->zone);
734	while(p) {
735		np = p->next;
736		free(p);
737		p = np;
738	}
739	free(sq);
740}
741
742void
743outside_network_quit_prepare(struct outside_network* outnet)
744{
745	if(!outnet)
746		return;
747	/* prevent queued items from being sent */
748	outnet->want_to_quit = 1;
749}
750
751void
752outside_network_delete(struct outside_network* outnet)
753{
754	if(!outnet)
755		return;
756	outnet->want_to_quit = 1;
757	/* check every element, since we can be called on malloc error */
758	if(outnet->pending) {
759		/* free pending elements, but do no unlink from tree. */
760		traverse_postorder(outnet->pending, pending_node_del, NULL);
761		free(outnet->pending);
762	}
763	if(outnet->serviced) {
764		traverse_postorder(outnet->serviced, serviced_node_del, NULL);
765		free(outnet->serviced);
766	}
767	if(outnet->udp_buff)
768		sldns_buffer_free(outnet->udp_buff);
769	if(outnet->unused_fds) {
770		struct port_comm* p = outnet->unused_fds, *np;
771		while(p) {
772			np = p->next;
773			comm_point_delete(p->cp);
774			free(p);
775			p = np;
776		}
777		outnet->unused_fds = NULL;
778	}
779	if(outnet->ip4_ifs) {
780		int i, k;
781		for(i=0; i<outnet->num_ip4; i++) {
782			for(k=0; k<outnet->ip4_ifs[i].inuse; k++) {
783				struct port_comm* pc = outnet->ip4_ifs[i].
784					out[k];
785				comm_point_delete(pc->cp);
786				free(pc);
787			}
788			free(outnet->ip4_ifs[i].avail_ports);
789			free(outnet->ip4_ifs[i].out);
790		}
791		free(outnet->ip4_ifs);
792	}
793	if(outnet->ip6_ifs) {
794		int i, k;
795		for(i=0; i<outnet->num_ip6; i++) {
796			for(k=0; k<outnet->ip6_ifs[i].inuse; k++) {
797				struct port_comm* pc = outnet->ip6_ifs[i].
798					out[k];
799				comm_point_delete(pc->cp);
800				free(pc);
801			}
802			free(outnet->ip6_ifs[i].avail_ports);
803			free(outnet->ip6_ifs[i].out);
804		}
805		free(outnet->ip6_ifs);
806	}
807	if(outnet->tcp_conns) {
808		size_t i;
809		for(i=0; i<outnet->num_tcp; i++)
810			if(outnet->tcp_conns[i]) {
811				comm_point_delete(outnet->tcp_conns[i]->c);
812				waiting_tcp_delete(outnet->tcp_conns[i]->query);
813				free(outnet->tcp_conns[i]);
814			}
815		free(outnet->tcp_conns);
816	}
817	if(outnet->tcp_wait_first) {
818		struct waiting_tcp* p = outnet->tcp_wait_first, *np;
819		while(p) {
820			np = p->next_waiting;
821			waiting_tcp_delete(p);
822			p = np;
823		}
824	}
825	if(outnet->udp_wait_first) {
826		struct pending* p = outnet->udp_wait_first, *np;
827		while(p) {
828			np = p->next_waiting;
829			pending_delete(NULL, p);
830			p = np;
831		}
832	}
833	free(outnet);
834}
835
836void
837pending_delete(struct outside_network* outnet, struct pending* p)
838{
839	if(!p)
840		return;
841	if(outnet && outnet->udp_wait_first &&
842		(p->next_waiting || p == outnet->udp_wait_last) ) {
843		/* delete from waiting list, if it is in the waiting list */
844		struct pending* prev = NULL, *x = outnet->udp_wait_first;
845		while(x && x != p) {
846			prev = x;
847			x = x->next_waiting;
848		}
849		if(x) {
850			log_assert(x == p);
851			if(prev)
852				prev->next_waiting = p->next_waiting;
853			else	outnet->udp_wait_first = p->next_waiting;
854			if(outnet->udp_wait_last == p)
855				outnet->udp_wait_last = prev;
856		}
857	}
858	if(outnet) {
859		(void)rbtree_delete(outnet->pending, p->node.key);
860	}
861	if(p->timer)
862		comm_timer_delete(p->timer);
863	free(p->pkt);
864	free(p);
865}
866
867/**
868 * Try to open a UDP socket for outgoing communication.
869 * Sets sockets options as needed.
870 * @param addr: socket address.
871 * @param addrlen: length of address.
872 * @param port: port override for addr.
873 * @param inuse: if -1 is returned, this bool means the port was in use.
874 * @return fd or -1
875 */
876static int
877udp_sockport(struct sockaddr_storage* addr, socklen_t addrlen, int port,
878	int* inuse)
879{
880	int fd, noproto;
881	if(addr_is_ip6(addr, addrlen)) {
882		struct sockaddr_in6* sa = (struct sockaddr_in6*)addr;
883		sa->sin6_port = (in_port_t)htons((uint16_t)port);
884		fd = create_udp_sock(AF_INET6, SOCK_DGRAM,
885			(struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
886			0, 0, 0, NULL);
887	} else {
888		struct sockaddr_in* sa = (struct sockaddr_in*)addr;
889		sa->sin_port = (in_port_t)htons((uint16_t)port);
890		fd = create_udp_sock(AF_INET, SOCK_DGRAM,
891			(struct sockaddr*)addr, addrlen, 1, inuse, &noproto,
892			0, 0, 0, NULL);
893	}
894	return fd;
895}
896
897/** Select random ID */
898static int
899select_id(struct outside_network* outnet, struct pending* pend,
900	sldns_buffer* packet)
901{
902	int id_tries = 0;
903	pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
904	LDNS_ID_SET(sldns_buffer_begin(packet), pend->id);
905
906	/* insert in tree */
907	pend->node.key = pend;
908	while(!rbtree_insert(outnet->pending, &pend->node)) {
909		/* change ID to avoid collision */
910		pend->id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
911		LDNS_ID_SET(sldns_buffer_begin(packet), pend->id);
912		id_tries++;
913		if(id_tries == MAX_ID_RETRY) {
914			pend->id=99999; /* non existant ID */
915			log_err("failed to generate unique ID, drop msg");
916			return 0;
917		}
918	}
919	verbose(VERB_ALGO, "inserted new pending reply id=%4.4x", pend->id);
920	return 1;
921}
922
923/** Select random interface and port */
924static int
925select_ifport(struct outside_network* outnet, struct pending* pend,
926	int num_if, struct port_if* ifs)
927{
928	int my_if, my_port, fd, portno, inuse, tries=0;
929	struct port_if* pif;
930	/* randomly select interface and port */
931	if(num_if == 0) {
932		verbose(VERB_QUERY, "Need to send query but have no "
933			"outgoing interfaces of that family");
934		return 0;
935	}
936	log_assert(outnet->unused_fds);
937	tries = 0;
938	while(1) {
939		my_if = ub_random_max(outnet->rnd, num_if);
940		pif = &ifs[my_if];
941		my_port = ub_random_max(outnet->rnd, pif->avail_total);
942		if(my_port < pif->inuse) {
943			/* port already open */
944			pend->pc = pif->out[my_port];
945			verbose(VERB_ALGO, "using UDP if=%d port=%d",
946				my_if, pend->pc->number);
947			break;
948		}
949		/* try to open new port, if fails, loop to try again */
950		log_assert(pif->inuse < pif->maxout);
951		portno = pif->avail_ports[my_port - pif->inuse];
952		fd = udp_sockport(&pif->addr, pif->addrlen, portno, &inuse);
953		if(fd == -1 && !inuse) {
954			/* nonrecoverable error making socket */
955			return 0;
956		}
957		if(fd != -1) {
958			verbose(VERB_ALGO, "opened UDP if=%d port=%d",
959				my_if, portno);
960			/* grab fd */
961			pend->pc = outnet->unused_fds;
962			outnet->unused_fds = pend->pc->next;
963
964			/* setup portcomm */
965			pend->pc->next = NULL;
966			pend->pc->number = portno;
967			pend->pc->pif = pif;
968			pend->pc->index = pif->inuse;
969			pend->pc->num_outstanding = 0;
970			comm_point_start_listening(pend->pc->cp, fd, -1);
971
972			/* grab port in interface */
973			pif->out[pif->inuse] = pend->pc;
974			pif->avail_ports[my_port - pif->inuse] =
975				pif->avail_ports[pif->avail_total-pif->inuse-1];
976			pif->inuse++;
977			break;
978		}
979		/* failed, already in use */
980		verbose(VERB_QUERY, "port %d in use, trying another", portno);
981		tries++;
982		if(tries == MAX_PORT_RETRY) {
983			log_err("failed to find an open port, drop msg");
984			return 0;
985		}
986	}
987	log_assert(pend->pc);
988	pend->pc->num_outstanding++;
989
990	return 1;
991}
992
993static int
994randomize_and_send_udp(struct outside_network* outnet, struct pending* pend,
995	sldns_buffer* packet, int timeout)
996{
997	struct timeval tv;
998
999	/* select id */
1000	if(!select_id(outnet, pend, packet)) {
1001		return 0;
1002	}
1003
1004	/* select src_if, port */
1005	if(addr_is_ip6(&pend->addr, pend->addrlen)) {
1006		if(!select_ifport(outnet, pend,
1007			outnet->num_ip6, outnet->ip6_ifs))
1008			return 0;
1009	} else {
1010		if(!select_ifport(outnet, pend,
1011			outnet->num_ip4, outnet->ip4_ifs))
1012			return 0;
1013	}
1014	log_assert(pend->pc && pend->pc->cp);
1015
1016	/* send it over the commlink */
1017	if(!comm_point_send_udp_msg(pend->pc->cp, packet,
1018		(struct sockaddr*)&pend->addr, pend->addrlen)) {
1019		portcomm_loweruse(outnet, pend->pc);
1020		return 0;
1021	}
1022
1023	/* system calls to set timeout after sending UDP to make roundtrip
1024	   smaller. */
1025#ifndef S_SPLINT_S
1026	tv.tv_sec = timeout/1000;
1027	tv.tv_usec = (timeout%1000)*1000;
1028#endif
1029	comm_timer_set(pend->timer, &tv);
1030	return 1;
1031}
1032
1033struct pending*
1034pending_udp_query(struct outside_network* outnet, sldns_buffer* packet,
1035	struct sockaddr_storage* addr, socklen_t addrlen, int timeout,
1036	comm_point_callback_t* cb, void* cb_arg)
1037{
1038	struct pending* pend = (struct pending*)calloc(1, sizeof(*pend));
1039	if(!pend) return NULL;
1040	pend->outnet = outnet;
1041	pend->addrlen = addrlen;
1042	memmove(&pend->addr, addr, addrlen);
1043	pend->cb = cb;
1044	pend->cb_arg = cb_arg;
1045	pend->node.key = pend;
1046	pend->timer = comm_timer_create(outnet->base, pending_udp_timer_cb,
1047		pend);
1048	if(!pend->timer) {
1049		free(pend);
1050		return NULL;
1051	}
1052
1053	if(outnet->unused_fds == NULL) {
1054		/* no unused fd, cannot create a new port (randomly) */
1055		verbose(VERB_ALGO, "no fds available, udp query waiting");
1056		pend->timeout = timeout;
1057		pend->pkt_len = sldns_buffer_limit(packet);
1058		pend->pkt = (uint8_t*)memdup(sldns_buffer_begin(packet),
1059			pend->pkt_len);
1060		if(!pend->pkt) {
1061			comm_timer_delete(pend->timer);
1062			free(pend);
1063			return NULL;
1064		}
1065		/* put at end of waiting list */
1066		if(outnet->udp_wait_last)
1067			outnet->udp_wait_last->next_waiting = pend;
1068		else
1069			outnet->udp_wait_first = pend;
1070		outnet->udp_wait_last = pend;
1071		return pend;
1072	}
1073	if(!randomize_and_send_udp(outnet, pend, packet, timeout)) {
1074		pending_delete(outnet, pend);
1075		return NULL;
1076	}
1077	return pend;
1078}
1079
1080void
1081outnet_tcptimer(void* arg)
1082{
1083	struct waiting_tcp* w = (struct waiting_tcp*)arg;
1084	struct outside_network* outnet = w->outnet;
1085	comm_point_callback_t* cb;
1086	void* cb_arg;
1087	if(w->pkt) {
1088		/* it is on the waiting list */
1089		struct waiting_tcp* p=outnet->tcp_wait_first, *prev=NULL;
1090		while(p) {
1091			if(p == w) {
1092				if(prev) prev->next_waiting = w->next_waiting;
1093				else	outnet->tcp_wait_first=w->next_waiting;
1094				outnet->tcp_wait_last = prev;
1095				break;
1096			}
1097			prev = p;
1098			p=p->next_waiting;
1099		}
1100	} else {
1101		/* it was in use */
1102		struct pending_tcp* pend=(struct pending_tcp*)w->next_waiting;
1103		comm_point_close(pend->c);
1104		pend->query = NULL;
1105		pend->next_free = outnet->tcp_free;
1106		outnet->tcp_free = pend;
1107	}
1108	cb = w->cb;
1109	cb_arg = w->cb_arg;
1110	waiting_tcp_delete(w);
1111	fptr_ok(fptr_whitelist_pending_tcp(cb));
1112	(void)(*cb)(NULL, cb_arg, NETEVENT_TIMEOUT, NULL);
1113	use_free_buffer(outnet);
1114}
1115
1116struct waiting_tcp*
1117pending_tcp_query(struct outside_network* outnet, sldns_buffer* packet,
1118	struct sockaddr_storage* addr, socklen_t addrlen, int timeout,
1119	comm_point_callback_t* callback, void* callback_arg, int ssl_upstream)
1120{
1121	struct pending_tcp* pend = outnet->tcp_free;
1122	struct waiting_tcp* w;
1123	struct timeval tv;
1124	uint16_t id;
1125	/* if no buffer is free allocate space to store query */
1126	w = (struct waiting_tcp*)malloc(sizeof(struct waiting_tcp)
1127		+ (pend?0:sldns_buffer_limit(packet)));
1128	if(!w) {
1129		return NULL;
1130	}
1131	if(!(w->timer = comm_timer_create(outnet->base, outnet_tcptimer, w))) {
1132		free(w);
1133		return NULL;
1134	}
1135	w->pkt = NULL;
1136	w->pkt_len = 0;
1137	id = ((unsigned)ub_random(outnet->rnd)>>8) & 0xffff;
1138	LDNS_ID_SET(sldns_buffer_begin(packet), id);
1139	memcpy(&w->addr, addr, addrlen);
1140	w->addrlen = addrlen;
1141	w->outnet = outnet;
1142	w->cb = callback;
1143	w->cb_arg = callback_arg;
1144	w->ssl_upstream = ssl_upstream;
1145#ifndef S_SPLINT_S
1146	tv.tv_sec = timeout;
1147	tv.tv_usec = 0;
1148#endif
1149	comm_timer_set(w->timer, &tv);
1150	if(pend) {
1151		/* we have a buffer available right now */
1152		if(!outnet_tcp_take_into_use(w, sldns_buffer_begin(packet),
1153			sldns_buffer_limit(packet))) {
1154			waiting_tcp_delete(w);
1155			return NULL;
1156		}
1157	} else {
1158		/* queue up */
1159		w->pkt = (uint8_t*)w + sizeof(struct waiting_tcp);
1160		w->pkt_len = sldns_buffer_limit(packet);
1161		memmove(w->pkt, sldns_buffer_begin(packet), w->pkt_len);
1162		w->next_waiting = NULL;
1163		if(outnet->tcp_wait_last)
1164			outnet->tcp_wait_last->next_waiting = w;
1165		else	outnet->tcp_wait_first = w;
1166		outnet->tcp_wait_last = w;
1167	}
1168	return w;
1169}
1170
1171/** create query for serviced queries */
1172static void
1173serviced_gen_query(sldns_buffer* buff, uint8_t* qname, size_t qnamelen,
1174	uint16_t qtype, uint16_t qclass, uint16_t flags)
1175{
1176	sldns_buffer_clear(buff);
1177	/* skip id */
1178	sldns_buffer_write_u16(buff, flags);
1179	sldns_buffer_write_u16(buff, 1); /* qdcount */
1180	sldns_buffer_write_u16(buff, 0); /* ancount */
1181	sldns_buffer_write_u16(buff, 0); /* nscount */
1182	sldns_buffer_write_u16(buff, 0); /* arcount */
1183	sldns_buffer_write(buff, qname, qnamelen);
1184	sldns_buffer_write_u16(buff, qtype);
1185	sldns_buffer_write_u16(buff, qclass);
1186	sldns_buffer_flip(buff);
1187}
1188
1189/** lookup serviced query in serviced query rbtree */
1190static struct serviced_query*
1191lookup_serviced(struct outside_network* outnet, sldns_buffer* buff, int dnssec,
1192	struct sockaddr_storage* addr, socklen_t addrlen)
1193{
1194	struct serviced_query key;
1195	key.node.key = &key;
1196	key.qbuf = sldns_buffer_begin(buff);
1197	key.qbuflen = sldns_buffer_limit(buff);
1198	key.dnssec = dnssec;
1199	memcpy(&key.addr, addr, addrlen);
1200	key.addrlen = addrlen;
1201	key.outnet = outnet;
1202	return (struct serviced_query*)rbtree_search(outnet->serviced, &key);
1203}
1204
1205/** Create new serviced entry */
1206static struct serviced_query*
1207serviced_create(struct outside_network* outnet, sldns_buffer* buff, int dnssec,
1208	int want_dnssec, int tcp_upstream, int ssl_upstream,
1209	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
1210	size_t zonelen, int qtype)
1211{
1212	struct serviced_query* sq = (struct serviced_query*)malloc(sizeof(*sq));
1213#ifdef UNBOUND_DEBUG
1214	rbnode_t* ins;
1215#endif
1216	if(!sq)
1217		return NULL;
1218	sq->node.key = sq;
1219	sq->qbuf = memdup(sldns_buffer_begin(buff), sldns_buffer_limit(buff));
1220	if(!sq->qbuf) {
1221		free(sq);
1222		return NULL;
1223	}
1224	sq->qbuflen = sldns_buffer_limit(buff);
1225	sq->zone = memdup(zone, zonelen);
1226	if(!sq->zone) {
1227		free(sq->qbuf);
1228		free(sq);
1229		return NULL;
1230	}
1231	sq->zonelen = zonelen;
1232	sq->qtype = qtype;
1233	sq->dnssec = dnssec;
1234	sq->want_dnssec = want_dnssec;
1235	sq->tcp_upstream = tcp_upstream;
1236	sq->ssl_upstream = ssl_upstream;
1237	memcpy(&sq->addr, addr, addrlen);
1238	sq->addrlen = addrlen;
1239	sq->outnet = outnet;
1240	sq->cblist = NULL;
1241	sq->pending = NULL;
1242	sq->status = serviced_initial;
1243	sq->retry = 0;
1244	sq->to_be_deleted = 0;
1245#ifdef UNBOUND_DEBUG
1246	ins =
1247#else
1248	(void)
1249#endif
1250	rbtree_insert(outnet->serviced, &sq->node);
1251	log_assert(ins != NULL); /* must not be already present */
1252	return sq;
1253}
1254
1255/** remove waiting tcp from the outnet waiting list */
1256static void
1257waiting_list_remove(struct outside_network* outnet, struct waiting_tcp* w)
1258{
1259	struct waiting_tcp* p = outnet->tcp_wait_first, *prev = NULL;
1260	while(p) {
1261		if(p == w) {
1262			/* remove w */
1263			if(prev)
1264				prev->next_waiting = w->next_waiting;
1265			else	outnet->tcp_wait_first = w->next_waiting;
1266			if(outnet->tcp_wait_last == w)
1267				outnet->tcp_wait_last = prev;
1268			return;
1269		}
1270		prev = p;
1271		p = p->next_waiting;
1272	}
1273}
1274
1275/** cleanup serviced query entry */
1276static void
1277serviced_delete(struct serviced_query* sq)
1278{
1279	if(sq->pending) {
1280		/* clear up the pending query */
1281		if(sq->status == serviced_query_UDP_EDNS ||
1282			sq->status == serviced_query_UDP ||
1283			sq->status == serviced_query_PROBE_EDNS ||
1284			sq->status == serviced_query_UDP_EDNS_FRAG ||
1285			sq->status == serviced_query_UDP_EDNS_fallback) {
1286			struct pending* p = (struct pending*)sq->pending;
1287			if(p->pc)
1288				portcomm_loweruse(sq->outnet, p->pc);
1289			pending_delete(sq->outnet, p);
1290			/* this call can cause reentrant calls back into the
1291			 * mesh */
1292			outnet_send_wait_udp(sq->outnet);
1293		} else {
1294			struct waiting_tcp* p = (struct waiting_tcp*)
1295				sq->pending;
1296			if(p->pkt == NULL) {
1297				decomission_pending_tcp(sq->outnet,
1298					(struct pending_tcp*)p->next_waiting);
1299			} else {
1300				waiting_list_remove(sq->outnet, p);
1301				waiting_tcp_delete(p);
1302			}
1303		}
1304	}
1305	/* does not delete from tree, caller has to do that */
1306	serviced_node_del(&sq->node, NULL);
1307}
1308
1309/** perturb a dname capitalization randomly */
1310static void
1311serviced_perturb_qname(struct ub_randstate* rnd, uint8_t* qbuf, size_t len)
1312{
1313	uint8_t lablen;
1314	uint8_t* d = qbuf + 10;
1315	long int random = 0;
1316	int bits = 0;
1317	log_assert(len >= 10 + 5 /* offset qname, root, qtype, qclass */);
1318	lablen = *d++;
1319	while(lablen) {
1320		while(lablen--) {
1321			/* only perturb A-Z, a-z */
1322			if(isalpha((int)*d)) {
1323				/* get a random bit */
1324				if(bits == 0) {
1325					random = ub_random(rnd);
1326					bits = 30;
1327				}
1328				if(random & 0x1) {
1329					*d = (uint8_t)toupper((int)*d);
1330				} else {
1331					*d = (uint8_t)tolower((int)*d);
1332				}
1333				random >>= 1;
1334				bits--;
1335			}
1336			d++;
1337		}
1338		lablen = *d++;
1339	}
1340	if(verbosity >= VERB_ALGO) {
1341		char buf[LDNS_MAX_DOMAINLEN+1];
1342		dname_str(qbuf+10, buf);
1343		verbose(VERB_ALGO, "qname perturbed to %s", buf);
1344	}
1345}
1346
1347/** put serviced query into a buffer */
1348static void
1349serviced_encode(struct serviced_query* sq, sldns_buffer* buff, int with_edns)
1350{
1351	/* if we are using 0x20 bits for ID randomness, perturb them */
1352	if(sq->outnet->use_caps_for_id) {
1353		serviced_perturb_qname(sq->outnet->rnd, sq->qbuf, sq->qbuflen);
1354	}
1355	/* generate query */
1356	sldns_buffer_clear(buff);
1357	sldns_buffer_write_u16(buff, 0); /* id placeholder */
1358	sldns_buffer_write(buff, sq->qbuf, sq->qbuflen);
1359	sldns_buffer_flip(buff);
1360	if(with_edns) {
1361		/* add edns section */
1362		struct edns_data edns;
1363		edns.edns_present = 1;
1364		edns.ext_rcode = 0;
1365		edns.edns_version = EDNS_ADVERTISED_VERSION;
1366		if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1367			if(addr_is_ip6(&sq->addr, sq->addrlen)) {
1368				if(EDNS_FRAG_SIZE_IP6 < EDNS_ADVERTISED_SIZE)
1369					edns.udp_size = EDNS_FRAG_SIZE_IP6;
1370				else	edns.udp_size = EDNS_ADVERTISED_SIZE;
1371			} else {
1372				if(EDNS_FRAG_SIZE_IP4 < EDNS_ADVERTISED_SIZE)
1373					edns.udp_size = EDNS_FRAG_SIZE_IP4;
1374				else	edns.udp_size = EDNS_ADVERTISED_SIZE;
1375			}
1376		} else {
1377			edns.udp_size = EDNS_ADVERTISED_SIZE;
1378		}
1379		edns.bits = 0;
1380		if(sq->dnssec & EDNS_DO)
1381			edns.bits = EDNS_DO;
1382		if(sq->dnssec & BIT_CD)
1383			LDNS_CD_SET(sldns_buffer_begin(buff));
1384		attach_edns_record(buff, &edns);
1385	}
1386}
1387
1388/**
1389 * Perform serviced query UDP sending operation.
1390 * Sends UDP with EDNS, unless infra host marked non EDNS.
1391 * @param sq: query to send.
1392 * @param buff: buffer scratch space.
1393 * @return 0 on error.
1394 */
1395static int
1396serviced_udp_send(struct serviced_query* sq, sldns_buffer* buff)
1397{
1398	int rtt, vs;
1399	uint8_t edns_lame_known;
1400	time_t now = *sq->outnet->now_secs;
1401
1402	if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1403		sq->zonelen, now, &vs, &edns_lame_known, &rtt))
1404		return 0;
1405	sq->last_rtt = rtt;
1406	verbose(VERB_ALGO, "EDNS lookup known=%d vs=%d", edns_lame_known, vs);
1407	if(sq->status == serviced_initial) {
1408		if(edns_lame_known == 0 && rtt > 5000 && rtt < 10001) {
1409			/* perform EDNS lame probe - check if server is
1410			 * EDNS lame (EDNS queries to it are dropped) */
1411			verbose(VERB_ALGO, "serviced query: send probe to see "
1412				" if use of EDNS causes timeouts");
1413			/* even 700 msec may be too small */
1414			rtt = 1000;
1415			sq->status = serviced_query_PROBE_EDNS;
1416		} else if(vs != -1) {
1417			sq->status = serviced_query_UDP_EDNS;
1418		} else {
1419			sq->status = serviced_query_UDP;
1420		}
1421	}
1422	serviced_encode(sq, buff, (sq->status == serviced_query_UDP_EDNS) ||
1423		(sq->status == serviced_query_UDP_EDNS_FRAG));
1424	sq->last_sent_time = *sq->outnet->now_tv;
1425	sq->edns_lame_known = (int)edns_lame_known;
1426	verbose(VERB_ALGO, "serviced query UDP timeout=%d msec", rtt);
1427	sq->pending = pending_udp_query(sq->outnet, buff, &sq->addr,
1428		sq->addrlen, rtt, serviced_udp_callback, sq);
1429	if(!sq->pending)
1430		return 0;
1431	return 1;
1432}
1433
1434/** check that perturbed qname is identical */
1435static int
1436serviced_check_qname(sldns_buffer* pkt, uint8_t* qbuf, size_t qbuflen)
1437{
1438	uint8_t* d1 = sldns_buffer_at(pkt, 12);
1439	uint8_t* d2 = qbuf+10;
1440	uint8_t len1, len2;
1441	int count = 0;
1442	log_assert(qbuflen >= 15 /* 10 header, root, type, class */);
1443	len1 = *d1++;
1444	len2 = *d2++;
1445	if(sldns_buffer_limit(pkt) < 12+1+4) /* packet too small for qname */
1446		return 0;
1447	while(len1 != 0 || len2 != 0) {
1448		if(LABEL_IS_PTR(len1)) {
1449			d1 = sldns_buffer_at(pkt, PTR_OFFSET(len1, *d1));
1450			if(d1 >= sldns_buffer_at(pkt, sldns_buffer_limit(pkt)))
1451				return 0;
1452			len1 = *d1++;
1453			if(count++ > MAX_COMPRESS_PTRS)
1454				return 0;
1455			continue;
1456		}
1457		if(d2 > qbuf+qbuflen)
1458			return 0;
1459		if(len1 != len2)
1460			return 0;
1461		if(len1 > LDNS_MAX_LABELLEN)
1462			return 0;
1463		log_assert(len1 <= LDNS_MAX_LABELLEN);
1464		log_assert(len2 <= LDNS_MAX_LABELLEN);
1465		log_assert(len1 == len2 && len1 != 0);
1466		/* compare the labels - bitwise identical */
1467		if(memcmp(d1, d2, len1) != 0)
1468			return 0;
1469		d1 += len1;
1470		d2 += len2;
1471		len1 = *d1++;
1472		len2 = *d2++;
1473	}
1474	return 1;
1475}
1476
1477/** call the callbacks for a serviced query */
1478static void
1479serviced_callbacks(struct serviced_query* sq, int error, struct comm_point* c,
1480	struct comm_reply* rep)
1481{
1482	struct service_callback* p;
1483	int dobackup = (sq->cblist && sq->cblist->next); /* >1 cb*/
1484	uint8_t *backup_p = NULL;
1485	size_t backlen = 0;
1486#ifdef UNBOUND_DEBUG
1487	rbnode_t* rem =
1488#else
1489	(void)
1490#endif
1491	/* remove from tree, and schedule for deletion, so that callbacks
1492	 * can safely deregister themselves and even create new serviced
1493	 * queries that are identical to this one. */
1494	rbtree_delete(sq->outnet->serviced, sq);
1495	log_assert(rem); /* should have been present */
1496	sq->to_be_deleted = 1;
1497	verbose(VERB_ALGO, "svcd callbacks start");
1498	if(sq->outnet->use_caps_for_id && error == NETEVENT_NOERROR && c) {
1499		/* noerror and nxdomain must have a qname in reply */
1500		if(sldns_buffer_read_u16_at(c->buffer, 4) == 0 &&
1501			(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1502				== LDNS_RCODE_NOERROR ||
1503			 LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1504				== LDNS_RCODE_NXDOMAIN)) {
1505			verbose(VERB_DETAIL, "no qname in reply to check 0x20ID");
1506			log_addr(VERB_DETAIL, "from server",
1507				&sq->addr, sq->addrlen);
1508			log_buf(VERB_DETAIL, "for packet", c->buffer);
1509			error = NETEVENT_CLOSED;
1510			c = NULL;
1511		} else if(sldns_buffer_read_u16_at(c->buffer, 4) > 0 &&
1512			!serviced_check_qname(c->buffer, sq->qbuf,
1513			sq->qbuflen)) {
1514			verbose(VERB_DETAIL, "wrong 0x20-ID in reply qname");
1515			log_addr(VERB_DETAIL, "from server",
1516				&sq->addr, sq->addrlen);
1517			log_buf(VERB_DETAIL, "for packet", c->buffer);
1518			error = NETEVENT_CAPSFAIL;
1519			/* and cleanup too */
1520			pkt_dname_tolower(c->buffer,
1521				sldns_buffer_at(c->buffer, 12));
1522		} else {
1523			verbose(VERB_ALGO, "good 0x20-ID in reply qname");
1524			/* cleanup caps, prettier cache contents. */
1525			pkt_dname_tolower(c->buffer,
1526				sldns_buffer_at(c->buffer, 12));
1527		}
1528	}
1529	if(dobackup && c) {
1530		/* make a backup of the query, since the querystate processing
1531		 * may send outgoing queries that overwrite the buffer.
1532		 * use secondary buffer to store the query.
1533		 * This is a data copy, but faster than packet to server */
1534		backlen = sldns_buffer_limit(c->buffer);
1535		backup_p = memdup(sldns_buffer_begin(c->buffer), backlen);
1536		if(!backup_p) {
1537			log_err("malloc failure in serviced query callbacks");
1538			error = NETEVENT_CLOSED;
1539			c = NULL;
1540		}
1541		sq->outnet->svcd_overhead = backlen;
1542	}
1543	/* test the actual sq->cblist, because the next elem could be deleted*/
1544	while((p=sq->cblist) != NULL) {
1545		sq->cblist = p->next; /* remove this element */
1546		if(dobackup && c) {
1547			sldns_buffer_clear(c->buffer);
1548			sldns_buffer_write(c->buffer, backup_p, backlen);
1549			sldns_buffer_flip(c->buffer);
1550		}
1551		fptr_ok(fptr_whitelist_serviced_query(p->cb));
1552		(void)(*p->cb)(c, p->cb_arg, error, rep);
1553		free(p);
1554	}
1555	if(backup_p) {
1556		free(backup_p);
1557		sq->outnet->svcd_overhead = 0;
1558	}
1559	verbose(VERB_ALGO, "svcd callbacks end");
1560	log_assert(sq->cblist == NULL);
1561	serviced_delete(sq);
1562}
1563
1564int
1565serviced_tcp_callback(struct comm_point* c, void* arg, int error,
1566        struct comm_reply* rep)
1567{
1568	struct serviced_query* sq = (struct serviced_query*)arg;
1569	struct comm_reply r2;
1570	sq->pending = NULL; /* removed after this callback */
1571	if(error != NETEVENT_NOERROR)
1572		log_addr(VERB_QUERY, "tcp error for address",
1573			&sq->addr, sq->addrlen);
1574	if(error==NETEVENT_NOERROR)
1575		infra_update_tcp_works(sq->outnet->infra, &sq->addr,
1576			sq->addrlen, sq->zone, sq->zonelen);
1577	if(error==NETEVENT_NOERROR && sq->status == serviced_query_TCP_EDNS &&
1578		(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1579		LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(sldns_buffer_begin(
1580		c->buffer)) == LDNS_RCODE_NOTIMPL) ) {
1581		/* attempt to fallback to nonEDNS */
1582		sq->status = serviced_query_TCP_EDNS_fallback;
1583		serviced_tcp_initiate(sq->outnet, sq, c->buffer);
1584		return 0;
1585	} else if(error==NETEVENT_NOERROR &&
1586		sq->status == serviced_query_TCP_EDNS_fallback &&
1587			(LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1588			LDNS_RCODE_NOERROR || LDNS_RCODE_WIRE(
1589			sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NXDOMAIN
1590			|| LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1591			== LDNS_RCODE_YXDOMAIN)) {
1592		/* the fallback produced a result that looks promising, note
1593		 * that this server should be approached without EDNS */
1594		/* only store noEDNS in cache if domain is noDNSSEC */
1595		if(!sq->want_dnssec)
1596		  if(!infra_edns_update(sq->outnet->infra, &sq->addr,
1597			sq->addrlen, sq->zone, sq->zonelen, -1,
1598			*sq->outnet->now_secs))
1599			log_err("Out of memory caching no edns for host");
1600		sq->status = serviced_query_TCP;
1601	}
1602	if(sq->tcp_upstream || sq->ssl_upstream) {
1603	    struct timeval now = *sq->outnet->now_tv;
1604	    if(now.tv_sec > sq->last_sent_time.tv_sec ||
1605		(now.tv_sec == sq->last_sent_time.tv_sec &&
1606		now.tv_usec > sq->last_sent_time.tv_usec)) {
1607		/* convert from microseconds to milliseconds */
1608		int roundtime = ((int)(now.tv_sec - sq->last_sent_time.tv_sec))*1000
1609		  + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1610		verbose(VERB_ALGO, "measured TCP-time at %d msec", roundtime);
1611		log_assert(roundtime >= 0);
1612		/* only store if less then AUTH_TIMEOUT seconds, it could be
1613		 * huge due to system-hibernated and we woke up */
1614		if(roundtime < TCP_AUTH_QUERY_TIMEOUT*1000) {
1615		    if(!infra_rtt_update(sq->outnet->infra, &sq->addr,
1616			sq->addrlen, sq->zone, sq->zonelen, sq->qtype,
1617			roundtime, sq->last_rtt, (time_t)now.tv_sec))
1618			log_err("out of memory noting rtt.");
1619		}
1620	    }
1621	}
1622	/* insert address into reply info */
1623	if(!rep) {
1624		/* create one if there isn't (on errors) */
1625		rep = &r2;
1626		r2.c = c;
1627	}
1628	memcpy(&rep->addr, &sq->addr, sq->addrlen);
1629	rep->addrlen = sq->addrlen;
1630	serviced_callbacks(sq, error, c, rep);
1631	return 0;
1632}
1633
1634static void
1635serviced_tcp_initiate(struct outside_network* outnet,
1636	struct serviced_query* sq, sldns_buffer* buff)
1637{
1638	verbose(VERB_ALGO, "initiate TCP query %s",
1639		sq->status==serviced_query_TCP_EDNS?"EDNS":"");
1640	serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1641	sq->last_sent_time = *sq->outnet->now_tv;
1642	sq->pending = pending_tcp_query(outnet, buff, &sq->addr,
1643		sq->addrlen, TCP_AUTH_QUERY_TIMEOUT, serviced_tcp_callback,
1644		sq, sq->ssl_upstream);
1645	if(!sq->pending) {
1646		/* delete from tree so that a retry by above layer does not
1647		 * clash with this entry */
1648		log_err("serviced_tcp_initiate: failed to send tcp query");
1649		serviced_callbacks(sq, NETEVENT_CLOSED, NULL, NULL);
1650	}
1651}
1652
1653/** Send serviced query over TCP return false on initial failure */
1654static int
1655serviced_tcp_send(struct serviced_query* sq, sldns_buffer* buff)
1656{
1657	int vs, rtt;
1658	uint8_t edns_lame_known;
1659	if(!infra_host(sq->outnet->infra, &sq->addr, sq->addrlen, sq->zone,
1660		sq->zonelen, *sq->outnet->now_secs, &vs, &edns_lame_known,
1661		&rtt))
1662		return 0;
1663	if(vs != -1)
1664		sq->status = serviced_query_TCP_EDNS;
1665	else 	sq->status = serviced_query_TCP;
1666	serviced_encode(sq, buff, sq->status == serviced_query_TCP_EDNS);
1667	sq->last_sent_time = *sq->outnet->now_tv;
1668	sq->pending = pending_tcp_query(sq->outnet, buff, &sq->addr,
1669		sq->addrlen, TCP_AUTH_QUERY_TIMEOUT, serviced_tcp_callback,
1670		sq, sq->ssl_upstream);
1671	return sq->pending != NULL;
1672}
1673
1674int
1675serviced_udp_callback(struct comm_point* c, void* arg, int error,
1676        struct comm_reply* rep)
1677{
1678	struct serviced_query* sq = (struct serviced_query*)arg;
1679	struct outside_network* outnet = sq->outnet;
1680	struct timeval now = *sq->outnet->now_tv;
1681	int fallback_tcp = 0;
1682
1683	sq->pending = NULL; /* removed after callback */
1684	if(error == NETEVENT_TIMEOUT) {
1685		int rto = 0;
1686		if(sq->status == serviced_query_PROBE_EDNS) {
1687			/* non-EDNS probe failed; we do not know its status,
1688			 * keep trying with EDNS, timeout may not be caused
1689			 * by EDNS. */
1690			sq->status = serviced_query_UDP_EDNS;
1691		}
1692		if(sq->status == serviced_query_UDP_EDNS && sq->last_rtt < 5000) {
1693			/* fallback to 1480/1280 */
1694			sq->status = serviced_query_UDP_EDNS_FRAG;
1695			log_name_addr(VERB_ALGO, "try edns1xx0", sq->qbuf+10,
1696				&sq->addr, sq->addrlen);
1697			if(!serviced_udp_send(sq, c->buffer)) {
1698				serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1699			}
1700			return 0;
1701		}
1702		if(sq->status == serviced_query_UDP_EDNS_FRAG) {
1703			/* fragmentation size did not fix it */
1704			sq->status = serviced_query_UDP_EDNS;
1705		}
1706		sq->retry++;
1707		if(!(rto=infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1708			sq->zone, sq->zonelen, sq->qtype, -1, sq->last_rtt,
1709			(time_t)now.tv_sec)))
1710			log_err("out of memory in UDP exponential backoff");
1711		if(sq->retry < OUTBOUND_UDP_RETRY) {
1712			log_name_addr(VERB_ALGO, "retry query", sq->qbuf+10,
1713				&sq->addr, sq->addrlen);
1714			if(!serviced_udp_send(sq, c->buffer)) {
1715				serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1716			}
1717			return 0;
1718		}
1719		if(rto >= RTT_MAX_TIMEOUT) {
1720			fallback_tcp = 1;
1721			/* UDP does not work, fallback to TCP below */
1722		} else {
1723			serviced_callbacks(sq, NETEVENT_TIMEOUT, c, rep);
1724			return 0;
1725		}
1726	} else if(error != NETEVENT_NOERROR) {
1727		/* udp returns error (due to no ID or interface available) */
1728		serviced_callbacks(sq, error, c, rep);
1729		return 0;
1730	}
1731	if(!fallback_tcp) {
1732	    if( (sq->status == serviced_query_UDP_EDNS
1733	        ||sq->status == serviced_query_UDP_EDNS_FRAG)
1734		&& (LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer))
1735			== LDNS_RCODE_FORMERR || LDNS_RCODE_WIRE(
1736			sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOTIMPL)) {
1737		/* try to get an answer by falling back without EDNS */
1738		verbose(VERB_ALGO, "serviced query: attempt without EDNS");
1739		sq->status = serviced_query_UDP_EDNS_fallback;
1740		sq->retry = 0;
1741		if(!serviced_udp_send(sq, c->buffer)) {
1742			serviced_callbacks(sq, NETEVENT_CLOSED, c, rep);
1743		}
1744		return 0;
1745	    } else if(sq->status == serviced_query_PROBE_EDNS) {
1746		/* probe without EDNS succeeds, so we conclude that this
1747		 * host likely has EDNS packets dropped */
1748		log_addr(VERB_DETAIL, "timeouts, concluded that connection to "
1749			"host drops EDNS packets", &sq->addr, sq->addrlen);
1750		/* only store noEDNS in cache if domain is noDNSSEC */
1751		if(!sq->want_dnssec)
1752		  if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1753			sq->zone, sq->zonelen, -1, (time_t)now.tv_sec)) {
1754			log_err("Out of memory caching no edns for host");
1755		  }
1756		sq->status = serviced_query_UDP;
1757	    } else if(sq->status == serviced_query_UDP_EDNS &&
1758		!sq->edns_lame_known) {
1759		/* now we know that edns queries received answers store that */
1760		log_addr(VERB_ALGO, "serviced query: EDNS works for",
1761			&sq->addr, sq->addrlen);
1762		if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1763			sq->zone, sq->zonelen, 0, (time_t)now.tv_sec)) {
1764			log_err("Out of memory caching edns works");
1765		}
1766		sq->edns_lame_known = 1;
1767	    } else if(sq->status == serviced_query_UDP_EDNS_fallback &&
1768		!sq->edns_lame_known && (LDNS_RCODE_WIRE(
1769		sldns_buffer_begin(c->buffer)) == LDNS_RCODE_NOERROR ||
1770		LDNS_RCODE_WIRE(sldns_buffer_begin(c->buffer)) ==
1771		LDNS_RCODE_NXDOMAIN || LDNS_RCODE_WIRE(sldns_buffer_begin(
1772		c->buffer)) == LDNS_RCODE_YXDOMAIN)) {
1773		/* the fallback produced a result that looks promising, note
1774		 * that this server should be approached without EDNS */
1775		/* only store noEDNS in cache if domain is noDNSSEC */
1776		if(!sq->want_dnssec) {
1777		  log_addr(VERB_ALGO, "serviced query: EDNS fails for",
1778			&sq->addr, sq->addrlen);
1779		  if(!infra_edns_update(outnet->infra, &sq->addr, sq->addrlen,
1780			sq->zone, sq->zonelen, -1, (time_t)now.tv_sec)) {
1781			log_err("Out of memory caching no edns for host");
1782		  }
1783		} else {
1784		  log_addr(VERB_ALGO, "serviced query: EDNS fails, but "
1785		  	"not stored because need DNSSEC for", &sq->addr,
1786			sq->addrlen);
1787		}
1788		sq->status = serviced_query_UDP;
1789	    }
1790	    if(now.tv_sec > sq->last_sent_time.tv_sec ||
1791		(now.tv_sec == sq->last_sent_time.tv_sec &&
1792		now.tv_usec > sq->last_sent_time.tv_usec)) {
1793		/* convert from microseconds to milliseconds */
1794		int roundtime = ((int)(now.tv_sec - sq->last_sent_time.tv_sec))*1000
1795		  + ((int)now.tv_usec - (int)sq->last_sent_time.tv_usec)/1000;
1796		verbose(VERB_ALGO, "measured roundtrip at %d msec", roundtime);
1797		log_assert(roundtime >= 0);
1798		/* in case the system hibernated, do not enter a huge value,
1799		 * above this value gives trouble with server selection */
1800		if(roundtime < 60000) {
1801		    if(!infra_rtt_update(outnet->infra, &sq->addr, sq->addrlen,
1802			sq->zone, sq->zonelen, sq->qtype, roundtime,
1803			sq->last_rtt, (time_t)now.tv_sec))
1804			log_err("out of memory noting rtt.");
1805		}
1806	    }
1807	} /* end of if_!fallback_tcp */
1808	/* perform TC flag check and TCP fallback after updating our
1809	 * cache entries for EDNS status and RTT times */
1810	if(LDNS_TC_WIRE(sldns_buffer_begin(c->buffer)) || fallback_tcp) {
1811		/* fallback to TCP */
1812		/* this discards partial UDP contents */
1813		if(sq->status == serviced_query_UDP_EDNS ||
1814			sq->status == serviced_query_UDP_EDNS_FRAG ||
1815			sq->status == serviced_query_UDP_EDNS_fallback)
1816			/* if we have unfinished EDNS_fallback, start again */
1817			sq->status = serviced_query_TCP_EDNS;
1818		else	sq->status = serviced_query_TCP;
1819		serviced_tcp_initiate(outnet, sq, c->buffer);
1820		return 0;
1821	}
1822	/* yay! an answer */
1823	serviced_callbacks(sq, error, c, rep);
1824	return 0;
1825}
1826
1827struct serviced_query*
1828outnet_serviced_query(struct outside_network* outnet,
1829	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
1830	uint16_t flags, int dnssec, int want_dnssec, int tcp_upstream,
1831	int ssl_upstream, struct sockaddr_storage* addr, socklen_t addrlen,
1832	uint8_t* zone, size_t zonelen, comm_point_callback_t* callback,
1833	void* callback_arg, sldns_buffer* buff)
1834{
1835	struct serviced_query* sq;
1836	struct service_callback* cb;
1837	serviced_gen_query(buff, qname, qnamelen, qtype, qclass, flags);
1838	sq = lookup_serviced(outnet, buff, dnssec, addr, addrlen);
1839	/* duplicate entries are included in the callback list, because
1840	 * there is a counterpart registration by our caller that needs to
1841	 * be doubly-removed (with callbacks perhaps). */
1842	if(!(cb = (struct service_callback*)malloc(sizeof(*cb))))
1843		return NULL;
1844	if(!sq) {
1845		/* make new serviced query entry */
1846		sq = serviced_create(outnet, buff, dnssec, want_dnssec,
1847			tcp_upstream, ssl_upstream, addr, addrlen, zone,
1848			zonelen, (int)qtype);
1849		if(!sq) {
1850			free(cb);
1851			return NULL;
1852		}
1853		/* perform first network action */
1854		if(outnet->do_udp && !(tcp_upstream || ssl_upstream)) {
1855			if(!serviced_udp_send(sq, buff)) {
1856				(void)rbtree_delete(outnet->serviced, sq);
1857				free(sq->qbuf);
1858				free(sq->zone);
1859				free(sq);
1860				free(cb);
1861				return NULL;
1862			}
1863		} else {
1864			if(!serviced_tcp_send(sq, buff)) {
1865				(void)rbtree_delete(outnet->serviced, sq);
1866				free(sq->qbuf);
1867				free(sq->zone);
1868				free(sq);
1869				free(cb);
1870				return NULL;
1871			}
1872		}
1873	}
1874	/* add callback to list of callbacks */
1875	cb->cb = callback;
1876	cb->cb_arg = callback_arg;
1877	cb->next = sq->cblist;
1878	sq->cblist = cb;
1879	return sq;
1880}
1881
1882/** remove callback from list */
1883static void
1884callback_list_remove(struct serviced_query* sq, void* cb_arg)
1885{
1886	struct service_callback** pp = &sq->cblist;
1887	while(*pp) {
1888		if((*pp)->cb_arg == cb_arg) {
1889			struct service_callback* del = *pp;
1890			*pp = del->next;
1891			free(del);
1892			return;
1893		}
1894		pp = &(*pp)->next;
1895	}
1896}
1897
1898void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg)
1899{
1900	if(!sq)
1901		return;
1902	callback_list_remove(sq, cb_arg);
1903	/* if callbacks() routine scheduled deletion, let it do that */
1904	if(!sq->cblist && !sq->to_be_deleted) {
1905#ifdef UNBOUND_DEBUG
1906		rbnode_t* rem =
1907#else
1908		(void)
1909#endif
1910		rbtree_delete(sq->outnet->serviced, sq);
1911		log_assert(rem); /* should be present */
1912		serviced_delete(sq);
1913	}
1914}
1915
1916/** get memory used by waiting tcp entry (in use or not) */
1917static size_t
1918waiting_tcp_get_mem(struct waiting_tcp* w)
1919{
1920	size_t s;
1921	if(!w) return 0;
1922	s = sizeof(*w) + w->pkt_len;
1923	if(w->timer)
1924		s += comm_timer_get_mem(w->timer);
1925	return s;
1926}
1927
1928/** get memory used by port if */
1929static size_t
1930if_get_mem(struct port_if* pif)
1931{
1932	size_t s;
1933	int i;
1934	s = sizeof(*pif) + sizeof(int)*pif->avail_total +
1935		sizeof(struct port_comm*)*pif->maxout;
1936	for(i=0; i<pif->inuse; i++)
1937		s += sizeof(*pif->out[i]) +
1938			comm_point_get_mem(pif->out[i]->cp);
1939	return s;
1940}
1941
1942/** get memory used by waiting udp */
1943static size_t
1944waiting_udp_get_mem(struct pending* w)
1945{
1946	size_t s;
1947	s = sizeof(*w) + comm_timer_get_mem(w->timer) + w->pkt_len;
1948	return s;
1949}
1950
1951size_t outnet_get_mem(struct outside_network* outnet)
1952{
1953	size_t i;
1954	int k;
1955	struct waiting_tcp* w;
1956	struct pending* u;
1957	struct serviced_query* sq;
1958	struct service_callback* sb;
1959	struct port_comm* pc;
1960	size_t s = sizeof(*outnet) + sizeof(*outnet->base) +
1961		sizeof(*outnet->udp_buff) +
1962		sldns_buffer_capacity(outnet->udp_buff);
1963	/* second buffer is not ours */
1964	for(pc = outnet->unused_fds; pc; pc = pc->next) {
1965		s += sizeof(*pc) + comm_point_get_mem(pc->cp);
1966	}
1967	for(k=0; k<outnet->num_ip4; k++)
1968		s += if_get_mem(&outnet->ip4_ifs[k]);
1969	for(k=0; k<outnet->num_ip6; k++)
1970		s += if_get_mem(&outnet->ip6_ifs[k]);
1971	for(u=outnet->udp_wait_first; u; u=u->next_waiting)
1972		s += waiting_udp_get_mem(u);
1973
1974	s += sizeof(struct pending_tcp*)*outnet->num_tcp;
1975	for(i=0; i<outnet->num_tcp; i++) {
1976		s += sizeof(struct pending_tcp);
1977		s += comm_point_get_mem(outnet->tcp_conns[i]->c);
1978		if(outnet->tcp_conns[i]->query)
1979			s += waiting_tcp_get_mem(outnet->tcp_conns[i]->query);
1980	}
1981	for(w=outnet->tcp_wait_first; w; w = w->next_waiting)
1982		s += waiting_tcp_get_mem(w);
1983	s += sizeof(*outnet->pending);
1984	s += (sizeof(struct pending) + comm_timer_get_mem(NULL)) *
1985		outnet->pending->count;
1986	s += sizeof(*outnet->serviced);
1987	s += outnet->svcd_overhead;
1988	RBTREE_FOR(sq, struct serviced_query*, outnet->serviced) {
1989		s += sizeof(*sq) + sq->qbuflen;
1990		for(sb = sq->cblist; sb; sb = sb->next)
1991			s += sizeof(*sb);
1992	}
1993	return s;
1994}
1995
1996size_t
1997serviced_get_mem(struct serviced_query* sq)
1998{
1999	struct service_callback* sb;
2000	size_t s;
2001	s = sizeof(*sq) + sq->qbuflen;
2002	for(sb = sq->cblist; sb; sb = sb->next)
2003		s += sizeof(*sb);
2004	if(sq->status == serviced_query_UDP_EDNS ||
2005		sq->status == serviced_query_UDP ||
2006		sq->status == serviced_query_PROBE_EDNS ||
2007		sq->status == serviced_query_UDP_EDNS_FRAG ||
2008		sq->status == serviced_query_UDP_EDNS_fallback) {
2009		s += sizeof(struct pending);
2010		s += comm_timer_get_mem(NULL);
2011	} else {
2012		/* does not have size of the pkt pointer */
2013		/* always has a timer except on malloc failures */
2014
2015		/* these sizes are part of the main outside network mem */
2016		/*
2017		s += sizeof(struct waiting_tcp);
2018		s += comm_timer_get_mem(NULL);
2019		*/
2020	}
2021	return s;
2022}
2023
2024