xref: /freebsd/contrib/unbound/services/outside_network.h (revision f44e67d120ad78ef7894241b519ee79fd190a16e)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * services/outside_network.h - listen to answers from the network
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file has functions to send queries to authoritative servers,
40b7579f77SDag-Erling Smørgrav  * and wait for the pending answer, with timeouts.
41b7579f77SDag-Erling Smørgrav  */
42b7579f77SDag-Erling Smørgrav 
43b7579f77SDag-Erling Smørgrav #ifndef OUTSIDE_NETWORK_H
44b7579f77SDag-Erling Smørgrav #define OUTSIDE_NETWORK_H
45b7579f77SDag-Erling Smørgrav 
46b7579f77SDag-Erling Smørgrav #include "util/rbtree.h"
47b7579f77SDag-Erling Smørgrav #include "util/netevent.h"
48ff825849SDag-Erling Smørgrav #include "dnstap/dnstap_config.h"
49b7579f77SDag-Erling Smørgrav struct pending;
50b7579f77SDag-Erling Smørgrav struct pending_timeout;
51b7579f77SDag-Erling Smørgrav struct ub_randstate;
52b7579f77SDag-Erling Smørgrav struct pending_tcp;
53b7579f77SDag-Erling Smørgrav struct waiting_tcp;
54b7579f77SDag-Erling Smørgrav struct waiting_udp;
55369c6923SCy Schubert struct reuse_tcp;
56b7579f77SDag-Erling Smørgrav struct infra_cache;
57b7579f77SDag-Erling Smørgrav struct port_comm;
58b7579f77SDag-Erling Smørgrav struct port_if;
5917d15b25SDag-Erling Smørgrav struct sldns_buffer;
60ff825849SDag-Erling Smørgrav struct serviced_query;
61ff825849SDag-Erling Smørgrav struct dt_env;
62e2d15004SDag-Erling Smørgrav struct edns_option;
63bc892140SDag-Erling Smørgrav struct module_env;
64bc892140SDag-Erling Smørgrav struct module_qstate;
65bc892140SDag-Erling Smørgrav struct query_info;
66b7579f77SDag-Erling Smørgrav 
67b7579f77SDag-Erling Smørgrav /**
68b7579f77SDag-Erling Smørgrav  * Send queries to outside servers and wait for answers from servers.
69b7579f77SDag-Erling Smørgrav  * Contains answer-listen sockets.
70b7579f77SDag-Erling Smørgrav  */
71b7579f77SDag-Erling Smørgrav struct outside_network {
72b7579f77SDag-Erling Smørgrav 	/** Base for select calls */
73b7579f77SDag-Erling Smørgrav 	struct comm_base* base;
74b7579f77SDag-Erling Smørgrav 	/** pointer to time in seconds */
7517d15b25SDag-Erling Smørgrav 	time_t* now_secs;
76b7579f77SDag-Erling Smørgrav 	/** pointer to time in microseconds */
77b7579f77SDag-Erling Smørgrav 	struct timeval* now_tv;
78b7579f77SDag-Erling Smørgrav 
79b7579f77SDag-Erling Smørgrav 	/** buffer shared by UDP connections, since there is only one
80b7579f77SDag-Erling Smørgrav 	    datagram at any time. */
8117d15b25SDag-Erling Smørgrav 	struct sldns_buffer* udp_buff;
82b7579f77SDag-Erling Smørgrav 	/** serviced_callbacks malloc overhead when processing multiple
83b7579f77SDag-Erling Smørgrav 	 * identical serviced queries to the same server. */
84b7579f77SDag-Erling Smørgrav 	size_t svcd_overhead;
85b7579f77SDag-Erling Smørgrav 	/** use x20 bits to encode additional ID random bits */
86b7579f77SDag-Erling Smørgrav 	int use_caps_for_id;
87b7579f77SDag-Erling Smørgrav 	/** outside network wants to quit. Stop queued msgs from sent. */
88b7579f77SDag-Erling Smørgrav 	int want_to_quit;
89b7579f77SDag-Erling Smørgrav 
90b7579f77SDag-Erling Smørgrav 	/** number of unwanted replies received (for statistics) */
91b7579f77SDag-Erling Smørgrav 	size_t unwanted_replies;
92b7579f77SDag-Erling Smørgrav 	/** cumulative total of unwanted replies (for defense) */
93b7579f77SDag-Erling Smørgrav 	size_t unwanted_total;
94b7579f77SDag-Erling Smørgrav 	/** threshold when to take defensive action. If 0 then never. */
95b7579f77SDag-Erling Smørgrav 	size_t unwanted_threshold;
96b7579f77SDag-Erling Smørgrav 	/** what action to take, called when defensive action is needed */
97b7579f77SDag-Erling Smørgrav 	void (*unwanted_action)(void*);
98b7579f77SDag-Erling Smørgrav 	/** user param for action */
99b7579f77SDag-Erling Smørgrav 	void* unwanted_param;
100b7579f77SDag-Erling Smørgrav 
101b7579f77SDag-Erling Smørgrav 	/** linked list of available commpoints, unused file descriptors,
102b7579f77SDag-Erling Smørgrav 	 * for use as outgoing UDP ports. cp.fd=-1 in them. */
103b7579f77SDag-Erling Smørgrav 	struct port_comm* unused_fds;
104b7579f77SDag-Erling Smørgrav 	/** if udp is done */
105b7579f77SDag-Erling Smørgrav 	int do_udp;
10617d15b25SDag-Erling Smørgrav 	/** if udp is delay-closed (delayed answers do not meet closed port)*/
10717d15b25SDag-Erling Smørgrav 	int delayclose;
10817d15b25SDag-Erling Smørgrav 	/** timeout for delayclose */
10917d15b25SDag-Erling Smørgrav 	struct timeval delay_tv;
110369c6923SCy Schubert 	/** if we perform udp-connect, connect() for UDP socket to mitigate
111369c6923SCy Schubert 	 * ICMP side channel leakage */
112369c6923SCy Schubert 	int udp_connect;
113b7579f77SDag-Erling Smørgrav 
114b7579f77SDag-Erling Smørgrav 	/** array of outgoing IP4 interfaces */
115b7579f77SDag-Erling Smørgrav 	struct port_if* ip4_ifs;
116b7579f77SDag-Erling Smørgrav 	/** number of outgoing IP4 interfaces */
117b7579f77SDag-Erling Smørgrav 	int num_ip4;
118b7579f77SDag-Erling Smørgrav 
119b7579f77SDag-Erling Smørgrav 	/** array of outgoing IP6 interfaces */
120b7579f77SDag-Erling Smørgrav 	struct port_if* ip6_ifs;
121b7579f77SDag-Erling Smørgrav 	/** number of outgoing IP6 interfaces */
122b7579f77SDag-Erling Smørgrav 	int num_ip6;
123b7579f77SDag-Erling Smørgrav 
124b7579f77SDag-Erling Smørgrav 	/** pending udp queries waiting to be sent out, waiting for fd */
125b7579f77SDag-Erling Smørgrav 	struct pending* udp_wait_first;
126b7579f77SDag-Erling Smørgrav 	/** last pending udp query in list */
127b7579f77SDag-Erling Smørgrav 	struct pending* udp_wait_last;
128b7579f77SDag-Erling Smørgrav 
129b7579f77SDag-Erling Smørgrav 	/** pending udp answers. sorted by id, addr */
1303005e0a3SDag-Erling Smørgrav 	rbtree_type* pending;
131b7579f77SDag-Erling Smørgrav 	/** serviced queries, sorted by qbuf, addr, dnssec */
1323005e0a3SDag-Erling Smørgrav 	rbtree_type* serviced;
133b7579f77SDag-Erling Smørgrav 	/** host cache, pointer but not owned by outnet. */
134b7579f77SDag-Erling Smørgrav 	struct infra_cache* infra;
135b7579f77SDag-Erling Smørgrav 	/** where to get random numbers */
136b7579f77SDag-Erling Smørgrav 	struct ub_randstate* rnd;
137b7579f77SDag-Erling Smørgrav 	/** ssl context to create ssl wrapped TCP with DNS connections */
138b7579f77SDag-Erling Smørgrav 	void* sslctx;
13925039b37SCy Schubert 	/** if SNI will be used for TLS connections */
14025039b37SCy Schubert 	int tls_use_sni;
141ff825849SDag-Erling Smørgrav #ifdef USE_DNSTAP
142ff825849SDag-Erling Smørgrav 	/** dnstap environment */
143ff825849SDag-Erling Smørgrav 	struct dt_env* dtenv;
144ff825849SDag-Erling Smørgrav #endif
145f61ef7f6SDag-Erling Smørgrav 	/** maximum segment size of tcp socket */
146f61ef7f6SDag-Erling Smørgrav 	int tcp_mss;
14725039b37SCy Schubert 	/** IP_TOS socket option requested on the sockets */
14825039b37SCy Schubert 	int ip_dscp;
149b7579f77SDag-Erling Smørgrav 
150b7579f77SDag-Erling Smørgrav 	/**
151b7579f77SDag-Erling Smørgrav 	 * Array of tcp pending used for outgoing TCP connections.
152b7579f77SDag-Erling Smørgrav 	 * Each can be used to establish a TCP connection with a server.
153b7579f77SDag-Erling Smørgrav 	 * The file descriptors are -1 if they are free, and need to be
154b7579f77SDag-Erling Smørgrav 	 * opened for the tcp connection. Can be used for ip4 and ip6.
155b7579f77SDag-Erling Smørgrav 	 */
156b7579f77SDag-Erling Smørgrav 	struct pending_tcp **tcp_conns;
157b7579f77SDag-Erling Smørgrav 	/** number of tcp communication points. */
158b7579f77SDag-Erling Smørgrav 	size_t num_tcp;
159ff825849SDag-Erling Smørgrav 	/** number of tcp communication points in use. */
160ff825849SDag-Erling Smørgrav 	size_t num_tcp_outgoing;
161369c6923SCy Schubert 	/**
162369c6923SCy Schubert 	 * tree of still-open and waiting tcp connections for reuse.
163369c6923SCy Schubert 	 * can be closed and reopened to get a new tcp connection.
164369c6923SCy Schubert 	 * or reused to the same destination again.  with timeout to close.
165369c6923SCy Schubert 	 * Entries are of type struct reuse_tcp.
166369c6923SCy Schubert 	 * The entries are both active and empty connections.
167369c6923SCy Schubert 	 */
168369c6923SCy Schubert 	rbtree_type tcp_reuse;
169369c6923SCy Schubert 	/** max number of tcp_reuse entries we want to keep open */
170369c6923SCy Schubert 	size_t tcp_reuse_max;
171369c6923SCy Schubert 	/** first and last(oldest) in lru list of reuse connections.
172369c6923SCy Schubert 	 * the oldest can be closed to get a new free pending_tcp if needed
173369c6923SCy Schubert 	 * The list contains empty connections, that wait for timeout or
174369c6923SCy Schubert 	 * a new query that can use the existing connection. */
175369c6923SCy Schubert 	struct reuse_tcp* tcp_reuse_first, *tcp_reuse_last;
176b7579f77SDag-Erling Smørgrav 	/** list of tcp comm points that are free for use */
177b7579f77SDag-Erling Smørgrav 	struct pending_tcp* tcp_free;
178b7579f77SDag-Erling Smørgrav 	/** list of tcp queries waiting for a buffer */
179b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* tcp_wait_first;
180b7579f77SDag-Erling Smørgrav 	/** last of waiting query list */
181b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* tcp_wait_last;
182b7579f77SDag-Erling Smørgrav };
183b7579f77SDag-Erling Smørgrav 
184b7579f77SDag-Erling Smørgrav /**
185b7579f77SDag-Erling Smørgrav  * Outgoing interface. Ports available and currently used are tracked
186b7579f77SDag-Erling Smørgrav  * per interface
187b7579f77SDag-Erling Smørgrav  */
188b7579f77SDag-Erling Smørgrav struct port_if {
189b7579f77SDag-Erling Smørgrav 	/** address ready to allocate new socket (except port no). */
190b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
191b7579f77SDag-Erling Smørgrav 	/** length of addr field */
192b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
193b7579f77SDag-Erling Smørgrav 
194b5663de9SDag-Erling Smørgrav 	/** prefix length of network address (in bits), for randomisation.
195b5663de9SDag-Erling Smørgrav 	 * if 0, no randomisation. */
196b5663de9SDag-Erling Smørgrav 	int pfxlen;
197b5663de9SDag-Erling Smørgrav 
19825039b37SCy Schubert #ifndef DISABLE_EXPLICIT_PORT_RANDOMISATION
199b7579f77SDag-Erling Smørgrav 	/** the available ports array. These are unused.
200b7579f77SDag-Erling Smørgrav 	 * Only the first total-inuse part is filled. */
201b7579f77SDag-Erling Smørgrav 	int* avail_ports;
202b7579f77SDag-Erling Smørgrav 	/** the total number of available ports (size of the array) */
203b7579f77SDag-Erling Smørgrav 	int avail_total;
20425039b37SCy Schubert #endif
205b7579f77SDag-Erling Smørgrav 
206b7579f77SDag-Erling Smørgrav 	/** array of the commpoints currently in use.
207b7579f77SDag-Erling Smørgrav 	 * allocated for max number of fds, first part in use. */
208b7579f77SDag-Erling Smørgrav 	struct port_comm** out;
209b7579f77SDag-Erling Smørgrav 	/** max number of fds, size of out array */
210b7579f77SDag-Erling Smørgrav 	int maxout;
211b7579f77SDag-Erling Smørgrav 	/** number of commpoints (and thus also ports) in use */
212b7579f77SDag-Erling Smørgrav 	int inuse;
213b7579f77SDag-Erling Smørgrav };
214b7579f77SDag-Erling Smørgrav 
215b7579f77SDag-Erling Smørgrav /**
216b7579f77SDag-Erling Smørgrav  * Outgoing commpoint for UDP port.
217b7579f77SDag-Erling Smørgrav  */
218b7579f77SDag-Erling Smørgrav struct port_comm {
219b7579f77SDag-Erling Smørgrav 	/** next in free list */
220b7579f77SDag-Erling Smørgrav 	struct port_comm* next;
221b7579f77SDag-Erling Smørgrav 	/** which port number (when in use) */
222b7579f77SDag-Erling Smørgrav 	int number;
223b7579f77SDag-Erling Smørgrav 	/** interface it is used in */
224b7579f77SDag-Erling Smørgrav 	struct port_if* pif;
225b7579f77SDag-Erling Smørgrav 	/** index in the out array of the interface */
226b7579f77SDag-Erling Smørgrav 	int index;
227b7579f77SDag-Erling Smørgrav 	/** number of outstanding queries on this port */
228b7579f77SDag-Erling Smørgrav 	int num_outstanding;
229b7579f77SDag-Erling Smørgrav 	/** UDP commpoint, fd=-1 if not in use */
230b7579f77SDag-Erling Smørgrav 	struct comm_point* cp;
231b7579f77SDag-Erling Smørgrav };
232b7579f77SDag-Erling Smørgrav 
233b7579f77SDag-Erling Smørgrav /**
234369c6923SCy Schubert  * Reuse TCP connection, still open can be used again.
235369c6923SCy Schubert  */
236369c6923SCy Schubert struct reuse_tcp {
237369c6923SCy Schubert 	/** rbtree node with links in tcp_reuse tree. key is NULL when not
238369c6923SCy Schubert 	 * in tree. Both active and empty connections are in the tree.
239369c6923SCy Schubert 	 * key is a pointer to this structure, the members used to compare
240369c6923SCy Schubert 	 * are the sockaddr and and then is-ssl bool, and then ptr value is
241369c6923SCy Schubert 	 * used in case the same address exists several times in the tree
242369c6923SCy Schubert 	 * when there are multiple connections to the same destination to
243369c6923SCy Schubert 	 * make the rbtree items unique. */
244369c6923SCy Schubert 	rbnode_type node;
245369c6923SCy Schubert 	/** the key for the tcp_reuse tree. address of peer, ip4 or ip6,
246369c6923SCy Schubert 	 * and port number of peer */
247369c6923SCy Schubert 	struct sockaddr_storage addr;
248369c6923SCy Schubert 	/** length of addr */
249369c6923SCy Schubert 	socklen_t addrlen;
250369c6923SCy Schubert 	/** also key for tcp_reuse tree, if ssl is used */
251369c6923SCy Schubert 	int is_ssl;
252369c6923SCy Schubert 	/** lru chain, so that the oldest can be removed to get a new
253369c6923SCy Schubert 	 * connection when all are in (re)use. oldest is last in list.
254369c6923SCy Schubert 	 * The lru only contains empty connections waiting for reuse,
255369c6923SCy Schubert 	 * the ones with active queries are not on the list because they
256369c6923SCy Schubert 	 * do not need to be closed to make space for others.  They already
257369c6923SCy Schubert 	 * service a query so the close for another query does not help
258369c6923SCy Schubert 	 * service a larger number of queries. */
259369c6923SCy Schubert 	struct reuse_tcp* lru_next, *lru_prev;
260369c6923SCy Schubert 	/** true if the reuse_tcp item is on the lru list with empty items */
261369c6923SCy Schubert 	int item_on_lru_list;
262369c6923SCy Schubert 	/** the connection to reuse, the fd is non-1 and is open.
263369c6923SCy Schubert 	 * the addr and port determine where the connection is going,
264369c6923SCy Schubert 	 * and is key to the rbtree.  The SSL ptr determines if it is
265369c6923SCy Schubert 	 * a TLS connection or a plain TCP connection there.  And TLS
266369c6923SCy Schubert 	 * or not is also part of the key to the rbtree.
267369c6923SCy Schubert 	 * There is a timeout and read event on the fd, to close it. */
268369c6923SCy Schubert 	struct pending_tcp* pending;
269369c6923SCy Schubert 	/**
270369c6923SCy Schubert 	 * The more read again value pointed to by the commpoint
271369c6923SCy Schubert 	 * tcp_more_read_again pointer, so that it exists after commpoint
272369c6923SCy Schubert 	 * delete
273369c6923SCy Schubert 	 */
274369c6923SCy Schubert 	int cp_more_read_again;
275369c6923SCy Schubert 	/**
276369c6923SCy Schubert 	 * The more write again value pointed to by the commpoint
277369c6923SCy Schubert 	 * tcp_more_write_again pointer, so that it exists after commpoint
278369c6923SCy Schubert 	 * delete
279369c6923SCy Schubert 	 */
280369c6923SCy Schubert 	int cp_more_write_again;
281369c6923SCy Schubert 	/** rbtree with other queries waiting on the connection, by ID number,
282369c6923SCy Schubert 	 * of type struct waiting_tcp. It is for looking up received
283369c6923SCy Schubert 	 * answers to the structure for callback.  And also to see if ID
284369c6923SCy Schubert 	 * numbers are unused and can be used for a new query.
285369c6923SCy Schubert 	 * The write_wait elements are also in the tree, so that ID numbers
286369c6923SCy Schubert 	 * can be looked up also for them.  They are bool write_wait_queued. */
287369c6923SCy Schubert 	rbtree_type tree_by_id;
288369c6923SCy Schubert 	/** list of queries waiting to be written on the channel,
289369c6923SCy Schubert 	 * if NULL no queries are waiting to be written and the pending->query
290369c6923SCy Schubert 	 * is the query currently serviced.  The first is the next in line.
291369c6923SCy Schubert 	 * They are also in the tree_by_id. Once written, the are removed
292369c6923SCy Schubert 	 * from this list, but stay in the tree. */
293369c6923SCy Schubert 	struct waiting_tcp* write_wait_first, *write_wait_last;
294369c6923SCy Schubert 	/** the outside network it is part of */
295369c6923SCy Schubert 	struct outside_network* outnet;
296369c6923SCy Schubert };
297369c6923SCy Schubert 
298369c6923SCy Schubert /** max number of queries on a reuse connection */
299369c6923SCy Schubert #define MAX_REUSE_TCP_QUERIES 200
300369c6923SCy Schubert /** timeout for REUSE entries in milliseconds. */
301369c6923SCy Schubert #define REUSE_TIMEOUT 60000
302369c6923SCy Schubert 
303369c6923SCy Schubert /**
304b7579f77SDag-Erling Smørgrav  * A query that has an answer pending for it.
305b7579f77SDag-Erling Smørgrav  */
306b7579f77SDag-Erling Smørgrav struct pending {
307b7579f77SDag-Erling Smørgrav 	/** redblacktree entry, key is the pending struct(id, addr). */
3083005e0a3SDag-Erling Smørgrav 	rbnode_type node;
309b7579f77SDag-Erling Smørgrav 	/** the ID for the query. int so that a value out of range can
310b7579f77SDag-Erling Smørgrav 	 * be used to signify a pending that is for certain not present in
311b7579f77SDag-Erling Smørgrav 	 * the rbtree. (and for which deletion is safe). */
312b7579f77SDag-Erling Smørgrav 	unsigned int id;
313b7579f77SDag-Erling Smørgrav 	/** remote address. */
314b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
315b7579f77SDag-Erling Smørgrav 	/** length of addr field in use. */
316b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
317b7579f77SDag-Erling Smørgrav 	/** comm point it was sent on (and reply must come back on). */
318b7579f77SDag-Erling Smørgrav 	struct port_comm* pc;
319b7579f77SDag-Erling Smørgrav 	/** timeout event */
320b7579f77SDag-Erling Smørgrav 	struct comm_timer* timer;
321b7579f77SDag-Erling Smørgrav 	/** callback for the timeout, error or reply to the message */
3223005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* cb;
323b7579f77SDag-Erling Smørgrav 	/** callback user argument */
324b7579f77SDag-Erling Smørgrav 	void* cb_arg;
325b7579f77SDag-Erling Smørgrav 	/** the outside network it is part of */
326b7579f77SDag-Erling Smørgrav 	struct outside_network* outnet;
327ff825849SDag-Erling Smørgrav 	/** the corresponding serviced_query */
328ff825849SDag-Erling Smørgrav 	struct serviced_query* sq;
329b7579f77SDag-Erling Smørgrav 
330b7579f77SDag-Erling Smørgrav 	/*---- filled if udp pending is waiting -----*/
331b7579f77SDag-Erling Smørgrav 	/** next in waiting list. */
332b7579f77SDag-Erling Smørgrav 	struct pending* next_waiting;
333b7579f77SDag-Erling Smørgrav 	/** timeout in msec */
334b7579f77SDag-Erling Smørgrav 	int timeout;
335b7579f77SDag-Erling Smørgrav 	/** The query itself, the query packet to send. */
336b7579f77SDag-Erling Smørgrav 	uint8_t* pkt;
337b7579f77SDag-Erling Smørgrav 	/** length of query packet. */
338b7579f77SDag-Erling Smørgrav 	size_t pkt_len;
339b7579f77SDag-Erling Smørgrav };
340b7579f77SDag-Erling Smørgrav 
341b7579f77SDag-Erling Smørgrav /**
342b7579f77SDag-Erling Smørgrav  * Pending TCP query to server.
343b7579f77SDag-Erling Smørgrav  */
344b7579f77SDag-Erling Smørgrav struct pending_tcp {
345b7579f77SDag-Erling Smørgrav 	/** next in list of free tcp comm points, or NULL. */
346b7579f77SDag-Erling Smørgrav 	struct pending_tcp* next_free;
347b7579f77SDag-Erling Smørgrav 	/** tcp comm point it was sent on (and reply must come back on). */
348b7579f77SDag-Erling Smørgrav 	struct comm_point* c;
349b7579f77SDag-Erling Smørgrav 	/** the query being serviced, NULL if the pending_tcp is unused. */
350b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* query;
351369c6923SCy Schubert 	/** the pre-allocated reuse tcp structure.  if ->pending is nonNULL
352369c6923SCy Schubert 	 * it is in use and the connection is waiting for reuse.
353369c6923SCy Schubert 	 * It is here for memory pre-allocation, and used to make this
354369c6923SCy Schubert 	 * pending_tcp wait for reuse. */
355369c6923SCy Schubert 	struct reuse_tcp reuse;
356b7579f77SDag-Erling Smørgrav };
357b7579f77SDag-Erling Smørgrav 
358b7579f77SDag-Erling Smørgrav /**
359b7579f77SDag-Erling Smørgrav  * Query waiting for TCP buffer.
360b7579f77SDag-Erling Smørgrav  */
361b7579f77SDag-Erling Smørgrav struct waiting_tcp {
362b7579f77SDag-Erling Smørgrav 	/**
363b7579f77SDag-Erling Smørgrav 	 * next in waiting list.
364369c6923SCy Schubert 	 * if on_tcp_waiting_list==0, this points to the pending_tcp structure.
365b7579f77SDag-Erling Smørgrav 	 */
366b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* next_waiting;
367369c6923SCy Schubert 	/** if true the item is on the tcp waiting list and next_waiting
368369c6923SCy Schubert 	 * is used for that.  If false, the next_waiting points to the
369369c6923SCy Schubert 	 * pending_tcp */
370369c6923SCy Schubert 	int on_tcp_waiting_list;
371369c6923SCy Schubert 	/** next and prev in query waiting list for stream connection */
372369c6923SCy Schubert 	struct waiting_tcp* write_wait_prev, *write_wait_next;
373369c6923SCy Schubert 	/** true if the waiting_tcp structure is on the write_wait queue */
374369c6923SCy Schubert 	int write_wait_queued;
375369c6923SCy Schubert 	/** entry in reuse.tree_by_id, if key is NULL, not in tree, otherwise,
376369c6923SCy Schubert 	 * this struct is key and sorted by ID (from waiting_tcp.id). */
377369c6923SCy Schubert 	rbnode_type id_node;
378369c6923SCy Schubert 	/** the ID for the query; checked in reply */
379369c6923SCy Schubert 	uint16_t id;
380b7579f77SDag-Erling Smørgrav 	/** timeout event; timer keeps running whether the query is
381b7579f77SDag-Erling Smørgrav 	 * waiting for a buffer or the tcp reply is pending */
382b7579f77SDag-Erling Smørgrav 	struct comm_timer* timer;
383369c6923SCy Schubert 	/** timeout in msec */
384369c6923SCy Schubert 	int timeout;
385b7579f77SDag-Erling Smørgrav 	/** the outside network it is part of */
386b7579f77SDag-Erling Smørgrav 	struct outside_network* outnet;
387b7579f77SDag-Erling Smørgrav 	/** remote address. */
388b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
389b7579f77SDag-Erling Smørgrav 	/** length of addr field in use. */
390b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
391b7579f77SDag-Erling Smørgrav 	/**
392b7579f77SDag-Erling Smørgrav 	 * The query itself, the query packet to send.
393b7579f77SDag-Erling Smørgrav 	 * allocated after the waiting_tcp structure.
394b7579f77SDag-Erling Smørgrav 	 */
395b7579f77SDag-Erling Smørgrav 	uint8_t* pkt;
396b7579f77SDag-Erling Smørgrav 	/** length of query packet. */
397b7579f77SDag-Erling Smørgrav 	size_t pkt_len;
398369c6923SCy Schubert 	/** callback for the timeout, error or reply to the message,
399369c6923SCy Schubert 	 * or NULL if no user is waiting. the entry uses an ID number.
400369c6923SCy Schubert 	 * a query that was written is no longer needed, but the ID number
401369c6923SCy Schubert 	 * and a reply will come back and can be ignored if NULL */
4023005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* cb;
403b7579f77SDag-Erling Smørgrav 	/** callback user argument */
404b7579f77SDag-Erling Smørgrav 	void* cb_arg;
405b7579f77SDag-Erling Smørgrav 	/** if it uses ssl upstream */
406b7579f77SDag-Erling Smørgrav 	int ssl_upstream;
4070fb34990SDag-Erling Smørgrav 	/** ref to the tls_auth_name from the serviced_query */
4080fb34990SDag-Erling Smørgrav 	char* tls_auth_name;
409369c6923SCy Schubert 	/** the packet was involved in an error, to stop looping errors */
410369c6923SCy Schubert 	int error_count;
411b7579f77SDag-Erling Smørgrav };
412b7579f77SDag-Erling Smørgrav 
413b7579f77SDag-Erling Smørgrav /**
414b7579f77SDag-Erling Smørgrav  * Callback to party interested in serviced query results.
415b7579f77SDag-Erling Smørgrav  */
416b7579f77SDag-Erling Smørgrav struct service_callback {
417b7579f77SDag-Erling Smørgrav 	/** next in callback list */
418b7579f77SDag-Erling Smørgrav 	struct service_callback* next;
419b7579f77SDag-Erling Smørgrav 	/** callback function */
4203005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* cb;
421b7579f77SDag-Erling Smørgrav 	/** user argument for callback function */
422b7579f77SDag-Erling Smørgrav 	void* cb_arg;
423b7579f77SDag-Erling Smørgrav };
424b7579f77SDag-Erling Smørgrav 
425b7579f77SDag-Erling Smørgrav /** fallback size for fragmentation for EDNS in IPv4 */
4268ed2b524SDag-Erling Smørgrav #define EDNS_FRAG_SIZE_IP4 1472
427b7579f77SDag-Erling Smørgrav /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
4288ed2b524SDag-Erling Smørgrav #define EDNS_FRAG_SIZE_IP6 1232
429b7579f77SDag-Erling Smørgrav 
430b7579f77SDag-Erling Smørgrav /**
431b7579f77SDag-Erling Smørgrav  * Query service record.
432b7579f77SDag-Erling Smørgrav  * Contains query and destination. UDP, TCP, EDNS are all tried.
433b7579f77SDag-Erling Smørgrav  * complete with retries and timeouts. A number of interested parties can
434b7579f77SDag-Erling Smørgrav  * receive a callback.
435b7579f77SDag-Erling Smørgrav  */
436b7579f77SDag-Erling Smørgrav struct serviced_query {
437b7579f77SDag-Erling Smørgrav 	/** The rbtree node, key is this record */
4383005e0a3SDag-Erling Smørgrav 	rbnode_type node;
439b7579f77SDag-Erling Smørgrav 	/** The query that needs to be answered. Starts with flags u16,
440b7579f77SDag-Erling Smørgrav 	 * then qdcount, ..., including qname, qtype, qclass. Does not include
441b7579f77SDag-Erling Smørgrav 	 * EDNS record. */
442b7579f77SDag-Erling Smørgrav 	uint8_t* qbuf;
443b7579f77SDag-Erling Smørgrav 	/** length of qbuf. */
444b7579f77SDag-Erling Smørgrav 	size_t qbuflen;
445b7579f77SDag-Erling Smørgrav 	/** If an EDNS section is included, the DO/CD bit will be turned on. */
446b7579f77SDag-Erling Smørgrav 	int dnssec;
447b7579f77SDag-Erling Smørgrav 	/** We want signatures, or else the answer is likely useless */
448b7579f77SDag-Erling Smørgrav 	int want_dnssec;
449ff825849SDag-Erling Smørgrav 	/** ignore capsforid */
450ff825849SDag-Erling Smørgrav 	int nocaps;
451b7579f77SDag-Erling Smørgrav 	/** tcp upstream used, use tcp, or ssl_upstream for SSL */
452b7579f77SDag-Erling Smørgrav 	int tcp_upstream, ssl_upstream;
4530fb34990SDag-Erling Smørgrav 	/** the name of the tls authentication name, eg. 'ns.example.com'
4540fb34990SDag-Erling Smørgrav 	 * or NULL */
4550fb34990SDag-Erling Smørgrav 	char* tls_auth_name;
456b7579f77SDag-Erling Smørgrav 	/** where to send it */
457b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
458b7579f77SDag-Erling Smørgrav 	/** length of addr field in use. */
459b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
460b7579f77SDag-Erling Smørgrav 	/** zone name, uncompressed domain name in wireformat */
461b7579f77SDag-Erling Smørgrav 	uint8_t* zone;
462b7579f77SDag-Erling Smørgrav 	/** length of zone name */
463b7579f77SDag-Erling Smørgrav 	size_t zonelen;
464b7579f77SDag-Erling Smørgrav 	/** qtype */
465b7579f77SDag-Erling Smørgrav 	int qtype;
466b7579f77SDag-Erling Smørgrav 	/** current status */
467b7579f77SDag-Erling Smørgrav 	enum serviced_query_status {
468b7579f77SDag-Erling Smørgrav 		/** initial status */
469b7579f77SDag-Erling Smørgrav 		serviced_initial,
470b7579f77SDag-Erling Smørgrav 		/** UDP with EDNS sent */
471b7579f77SDag-Erling Smørgrav 		serviced_query_UDP_EDNS,
472b7579f77SDag-Erling Smørgrav 		/** UDP without EDNS sent */
473b7579f77SDag-Erling Smørgrav 		serviced_query_UDP,
474b7579f77SDag-Erling Smørgrav 		/** TCP with EDNS sent */
475b7579f77SDag-Erling Smørgrav 		serviced_query_TCP_EDNS,
476b7579f77SDag-Erling Smørgrav 		/** TCP without EDNS sent */
477b7579f77SDag-Erling Smørgrav 		serviced_query_TCP,
478b7579f77SDag-Erling Smørgrav 		/** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
479b7579f77SDag-Erling Smørgrav 		serviced_query_UDP_EDNS_fallback,
480b7579f77SDag-Erling Smørgrav 		/** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
481b7579f77SDag-Erling Smørgrav 		serviced_query_TCP_EDNS_fallback,
482b7579f77SDag-Erling Smørgrav 		/** send UDP query with EDNS1480 (or 1280) */
483b7579f77SDag-Erling Smørgrav 		serviced_query_UDP_EDNS_FRAG
484b7579f77SDag-Erling Smørgrav 	}
485b7579f77SDag-Erling Smørgrav 		/** variable with current status */
486b7579f77SDag-Erling Smørgrav 		status;
487b7579f77SDag-Erling Smørgrav 	/** true if serviced_query is scheduled for deletion already */
488b7579f77SDag-Erling Smørgrav 	int to_be_deleted;
489b7579f77SDag-Erling Smørgrav 	/** number of UDP retries */
490b7579f77SDag-Erling Smørgrav 	int retry;
491b7579f77SDag-Erling Smørgrav 	/** time last UDP was sent */
492b7579f77SDag-Erling Smørgrav 	struct timeval last_sent_time;
4933bd4df0aSDag-Erling Smørgrav 	/** rtt of last message */
494b7579f77SDag-Erling Smørgrav 	int last_rtt;
495b7579f77SDag-Erling Smørgrav 	/** do we know edns probe status already, for UDP_EDNS queries */
496b7579f77SDag-Erling Smørgrav 	int edns_lame_known;
497e2d15004SDag-Erling Smørgrav 	/** edns options to use for sending upstream packet */
498e2d15004SDag-Erling Smørgrav 	struct edns_option* opt_list;
499b7579f77SDag-Erling Smørgrav 	/** outside network this is part of */
500b7579f77SDag-Erling Smørgrav 	struct outside_network* outnet;
501b7579f77SDag-Erling Smørgrav 	/** list of interested parties that need callback on results. */
502b7579f77SDag-Erling Smørgrav 	struct service_callback* cblist;
503b7579f77SDag-Erling Smørgrav 	/** the UDP or TCP query that is pending, see status which */
504b7579f77SDag-Erling Smørgrav 	void* pending;
505*f44e67d1SCy Schubert 	/** block size with which to pad encrypted queries (default: 128) */
506*f44e67d1SCy Schubert 	size_t padding_block_size;
507b7579f77SDag-Erling Smørgrav };
508b7579f77SDag-Erling Smørgrav 
509b7579f77SDag-Erling Smørgrav /**
510b7579f77SDag-Erling Smørgrav  * Create outside_network structure with N udp ports.
511b7579f77SDag-Erling Smørgrav  * @param base: the communication base to use for event handling.
512b7579f77SDag-Erling Smørgrav  * @param bufsize: size for network buffers.
513b7579f77SDag-Erling Smørgrav  * @param num_ports: number of udp ports to open per interface.
514b7579f77SDag-Erling Smørgrav  * @param ifs: interface names (or NULL for default interface).
515b7579f77SDag-Erling Smørgrav  *    These interfaces must be able to access all authoritative servers.
516b7579f77SDag-Erling Smørgrav  * @param num_ifs: number of names in array ifs.
517b7579f77SDag-Erling Smørgrav  * @param do_ip4: service IP4.
518b7579f77SDag-Erling Smørgrav  * @param do_ip6: service IP6.
519b7579f77SDag-Erling Smørgrav  * @param num_tcp: number of outgoing tcp buffers to preallocate.
52025039b37SCy Schubert  * @param dscp: DSCP to use.
521b7579f77SDag-Erling Smørgrav  * @param infra: pointer to infra cached used for serviced queries.
522b7579f77SDag-Erling Smørgrav  * @param rnd: stored to create random numbers for serviced queries.
523b7579f77SDag-Erling Smørgrav  * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
524b7579f77SDag-Erling Smørgrav  * @param availports: array of available ports.
525b7579f77SDag-Erling Smørgrav  * @param numavailports: number of available ports in array.
526b7579f77SDag-Erling Smørgrav  * @param unwanted_threshold: when to take defensive action.
527b7579f77SDag-Erling Smørgrav  * @param unwanted_action: the action to take.
528b7579f77SDag-Erling Smørgrav  * @param unwanted_param: user parameter to action.
529f61ef7f6SDag-Erling Smørgrav  * @param tcp_mss: maximum segment size of tcp socket.
530b7579f77SDag-Erling Smørgrav  * @param do_udp: if udp is done.
531b7579f77SDag-Erling Smørgrav  * @param sslctx: context to create outgoing connections with (if enabled).
53217d15b25SDag-Erling Smørgrav  * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
53317d15b25SDag-Erling Smørgrav  * 	msec to wait on timeouted udp sockets.
53425039b37SCy Schubert  * @param tls_use_sni: if SNI is used for TLS connections.
535ff825849SDag-Erling Smørgrav  * @param dtenv: environment to send dnstap events with (if enabled).
536369c6923SCy Schubert  * @param udp_connect: if the udp_connect option is enabled.
537b7579f77SDag-Erling Smørgrav  * @return: the new structure (with no pending answers) or NULL on error.
538b7579f77SDag-Erling Smørgrav  */
539b7579f77SDag-Erling Smørgrav struct outside_network* outside_network_create(struct comm_base* base,
540b7579f77SDag-Erling Smørgrav 	size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
54125039b37SCy Schubert 	int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache* infra,
542b7579f77SDag-Erling Smørgrav 	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
543f61ef7f6SDag-Erling Smørgrav 	int numavailports, size_t unwanted_threshold, int tcp_mss,
544b7579f77SDag-Erling Smørgrav 	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
545369c6923SCy Schubert 	void* sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv,
546369c6923SCy Schubert 	int udp_connect);
547b7579f77SDag-Erling Smørgrav 
548b7579f77SDag-Erling Smørgrav /**
549b7579f77SDag-Erling Smørgrav  * Delete outside_network structure.
550b7579f77SDag-Erling Smørgrav  * @param outnet: object to delete.
551b7579f77SDag-Erling Smørgrav  */
552b7579f77SDag-Erling Smørgrav void outside_network_delete(struct outside_network* outnet);
553b7579f77SDag-Erling Smørgrav 
554b7579f77SDag-Erling Smørgrav /**
555b7579f77SDag-Erling Smørgrav  * Prepare for quit. Sends no more queries, even if queued up.
556b7579f77SDag-Erling Smørgrav  * @param outnet: object to prepare for removal
557b7579f77SDag-Erling Smørgrav  */
558b7579f77SDag-Erling Smørgrav void outside_network_quit_prepare(struct outside_network* outnet);
559b7579f77SDag-Erling Smørgrav 
560b7579f77SDag-Erling Smørgrav /**
561b7579f77SDag-Erling Smørgrav  * Send UDP query, create pending answer.
562b7579f77SDag-Erling Smørgrav  * Changes the ID for the query to be random and unique for that destination.
563ff825849SDag-Erling Smørgrav  * @param sq: serviced query.
564b7579f77SDag-Erling Smørgrav  * @param packet: wireformat query to send to destination.
565b7579f77SDag-Erling Smørgrav  * @param timeout: in milliseconds from now.
566b7579f77SDag-Erling Smørgrav  * @param callback: function to call on error, timeout or reply.
567b7579f77SDag-Erling Smørgrav  * @param callback_arg: user argument for callback function.
568b7579f77SDag-Erling Smørgrav  * @return: NULL on error for malloc or socket. Else the pending query object.
569b7579f77SDag-Erling Smørgrav  */
570ff825849SDag-Erling Smørgrav struct pending* pending_udp_query(struct serviced_query* sq,
5713005e0a3SDag-Erling Smørgrav 	struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
572b7579f77SDag-Erling Smørgrav 	void* callback_arg);
573b7579f77SDag-Erling Smørgrav 
574b7579f77SDag-Erling Smørgrav /**
575b7579f77SDag-Erling Smørgrav  * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
576b7579f77SDag-Erling Smørgrav  * checks id.
577ff825849SDag-Erling Smørgrav  * @param sq: serviced query.
578b7579f77SDag-Erling Smørgrav  * @param packet: wireformat query to send to destination. copied from.
5793bd4df0aSDag-Erling Smørgrav  * @param timeout: in milliseconds from now.
580b7579f77SDag-Erling Smørgrav  *    Timer starts running now. Timer may expire if all buffers are used,
581b7579f77SDag-Erling Smørgrav  *    without any query been sent to the server yet.
582b7579f77SDag-Erling Smørgrav  * @param callback: function to call on error, timeout or reply.
583b7579f77SDag-Erling Smørgrav  * @param callback_arg: user argument for callback function.
584b7579f77SDag-Erling Smørgrav  * @return: false on error for malloc or socket. Else the pending TCP object.
585b7579f77SDag-Erling Smørgrav  */
586ff825849SDag-Erling Smørgrav struct waiting_tcp* pending_tcp_query(struct serviced_query* sq,
5873005e0a3SDag-Erling Smørgrav 	struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
588ff825849SDag-Erling Smørgrav 	void* callback_arg);
589b7579f77SDag-Erling Smørgrav 
590b7579f77SDag-Erling Smørgrav /**
591b7579f77SDag-Erling Smørgrav  * Delete pending answer.
592b7579f77SDag-Erling Smørgrav  * @param outnet: outside network the pending query is part of.
593b7579f77SDag-Erling Smørgrav  *    Internal feature: if outnet is NULL, p is not unlinked from rbtree.
594b7579f77SDag-Erling Smørgrav  * @param p: deleted
595b7579f77SDag-Erling Smørgrav  */
596b7579f77SDag-Erling Smørgrav void pending_delete(struct outside_network* outnet, struct pending* p);
597b7579f77SDag-Erling Smørgrav 
598b7579f77SDag-Erling Smørgrav /**
599b7579f77SDag-Erling Smørgrav  * Perform a serviced query to the authoritative servers.
600b7579f77SDag-Erling Smørgrav  * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
601b7579f77SDag-Erling Smørgrav  * @param outnet: outside network, with rbtree of serviced queries.
602bc892140SDag-Erling Smørgrav  * @param qinfo: query info.
603b7579f77SDag-Erling Smørgrav  * @param flags: flags u16 (host format), includes opcode, CD bit.
604b7579f77SDag-Erling Smørgrav  * @param dnssec: if set, DO bit is set in EDNS queries.
605b7579f77SDag-Erling Smørgrav  *	If the value includes BIT_CD, CD bit is set when in EDNS queries.
606b7579f77SDag-Erling Smørgrav  *	If the value includes BIT_DO, DO bit is set when in EDNS queries.
607b7579f77SDag-Erling Smørgrav  * @param want_dnssec: signatures are needed, without EDNS the answer is
608b7579f77SDag-Erling Smørgrav  * 	likely to be useless.
609ff825849SDag-Erling Smørgrav  * @param nocaps: ignore use_caps_for_id and use unperturbed qname.
610b7579f77SDag-Erling Smørgrav  * @param tcp_upstream: use TCP for upstream queries.
611b7579f77SDag-Erling Smørgrav  * @param ssl_upstream: use SSL for upstream queries.
6120fb34990SDag-Erling Smørgrav  * @param tls_auth_name: when ssl_upstream is true, use this name to check
6130fb34990SDag-Erling Smørgrav  * 	the server's peer certificate.
614b7579f77SDag-Erling Smørgrav  * @param addr: to which server to send the query.
615b7579f77SDag-Erling Smørgrav  * @param addrlen: length of addr.
616b7579f77SDag-Erling Smørgrav  * @param zone: name of the zone of the delegation point. wireformat dname.
617b7579f77SDag-Erling Smørgrav 	This is the delegation point name for which the server is deemed
618b7579f77SDag-Erling Smørgrav 	authoritative.
619b7579f77SDag-Erling Smørgrav  * @param zonelen: length of zone.
620bc892140SDag-Erling Smørgrav  * @param qstate: module qstate. Mainly for inspecting the available
621bc892140SDag-Erling Smørgrav  *	edns_opts_lists.
622bc892140SDag-Erling Smørgrav  * @param callback: callback function.
623bc892140SDag-Erling Smørgrav  * @param callback_arg: user argument to callback function.
624b7579f77SDag-Erling Smørgrav  * @param buff: scratch buffer to create query contents in. Empty on exit.
625bc892140SDag-Erling Smørgrav  * @param env: the module environment.
626b7579f77SDag-Erling Smørgrav  * @return 0 on error, or pointer to serviced query that is used to answer
627b7579f77SDag-Erling Smørgrav  *	this serviced query may be shared with other callbacks as well.
628b7579f77SDag-Erling Smørgrav  */
629b7579f77SDag-Erling Smørgrav struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
630bc892140SDag-Erling Smørgrav 	struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec,
6310fb34990SDag-Erling Smørgrav 	int nocaps, int tcp_upstream, int ssl_upstream, char* tls_auth_name,
632e2d15004SDag-Erling Smørgrav 	struct sockaddr_storage* addr, socklen_t addrlen, uint8_t* zone,
633bc892140SDag-Erling Smørgrav 	size_t zonelen, struct module_qstate* qstate,
6343005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback, void* callback_arg,
635bc892140SDag-Erling Smørgrav 	struct sldns_buffer* buff, struct module_env* env);
636b7579f77SDag-Erling Smørgrav 
637b7579f77SDag-Erling Smørgrav /**
638b7579f77SDag-Erling Smørgrav  * Remove service query callback.
639b7579f77SDag-Erling Smørgrav  * If that leads to zero callbacks, the query is completely cancelled.
640b7579f77SDag-Erling Smørgrav  * @param sq: serviced query to adjust.
641b7579f77SDag-Erling Smørgrav  * @param cb_arg: callback argument of callback that needs removal.
642b7579f77SDag-Erling Smørgrav  *	same as the callback_arg to outnet_serviced_query().
643b7579f77SDag-Erling Smørgrav  */
644b7579f77SDag-Erling Smørgrav void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
645b7579f77SDag-Erling Smørgrav 
646b7579f77SDag-Erling Smørgrav /**
647b7579f77SDag-Erling Smørgrav  * Get memory size in use by outside network.
648b7579f77SDag-Erling Smørgrav  * Counts buffers and outstanding query (serviced queries) malloced data.
649b7579f77SDag-Erling Smørgrav  * @param outnet: outside network structure.
650b7579f77SDag-Erling Smørgrav  * @return size in bytes.
651b7579f77SDag-Erling Smørgrav  */
652b7579f77SDag-Erling Smørgrav size_t outnet_get_mem(struct outside_network* outnet);
653b7579f77SDag-Erling Smørgrav 
654b7579f77SDag-Erling Smørgrav /**
655b7579f77SDag-Erling Smørgrav  * Get memory size in use by serviced query while it is servicing callbacks.
656b7579f77SDag-Erling Smørgrav  * This takes into account the pre-deleted status of it; it will be deleted
657b7579f77SDag-Erling Smørgrav  * when the callbacks are done.
658b7579f77SDag-Erling Smørgrav  * @param sq: serviced query.
659b7579f77SDag-Erling Smørgrav  * @return size in bytes.
660b7579f77SDag-Erling Smørgrav  */
661b7579f77SDag-Erling Smørgrav size_t serviced_get_mem(struct serviced_query* sq);
662b7579f77SDag-Erling Smørgrav 
663369c6923SCy Schubert /** Pick random ID value for a tcp stream, avoids existing IDs. */
664369c6923SCy Schubert uint16_t reuse_tcp_select_id(struct reuse_tcp* reuse,
665369c6923SCy Schubert 	struct outside_network* outnet);
666369c6923SCy Schubert 
667369c6923SCy Schubert /** find element in tree by id */
668369c6923SCy Schubert struct waiting_tcp* reuse_tcp_by_id_find(struct reuse_tcp* reuse, uint16_t id);
669369c6923SCy Schubert 
670369c6923SCy Schubert /** insert element in tree by id */
671369c6923SCy Schubert void reuse_tree_by_id_insert(struct reuse_tcp* reuse, struct waiting_tcp* w);
672369c6923SCy Schubert 
673369c6923SCy Schubert /** delete readwait waiting_tcp elements, deletes the elements in the list */
674369c6923SCy Schubert void reuse_del_readwait(rbtree_type* tree_by_id);
675369c6923SCy Schubert 
67657bddd21SDag-Erling Smørgrav /** get TCP file descriptor for address, returns -1 on failure,
67757bddd21SDag-Erling Smørgrav  * tcp_mss is 0 or maxseg size to set for TCP packets. */
67825039b37SCy Schubert int outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen, int tcp_mss, int dscp);
67957bddd21SDag-Erling Smørgrav 
68057bddd21SDag-Erling Smørgrav /**
68157bddd21SDag-Erling Smørgrav  * Create udp commpoint suitable for sending packets to the destination.
68257bddd21SDag-Erling Smørgrav  * @param outnet: outside_network with the comm_base it is attached to,
68357bddd21SDag-Erling Smørgrav  * 	with the outgoing interfaces chosen from, and rnd gen for random.
68457bddd21SDag-Erling Smørgrav  * @param cb: callback function for the commpoint.
68557bddd21SDag-Erling Smørgrav  * @param cb_arg: callback argument for cb.
68657bddd21SDag-Erling Smørgrav  * @param to_addr: intended destination.
68757bddd21SDag-Erling Smørgrav  * @param to_addrlen: length of to_addr.
68857bddd21SDag-Erling Smørgrav  * @return commpoint that you can comm_point_send_udp_msg with, or NULL.
68957bddd21SDag-Erling Smørgrav  */
69057bddd21SDag-Erling Smørgrav struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
69157bddd21SDag-Erling Smørgrav 	comm_point_callback_type* cb, void* cb_arg,
69257bddd21SDag-Erling Smørgrav 	struct sockaddr_storage* to_addr, socklen_t to_addrlen);
69357bddd21SDag-Erling Smørgrav 
69457bddd21SDag-Erling Smørgrav /**
69557bddd21SDag-Erling Smørgrav  * Create tcp commpoint suitable for communication to the destination.
69657bddd21SDag-Erling Smørgrav  * It also performs connect() to the to_addr.
69757bddd21SDag-Erling Smørgrav  * @param outnet: outside_network with the comm_base it is attached to,
69857bddd21SDag-Erling Smørgrav  * 	and the tcp_mss.
69957bddd21SDag-Erling Smørgrav  * @param cb: callback function for the commpoint.
70057bddd21SDag-Erling Smørgrav  * @param cb_arg: callback argument for cb.
70157bddd21SDag-Erling Smørgrav  * @param to_addr: intended destination.
70257bddd21SDag-Erling Smørgrav  * @param to_addrlen: length of to_addr.
70357bddd21SDag-Erling Smørgrav  * @param query: initial packet to send writing, in buffer.  It is copied
70457bddd21SDag-Erling Smørgrav  * 	to the commpoint buffer that is created.
70557bddd21SDag-Erling Smørgrav  * @param timeout: timeout for the TCP connection.
70657bddd21SDag-Erling Smørgrav  * 	timeout in milliseconds, or -1 for no (change to the) timeout.
70757bddd21SDag-Erling Smørgrav  *	So seconds*1000.
708e86b9096SDag-Erling Smørgrav  * @param ssl: set to true for TLS.
709e86b9096SDag-Erling Smørgrav  * @param host: hostname for host name verification of TLS (or NULL if no TLS).
71057bddd21SDag-Erling Smørgrav  * @return tcp_out commpoint, or NULL.
71157bddd21SDag-Erling Smørgrav  */
71257bddd21SDag-Erling Smørgrav struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
71357bddd21SDag-Erling Smørgrav 	comm_point_callback_type* cb, void* cb_arg,
71457bddd21SDag-Erling Smørgrav 	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
715e86b9096SDag-Erling Smørgrav 	struct sldns_buffer* query, int timeout, int ssl, char* host);
71657bddd21SDag-Erling Smørgrav 
71757bddd21SDag-Erling Smørgrav /**
71857bddd21SDag-Erling Smørgrav  * Create http commpoint suitable for communication to the destination.
71957bddd21SDag-Erling Smørgrav  * Creates the http request buffer. It also performs connect() to the to_addr.
72057bddd21SDag-Erling Smørgrav  * @param outnet: outside_network with the comm_base it is attached to,
72157bddd21SDag-Erling Smørgrav  * 	and the tcp_mss.
72257bddd21SDag-Erling Smørgrav  * @param cb: callback function for the commpoint.
72357bddd21SDag-Erling Smørgrav  * @param cb_arg: callback argument for cb.
72457bddd21SDag-Erling Smørgrav  * @param to_addr: intended destination.
72557bddd21SDag-Erling Smørgrav  * @param to_addrlen: length of to_addr.
72657bddd21SDag-Erling Smørgrav  * @param timeout: timeout for the TCP connection.
72757bddd21SDag-Erling Smørgrav  * 	timeout in milliseconds, or -1 for no (change to the) timeout.
72857bddd21SDag-Erling Smørgrav  *	So seconds*1000.
72957bddd21SDag-Erling Smørgrav  * @param ssl: set to true for https.
73057bddd21SDag-Erling Smørgrav  * @param host: hostname to use for the destination. part of http request.
73157bddd21SDag-Erling Smørgrav  * @param path: pathname to lookup, eg. name of the file on the destination.
73257bddd21SDag-Erling Smørgrav  * @return http_out commpoint, or NULL.
73357bddd21SDag-Erling Smørgrav  */
73457bddd21SDag-Erling Smørgrav struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
73557bddd21SDag-Erling Smørgrav 	comm_point_callback_type* cb, void* cb_arg,
73657bddd21SDag-Erling Smørgrav 	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
73757bddd21SDag-Erling Smørgrav 	int ssl, char* host, char* path);
73857bddd21SDag-Erling Smørgrav 
73957bddd21SDag-Erling Smørgrav /** connect tcp connection to addr, 0 on failure */
74057bddd21SDag-Erling Smørgrav int outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen);
74157bddd21SDag-Erling Smørgrav 
742b7579f77SDag-Erling Smørgrav /** callback for incoming udp answers from the network */
743b7579f77SDag-Erling Smørgrav int outnet_udp_cb(struct comm_point* c, void* arg, int error,
744b7579f77SDag-Erling Smørgrav 	struct comm_reply *reply_info);
745b7579f77SDag-Erling Smørgrav 
746b7579f77SDag-Erling Smørgrav /** callback for pending tcp connections */
747b7579f77SDag-Erling Smørgrav int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
748b7579f77SDag-Erling Smørgrav 	struct comm_reply *reply_info);
749b7579f77SDag-Erling Smørgrav 
750b7579f77SDag-Erling Smørgrav /** callback for udp timeout */
751b7579f77SDag-Erling Smørgrav void pending_udp_timer_cb(void *arg);
752b7579f77SDag-Erling Smørgrav 
75317d15b25SDag-Erling Smørgrav /** callback for udp delay for timeout */
75417d15b25SDag-Erling Smørgrav void pending_udp_timer_delay_cb(void *arg);
75517d15b25SDag-Erling Smørgrav 
756b7579f77SDag-Erling Smørgrav /** callback for outgoing TCP timer event */
757b7579f77SDag-Erling Smørgrav void outnet_tcptimer(void* arg);
758b7579f77SDag-Erling Smørgrav 
759b7579f77SDag-Erling Smørgrav /** callback for serviced query UDP answers */
760b7579f77SDag-Erling Smørgrav int serviced_udp_callback(struct comm_point* c, void* arg, int error,
761b7579f77SDag-Erling Smørgrav         struct comm_reply* rep);
762b7579f77SDag-Erling Smørgrav 
763b7579f77SDag-Erling Smørgrav /** TCP reply or error callback for serviced queries */
764b7579f77SDag-Erling Smørgrav int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
765b7579f77SDag-Erling Smørgrav         struct comm_reply* rep);
766b7579f77SDag-Erling Smørgrav 
767b7579f77SDag-Erling Smørgrav /** compare function of pending rbtree */
768b7579f77SDag-Erling Smørgrav int pending_cmp(const void* key1, const void* key2);
769b7579f77SDag-Erling Smørgrav 
770b7579f77SDag-Erling Smørgrav /** compare function of serviced query rbtree */
771b7579f77SDag-Erling Smørgrav int serviced_cmp(const void* key1, const void* key2);
772b7579f77SDag-Erling Smørgrav 
773369c6923SCy Schubert /** compare function of reuse_tcp rbtree in outside_network struct */
774369c6923SCy Schubert int reuse_cmp(const void* key1, const void* key2);
775369c6923SCy Schubert 
776369c6923SCy Schubert /** compare function of reuse_tcp tree_by_id rbtree */
777369c6923SCy Schubert int reuse_id_cmp(const void* key1, const void* key2);
778369c6923SCy Schubert 
779b7579f77SDag-Erling Smørgrav #endif /* OUTSIDE_NETWORK_H */
780