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