xref: /freebsd/contrib/unbound/util/netevent.h (revision 335c7cda)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * util/netevent.h - event notification
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 contains event notification functions.
40b7579f77SDag-Erling Smørgrav  *
41b7579f77SDag-Erling Smørgrav  * There are three types of communication points
42b7579f77SDag-Erling Smørgrav  *    o UDP socket - perthread buffer.
43b7579f77SDag-Erling Smørgrav  *    o TCP-accept socket - array of TCP-sockets, socketcount.
44b7579f77SDag-Erling Smørgrav  *    o TCP socket - own buffer, parent-TCPaccept, read/write state,
45b7579f77SDag-Erling Smørgrav  *                   number of bytes read/written, timeout.
46b7579f77SDag-Erling Smørgrav  *
47b7579f77SDag-Erling Smørgrav  * There are sockets aimed towards our clients and towards the internet.
48b7579f77SDag-Erling Smørgrav  *    o frontside - aimed towards our clients, queries come in, answers back.
49b7579f77SDag-Erling Smørgrav  *    o behind - aimed towards internet, to the authoritative DNS servers.
50b7579f77SDag-Erling Smørgrav  *
51b7579f77SDag-Erling Smørgrav  * Several event types are available:
52b7579f77SDag-Erling Smørgrav  *    o comm_base - for thread safety of the comm points, one per thread.
53b7579f77SDag-Erling Smørgrav  *    o comm_point - udp and tcp networking, with callbacks.
54b7579f77SDag-Erling Smørgrav  *    o comm_timer - a timeout with callback.
55b7579f77SDag-Erling Smørgrav  *    o comm_signal - callbacks when signal is caught.
56b7579f77SDag-Erling Smørgrav  *    o comm_reply - holds reply info during networking callback.
57b7579f77SDag-Erling Smørgrav  *
58b7579f77SDag-Erling Smørgrav  */
59b7579f77SDag-Erling Smørgrav 
60b7579f77SDag-Erling Smørgrav #ifndef NET_EVENT_H
61b7579f77SDag-Erling Smørgrav #define NET_EVENT_H
62b7579f77SDag-Erling Smørgrav 
638f76bb7dSCy Schubert #include <sys/time.h>
6465b390aaSDag-Erling Smørgrav #include "dnscrypt/dnscrypt.h"
65c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2_NGHTTP2_H
66c0caa2e2SCy Schubert #include <nghttp2/nghttp2.h>
67c0caa2e2SCy Schubert #endif
6865b390aaSDag-Erling Smørgrav 
6917d15b25SDag-Erling Smørgrav struct sldns_buffer;
70b7579f77SDag-Erling Smørgrav struct comm_point;
71b7579f77SDag-Erling Smørgrav struct comm_reply;
724c75e3aaSDag-Erling Smørgrav struct tcl_list;
73e2d15004SDag-Erling Smørgrav struct ub_event_base;
745469a995SCy Schubert struct unbound_socket;
75b7579f77SDag-Erling Smørgrav 
76c0caa2e2SCy Schubert struct mesh_state;
77c0caa2e2SCy Schubert struct mesh_area;
78c0caa2e2SCy Schubert 
79b7579f77SDag-Erling Smørgrav /* internal event notification data storage structure. */
80b7579f77SDag-Erling Smørgrav struct internal_event;
81b7579f77SDag-Erling Smørgrav struct internal_base;
82e2d15004SDag-Erling Smørgrav struct internal_timer; /* A sub struct of the comm_timer super struct */
83b7579f77SDag-Erling Smørgrav 
84c0caa2e2SCy Schubert enum listen_type;
85c0caa2e2SCy Schubert 
86b7579f77SDag-Erling Smørgrav /** callback from communication point function type */
873005e0a3SDag-Erling Smørgrav typedef int comm_point_callback_type(struct comm_point*, void*, int,
88b7579f77SDag-Erling Smørgrav 	struct comm_reply*);
89b7579f77SDag-Erling Smørgrav 
90b7579f77SDag-Erling Smørgrav /** to pass no_error to callback function */
91b7579f77SDag-Erling Smørgrav #define NETEVENT_NOERROR 0
92b7579f77SDag-Erling Smørgrav /** to pass closed connection to callback function */
93b7579f77SDag-Erling Smørgrav #define NETEVENT_CLOSED -1
94b7579f77SDag-Erling Smørgrav /** to pass timeout happened to callback function */
95b7579f77SDag-Erling Smørgrav #define NETEVENT_TIMEOUT -2
96b7579f77SDag-Erling Smørgrav /** to pass fallback from capsforID to callback function; 0x20 failed */
97b7579f77SDag-Erling Smørgrav #define NETEVENT_CAPSFAIL -3
9857bddd21SDag-Erling Smørgrav /** to pass done transfer to callback function; http file is complete */
9957bddd21SDag-Erling Smørgrav #define NETEVENT_DONE -4
100369c6923SCy Schubert /** to pass write of the write packet is done to callback function
101369c6923SCy Schubert  * used when tcp_write_and_read is enabled */
102369c6923SCy Schubert #define NETEVENT_PKT_WRITTEN -5
103b7579f77SDag-Erling Smørgrav 
104b7579f77SDag-Erling Smørgrav /** timeout to slow accept calls when not possible, in msec. */
105b7579f77SDag-Erling Smørgrav #define NETEVENT_SLOW_ACCEPT_TIME 2000
106865f46b2SCy Schubert /** timeout to slow down log print, so it does not spam the logs, in sec */
107865f46b2SCy Schubert #define SLOW_LOG_TIME 10
108b7579f77SDag-Erling Smørgrav 
109b7579f77SDag-Erling Smørgrav /**
110b7579f77SDag-Erling Smørgrav  * A communication point dispatcher. Thread specific.
111b7579f77SDag-Erling Smørgrav  */
112b7579f77SDag-Erling Smørgrav struct comm_base {
113b7579f77SDag-Erling Smørgrav 	/** behind the scenes structure. with say libevent info. alloced */
114b7579f77SDag-Erling Smørgrav 	struct internal_base* eb;
115b7579f77SDag-Erling Smørgrav 	/** callback to stop listening on accept sockets,
116b7579f77SDag-Erling Smørgrav 	 * performed when accept() will not function properly */
117b7579f77SDag-Erling Smørgrav 	void (*stop_accept)(void*);
118b7579f77SDag-Erling Smørgrav 	/** callback to start listening on accept sockets, performed
119b7579f77SDag-Erling Smørgrav 	 * after stop_accept() then a timeout has passed. */
120b7579f77SDag-Erling Smørgrav 	void (*start_accept)(void*);
121b7579f77SDag-Erling Smørgrav 	/** user argument for stop_accept and start_accept functions */
122b7579f77SDag-Erling Smørgrav 	void* cb_arg;
123b7579f77SDag-Erling Smørgrav };
124b7579f77SDag-Erling Smørgrav 
125b7579f77SDag-Erling Smørgrav /**
126b7579f77SDag-Erling Smørgrav  * Reply information for a communication point.
127b7579f77SDag-Erling Smørgrav  */
128b7579f77SDag-Erling Smørgrav struct comm_reply {
129b7579f77SDag-Erling Smørgrav 	/** the comm_point with fd to send reply on to. */
130b7579f77SDag-Erling Smørgrav 	struct comm_point* c;
131b7579f77SDag-Erling Smørgrav 	/** the address (for UDP based communication) */
132865f46b2SCy Schubert 	struct sockaddr_storage remote_addr;
133b7579f77SDag-Erling Smørgrav 	/** length of address */
134865f46b2SCy Schubert 	socklen_t remote_addrlen;
135865f46b2SCy Schubert 	/** return type 0 (none), 4(IP4), 6(IP6)
136865f46b2SCy Schubert 	 *  used only with listen_type_udp_ancil* */
137b7579f77SDag-Erling Smørgrav 	int srctype;
13865b390aaSDag-Erling Smørgrav 	/* DnsCrypt context */
13965b390aaSDag-Erling Smørgrav #ifdef USE_DNSCRYPT
14065b390aaSDag-Erling Smørgrav 	uint8_t client_nonce[crypto_box_HALF_NONCEBYTES];
14165b390aaSDag-Erling Smørgrav 	uint8_t nmkey[crypto_box_BEFORENMBYTES];
142c7f4d7adSDag-Erling Smørgrav 	const dnsccert *dnsc_cert;
14365b390aaSDag-Erling Smørgrav 	int is_dnscrypted;
14465b390aaSDag-Erling Smørgrav #endif
145b7579f77SDag-Erling Smørgrav 	/** the return source interface data */
146b7579f77SDag-Erling Smørgrav 	union {
147b7579f77SDag-Erling Smørgrav #ifdef IPV6_PKTINFO
148b7579f77SDag-Erling Smørgrav 		struct in6_pktinfo v6info;
149b7579f77SDag-Erling Smørgrav #endif
150b7579f77SDag-Erling Smørgrav #ifdef IP_PKTINFO
151b7579f77SDag-Erling Smørgrav 		struct in_pktinfo v4info;
152b7579f77SDag-Erling Smørgrav #elif defined(IP_RECVDSTADDR)
153b7579f77SDag-Erling Smørgrav 		struct in_addr v4addr;
154b7579f77SDag-Erling Smørgrav #endif
155b7579f77SDag-Erling Smørgrav 	}
156b7579f77SDag-Erling Smørgrav 		/** variable with return source data */
157b7579f77SDag-Erling Smørgrav 		pktinfo;
15865b390aaSDag-Erling Smørgrav 	/** max udp size for udp packets */
15965b390aaSDag-Erling Smørgrav 	size_t max_udp_size;
160865f46b2SCy Schubert 	/* if set, the request came through a proxy */
161865f46b2SCy Schubert 	int is_proxied;
162865f46b2SCy Schubert 	/** the client address
163865f46b2SCy Schubert 	 *  the same as remote_addr if not proxied */
164865f46b2SCy Schubert 	struct sockaddr_storage client_addr;
165865f46b2SCy Schubert 	/** the original address length */
166865f46b2SCy Schubert 	socklen_t client_addrlen;
167b7579f77SDag-Erling Smørgrav };
168b7579f77SDag-Erling Smørgrav 
169b7579f77SDag-Erling Smørgrav /**
170b7579f77SDag-Erling Smørgrav  * Communication point to the network
171b7579f77SDag-Erling Smørgrav  * These behaviours can be accomplished by setting the flags
172b7579f77SDag-Erling Smørgrav  * and passing return values from the callback.
173b7579f77SDag-Erling Smørgrav  *    udp frontside: called after readdone. sendafter.
174b7579f77SDag-Erling Smørgrav  *    tcp frontside: called readdone, sendafter. close.
175b7579f77SDag-Erling Smørgrav  *    udp behind: called after readdone. No send after.
176b7579f77SDag-Erling Smørgrav  *    tcp behind: write done, read done, then called. No send after.
177b7579f77SDag-Erling Smørgrav  */
178b7579f77SDag-Erling Smørgrav struct comm_point {
179b7579f77SDag-Erling Smørgrav 	/** behind the scenes structure, with say libevent info. alloced. */
180b7579f77SDag-Erling Smørgrav 	struct internal_event* ev;
181f44e67d1SCy Schubert 	/** if the event is added or not */
182f44e67d1SCy Schubert 	int event_added;
183b7579f77SDag-Erling Smørgrav 
184*335c7cdaSCy Schubert 	/** Reference to struct that is part of the listening ports,
185*335c7cdaSCy Schubert 	 * where for listening ports information is kept about the address. */
1865469a995SCy Schubert 	struct unbound_socket* socket;
1875469a995SCy Schubert 
188b7579f77SDag-Erling Smørgrav 	/** file descriptor for communication point */
189b7579f77SDag-Erling Smørgrav 	int fd;
190b7579f77SDag-Erling Smørgrav 
191b7579f77SDag-Erling Smørgrav 	/** timeout (NULL if it does not). Malloced. */
192b7579f77SDag-Erling Smørgrav 	struct timeval* timeout;
193b7579f77SDag-Erling Smørgrav 
194b7579f77SDag-Erling Smørgrav 	/** buffer pointer. Either to perthread, or own buffer or NULL */
19517d15b25SDag-Erling Smørgrav 	struct sldns_buffer* buffer;
196b7579f77SDag-Erling Smørgrav 
197b7579f77SDag-Erling Smørgrav 	/* -------- TCP Handler -------- */
198b7579f77SDag-Erling Smørgrav 	/** Read/Write state for TCP */
199b7579f77SDag-Erling Smørgrav 	int tcp_is_reading;
200b7579f77SDag-Erling Smørgrav 	/** The current read/write count for TCP */
201b7579f77SDag-Erling Smørgrav 	size_t tcp_byte_count;
202b7579f77SDag-Erling Smørgrav 	/** parent communication point (for TCP sockets) */
203b7579f77SDag-Erling Smørgrav 	struct comm_point* tcp_parent;
204b7579f77SDag-Erling Smørgrav 	/** sockaddr from peer, for TCP handlers */
205b7579f77SDag-Erling Smørgrav 	struct comm_reply repinfo;
206b7579f77SDag-Erling Smørgrav 
207b7579f77SDag-Erling Smørgrav 	/* -------- TCP Accept -------- */
208b7579f77SDag-Erling Smørgrav 	/** the number of TCP handlers for this tcp-accept socket */
209b7579f77SDag-Erling Smørgrav 	int max_tcp_count;
21009a3aaf3SDag-Erling Smørgrav 	/** current number of tcp handler in-use for this accept socket */
21109a3aaf3SDag-Erling Smørgrav 	int cur_tcp_count;
212b7579f77SDag-Erling Smørgrav 	/** malloced array of tcp handlers for a tcp-accept,
213b7579f77SDag-Erling Smørgrav 	    of size max_tcp_count. */
214b7579f77SDag-Erling Smørgrav 	struct comm_point** tcp_handlers;
215b7579f77SDag-Erling Smørgrav 	/** linked list of free tcp_handlers to use for new queries.
216b7579f77SDag-Erling Smørgrav 	    For tcp_accept the first entry, for tcp_handlers the next one. */
217b7579f77SDag-Erling Smørgrav 	struct comm_point* tcp_free;
218b7579f77SDag-Erling Smørgrav 
219b7579f77SDag-Erling Smørgrav 	/* -------- SSL TCP DNS ------- */
220b7579f77SDag-Erling Smørgrav 	/** the SSL object with rw bio (owned) or for commaccept ctx ref */
221b7579f77SDag-Erling Smørgrav 	void* ssl;
222b7579f77SDag-Erling Smørgrav 	/** handshake state for init and renegotiate */
223b7579f77SDag-Erling Smørgrav 	enum {
224b7579f77SDag-Erling Smørgrav 		/** no handshake, it has been done */
225b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_none = 0,
226b7579f77SDag-Erling Smørgrav 		/** ssl initial handshake wants to read */
227b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_read,
228b7579f77SDag-Erling Smørgrav 		/** ssl initial handshake wants to write */
229b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_write,
230b7579f77SDag-Erling Smørgrav 		/** ssl_write wants to read */
231b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_hs_read,
232b7579f77SDag-Erling Smørgrav 		/** ssl_read wants to write */
233b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_hs_write
234b7579f77SDag-Erling Smørgrav 	} ssl_shake_state;
235b7579f77SDag-Erling Smørgrav 
23657bddd21SDag-Erling Smørgrav 	/* -------- HTTP ------- */
237c0caa2e2SCy Schubert 	/** Do not allow connection to use HTTP version lower than this. 0=no
238c0caa2e2SCy Schubert 	 * minimum. */
239c0caa2e2SCy Schubert 	enum {
240c0caa2e2SCy Schubert 		http_version_none = 0,
241c0caa2e2SCy Schubert 		http_version_2 = 2
242c0caa2e2SCy Schubert 	} http_min_version;
243c0caa2e2SCy Schubert 	/** http endpoint */
244c0caa2e2SCy Schubert 	char* http_endpoint;
245c0caa2e2SCy Schubert 	/* -------- HTTP/1.1 ------- */
24657bddd21SDag-Erling Smørgrav 	/** Currently reading in http headers */
24757bddd21SDag-Erling Smørgrav 	int http_in_headers;
24857bddd21SDag-Erling Smørgrav 	/** Currently reading in chunk headers, 0=not, 1=firstline, 2=unused
24957bddd21SDag-Erling Smørgrav 	 * (more lines), 3=trailer headers after chunk */
25057bddd21SDag-Erling Smørgrav 	int http_in_chunk_headers;
25157bddd21SDag-Erling Smørgrav 	/** chunked transfer */
25257bddd21SDag-Erling Smørgrav 	int http_is_chunked;
25357bddd21SDag-Erling Smørgrav 	/** http temp buffer (shared buffer for temporary work) */
25457bddd21SDag-Erling Smørgrav 	struct sldns_buffer* http_temp;
25557bddd21SDag-Erling Smørgrav 	/** http stored content in buffer */
25657bddd21SDag-Erling Smørgrav 	size_t http_stored;
257c0caa2e2SCy Schubert 	/* -------- HTTP/2 ------- */
258c0caa2e2SCy Schubert 	/** http2 session */
259c0caa2e2SCy Schubert 	struct http2_session* h2_session;
260c0caa2e2SCy Schubert 	/** set to 1 if h2 is negotiated to be used (using alpn) */
261c0caa2e2SCy Schubert 	int use_h2;
262c0caa2e2SCy Schubert 	/** stream currently being handled */
263c0caa2e2SCy Schubert 	struct http2_stream* h2_stream;
264c0caa2e2SCy Schubert 	/** maximum allowed query buffer size, per stream */
265c0caa2e2SCy Schubert 	size_t http2_stream_max_qbuffer_size;
266c0caa2e2SCy Schubert 	/** maximum number of HTTP/2 streams per connection. Send in HTTP/2
267c0caa2e2SCy Schubert 	 * SETTINGS frame. */
268c0caa2e2SCy Schubert 	uint32_t http2_max_streams;
26957bddd21SDag-Erling Smørgrav 
270ff825849SDag-Erling Smørgrav 	/* -------- dnstap ------- */
271ff825849SDag-Erling Smørgrav 	/** the dnstap environment */
272ff825849SDag-Erling Smørgrav 	struct dt_env* dtenv;
273ff825849SDag-Erling Smørgrav 
274b7579f77SDag-Erling Smørgrav 	/** is this a UDP, TCP-accept or TCP socket. */
275b7579f77SDag-Erling Smørgrav 	enum comm_point_type {
276b7579f77SDag-Erling Smørgrav 		/** UDP socket - handle datagrams. */
277b7579f77SDag-Erling Smørgrav 		comm_udp,
278b7579f77SDag-Erling Smørgrav 		/** TCP accept socket - only creates handlers if readable. */
279b7579f77SDag-Erling Smørgrav 		comm_tcp_accept,
280b7579f77SDag-Erling Smørgrav 		/** TCP handler socket - handle byteperbyte readwrite. */
281b7579f77SDag-Erling Smørgrav 		comm_tcp,
28257bddd21SDag-Erling Smørgrav 		/** HTTP handler socket */
28357bddd21SDag-Erling Smørgrav 		comm_http,
284b7579f77SDag-Erling Smørgrav 		/** AF_UNIX socket - for internal commands. */
285b7579f77SDag-Erling Smørgrav 		comm_local,
286b7579f77SDag-Erling Smørgrav 		/** raw - not DNS format - for pipe readers and writers */
287b7579f77SDag-Erling Smørgrav 		comm_raw
288b7579f77SDag-Erling Smørgrav 	}
289b7579f77SDag-Erling Smørgrav 		/** variable with type of socket, UDP,TCP-accept,TCP,pipe */
290b7579f77SDag-Erling Smørgrav 		type;
291b7579f77SDag-Erling Smørgrav 
292865f46b2SCy Schubert 	/* -------- PROXYv2 ------- */
293865f46b2SCy Schubert 	/** if set, PROXYv2 is expected on this connection */
294865f46b2SCy Schubert 	int pp2_enabled;
295865f46b2SCy Schubert 	/** header state for the PROXYv2 header (for TCP) */
296865f46b2SCy Schubert 	enum {
297865f46b2SCy Schubert 		/** no header encounter yet */
298865f46b2SCy Schubert 		pp2_header_none = 0,
299865f46b2SCy Schubert 		/** read the static part of the header */
300865f46b2SCy Schubert 		pp2_header_init,
301865f46b2SCy Schubert 		/** read the full header */
302865f46b2SCy Schubert 		pp2_header_done
303865f46b2SCy Schubert 	} pp2_header_state;
304865f46b2SCy Schubert 
305b7579f77SDag-Erling Smørgrav 	/* ---------- Behaviour ----------- */
306b7579f77SDag-Erling Smørgrav 	/** if set the connection is NOT closed on delete. */
307b7579f77SDag-Erling Smørgrav 	int do_not_close;
308b7579f77SDag-Erling Smørgrav 
309b7579f77SDag-Erling Smørgrav 	/** if set, the connection is closed on error, on timeout,
310b7579f77SDag-Erling Smørgrav 	    and after read/write completes. No callback is done. */
311b7579f77SDag-Erling Smørgrav 	int tcp_do_close;
312b7579f77SDag-Erling Smørgrav 
313369c6923SCy Schubert 	/** flag that indicates the stream is both written and read from. */
314369c6923SCy Schubert 	int tcp_write_and_read;
315369c6923SCy Schubert 
316369c6923SCy Schubert 	/** byte count for written length over write channel, for when
317369c6923SCy Schubert 	 * tcp_write_and_read is enabled.  When tcp_write_and_read is enabled,
318369c6923SCy Schubert 	 * this is the counter for writing, the one for reading is in the
319369c6923SCy Schubert 	 * commpoint.buffer sldns buffer.  The counter counts from 0 to
320369c6923SCy Schubert 	 * 2+tcp_write_pkt_len, and includes the tcp length bytes. */
321369c6923SCy Schubert 	size_t tcp_write_byte_count;
322369c6923SCy Schubert 
323369c6923SCy Schubert 	/** packet to write currently over the write channel. for when
324369c6923SCy Schubert 	 * tcp_write_and_read is enabled.  When tcp_write_and_read is enabled,
325369c6923SCy Schubert 	 * this is the buffer for the written packet, the commpoint.buffer
326369c6923SCy Schubert 	 * sldns buffer is the buffer for the received packet. */
327369c6923SCy Schubert 	uint8_t* tcp_write_pkt;
328369c6923SCy Schubert 	/** length of tcp_write_pkt in bytes */
329369c6923SCy Schubert 	size_t tcp_write_pkt_len;
330369c6923SCy Schubert 
331369c6923SCy Schubert 	/** if set try to read another packet again (over connection with
332369c6923SCy Schubert 	 * multiple packets), once set, tries once, then zero again,
333369c6923SCy Schubert 	 * so set it in the packet complete section.
334369c6923SCy Schubert 	 * The pointer itself has to be set before the callback is invoked,
335369c6923SCy Schubert 	 * when you set things up, and continue to exist also after the
336369c6923SCy Schubert 	 * commpoint is closed and deleted in your callback.  So that after
337369c6923SCy Schubert 	 * the callback cleans up netevent can see what it has to do.
338369c6923SCy Schubert 	 * Or leave NULL if it is not used at all. */
339369c6923SCy Schubert 	int* tcp_more_read_again;
340369c6923SCy Schubert 
341369c6923SCy Schubert 	/** if set try to write another packet (over connection with
342369c6923SCy Schubert 	 * multiple packets), once set, tries once, then zero again,
343369c6923SCy Schubert 	 * so set it in the packet complete section.
344369c6923SCy Schubert 	 * The pointer itself has to be set before the callback is invoked,
345369c6923SCy Schubert 	 * when you set things up, and continue to exist also after the
346369c6923SCy Schubert 	 * commpoint is closed and deleted in your callback.  So that after
347369c6923SCy Schubert 	 * the callback cleans up netevent can see what it has to do.
348369c6923SCy Schubert 	 * Or leave NULL if it is not used at all. */
349369c6923SCy Schubert 	int* tcp_more_write_again;
350369c6923SCy Schubert 
351b7579f77SDag-Erling Smørgrav 	/** if set, read/write completes:
352b7579f77SDag-Erling Smørgrav 		read/write state of tcp is toggled.
353b7579f77SDag-Erling Smørgrav 		buffer reset/bytecount reset.
354b7579f77SDag-Erling Smørgrav 		this flag cleared.
355b7579f77SDag-Erling Smørgrav 	    So that when that is done the callback is called. */
356b7579f77SDag-Erling Smørgrav 	int tcp_do_toggle_rw;
357b7579f77SDag-Erling Smørgrav 
358b5663de9SDag-Erling Smørgrav 	/** timeout in msec for TCP wait times for this connection */
359b5663de9SDag-Erling Smørgrav 	int tcp_timeout_msec;
360b5663de9SDag-Erling Smørgrav 
3614c75e3aaSDag-Erling Smørgrav 	/** if set, tcp keepalive is enabled on this connection */
3624c75e3aaSDag-Erling Smørgrav 	int tcp_keepalive;
3634c75e3aaSDag-Erling Smørgrav 
364b7579f77SDag-Erling Smørgrav 	/** if set, checks for pending error from nonblocking connect() call.*/
365b7579f77SDag-Erling Smørgrav 	int tcp_check_nb_connect;
366b7579f77SDag-Erling Smørgrav 
3674c75e3aaSDag-Erling Smørgrav 	/** if set, check for connection limit on tcp accept. */
3684c75e3aaSDag-Erling Smørgrav 	struct tcl_list* tcp_conn_limit;
3694c75e3aaSDag-Erling Smørgrav 	/** the entry for the connection. */
3704c75e3aaSDag-Erling Smørgrav 	struct tcl_addr* tcl_addr;
3714c75e3aaSDag-Erling Smørgrav 
372e86b9096SDag-Erling Smørgrav 	/** the structure to keep track of open requests on this channel */
373e86b9096SDag-Erling Smørgrav 	struct tcp_req_info* tcp_req_info;
374e86b9096SDag-Erling Smørgrav 
375b5663de9SDag-Erling Smørgrav #ifdef USE_MSG_FASTOPEN
376b5663de9SDag-Erling Smørgrav 	/** used to track if the sendto() call should be done when using TFO. */
377b5663de9SDag-Erling Smørgrav 	int tcp_do_fastopen;
378b5663de9SDag-Erling Smørgrav #endif
379b5663de9SDag-Erling Smørgrav 
38065b390aaSDag-Erling Smørgrav #ifdef USE_DNSCRYPT
38165b390aaSDag-Erling Smørgrav 	/** Is this a dnscrypt channel */
38265b390aaSDag-Erling Smørgrav 	int dnscrypt;
38365b390aaSDag-Erling Smørgrav 	/** encrypted buffer pointer. Either to perthread, or own buffer or NULL */
38465b390aaSDag-Erling Smørgrav 	struct sldns_buffer* dnscrypt_buffer;
38565b390aaSDag-Erling Smørgrav #endif
386b7579f77SDag-Erling Smørgrav 	/** number of queries outstanding on this socket, used by
387b7579f77SDag-Erling Smørgrav 	 * outside network for udp ports */
388b7579f77SDag-Erling Smørgrav 	int inuse;
3898f76bb7dSCy Schubert 	/** the timestamp when the packet was received by the kernel */
3908f76bb7dSCy Schubert 	struct timeval recv_tv;
391b7579f77SDag-Erling Smørgrav 	/** callback when done.
392b7579f77SDag-Erling Smørgrav 	    tcp_accept does not get called back, is NULL then.
393b7579f77SDag-Erling Smørgrav 	    If a timeout happens, callback with timeout=1 is called.
394b7579f77SDag-Erling Smørgrav 	    If an error happens, callback is called with error set
395b7579f77SDag-Erling Smørgrav 	    nonzero. If not NETEVENT_NOERROR, it is an errno value.
396b7579f77SDag-Erling Smørgrav 	    If the connection is closed (by remote end) then the
397b7579f77SDag-Erling Smørgrav 	    callback is called with error set to NETEVENT_CLOSED=-1.
398b7579f77SDag-Erling Smørgrav 	    If a timeout happens on the connection, the error is set to
399b7579f77SDag-Erling Smørgrav 	    NETEVENT_TIMEOUT=-2.
400b7579f77SDag-Erling Smørgrav 	    The reply_info can be copied if the reply needs to happen at a
401b7579f77SDag-Erling Smørgrav 	    later time. It consists of a struct with commpoint and address.
402b7579f77SDag-Erling Smørgrav 	    It can be passed to a msg send routine some time later.
403b7579f77SDag-Erling Smørgrav 	    Note the reply information is temporary and must be copied.
404b7579f77SDag-Erling Smørgrav 	    NULL is passed for_reply info, in cases where error happened.
405b7579f77SDag-Erling Smørgrav 
406b7579f77SDag-Erling Smørgrav 	    declare as:
407b7579f77SDag-Erling Smørgrav 	    int my_callback(struct comm_point* c, void* my_arg, int error,
408b7579f77SDag-Erling Smørgrav 		struct comm_reply *reply_info);
409b7579f77SDag-Erling Smørgrav 
410b7579f77SDag-Erling Smørgrav 	    if the routine returns 0, nothing is done.
411b7579f77SDag-Erling Smørgrav 	    Notzero, the buffer will be sent back to client.
412b7579f77SDag-Erling Smørgrav 	    		For UDP this is done without changing the commpoint.
413b7579f77SDag-Erling Smørgrav 			In TCP it sets write state.
414b7579f77SDag-Erling Smørgrav 	*/
4153005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback;
416b7579f77SDag-Erling Smørgrav 	/** argument to pass to callback. */
417b7579f77SDag-Erling Smørgrav 	void *cb_arg;
418b7579f77SDag-Erling Smørgrav };
419b7579f77SDag-Erling Smørgrav 
420b7579f77SDag-Erling Smørgrav /**
421b7579f77SDag-Erling Smørgrav  * Structure only for making timeout events.
422b7579f77SDag-Erling Smørgrav  */
423b7579f77SDag-Erling Smørgrav struct comm_timer {
424e2d15004SDag-Erling Smørgrav 	/** the internal event stuff (derived) */
425b7579f77SDag-Erling Smørgrav 	struct internal_timer* ev_timer;
426b7579f77SDag-Erling Smørgrav 
427b7579f77SDag-Erling Smørgrav 	/** callback function, takes user arg only */
428b7579f77SDag-Erling Smørgrav 	void (*callback)(void*);
429b7579f77SDag-Erling Smørgrav 
430b7579f77SDag-Erling Smørgrav 	/** callback user argument */
431b7579f77SDag-Erling Smørgrav 	void* cb_arg;
432b7579f77SDag-Erling Smørgrav };
433b7579f77SDag-Erling Smørgrav 
434b7579f77SDag-Erling Smørgrav /**
435b7579f77SDag-Erling Smørgrav  * Structure only for signal events.
436b7579f77SDag-Erling Smørgrav  */
437b7579f77SDag-Erling Smørgrav struct comm_signal {
438b7579f77SDag-Erling Smørgrav 	/** the communication base */
439b7579f77SDag-Erling Smørgrav 	struct comm_base* base;
440b7579f77SDag-Erling Smørgrav 
441b7579f77SDag-Erling Smørgrav 	/** the internal event stuff */
442b7579f77SDag-Erling Smørgrav 	struct internal_signal* ev_signal;
443b7579f77SDag-Erling Smørgrav 
444b7579f77SDag-Erling Smørgrav 	/** callback function, takes signal number and user arg */
445b7579f77SDag-Erling Smørgrav 	void (*callback)(int, void*);
446b7579f77SDag-Erling Smørgrav 
447b7579f77SDag-Erling Smørgrav 	/** callback user argument */
448b7579f77SDag-Erling Smørgrav 	void* cb_arg;
449b7579f77SDag-Erling Smørgrav };
450b7579f77SDag-Erling Smørgrav 
451b7579f77SDag-Erling Smørgrav /**
452b7579f77SDag-Erling Smørgrav  * Create a new comm base.
453b7579f77SDag-Erling Smørgrav  * @param sigs: if true it attempts to create a default loop for
454b7579f77SDag-Erling Smørgrav  *   signal handling.
455b7579f77SDag-Erling Smørgrav  * @return: the new comm base. NULL on error.
456b7579f77SDag-Erling Smørgrav  */
457b7579f77SDag-Erling Smørgrav struct comm_base* comm_base_create(int sigs);
458b7579f77SDag-Erling Smørgrav 
459b7579f77SDag-Erling Smørgrav /**
460e2d15004SDag-Erling Smørgrav  * Create comm base that uses the given ub_event_base (underlying pluggable
461e2d15004SDag-Erling Smørgrav  * event mechanism pointer).
462e2d15004SDag-Erling Smørgrav  * @param base: underlying pluggable event base.
46317d15b25SDag-Erling Smørgrav  * @return: the new comm base. NULL on error.
46417d15b25SDag-Erling Smørgrav  */
465e2d15004SDag-Erling Smørgrav struct comm_base* comm_base_create_event(struct ub_event_base* base);
46617d15b25SDag-Erling Smørgrav 
46717d15b25SDag-Erling Smørgrav /**
46817d15b25SDag-Erling Smørgrav  * Delete comm base structure but not the underlying lib event base.
46917d15b25SDag-Erling Smørgrav  * All comm points must have been deleted.
47017d15b25SDag-Erling Smørgrav  * @param b: the base to delete.
47117d15b25SDag-Erling Smørgrav  */
47217d15b25SDag-Erling Smørgrav void comm_base_delete_no_base(struct comm_base* b);
47317d15b25SDag-Erling Smørgrav 
47417d15b25SDag-Erling Smørgrav /**
475b7579f77SDag-Erling Smørgrav  * Destroy a comm base.
476b7579f77SDag-Erling Smørgrav  * All comm points must have been deleted.
477b7579f77SDag-Erling Smørgrav  * @param b: the base to delete.
478b7579f77SDag-Erling Smørgrav  */
479b7579f77SDag-Erling Smørgrav void comm_base_delete(struct comm_base* b);
480b7579f77SDag-Erling Smørgrav 
481b7579f77SDag-Erling Smørgrav /**
482b7579f77SDag-Erling Smørgrav  * Obtain two pointers. The pointers never change (until base_delete()).
483b7579f77SDag-Erling Smørgrav  * The pointers point to time values that are updated regularly.
484b7579f77SDag-Erling Smørgrav  * @param b: the communication base that will update the time values.
485b7579f77SDag-Erling Smørgrav  * @param tt: pointer to time in seconds is returned.
486b7579f77SDag-Erling Smørgrav  * @param tv: pointer to time in microseconds is returned.
487b7579f77SDag-Erling Smørgrav  */
48817d15b25SDag-Erling Smørgrav void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
489b7579f77SDag-Erling Smørgrav 
490b7579f77SDag-Erling Smørgrav /**
491b7579f77SDag-Erling Smørgrav  * Dispatch the comm base events.
492b7579f77SDag-Erling Smørgrav  * @param b: the communication to perform.
493b7579f77SDag-Erling Smørgrav  */
494b7579f77SDag-Erling Smørgrav void comm_base_dispatch(struct comm_base* b);
495b7579f77SDag-Erling Smørgrav 
496b7579f77SDag-Erling Smørgrav /**
497b7579f77SDag-Erling Smørgrav  * Exit from dispatch loop.
498b7579f77SDag-Erling Smørgrav  * @param b: the communication base that is in dispatch().
499b7579f77SDag-Erling Smørgrav  */
500b7579f77SDag-Erling Smørgrav void comm_base_exit(struct comm_base* b);
501b7579f77SDag-Erling Smørgrav 
502b7579f77SDag-Erling Smørgrav /**
503b7579f77SDag-Erling Smørgrav  * Set the slow_accept mode handlers.  You can not provide these if you do
504b7579f77SDag-Erling Smørgrav  * not perform accept() calls.
505b7579f77SDag-Erling Smørgrav  * @param b: comm base
506b7579f77SDag-Erling Smørgrav  * @param stop_accept: function that stops listening to accept fds.
507b7579f77SDag-Erling Smørgrav  * @param start_accept: function that resumes listening to accept fds.
508b7579f77SDag-Erling Smørgrav  * @param arg: callback arg to pass to the functions.
509b7579f77SDag-Erling Smørgrav  */
510b7579f77SDag-Erling Smørgrav void comm_base_set_slow_accept_handlers(struct comm_base* b,
511b7579f77SDag-Erling Smørgrav 	void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
512b7579f77SDag-Erling Smørgrav 
513b7579f77SDag-Erling Smørgrav /**
514b7579f77SDag-Erling Smørgrav  * Access internal data structure (for util/tube.c on windows)
515b7579f77SDag-Erling Smørgrav  * @param b: comm base
516e2d15004SDag-Erling Smørgrav  * @return ub_event_base.
517b7579f77SDag-Erling Smørgrav  */
518e2d15004SDag-Erling Smørgrav struct ub_event_base* comm_base_internal(struct comm_base* b);
519b7579f77SDag-Erling Smørgrav 
520b7579f77SDag-Erling Smørgrav /**
521b7579f77SDag-Erling Smørgrav  * Create an UDP comm point. Calls malloc.
522b7579f77SDag-Erling Smørgrav  * setups the structure with the parameters you provide.
523b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
524b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor of open UDP socket.
525b7579f77SDag-Erling Smørgrav  * @param buffer: shared buffer by UDP sockets from this thread.
526865f46b2SCy Schubert  * @param pp2_enabled: if the comm point will support PROXYv2.
527b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer.
528b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
5295469a995SCy Schubert  * @param socket: and opened socket properties will be passed to your callback function.
530b7579f77SDag-Erling Smørgrav  * @return: returns the allocated communication point. NULL on error.
531b7579f77SDag-Erling Smørgrav  * Sets timeout to NULL. Turns off TCP options.
532b7579f77SDag-Erling Smørgrav  */
533b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_udp(struct comm_base* base,
534865f46b2SCy Schubert 	int fd, struct sldns_buffer* buffer, int pp2_enabled,
5355469a995SCy Schubert 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
536b7579f77SDag-Erling Smørgrav 
537b7579f77SDag-Erling Smørgrav /**
538b7579f77SDag-Erling Smørgrav  * Create an UDP with ancillary data comm point. Calls malloc.
539b7579f77SDag-Erling Smørgrav  * Uses recvmsg instead of recv to get udp message.
540b7579f77SDag-Erling Smørgrav  * setups the structure with the parameters you provide.
541b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
542b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor of open UDP socket.
543b7579f77SDag-Erling Smørgrav  * @param buffer: shared buffer by UDP sockets from this thread.
544865f46b2SCy Schubert  * @param pp2_enabled: if the comm point will support PROXYv2.
545b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer.
546b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
5475469a995SCy Schubert  * @param socket: and opened socket properties will be passed to your callback function.
548b7579f77SDag-Erling Smørgrav  * @return: returns the allocated communication point. NULL on error.
549b7579f77SDag-Erling Smørgrav  * Sets timeout to NULL. Turns off TCP options.
550b7579f77SDag-Erling Smørgrav  */
551b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
552865f46b2SCy Schubert 	int fd, struct sldns_buffer* buffer, int pp2_enabled,
5535469a995SCy Schubert 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
554b7579f77SDag-Erling Smørgrav 
555b7579f77SDag-Erling Smørgrav /**
556b7579f77SDag-Erling Smørgrav  * Create a TCP listener comm point. Calls malloc.
557b7579f77SDag-Erling Smørgrav  * Setups the structure with the parameters you provide.
558b7579f77SDag-Erling Smørgrav  * Also Creates TCP Handlers, pre allocated for you.
559b7579f77SDag-Erling Smørgrav  * Uses the parameters you provide.
560b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
561b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor of open TCP socket set to listen nonblocking.
562b7579f77SDag-Erling Smørgrav  * @param num: becomes max_tcp_count, the routine allocates that
563b7579f77SDag-Erling Smørgrav  *	many tcp handler commpoints.
5644c75e3aaSDag-Erling Smørgrav  * @param idle_timeout: TCP idle timeout in ms.
565c0caa2e2SCy Schubert  * @param harden_large_queries: whether query size should be limited.
566c0caa2e2SCy Schubert  * @param http_max_streams: maximum number of HTTP/2 streams per connection.
567c0caa2e2SCy Schubert  * @param http_endpoint: HTTP endpoint to service queries on
5684c75e3aaSDag-Erling Smørgrav  * @param tcp_conn_limit: TCP connection limit info.
569b7579f77SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
570e86b9096SDag-Erling Smørgrav  * @param spoolbuf: shared spool buffer for tcp_req_info structures.
571e86b9096SDag-Erling Smørgrav  * 	or NULL to not create those structures in the tcp handlers.
572c0caa2e2SCy Schubert  * @param port_type: the type of port we are creating a TCP listener for. Used
573c0caa2e2SCy Schubert  * 	to select handler type to use.
574865f46b2SCy Schubert  * @param pp2_enabled: if the comm point will support PROXYv2.
575b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for TCP handlers.
576b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
5775469a995SCy Schubert  * @param socket: and opened socket properties will be passed to your callback function.
578b7579f77SDag-Erling Smørgrav  * @return: returns the TCP listener commpoint. You can find the
579b7579f77SDag-Erling Smørgrav  *  	TCP handlers in the array inside the listener commpoint.
580b7579f77SDag-Erling Smørgrav  *	returns NULL on error.
581b7579f77SDag-Erling Smørgrav  * Inits timeout to NULL. All handlers are on the free list.
582b7579f77SDag-Erling Smørgrav  */
583b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_tcp(struct comm_base* base,
584c0caa2e2SCy Schubert 	int fd, int num, int idle_timeout, int harden_large_queries,
585c0caa2e2SCy Schubert 	uint32_t http_max_streams, char* http_endpoint,
586c0caa2e2SCy Schubert 	struct tcl_list* tcp_conn_limit,
587e86b9096SDag-Erling Smørgrav 	size_t bufsize, struct sldns_buffer* spoolbuf,
588865f46b2SCy Schubert 	enum listen_type port_type, int pp2_enabled,
5895469a995SCy Schubert 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
590b7579f77SDag-Erling Smørgrav 
591b7579f77SDag-Erling Smørgrav /**
592b7579f77SDag-Erling Smørgrav  * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
593b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
594b7579f77SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
595b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
596b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
597b7579f77SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
598b7579f77SDag-Erling Smørgrav  */
599b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
6003005e0a3SDag-Erling Smørgrav 	size_t bufsize, comm_point_callback_type* callback, void* callback_arg);
601b7579f77SDag-Erling Smørgrav 
602b7579f77SDag-Erling Smørgrav /**
60357bddd21SDag-Erling Smørgrav  * Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1.
60457bddd21SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
60557bddd21SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
60657bddd21SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
60757bddd21SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
60857bddd21SDag-Erling Smørgrav  * @param temp: sldns buffer, shared between other http_out commpoints, for
60957bddd21SDag-Erling Smørgrav  * 	temporary data when performing callbacks.
61057bddd21SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
61157bddd21SDag-Erling Smørgrav  */
61257bddd21SDag-Erling Smørgrav struct comm_point* comm_point_create_http_out(struct comm_base* base,
61357bddd21SDag-Erling Smørgrav 	size_t bufsize, comm_point_callback_type* callback,
61457bddd21SDag-Erling Smørgrav 	void* callback_arg, struct sldns_buffer* temp);
61557bddd21SDag-Erling Smørgrav 
61657bddd21SDag-Erling Smørgrav /**
617b7579f77SDag-Erling Smørgrav  * Create commpoint to listen to a local domain file descriptor.
618b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
619b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
620b7579f77SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
621b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
622b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
623b7579f77SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
624b7579f77SDag-Erling Smørgrav  */
625b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_local(struct comm_base* base,
626b7579f77SDag-Erling Smørgrav 	int fd, size_t bufsize,
6273005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback, void* callback_arg);
628b7579f77SDag-Erling Smørgrav 
629b7579f77SDag-Erling Smørgrav /**
630b7579f77SDag-Erling Smørgrav  * Create commpoint to listen to a local domain pipe descriptor.
631b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
632b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
633b7579f77SDag-Erling Smørgrav  * @param writing: true if you want to listen to writes, false for reads.
634b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
635b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
636b7579f77SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
637b7579f77SDag-Erling Smørgrav  */
638b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_raw(struct comm_base* base,
639b7579f77SDag-Erling Smørgrav 	int fd, int writing,
6403005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback, void* callback_arg);
641b7579f77SDag-Erling Smørgrav 
642b7579f77SDag-Erling Smørgrav /**
643b7579f77SDag-Erling Smørgrav  * Close a comm point fd.
644b7579f77SDag-Erling Smørgrav  * @param c: comm point to close.
645b7579f77SDag-Erling Smørgrav  */
646b7579f77SDag-Erling Smørgrav void comm_point_close(struct comm_point* c);
647b7579f77SDag-Erling Smørgrav 
648b7579f77SDag-Erling Smørgrav /**
649b7579f77SDag-Erling Smørgrav  * Close and deallocate (free) the comm point. If the comm point is
650b7579f77SDag-Erling Smørgrav  * a tcp-accept point, also its tcp-handler points are deleted.
651b7579f77SDag-Erling Smørgrav  * @param c: comm point to delete.
652b7579f77SDag-Erling Smørgrav  */
653b7579f77SDag-Erling Smørgrav void comm_point_delete(struct comm_point* c);
654b7579f77SDag-Erling Smørgrav 
655b7579f77SDag-Erling Smørgrav /**
656b7579f77SDag-Erling Smørgrav  * Send reply. Put message into commpoint buffer.
657b7579f77SDag-Erling Smørgrav  * @param repinfo: The reply info copied from a commpoint callback call.
658b7579f77SDag-Erling Smørgrav  */
659b7579f77SDag-Erling Smørgrav void comm_point_send_reply(struct comm_reply* repinfo);
660b7579f77SDag-Erling Smørgrav 
661b7579f77SDag-Erling Smørgrav /**
662b7579f77SDag-Erling Smørgrav  * Drop reply. Cleans up.
663b7579f77SDag-Erling Smørgrav  * @param repinfo: The reply info copied from a commpoint callback call.
664b7579f77SDag-Erling Smørgrav  */
665b7579f77SDag-Erling Smørgrav void comm_point_drop_reply(struct comm_reply* repinfo);
666b7579f77SDag-Erling Smørgrav 
667b7579f77SDag-Erling Smørgrav /**
668b7579f77SDag-Erling Smørgrav  * Send an udp message over a commpoint.
669b7579f77SDag-Erling Smørgrav  * @param c: commpoint to send it from.
670b7579f77SDag-Erling Smørgrav  * @param packet: what to send.
671369c6923SCy Schubert  * @param addr: where to send it to.   If NULL, send is performed,
672369c6923SCy Schubert  * 	for connected sockets, to the connected address.
673b7579f77SDag-Erling Smørgrav  * @param addrlen: length of addr.
6747341cb0cSXin LI  * @param is_connected: if the UDP socket is connect()ed.
675b7579f77SDag-Erling Smørgrav  * @return: false on a failure.
676b7579f77SDag-Erling Smørgrav  */
67717d15b25SDag-Erling Smørgrav int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
6787341cb0cSXin LI 	struct sockaddr* addr, socklen_t addrlen,int is_connected);
679b7579f77SDag-Erling Smørgrav 
680b7579f77SDag-Erling Smørgrav /**
681b7579f77SDag-Erling Smørgrav  * Stop listening for input on the commpoint. No callbacks will happen.
682b7579f77SDag-Erling Smørgrav  * @param c: commpoint to disable. The fd is not closed.
683b7579f77SDag-Erling Smørgrav  */
684b7579f77SDag-Erling Smørgrav void comm_point_stop_listening(struct comm_point* c);
685b7579f77SDag-Erling Smørgrav 
686b7579f77SDag-Erling Smørgrav /**
687b7579f77SDag-Erling Smørgrav  * Start listening again for input on the comm point.
688b7579f77SDag-Erling Smørgrav  * @param c: commpoint to enable again.
689b7579f77SDag-Erling Smørgrav  * @param newfd: new fd, or -1 to leave fd be.
690b5663de9SDag-Erling Smørgrav  * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout.
691b5663de9SDag-Erling Smørgrav  *	So seconds*1000.
692b7579f77SDag-Erling Smørgrav  */
693b5663de9SDag-Erling Smørgrav void comm_point_start_listening(struct comm_point* c, int newfd, int msec);
694b7579f77SDag-Erling Smørgrav 
695b7579f77SDag-Erling Smørgrav /**
696b7579f77SDag-Erling Smørgrav  * Stop listening and start listening again for reading or writing.
697b7579f77SDag-Erling Smørgrav  * @param c: commpoint
698b7579f77SDag-Erling Smørgrav  * @param rd: if true, listens for reading.
699b7579f77SDag-Erling Smørgrav  * @param wr: if true, listens for writing.
700b7579f77SDag-Erling Smørgrav  */
701b7579f77SDag-Erling Smørgrav void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
702b7579f77SDag-Erling Smørgrav 
703b7579f77SDag-Erling Smørgrav /**
704f44e67d1SCy Schubert  * For TCP handlers that use c->tcp_timeout_msec, this routine adjusts
705f44e67d1SCy Schubert  * it with the minimum.  Otherwise, a 0 value advertised without the
706f44e67d1SCy Schubert  * minimum applied moves to a 0 in comm_point_start_listening and that
707f44e67d1SCy Schubert  * routine treats it as no timeout, listen forever, which is not wanted.
708f44e67d1SCy Schubert  * @param c: comm point to use the tcp_timeout_msec of.
709f44e67d1SCy Schubert  * @return adjusted tcp_timeout_msec value with the minimum if smaller.
710f44e67d1SCy Schubert  */
711f44e67d1SCy Schubert int adjusted_tcp_timeout(struct comm_point* c);
712f44e67d1SCy Schubert 
713f44e67d1SCy Schubert /**
714b7579f77SDag-Erling Smørgrav  * Get size of memory used by comm point.
715b7579f77SDag-Erling Smørgrav  * For TCP handlers this includes subhandlers.
716b7579f77SDag-Erling Smørgrav  * For UDP handlers, this does not include the (shared) UDP buffer.
717b7579f77SDag-Erling Smørgrav  * @param c: commpoint.
718b7579f77SDag-Erling Smørgrav  * @return size in bytes.
719b7579f77SDag-Erling Smørgrav  */
720b7579f77SDag-Erling Smørgrav size_t comm_point_get_mem(struct comm_point* c);
721b7579f77SDag-Erling Smørgrav 
722b7579f77SDag-Erling Smørgrav /**
723b7579f77SDag-Erling Smørgrav  * create timer. Not active upon creation.
724b7579f77SDag-Erling Smørgrav  * @param base: event handling base.
725b7579f77SDag-Erling Smørgrav  * @param cb: callback function: void myfunc(void* myarg);
726b7579f77SDag-Erling Smørgrav  * @param cb_arg: user callback argument.
727b7579f77SDag-Erling Smørgrav  * @return: the new timer or NULL on error.
728b7579f77SDag-Erling Smørgrav  */
729b7579f77SDag-Erling Smørgrav struct comm_timer* comm_timer_create(struct comm_base* base,
730b7579f77SDag-Erling Smørgrav 	void (*cb)(void*), void* cb_arg);
731b7579f77SDag-Erling Smørgrav 
732b7579f77SDag-Erling Smørgrav /**
733b7579f77SDag-Erling Smørgrav  * disable timer. Stops callbacks from happening.
734b7579f77SDag-Erling Smørgrav  * @param timer: to disable.
735b7579f77SDag-Erling Smørgrav  */
736b7579f77SDag-Erling Smørgrav void comm_timer_disable(struct comm_timer* timer);
737b7579f77SDag-Erling Smørgrav 
738b7579f77SDag-Erling Smørgrav /**
739b7579f77SDag-Erling Smørgrav  * reset timevalue for timer.
740b7579f77SDag-Erling Smørgrav  * @param timer: timer to (re)set.
741b7579f77SDag-Erling Smørgrav  * @param tv: when the timer should activate. if NULL timer is disabled.
742b7579f77SDag-Erling Smørgrav  */
743b7579f77SDag-Erling Smørgrav void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
744b7579f77SDag-Erling Smørgrav 
745b7579f77SDag-Erling Smørgrav /**
746b7579f77SDag-Erling Smørgrav  * delete timer.
747b7579f77SDag-Erling Smørgrav  * @param timer: to delete.
748b7579f77SDag-Erling Smørgrav  */
749b7579f77SDag-Erling Smørgrav void comm_timer_delete(struct comm_timer* timer);
750b7579f77SDag-Erling Smørgrav 
751b7579f77SDag-Erling Smørgrav /**
752b7579f77SDag-Erling Smørgrav  * see if timeout has been set to a value.
753b7579f77SDag-Erling Smørgrav  * @param timer: the timer to examine.
754b7579f77SDag-Erling Smørgrav  * @return: false if disabled or not set.
755b7579f77SDag-Erling Smørgrav  */
756b7579f77SDag-Erling Smørgrav int comm_timer_is_set(struct comm_timer* timer);
757b7579f77SDag-Erling Smørgrav 
758b7579f77SDag-Erling Smørgrav /**
759b7579f77SDag-Erling Smørgrav  * Get size of memory used by comm timer.
760b7579f77SDag-Erling Smørgrav  * @param timer: the timer to examine.
761b7579f77SDag-Erling Smørgrav  * @return size in bytes.
762b7579f77SDag-Erling Smørgrav  */
763b7579f77SDag-Erling Smørgrav size_t comm_timer_get_mem(struct comm_timer* timer);
764b7579f77SDag-Erling Smørgrav 
765b7579f77SDag-Erling Smørgrav /**
766b7579f77SDag-Erling Smørgrav  * Create a signal handler. Call signal_bind() later to bind to a signal.
767b7579f77SDag-Erling Smørgrav  * @param base: communication base to use.
768b7579f77SDag-Erling Smørgrav  * @param callback: called when signal is caught.
769b7579f77SDag-Erling Smørgrav  * @param cb_arg: user argument to callback
770b7579f77SDag-Erling Smørgrav  * @return: the signal struct or NULL on error.
771b7579f77SDag-Erling Smørgrav  */
772b7579f77SDag-Erling Smørgrav struct comm_signal* comm_signal_create(struct comm_base* base,
773b7579f77SDag-Erling Smørgrav 	void (*callback)(int, void*), void* cb_arg);
774b7579f77SDag-Erling Smørgrav 
775b7579f77SDag-Erling Smørgrav /**
77624e36522SCy Schubert  * Bind signal struct to catch a signal. A single comm_signal can be bound
777b7579f77SDag-Erling Smørgrav  * to multiple signals, calling comm_signal_bind multiple times.
778b7579f77SDag-Erling Smørgrav  * @param comsig: the communication point, with callback information.
779b7579f77SDag-Erling Smørgrav  * @param sig: signal number.
780b7579f77SDag-Erling Smørgrav  * @return: true on success. false on error.
781b7579f77SDag-Erling Smørgrav  */
782b7579f77SDag-Erling Smørgrav int comm_signal_bind(struct comm_signal* comsig, int sig);
783b7579f77SDag-Erling Smørgrav 
784b7579f77SDag-Erling Smørgrav /**
785b7579f77SDag-Erling Smørgrav  * Delete the signal communication point.
786b7579f77SDag-Erling Smørgrav  * @param comsig: to delete.
787b7579f77SDag-Erling Smørgrav  */
788b7579f77SDag-Erling Smørgrav void comm_signal_delete(struct comm_signal* comsig);
789b7579f77SDag-Erling Smørgrav 
790b7579f77SDag-Erling Smørgrav /**
791b7579f77SDag-Erling Smørgrav  * perform accept(2) with error checking.
792b7579f77SDag-Erling Smørgrav  * @param c: commpoint with accept fd.
793b7579f77SDag-Erling Smørgrav  * @param addr: remote end returned here.
794b7579f77SDag-Erling Smørgrav  * @param addrlen: length of remote end returned here.
795b7579f77SDag-Erling Smørgrav  * @return new fd, or -1 on error.
796b7579f77SDag-Erling Smørgrav  *	if -1, error message has been printed if necessary, simply drop
797b7579f77SDag-Erling Smørgrav  *	out of the reading handler.
798b7579f77SDag-Erling Smørgrav  */
799b7579f77SDag-Erling Smørgrav int comm_point_perform_accept(struct comm_point* c,
800b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage* addr, socklen_t* addrlen);
801b7579f77SDag-Erling Smørgrav 
802b7579f77SDag-Erling Smørgrav /**** internal routines ****/
803b7579f77SDag-Erling Smørgrav 
804b7579f77SDag-Erling Smørgrav /**
805b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
806b7579f77SDag-Erling Smørgrav  * handle libevent callback for udp comm point.
807b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
808b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
809b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
810b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
811b7579f77SDag-Erling Smørgrav  */
812b7579f77SDag-Erling Smørgrav void comm_point_udp_callback(int fd, short event, void* arg);
813b7579f77SDag-Erling Smørgrav 
814b7579f77SDag-Erling Smørgrav /**
815b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
816b7579f77SDag-Erling Smørgrav  * handle libevent callback for udp ancillary data comm point.
817b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
818b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
819b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
820b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
821b7579f77SDag-Erling Smørgrav  */
822b7579f77SDag-Erling Smørgrav void comm_point_udp_ancil_callback(int fd, short event, void* arg);
823b7579f77SDag-Erling Smørgrav 
824b7579f77SDag-Erling Smørgrav /**
825b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
826b7579f77SDag-Erling Smørgrav  * handle libevent callback for tcp accept comm point
827b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
828b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
829b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
830b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
831b7579f77SDag-Erling Smørgrav  */
832b7579f77SDag-Erling Smørgrav void comm_point_tcp_accept_callback(int fd, short event, void* arg);
833b7579f77SDag-Erling Smørgrav 
834b7579f77SDag-Erling Smørgrav /**
835b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
836b7579f77SDag-Erling Smørgrav  * handle libevent callback for tcp data comm point
837b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
838b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
839b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
840b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
841b7579f77SDag-Erling Smørgrav  */
842b7579f77SDag-Erling Smørgrav void comm_point_tcp_handle_callback(int fd, short event, void* arg);
843b7579f77SDag-Erling Smørgrav 
844b7579f77SDag-Erling Smørgrav /**
845b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
84657bddd21SDag-Erling Smørgrav  * handle libevent callback for tcp data comm point
84757bddd21SDag-Erling Smørgrav  * @param fd: file descriptor.
84857bddd21SDag-Erling Smørgrav  * @param event: event bits from libevent:
84957bddd21SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
85057bddd21SDag-Erling Smørgrav  * @param arg: the comm_point structure.
85157bddd21SDag-Erling Smørgrav  */
85257bddd21SDag-Erling Smørgrav void comm_point_http_handle_callback(int fd, short event, void* arg);
85357bddd21SDag-Erling Smørgrav 
85457bddd21SDag-Erling Smørgrav /**
855c0caa2e2SCy Schubert  * HTTP2 session.  HTTP2 related info per comm point.
856c0caa2e2SCy Schubert  */
857c0caa2e2SCy Schubert struct http2_session {
858c0caa2e2SCy Schubert 	/** first item in list of streams */
859c0caa2e2SCy Schubert 	struct http2_stream* first_stream;
860c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2
861c0caa2e2SCy Schubert 	/** nghttp2 session */
862c0caa2e2SCy Schubert 	nghttp2_session *session;
863c0caa2e2SCy Schubert 	/** store nghttp2 callbacks for easy reuse */
864c0caa2e2SCy Schubert 	nghttp2_session_callbacks* callbacks;
865c0caa2e2SCy Schubert #endif
866c0caa2e2SCy Schubert 	/** comm point containing buffer used to build answer in worker or
867c0caa2e2SCy Schubert 	 * module */
868c0caa2e2SCy Schubert 	struct comm_point* c;
869c0caa2e2SCy Schubert 	/** session is instructed to get dropped (comm port will be closed) */
870c0caa2e2SCy Schubert 	int is_drop;
871c0caa2e2SCy Schubert 	/** postpone dropping the session, can be used to prevent dropping
872c0caa2e2SCy Schubert 	 * while being in a callback */
873c0caa2e2SCy Schubert 	int postpone_drop;
874c0caa2e2SCy Schubert };
875c0caa2e2SCy Schubert 
876c0caa2e2SCy Schubert /** enum of HTTP status */
877c0caa2e2SCy Schubert enum http_status {
878c0caa2e2SCy Schubert 	HTTP_STATUS_OK = 200,
879c0caa2e2SCy Schubert 	HTTP_STATUS_BAD_REQUEST = 400,
880c0caa2e2SCy Schubert 	HTTP_STATUS_NOT_FOUND = 404,
881c0caa2e2SCy Schubert 	HTTP_STATUS_PAYLOAD_TOO_LARGE = 413,
882c0caa2e2SCy Schubert 	HTTP_STATUS_URI_TOO_LONG = 414,
883c0caa2e2SCy Schubert 	HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
884c0caa2e2SCy Schubert 	HTTP_STATUS_NOT_IMPLEMENTED = 501
885c0caa2e2SCy Schubert };
886c0caa2e2SCy Schubert 
887c0caa2e2SCy Schubert /**
888c0caa2e2SCy Schubert  * HTTP stream. Part of list of HTTP2 streams per session.
889c0caa2e2SCy Schubert  */
890c0caa2e2SCy Schubert struct http2_stream {
891c0caa2e2SCy Schubert 	/** next stream in list per session */
892c0caa2e2SCy Schubert 	struct http2_stream* next;
893c0caa2e2SCy Schubert 	/** previous stream in list per session */
894c0caa2e2SCy Schubert 	struct http2_stream* prev;
895c0caa2e2SCy Schubert 	/** HTTP2 stream ID is an unsigned 31-bit integer */
896c0caa2e2SCy Schubert 	int32_t stream_id;
897c0caa2e2SCy Schubert 	/** HTTP method used for this stream */
898c0caa2e2SCy Schubert 	enum {
899c0caa2e2SCy Schubert 		HTTP_METHOD_POST = 1,
900c0caa2e2SCy Schubert 		HTTP_METHOD_GET,
901c0caa2e2SCy Schubert 		HTTP_METHOD_UNSUPPORTED
902c0caa2e2SCy Schubert 	} http_method;
903c0caa2e2SCy Schubert 	/** message contains invalid content type */
904c0caa2e2SCy Schubert 	int invalid_content_type;
905c0caa2e2SCy Schubert 	/** message body content type */
906c0caa2e2SCy Schubert 	size_t content_length;
907c0caa2e2SCy Schubert 	/** HTTP response status */
908c0caa2e2SCy Schubert 	enum http_status status;
909c0caa2e2SCy Schubert 	/** request for non existing endpoint */
910c0caa2e2SCy Schubert 	int invalid_endpoint;
911c0caa2e2SCy Schubert 	/** query in request is too large */
912c0caa2e2SCy Schubert 	int query_too_large;
913c0caa2e2SCy Schubert 	/** buffer to store query into. Can't use session shared buffer as query
914c0caa2e2SCy Schubert 	 * can arrive in parts, intertwined with frames for other queries. */
915c0caa2e2SCy Schubert 	struct sldns_buffer* qbuffer;
916c0caa2e2SCy Schubert 	/** buffer to store response into. Can't use shared buffer as a next
917c0caa2e2SCy Schubert 	 * query read callback can overwrite it before it is send out. */
918c0caa2e2SCy Schubert 	struct sldns_buffer* rbuffer;
919c0caa2e2SCy Schubert 	/** mesh area containing mesh state */
920c0caa2e2SCy Schubert 	struct mesh_area* mesh;
921c0caa2e2SCy Schubert 	/** mesh state for query. Used to remove mesh reply before closing
922c0caa2e2SCy Schubert 	 * stream. */
923c0caa2e2SCy Schubert 	struct mesh_state* mesh_state;
924c0caa2e2SCy Schubert };
925c0caa2e2SCy Schubert 
926c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2
927c0caa2e2SCy Schubert /** nghttp2 receive cb. Read from SSL connection into nghttp2 buffer */
928c0caa2e2SCy Schubert ssize_t http2_recv_cb(nghttp2_session* session, uint8_t* buf,
929c0caa2e2SCy Schubert 	size_t len, int flags, void* cb_arg);
930c0caa2e2SCy Schubert /** nghttp2 send callback. Send from nghttp2 buffer to ssl socket */
931c0caa2e2SCy Schubert ssize_t http2_send_cb(nghttp2_session* session, const uint8_t* buf,
932c0caa2e2SCy Schubert 	size_t len, int flags, void* cb_arg);
933c0caa2e2SCy Schubert /** nghttp2 callback on closing stream */
934c0caa2e2SCy Schubert int http2_stream_close_cb(nghttp2_session* session, int32_t stream_id,
935c0caa2e2SCy Schubert 	uint32_t error_code, void* cb_arg);
936c0caa2e2SCy Schubert #endif
937c0caa2e2SCy Schubert 
938c0caa2e2SCy Schubert /**
939c0caa2e2SCy Schubert  * Create new http2 stream
940c0caa2e2SCy Schubert  * @param stream_id: ID for stream to create.
941c0caa2e2SCy Schubert  * @return malloc'ed stream, NULL on error
942c0caa2e2SCy Schubert  */
943c0caa2e2SCy Schubert struct http2_stream* http2_stream_create(int32_t stream_id);
944c0caa2e2SCy Schubert 
945c0caa2e2SCy Schubert /**
946c0caa2e2SCy Schubert  * Add new stream to session linked list
947c0caa2e2SCy Schubert  * @param h2_session: http2 session to add stream to
948c0caa2e2SCy Schubert  * @param h2_stream: stream to add to session list
949c0caa2e2SCy Schubert  */
950c0caa2e2SCy Schubert void http2_session_add_stream(struct http2_session* h2_session,
951c0caa2e2SCy Schubert 	struct http2_stream* h2_stream);
952c0caa2e2SCy Schubert 
953c0caa2e2SCy Schubert /** Add mesh state to stream. To be able to remove mesh reply on stream closure
954c0caa2e2SCy Schubert  */
955c0caa2e2SCy Schubert void http2_stream_add_meshstate(struct http2_stream* h2_stream,
956c0caa2e2SCy Schubert 	struct mesh_area* mesh, struct mesh_state* m);
957c0caa2e2SCy Schubert 
958c0caa2e2SCy Schubert /**
95957bddd21SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
960b7579f77SDag-Erling Smørgrav  * handle libevent callback for timer comm.
961b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor (always -1).
962b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
963b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
964b7579f77SDag-Erling Smørgrav  * @param arg: the comm_timer structure.
965b7579f77SDag-Erling Smørgrav  */
966b7579f77SDag-Erling Smørgrav void comm_timer_callback(int fd, short event, void* arg);
967b7579f77SDag-Erling Smørgrav 
968b7579f77SDag-Erling Smørgrav /**
969b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
970b7579f77SDag-Erling Smørgrav  * handle libevent callback for signal comm.
971b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor (used for the signal number).
972b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
973b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
974b7579f77SDag-Erling Smørgrav  * @param arg: the internal commsignal structure.
975b7579f77SDag-Erling Smørgrav  */
976b7579f77SDag-Erling Smørgrav void comm_signal_callback(int fd, short event, void* arg);
977b7579f77SDag-Erling Smørgrav 
978b7579f77SDag-Erling Smørgrav /**
979b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
980b7579f77SDag-Erling Smørgrav  * libevent callback for AF_UNIX fds
981b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
982b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
983b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
984b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
985b7579f77SDag-Erling Smørgrav  */
986b7579f77SDag-Erling Smørgrav void comm_point_local_handle_callback(int fd, short event, void* arg);
987b7579f77SDag-Erling Smørgrav 
988b7579f77SDag-Erling Smørgrav /**
989b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
990b7579f77SDag-Erling Smørgrav  * libevent callback for raw fd access.
991b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
992b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
993b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
994b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
995b7579f77SDag-Erling Smørgrav  */
996b7579f77SDag-Erling Smørgrav void comm_point_raw_handle_callback(int fd, short event, void* arg);
997b7579f77SDag-Erling Smørgrav 
998b7579f77SDag-Erling Smørgrav /**
999b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
1000b7579f77SDag-Erling Smørgrav  * libevent callback for timeout on slow accept.
1001b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
1002b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
1003b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
1004b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
1005b7579f77SDag-Erling Smørgrav  */
1006b7579f77SDag-Erling Smørgrav void comm_base_handle_slow_accept(int fd, short event, void* arg);
1007b7579f77SDag-Erling Smørgrav 
1008b7579f77SDag-Erling Smørgrav #ifdef USE_WINSOCK
1009b7579f77SDag-Erling Smørgrav /**
1010b7579f77SDag-Erling Smørgrav  * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
1011b7579f77SDag-Erling Smørgrav  * the winsock_event of this for proper TCP nonblocking implementation.
1012b7579f77SDag-Erling Smørgrav  * @param c: comm_point, fd must be set its struct event is registered.
1013b7579f77SDag-Erling Smørgrav  * @param ssl: openssl SSL, fd must be set so it has a bio.
1014b7579f77SDag-Erling Smørgrav  */
1015b7579f77SDag-Erling Smørgrav void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
1016b7579f77SDag-Erling Smørgrav #endif
1017b7579f77SDag-Erling Smørgrav 
101825039b37SCy Schubert /**
101925039b37SCy Schubert  * See if errno for tcp connect has to be logged or not. This uses errno
102025039b37SCy Schubert  * @param addr: apart from checking errno, the addr is checked for ip4mapped
102125039b37SCy Schubert  * 	and broadcast type, hence passed.
102225039b37SCy Schubert  * @param addrlen: length of the addr parameter.
102325039b37SCy Schubert  * @return true if it needs to be logged.
102425039b37SCy Schubert  */
1025b7579f77SDag-Erling Smørgrav int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
1026b7579f77SDag-Erling Smørgrav 
102725039b37SCy Schubert #ifdef HAVE_SSL
102825039b37SCy Schubert /**
102925039b37SCy Schubert  * True if the ssl handshake error has to be squelched from the logs
103025039b37SCy Schubert  * @param err: the error returned by the openssl routine, ERR_get_error.
103125039b37SCy Schubert  * 	This is a packed structure with elements that are examined.
103225039b37SCy Schubert  * @return true if the error is squelched (not logged).
103325039b37SCy Schubert  */
103425039b37SCy Schubert int squelch_err_ssl_handshake(unsigned long err);
103525039b37SCy Schubert #endif
103625039b37SCy Schubert 
1037b7579f77SDag-Erling Smørgrav #endif /* NET_EVENT_H */
1038