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