xref: /freebsd/contrib/unbound/services/outside_network.h (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*
2  * services/outside_network.h - listen to answers from the network
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,
40  * and wait for the pending answer, with timeouts.
41  */
42 
43 #ifndef OUTSIDE_NETWORK_H
44 #define OUTSIDE_NETWORK_H
45 
46 #include "util/alloc.h"
47 #include "util/rbtree.h"
48 #include "util/regional.h"
49 #include "util/netevent.h"
50 #include "dnstap/dnstap_config.h"
51 struct pending;
52 struct pending_timeout;
53 struct ub_randstate;
54 struct pending_tcp;
55 struct waiting_tcp;
56 struct waiting_udp;
57 struct reuse_tcp;
58 struct infra_cache;
59 struct port_comm;
60 struct port_if;
61 struct sldns_buffer;
62 struct serviced_query;
63 struct dt_env;
64 struct edns_option;
65 struct module_env;
66 struct module_qstate;
67 struct query_info;
68 struct config_file;
69 
70 /**
71  * Send queries to outside servers and wait for answers from servers.
72  * Contains answer-listen sockets.
73  */
74 struct outside_network {
75 	/** Base for select calls */
76 	struct comm_base* base;
77 	/** pointer to time in seconds */
78 	time_t* now_secs;
79 	/** pointer to time in microseconds */
80 	struct timeval* now_tv;
81 
82 	/** buffer shared by UDP connections, since there is only one
83 	    datagram at any time. */
84 	struct sldns_buffer* udp_buff;
85 	/** serviced_callbacks malloc overhead when processing multiple
86 	 * identical serviced queries to the same server. */
87 	size_t svcd_overhead;
88 	/** use x20 bits to encode additional ID random bits */
89 	int use_caps_for_id;
90 	/** outside network wants to quit. Stop queued msgs from sent. */
91 	int want_to_quit;
92 
93 	/** number of unwanted replies received (for statistics) */
94 	size_t unwanted_replies;
95 	/** cumulative total of unwanted replies (for defense) */
96 	size_t unwanted_total;
97 	/** threshold when to take defensive action. If 0 then never. */
98 	size_t unwanted_threshold;
99 	/** what action to take, called when defensive action is needed */
100 	void (*unwanted_action)(void*);
101 	/** user param for action */
102 	void* unwanted_param;
103 
104 	/** linked list of available commpoints, unused file descriptors,
105 	 * for use as outgoing UDP ports. cp.fd=-1 in them. */
106 	struct port_comm* unused_fds;
107 	/** if udp is done */
108 	int do_udp;
109 	/** if udp is delay-closed (delayed answers do not meet closed port)*/
110 	int delayclose;
111 	/** timeout for delayclose */
112 	struct timeval delay_tv;
113 	/** if we perform udp-connect, connect() for UDP socket to mitigate
114 	 * ICMP side channel leakage */
115 	int udp_connect;
116 	/** number of udp packets sent. */
117 	size_t num_udp_outgoing;
118 
119 	/** array of outgoing IP4 interfaces */
120 	struct port_if* ip4_ifs;
121 	/** number of outgoing IP4 interfaces */
122 	int num_ip4;
123 
124 	/** array of outgoing IP6 interfaces */
125 	struct port_if* ip6_ifs;
126 	/** number of outgoing IP6 interfaces */
127 	int num_ip6;
128 
129 	/** pending udp queries waiting to be sent out, waiting for fd */
130 	struct pending* udp_wait_first;
131 	/** last pending udp query in list */
132 	struct pending* udp_wait_last;
133 
134 	/** pending udp answers. sorted by id, addr */
135 	rbtree_type* pending;
136 	/** serviced queries, sorted by qbuf, addr, dnssec */
137 	rbtree_type* serviced;
138 	/** host cache, pointer but not owned by outnet. */
139 	struct infra_cache* infra;
140 	/** where to get random numbers */
141 	struct ub_randstate* rnd;
142 	/** ssl context to create ssl wrapped TCP with DNS connections */
143 	void* sslctx;
144 	/** if SNI will be used for TLS connections */
145 	int tls_use_sni;
146 #ifdef USE_DNSTAP
147 	/** dnstap environment */
148 	struct dt_env* dtenv;
149 #endif
150 	/** maximum segment size of tcp socket */
151 	int tcp_mss;
152 	/** IP_TOS socket option requested on the sockets */
153 	int ip_dscp;
154 
155 	/**
156 	 * Array of tcp pending used for outgoing TCP connections.
157 	 * Each can be used to establish a TCP connection with a server.
158 	 * The file descriptors are -1 if they are free, and need to be
159 	 * opened for the tcp connection. Can be used for ip4 and ip6.
160 	 */
161 	struct pending_tcp **tcp_conns;
162 	/** number of tcp communication points. */
163 	size_t num_tcp;
164 	/** number of tcp communication points in use. */
165 	size_t num_tcp_outgoing;
166 	/** max number of queries on a reuse connection */
167 	size_t max_reuse_tcp_queries;
168 	/** timeout for REUSE entries in milliseconds. */
169 	int tcp_reuse_timeout;
170 	/** timeout in milliseconds for TCP queries to auth servers. */
171 	int tcp_auth_query_timeout;
172 	/**
173 	 * tree of still-open and waiting tcp connections for reuse.
174 	 * can be closed and reopened to get a new tcp connection.
175 	 * or reused to the same destination again.  with timeout to close.
176 	 * Entries are of type struct reuse_tcp.
177 	 * The entries are both active and empty connections.
178 	 */
179 	rbtree_type tcp_reuse;
180 	/** max number of tcp_reuse entries we want to keep open */
181 	size_t tcp_reuse_max;
182 	/** first and last(oldest) in lru list of reuse connections.
183 	 * the oldest can be closed to get a new free pending_tcp if needed
184 	 * The list contains empty connections, that wait for timeout or
185 	 * a new query that can use the existing connection. */
186 	struct reuse_tcp* tcp_reuse_first, *tcp_reuse_last;
187 	/** list of tcp comm points that are free for use */
188 	struct pending_tcp* tcp_free;
189 	/** list of tcp queries waiting for a buffer */
190 	struct waiting_tcp* tcp_wait_first;
191 	/** last of waiting query list */
192 	struct waiting_tcp* tcp_wait_last;
193 };
194 
195 /**
196  * Outgoing interface. Ports available and currently used are tracked
197  * per interface
198  */
199 struct port_if {
200 	/** address ready to allocate new socket (except port no). */
201 	struct sockaddr_storage addr;
202 	/** length of addr field */
203 	socklen_t addrlen;
204 
205 	/** prefix length of network address (in bits), for randomisation.
206 	 * if 0, no randomisation. */
207 	int pfxlen;
208 
209 #ifndef DISABLE_EXPLICIT_PORT_RANDOMISATION
210 	/** the available ports array. These are unused.
211 	 * Only the first total-inuse part is filled. */
212 	int* avail_ports;
213 	/** the total number of available ports (size of the array) */
214 	int avail_total;
215 #endif
216 
217 	/** array of the commpoints currently in use.
218 	 * allocated for max number of fds, first part in use. */
219 	struct port_comm** out;
220 	/** max number of fds, size of out array */
221 	int maxout;
222 	/** number of commpoints (and thus also ports) in use */
223 	int inuse;
224 };
225 
226 /**
227  * Outgoing commpoint for UDP port.
228  */
229 struct port_comm {
230 	/** next in free list */
231 	struct port_comm* next;
232 	/** which port number (when in use) */
233 	int number;
234 	/** interface it is used in */
235 	struct port_if* pif;
236 	/** index in the out array of the interface */
237 	int index;
238 	/** number of outstanding queries on this port */
239 	int num_outstanding;
240 	/** UDP commpoint, fd=-1 if not in use */
241 	struct comm_point* cp;
242 };
243 
244 /**
245  * Reuse TCP connection, still open can be used again.
246  */
247 struct reuse_tcp {
248 	/** rbtree node with links in tcp_reuse tree. key is NULL when not
249 	 * in tree. Both active and empty connections are in the tree.
250 	 * key is a pointer to this structure, the members used to compare
251 	 * are the sockaddr and and then is-ssl bool, and then ptr value is
252 	 * used in case the same address exists several times in the tree
253 	 * when there are multiple connections to the same destination to
254 	 * make the rbtree items unique. */
255 	rbnode_type node;
256 	/** the key for the tcp_reuse tree. address of peer, ip4 or ip6,
257 	 * and port number of peer */
258 	struct sockaddr_storage addr;
259 	/** length of addr */
260 	socklen_t addrlen;
261 	/** also key for tcp_reuse tree, if ssl is used */
262 	int is_ssl;
263 	/** lru chain, so that the oldest can be removed to get a new
264 	 * connection when all are in (re)use. oldest is last in list.
265 	 * The lru only contains empty connections waiting for reuse,
266 	 * the ones with active queries are not on the list because they
267 	 * do not need to be closed to make space for others.  They already
268 	 * service a query so the close for another query does not help
269 	 * service a larger number of queries. */
270 	struct reuse_tcp* lru_next, *lru_prev;
271 	/** true if the reuse_tcp item is on the lru list with empty items */
272 	int item_on_lru_list;
273 	/** the connection to reuse, the fd is non-1 and is open.
274 	 * the addr and port determine where the connection is going,
275 	 * and is key to the rbtree.  The SSL ptr determines if it is
276 	 * a TLS connection or a plain TCP connection there.  And TLS
277 	 * or not is also part of the key to the rbtree.
278 	 * There is a timeout and read event on the fd, to close it. */
279 	struct pending_tcp* pending;
280 	/**
281 	 * The more read again value pointed to by the commpoint
282 	 * tcp_more_read_again pointer, so that it exists after commpoint
283 	 * delete
284 	 */
285 	int cp_more_read_again;
286 	/**
287 	 * The more write again value pointed to by the commpoint
288 	 * tcp_more_write_again pointer, so that it exists after commpoint
289 	 * delete
290 	 */
291 	int cp_more_write_again;
292 	/** rbtree with other queries waiting on the connection, by ID number,
293 	 * of type struct waiting_tcp. It is for looking up received
294 	 * answers to the structure for callback.  And also to see if ID
295 	 * numbers are unused and can be used for a new query.
296 	 * The write_wait elements are also in the tree, so that ID numbers
297 	 * can be looked up also for them.  They are bool write_wait_queued. */
298 	rbtree_type tree_by_id;
299 	/** list of queries waiting to be written on the channel,
300 	 * if NULL no queries are waiting to be written and the pending->query
301 	 * is the query currently serviced.  The first is the next in line.
302 	 * They are also in the tree_by_id. Once written, the are removed
303 	 * from this list, but stay in the tree. */
304 	struct waiting_tcp* write_wait_first, *write_wait_last;
305 	/** the outside network it is part of */
306 	struct outside_network* outnet;
307 };
308 
309 /**
310  * A query that has an answer pending for it.
311  */
312 struct pending {
313 	/** redblacktree entry, key is the pending struct(id, addr). */
314 	rbnode_type node;
315 	/** the ID for the query. int so that a value out of range can
316 	 * be used to signify a pending that is for certain not present in
317 	 * the rbtree. (and for which deletion is safe). */
318 	unsigned int id;
319 	/** remote address. */
320 	struct sockaddr_storage addr;
321 	/** length of addr field in use. */
322 	socklen_t addrlen;
323 	/** comm point it was sent on (and reply must come back on). */
324 	struct port_comm* pc;
325 	/** timeout event */
326 	struct comm_timer* timer;
327 	/** callback for the timeout, error or reply to the message */
328 	comm_point_callback_type* cb;
329 	/** callback user argument */
330 	void* cb_arg;
331 	/** the outside network it is part of */
332 	struct outside_network* outnet;
333 	/** the corresponding serviced_query */
334 	struct serviced_query* sq;
335 
336 	/*---- filled if udp pending is waiting -----*/
337 	/** next in waiting list. */
338 	struct pending* next_waiting;
339 	/** timeout in msec */
340 	int timeout;
341 	/** The query itself, the query packet to send. */
342 	uint8_t* pkt;
343 	/** length of query packet. */
344 	size_t pkt_len;
345 };
346 
347 /**
348  * Pending TCP query to server.
349  */
350 struct pending_tcp {
351 	/** next in list of free tcp comm points, or NULL. */
352 	struct pending_tcp* next_free;
353 	/** port for of the outgoing interface that is used */
354 	struct port_if* pi;
355 	/** tcp comm point it was sent on (and reply must come back on). */
356 	struct comm_point* c;
357 	/** the query being serviced, NULL if the pending_tcp is unused. */
358 	struct waiting_tcp* query;
359 	/** the pre-allocated reuse tcp structure.  if ->pending is nonNULL
360 	 * it is in use and the connection is waiting for reuse.
361 	 * It is here for memory pre-allocation, and used to make this
362 	 * pending_tcp wait for reuse. */
363 	struct reuse_tcp reuse;
364 };
365 
366 /**
367  * Query waiting for TCP buffer.
368  */
369 struct waiting_tcp {
370 	/**
371 	 * next in waiting list.
372 	 * if on_tcp_waiting_list==0, this points to the pending_tcp structure.
373 	 */
374 	struct waiting_tcp* next_waiting;
375 	/** if true the item is on the tcp waiting list and next_waiting
376 	 * is used for that.  If false, the next_waiting points to the
377 	 * pending_tcp */
378 	int on_tcp_waiting_list;
379 	/** next and prev in query waiting list for stream connection */
380 	struct waiting_tcp* write_wait_prev, *write_wait_next;
381 	/** true if the waiting_tcp structure is on the write_wait queue */
382 	int write_wait_queued;
383 	/** entry in reuse.tree_by_id, if key is NULL, not in tree, otherwise,
384 	 * this struct is key and sorted by ID (from waiting_tcp.id). */
385 	rbnode_type id_node;
386 	/** the ID for the query; checked in reply */
387 	uint16_t id;
388 	/** timeout event; timer keeps running whether the query is
389 	 * waiting for a buffer or the tcp reply is pending */
390 	struct comm_timer* timer;
391 	/** timeout in msec */
392 	int timeout;
393 	/** the outside network it is part of */
394 	struct outside_network* outnet;
395 	/** remote address. */
396 	struct sockaddr_storage addr;
397 	/** length of addr field in use. */
398 	socklen_t addrlen;
399 	/**
400 	 * The query itself, the query packet to send.
401 	 * allocated after the waiting_tcp structure.
402 	 */
403 	uint8_t* pkt;
404 	/** length of query packet. */
405 	size_t pkt_len;
406 	/** callback for the timeout, error or reply to the message,
407 	 * or NULL if no user is waiting. the entry uses an ID number.
408 	 * a query that was written is no longer needed, but the ID number
409 	 * and a reply will come back and can be ignored if NULL */
410 	comm_point_callback_type* cb;
411 	/** callback user argument */
412 	void* cb_arg;
413 	/** if it uses ssl upstream */
414 	int ssl_upstream;
415 	/** ref to the tls_auth_name from the serviced_query */
416 	char* tls_auth_name;
417 	/** the packet was involved in an error, to stop looping errors */
418 	int error_count;
419 	/** if true, the item is at the cb_and_decommission stage */
420 	int in_cb_and_decommission;
421 #ifdef USE_DNSTAP
422 	/** serviced query pointer for dnstap to get logging info, if nonNULL*/
423 	struct serviced_query* sq;
424 #endif
425 };
426 
427 /**
428  * Callback to party interested in serviced query results.
429  */
430 struct service_callback {
431 	/** next in callback list */
432 	struct service_callback* next;
433 	/** callback function */
434 	comm_point_callback_type* cb;
435 	/** user argument for callback function */
436 	void* cb_arg;
437 };
438 
439 /** fallback size for fragmentation for EDNS in IPv4 */
440 #define EDNS_FRAG_SIZE_IP4 1472
441 /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
442 #define EDNS_FRAG_SIZE_IP6 1232
443 
444 /**
445  * Query service record.
446  * Contains query and destination. UDP, TCP, EDNS are all tried.
447  * complete with retries and timeouts. A number of interested parties can
448  * receive a callback.
449  */
450 struct serviced_query {
451 	/** The rbtree node, key is this record */
452 	rbnode_type node;
453 	/** The query that needs to be answered. Starts with flags u16,
454 	 * then qdcount, ..., including qname, qtype, qclass. Does not include
455 	 * EDNS record. */
456 	uint8_t* qbuf;
457 	/** length of qbuf. */
458 	size_t qbuflen;
459 	/** If an EDNS section is included, the DO/CD bit will be turned on. */
460 	int dnssec;
461 	/** We want signatures, or else the answer is likely useless */
462 	int want_dnssec;
463 	/** ignore capsforid */
464 	int nocaps;
465 	/** tcp upstream used, use tcp, or ssl_upstream for SSL */
466 	int tcp_upstream, ssl_upstream;
467 	/** the name of the tls authentication name, eg. 'ns.example.com'
468 	 * or NULL */
469 	char* tls_auth_name;
470 	/** where to send it */
471 	struct sockaddr_storage addr;
472 	/** length of addr field in use. */
473 	socklen_t addrlen;
474 	/** zone name, uncompressed domain name in wireformat */
475 	uint8_t* zone;
476 	/** length of zone name */
477 	size_t zonelen;
478 	/** qtype */
479 	int qtype;
480 	/** current status */
481 	enum serviced_query_status {
482 		/** initial status */
483 		serviced_initial,
484 		/** UDP with EDNS sent */
485 		serviced_query_UDP_EDNS,
486 		/** UDP without EDNS sent */
487 		serviced_query_UDP,
488 		/** TCP with EDNS sent */
489 		serviced_query_TCP_EDNS,
490 		/** TCP without EDNS sent */
491 		serviced_query_TCP,
492 		/** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
493 		serviced_query_UDP_EDNS_fallback,
494 		/** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
495 		serviced_query_TCP_EDNS_fallback,
496 		/** send UDP query with EDNS1480 (or 1280) */
497 		serviced_query_UDP_EDNS_FRAG
498 	}
499 		/** variable with current status */
500 		status;
501 	/** true if serviced_query is scheduled for deletion already */
502 	int to_be_deleted;
503 	/** number of UDP retries */
504 	int retry;
505 	/** time last UDP was sent */
506 	struct timeval last_sent_time;
507 	/** rtt of last message */
508 	int last_rtt;
509 	/** do we know edns probe status already, for UDP_EDNS queries */
510 	int edns_lame_known;
511 	/** edns options to use for sending upstream packet */
512 	struct edns_option* opt_list;
513 	/** outside network this is part of */
514 	struct outside_network* outnet;
515 	/** list of interested parties that need callback on results. */
516 	struct service_callback* cblist;
517 	/** the UDP or TCP query that is pending, see status which */
518 	void* pending;
519 	/** block size with which to pad encrypted queries (default: 128) */
520 	size_t padding_block_size;
521 	/** region for this serviced query. Will be cleared when this
522 	 * serviced_query will be deleted */
523 	struct regional* region;
524 	/** allocation service for the region */
525 	struct alloc_cache* alloc;
526 	/** flash timer to start the net I/O as a separate event */
527 	struct comm_timer* timer;
528 	/** true if serviced_query is currently doing net I/O and may block */
529 	int busy;
530 };
531 
532 /**
533  * Create outside_network structure with N udp ports.
534  * @param base: the communication base to use for event handling.
535  * @param bufsize: size for network buffers.
536  * @param num_ports: number of udp ports to open per interface.
537  * @param ifs: interface names (or NULL for default interface).
538  *    These interfaces must be able to access all authoritative servers.
539  * @param num_ifs: number of names in array ifs.
540  * @param do_ip4: service IP4.
541  * @param do_ip6: service IP6.
542  * @param num_tcp: number of outgoing tcp buffers to preallocate.
543  * @param dscp: DSCP to use.
544  * @param infra: pointer to infra cached used for serviced queries.
545  * @param rnd: stored to create random numbers for serviced queries.
546  * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
547  * @param availports: array of available ports.
548  * @param numavailports: number of available ports in array.
549  * @param unwanted_threshold: when to take defensive action.
550  * @param unwanted_action: the action to take.
551  * @param unwanted_param: user parameter to action.
552  * @param tcp_mss: maximum segment size of tcp socket.
553  * @param do_udp: if udp is done.
554  * @param sslctx: context to create outgoing connections with (if enabled).
555  * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
556  * 	msec to wait on timeouted udp sockets.
557  * @param tls_use_sni: if SNI is used for TLS connections.
558  * @param dtenv: environment to send dnstap events with (if enabled).
559  * @param udp_connect: if the udp_connect option is enabled.
560  * @param max_reuse_tcp_queries: max number of queries on a reuse connection.
561  * @param tcp_reuse_timeout: timeout for REUSE entries in milliseconds.
562  * @param tcp_auth_query_timeout: timeout in milliseconds for TCP queries to auth servers.
563  * @return: the new structure (with no pending answers) or NULL on error.
564  */
565 struct outside_network* outside_network_create(struct comm_base* base,
566 	size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
567 	int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache* infra,
568 	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
569 	int numavailports, size_t unwanted_threshold, int tcp_mss,
570 	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
571 	void* sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv,
572 	int udp_connect, int max_reuse_tcp_queries, int tcp_reuse_timeout,
573 	int tcp_auth_query_timeout);
574 
575 /**
576  * Delete outside_network structure.
577  * @param outnet: object to delete.
578  */
579 void outside_network_delete(struct outside_network* outnet);
580 
581 /**
582  * Prepare for quit. Sends no more queries, even if queued up.
583  * @param outnet: object to prepare for removal
584  */
585 void outside_network_quit_prepare(struct outside_network* outnet);
586 
587 /**
588  * Send UDP query, create pending answer.
589  * Changes the ID for the query to be random and unique for that destination.
590  * @param sq: serviced query.
591  * @param packet: wireformat query to send to destination.
592  * @param timeout: in milliseconds from now.
593  * @param callback: function to call on error, timeout or reply.
594  * @param callback_arg: user argument for callback function.
595  * @return: NULL on error for malloc or socket. Else the pending query object.
596  */
597 struct pending* pending_udp_query(struct serviced_query* sq,
598 	struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
599 	void* callback_arg);
600 
601 /**
602  * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
603  * checks id.
604  * @param sq: serviced query.
605  * @param packet: wireformat query to send to destination. copied from.
606  * @param timeout: in milliseconds from now.
607  *    Timer starts running now. Timer may expire if all buffers are used,
608  *    without any query been sent to the server yet.
609  * @param callback: function to call on error, timeout or reply.
610  * @param callback_arg: user argument for callback function.
611  * @return: false on error for malloc or socket. Else the pending TCP object.
612  */
613 struct waiting_tcp* pending_tcp_query(struct serviced_query* sq,
614 	struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
615 	void* callback_arg);
616 
617 /**
618  * Delete pending answer.
619  * @param outnet: outside network the pending query is part of.
620  *    Internal feature: if outnet is NULL, p is not unlinked from rbtree.
621  * @param p: deleted
622  */
623 void pending_delete(struct outside_network* outnet, struct pending* p);
624 
625 /**
626  * Perform a serviced query to the authoritative servers.
627  * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
628  * @param outnet: outside network, with rbtree of serviced queries.
629  * @param qinfo: query info.
630  * @param flags: flags u16 (host format), includes opcode, CD bit.
631  * @param dnssec: if set, DO bit is set in EDNS queries.
632  *	If the value includes BIT_CD, CD bit is set when in EDNS queries.
633  *	If the value includes BIT_DO, DO bit is set when in EDNS queries.
634  * @param want_dnssec: signatures are needed, without EDNS the answer is
635  * 	likely to be useless.
636  * @param nocaps: ignore use_caps_for_id and use unperturbed qname.
637  * @param check_ratelimit: if set, will check ratelimit before sending out.
638  * @param tcp_upstream: use TCP for upstream queries.
639  * @param ssl_upstream: use SSL for upstream queries.
640  * @param tls_auth_name: when ssl_upstream is true, use this name to check
641  * 	the server's peer certificate.
642  * @param addr: to which server to send the query.
643  * @param addrlen: length of addr.
644  * @param zone: name of the zone of the delegation point. wireformat dname.
645 	This is the delegation point name for which the server is deemed
646 	authoritative.
647  * @param zonelen: length of zone.
648  * @param qstate: module qstate. Mainly for inspecting the available
649  *	edns_opts_lists.
650  * @param callback: callback function.
651  * @param callback_arg: user argument to callback function.
652  * @param buff: scratch buffer to create query contents in. Empty on exit.
653  * @param env: the module environment.
654  * @param was_ratelimited: it will signal back if the query failed to pass the
655  *	ratelimit check.
656  * @return 0 on error, or pointer to serviced query that is used to answer
657  *	this serviced query may be shared with other callbacks as well.
658  */
659 struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
660 	struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec,
661 	int nocaps, int check_ratelimit, int tcp_upstream, int ssl_upstream,
662 	char* tls_auth_name, struct sockaddr_storage* addr, socklen_t addrlen,
663 	uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
664 	comm_point_callback_type* callback, void* callback_arg,
665 	struct sldns_buffer* buff, struct module_env* env, int* was_ratelimited);
666 
667 /**
668  * Remove service query callback.
669  * If that leads to zero callbacks, the query is completely cancelled.
670  * @param sq: serviced query to adjust.
671  * @param cb_arg: callback argument of callback that needs removal.
672  *	same as the callback_arg to outnet_serviced_query().
673  */
674 void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
675 
676 /**
677  * Get memory size in use by outside network.
678  * Counts buffers and outstanding query (serviced queries) malloced data.
679  * @param outnet: outside network structure.
680  * @return size in bytes.
681  */
682 size_t outnet_get_mem(struct outside_network* outnet);
683 
684 /**
685  * Get memory size in use by serviced query while it is servicing callbacks.
686  * This takes into account the pre-deleted status of it; it will be deleted
687  * when the callbacks are done.
688  * @param sq: serviced query.
689  * @return size in bytes.
690  */
691 size_t serviced_get_mem(struct serviced_query* sq);
692 
693 /** Pick random ID value for a tcp stream, avoids existing IDs. */
694 uint16_t reuse_tcp_select_id(struct reuse_tcp* reuse,
695 	struct outside_network* outnet);
696 
697 /** find element in tree by id */
698 struct waiting_tcp* reuse_tcp_by_id_find(struct reuse_tcp* reuse, uint16_t id);
699 
700 /** insert element in tree by id */
701 void reuse_tree_by_id_insert(struct reuse_tcp* reuse, struct waiting_tcp* w);
702 
703 /** insert element in tcp_reuse tree and LRU list */
704 int reuse_tcp_insert(struct outside_network* outnet,
705 	struct pending_tcp* pend_tcp);
706 
707 /** touch the LRU of the element */
708 void reuse_tcp_lru_touch(struct outside_network* outnet,
709 	struct reuse_tcp* reuse);
710 
711 /** remove element from tree and LRU list */
712 void reuse_tcp_remove_tree_list(struct outside_network* outnet,
713 	struct reuse_tcp* reuse);
714 
715 /** snip the last reuse_tcp element off of the LRU list if any */
716 struct reuse_tcp* reuse_tcp_lru_snip(struct outside_network* outnet);
717 
718 /** delete readwait waiting_tcp elements, deletes the elements in the list */
719 void reuse_del_readwait(rbtree_type* tree_by_id);
720 
721 /** get TCP file descriptor for address, returns -1 on failure,
722  * tcp_mss is 0 or maxseg size to set for TCP packets. */
723 int outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen,
724 	int tcp_mss, int dscp);
725 
726 /**
727  * Create udp commpoint suitable for sending packets to the destination.
728  * @param outnet: outside_network with the comm_base it is attached to,
729  * 	with the outgoing interfaces chosen from, and rnd gen for random.
730  * @param cb: callback function for the commpoint.
731  * @param cb_arg: callback argument for cb.
732  * @param to_addr: intended destination.
733  * @param to_addrlen: length of to_addr.
734  * @return commpoint that you can comm_point_send_udp_msg with, or NULL.
735  */
736 struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
737 	comm_point_callback_type* cb, void* cb_arg,
738 	struct sockaddr_storage* to_addr, socklen_t to_addrlen);
739 
740 /**
741  * Create tcp commpoint suitable for communication to the destination.
742  * It also performs connect() to the to_addr.
743  * @param outnet: outside_network with the comm_base it is attached to,
744  * 	and the tcp_mss.
745  * @param cb: callback function for the commpoint.
746  * @param cb_arg: callback argument for cb.
747  * @param to_addr: intended destination.
748  * @param to_addrlen: length of to_addr.
749  * @param query: initial packet to send writing, in buffer.  It is copied
750  * 	to the commpoint buffer that is created.
751  * @param timeout: timeout for the TCP connection.
752  * 	timeout in milliseconds, or -1 for no (change to the) timeout.
753  *	So seconds*1000.
754  * @param ssl: set to true for TLS.
755  * @param host: hostname for host name verification of TLS (or NULL if no TLS).
756  * @return tcp_out commpoint, or NULL.
757  */
758 struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
759 	comm_point_callback_type* cb, void* cb_arg,
760 	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
761 	struct sldns_buffer* query, int timeout, int ssl, char* host);
762 
763 /**
764  * Create http commpoint suitable for communication to the destination.
765  * Creates the http request buffer. It also performs connect() to the to_addr.
766  * @param outnet: outside_network with the comm_base it is attached to,
767  * 	and the tcp_mss.
768  * @param cb: callback function for the commpoint.
769  * @param cb_arg: callback argument for cb.
770  * @param to_addr: intended destination.
771  * @param to_addrlen: length of to_addr.
772  * @param timeout: timeout for the TCP connection.
773  * 	timeout in milliseconds, or -1 for no (change to the) timeout.
774  *	So seconds*1000.
775  * @param ssl: set to true for https.
776  * @param host: hostname to use for the destination. part of http request.
777  * @param path: pathname to lookup, eg. name of the file on the destination.
778  * @param cfg: running configuration for User-Agent setup.
779  * @return http_out commpoint, or NULL.
780  */
781 struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
782 	comm_point_callback_type* cb, void* cb_arg,
783 	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
784 	int ssl, char* host, char* path, struct config_file* cfg);
785 
786 /** connect tcp connection to addr, 0 on failure */
787 int outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen);
788 
789 /** callback for incoming udp answers from the network */
790 int outnet_udp_cb(struct comm_point* c, void* arg, int error,
791 	struct comm_reply *reply_info);
792 
793 /** callback for pending tcp connections */
794 int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
795 	struct comm_reply *reply_info);
796 
797 /** callback for udp timeout */
798 void pending_udp_timer_cb(void *arg);
799 
800 /** callback for udp delay for timeout */
801 void pending_udp_timer_delay_cb(void *arg);
802 
803 /** callback for outgoing TCP timer event */
804 void outnet_tcptimer(void* arg);
805 
806 /** callback to send serviced queries */
807 void serviced_timer_cb(void *arg);
808 
809 /** callback for serviced query UDP answers */
810 int serviced_udp_callback(struct comm_point* c, void* arg, int error,
811         struct comm_reply* rep);
812 
813 /** TCP reply or error callback for serviced queries */
814 int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
815         struct comm_reply* rep);
816 
817 /** compare function of pending rbtree */
818 int pending_cmp(const void* key1, const void* key2);
819 
820 /** compare function of serviced query rbtree */
821 int serviced_cmp(const void* key1, const void* key2);
822 
823 /** compare function of reuse_tcp rbtree in outside_network struct */
824 int reuse_cmp(const void* key1, const void* key2);
825 
826 /** compare function of reuse_tcp tree_by_id rbtree */
827 int reuse_id_cmp(const void* key1, const void* key2);
828 
829 #endif /* OUTSIDE_NETWORK_H */
830