1933707f3Ssthen /*
2933707f3Ssthen  * services/outside_network.h - listen to answers from the network
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
245d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
255d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
265d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
275d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
285d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
295d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
305d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
315d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
325d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
335d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file has functions to send queries to authoritative servers,
40933707f3Ssthen  * and wait for the pending answer, with timeouts.
41933707f3Ssthen  */
42933707f3Ssthen 
43933707f3Ssthen #ifndef OUTSIDE_NETWORK_H
44933707f3Ssthen #define OUTSIDE_NETWORK_H
45933707f3Ssthen 
46e21c60efSsthen #include "util/alloc.h"
47933707f3Ssthen #include "util/rbtree.h"
48e21c60efSsthen #include "util/regional.h"
49933707f3Ssthen #include "util/netevent.h"
5098f3ca02Sbrad #include "dnstap/dnstap_config.h"
51933707f3Ssthen struct pending;
52933707f3Ssthen struct pending_timeout;
53933707f3Ssthen struct ub_randstate;
54933707f3Ssthen struct pending_tcp;
55933707f3Ssthen struct waiting_tcp;
56933707f3Ssthen struct waiting_udp;
57eba819a2Ssthen struct reuse_tcp;
58933707f3Ssthen struct infra_cache;
59933707f3Ssthen struct port_comm;
60933707f3Ssthen struct port_if;
615d76a658Ssthen struct sldns_buffer;
6298f3ca02Sbrad struct serviced_query;
6398f3ca02Sbrad struct dt_env;
642ee382b6Ssthen struct edns_option;
6577079be7Ssthen struct module_env;
6677079be7Ssthen struct module_qstate;
6777079be7Ssthen struct query_info;
68191f22c6Ssthen struct config_file;
69933707f3Ssthen 
70933707f3Ssthen /**
71933707f3Ssthen  * Send queries to outside servers and wait for answers from servers.
72933707f3Ssthen  * Contains answer-listen sockets.
73933707f3Ssthen  */
74933707f3Ssthen struct outside_network {
75933707f3Ssthen 	/** Base for select calls */
76933707f3Ssthen 	struct comm_base* base;
77933707f3Ssthen 	/** pointer to time in seconds */
78229e174cSsthen 	time_t* now_secs;
79933707f3Ssthen 	/** pointer to time in microseconds */
80933707f3Ssthen 	struct timeval* now_tv;
81933707f3Ssthen 
82933707f3Ssthen 	/** buffer shared by UDP connections, since there is only one
83933707f3Ssthen 	    datagram at any time. */
845d76a658Ssthen 	struct sldns_buffer* udp_buff;
85933707f3Ssthen 	/** serviced_callbacks malloc overhead when processing multiple
86933707f3Ssthen 	 * identical serviced queries to the same server. */
87933707f3Ssthen 	size_t svcd_overhead;
88933707f3Ssthen 	/** use x20 bits to encode additional ID random bits */
89933707f3Ssthen 	int use_caps_for_id;
90933707f3Ssthen 	/** outside network wants to quit. Stop queued msgs from sent. */
91933707f3Ssthen 	int want_to_quit;
92933707f3Ssthen 
93933707f3Ssthen 	/** number of unwanted replies received (for statistics) */
94933707f3Ssthen 	size_t unwanted_replies;
95933707f3Ssthen 	/** cumulative total of unwanted replies (for defense) */
96933707f3Ssthen 	size_t unwanted_total;
97933707f3Ssthen 	/** threshold when to take defensive action. If 0 then never. */
98933707f3Ssthen 	size_t unwanted_threshold;
99933707f3Ssthen 	/** what action to take, called when defensive action is needed */
100933707f3Ssthen 	void (*unwanted_action)(void*);
101933707f3Ssthen 	/** user param for action */
102933707f3Ssthen 	void* unwanted_param;
103933707f3Ssthen 
104933707f3Ssthen 	/** linked list of available commpoints, unused file descriptors,
105933707f3Ssthen 	 * for use as outgoing UDP ports. cp.fd=-1 in them. */
106933707f3Ssthen 	struct port_comm* unused_fds;
107933707f3Ssthen 	/** if udp is done */
108933707f3Ssthen 	int do_udp;
1095d76a658Ssthen 	/** if udp is delay-closed (delayed answers do not meet closed port)*/
1105d76a658Ssthen 	int delayclose;
1115d76a658Ssthen 	/** timeout for delayclose */
1125d76a658Ssthen 	struct timeval delay_tv;
113eba819a2Ssthen 	/** if we perform udp-connect, connect() for UDP socket to mitigate
114eba819a2Ssthen 	 * ICMP side channel leakage */
115eba819a2Ssthen 	int udp_connect;
116d1e2768aSsthen 	/** number of udp packets sent. */
117d1e2768aSsthen 	size_t num_udp_outgoing;
118933707f3Ssthen 
119933707f3Ssthen 	/** array of outgoing IP4 interfaces */
120933707f3Ssthen 	struct port_if* ip4_ifs;
121933707f3Ssthen 	/** number of outgoing IP4 interfaces */
122933707f3Ssthen 	int num_ip4;
123933707f3Ssthen 
124933707f3Ssthen 	/** array of outgoing IP6 interfaces */
125933707f3Ssthen 	struct port_if* ip6_ifs;
126933707f3Ssthen 	/** number of outgoing IP6 interfaces */
127933707f3Ssthen 	int num_ip6;
128933707f3Ssthen 
129933707f3Ssthen 	/** pending udp queries waiting to be sent out, waiting for fd */
130933707f3Ssthen 	struct pending* udp_wait_first;
131933707f3Ssthen 	/** last pending udp query in list */
132933707f3Ssthen 	struct pending* udp_wait_last;
133933707f3Ssthen 
134933707f3Ssthen 	/** pending udp answers. sorted by id, addr */
13577079be7Ssthen 	rbtree_type* pending;
136933707f3Ssthen 	/** serviced queries, sorted by qbuf, addr, dnssec */
13777079be7Ssthen 	rbtree_type* serviced;
138933707f3Ssthen 	/** host cache, pointer but not owned by outnet. */
139933707f3Ssthen 	struct infra_cache* infra;
140933707f3Ssthen 	/** where to get random numbers */
141933707f3Ssthen 	struct ub_randstate* rnd;
142933707f3Ssthen 	/** ssl context to create ssl wrapped TCP with DNS connections */
143933707f3Ssthen 	void* sslctx;
144a3167c07Ssthen 	/** if SNI will be used for TLS connections */
145a3167c07Ssthen 	int tls_use_sni;
14698f3ca02Sbrad #ifdef USE_DNSTAP
14798f3ca02Sbrad 	/** dnstap environment */
14898f3ca02Sbrad 	struct dt_env* dtenv;
14998f3ca02Sbrad #endif
15032e31f52Ssthen 	/** maximum segment size of tcp socket */
15132e31f52Ssthen 	int tcp_mss;
152a3167c07Ssthen 	/** IP_TOS socket option requested on the sockets */
153a3167c07Ssthen 	int ip_dscp;
154933707f3Ssthen 
155933707f3Ssthen 	/**
156933707f3Ssthen 	 * Array of tcp pending used for outgoing TCP connections.
157933707f3Ssthen 	 * Each can be used to establish a TCP connection with a server.
158933707f3Ssthen 	 * The file descriptors are -1 if they are free, and need to be
159933707f3Ssthen 	 * opened for the tcp connection. Can be used for ip4 and ip6.
160933707f3Ssthen 	 */
161933707f3Ssthen 	struct pending_tcp **tcp_conns;
162933707f3Ssthen 	/** number of tcp communication points. */
163933707f3Ssthen 	size_t num_tcp;
16498f3ca02Sbrad 	/** number of tcp communication points in use. */
16598f3ca02Sbrad 	size_t num_tcp_outgoing;
166191f22c6Ssthen 	/** max number of queries on a reuse connection */
167191f22c6Ssthen 	size_t max_reuse_tcp_queries;
168191f22c6Ssthen 	/** timeout for REUSE entries in milliseconds. */
169191f22c6Ssthen 	int tcp_reuse_timeout;
170191f22c6Ssthen 	/** timeout in milliseconds for TCP queries to auth servers. */
171191f22c6Ssthen 	int tcp_auth_query_timeout;
172eba819a2Ssthen 	/**
173eba819a2Ssthen 	 * tree of still-open and waiting tcp connections for reuse.
174eba819a2Ssthen 	 * can be closed and reopened to get a new tcp connection.
175eba819a2Ssthen 	 * or reused to the same destination again.  with timeout to close.
176eba819a2Ssthen 	 * Entries are of type struct reuse_tcp.
177eba819a2Ssthen 	 * The entries are both active and empty connections.
178eba819a2Ssthen 	 */
179eba819a2Ssthen 	rbtree_type tcp_reuse;
180eba819a2Ssthen 	/** max number of tcp_reuse entries we want to keep open */
181eba819a2Ssthen 	size_t tcp_reuse_max;
182eba819a2Ssthen 	/** first and last(oldest) in lru list of reuse connections.
183eba819a2Ssthen 	 * the oldest can be closed to get a new free pending_tcp if needed
184eba819a2Ssthen 	 * The list contains empty connections, that wait for timeout or
185eba819a2Ssthen 	 * a new query that can use the existing connection. */
186eba819a2Ssthen 	struct reuse_tcp* tcp_reuse_first, *tcp_reuse_last;
187933707f3Ssthen 	/** list of tcp comm points that are free for use */
188933707f3Ssthen 	struct pending_tcp* tcp_free;
189933707f3Ssthen 	/** list of tcp queries waiting for a buffer */
190933707f3Ssthen 	struct waiting_tcp* tcp_wait_first;
191933707f3Ssthen 	/** last of waiting query list */
192933707f3Ssthen 	struct waiting_tcp* tcp_wait_last;
193933707f3Ssthen };
194933707f3Ssthen 
195933707f3Ssthen /**
196933707f3Ssthen  * Outgoing interface. Ports available and currently used are tracked
197933707f3Ssthen  * per interface
198933707f3Ssthen  */
199933707f3Ssthen struct port_if {
200933707f3Ssthen 	/** address ready to allocate new socket (except port no). */
201933707f3Ssthen 	struct sockaddr_storage addr;
202933707f3Ssthen 	/** length of addr field */
203933707f3Ssthen 	socklen_t addrlen;
204933707f3Ssthen 
20577079be7Ssthen 	/** prefix length of network address (in bits), for randomisation.
20677079be7Ssthen 	 * if 0, no randomisation. */
20777079be7Ssthen 	int pfxlen;
20877079be7Ssthen 
209a3167c07Ssthen #ifndef DISABLE_EXPLICIT_PORT_RANDOMISATION
210933707f3Ssthen 	/** the available ports array. These are unused.
211933707f3Ssthen 	 * Only the first total-inuse part is filled. */
212933707f3Ssthen 	int* avail_ports;
213933707f3Ssthen 	/** the total number of available ports (size of the array) */
214933707f3Ssthen 	int avail_total;
215a3167c07Ssthen #endif
216933707f3Ssthen 
217933707f3Ssthen 	/** array of the commpoints currently in use.
218933707f3Ssthen 	 * allocated for max number of fds, first part in use. */
219933707f3Ssthen 	struct port_comm** out;
220933707f3Ssthen 	/** max number of fds, size of out array */
221933707f3Ssthen 	int maxout;
222933707f3Ssthen 	/** number of commpoints (and thus also ports) in use */
223933707f3Ssthen 	int inuse;
224933707f3Ssthen };
225933707f3Ssthen 
226933707f3Ssthen /**
227933707f3Ssthen  * Outgoing commpoint for UDP port.
228933707f3Ssthen  */
229933707f3Ssthen struct port_comm {
230933707f3Ssthen 	/** next in free list */
231933707f3Ssthen 	struct port_comm* next;
232933707f3Ssthen 	/** which port number (when in use) */
233933707f3Ssthen 	int number;
234933707f3Ssthen 	/** interface it is used in */
235933707f3Ssthen 	struct port_if* pif;
236933707f3Ssthen 	/** index in the out array of the interface */
237933707f3Ssthen 	int index;
238933707f3Ssthen 	/** number of outstanding queries on this port */
239933707f3Ssthen 	int num_outstanding;
240933707f3Ssthen 	/** UDP commpoint, fd=-1 if not in use */
241933707f3Ssthen 	struct comm_point* cp;
242933707f3Ssthen };
243933707f3Ssthen 
244933707f3Ssthen /**
245eba819a2Ssthen  * Reuse TCP connection, still open can be used again.
246eba819a2Ssthen  */
247eba819a2Ssthen struct reuse_tcp {
248eba819a2Ssthen 	/** rbtree node with links in tcp_reuse tree. key is NULL when not
249eba819a2Ssthen 	 * in tree. Both active and empty connections are in the tree.
250eba819a2Ssthen 	 * key is a pointer to this structure, the members used to compare
251eba819a2Ssthen 	 * are the sockaddr and and then is-ssl bool, and then ptr value is
252eba819a2Ssthen 	 * used in case the same address exists several times in the tree
253eba819a2Ssthen 	 * when there are multiple connections to the same destination to
254eba819a2Ssthen 	 * make the rbtree items unique. */
255eba819a2Ssthen 	rbnode_type node;
256eba819a2Ssthen 	/** the key for the tcp_reuse tree. address of peer, ip4 or ip6,
257eba819a2Ssthen 	 * and port number of peer */
258eba819a2Ssthen 	struct sockaddr_storage addr;
259eba819a2Ssthen 	/** length of addr */
260eba819a2Ssthen 	socklen_t addrlen;
261eba819a2Ssthen 	/** also key for tcp_reuse tree, if ssl is used */
262eba819a2Ssthen 	int is_ssl;
263eba819a2Ssthen 	/** lru chain, so that the oldest can be removed to get a new
264eba819a2Ssthen 	 * connection when all are in (re)use. oldest is last in list.
265eba819a2Ssthen 	 * The lru only contains empty connections waiting for reuse,
266eba819a2Ssthen 	 * the ones with active queries are not on the list because they
267eba819a2Ssthen 	 * do not need to be closed to make space for others.  They already
268eba819a2Ssthen 	 * service a query so the close for another query does not help
269eba819a2Ssthen 	 * service a larger number of queries. */
270eba819a2Ssthen 	struct reuse_tcp* lru_next, *lru_prev;
271eba819a2Ssthen 	/** true if the reuse_tcp item is on the lru list with empty items */
272eba819a2Ssthen 	int item_on_lru_list;
273eba819a2Ssthen 	/** the connection to reuse, the fd is non-1 and is open.
274eba819a2Ssthen 	 * the addr and port determine where the connection is going,
275eba819a2Ssthen 	 * and is key to the rbtree.  The SSL ptr determines if it is
276eba819a2Ssthen 	 * a TLS connection or a plain TCP connection there.  And TLS
277eba819a2Ssthen 	 * or not is also part of the key to the rbtree.
278eba819a2Ssthen 	 * There is a timeout and read event on the fd, to close it. */
279eba819a2Ssthen 	struct pending_tcp* pending;
280eba819a2Ssthen 	/**
281eba819a2Ssthen 	 * The more read again value pointed to by the commpoint
282eba819a2Ssthen 	 * tcp_more_read_again pointer, so that it exists after commpoint
283eba819a2Ssthen 	 * delete
284eba819a2Ssthen 	 */
285eba819a2Ssthen 	int cp_more_read_again;
286eba819a2Ssthen 	/**
287eba819a2Ssthen 	 * The more write again value pointed to by the commpoint
288eba819a2Ssthen 	 * tcp_more_write_again pointer, so that it exists after commpoint
289eba819a2Ssthen 	 * delete
290eba819a2Ssthen 	 */
291eba819a2Ssthen 	int cp_more_write_again;
292eba819a2Ssthen 	/** rbtree with other queries waiting on the connection, by ID number,
293eba819a2Ssthen 	 * of type struct waiting_tcp. It is for looking up received
294eba819a2Ssthen 	 * answers to the structure for callback.  And also to see if ID
295eba819a2Ssthen 	 * numbers are unused and can be used for a new query.
296eba819a2Ssthen 	 * The write_wait elements are also in the tree, so that ID numbers
297eba819a2Ssthen 	 * can be looked up also for them.  They are bool write_wait_queued. */
298eba819a2Ssthen 	rbtree_type tree_by_id;
299eba819a2Ssthen 	/** list of queries waiting to be written on the channel,
300eba819a2Ssthen 	 * if NULL no queries are waiting to be written and the pending->query
301eba819a2Ssthen 	 * is the query currently serviced.  The first is the next in line.
302eba819a2Ssthen 	 * They are also in the tree_by_id. Once written, the are removed
303eba819a2Ssthen 	 * from this list, but stay in the tree. */
304eba819a2Ssthen 	struct waiting_tcp* write_wait_first, *write_wait_last;
305eba819a2Ssthen 	/** the outside network it is part of */
306eba819a2Ssthen 	struct outside_network* outnet;
307eba819a2Ssthen };
308eba819a2Ssthen 
309eba819a2Ssthen /**
310933707f3Ssthen  * A query that has an answer pending for it.
311933707f3Ssthen  */
312933707f3Ssthen struct pending {
313933707f3Ssthen 	/** redblacktree entry, key is the pending struct(id, addr). */
31477079be7Ssthen 	rbnode_type node;
315933707f3Ssthen 	/** the ID for the query. int so that a value out of range can
316933707f3Ssthen 	 * be used to signify a pending that is for certain not present in
317933707f3Ssthen 	 * the rbtree. (and for which deletion is safe). */
318933707f3Ssthen 	unsigned int id;
319933707f3Ssthen 	/** remote address. */
320933707f3Ssthen 	struct sockaddr_storage addr;
321933707f3Ssthen 	/** length of addr field in use. */
322933707f3Ssthen 	socklen_t addrlen;
323933707f3Ssthen 	/** comm point it was sent on (and reply must come back on). */
324933707f3Ssthen 	struct port_comm* pc;
325933707f3Ssthen 	/** timeout event */
326933707f3Ssthen 	struct comm_timer* timer;
327933707f3Ssthen 	/** callback for the timeout, error or reply to the message */
32877079be7Ssthen 	comm_point_callback_type* cb;
329933707f3Ssthen 	/** callback user argument */
330933707f3Ssthen 	void* cb_arg;
331933707f3Ssthen 	/** the outside network it is part of */
332933707f3Ssthen 	struct outside_network* outnet;
33398f3ca02Sbrad 	/** the corresponding serviced_query */
33498f3ca02Sbrad 	struct serviced_query* sq;
335933707f3Ssthen 
336933707f3Ssthen 	/*---- filled if udp pending is waiting -----*/
337933707f3Ssthen 	/** next in waiting list. */
338933707f3Ssthen 	struct pending* next_waiting;
339933707f3Ssthen 	/** timeout in msec */
340933707f3Ssthen 	int timeout;
341933707f3Ssthen 	/** The query itself, the query packet to send. */
342933707f3Ssthen 	uint8_t* pkt;
343933707f3Ssthen 	/** length of query packet. */
344933707f3Ssthen 	size_t pkt_len;
345933707f3Ssthen };
346933707f3Ssthen 
347933707f3Ssthen /**
348933707f3Ssthen  * Pending TCP query to server.
349933707f3Ssthen  */
350933707f3Ssthen struct pending_tcp {
351933707f3Ssthen 	/** next in list of free tcp comm points, or NULL. */
352933707f3Ssthen 	struct pending_tcp* next_free;
353191f22c6Ssthen 	/** port for of the outgoing interface that is used */
354191f22c6Ssthen 	struct port_if* pi;
355933707f3Ssthen 	/** tcp comm point it was sent on (and reply must come back on). */
356933707f3Ssthen 	struct comm_point* c;
357933707f3Ssthen 	/** the query being serviced, NULL if the pending_tcp is unused. */
358933707f3Ssthen 	struct waiting_tcp* query;
359eba819a2Ssthen 	/** the pre-allocated reuse tcp structure.  if ->pending is nonNULL
360eba819a2Ssthen 	 * it is in use and the connection is waiting for reuse.
361eba819a2Ssthen 	 * It is here for memory pre-allocation, and used to make this
362eba819a2Ssthen 	 * pending_tcp wait for reuse. */
363eba819a2Ssthen 	struct reuse_tcp reuse;
364933707f3Ssthen };
365933707f3Ssthen 
366933707f3Ssthen /**
367933707f3Ssthen  * Query waiting for TCP buffer.
368933707f3Ssthen  */
369933707f3Ssthen struct waiting_tcp {
370933707f3Ssthen 	/**
371933707f3Ssthen 	 * next in waiting list.
372eba819a2Ssthen 	 * if on_tcp_waiting_list==0, this points to the pending_tcp structure.
373933707f3Ssthen 	 */
374933707f3Ssthen 	struct waiting_tcp* next_waiting;
375eba819a2Ssthen 	/** if true the item is on the tcp waiting list and next_waiting
376eba819a2Ssthen 	 * is used for that.  If false, the next_waiting points to the
377eba819a2Ssthen 	 * pending_tcp */
378eba819a2Ssthen 	int on_tcp_waiting_list;
379eba819a2Ssthen 	/** next and prev in query waiting list for stream connection */
380eba819a2Ssthen 	struct waiting_tcp* write_wait_prev, *write_wait_next;
381eba819a2Ssthen 	/** true if the waiting_tcp structure is on the write_wait queue */
382eba819a2Ssthen 	int write_wait_queued;
383eba819a2Ssthen 	/** entry in reuse.tree_by_id, if key is NULL, not in tree, otherwise,
384eba819a2Ssthen 	 * this struct is key and sorted by ID (from waiting_tcp.id). */
385eba819a2Ssthen 	rbnode_type id_node;
386eba819a2Ssthen 	/** the ID for the query; checked in reply */
387eba819a2Ssthen 	uint16_t id;
388933707f3Ssthen 	/** timeout event; timer keeps running whether the query is
389933707f3Ssthen 	 * waiting for a buffer or the tcp reply is pending */
390933707f3Ssthen 	struct comm_timer* timer;
391eba819a2Ssthen 	/** timeout in msec */
392eba819a2Ssthen 	int timeout;
393933707f3Ssthen 	/** the outside network it is part of */
394933707f3Ssthen 	struct outside_network* outnet;
395933707f3Ssthen 	/** remote address. */
396933707f3Ssthen 	struct sockaddr_storage addr;
397933707f3Ssthen 	/** length of addr field in use. */
398933707f3Ssthen 	socklen_t addrlen;
399933707f3Ssthen 	/**
400933707f3Ssthen 	 * The query itself, the query packet to send.
401933707f3Ssthen 	 * allocated after the waiting_tcp structure.
402933707f3Ssthen 	 */
403933707f3Ssthen 	uint8_t* pkt;
404933707f3Ssthen 	/** length of query packet. */
405933707f3Ssthen 	size_t pkt_len;
406eba819a2Ssthen 	/** callback for the timeout, error or reply to the message,
407eba819a2Ssthen 	 * or NULL if no user is waiting. the entry uses an ID number.
408eba819a2Ssthen 	 * a query that was written is no longer needed, but the ID number
409eba819a2Ssthen 	 * and a reply will come back and can be ignored if NULL */
41077079be7Ssthen 	comm_point_callback_type* cb;
411933707f3Ssthen 	/** callback user argument */
412933707f3Ssthen 	void* cb_arg;
413933707f3Ssthen 	/** if it uses ssl upstream */
414933707f3Ssthen 	int ssl_upstream;
41520237c55Ssthen 	/** ref to the tls_auth_name from the serviced_query */
41620237c55Ssthen 	char* tls_auth_name;
417eba819a2Ssthen 	/** the packet was involved in an error, to stop looping errors */
418eba819a2Ssthen 	int error_count;
419e21c60efSsthen 	/** if true, the item is at the cb_and_decommission stage */
420e21c60efSsthen 	int in_cb_and_decommission;
421191f22c6Ssthen #ifdef USE_DNSTAP
422191f22c6Ssthen 	/** serviced query pointer for dnstap to get logging info, if nonNULL*/
423191f22c6Ssthen 	struct serviced_query* sq;
424191f22c6Ssthen #endif
425933707f3Ssthen };
426933707f3Ssthen 
427933707f3Ssthen /**
428933707f3Ssthen  * Callback to party interested in serviced query results.
429933707f3Ssthen  */
430933707f3Ssthen struct service_callback {
431933707f3Ssthen 	/** next in callback list */
432933707f3Ssthen 	struct service_callback* next;
433933707f3Ssthen 	/** callback function */
43477079be7Ssthen 	comm_point_callback_type* cb;
435933707f3Ssthen 	/** user argument for callback function */
436933707f3Ssthen 	void* cb_arg;
437933707f3Ssthen };
438933707f3Ssthen 
439933707f3Ssthen /** fallback size for fragmentation for EDNS in IPv4 */
440229e174cSsthen #define EDNS_FRAG_SIZE_IP4 1472
441933707f3Ssthen /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
442229e174cSsthen #define EDNS_FRAG_SIZE_IP6 1232
443933707f3Ssthen 
444933707f3Ssthen /**
445933707f3Ssthen  * Query service record.
446933707f3Ssthen  * Contains query and destination. UDP, TCP, EDNS are all tried.
447933707f3Ssthen  * complete with retries and timeouts. A number of interested parties can
448933707f3Ssthen  * receive a callback.
449933707f3Ssthen  */
450933707f3Ssthen struct serviced_query {
451933707f3Ssthen 	/** The rbtree node, key is this record */
45277079be7Ssthen 	rbnode_type node;
453933707f3Ssthen 	/** The query that needs to be answered. Starts with flags u16,
454933707f3Ssthen 	 * then qdcount, ..., including qname, qtype, qclass. Does not include
455933707f3Ssthen 	 * EDNS record. */
456933707f3Ssthen 	uint8_t* qbuf;
457933707f3Ssthen 	/** length of qbuf. */
458933707f3Ssthen 	size_t qbuflen;
459933707f3Ssthen 	/** If an EDNS section is included, the DO/CD bit will be turned on. */
460933707f3Ssthen 	int dnssec;
461933707f3Ssthen 	/** We want signatures, or else the answer is likely useless */
462933707f3Ssthen 	int want_dnssec;
46398f3ca02Sbrad 	/** ignore capsforid */
46498f3ca02Sbrad 	int nocaps;
465933707f3Ssthen 	/** tcp upstream used, use tcp, or ssl_upstream for SSL */
466933707f3Ssthen 	int tcp_upstream, ssl_upstream;
46720237c55Ssthen 	/** the name of the tls authentication name, eg. 'ns.example.com'
46820237c55Ssthen 	 * or NULL */
46920237c55Ssthen 	char* tls_auth_name;
470933707f3Ssthen 	/** where to send it */
471933707f3Ssthen 	struct sockaddr_storage addr;
472933707f3Ssthen 	/** length of addr field in use. */
473933707f3Ssthen 	socklen_t addrlen;
474933707f3Ssthen 	/** zone name, uncompressed domain name in wireformat */
475933707f3Ssthen 	uint8_t* zone;
476933707f3Ssthen 	/** length of zone name */
477933707f3Ssthen 	size_t zonelen;
478d8d14d0cSsthen 	/** qtype */
479d8d14d0cSsthen 	int qtype;
480933707f3Ssthen 	/** current status */
481933707f3Ssthen 	enum serviced_query_status {
482933707f3Ssthen 		/** initial status */
483933707f3Ssthen 		serviced_initial,
484933707f3Ssthen 		/** UDP with EDNS sent */
485933707f3Ssthen 		serviced_query_UDP_EDNS,
486933707f3Ssthen 		/** UDP without EDNS sent */
487933707f3Ssthen 		serviced_query_UDP,
488933707f3Ssthen 		/** TCP with EDNS sent */
489933707f3Ssthen 		serviced_query_TCP_EDNS,
490933707f3Ssthen 		/** TCP without EDNS sent */
491933707f3Ssthen 		serviced_query_TCP,
492933707f3Ssthen 		/** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
493933707f3Ssthen 		serviced_query_UDP_EDNS_fallback,
494933707f3Ssthen 		/** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
495933707f3Ssthen 		serviced_query_TCP_EDNS_fallback,
496933707f3Ssthen 		/** send UDP query with EDNS1480 (or 1280) */
497933707f3Ssthen 		serviced_query_UDP_EDNS_FRAG
498933707f3Ssthen 	}
499933707f3Ssthen 		/** variable with current status */
500933707f3Ssthen 		status;
501933707f3Ssthen 	/** true if serviced_query is scheduled for deletion already */
502933707f3Ssthen 	int to_be_deleted;
503933707f3Ssthen 	/** number of UDP retries */
504933707f3Ssthen 	int retry;
505933707f3Ssthen 	/** time last UDP was sent */
506933707f3Ssthen 	struct timeval last_sent_time;
50720237c55Ssthen 	/** rtt of last message */
508933707f3Ssthen 	int last_rtt;
509933707f3Ssthen 	/** do we know edns probe status already, for UDP_EDNS queries */
510933707f3Ssthen 	int edns_lame_known;
5112ee382b6Ssthen 	/** edns options to use for sending upstream packet */
5122ee382b6Ssthen 	struct edns_option* opt_list;
513933707f3Ssthen 	/** outside network this is part of */
514933707f3Ssthen 	struct outside_network* outnet;
515933707f3Ssthen 	/** list of interested parties that need callback on results. */
516933707f3Ssthen 	struct service_callback* cblist;
517933707f3Ssthen 	/** the UDP or TCP query that is pending, see status which */
518933707f3Ssthen 	void* pending;
5199982a05dSsthen 	/** block size with which to pad encrypted queries (default: 128) */
5209982a05dSsthen 	size_t padding_block_size;
521e21c60efSsthen 	/** region for this serviced query. Will be cleared when this
522e21c60efSsthen 	 * serviced_query will be deleted */
523e21c60efSsthen 	struct regional* region;
524e21c60efSsthen 	/** allocation service for the region */
525e21c60efSsthen 	struct alloc_cache* alloc;
526e21c60efSsthen 	/** flash timer to start the net I/O as a separate event */
527e21c60efSsthen 	struct comm_timer* timer;
528e21c60efSsthen 	/** true if serviced_query is currently doing net I/O and may block */
529e21c60efSsthen 	int busy;
530933707f3Ssthen };
531933707f3Ssthen 
532933707f3Ssthen /**
533933707f3Ssthen  * Create outside_network structure with N udp ports.
534933707f3Ssthen  * @param base: the communication base to use for event handling.
535933707f3Ssthen  * @param bufsize: size for network buffers.
536933707f3Ssthen  * @param num_ports: number of udp ports to open per interface.
537933707f3Ssthen  * @param ifs: interface names (or NULL for default interface).
538933707f3Ssthen  *    These interfaces must be able to access all authoritative servers.
539933707f3Ssthen  * @param num_ifs: number of names in array ifs.
540933707f3Ssthen  * @param do_ip4: service IP4.
541933707f3Ssthen  * @param do_ip6: service IP6.
542933707f3Ssthen  * @param num_tcp: number of outgoing tcp buffers to preallocate.
543a3167c07Ssthen  * @param dscp: DSCP to use.
544933707f3Ssthen  * @param infra: pointer to infra cached used for serviced queries.
545933707f3Ssthen  * @param rnd: stored to create random numbers for serviced queries.
546933707f3Ssthen  * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
547933707f3Ssthen  * @param availports: array of available ports.
548933707f3Ssthen  * @param numavailports: number of available ports in array.
549933707f3Ssthen  * @param unwanted_threshold: when to take defensive action.
550933707f3Ssthen  * @param unwanted_action: the action to take.
551933707f3Ssthen  * @param unwanted_param: user parameter to action.
55232e31f52Ssthen  * @param tcp_mss: maximum segment size of tcp socket.
553933707f3Ssthen  * @param do_udp: if udp is done.
554933707f3Ssthen  * @param sslctx: context to create outgoing connections with (if enabled).
5555d76a658Ssthen  * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
5565d76a658Ssthen  * 	msec to wait on timeouted udp sockets.
557a3167c07Ssthen  * @param tls_use_sni: if SNI is used for TLS connections.
55898f3ca02Sbrad  * @param dtenv: environment to send dnstap events with (if enabled).
559eba819a2Ssthen  * @param udp_connect: if the udp_connect option is enabled.
560191f22c6Ssthen  * @param max_reuse_tcp_queries: max number of queries on a reuse connection.
561191f22c6Ssthen  * @param tcp_reuse_timeout: timeout for REUSE entries in milliseconds.
562191f22c6Ssthen  * @param tcp_auth_query_timeout: timeout in milliseconds for TCP queries to auth servers.
563933707f3Ssthen  * @return: the new structure (with no pending answers) or NULL on error.
564933707f3Ssthen  */
565933707f3Ssthen struct outside_network* outside_network_create(struct comm_base* base,
566933707f3Ssthen 	size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
567a3167c07Ssthen 	int do_ip4, int do_ip6, size_t num_tcp, int dscp, struct infra_cache* infra,
568933707f3Ssthen 	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
56932e31f52Ssthen 	int numavailports, size_t unwanted_threshold, int tcp_mss,
570933707f3Ssthen 	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
571eba819a2Ssthen 	void* sslctx, int delayclose, int tls_use_sni, struct dt_env *dtenv,
572191f22c6Ssthen 	int udp_connect, int max_reuse_tcp_queries, int tcp_reuse_timeout,
573191f22c6Ssthen 	int tcp_auth_query_timeout);
574933707f3Ssthen 
575933707f3Ssthen /**
576933707f3Ssthen  * Delete outside_network structure.
577933707f3Ssthen  * @param outnet: object to delete.
578933707f3Ssthen  */
579933707f3Ssthen void outside_network_delete(struct outside_network* outnet);
580933707f3Ssthen 
581933707f3Ssthen /**
582933707f3Ssthen  * Prepare for quit. Sends no more queries, even if queued up.
583933707f3Ssthen  * @param outnet: object to prepare for removal
584933707f3Ssthen  */
585933707f3Ssthen void outside_network_quit_prepare(struct outside_network* outnet);
586933707f3Ssthen 
587933707f3Ssthen /**
588933707f3Ssthen  * Send UDP query, create pending answer.
589933707f3Ssthen  * Changes the ID for the query to be random and unique for that destination.
59098f3ca02Sbrad  * @param sq: serviced query.
591933707f3Ssthen  * @param packet: wireformat query to send to destination.
592933707f3Ssthen  * @param timeout: in milliseconds from now.
593933707f3Ssthen  * @param callback: function to call on error, timeout or reply.
594933707f3Ssthen  * @param callback_arg: user argument for callback function.
595933707f3Ssthen  * @return: NULL on error for malloc or socket. Else the pending query object.
596933707f3Ssthen  */
59798f3ca02Sbrad struct pending* pending_udp_query(struct serviced_query* sq,
59877079be7Ssthen 	struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
599933707f3Ssthen 	void* callback_arg);
600933707f3Ssthen 
601933707f3Ssthen /**
602933707f3Ssthen  * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
603933707f3Ssthen  * checks id.
60498f3ca02Sbrad  * @param sq: serviced query.
605933707f3Ssthen  * @param packet: wireformat query to send to destination. copied from.
60620237c55Ssthen  * @param timeout: in milliseconds from now.
607933707f3Ssthen  *    Timer starts running now. Timer may expire if all buffers are used,
608933707f3Ssthen  *    without any query been sent to the server yet.
609933707f3Ssthen  * @param callback: function to call on error, timeout or reply.
610933707f3Ssthen  * @param callback_arg: user argument for callback function.
611933707f3Ssthen  * @return: false on error for malloc or socket. Else the pending TCP object.
612933707f3Ssthen  */
61398f3ca02Sbrad struct waiting_tcp* pending_tcp_query(struct serviced_query* sq,
61477079be7Ssthen 	struct sldns_buffer* packet, int timeout, comm_point_callback_type* callback,
61598f3ca02Sbrad 	void* callback_arg);
616933707f3Ssthen 
617933707f3Ssthen /**
618933707f3Ssthen  * Delete pending answer.
619933707f3Ssthen  * @param outnet: outside network the pending query is part of.
620933707f3Ssthen  *    Internal feature: if outnet is NULL, p is not unlinked from rbtree.
621933707f3Ssthen  * @param p: deleted
622933707f3Ssthen  */
623933707f3Ssthen void pending_delete(struct outside_network* outnet, struct pending* p);
624933707f3Ssthen 
625933707f3Ssthen /**
626933707f3Ssthen  * Perform a serviced query to the authoritative servers.
627933707f3Ssthen  * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
628933707f3Ssthen  * @param outnet: outside network, with rbtree of serviced queries.
62977079be7Ssthen  * @param qinfo: query info.
630933707f3Ssthen  * @param flags: flags u16 (host format), includes opcode, CD bit.
631933707f3Ssthen  * @param dnssec: if set, DO bit is set in EDNS queries.
632933707f3Ssthen  *	If the value includes BIT_CD, CD bit is set when in EDNS queries.
633933707f3Ssthen  *	If the value includes BIT_DO, DO bit is set when in EDNS queries.
634933707f3Ssthen  * @param want_dnssec: signatures are needed, without EDNS the answer is
635933707f3Ssthen  * 	likely to be useless.
63698f3ca02Sbrad  * @param nocaps: ignore use_caps_for_id and use unperturbed qname.
637e21c60efSsthen  * @param check_ratelimit: if set, will check ratelimit before sending out.
638933707f3Ssthen  * @param tcp_upstream: use TCP for upstream queries.
639933707f3Ssthen  * @param ssl_upstream: use SSL for upstream queries.
64020237c55Ssthen  * @param tls_auth_name: when ssl_upstream is true, use this name to check
64120237c55Ssthen  * 	the server's peer certificate.
642933707f3Ssthen  * @param addr: to which server to send the query.
643933707f3Ssthen  * @param addrlen: length of addr.
644933707f3Ssthen  * @param zone: name of the zone of the delegation point. wireformat dname.
645933707f3Ssthen 	This is the delegation point name for which the server is deemed
646933707f3Ssthen 	authoritative.
647933707f3Ssthen  * @param zonelen: length of zone.
64877079be7Ssthen  * @param qstate: module qstate. Mainly for inspecting the available
64977079be7Ssthen  *	edns_opts_lists.
65077079be7Ssthen  * @param callback: callback function.
65177079be7Ssthen  * @param callback_arg: user argument to callback function.
652933707f3Ssthen  * @param buff: scratch buffer to create query contents in. Empty on exit.
65377079be7Ssthen  * @param env: the module environment.
654e21c60efSsthen  * @param was_ratelimited: it will signal back if the query failed to pass the
655e21c60efSsthen  *	ratelimit check.
656933707f3Ssthen  * @return 0 on error, or pointer to serviced query that is used to answer
657933707f3Ssthen  *	this serviced query may be shared with other callbacks as well.
658933707f3Ssthen  */
659933707f3Ssthen struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
66077079be7Ssthen 	struct query_info* qinfo, uint16_t flags, int dnssec, int want_dnssec,
661e21c60efSsthen 	int nocaps, int check_ratelimit, int tcp_upstream, int ssl_upstream,
662e21c60efSsthen 	char* tls_auth_name, struct sockaddr_storage* addr, socklen_t addrlen,
663e21c60efSsthen 	uint8_t* zone, size_t zonelen, struct module_qstate* qstate,
66477079be7Ssthen 	comm_point_callback_type* callback, void* callback_arg,
665e21c60efSsthen 	struct sldns_buffer* buff, struct module_env* env, int* was_ratelimited);
666933707f3Ssthen 
667933707f3Ssthen /**
668933707f3Ssthen  * Remove service query callback.
669933707f3Ssthen  * If that leads to zero callbacks, the query is completely cancelled.
670933707f3Ssthen  * @param sq: serviced query to adjust.
671933707f3Ssthen  * @param cb_arg: callback argument of callback that needs removal.
672933707f3Ssthen  *	same as the callback_arg to outnet_serviced_query().
673933707f3Ssthen  */
674933707f3Ssthen void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
675933707f3Ssthen 
676933707f3Ssthen /**
677933707f3Ssthen  * Get memory size in use by outside network.
678933707f3Ssthen  * Counts buffers and outstanding query (serviced queries) malloced data.
679933707f3Ssthen  * @param outnet: outside network structure.
680933707f3Ssthen  * @return size in bytes.
681933707f3Ssthen  */
682933707f3Ssthen size_t outnet_get_mem(struct outside_network* outnet);
683933707f3Ssthen 
684933707f3Ssthen /**
685933707f3Ssthen  * Get memory size in use by serviced query while it is servicing callbacks.
686933707f3Ssthen  * This takes into account the pre-deleted status of it; it will be deleted
687933707f3Ssthen  * when the callbacks are done.
688933707f3Ssthen  * @param sq: serviced query.
689933707f3Ssthen  * @return size in bytes.
690933707f3Ssthen  */
691933707f3Ssthen size_t serviced_get_mem(struct serviced_query* sq);
692933707f3Ssthen 
693eba819a2Ssthen /** Pick random ID value for a tcp stream, avoids existing IDs. */
694eba819a2Ssthen uint16_t reuse_tcp_select_id(struct reuse_tcp* reuse,
695eba819a2Ssthen 	struct outside_network* outnet);
696eba819a2Ssthen 
697eba819a2Ssthen /** find element in tree by id */
698eba819a2Ssthen struct waiting_tcp* reuse_tcp_by_id_find(struct reuse_tcp* reuse, uint16_t id);
699eba819a2Ssthen 
700eba819a2Ssthen /** insert element in tree by id */
701eba819a2Ssthen void reuse_tree_by_id_insert(struct reuse_tcp* reuse, struct waiting_tcp* w);
702eba819a2Ssthen 
703191f22c6Ssthen /** insert element in tcp_reuse tree and LRU list */
704191f22c6Ssthen int reuse_tcp_insert(struct outside_network* outnet,
705191f22c6Ssthen 	struct pending_tcp* pend_tcp);
706191f22c6Ssthen 
707191f22c6Ssthen /** touch the LRU of the element */
708191f22c6Ssthen void reuse_tcp_lru_touch(struct outside_network* outnet,
709191f22c6Ssthen 	struct reuse_tcp* reuse);
710191f22c6Ssthen 
711191f22c6Ssthen /** remove element from tree and LRU list */
712191f22c6Ssthen void reuse_tcp_remove_tree_list(struct outside_network* outnet,
713191f22c6Ssthen 	struct reuse_tcp* reuse);
714191f22c6Ssthen 
715191f22c6Ssthen /** snip the last reuse_tcp element off of the LRU list if any */
716191f22c6Ssthen struct reuse_tcp* reuse_tcp_lru_snip(struct outside_network* outnet);
717191f22c6Ssthen 
718eba819a2Ssthen /** delete readwait waiting_tcp elements, deletes the elements in the list */
719eba819a2Ssthen void reuse_del_readwait(rbtree_type* tree_by_id);
720eba819a2Ssthen 
721*45872187Ssthen /** remove waiting tcp from the outnet waiting list */
722*45872187Ssthen void outnet_waiting_tcp_list_remove(struct outside_network* outnet,
723*45872187Ssthen 	struct waiting_tcp* w);
724*45872187Ssthen 
725*45872187Ssthen /** pop the first waiting tcp from the outnet waiting list */
726*45872187Ssthen struct waiting_tcp* outnet_waiting_tcp_list_pop(struct outside_network* outnet);
727*45872187Ssthen 
728*45872187Ssthen /** add waiting_tcp element to the outnet tcp waiting list */
729*45872187Ssthen void outnet_waiting_tcp_list_add(struct outside_network* outnet,
730*45872187Ssthen 	struct waiting_tcp* w, int set_timer);
731*45872187Ssthen 
732*45872187Ssthen /** add waiting_tcp element as first to the outnet tcp waiting list */
733*45872187Ssthen void outnet_waiting_tcp_list_add_first(struct outside_network* outnet,
734*45872187Ssthen 	struct waiting_tcp* w, int reset_timer);
735*45872187Ssthen 
736*45872187Ssthen /** pop the first element from the writewait list */
737*45872187Ssthen struct waiting_tcp* reuse_write_wait_pop(struct reuse_tcp* reuse);
738*45872187Ssthen 
739*45872187Ssthen /** remove the element from the writewait list */
740*45872187Ssthen void reuse_write_wait_remove(struct reuse_tcp* reuse, struct waiting_tcp* w);
741*45872187Ssthen 
742*45872187Ssthen /** push the element after the last on the writewait list */
743*45872187Ssthen void reuse_write_wait_push_back(struct reuse_tcp* reuse, struct waiting_tcp* w);
744*45872187Ssthen 
745bdfc4d55Sflorian /** get TCP file descriptor for address, returns -1 on failure,
746bdfc4d55Sflorian  * tcp_mss is 0 or maxseg size to set for TCP packets. */
747191f22c6Ssthen int outnet_get_tcp_fd(struct sockaddr_storage* addr, socklen_t addrlen,
748191f22c6Ssthen 	int tcp_mss, int dscp);
749bdfc4d55Sflorian 
750938a3a5eSflorian /**
751938a3a5eSflorian  * Create udp commpoint suitable for sending packets to the destination.
752938a3a5eSflorian  * @param outnet: outside_network with the comm_base it is attached to,
753938a3a5eSflorian  * 	with the outgoing interfaces chosen from, and rnd gen for random.
754938a3a5eSflorian  * @param cb: callback function for the commpoint.
755938a3a5eSflorian  * @param cb_arg: callback argument for cb.
756938a3a5eSflorian  * @param to_addr: intended destination.
757938a3a5eSflorian  * @param to_addrlen: length of to_addr.
758938a3a5eSflorian  * @return commpoint that you can comm_point_send_udp_msg with, or NULL.
759938a3a5eSflorian  */
760938a3a5eSflorian struct comm_point* outnet_comm_point_for_udp(struct outside_network* outnet,
761938a3a5eSflorian 	comm_point_callback_type* cb, void* cb_arg,
762938a3a5eSflorian 	struct sockaddr_storage* to_addr, socklen_t to_addrlen);
763938a3a5eSflorian 
764938a3a5eSflorian /**
765938a3a5eSflorian  * Create tcp commpoint suitable for communication to the destination.
766938a3a5eSflorian  * It also performs connect() to the to_addr.
767938a3a5eSflorian  * @param outnet: outside_network with the comm_base it is attached to,
768938a3a5eSflorian  * 	and the tcp_mss.
769938a3a5eSflorian  * @param cb: callback function for the commpoint.
770938a3a5eSflorian  * @param cb_arg: callback argument for cb.
771938a3a5eSflorian  * @param to_addr: intended destination.
772938a3a5eSflorian  * @param to_addrlen: length of to_addr.
773938a3a5eSflorian  * @param query: initial packet to send writing, in buffer.  It is copied
774938a3a5eSflorian  * 	to the commpoint buffer that is created.
775938a3a5eSflorian  * @param timeout: timeout for the TCP connection.
776938a3a5eSflorian  * 	timeout in milliseconds, or -1 for no (change to the) timeout.
777938a3a5eSflorian  *	So seconds*1000.
778550cf4a9Ssthen  * @param ssl: set to true for TLS.
779550cf4a9Ssthen  * @param host: hostname for host name verification of TLS (or NULL if no TLS).
780938a3a5eSflorian  * @return tcp_out commpoint, or NULL.
781938a3a5eSflorian  */
782938a3a5eSflorian struct comm_point* outnet_comm_point_for_tcp(struct outside_network* outnet,
783938a3a5eSflorian 	comm_point_callback_type* cb, void* cb_arg,
784938a3a5eSflorian 	struct sockaddr_storage* to_addr, socklen_t to_addrlen,
785550cf4a9Ssthen 	struct sldns_buffer* query, int timeout, int ssl, char* host);
786938a3a5eSflorian 
787938a3a5eSflorian /**
788938a3a5eSflorian  * Create http commpoint suitable for communication to the destination.
789938a3a5eSflorian  * Creates the http request buffer. It also performs connect() to the to_addr.
790938a3a5eSflorian  * @param outnet: outside_network with the comm_base it is attached to,
791938a3a5eSflorian  * 	and the tcp_mss.
792938a3a5eSflorian  * @param cb: callback function for the commpoint.
793938a3a5eSflorian  * @param cb_arg: callback argument for cb.
794938a3a5eSflorian  * @param to_addr: intended destination.
795938a3a5eSflorian  * @param to_addrlen: length of to_addr.
796938a3a5eSflorian  * @param timeout: timeout for the TCP connection.
797938a3a5eSflorian  * 	timeout in milliseconds, or -1 for no (change to the) timeout.
798938a3a5eSflorian  *	So seconds*1000.
799938a3a5eSflorian  * @param ssl: set to true for https.
800938a3a5eSflorian  * @param host: hostname to use for the destination. part of http request.
801938a3a5eSflorian  * @param path: pathname to lookup, eg. name of the file on the destination.
802191f22c6Ssthen  * @param cfg: running configuration for User-Agent setup.
803938a3a5eSflorian  * @return http_out commpoint, or NULL.
804938a3a5eSflorian  */
805938a3a5eSflorian struct comm_point* outnet_comm_point_for_http(struct outside_network* outnet,
806938a3a5eSflorian 	comm_point_callback_type* cb, void* cb_arg,
807938a3a5eSflorian 	struct sockaddr_storage* to_addr, socklen_t to_addrlen, int timeout,
808191f22c6Ssthen 	int ssl, char* host, char* path, struct config_file* cfg);
809938a3a5eSflorian 
810bdfc4d55Sflorian /** connect tcp connection to addr, 0 on failure */
811bdfc4d55Sflorian int outnet_tcp_connect(int s, struct sockaddr_storage* addr, socklen_t addrlen);
812bdfc4d55Sflorian 
813933707f3Ssthen /** callback for incoming udp answers from the network */
814933707f3Ssthen int outnet_udp_cb(struct comm_point* c, void* arg, int error,
815933707f3Ssthen 	struct comm_reply *reply_info);
816933707f3Ssthen 
817933707f3Ssthen /** callback for pending tcp connections */
818933707f3Ssthen int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
819933707f3Ssthen 	struct comm_reply *reply_info);
820933707f3Ssthen 
821933707f3Ssthen /** callback for udp timeout */
822933707f3Ssthen void pending_udp_timer_cb(void *arg);
823933707f3Ssthen 
8245d76a658Ssthen /** callback for udp delay for timeout */
8255d76a658Ssthen void pending_udp_timer_delay_cb(void *arg);
8265d76a658Ssthen 
827933707f3Ssthen /** callback for outgoing TCP timer event */
828933707f3Ssthen void outnet_tcptimer(void* arg);
829933707f3Ssthen 
830e21c60efSsthen /** callback to send serviced queries */
831e21c60efSsthen void serviced_timer_cb(void *arg);
832e21c60efSsthen 
833933707f3Ssthen /** callback for serviced query UDP answers */
834933707f3Ssthen int serviced_udp_callback(struct comm_point* c, void* arg, int error,
835933707f3Ssthen         struct comm_reply* rep);
836933707f3Ssthen 
837933707f3Ssthen /** TCP reply or error callback for serviced queries */
838933707f3Ssthen int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
839933707f3Ssthen         struct comm_reply* rep);
840933707f3Ssthen 
841933707f3Ssthen /** compare function of pending rbtree */
842933707f3Ssthen int pending_cmp(const void* key1, const void* key2);
843933707f3Ssthen 
844933707f3Ssthen /** compare function of serviced query rbtree */
845933707f3Ssthen int serviced_cmp(const void* key1, const void* key2);
846933707f3Ssthen 
847eba819a2Ssthen /** compare function of reuse_tcp rbtree in outside_network struct */
848eba819a2Ssthen int reuse_cmp(const void* key1, const void* key2);
849eba819a2Ssthen 
850eba819a2Ssthen /** compare function of reuse_tcp tree_by_id rbtree */
851eba819a2Ssthen int reuse_id_cmp(const void* key1, const void* key2);
852eba819a2Ssthen 
853933707f3Ssthen #endif /* OUTSIDE_NETWORK_H */
854