1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * services/listen_dnsport.h - listen on port 53 for incoming DNS queries.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file has functions to get queries from clients.
40b7579f77SDag-Erling Smørgrav  */
41b7579f77SDag-Erling Smørgrav 
42b7579f77SDag-Erling Smørgrav #ifndef LISTEN_DNSPORT_H
43b7579f77SDag-Erling Smørgrav #define LISTEN_DNSPORT_H
44b7579f77SDag-Erling Smørgrav 
45b7579f77SDag-Erling Smørgrav #include "util/netevent.h"
46865f46b2SCy Schubert #include "daemon/acl_list.h"
47c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2_NGHTTP2_H
48c0caa2e2SCy Schubert #include <nghttp2/nghttp2.h>
49c0caa2e2SCy Schubert #endif
50b7579f77SDag-Erling Smørgrav struct listen_list;
51b7579f77SDag-Erling Smørgrav struct config_file;
52b7579f77SDag-Erling Smørgrav struct addrinfo;
5317d15b25SDag-Erling Smørgrav struct sldns_buffer;
544c75e3aaSDag-Erling Smørgrav struct tcl_list;
55b7579f77SDag-Erling Smørgrav 
56b7579f77SDag-Erling Smørgrav /**
57b7579f77SDag-Erling Smørgrav  * Listening for queries structure.
58b7579f77SDag-Erling Smørgrav  * Contains list of query-listen sockets.
59b7579f77SDag-Erling Smørgrav  */
60b7579f77SDag-Erling Smørgrav struct listen_dnsport {
61b7579f77SDag-Erling Smørgrav 	/** Base for select calls */
62b7579f77SDag-Erling Smørgrav 	struct comm_base* base;
63b7579f77SDag-Erling Smørgrav 
64b7579f77SDag-Erling Smørgrav 	/** buffer shared by UDP connections, since there is only one
65b7579f77SDag-Erling Smørgrav 	    datagram at any time. */
6617d15b25SDag-Erling Smørgrav 	struct sldns_buffer* udp_buff;
6765b390aaSDag-Erling Smørgrav #ifdef USE_DNSCRYPT
6865b390aaSDag-Erling Smørgrav 	struct sldns_buffer* dnscrypt_udp_buff;
6965b390aaSDag-Erling Smørgrav #endif
70b7579f77SDag-Erling Smørgrav 	/** list of comm points used to get incoming events */
71b7579f77SDag-Erling Smørgrav 	struct listen_list* cps;
72b7579f77SDag-Erling Smørgrav };
73b7579f77SDag-Erling Smørgrav 
74b7579f77SDag-Erling Smørgrav /**
75b7579f77SDag-Erling Smørgrav  * Single linked list to store event points.
76b7579f77SDag-Erling Smørgrav  */
77b7579f77SDag-Erling Smørgrav struct listen_list {
78b7579f77SDag-Erling Smørgrav 	/** next in list */
79b7579f77SDag-Erling Smørgrav 	struct listen_list* next;
80b7579f77SDag-Erling Smørgrav 	/** event info */
81b7579f77SDag-Erling Smørgrav 	struct comm_point* com;
82b7579f77SDag-Erling Smørgrav };
83b7579f77SDag-Erling Smørgrav 
84b7579f77SDag-Erling Smørgrav /**
85b7579f77SDag-Erling Smørgrav  * type of ports
86b7579f77SDag-Erling Smørgrav  */
87b7579f77SDag-Erling Smørgrav enum listen_type {
88b7579f77SDag-Erling Smørgrav 	/** udp type */
89b7579f77SDag-Erling Smørgrav 	listen_type_udp,
90b7579f77SDag-Erling Smørgrav 	/** tcp type */
91b7579f77SDag-Erling Smørgrav 	listen_type_tcp,
92b7579f77SDag-Erling Smørgrav 	/** udp ipv6 (v4mapped) for use with ancillary data */
93b7579f77SDag-Erling Smørgrav 	listen_type_udpancil,
94b7579f77SDag-Erling Smørgrav 	/** ssl over tcp type */
9565b390aaSDag-Erling Smørgrav 	listen_type_ssl,
9665b390aaSDag-Erling Smørgrav 	/** udp type  + dnscrypt*/
9765b390aaSDag-Erling Smørgrav 	listen_type_udp_dnscrypt,
9865b390aaSDag-Erling Smørgrav 	/** tcp type + dnscrypt */
9965b390aaSDag-Erling Smørgrav 	listen_type_tcp_dnscrypt,
10065b390aaSDag-Erling Smørgrav 	/** udp ipv6 (v4mapped) for use with ancillary data + dnscrypt*/
101c0caa2e2SCy Schubert 	listen_type_udpancil_dnscrypt,
102c0caa2e2SCy Schubert 	/** HTTP(2) over TLS over TCP */
103c0caa2e2SCy Schubert 	listen_type_http
104b7579f77SDag-Erling Smørgrav };
105b7579f77SDag-Erling Smørgrav 
1065469a995SCy Schubert /*
1075469a995SCy Schubert  * socket properties (just like NSD nsd_socket structure definition)
1085469a995SCy Schubert  */
1095469a995SCy Schubert struct unbound_socket {
110*335c7cdaSCy Schubert 	/** the address of the socket */
111*335c7cdaSCy Schubert 	struct sockaddr* addr;
112*335c7cdaSCy Schubert 	/** length of the address */
113*335c7cdaSCy Schubert 	socklen_t addrlen;
1145469a995SCy Schubert 	/** socket descriptor returned by socket() syscall */
1155469a995SCy Schubert 	int s;
116*335c7cdaSCy Schubert 	/** address family (AF_INET/AF_INET6) */
1175469a995SCy Schubert 	int fam;
118865f46b2SCy Schubert 	/** ACL on the socket (listening interface) */
119865f46b2SCy Schubert 	struct acl_addr* acl;
1205469a995SCy Schubert };
1215469a995SCy Schubert 
122b7579f77SDag-Erling Smørgrav /**
123b7579f77SDag-Erling Smørgrav  * Single linked list to store shared ports that have been
124b7579f77SDag-Erling Smørgrav  * opened for use by all threads.
125b7579f77SDag-Erling Smørgrav  */
126b7579f77SDag-Erling Smørgrav struct listen_port {
127b7579f77SDag-Erling Smørgrav 	/** next in list */
128b7579f77SDag-Erling Smørgrav 	struct listen_port* next;
129b7579f77SDag-Erling Smørgrav 	/** file descriptor, open and ready for use */
130b7579f77SDag-Erling Smørgrav 	int fd;
131b7579f77SDag-Erling Smørgrav 	/** type of file descriptor, udp or tcp */
132b7579f77SDag-Erling Smørgrav 	enum listen_type ftype;
133865f46b2SCy Schubert 	/** if the port should support PROXYv2 */
134865f46b2SCy Schubert 	int pp2_enabled;
135865f46b2SCy Schubert 	/** fill in unbound_socket structure for every opened socket at
136865f46b2SCy Schubert 	 * Unbound startup */
1375469a995SCy Schubert 	struct unbound_socket* socket;
138b7579f77SDag-Erling Smørgrav };
139b7579f77SDag-Erling Smørgrav 
140b7579f77SDag-Erling Smørgrav /**
141b7579f77SDag-Erling Smørgrav  * Create shared listening ports
142b7579f77SDag-Erling Smørgrav  * Getaddrinfo, create socket, bind and listen to zero or more
143b7579f77SDag-Erling Smørgrav  * interfaces for IP4 and/or IP6, for UDP and/or TCP.
144b7579f77SDag-Erling Smørgrav  * On the given port number. It creates the sockets.
145b7579f77SDag-Erling Smørgrav  * @param cfg: settings on what ports to open.
146c0caa2e2SCy Schubert  * @param ifs: interfaces to open, array of IP addresses, "ip[@port]".
147c0caa2e2SCy Schubert  * @param num_ifs: length of ifs.
14817d15b25SDag-Erling Smørgrav  * @param reuseport: set to true if you want reuseport, or NULL to not have it,
14917d15b25SDag-Erling Smørgrav  *   set to false on exit if reuseport failed to apply (because of no
15017d15b25SDag-Erling Smørgrav  *   kernel support).
151b7579f77SDag-Erling Smørgrav  * @return: linked list of ports or NULL on error.
152b7579f77SDag-Erling Smørgrav  */
15317d15b25SDag-Erling Smørgrav struct listen_port* listening_ports_open(struct config_file* cfg,
154c0caa2e2SCy Schubert 	char** ifs, int num_ifs, int* reuseport);
155b7579f77SDag-Erling Smørgrav 
156b7579f77SDag-Erling Smørgrav /**
157b7579f77SDag-Erling Smørgrav  * Close and delete the (list of) listening ports.
158b7579f77SDag-Erling Smørgrav  */
159b7579f77SDag-Erling Smørgrav void listening_ports_free(struct listen_port* list);
160b7579f77SDag-Erling Smørgrav 
1615469a995SCy Schubert struct config_strlist;
162b7579f77SDag-Erling Smørgrav /**
163c0caa2e2SCy Schubert  * Resolve interface names in config and store result IP addresses
1645469a995SCy Schubert  * @param ifs: array of interfaces.  The list of interface names, if not NULL.
1655469a995SCy Schubert  * @param num_ifs: length of ifs array.
1665469a995SCy Schubert  * @param list: if not NULL, this is used as the list of interface names.
167c0caa2e2SCy Schubert  * @param resif: string array (malloced array of malloced strings) with
168c0caa2e2SCy Schubert  * 	result.  NULL if cfg has none.
169c0caa2e2SCy Schubert  * @param num_resif: length of resif.  Zero if cfg has zero num_ifs.
170c0caa2e2SCy Schubert  * @return 0 on failure.
171c0caa2e2SCy Schubert  */
1725469a995SCy Schubert int resolve_interface_names(char** ifs, int num_ifs,
1735469a995SCy Schubert 	struct config_strlist* list, char*** resif, int* num_resif);
174c0caa2e2SCy Schubert 
175c0caa2e2SCy Schubert /**
176b7579f77SDag-Erling Smørgrav  * Create commpoints with for this thread for the shared ports.
177b7579f77SDag-Erling Smørgrav  * @param base: the comm_base that provides event functionality.
178b7579f77SDag-Erling Smørgrav  *	for default all ifs.
179b7579f77SDag-Erling Smørgrav  * @param ports: the list of shared ports.
180b7579f77SDag-Erling Smørgrav  * @param bufsize: size of datagram buffer.
181b7579f77SDag-Erling Smørgrav  * @param tcp_accept_count: max number of simultaneous TCP connections
182b7579f77SDag-Erling Smørgrav  * 	from clients.
1834c75e3aaSDag-Erling Smørgrav  * @param tcp_idle_timeout: idle timeout for TCP connections in msec.
184c0caa2e2SCy Schubert  * @param harden_large_queries: whether query size should be limited.
185c0caa2e2SCy Schubert  * @param http_max_streams: maximum number of HTTP/2 streams per connection.
186c0caa2e2SCy Schubert  * @param http_endpoint: HTTP endpoint to service queries on
187369c6923SCy Schubert  * @param http_notls: no TLS for http downstream
1884c75e3aaSDag-Erling Smørgrav  * @param tcp_conn_limit: TCP connection limit info.
189b7579f77SDag-Erling Smørgrav  * @param sslctx: nonNULL if ssl context.
190ff825849SDag-Erling Smørgrav  * @param dtenv: nonNULL if dnstap enabled.
191b7579f77SDag-Erling Smørgrav  * @param cb: callback function when a request arrives. It is passed
192b7579f77SDag-Erling Smørgrav  *	  the packet and user argument. Return true to send a reply.
193b7579f77SDag-Erling Smørgrav  * @param cb_arg: user data argument for callback function.
194b7579f77SDag-Erling Smørgrav  * @return: the malloced listening structure, ready for use. NULL on error.
195b7579f77SDag-Erling Smørgrav  */
196c0caa2e2SCy Schubert struct listen_dnsport*
197c0caa2e2SCy Schubert listen_create(struct comm_base* base, struct listen_port* ports,
198c0caa2e2SCy Schubert 	size_t bufsize, int tcp_accept_count, int tcp_idle_timeout,
199c0caa2e2SCy Schubert 	int harden_large_queries, uint32_t http_max_streams,
200369c6923SCy Schubert 	char* http_endpoint, int http_notls, struct tcl_list* tcp_conn_limit,
201369c6923SCy Schubert 	void* sslctx, struct dt_env* dtenv, comm_point_callback_type* cb,
202369c6923SCy Schubert 	void *cb_arg);
203b7579f77SDag-Erling Smørgrav 
204b7579f77SDag-Erling Smørgrav /**
205b7579f77SDag-Erling Smørgrav  * delete the listening structure
206b7579f77SDag-Erling Smørgrav  * @param listen: listening structure.
207b7579f77SDag-Erling Smørgrav  */
208b7579f77SDag-Erling Smørgrav void listen_delete(struct listen_dnsport* listen);
209b7579f77SDag-Erling Smørgrav 
21024e36522SCy Schubert /** setup the locks for the listen ports */
21124e36522SCy Schubert void listen_setup_locks(void);
21224e36522SCy Schubert /** desetup the locks for the listen ports */
21324e36522SCy Schubert void listen_desetup_locks(void);
21424e36522SCy Schubert 
215b7579f77SDag-Erling Smørgrav /**
216b7579f77SDag-Erling Smørgrav  * delete listen_list of commpoints. Calls commpointdelete() on items.
217b7579f77SDag-Erling Smørgrav  * This may close the fds or not depending on flags.
218b7579f77SDag-Erling Smørgrav  * @param list: to delete.
219b7579f77SDag-Erling Smørgrav  */
220b7579f77SDag-Erling Smørgrav void listen_list_delete(struct listen_list* list);
221b7579f77SDag-Erling Smørgrav 
222b7579f77SDag-Erling Smørgrav /**
223b7579f77SDag-Erling Smørgrav  * get memory size used by the listening structs
224b7579f77SDag-Erling Smørgrav  * @param listen: listening structure.
225b7579f77SDag-Erling Smørgrav  * @return: size in bytes.
226b7579f77SDag-Erling Smørgrav  */
227b7579f77SDag-Erling Smørgrav size_t listen_get_mem(struct listen_dnsport* listen);
228b7579f77SDag-Erling Smørgrav 
229b7579f77SDag-Erling Smørgrav /**
230b7579f77SDag-Erling Smørgrav  * stop accept handlers for TCP (until enabled again)
231b7579f77SDag-Erling Smørgrav  * @param listen: listening structure.
232b7579f77SDag-Erling Smørgrav  */
233b7579f77SDag-Erling Smørgrav void listen_stop_accept(struct listen_dnsport* listen);
234b7579f77SDag-Erling Smørgrav 
235b7579f77SDag-Erling Smørgrav /**
236b7579f77SDag-Erling Smørgrav  * start accept handlers for TCP (was stopped before)
237b7579f77SDag-Erling Smørgrav  * @param listen: listening structure.
238b7579f77SDag-Erling Smørgrav  */
239b7579f77SDag-Erling Smørgrav void listen_start_accept(struct listen_dnsport* listen);
240b7579f77SDag-Erling Smørgrav 
241b7579f77SDag-Erling Smørgrav /**
242b7579f77SDag-Erling Smørgrav  * Create and bind nonblocking UDP socket
243b7579f77SDag-Erling Smørgrav  * @param family: for socket call.
244b7579f77SDag-Erling Smørgrav  * @param socktype: for socket call.
245b7579f77SDag-Erling Smørgrav  * @param addr: for bind call.
246b7579f77SDag-Erling Smørgrav  * @param addrlen: for bind call.
247b7579f77SDag-Erling Smørgrav  * @param v6only: if enabled, IP6 sockets get IP6ONLY option set.
248b7579f77SDag-Erling Smørgrav  * 	if enabled with value 2 IP6ONLY option is disabled.
249b7579f77SDag-Erling Smørgrav  * @param inuse: on error, this is set true if the port was in use.
250b7579f77SDag-Erling Smørgrav  * @param noproto: on error, this is set true if cause is that the
251b7579f77SDag-Erling Smørgrav 	IPv6 proto (family) is not available.
252b7579f77SDag-Erling Smørgrav  * @param rcv: set size on rcvbuf with socket option, if 0 it is not set.
253b7579f77SDag-Erling Smørgrav  * @param snd: set size on sndbuf with socket option, if 0 it is not set.
25417d15b25SDag-Erling Smørgrav  * @param listen: if true, this is a listening UDP port, eg port 53, and
25517d15b25SDag-Erling Smørgrav  * 	set SO_REUSEADDR on it.
25617d15b25SDag-Erling Smørgrav  * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
25717d15b25SDag-Erling Smørgrav  * 	listening UDP port.  Set to false on return if it failed to do so.
25809a3aaf3SDag-Erling Smørgrav  * @param transparent: set IP_TRANSPARENT socket option.
259e2d15004SDag-Erling Smørgrav  * @param freebind: set IP_FREEBIND socket option.
2603005e0a3SDag-Erling Smørgrav  * @param use_systemd: if true, fetch sockets from systemd.
26125039b37SCy Schubert  * @param dscp: DSCP to use.
262b7579f77SDag-Erling Smørgrav  * @return: the socket. -1 on error.
263b7579f77SDag-Erling Smørgrav  */
264b7579f77SDag-Erling Smørgrav int create_udp_sock(int family, int socktype, struct sockaddr* addr,
265b7579f77SDag-Erling Smørgrav 	socklen_t addrlen, int v6only, int* inuse, int* noproto, int rcv,
26625039b37SCy Schubert 	int snd, int listen, int* reuseport, int transparent, int freebind, int use_systemd, int dscp);
267b7579f77SDag-Erling Smørgrav 
268b7579f77SDag-Erling Smørgrav /**
269b7579f77SDag-Erling Smørgrav  * Create and bind TCP listening socket
270b7579f77SDag-Erling Smørgrav  * @param addr: address info ready to make socket.
271b7579f77SDag-Erling Smørgrav  * @param v6only: enable ip6 only flag on ip6 sockets.
272b7579f77SDag-Erling Smørgrav  * @param noproto: if error caused by lack of protocol support.
27317d15b25SDag-Erling Smørgrav  * @param reuseport: if nonNULL and true, try to set SO_REUSEPORT on
27417d15b25SDag-Erling Smørgrav  * 	listening UDP port.  Set to false on return if it failed to do so.
27509a3aaf3SDag-Erling Smørgrav  * @param transparent: set IP_TRANSPARENT socket option.
276f61ef7f6SDag-Erling Smørgrav  * @param mss: maximum segment size of the socket. if zero, leaves the default.
277c0caa2e2SCy Schubert  * @param nodelay: if true set TCP_NODELAY and TCP_QUICKACK socket options.
278e2d15004SDag-Erling Smørgrav  * @param freebind: set IP_FREEBIND socket option.
2793005e0a3SDag-Erling Smørgrav  * @param use_systemd: if true, fetch sockets from systemd.
28025039b37SCy Schubert  * @param dscp: DSCP to use.
281b7579f77SDag-Erling Smørgrav  * @return: the socket. -1 on error.
282b7579f77SDag-Erling Smørgrav  */
28317d15b25SDag-Erling Smørgrav int create_tcp_accept_sock(struct addrinfo *addr, int v6only, int* noproto,
284c0caa2e2SCy Schubert 	int* reuseport, int transparent, int mss, int nodelay, int freebind,
285c0caa2e2SCy Schubert 	int use_systemd, int dscp);
286b7579f77SDag-Erling Smørgrav 
28731099b50SDag-Erling Smørgrav /**
28831099b50SDag-Erling Smørgrav  * Create and bind local listening socket
28931099b50SDag-Erling Smørgrav  * @param path: path to the socket.
29031099b50SDag-Erling Smørgrav  * @param noproto: on error, this is set true if cause is that local sockets
29131099b50SDag-Erling Smørgrav  *	are not supported.
2923005e0a3SDag-Erling Smørgrav  * @param use_systemd: if true, fetch sockets from systemd.
29331099b50SDag-Erling Smørgrav  * @return: the socket. -1 on error.
29431099b50SDag-Erling Smørgrav  */
2953005e0a3SDag-Erling Smørgrav int create_local_accept_sock(const char* path, int* noproto, int use_systemd);
29631099b50SDag-Erling Smørgrav 
297e86b9096SDag-Erling Smørgrav /**
298e86b9096SDag-Erling Smørgrav  * TCP request info.  List of requests outstanding on the channel, that
299e86b9096SDag-Erling Smørgrav  * are asked for but not yet answered back.
300e86b9096SDag-Erling Smørgrav  */
301e86b9096SDag-Erling Smørgrav struct tcp_req_info {
302e86b9096SDag-Erling Smørgrav 	/** the TCP comm point for this.  Its buffer is used for read/write */
303e86b9096SDag-Erling Smørgrav 	struct comm_point* cp;
304e86b9096SDag-Erling Smørgrav 	/** the buffer to use to spool reply from mesh into,
305e86b9096SDag-Erling Smørgrav 	 * it can then be copied to the result list and written.
306e86b9096SDag-Erling Smørgrav 	 * it is a pointer to the shared udp buffer. */
307e86b9096SDag-Erling Smørgrav 	struct sldns_buffer* spool_buffer;
308e86b9096SDag-Erling Smørgrav 	/** are we in worker_handle function call (for recursion callback)*/
309e86b9096SDag-Erling Smørgrav 	int in_worker_handle;
310e86b9096SDag-Erling Smørgrav 	/** is the comm point dropped (by worker handle).
311e86b9096SDag-Erling Smørgrav 	 * That means we have to disconnect the channel. */
312e86b9096SDag-Erling Smørgrav 	int is_drop;
313e86b9096SDag-Erling Smørgrav 	/** is the comm point set to send_reply (by mesh new client in worker
314e86b9096SDag-Erling Smørgrav 	 * handle), if so answer is available in c.buffer */
315e86b9096SDag-Erling Smørgrav 	int is_reply;
316e86b9096SDag-Erling Smørgrav 	/** read channel has closed, just write pending results */
317e86b9096SDag-Erling Smørgrav 	int read_is_closed;
318e86b9096SDag-Erling Smørgrav 	/** read again */
319e86b9096SDag-Erling Smørgrav 	int read_again;
320e86b9096SDag-Erling Smørgrav 	/** number of outstanding requests */
321e86b9096SDag-Erling Smørgrav 	int num_open_req;
322e86b9096SDag-Erling Smørgrav 	/** list of outstanding requests */
323e86b9096SDag-Erling Smørgrav 	struct tcp_req_open_item* open_req_list;
324e86b9096SDag-Erling Smørgrav 	/** number of pending writeable results */
325e86b9096SDag-Erling Smørgrav 	int num_done_req;
326e86b9096SDag-Erling Smørgrav 	/** list of pending writable result packets, malloced one at a time */
327e86b9096SDag-Erling Smørgrav 	struct tcp_req_done_item* done_req_list;
328e86b9096SDag-Erling Smørgrav };
329e86b9096SDag-Erling Smørgrav 
330e86b9096SDag-Erling Smørgrav /**
331e86b9096SDag-Erling Smørgrav  * List of open items in TCP channel
332e86b9096SDag-Erling Smørgrav  */
333e86b9096SDag-Erling Smørgrav struct tcp_req_open_item {
334e86b9096SDag-Erling Smørgrav 	/** next in list */
335e86b9096SDag-Erling Smørgrav 	struct tcp_req_open_item* next;
336e86b9096SDag-Erling Smørgrav 	/** the mesh area of the mesh_state */
337e86b9096SDag-Erling Smørgrav 	struct mesh_area* mesh;
338e86b9096SDag-Erling Smørgrav 	/** the mesh state */
339e86b9096SDag-Erling Smørgrav 	struct mesh_state* mesh_state;
340e86b9096SDag-Erling Smørgrav };
341e86b9096SDag-Erling Smørgrav 
342e86b9096SDag-Erling Smørgrav /**
343e86b9096SDag-Erling Smørgrav  * List of done items in TCP channel
344e86b9096SDag-Erling Smørgrav  */
345e86b9096SDag-Erling Smørgrav struct tcp_req_done_item {
346e86b9096SDag-Erling Smørgrav 	/** next in list */
347e86b9096SDag-Erling Smørgrav 	struct tcp_req_done_item* next;
348e86b9096SDag-Erling Smørgrav 	/** the buffer with packet contents */
349e86b9096SDag-Erling Smørgrav 	uint8_t* buf;
350e86b9096SDag-Erling Smørgrav 	/** length of the buffer */
351e86b9096SDag-Erling Smørgrav 	size_t len;
352e86b9096SDag-Erling Smørgrav };
353e86b9096SDag-Erling Smørgrav 
354e86b9096SDag-Erling Smørgrav /**
355e86b9096SDag-Erling Smørgrav  * Create tcp request info structure that keeps track of open
356e86b9096SDag-Erling Smørgrav  * requests on the TCP channel that are resolved at the same time,
357e86b9096SDag-Erling Smørgrav  * and the pending results that have to get written back to that client.
358e86b9096SDag-Erling Smørgrav  * @param spoolbuf: shared buffer
359e86b9096SDag-Erling Smørgrav  * @return new structure or NULL on alloc failure.
360e86b9096SDag-Erling Smørgrav  */
361e86b9096SDag-Erling Smørgrav struct tcp_req_info* tcp_req_info_create(struct sldns_buffer* spoolbuf);
362e86b9096SDag-Erling Smørgrav 
363e86b9096SDag-Erling Smørgrav /**
364e86b9096SDag-Erling Smørgrav  * Delete tcp request structure.  Called by owning commpoint.
365e86b9096SDag-Erling Smørgrav  * Removes mesh entry references and stored results from the lists.
366e86b9096SDag-Erling Smørgrav  * @param req: the tcp request info
367e86b9096SDag-Erling Smørgrav  */
368e86b9096SDag-Erling Smørgrav void tcp_req_info_delete(struct tcp_req_info* req);
369e86b9096SDag-Erling Smørgrav 
370e86b9096SDag-Erling Smørgrav /**
371e86b9096SDag-Erling Smørgrav  * Clear tcp request structure.  Removes list entries, sets it up ready
372e86b9096SDag-Erling Smørgrav  * for the next connection.
373e86b9096SDag-Erling Smørgrav  * @param req: tcp request info structure.
374e86b9096SDag-Erling Smørgrav  */
375e86b9096SDag-Erling Smørgrav void tcp_req_info_clear(struct tcp_req_info* req);
376e86b9096SDag-Erling Smørgrav 
377e86b9096SDag-Erling Smørgrav /**
378e86b9096SDag-Erling Smørgrav  * Remove mesh state entry from list in tcp_req_info.
379e86b9096SDag-Erling Smørgrav  * caller has to manage the mesh state reply entry in the mesh state.
380e86b9096SDag-Erling Smørgrav  * @param req: the tcp req info that has the entry removed from the list.
381e86b9096SDag-Erling Smørgrav  * @param m: the state removed from the list.
382e86b9096SDag-Erling Smørgrav  */
383e86b9096SDag-Erling Smørgrav void tcp_req_info_remove_mesh_state(struct tcp_req_info* req,
384e86b9096SDag-Erling Smørgrav 	struct mesh_state* m);
385e86b9096SDag-Erling Smørgrav 
386e86b9096SDag-Erling Smørgrav /**
387e86b9096SDag-Erling Smørgrav  * Handle write done of the last result packet
388e86b9096SDag-Erling Smørgrav  * @param req: the tcp req info.
389e86b9096SDag-Erling Smørgrav  */
390e86b9096SDag-Erling Smørgrav void tcp_req_info_handle_writedone(struct tcp_req_info* req);
391e86b9096SDag-Erling Smørgrav 
392e86b9096SDag-Erling Smørgrav /**
393e86b9096SDag-Erling Smørgrav  * Handle read done of a new request from the client
394e86b9096SDag-Erling Smørgrav  * @param req: the tcp req info.
395e86b9096SDag-Erling Smørgrav  */
396e86b9096SDag-Erling Smørgrav void tcp_req_info_handle_readdone(struct tcp_req_info* req);
397e86b9096SDag-Erling Smørgrav 
398e86b9096SDag-Erling Smørgrav /**
399e86b9096SDag-Erling Smørgrav  * Add mesh state to the tcp req list of open requests.
400e86b9096SDag-Erling Smørgrav  * So the comm_reply can be removed off the mesh reply list when
401e86b9096SDag-Erling Smørgrav  * the tcp channel has to be closed (for other reasons then that that
402e86b9096SDag-Erling Smørgrav  * request was done, eg. channel closed by client or some format error).
403e86b9096SDag-Erling Smørgrav  * @param req: tcp req info structure.  It keeps track of the simultaneous
404e86b9096SDag-Erling Smørgrav  * 	requests and results on a tcp (or TLS) channel.
405e86b9096SDag-Erling Smørgrav  * @param mesh: mesh area for the state.
406e86b9096SDag-Erling Smørgrav  * @param m: mesh state to add.
407e86b9096SDag-Erling Smørgrav  * @return 0 on failure (malloc failure).
408e86b9096SDag-Erling Smørgrav  */
409e86b9096SDag-Erling Smørgrav int tcp_req_info_add_meshstate(struct tcp_req_info* req,
410e86b9096SDag-Erling Smørgrav 	struct mesh_area* mesh, struct mesh_state* m);
411e86b9096SDag-Erling Smørgrav 
412e86b9096SDag-Erling Smørgrav /**
413e86b9096SDag-Erling Smørgrav  * Send reply on tcp simultaneous answer channel.  May queue it up.
414e86b9096SDag-Erling Smørgrav  * @param req: request info structure.
415e86b9096SDag-Erling Smørgrav  */
416e86b9096SDag-Erling Smørgrav void tcp_req_info_send_reply(struct tcp_req_info* req);
417e86b9096SDag-Erling Smørgrav 
418e86b9096SDag-Erling Smørgrav /** the read channel has closed
419e86b9096SDag-Erling Smørgrav  * @param req: request. remaining queries are looked up and answered.
420e86b9096SDag-Erling Smørgrav  * @return zero if nothing to do, just close the tcp.
421e86b9096SDag-Erling Smørgrav  */
422e86b9096SDag-Erling Smørgrav int tcp_req_info_handle_read_close(struct tcp_req_info* req);
423e86b9096SDag-Erling Smørgrav 
424e86b9096SDag-Erling Smørgrav /** get the size of currently used tcp stream wait buffers (in bytes) */
425e86b9096SDag-Erling Smørgrav size_t tcp_req_info_get_stream_buffer_size(void);
426e86b9096SDag-Erling Smørgrav 
427c0caa2e2SCy Schubert /** get the size of currently used HTTP2 query buffers (in bytes) */
428c0caa2e2SCy Schubert size_t http2_get_query_buffer_size(void);
429c0caa2e2SCy Schubert /** get the size of currently used HTTP2 response buffers (in bytes) */
430c0caa2e2SCy Schubert size_t http2_get_response_buffer_size(void);
431c0caa2e2SCy Schubert 
432c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2
433c0caa2e2SCy Schubert /**
434c0caa2e2SCy Schubert  * Create nghttp2 callbacks to handle HTTP2 requests.
435c0caa2e2SCy Schubert  * @return malloc'ed struct, NULL on failure
436c0caa2e2SCy Schubert  */
437f44e67d1SCy Schubert nghttp2_session_callbacks* http2_req_callbacks_create(void);
438c0caa2e2SCy Schubert 
439c0caa2e2SCy Schubert /** Free http2 stream buffers and decrease buffer counters */
440c0caa2e2SCy Schubert void http2_req_stream_clear(struct http2_stream* h2_stream);
441c0caa2e2SCy Schubert 
442c0caa2e2SCy Schubert /**
443c0caa2e2SCy Schubert  * DNS response ready to be submitted to nghttp2, to be prepared for sending
444c0caa2e2SCy Schubert  * out. Response is stored in c->buffer. Copy to rbuffer because the c->buffer
445c0caa2e2SCy Schubert  * might be used before this will be send out.
446c0caa2e2SCy Schubert  * @param h2_session: http2 session, containing c->buffer which contains answer
447c0caa2e2SCy Schubert  * @param h2_stream: http2 stream, containing buffer to store answer in
448c0caa2e2SCy Schubert  * @return 0 on error, 1 otherwise
449c0caa2e2SCy Schubert  */
450c0caa2e2SCy Schubert int http2_submit_dns_response(struct http2_session* h2_session);
451c0caa2e2SCy Schubert #else
452c0caa2e2SCy Schubert int http2_submit_dns_response(void* v);
453c0caa2e2SCy Schubert #endif /* HAVE_NGHTTP2 */
454c0caa2e2SCy Schubert 
45525039b37SCy Schubert char* set_ip_dscp(int socket, int addrfamily, int ds);
45625039b37SCy Schubert 
4575469a995SCy Schubert /** for debug and profiling purposes only
4585469a995SCy Schubert  * @param ub_sock: the structure containing created socket info we want to print or log for
4595469a995SCy Schubert  */
4605469a995SCy Schubert void verbose_print_unbound_socket(struct unbound_socket* ub_sock);
4615469a995SCy Schubert 
462b7579f77SDag-Erling Smørgrav #endif /* LISTEN_DNSPORT_H */
463