xref: /freebsd/contrib/unbound/services/outside_network.h (revision 17d15b251108ff09e632ed4812f7c66999a6b69b)
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
24*17d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*17d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*17d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*17d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*17d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*17d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*17d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*17d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*17d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*17d15b25SDag-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"
48b7579f77SDag-Erling Smørgrav struct pending;
49b7579f77SDag-Erling Smørgrav struct pending_timeout;
50b7579f77SDag-Erling Smørgrav struct ub_randstate;
51b7579f77SDag-Erling Smørgrav struct pending_tcp;
52b7579f77SDag-Erling Smørgrav struct waiting_tcp;
53b7579f77SDag-Erling Smørgrav struct waiting_udp;
54b7579f77SDag-Erling Smørgrav struct infra_cache;
55b7579f77SDag-Erling Smørgrav struct port_comm;
56b7579f77SDag-Erling Smørgrav struct port_if;
57*17d15b25SDag-Erling Smørgrav struct sldns_buffer;
58b7579f77SDag-Erling Smørgrav 
59b7579f77SDag-Erling Smørgrav /**
60b7579f77SDag-Erling Smørgrav  * Send queries to outside servers and wait for answers from servers.
61b7579f77SDag-Erling Smørgrav  * Contains answer-listen sockets.
62b7579f77SDag-Erling Smørgrav  */
63b7579f77SDag-Erling Smørgrav struct outside_network {
64b7579f77SDag-Erling Smørgrav 	/** Base for select calls */
65b7579f77SDag-Erling Smørgrav 	struct comm_base* base;
66b7579f77SDag-Erling Smørgrav 	/** pointer to time in seconds */
67*17d15b25SDag-Erling Smørgrav 	time_t* now_secs;
68b7579f77SDag-Erling Smørgrav 	/** pointer to time in microseconds */
69b7579f77SDag-Erling Smørgrav 	struct timeval* now_tv;
70b7579f77SDag-Erling Smørgrav 
71b7579f77SDag-Erling Smørgrav 	/** buffer shared by UDP connections, since there is only one
72b7579f77SDag-Erling Smørgrav 	    datagram at any time. */
73*17d15b25SDag-Erling Smørgrav 	struct sldns_buffer* udp_buff;
74b7579f77SDag-Erling Smørgrav 	/** serviced_callbacks malloc overhead when processing multiple
75b7579f77SDag-Erling Smørgrav 	 * identical serviced queries to the same server. */
76b7579f77SDag-Erling Smørgrav 	size_t svcd_overhead;
77b7579f77SDag-Erling Smørgrav 	/** use x20 bits to encode additional ID random bits */
78b7579f77SDag-Erling Smørgrav 	int use_caps_for_id;
79b7579f77SDag-Erling Smørgrav 	/** outside network wants to quit. Stop queued msgs from sent. */
80b7579f77SDag-Erling Smørgrav 	int want_to_quit;
81b7579f77SDag-Erling Smørgrav 
82b7579f77SDag-Erling Smørgrav 	/** number of unwanted replies received (for statistics) */
83b7579f77SDag-Erling Smørgrav 	size_t unwanted_replies;
84b7579f77SDag-Erling Smørgrav 	/** cumulative total of unwanted replies (for defense) */
85b7579f77SDag-Erling Smørgrav 	size_t unwanted_total;
86b7579f77SDag-Erling Smørgrav 	/** threshold when to take defensive action. If 0 then never. */
87b7579f77SDag-Erling Smørgrav 	size_t unwanted_threshold;
88b7579f77SDag-Erling Smørgrav 	/** what action to take, called when defensive action is needed */
89b7579f77SDag-Erling Smørgrav 	void (*unwanted_action)(void*);
90b7579f77SDag-Erling Smørgrav 	/** user param for action */
91b7579f77SDag-Erling Smørgrav 	void* unwanted_param;
92b7579f77SDag-Erling Smørgrav 
93b7579f77SDag-Erling Smørgrav 	/** linked list of available commpoints, unused file descriptors,
94b7579f77SDag-Erling Smørgrav 	 * for use as outgoing UDP ports. cp.fd=-1 in them. */
95b7579f77SDag-Erling Smørgrav 	struct port_comm* unused_fds;
96b7579f77SDag-Erling Smørgrav 	/** if udp is done */
97b7579f77SDag-Erling Smørgrav 	int do_udp;
98*17d15b25SDag-Erling Smørgrav 	/** if udp is delay-closed (delayed answers do not meet closed port)*/
99*17d15b25SDag-Erling Smørgrav 	int delayclose;
100*17d15b25SDag-Erling Smørgrav 	/** timeout for delayclose */
101*17d15b25SDag-Erling Smørgrav 	struct timeval delay_tv;
102b7579f77SDag-Erling Smørgrav 
103b7579f77SDag-Erling Smørgrav 	/** array of outgoing IP4 interfaces */
104b7579f77SDag-Erling Smørgrav 	struct port_if* ip4_ifs;
105b7579f77SDag-Erling Smørgrav 	/** number of outgoing IP4 interfaces */
106b7579f77SDag-Erling Smørgrav 	int num_ip4;
107b7579f77SDag-Erling Smørgrav 
108b7579f77SDag-Erling Smørgrav 	/** array of outgoing IP6 interfaces */
109b7579f77SDag-Erling Smørgrav 	struct port_if* ip6_ifs;
110b7579f77SDag-Erling Smørgrav 	/** number of outgoing IP6 interfaces */
111b7579f77SDag-Erling Smørgrav 	int num_ip6;
112b7579f77SDag-Erling Smørgrav 
113b7579f77SDag-Erling Smørgrav 	/** pending udp queries waiting to be sent out, waiting for fd */
114b7579f77SDag-Erling Smørgrav 	struct pending* udp_wait_first;
115b7579f77SDag-Erling Smørgrav 	/** last pending udp query in list */
116b7579f77SDag-Erling Smørgrav 	struct pending* udp_wait_last;
117b7579f77SDag-Erling Smørgrav 
118b7579f77SDag-Erling Smørgrav 	/** pending udp answers. sorted by id, addr */
119b7579f77SDag-Erling Smørgrav 	rbtree_t* pending;
120b7579f77SDag-Erling Smørgrav 	/** serviced queries, sorted by qbuf, addr, dnssec */
121b7579f77SDag-Erling Smørgrav 	rbtree_t* serviced;
122b7579f77SDag-Erling Smørgrav 	/** host cache, pointer but not owned by outnet. */
123b7579f77SDag-Erling Smørgrav 	struct infra_cache* infra;
124b7579f77SDag-Erling Smørgrav 	/** where to get random numbers */
125b7579f77SDag-Erling Smørgrav 	struct ub_randstate* rnd;
126b7579f77SDag-Erling Smørgrav 	/** ssl context to create ssl wrapped TCP with DNS connections */
127b7579f77SDag-Erling Smørgrav 	void* sslctx;
128b7579f77SDag-Erling Smørgrav 
129b7579f77SDag-Erling Smørgrav 	/**
130b7579f77SDag-Erling Smørgrav 	 * Array of tcp pending used for outgoing TCP connections.
131b7579f77SDag-Erling Smørgrav 	 * Each can be used to establish a TCP connection with a server.
132b7579f77SDag-Erling Smørgrav 	 * The file descriptors are -1 if they are free, and need to be
133b7579f77SDag-Erling Smørgrav 	 * opened for the tcp connection. Can be used for ip4 and ip6.
134b7579f77SDag-Erling Smørgrav 	 */
135b7579f77SDag-Erling Smørgrav 	struct pending_tcp **tcp_conns;
136b7579f77SDag-Erling Smørgrav 	/** number of tcp communication points. */
137b7579f77SDag-Erling Smørgrav 	size_t num_tcp;
138b7579f77SDag-Erling Smørgrav 	/** list of tcp comm points that are free for use */
139b7579f77SDag-Erling Smørgrav 	struct pending_tcp* tcp_free;
140b7579f77SDag-Erling Smørgrav 	/** list of tcp queries waiting for a buffer */
141b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* tcp_wait_first;
142b7579f77SDag-Erling Smørgrav 	/** last of waiting query list */
143b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* tcp_wait_last;
144b7579f77SDag-Erling Smørgrav };
145b7579f77SDag-Erling Smørgrav 
146b7579f77SDag-Erling Smørgrav /**
147b7579f77SDag-Erling Smørgrav  * Outgoing interface. Ports available and currently used are tracked
148b7579f77SDag-Erling Smørgrav  * per interface
149b7579f77SDag-Erling Smørgrav  */
150b7579f77SDag-Erling Smørgrav struct port_if {
151b7579f77SDag-Erling Smørgrav 	/** address ready to allocate new socket (except port no). */
152b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
153b7579f77SDag-Erling Smørgrav 	/** length of addr field */
154b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
155b7579f77SDag-Erling Smørgrav 
156b7579f77SDag-Erling Smørgrav 	/** the available ports array. These are unused.
157b7579f77SDag-Erling Smørgrav 	 * Only the first total-inuse part is filled. */
158b7579f77SDag-Erling Smørgrav 	int* avail_ports;
159b7579f77SDag-Erling Smørgrav 	/** the total number of available ports (size of the array) */
160b7579f77SDag-Erling Smørgrav 	int avail_total;
161b7579f77SDag-Erling Smørgrav 
162b7579f77SDag-Erling Smørgrav 	/** array of the commpoints currently in use.
163b7579f77SDag-Erling Smørgrav 	 * allocated for max number of fds, first part in use. */
164b7579f77SDag-Erling Smørgrav 	struct port_comm** out;
165b7579f77SDag-Erling Smørgrav 	/** max number of fds, size of out array */
166b7579f77SDag-Erling Smørgrav 	int maxout;
167b7579f77SDag-Erling Smørgrav 	/** number of commpoints (and thus also ports) in use */
168b7579f77SDag-Erling Smørgrav 	int inuse;
169b7579f77SDag-Erling Smørgrav };
170b7579f77SDag-Erling Smørgrav 
171b7579f77SDag-Erling Smørgrav /**
172b7579f77SDag-Erling Smørgrav  * Outgoing commpoint for UDP port.
173b7579f77SDag-Erling Smørgrav  */
174b7579f77SDag-Erling Smørgrav struct port_comm {
175b7579f77SDag-Erling Smørgrav 	/** next in free list */
176b7579f77SDag-Erling Smørgrav 	struct port_comm* next;
177b7579f77SDag-Erling Smørgrav 	/** which port number (when in use) */
178b7579f77SDag-Erling Smørgrav 	int number;
179b7579f77SDag-Erling Smørgrav 	/** interface it is used in */
180b7579f77SDag-Erling Smørgrav 	struct port_if* pif;
181b7579f77SDag-Erling Smørgrav 	/** index in the out array of the interface */
182b7579f77SDag-Erling Smørgrav 	int index;
183b7579f77SDag-Erling Smørgrav 	/** number of outstanding queries on this port */
184b7579f77SDag-Erling Smørgrav 	int num_outstanding;
185b7579f77SDag-Erling Smørgrav 	/** UDP commpoint, fd=-1 if not in use */
186b7579f77SDag-Erling Smørgrav 	struct comm_point* cp;
187b7579f77SDag-Erling Smørgrav };
188b7579f77SDag-Erling Smørgrav 
189b7579f77SDag-Erling Smørgrav /**
190b7579f77SDag-Erling Smørgrav  * A query that has an answer pending for it.
191b7579f77SDag-Erling Smørgrav  */
192b7579f77SDag-Erling Smørgrav struct pending {
193b7579f77SDag-Erling Smørgrav 	/** redblacktree entry, key is the pending struct(id, addr). */
194b7579f77SDag-Erling Smørgrav 	rbnode_t node;
195b7579f77SDag-Erling Smørgrav 	/** the ID for the query. int so that a value out of range can
196b7579f77SDag-Erling Smørgrav 	 * be used to signify a pending that is for certain not present in
197b7579f77SDag-Erling Smørgrav 	 * the rbtree. (and for which deletion is safe). */
198b7579f77SDag-Erling Smørgrav 	unsigned int id;
199b7579f77SDag-Erling Smørgrav 	/** remote address. */
200b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
201b7579f77SDag-Erling Smørgrav 	/** length of addr field in use. */
202b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
203b7579f77SDag-Erling Smørgrav 	/** comm point it was sent on (and reply must come back on). */
204b7579f77SDag-Erling Smørgrav 	struct port_comm* pc;
205b7579f77SDag-Erling Smørgrav 	/** timeout event */
206b7579f77SDag-Erling Smørgrav 	struct comm_timer* timer;
207b7579f77SDag-Erling Smørgrav 	/** callback for the timeout, error or reply to the message */
208b7579f77SDag-Erling Smørgrav 	comm_point_callback_t* cb;
209b7579f77SDag-Erling Smørgrav 	/** callback user argument */
210b7579f77SDag-Erling Smørgrav 	void* cb_arg;
211b7579f77SDag-Erling Smørgrav 	/** the outside network it is part of */
212b7579f77SDag-Erling Smørgrav 	struct outside_network* outnet;
213b7579f77SDag-Erling Smørgrav 
214b7579f77SDag-Erling Smørgrav 	/*---- filled if udp pending is waiting -----*/
215b7579f77SDag-Erling Smørgrav 	/** next in waiting list. */
216b7579f77SDag-Erling Smørgrav 	struct pending* next_waiting;
217b7579f77SDag-Erling Smørgrav 	/** timeout in msec */
218b7579f77SDag-Erling Smørgrav 	int timeout;
219b7579f77SDag-Erling Smørgrav 	/** The query itself, the query packet to send. */
220b7579f77SDag-Erling Smørgrav 	uint8_t* pkt;
221b7579f77SDag-Erling Smørgrav 	/** length of query packet. */
222b7579f77SDag-Erling Smørgrav 	size_t pkt_len;
223b7579f77SDag-Erling Smørgrav };
224b7579f77SDag-Erling Smørgrav 
225b7579f77SDag-Erling Smørgrav /**
226b7579f77SDag-Erling Smørgrav  * Pending TCP query to server.
227b7579f77SDag-Erling Smørgrav  */
228b7579f77SDag-Erling Smørgrav struct pending_tcp {
229b7579f77SDag-Erling Smørgrav 	/** next in list of free tcp comm points, or NULL. */
230b7579f77SDag-Erling Smørgrav 	struct pending_tcp* next_free;
231b7579f77SDag-Erling Smørgrav 	/** the ID for the query; checked in reply */
232b7579f77SDag-Erling Smørgrav 	uint16_t id;
233b7579f77SDag-Erling Smørgrav 	/** tcp comm point it was sent on (and reply must come back on). */
234b7579f77SDag-Erling Smørgrav 	struct comm_point* c;
235b7579f77SDag-Erling Smørgrav 	/** the query being serviced, NULL if the pending_tcp is unused. */
236b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* query;
237b7579f77SDag-Erling Smørgrav };
238b7579f77SDag-Erling Smørgrav 
239b7579f77SDag-Erling Smørgrav /**
240b7579f77SDag-Erling Smørgrav  * Query waiting for TCP buffer.
241b7579f77SDag-Erling Smørgrav  */
242b7579f77SDag-Erling Smørgrav struct waiting_tcp {
243b7579f77SDag-Erling Smørgrav 	/**
244b7579f77SDag-Erling Smørgrav 	 * next in waiting list.
245b7579f77SDag-Erling Smørgrav 	 * if pkt==0, this points to the pending_tcp structure.
246b7579f77SDag-Erling Smørgrav 	 */
247b7579f77SDag-Erling Smørgrav 	struct waiting_tcp* next_waiting;
248b7579f77SDag-Erling Smørgrav 	/** timeout event; timer keeps running whether the query is
249b7579f77SDag-Erling Smørgrav 	 * waiting for a buffer or the tcp reply is pending */
250b7579f77SDag-Erling Smørgrav 	struct comm_timer* timer;
251b7579f77SDag-Erling Smørgrav 	/** the outside network it is part of */
252b7579f77SDag-Erling Smørgrav 	struct outside_network* outnet;
253b7579f77SDag-Erling Smørgrav 	/** remote address. */
254b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
255b7579f77SDag-Erling Smørgrav 	/** length of addr field in use. */
256b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
257b7579f77SDag-Erling Smørgrav 	/**
258b7579f77SDag-Erling Smørgrav 	 * The query itself, the query packet to send.
259b7579f77SDag-Erling Smørgrav 	 * allocated after the waiting_tcp structure.
260b7579f77SDag-Erling Smørgrav 	 * set to NULL when the query is serviced and it part of pending_tcp.
261b7579f77SDag-Erling Smørgrav 	 * if this is NULL, the next_waiting points to the pending_tcp.
262b7579f77SDag-Erling Smørgrav 	 */
263b7579f77SDag-Erling Smørgrav 	uint8_t* pkt;
264b7579f77SDag-Erling Smørgrav 	/** length of query packet. */
265b7579f77SDag-Erling Smørgrav 	size_t pkt_len;
266b7579f77SDag-Erling Smørgrav 	/** callback for the timeout, error or reply to the message */
267b7579f77SDag-Erling Smørgrav 	comm_point_callback_t* cb;
268b7579f77SDag-Erling Smørgrav 	/** callback user argument */
269b7579f77SDag-Erling Smørgrav 	void* cb_arg;
270b7579f77SDag-Erling Smørgrav 	/** if it uses ssl upstream */
271b7579f77SDag-Erling Smørgrav 	int ssl_upstream;
272b7579f77SDag-Erling Smørgrav };
273b7579f77SDag-Erling Smørgrav 
274b7579f77SDag-Erling Smørgrav /**
275b7579f77SDag-Erling Smørgrav  * Callback to party interested in serviced query results.
276b7579f77SDag-Erling Smørgrav  */
277b7579f77SDag-Erling Smørgrav struct service_callback {
278b7579f77SDag-Erling Smørgrav 	/** next in callback list */
279b7579f77SDag-Erling Smørgrav 	struct service_callback* next;
280b7579f77SDag-Erling Smørgrav 	/** callback function */
281b7579f77SDag-Erling Smørgrav 	comm_point_callback_t* cb;
282b7579f77SDag-Erling Smørgrav 	/** user argument for callback function */
283b7579f77SDag-Erling Smørgrav 	void* cb_arg;
284b7579f77SDag-Erling Smørgrav };
285b7579f77SDag-Erling Smørgrav 
286b7579f77SDag-Erling Smørgrav /** fallback size for fragmentation for EDNS in IPv4 */
2878ed2b524SDag-Erling Smørgrav #define EDNS_FRAG_SIZE_IP4 1472
288b7579f77SDag-Erling Smørgrav /** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
2898ed2b524SDag-Erling Smørgrav #define EDNS_FRAG_SIZE_IP6 1232
290b7579f77SDag-Erling Smørgrav 
291b7579f77SDag-Erling Smørgrav /**
292b7579f77SDag-Erling Smørgrav  * Query service record.
293b7579f77SDag-Erling Smørgrav  * Contains query and destination. UDP, TCP, EDNS are all tried.
294b7579f77SDag-Erling Smørgrav  * complete with retries and timeouts. A number of interested parties can
295b7579f77SDag-Erling Smørgrav  * receive a callback.
296b7579f77SDag-Erling Smørgrav  */
297b7579f77SDag-Erling Smørgrav struct serviced_query {
298b7579f77SDag-Erling Smørgrav 	/** The rbtree node, key is this record */
299b7579f77SDag-Erling Smørgrav 	rbnode_t node;
300b7579f77SDag-Erling Smørgrav 	/** The query that needs to be answered. Starts with flags u16,
301b7579f77SDag-Erling Smørgrav 	 * then qdcount, ..., including qname, qtype, qclass. Does not include
302b7579f77SDag-Erling Smørgrav 	 * EDNS record. */
303b7579f77SDag-Erling Smørgrav 	uint8_t* qbuf;
304b7579f77SDag-Erling Smørgrav 	/** length of qbuf. */
305b7579f77SDag-Erling Smørgrav 	size_t qbuflen;
306b7579f77SDag-Erling Smørgrav 	/** If an EDNS section is included, the DO/CD bit will be turned on. */
307b7579f77SDag-Erling Smørgrav 	int dnssec;
308b7579f77SDag-Erling Smørgrav 	/** We want signatures, or else the answer is likely useless */
309b7579f77SDag-Erling Smørgrav 	int want_dnssec;
310b7579f77SDag-Erling Smørgrav 	/** tcp upstream used, use tcp, or ssl_upstream for SSL */
311b7579f77SDag-Erling Smørgrav 	int tcp_upstream, ssl_upstream;
312b7579f77SDag-Erling Smørgrav 	/** where to send it */
313b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
314b7579f77SDag-Erling Smørgrav 	/** length of addr field in use. */
315b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
316b7579f77SDag-Erling Smørgrav 	/** zone name, uncompressed domain name in wireformat */
317b7579f77SDag-Erling Smørgrav 	uint8_t* zone;
318b7579f77SDag-Erling Smørgrav 	/** length of zone name */
319b7579f77SDag-Erling Smørgrav 	size_t zonelen;
320b7579f77SDag-Erling Smørgrav 	/** qtype */
321b7579f77SDag-Erling Smørgrav 	int qtype;
322b7579f77SDag-Erling Smørgrav 	/** current status */
323b7579f77SDag-Erling Smørgrav 	enum serviced_query_status {
324b7579f77SDag-Erling Smørgrav 		/** initial status */
325b7579f77SDag-Erling Smørgrav 		serviced_initial,
326b7579f77SDag-Erling Smørgrav 		/** UDP with EDNS sent */
327b7579f77SDag-Erling Smørgrav 		serviced_query_UDP_EDNS,
328b7579f77SDag-Erling Smørgrav 		/** UDP without EDNS sent */
329b7579f77SDag-Erling Smørgrav 		serviced_query_UDP,
330b7579f77SDag-Erling Smørgrav 		/** TCP with EDNS sent */
331b7579f77SDag-Erling Smørgrav 		serviced_query_TCP_EDNS,
332b7579f77SDag-Erling Smørgrav 		/** TCP without EDNS sent */
333b7579f77SDag-Erling Smørgrav 		serviced_query_TCP,
334b7579f77SDag-Erling Smørgrav 		/** probe to test EDNS lameness (EDNS is dropped) */
335b7579f77SDag-Erling Smørgrav 		serviced_query_PROBE_EDNS,
336b7579f77SDag-Erling Smørgrav 		/** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
337b7579f77SDag-Erling Smørgrav 		serviced_query_UDP_EDNS_fallback,
338b7579f77SDag-Erling Smørgrav 		/** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
339b7579f77SDag-Erling Smørgrav 		serviced_query_TCP_EDNS_fallback,
340b7579f77SDag-Erling Smørgrav 		/** send UDP query with EDNS1480 (or 1280) */
341b7579f77SDag-Erling Smørgrav 		serviced_query_UDP_EDNS_FRAG
342b7579f77SDag-Erling Smørgrav 	}
343b7579f77SDag-Erling Smørgrav 		/** variable with current status */
344b7579f77SDag-Erling Smørgrav 		status;
345b7579f77SDag-Erling Smørgrav 	/** true if serviced_query is scheduled for deletion already */
346b7579f77SDag-Erling Smørgrav 	int to_be_deleted;
347b7579f77SDag-Erling Smørgrav 	/** number of UDP retries */
348b7579f77SDag-Erling Smørgrav 	int retry;
349b7579f77SDag-Erling Smørgrav 	/** time last UDP was sent */
350b7579f77SDag-Erling Smørgrav 	struct timeval last_sent_time;
351b7579f77SDag-Erling Smørgrav 	/** rtt of last (UDP) message */
352b7579f77SDag-Erling Smørgrav 	int last_rtt;
353b7579f77SDag-Erling Smørgrav 	/** do we know edns probe status already, for UDP_EDNS queries */
354b7579f77SDag-Erling Smørgrav 	int edns_lame_known;
355b7579f77SDag-Erling Smørgrav 	/** outside network this is part of */
356b7579f77SDag-Erling Smørgrav 	struct outside_network* outnet;
357b7579f77SDag-Erling Smørgrav 	/** list of interested parties that need callback on results. */
358b7579f77SDag-Erling Smørgrav 	struct service_callback* cblist;
359b7579f77SDag-Erling Smørgrav 	/** the UDP or TCP query that is pending, see status which */
360b7579f77SDag-Erling Smørgrav 	void* pending;
361b7579f77SDag-Erling Smørgrav };
362b7579f77SDag-Erling Smørgrav 
363b7579f77SDag-Erling Smørgrav /**
364b7579f77SDag-Erling Smørgrav  * Create outside_network structure with N udp ports.
365b7579f77SDag-Erling Smørgrav  * @param base: the communication base to use for event handling.
366b7579f77SDag-Erling Smørgrav  * @param bufsize: size for network buffers.
367b7579f77SDag-Erling Smørgrav  * @param num_ports: number of udp ports to open per interface.
368b7579f77SDag-Erling Smørgrav  * @param ifs: interface names (or NULL for default interface).
369b7579f77SDag-Erling Smørgrav  *    These interfaces must be able to access all authoritative servers.
370b7579f77SDag-Erling Smørgrav  * @param num_ifs: number of names in array ifs.
371b7579f77SDag-Erling Smørgrav  * @param do_ip4: service IP4.
372b7579f77SDag-Erling Smørgrav  * @param do_ip6: service IP6.
373b7579f77SDag-Erling Smørgrav  * @param num_tcp: number of outgoing tcp buffers to preallocate.
374b7579f77SDag-Erling Smørgrav  * @param infra: pointer to infra cached used for serviced queries.
375b7579f77SDag-Erling Smørgrav  * @param rnd: stored to create random numbers for serviced queries.
376b7579f77SDag-Erling Smørgrav  * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
377b7579f77SDag-Erling Smørgrav  * @param availports: array of available ports.
378b7579f77SDag-Erling Smørgrav  * @param numavailports: number of available ports in array.
379b7579f77SDag-Erling Smørgrav  * @param unwanted_threshold: when to take defensive action.
380b7579f77SDag-Erling Smørgrav  * @param unwanted_action: the action to take.
381b7579f77SDag-Erling Smørgrav  * @param unwanted_param: user parameter to action.
382b7579f77SDag-Erling Smørgrav  * @param do_udp: if udp is done.
383b7579f77SDag-Erling Smørgrav  * @param sslctx: context to create outgoing connections with (if enabled).
384*17d15b25SDag-Erling Smørgrav  * @param delayclose: if not 0, udp sockets are delayed before timeout closure.
385*17d15b25SDag-Erling Smørgrav  * 	msec to wait on timeouted udp sockets.
386b7579f77SDag-Erling Smørgrav  * @return: the new structure (with no pending answers) or NULL on error.
387b7579f77SDag-Erling Smørgrav  */
388b7579f77SDag-Erling Smørgrav struct outside_network* outside_network_create(struct comm_base* base,
389b7579f77SDag-Erling Smørgrav 	size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
390b7579f77SDag-Erling Smørgrav 	int do_ip4, int do_ip6, size_t num_tcp, struct infra_cache* infra,
391b7579f77SDag-Erling Smørgrav 	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
392b7579f77SDag-Erling Smørgrav 	int numavailports, size_t unwanted_threshold,
393b7579f77SDag-Erling Smørgrav 	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
394*17d15b25SDag-Erling Smørgrav 	void* sslctx, int delayclose);
395b7579f77SDag-Erling Smørgrav 
396b7579f77SDag-Erling Smørgrav /**
397b7579f77SDag-Erling Smørgrav  * Delete outside_network structure.
398b7579f77SDag-Erling Smørgrav  * @param outnet: object to delete.
399b7579f77SDag-Erling Smørgrav  */
400b7579f77SDag-Erling Smørgrav void outside_network_delete(struct outside_network* outnet);
401b7579f77SDag-Erling Smørgrav 
402b7579f77SDag-Erling Smørgrav /**
403b7579f77SDag-Erling Smørgrav  * Prepare for quit. Sends no more queries, even if queued up.
404b7579f77SDag-Erling Smørgrav  * @param outnet: object to prepare for removal
405b7579f77SDag-Erling Smørgrav  */
406b7579f77SDag-Erling Smørgrav void outside_network_quit_prepare(struct outside_network* outnet);
407b7579f77SDag-Erling Smørgrav 
408b7579f77SDag-Erling Smørgrav /**
409b7579f77SDag-Erling Smørgrav  * Send UDP query, create pending answer.
410b7579f77SDag-Erling Smørgrav  * Changes the ID for the query to be random and unique for that destination.
411b7579f77SDag-Erling Smørgrav  * @param outnet: provides the event handling
412b7579f77SDag-Erling Smørgrav  * @param packet: wireformat query to send to destination.
413b7579f77SDag-Erling Smørgrav  * @param addr: address to send to.
414b7579f77SDag-Erling Smørgrav  * @param addrlen: length of addr.
415b7579f77SDag-Erling Smørgrav  * @param timeout: in milliseconds from now.
416b7579f77SDag-Erling Smørgrav  * @param callback: function to call on error, timeout or reply.
417b7579f77SDag-Erling Smørgrav  * @param callback_arg: user argument for callback function.
418b7579f77SDag-Erling Smørgrav  * @return: NULL on error for malloc or socket. Else the pending query object.
419b7579f77SDag-Erling Smørgrav  */
420b7579f77SDag-Erling Smørgrav struct pending* pending_udp_query(struct outside_network* outnet,
421*17d15b25SDag-Erling Smørgrav 	struct sldns_buffer* packet, struct sockaddr_storage* addr,
422b7579f77SDag-Erling Smørgrav 	socklen_t addrlen, int timeout, comm_point_callback_t* callback,
423b7579f77SDag-Erling Smørgrav 	void* callback_arg);
424b7579f77SDag-Erling Smørgrav 
425b7579f77SDag-Erling Smørgrav /**
426b7579f77SDag-Erling Smørgrav  * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
427b7579f77SDag-Erling Smørgrav  * checks id.
428b7579f77SDag-Erling Smørgrav  * @param outnet: provides the event handling.
429b7579f77SDag-Erling Smørgrav  * @param packet: wireformat query to send to destination. copied from.
430b7579f77SDag-Erling Smørgrav  * @param addr: address to send to.
431b7579f77SDag-Erling Smørgrav  * @param addrlen: length of addr.
432b7579f77SDag-Erling Smørgrav  * @param timeout: in seconds from now.
433b7579f77SDag-Erling Smørgrav  *    Timer starts running now. Timer may expire if all buffers are used,
434b7579f77SDag-Erling Smørgrav  *    without any query been sent to the server yet.
435b7579f77SDag-Erling Smørgrav  * @param callback: function to call on error, timeout or reply.
436b7579f77SDag-Erling Smørgrav  * @param callback_arg: user argument for callback function.
437b7579f77SDag-Erling Smørgrav  * @param ssl_upstream: if the tcp connection must use SSL.
438b7579f77SDag-Erling Smørgrav  * @return: false on error for malloc or socket. Else the pending TCP object.
439b7579f77SDag-Erling Smørgrav  */
440b7579f77SDag-Erling Smørgrav struct waiting_tcp* pending_tcp_query(struct outside_network* outnet,
441*17d15b25SDag-Erling Smørgrav 	struct sldns_buffer* packet, struct sockaddr_storage* addr,
442b7579f77SDag-Erling Smørgrav 	socklen_t addrlen, int timeout, comm_point_callback_t* callback,
443b7579f77SDag-Erling Smørgrav 	void* callback_arg, int ssl_upstream);
444b7579f77SDag-Erling Smørgrav 
445b7579f77SDag-Erling Smørgrav /**
446b7579f77SDag-Erling Smørgrav  * Delete pending answer.
447b7579f77SDag-Erling Smørgrav  * @param outnet: outside network the pending query is part of.
448b7579f77SDag-Erling Smørgrav  *    Internal feature: if outnet is NULL, p is not unlinked from rbtree.
449b7579f77SDag-Erling Smørgrav  * @param p: deleted
450b7579f77SDag-Erling Smørgrav  */
451b7579f77SDag-Erling Smørgrav void pending_delete(struct outside_network* outnet, struct pending* p);
452b7579f77SDag-Erling Smørgrav 
453b7579f77SDag-Erling Smørgrav /**
454b7579f77SDag-Erling Smørgrav  * Perform a serviced query to the authoritative servers.
455b7579f77SDag-Erling Smørgrav  * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
456b7579f77SDag-Erling Smørgrav  * @param outnet: outside network, with rbtree of serviced queries.
457b7579f77SDag-Erling Smørgrav  * @param qname: what qname to query.
458b7579f77SDag-Erling Smørgrav  * @param qnamelen: length of qname in octets including 0 root label.
459b7579f77SDag-Erling Smørgrav  * @param qtype: rrset type to query (host format)
460b7579f77SDag-Erling Smørgrav  * @param qclass: query class. (host format)
461b7579f77SDag-Erling Smørgrav  * @param flags: flags u16 (host format), includes opcode, CD bit.
462b7579f77SDag-Erling Smørgrav  * @param dnssec: if set, DO bit is set in EDNS queries.
463b7579f77SDag-Erling Smørgrav  *	If the value includes BIT_CD, CD bit is set when in EDNS queries.
464b7579f77SDag-Erling Smørgrav  *	If the value includes BIT_DO, DO bit is set when in EDNS queries.
465b7579f77SDag-Erling Smørgrav  * @param want_dnssec: signatures are needed, without EDNS the answer is
466b7579f77SDag-Erling Smørgrav  * 	likely to be useless.
467b7579f77SDag-Erling Smørgrav  * @param tcp_upstream: use TCP for upstream queries.
468b7579f77SDag-Erling Smørgrav  * @param ssl_upstream: use SSL for upstream queries.
469b7579f77SDag-Erling Smørgrav  * @param callback: callback function.
470b7579f77SDag-Erling Smørgrav  * @param callback_arg: user argument to callback function.
471b7579f77SDag-Erling Smørgrav  * @param addr: to which server to send the query.
472b7579f77SDag-Erling Smørgrav  * @param addrlen: length of addr.
473b7579f77SDag-Erling Smørgrav  * @param zone: name of the zone of the delegation point. wireformat dname.
474b7579f77SDag-Erling Smørgrav 	This is the delegation point name for which the server is deemed
475b7579f77SDag-Erling Smørgrav 	authoritative.
476b7579f77SDag-Erling Smørgrav  * @param zonelen: length of zone.
477b7579f77SDag-Erling Smørgrav  * @param buff: scratch buffer to create query contents in. Empty on exit.
478b7579f77SDag-Erling Smørgrav  * @return 0 on error, or pointer to serviced query that is used to answer
479b7579f77SDag-Erling Smørgrav  *	this serviced query may be shared with other callbacks as well.
480b7579f77SDag-Erling Smørgrav  */
481b7579f77SDag-Erling Smørgrav struct serviced_query* outnet_serviced_query(struct outside_network* outnet,
482b7579f77SDag-Erling Smørgrav 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
483b7579f77SDag-Erling Smørgrav 	uint16_t flags, int dnssec, int want_dnssec, int tcp_upstream,
484b7579f77SDag-Erling Smørgrav 	int ssl_upstream, struct sockaddr_storage* addr, socklen_t addrlen,
485b7579f77SDag-Erling Smørgrav 	uint8_t* zone, size_t zonelen, comm_point_callback_t* callback,
486*17d15b25SDag-Erling Smørgrav 	void* callback_arg, struct sldns_buffer* buff);
487b7579f77SDag-Erling Smørgrav 
488b7579f77SDag-Erling Smørgrav /**
489b7579f77SDag-Erling Smørgrav  * Remove service query callback.
490b7579f77SDag-Erling Smørgrav  * If that leads to zero callbacks, the query is completely cancelled.
491b7579f77SDag-Erling Smørgrav  * @param sq: serviced query to adjust.
492b7579f77SDag-Erling Smørgrav  * @param cb_arg: callback argument of callback that needs removal.
493b7579f77SDag-Erling Smørgrav  *	same as the callback_arg to outnet_serviced_query().
494b7579f77SDag-Erling Smørgrav  */
495b7579f77SDag-Erling Smørgrav void outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
496b7579f77SDag-Erling Smørgrav 
497b7579f77SDag-Erling Smørgrav /**
498b7579f77SDag-Erling Smørgrav  * Get memory size in use by outside network.
499b7579f77SDag-Erling Smørgrav  * Counts buffers and outstanding query (serviced queries) malloced data.
500b7579f77SDag-Erling Smørgrav  * @param outnet: outside network structure.
501b7579f77SDag-Erling Smørgrav  * @return size in bytes.
502b7579f77SDag-Erling Smørgrav  */
503b7579f77SDag-Erling Smørgrav size_t outnet_get_mem(struct outside_network* outnet);
504b7579f77SDag-Erling Smørgrav 
505b7579f77SDag-Erling Smørgrav /**
506b7579f77SDag-Erling Smørgrav  * Get memory size in use by serviced query while it is servicing callbacks.
507b7579f77SDag-Erling Smørgrav  * This takes into account the pre-deleted status of it; it will be deleted
508b7579f77SDag-Erling Smørgrav  * when the callbacks are done.
509b7579f77SDag-Erling Smørgrav  * @param sq: serviced query.
510b7579f77SDag-Erling Smørgrav  * @return size in bytes.
511b7579f77SDag-Erling Smørgrav  */
512b7579f77SDag-Erling Smørgrav size_t serviced_get_mem(struct serviced_query* sq);
513b7579f77SDag-Erling Smørgrav 
514b7579f77SDag-Erling Smørgrav /** callback for incoming udp answers from the network */
515b7579f77SDag-Erling Smørgrav int outnet_udp_cb(struct comm_point* c, void* arg, int error,
516b7579f77SDag-Erling Smørgrav 	struct comm_reply *reply_info);
517b7579f77SDag-Erling Smørgrav 
518b7579f77SDag-Erling Smørgrav /** callback for pending tcp connections */
519b7579f77SDag-Erling Smørgrav int outnet_tcp_cb(struct comm_point* c, void* arg, int error,
520b7579f77SDag-Erling Smørgrav 	struct comm_reply *reply_info);
521b7579f77SDag-Erling Smørgrav 
522b7579f77SDag-Erling Smørgrav /** callback for udp timeout */
523b7579f77SDag-Erling Smørgrav void pending_udp_timer_cb(void *arg);
524b7579f77SDag-Erling Smørgrav 
525*17d15b25SDag-Erling Smørgrav /** callback for udp delay for timeout */
526*17d15b25SDag-Erling Smørgrav void pending_udp_timer_delay_cb(void *arg);
527*17d15b25SDag-Erling Smørgrav 
528b7579f77SDag-Erling Smørgrav /** callback for outgoing TCP timer event */
529b7579f77SDag-Erling Smørgrav void outnet_tcptimer(void* arg);
530b7579f77SDag-Erling Smørgrav 
531b7579f77SDag-Erling Smørgrav /** callback for serviced query UDP answers */
532b7579f77SDag-Erling Smørgrav int serviced_udp_callback(struct comm_point* c, void* arg, int error,
533b7579f77SDag-Erling Smørgrav         struct comm_reply* rep);
534b7579f77SDag-Erling Smørgrav 
535b7579f77SDag-Erling Smørgrav /** TCP reply or error callback for serviced queries */
536b7579f77SDag-Erling Smørgrav int serviced_tcp_callback(struct comm_point* c, void* arg, int error,
537b7579f77SDag-Erling Smørgrav         struct comm_reply* rep);
538b7579f77SDag-Erling Smørgrav 
539b7579f77SDag-Erling Smørgrav /** compare function of pending rbtree */
540b7579f77SDag-Erling Smørgrav int pending_cmp(const void* key1, const void* key2);
541b7579f77SDag-Erling Smørgrav 
542b7579f77SDag-Erling Smørgrav /** compare function of serviced query rbtree */
543b7579f77SDag-Erling Smørgrav int serviced_cmp(const void* key1, const void* key2);
544b7579f77SDag-Erling Smørgrav 
545b7579f77SDag-Erling Smørgrav #endif /* OUTSIDE_NETWORK_H */
546