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