1238106Sdes/*
2238106Sdes * services/outside_network.h - listen to answers from the network
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24238106Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25238106Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26238106Sdes * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27238106Sdes * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28238106Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29238106Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30238106Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31238106Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32238106Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33238106Sdes * POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file has functions to send queries to authoritative servers,
40238106Sdes * and wait for the pending answer, with timeouts.
41238106Sdes */
42238106Sdes
43238106Sdes#ifndef OUTSIDE_NETWORK_H
44238106Sdes#define OUTSIDE_NETWORK_H
45238106Sdes
46238106Sdes#include "util/rbtree.h"
47238106Sdes#include "util/netevent.h"
48238106Sdesstruct pending;
49238106Sdesstruct pending_timeout;
50238106Sdesstruct ub_randstate;
51238106Sdesstruct pending_tcp;
52238106Sdesstruct waiting_tcp;
53238106Sdesstruct waiting_udp;
54238106Sdesstruct infra_cache;
55238106Sdesstruct port_comm;
56238106Sdesstruct port_if;
57238106Sdes
58238106Sdes/**
59238106Sdes * Send queries to outside servers and wait for answers from servers.
60238106Sdes * Contains answer-listen sockets.
61238106Sdes */
62238106Sdesstruct outside_network {
63238106Sdes	/** Base for select calls */
64238106Sdes	struct comm_base* base;
65238106Sdes	/** pointer to time in seconds */
66238106Sdes	uint32_t* now_secs;
67238106Sdes	/** pointer to time in microseconds */
68238106Sdes	struct timeval* now_tv;
69238106Sdes
70238106Sdes	/** buffer shared by UDP connections, since there is only one
71238106Sdes	    datagram at any time. */
72238106Sdes	ldns_buffer* udp_buff;
73238106Sdes	/** serviced_callbacks malloc overhead when processing multiple
74238106Sdes	 * identical serviced queries to the same server. */
75238106Sdes	size_t svcd_overhead;
76238106Sdes	/** use x20 bits to encode additional ID random bits */
77238106Sdes	int use_caps_for_id;
78238106Sdes	/** outside network wants to quit. Stop queued msgs from sent. */
79238106Sdes	int want_to_quit;
80238106Sdes
81238106Sdes	/** number of unwanted replies received (for statistics) */
82238106Sdes	size_t unwanted_replies;
83238106Sdes	/** cumulative total of unwanted replies (for defense) */
84238106Sdes	size_t unwanted_total;
85238106Sdes	/** threshold when to take defensive action. If 0 then never. */
86238106Sdes	size_t unwanted_threshold;
87238106Sdes	/** what action to take, called when defensive action is needed */
88238106Sdes	void (*unwanted_action)(void*);
89238106Sdes	/** user param for action */
90238106Sdes	void* unwanted_param;
91238106Sdes
92238106Sdes	/** linked list of available commpoints, unused file descriptors,
93238106Sdes	 * for use as outgoing UDP ports. cp.fd=-1 in them. */
94238106Sdes	struct port_comm* unused_fds;
95238106Sdes	/** if udp is done */
96238106Sdes	int do_udp;
97238106Sdes
98238106Sdes	/** array of outgoing IP4 interfaces */
99238106Sdes	struct port_if* ip4_ifs;
100238106Sdes	/** number of outgoing IP4 interfaces */
101238106Sdes	int num_ip4;
102238106Sdes
103238106Sdes	/** array of outgoing IP6 interfaces */
104238106Sdes	struct port_if* ip6_ifs;
105238106Sdes	/** number of outgoing IP6 interfaces */
106238106Sdes	int num_ip6;
107238106Sdes
108238106Sdes	/** pending udp queries waiting to be sent out, waiting for fd */
109238106Sdes	struct pending* udp_wait_first;
110238106Sdes	/** last pending udp query in list */
111238106Sdes	struct pending* udp_wait_last;
112238106Sdes
113238106Sdes	/** pending udp answers. sorted by id, addr */
114238106Sdes	rbtree_t* pending;
115238106Sdes	/** serviced queries, sorted by qbuf, addr, dnssec */
116238106Sdes	rbtree_t* serviced;
117238106Sdes	/** host cache, pointer but not owned by outnet. */
118238106Sdes	struct infra_cache* infra;
119238106Sdes	/** where to get random numbers */
120238106Sdes	struct ub_randstate* rnd;
121238106Sdes	/** ssl context to create ssl wrapped TCP with DNS connections */
122238106Sdes	void* sslctx;
123238106Sdes
124238106Sdes	/**
125238106Sdes	 * Array of tcp pending used for outgoing TCP connections.
126238106Sdes	 * Each can be used to establish a TCP connection with a server.
127238106Sdes	 * The file descriptors are -1 if they are free, and need to be
128238106Sdes	 * opened for the tcp connection. Can be used for ip4 and ip6.
129238106Sdes	 */
130238106Sdes	struct pending_tcp **tcp_conns;
131238106Sdes	/** number of tcp communication points. */
132238106Sdes	size_t num_tcp;
133238106Sdes	/** list of tcp comm points that are free for use */
134238106Sdes	struct pending_tcp* tcp_free;
135238106Sdes	/** list of tcp queries waiting for a buffer */
136238106Sdes	struct waiting_tcp* tcp_wait_first;
137238106Sdes	/** last of waiting query list */
138238106Sdes	struct waiting_tcp* tcp_wait_last;
139238106Sdes};
140238106Sdes
141238106Sdes/**
142238106Sdes * Outgoing interface. Ports available and currently used are tracked
143238106Sdes * per interface
144238106Sdes */
145238106Sdesstruct port_if {
146238106Sdes	/** address ready to allocate new socket (except port no). */
147238106Sdes	struct sockaddr_storage addr;
148238106Sdes	/** length of addr field */
149238106Sdes	socklen_t addrlen;
150238106Sdes
151238106Sdes	/** the available ports array. These are unused.
152238106Sdes	 * Only the first total-inuse part is filled. */
153238106Sdes	int* avail_ports;
154238106Sdes	/** the total number of available ports (size of the array) */
155238106Sdes	int avail_total;
156238106Sdes
157238106Sdes	/** array of the commpoints currently in use.
158238106Sdes	 * allocated for max number of fds, first part in use. */
159238106Sdes	struct port_comm** out;
160238106Sdes	/** max number of fds, size of out array */
161238106Sdes	int maxout;
162238106Sdes	/** number of commpoints (and thus also ports) in use */
163238106Sdes	int inuse;
164238106Sdes};
165238106Sdes
166238106Sdes/**
167238106Sdes * Outgoing commpoint for UDP port.
168238106Sdes */
169238106Sdesstruct port_comm {
170238106Sdes	/** next in free list */
171238106Sdes	struct port_comm* next;
172238106Sdes	/** which port number (when in use) */
173238106Sdes	int number;
174238106Sdes	/** interface it is used in */
175238106Sdes	struct port_if* pif;
176238106Sdes	/** index in the out array of the interface */
177238106Sdes	int index;
178238106Sdes	/** number of outstanding queries on this port */
179238106Sdes	int num_outstanding;
180238106Sdes	/** UDP commpoint, fd=-1 if not in use */
181238106Sdes	struct comm_point* cp;
182238106Sdes};
183238106Sdes
184238106Sdes/**
185238106Sdes * A query that has an answer pending for it.
186238106Sdes */
187238106Sdesstruct pending {
188238106Sdes	/** redblacktree entry, key is the pending struct(id, addr). */
189238106Sdes	rbnode_t node;
190238106Sdes	/** the ID for the query. int so that a value out of range can
191238106Sdes	 * be used to signify a pending that is for certain not present in
192238106Sdes	 * the rbtree. (and for which deletion is safe). */
193238106Sdes	unsigned int id;
194238106Sdes	/** remote address. */
195238106Sdes	struct sockaddr_storage addr;
196238106Sdes	/** length of addr field in use. */
197238106Sdes	socklen_t addrlen;
198238106Sdes	/** comm point it was sent on (and reply must come back on). */
199238106Sdes	struct port_comm* pc;
200238106Sdes	/** timeout event */
201238106Sdes	struct comm_timer* timer;
202238106Sdes	/** callback for the timeout, error or reply to the message */
203238106Sdes	comm_point_callback_t* cb;
204238106Sdes	/** callback user argument */
205238106Sdes	void* cb_arg;
206238106Sdes	/** the outside network it is part of */
207238106Sdes	struct outside_network* outnet;
208238106Sdes
209238106Sdes	/*---- filled if udp pending is waiting -----*/
210238106Sdes	/** next in waiting list. */
211238106Sdes	struct pending* next_waiting;
212238106Sdes	/** timeout in msec */
213238106Sdes	int timeout;
214238106Sdes	/** The query itself, the query packet to send. */
215238106Sdes	uint8_t* pkt;
216238106Sdes	/** length of query packet. */
217238106Sdes	size_t pkt_len;
218238106Sdes};
219238106Sdes
220238106Sdes/**
221238106Sdes * Pending TCP query to server.
222238106Sdes */
223238106Sdesstruct pending_tcp {
224238106Sdes	/** next in list of free tcp comm points, or NULL. */
225238106Sdes	struct pending_tcp* next_free;
226238106Sdes	/** the ID for the query; checked in reply */
227238106Sdes	uint16_t id;
228238106Sdes	/** tcp comm point it was sent on (and reply must come back on). */
229238106Sdes	struct comm_point* c;
230238106Sdes	/** the query being serviced, NULL if the pending_tcp is unused. */
231238106Sdes	struct waiting_tcp* query;
232238106Sdes};
233238106Sdes
234238106Sdes/**
235238106Sdes * Query waiting for TCP buffer.
236238106Sdes */
237238106Sdesstruct waiting_tcp {
238238106Sdes	/**
239238106Sdes	 * next in waiting list.
240238106Sdes	 * if pkt==0, this points to the pending_tcp structure.
241238106Sdes	 */
242238106Sdes	struct waiting_tcp* next_waiting;
243238106Sdes	/** timeout event; timer keeps running whether the query is
244238106Sdes	 * waiting for a buffer or the tcp reply is pending */
245238106Sdes	struct comm_timer* timer;
246238106Sdes	/** the outside network it is part of */
247238106Sdes	struct outside_network* outnet;
248238106Sdes	/** remote address. */
249238106Sdes	struct sockaddr_storage addr;
250238106Sdes	/** length of addr field in use. */
251238106Sdes	socklen_t addrlen;
252238106Sdes	/**
253238106Sdes	 * The query itself, the query packet to send.
254238106Sdes	 * allocated after the waiting_tcp structure.
255238106Sdes	 * set to NULL when the query is serviced and it part of pending_tcp.
256238106Sdes	 * if this is NULL, the next_waiting points to the pending_tcp.
257238106Sdes	 */
258238106Sdes	uint8_t* pkt;
259238106Sdes	/** length of query packet. */
260238106Sdes	size_t pkt_len;
261238106Sdes	/** callback for the timeout, error or reply to the message */
262238106Sdes	comm_point_callback_t* cb;
263238106Sdes	/** callback user argument */
264238106Sdes	void* cb_arg;
265238106Sdes	/** if it uses ssl upstream */
266238106Sdes	int ssl_upstream;
267238106Sdes};
268238106Sdes
269238106Sdes/**
270238106Sdes * Callback to party interested in serviced query results.
271238106Sdes */
272238106Sdesstruct service_callback {
273238106Sdes	/** next in callback list */
274238106Sdes	struct service_callback* next;
275238106Sdes	/** callback function */
276238106Sdes	comm_point_callback_t* cb;
277238106Sdes	/** user argument for callback function */
278238106Sdes	void* cb_arg;
279238106Sdes};
280238106Sdes
281238106Sdes/** fallback size for fragmentation for EDNS in IPv4 */
282249141Sdes#define EDNS_FRAG_SIZE_IP4 1472
283238106Sdes/** fallback size for EDNS in IPv6, fits one fragment with ip6-tunnel-ids */
284249141Sdes#define EDNS_FRAG_SIZE_IP6 1232
285238106Sdes
286238106Sdes/**
287238106Sdes * Query service record.
288238106Sdes * Contains query and destination. UDP, TCP, EDNS are all tried.
289238106Sdes * complete with retries and timeouts. A number of interested parties can
290238106Sdes * receive a callback.
291238106Sdes */
292238106Sdesstruct serviced_query {
293238106Sdes	/** The rbtree node, key is this record */
294238106Sdes	rbnode_t node;
295238106Sdes	/** The query that needs to be answered. Starts with flags u16,
296238106Sdes	 * then qdcount, ..., including qname, qtype, qclass. Does not include
297238106Sdes	 * EDNS record. */
298238106Sdes	uint8_t* qbuf;
299238106Sdes	/** length of qbuf. */
300238106Sdes	size_t qbuflen;
301238106Sdes	/** If an EDNS section is included, the DO/CD bit will be turned on. */
302238106Sdes	int dnssec;
303238106Sdes	/** We want signatures, or else the answer is likely useless */
304238106Sdes	int want_dnssec;
305238106Sdes	/** tcp upstream used, use tcp, or ssl_upstream for SSL */
306238106Sdes	int tcp_upstream, ssl_upstream;
307238106Sdes	/** where to send it */
308238106Sdes	struct sockaddr_storage addr;
309238106Sdes	/** length of addr field in use. */
310238106Sdes	socklen_t addrlen;
311238106Sdes	/** zone name, uncompressed domain name in wireformat */
312238106Sdes	uint8_t* zone;
313238106Sdes	/** length of zone name */
314238106Sdes	size_t zonelen;
315238106Sdes	/** qtype */
316238106Sdes	int qtype;
317238106Sdes	/** current status */
318238106Sdes	enum serviced_query_status {
319238106Sdes		/** initial status */
320238106Sdes		serviced_initial,
321238106Sdes		/** UDP with EDNS sent */
322238106Sdes		serviced_query_UDP_EDNS,
323238106Sdes		/** UDP without EDNS sent */
324238106Sdes		serviced_query_UDP,
325238106Sdes		/** TCP with EDNS sent */
326238106Sdes		serviced_query_TCP_EDNS,
327238106Sdes		/** TCP without EDNS sent */
328238106Sdes		serviced_query_TCP,
329238106Sdes		/** probe to test EDNS lameness (EDNS is dropped) */
330238106Sdes		serviced_query_PROBE_EDNS,
331238106Sdes		/** probe to test noEDNS0 (EDNS gives FORMERRorNOTIMP) */
332238106Sdes		serviced_query_UDP_EDNS_fallback,
333238106Sdes		/** probe to test TCP noEDNS0 (EDNS gives FORMERRorNOTIMP) */
334238106Sdes		serviced_query_TCP_EDNS_fallback,
335238106Sdes		/** send UDP query with EDNS1480 (or 1280) */
336238106Sdes		serviced_query_UDP_EDNS_FRAG
337238106Sdes	}
338238106Sdes		/** variable with current status */
339238106Sdes		status;
340238106Sdes	/** true if serviced_query is scheduled for deletion already */
341238106Sdes	int to_be_deleted;
342238106Sdes	/** number of UDP retries */
343238106Sdes	int retry;
344238106Sdes	/** time last UDP was sent */
345238106Sdes	struct timeval last_sent_time;
346238106Sdes	/** rtt of last (UDP) message */
347238106Sdes	int last_rtt;
348238106Sdes	/** do we know edns probe status already, for UDP_EDNS queries */
349238106Sdes	int edns_lame_known;
350238106Sdes	/** outside network this is part of */
351238106Sdes	struct outside_network* outnet;
352238106Sdes	/** list of interested parties that need callback on results. */
353238106Sdes	struct service_callback* cblist;
354238106Sdes	/** the UDP or TCP query that is pending, see status which */
355238106Sdes	void* pending;
356238106Sdes};
357238106Sdes
358238106Sdes/**
359238106Sdes * Create outside_network structure with N udp ports.
360238106Sdes * @param base: the communication base to use for event handling.
361238106Sdes * @param bufsize: size for network buffers.
362238106Sdes * @param num_ports: number of udp ports to open per interface.
363238106Sdes * @param ifs: interface names (or NULL for default interface).
364238106Sdes *    These interfaces must be able to access all authoritative servers.
365238106Sdes * @param num_ifs: number of names in array ifs.
366238106Sdes * @param do_ip4: service IP4.
367238106Sdes * @param do_ip6: service IP6.
368238106Sdes * @param num_tcp: number of outgoing tcp buffers to preallocate.
369238106Sdes * @param infra: pointer to infra cached used for serviced queries.
370238106Sdes * @param rnd: stored to create random numbers for serviced queries.
371238106Sdes * @param use_caps_for_id: enable to use 0x20 bits to encode id randomness.
372238106Sdes * @param availports: array of available ports.
373238106Sdes * @param numavailports: number of available ports in array.
374238106Sdes * @param unwanted_threshold: when to take defensive action.
375238106Sdes * @param unwanted_action: the action to take.
376238106Sdes * @param unwanted_param: user parameter to action.
377238106Sdes * @param do_udp: if udp is done.
378238106Sdes * @param sslctx: context to create outgoing connections with (if enabled).
379238106Sdes * @return: the new structure (with no pending answers) or NULL on error.
380238106Sdes */
381238106Sdesstruct outside_network* outside_network_create(struct comm_base* base,
382238106Sdes	size_t bufsize, size_t num_ports, char** ifs, int num_ifs,
383238106Sdes	int do_ip4, int do_ip6, size_t num_tcp, struct infra_cache* infra,
384238106Sdes	struct ub_randstate* rnd, int use_caps_for_id, int* availports,
385238106Sdes	int numavailports, size_t unwanted_threshold,
386238106Sdes	void (*unwanted_action)(void*), void* unwanted_param, int do_udp,
387238106Sdes	void* sslctx);
388238106Sdes
389238106Sdes/**
390238106Sdes * Delete outside_network structure.
391238106Sdes * @param outnet: object to delete.
392238106Sdes */
393238106Sdesvoid outside_network_delete(struct outside_network* outnet);
394238106Sdes
395238106Sdes/**
396238106Sdes * Prepare for quit. Sends no more queries, even if queued up.
397238106Sdes * @param outnet: object to prepare for removal
398238106Sdes */
399238106Sdesvoid outside_network_quit_prepare(struct outside_network* outnet);
400238106Sdes
401238106Sdes/**
402238106Sdes * Send UDP query, create pending answer.
403238106Sdes * Changes the ID for the query to be random and unique for that destination.
404238106Sdes * @param outnet: provides the event handling
405238106Sdes * @param packet: wireformat query to send to destination.
406238106Sdes * @param addr: address to send to.
407238106Sdes * @param addrlen: length of addr.
408238106Sdes * @param timeout: in milliseconds from now.
409238106Sdes * @param callback: function to call on error, timeout or reply.
410238106Sdes * @param callback_arg: user argument for callback function.
411238106Sdes * @return: NULL on error for malloc or socket. Else the pending query object.
412238106Sdes */
413238106Sdesstruct pending* pending_udp_query(struct outside_network* outnet,
414238106Sdes	ldns_buffer* packet, struct sockaddr_storage* addr,
415238106Sdes	socklen_t addrlen, int timeout, comm_point_callback_t* callback,
416238106Sdes	void* callback_arg);
417238106Sdes
418238106Sdes/**
419238106Sdes * Send TCP query. May wait for TCP buffer. Selects ID to be random, and
420238106Sdes * checks id.
421238106Sdes * @param outnet: provides the event handling.
422238106Sdes * @param packet: wireformat query to send to destination. copied from.
423238106Sdes * @param addr: address to send to.
424238106Sdes * @param addrlen: length of addr.
425238106Sdes * @param timeout: in seconds from now.
426238106Sdes *    Timer starts running now. Timer may expire if all buffers are used,
427238106Sdes *    without any query been sent to the server yet.
428238106Sdes * @param callback: function to call on error, timeout or reply.
429238106Sdes * @param callback_arg: user argument for callback function.
430238106Sdes * @param ssl_upstream: if the tcp connection must use SSL.
431238106Sdes * @return: false on error for malloc or socket. Else the pending TCP object.
432238106Sdes */
433238106Sdesstruct waiting_tcp* pending_tcp_query(struct outside_network* outnet,
434238106Sdes	ldns_buffer* packet, struct sockaddr_storage* addr,
435238106Sdes	socklen_t addrlen, int timeout, comm_point_callback_t* callback,
436238106Sdes	void* callback_arg, int ssl_upstream);
437238106Sdes
438238106Sdes/**
439238106Sdes * Delete pending answer.
440238106Sdes * @param outnet: outside network the pending query is part of.
441238106Sdes *    Internal feature: if outnet is NULL, p is not unlinked from rbtree.
442238106Sdes * @param p: deleted
443238106Sdes */
444238106Sdesvoid pending_delete(struct outside_network* outnet, struct pending* p);
445238106Sdes
446238106Sdes/**
447238106Sdes * Perform a serviced query to the authoritative servers.
448238106Sdes * Duplicate efforts are detected, and EDNS, TCP and UDP retry is performed.
449238106Sdes * @param outnet: outside network, with rbtree of serviced queries.
450238106Sdes * @param qname: what qname to query.
451238106Sdes * @param qnamelen: length of qname in octets including 0 root label.
452238106Sdes * @param qtype: rrset type to query (host format)
453238106Sdes * @param qclass: query class. (host format)
454238106Sdes * @param flags: flags u16 (host format), includes opcode, CD bit.
455238106Sdes * @param dnssec: if set, DO bit is set in EDNS queries.
456238106Sdes *	If the value includes BIT_CD, CD bit is set when in EDNS queries.
457238106Sdes *	If the value includes BIT_DO, DO bit is set when in EDNS queries.
458238106Sdes * @param want_dnssec: signatures are needed, without EDNS the answer is
459238106Sdes * 	likely to be useless.
460238106Sdes * @param tcp_upstream: use TCP for upstream queries.
461238106Sdes * @param ssl_upstream: use SSL for upstream queries.
462238106Sdes * @param callback: callback function.
463238106Sdes * @param callback_arg: user argument to callback function.
464238106Sdes * @param addr: to which server to send the query.
465238106Sdes * @param addrlen: length of addr.
466238106Sdes * @param zone: name of the zone of the delegation point. wireformat dname.
467238106Sdes	This is the delegation point name for which the server is deemed
468238106Sdes	authoritative.
469238106Sdes * @param zonelen: length of zone.
470238106Sdes * @param buff: scratch buffer to create query contents in. Empty on exit.
471238106Sdes * @return 0 on error, or pointer to serviced query that is used to answer
472238106Sdes *	this serviced query may be shared with other callbacks as well.
473238106Sdes */
474238106Sdesstruct serviced_query* outnet_serviced_query(struct outside_network* outnet,
475238106Sdes	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
476238106Sdes	uint16_t flags, int dnssec, int want_dnssec, int tcp_upstream,
477238106Sdes	int ssl_upstream, struct sockaddr_storage* addr, socklen_t addrlen,
478238106Sdes	uint8_t* zone, size_t zonelen, comm_point_callback_t* callback,
479249141Sdes	void* callback_arg, ldns_buffer* buff);
480238106Sdes
481238106Sdes/**
482238106Sdes * Remove service query callback.
483238106Sdes * If that leads to zero callbacks, the query is completely cancelled.
484238106Sdes * @param sq: serviced query to adjust.
485238106Sdes * @param cb_arg: callback argument of callback that needs removal.
486238106Sdes *	same as the callback_arg to outnet_serviced_query().
487238106Sdes */
488238106Sdesvoid outnet_serviced_query_stop(struct serviced_query* sq, void* cb_arg);
489238106Sdes
490238106Sdes/**
491238106Sdes * Get memory size in use by outside network.
492238106Sdes * Counts buffers and outstanding query (serviced queries) malloced data.
493238106Sdes * @param outnet: outside network structure.
494238106Sdes * @return size in bytes.
495238106Sdes */
496238106Sdessize_t outnet_get_mem(struct outside_network* outnet);
497238106Sdes
498238106Sdes/**
499238106Sdes * Get memory size in use by serviced query while it is servicing callbacks.
500238106Sdes * This takes into account the pre-deleted status of it; it will be deleted
501238106Sdes * when the callbacks are done.
502238106Sdes * @param sq: serviced query.
503238106Sdes * @return size in bytes.
504238106Sdes */
505238106Sdessize_t serviced_get_mem(struct serviced_query* sq);
506238106Sdes
507238106Sdes/** callback for incoming udp answers from the network */
508238106Sdesint outnet_udp_cb(struct comm_point* c, void* arg, int error,
509238106Sdes	struct comm_reply *reply_info);
510238106Sdes
511238106Sdes/** callback for pending tcp connections */
512238106Sdesint outnet_tcp_cb(struct comm_point* c, void* arg, int error,
513238106Sdes	struct comm_reply *reply_info);
514238106Sdes
515238106Sdes/** callback for udp timeout */
516238106Sdesvoid pending_udp_timer_cb(void *arg);
517238106Sdes
518238106Sdes/** callback for outgoing TCP timer event */
519238106Sdesvoid outnet_tcptimer(void* arg);
520238106Sdes
521238106Sdes/** callback for serviced query UDP answers */
522238106Sdesint serviced_udp_callback(struct comm_point* c, void* arg, int error,
523238106Sdes        struct comm_reply* rep);
524238106Sdes
525238106Sdes/** TCP reply or error callback for serviced queries */
526238106Sdesint serviced_tcp_callback(struct comm_point* c, void* arg, int error,
527238106Sdes        struct comm_reply* rep);
528238106Sdes
529238106Sdes/** compare function of pending rbtree */
530238106Sdesint pending_cmp(const void* key1, const void* key2);
531238106Sdes
532238106Sdes/** compare function of serviced query rbtree */
533238106Sdesint serviced_cmp(const void* key1, const void* key2);
534238106Sdes
535238106Sdes#endif /* OUTSIDE_NETWORK_H */
536