xref: /freebsd/contrib/unbound/util/netevent.h (revision 5469a9953005a9a4d4aad7be88545d441622e9a0)
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 
6365b390aaSDag-Erling Smørgrav #include "dnscrypt/dnscrypt.h"
64c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2_NGHTTP2_H
65c0caa2e2SCy Schubert #include <nghttp2/nghttp2.h>
66c0caa2e2SCy Schubert #endif
6765b390aaSDag-Erling Smørgrav 
6817d15b25SDag-Erling Smørgrav struct sldns_buffer;
69b7579f77SDag-Erling Smørgrav struct comm_point;
70b7579f77SDag-Erling Smørgrav struct comm_reply;
714c75e3aaSDag-Erling Smørgrav struct tcl_list;
72e2d15004SDag-Erling Smørgrav struct ub_event_base;
73*5469a995SCy Schubert struct unbound_socket;
74b7579f77SDag-Erling Smørgrav 
75c0caa2e2SCy Schubert struct mesh_state;
76c0caa2e2SCy Schubert struct mesh_area;
77c0caa2e2SCy Schubert 
78b7579f77SDag-Erling Smørgrav /* internal event notification data storage structure. */
79b7579f77SDag-Erling Smørgrav struct internal_event;
80b7579f77SDag-Erling Smørgrav struct internal_base;
81e2d15004SDag-Erling Smørgrav struct internal_timer; /* A sub struct of the comm_timer super struct */
82b7579f77SDag-Erling Smørgrav 
83c0caa2e2SCy Schubert enum listen_type;
84c0caa2e2SCy Schubert 
85b7579f77SDag-Erling Smørgrav /** callback from communication point function type */
863005e0a3SDag-Erling Smørgrav typedef int comm_point_callback_type(struct comm_point*, void*, int,
87b7579f77SDag-Erling Smørgrav 	struct comm_reply*);
88b7579f77SDag-Erling Smørgrav 
89b7579f77SDag-Erling Smørgrav /** to pass no_error to callback function */
90b7579f77SDag-Erling Smørgrav #define NETEVENT_NOERROR 0
91b7579f77SDag-Erling Smørgrav /** to pass closed connection to callback function */
92b7579f77SDag-Erling Smørgrav #define NETEVENT_CLOSED -1
93b7579f77SDag-Erling Smørgrav /** to pass timeout happened to callback function */
94b7579f77SDag-Erling Smørgrav #define NETEVENT_TIMEOUT -2
95b7579f77SDag-Erling Smørgrav /** to pass fallback from capsforID to callback function; 0x20 failed */
96b7579f77SDag-Erling Smørgrav #define NETEVENT_CAPSFAIL -3
9757bddd21SDag-Erling Smørgrav /** to pass done transfer to callback function; http file is complete */
9857bddd21SDag-Erling Smørgrav #define NETEVENT_DONE -4
99369c6923SCy Schubert /** to pass write of the write packet is done to callback function
100369c6923SCy Schubert  * used when tcp_write_and_read is enabled */
101369c6923SCy Schubert #define NETEVENT_PKT_WRITTEN -5
102b7579f77SDag-Erling Smørgrav 
103b7579f77SDag-Erling Smørgrav /** timeout to slow accept calls when not possible, in msec. */
104b7579f77SDag-Erling Smørgrav #define NETEVENT_SLOW_ACCEPT_TIME 2000
105b7579f77SDag-Erling Smørgrav 
106b7579f77SDag-Erling Smørgrav /**
107b7579f77SDag-Erling Smørgrav  * A communication point dispatcher. Thread specific.
108b7579f77SDag-Erling Smørgrav  */
109b7579f77SDag-Erling Smørgrav struct comm_base {
110b7579f77SDag-Erling Smørgrav 	/** behind the scenes structure. with say libevent info. alloced */
111b7579f77SDag-Erling Smørgrav 	struct internal_base* eb;
112b7579f77SDag-Erling Smørgrav 	/** callback to stop listening on accept sockets,
113b7579f77SDag-Erling Smørgrav 	 * performed when accept() will not function properly */
114b7579f77SDag-Erling Smørgrav 	void (*stop_accept)(void*);
115b7579f77SDag-Erling Smørgrav 	/** callback to start listening on accept sockets, performed
116b7579f77SDag-Erling Smørgrav 	 * after stop_accept() then a timeout has passed. */
117b7579f77SDag-Erling Smørgrav 	void (*start_accept)(void*);
118b7579f77SDag-Erling Smørgrav 	/** user argument for stop_accept and start_accept functions */
119b7579f77SDag-Erling Smørgrav 	void* cb_arg;
120b7579f77SDag-Erling Smørgrav };
121b7579f77SDag-Erling Smørgrav 
122b7579f77SDag-Erling Smørgrav /**
123b7579f77SDag-Erling Smørgrav  * Reply information for a communication point.
124b7579f77SDag-Erling Smørgrav  */
125b7579f77SDag-Erling Smørgrav struct comm_reply {
126b7579f77SDag-Erling Smørgrav 	/** the comm_point with fd to send reply on to. */
127b7579f77SDag-Erling Smørgrav 	struct comm_point* c;
128b7579f77SDag-Erling Smørgrav 	/** the address (for UDP based communication) */
129b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage addr;
130b7579f77SDag-Erling Smørgrav 	/** length of address */
131b7579f77SDag-Erling Smørgrav 	socklen_t addrlen;
132b7579f77SDag-Erling Smørgrav 	/** return type 0 (none), 4(IP4), 6(IP6) */
133b7579f77SDag-Erling Smørgrav 	int srctype;
13465b390aaSDag-Erling Smørgrav 	/* DnsCrypt context */
13565b390aaSDag-Erling Smørgrav #ifdef USE_DNSCRYPT
13665b390aaSDag-Erling Smørgrav 	uint8_t client_nonce[crypto_box_HALF_NONCEBYTES];
13765b390aaSDag-Erling Smørgrav 	uint8_t nmkey[crypto_box_BEFORENMBYTES];
138c7f4d7adSDag-Erling Smørgrav 	const dnsccert *dnsc_cert;
13965b390aaSDag-Erling Smørgrav 	int is_dnscrypted;
14065b390aaSDag-Erling Smørgrav #endif
141b7579f77SDag-Erling Smørgrav 	/** the return source interface data */
142b7579f77SDag-Erling Smørgrav 	union {
143b7579f77SDag-Erling Smørgrav #ifdef IPV6_PKTINFO
144b7579f77SDag-Erling Smørgrav 		struct in6_pktinfo v6info;
145b7579f77SDag-Erling Smørgrav #endif
146b7579f77SDag-Erling Smørgrav #ifdef IP_PKTINFO
147b7579f77SDag-Erling Smørgrav 		struct in_pktinfo v4info;
148b7579f77SDag-Erling Smørgrav #elif defined(IP_RECVDSTADDR)
149b7579f77SDag-Erling Smørgrav 		struct in_addr v4addr;
150b7579f77SDag-Erling Smørgrav #endif
151b7579f77SDag-Erling Smørgrav 	}
152b7579f77SDag-Erling Smørgrav 		/** variable with return source data */
153b7579f77SDag-Erling Smørgrav 		pktinfo;
15465b390aaSDag-Erling Smørgrav 	/** max udp size for udp packets */
15565b390aaSDag-Erling Smørgrav 	size_t max_udp_size;
156b7579f77SDag-Erling Smørgrav };
157b7579f77SDag-Erling Smørgrav 
158b7579f77SDag-Erling Smørgrav /**
159b7579f77SDag-Erling Smørgrav  * Communication point to the network
160b7579f77SDag-Erling Smørgrav  * These behaviours can be accomplished by setting the flags
161b7579f77SDag-Erling Smørgrav  * and passing return values from the callback.
162b7579f77SDag-Erling Smørgrav  *    udp frontside: called after readdone. sendafter.
163b7579f77SDag-Erling Smørgrav  *    tcp frontside: called readdone, sendafter. close.
164b7579f77SDag-Erling Smørgrav  *    udp behind: called after readdone. No send after.
165b7579f77SDag-Erling Smørgrav  *    tcp behind: write done, read done, then called. No send after.
166b7579f77SDag-Erling Smørgrav  */
167b7579f77SDag-Erling Smørgrav struct comm_point {
168b7579f77SDag-Erling Smørgrav 	/** behind the scenes structure, with say libevent info. alloced. */
169b7579f77SDag-Erling Smørgrav 	struct internal_event* ev;
170f44e67d1SCy Schubert 	/** if the event is added or not */
171f44e67d1SCy Schubert 	int event_added;
172b7579f77SDag-Erling Smørgrav 
173*5469a995SCy Schubert 	struct unbound_socket* socket;
174*5469a995SCy Schubert 
175b7579f77SDag-Erling Smørgrav 	/** file descriptor for communication point */
176b7579f77SDag-Erling Smørgrav 	int fd;
177b7579f77SDag-Erling Smørgrav 
178b7579f77SDag-Erling Smørgrav 	/** timeout (NULL if it does not). Malloced. */
179b7579f77SDag-Erling Smørgrav 	struct timeval* timeout;
180b7579f77SDag-Erling Smørgrav 
181b7579f77SDag-Erling Smørgrav 	/** buffer pointer. Either to perthread, or own buffer or NULL */
18217d15b25SDag-Erling Smørgrav 	struct sldns_buffer* buffer;
183b7579f77SDag-Erling Smørgrav 
184b7579f77SDag-Erling Smørgrav 	/* -------- TCP Handler -------- */
185b7579f77SDag-Erling Smørgrav 	/** Read/Write state for TCP */
186b7579f77SDag-Erling Smørgrav 	int tcp_is_reading;
187b7579f77SDag-Erling Smørgrav 	/** The current read/write count for TCP */
188b7579f77SDag-Erling Smørgrav 	size_t tcp_byte_count;
189b7579f77SDag-Erling Smørgrav 	/** parent communication point (for TCP sockets) */
190b7579f77SDag-Erling Smørgrav 	struct comm_point* tcp_parent;
191b7579f77SDag-Erling Smørgrav 	/** sockaddr from peer, for TCP handlers */
192b7579f77SDag-Erling Smørgrav 	struct comm_reply repinfo;
193b7579f77SDag-Erling Smørgrav 
194b7579f77SDag-Erling Smørgrav 	/* -------- TCP Accept -------- */
195b7579f77SDag-Erling Smørgrav 	/** the number of TCP handlers for this tcp-accept socket */
196b7579f77SDag-Erling Smørgrav 	int max_tcp_count;
19709a3aaf3SDag-Erling Smørgrav 	/** current number of tcp handler in-use for this accept socket */
19809a3aaf3SDag-Erling Smørgrav 	int cur_tcp_count;
199b7579f77SDag-Erling Smørgrav 	/** malloced array of tcp handlers for a tcp-accept,
200b7579f77SDag-Erling Smørgrav 	    of size max_tcp_count. */
201b7579f77SDag-Erling Smørgrav 	struct comm_point** tcp_handlers;
202b7579f77SDag-Erling Smørgrav 	/** linked list of free tcp_handlers to use for new queries.
203b7579f77SDag-Erling Smørgrav 	    For tcp_accept the first entry, for tcp_handlers the next one. */
204b7579f77SDag-Erling Smørgrav 	struct comm_point* tcp_free;
205b7579f77SDag-Erling Smørgrav 
206b7579f77SDag-Erling Smørgrav 	/* -------- SSL TCP DNS ------- */
207b7579f77SDag-Erling Smørgrav 	/** the SSL object with rw bio (owned) or for commaccept ctx ref */
208b7579f77SDag-Erling Smørgrav 	void* ssl;
209b7579f77SDag-Erling Smørgrav 	/** handshake state for init and renegotiate */
210b7579f77SDag-Erling Smørgrav 	enum {
211b7579f77SDag-Erling Smørgrav 		/** no handshake, it has been done */
212b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_none = 0,
213b7579f77SDag-Erling Smørgrav 		/** ssl initial handshake wants to read */
214b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_read,
215b7579f77SDag-Erling Smørgrav 		/** ssl initial handshake wants to write */
216b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_write,
217b7579f77SDag-Erling Smørgrav 		/** ssl_write wants to read */
218b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_hs_read,
219b7579f77SDag-Erling Smørgrav 		/** ssl_read wants to write */
220b7579f77SDag-Erling Smørgrav 		comm_ssl_shake_hs_write
221b7579f77SDag-Erling Smørgrav 	} ssl_shake_state;
222b7579f77SDag-Erling Smørgrav 
22357bddd21SDag-Erling Smørgrav 	/* -------- HTTP ------- */
224c0caa2e2SCy Schubert 	/** Do not allow connection to use HTTP version lower than this. 0=no
225c0caa2e2SCy Schubert 	 * minimum. */
226c0caa2e2SCy Schubert 	enum {
227c0caa2e2SCy Schubert 		http_version_none = 0,
228c0caa2e2SCy Schubert 		http_version_2 = 2
229c0caa2e2SCy Schubert 	} http_min_version;
230c0caa2e2SCy Schubert 	/** http endpoint */
231c0caa2e2SCy Schubert 	char* http_endpoint;
232c0caa2e2SCy Schubert 	/* -------- HTTP/1.1 ------- */
23357bddd21SDag-Erling Smørgrav 	/** Currently reading in http headers */
23457bddd21SDag-Erling Smørgrav 	int http_in_headers;
23557bddd21SDag-Erling Smørgrav 	/** Currently reading in chunk headers, 0=not, 1=firstline, 2=unused
23657bddd21SDag-Erling Smørgrav 	 * (more lines), 3=trailer headers after chunk */
23757bddd21SDag-Erling Smørgrav 	int http_in_chunk_headers;
23857bddd21SDag-Erling Smørgrav 	/** chunked transfer */
23957bddd21SDag-Erling Smørgrav 	int http_is_chunked;
24057bddd21SDag-Erling Smørgrav 	/** http temp buffer (shared buffer for temporary work) */
24157bddd21SDag-Erling Smørgrav 	struct sldns_buffer* http_temp;
24257bddd21SDag-Erling Smørgrav 	/** http stored content in buffer */
24357bddd21SDag-Erling Smørgrav 	size_t http_stored;
244c0caa2e2SCy Schubert 	/* -------- HTTP/2 ------- */
245c0caa2e2SCy Schubert 	/** http2 session */
246c0caa2e2SCy Schubert 	struct http2_session* h2_session;
247c0caa2e2SCy Schubert 	/** set to 1 if h2 is negotiated to be used (using alpn) */
248c0caa2e2SCy Schubert 	int use_h2;
249c0caa2e2SCy Schubert 	/** stream currently being handled */
250c0caa2e2SCy Schubert 	struct http2_stream* h2_stream;
251c0caa2e2SCy Schubert 	/** maximum allowed query buffer size, per stream */
252c0caa2e2SCy Schubert 	size_t http2_stream_max_qbuffer_size;
253c0caa2e2SCy Schubert 	/** maximum number of HTTP/2 streams per connection. Send in HTTP/2
254c0caa2e2SCy Schubert 	 * SETTINGS frame. */
255c0caa2e2SCy Schubert 	uint32_t http2_max_streams;
25657bddd21SDag-Erling Smørgrav 
257ff825849SDag-Erling Smørgrav 	/* -------- dnstap ------- */
258ff825849SDag-Erling Smørgrav 	/** the dnstap environment */
259ff825849SDag-Erling Smørgrav 	struct dt_env* dtenv;
260ff825849SDag-Erling Smørgrav 
261b7579f77SDag-Erling Smørgrav 	/** is this a UDP, TCP-accept or TCP socket. */
262b7579f77SDag-Erling Smørgrav 	enum comm_point_type {
263b7579f77SDag-Erling Smørgrav 		/** UDP socket - handle datagrams. */
264b7579f77SDag-Erling Smørgrav 		comm_udp,
265b7579f77SDag-Erling Smørgrav 		/** TCP accept socket - only creates handlers if readable. */
266b7579f77SDag-Erling Smørgrav 		comm_tcp_accept,
267b7579f77SDag-Erling Smørgrav 		/** TCP handler socket - handle byteperbyte readwrite. */
268b7579f77SDag-Erling Smørgrav 		comm_tcp,
26957bddd21SDag-Erling Smørgrav 		/** HTTP handler socket */
27057bddd21SDag-Erling Smørgrav 		comm_http,
271b7579f77SDag-Erling Smørgrav 		/** AF_UNIX socket - for internal commands. */
272b7579f77SDag-Erling Smørgrav 		comm_local,
273b7579f77SDag-Erling Smørgrav 		/** raw - not DNS format - for pipe readers and writers */
274b7579f77SDag-Erling Smørgrav 		comm_raw
275b7579f77SDag-Erling Smørgrav 	}
276b7579f77SDag-Erling Smørgrav 		/** variable with type of socket, UDP,TCP-accept,TCP,pipe */
277b7579f77SDag-Erling Smørgrav 		type;
278b7579f77SDag-Erling Smørgrav 
279b7579f77SDag-Erling Smørgrav 	/* ---------- Behaviour ----------- */
280b7579f77SDag-Erling Smørgrav 	/** if set the connection is NOT closed on delete. */
281b7579f77SDag-Erling Smørgrav 	int do_not_close;
282b7579f77SDag-Erling Smørgrav 
283b7579f77SDag-Erling Smørgrav 	/** if set, the connection is closed on error, on timeout,
284b7579f77SDag-Erling Smørgrav 	    and after read/write completes. No callback is done. */
285b7579f77SDag-Erling Smørgrav 	int tcp_do_close;
286b7579f77SDag-Erling Smørgrav 
287369c6923SCy Schubert 	/** flag that indicates the stream is both written and read from. */
288369c6923SCy Schubert 	int tcp_write_and_read;
289369c6923SCy Schubert 
290369c6923SCy Schubert 	/** byte count for written length over write channel, for when
291369c6923SCy Schubert 	 * tcp_write_and_read is enabled.  When tcp_write_and_read is enabled,
292369c6923SCy Schubert 	 * this is the counter for writing, the one for reading is in the
293369c6923SCy Schubert 	 * commpoint.buffer sldns buffer.  The counter counts from 0 to
294369c6923SCy Schubert 	 * 2+tcp_write_pkt_len, and includes the tcp length bytes. */
295369c6923SCy Schubert 	size_t tcp_write_byte_count;
296369c6923SCy Schubert 
297369c6923SCy Schubert 	/** packet to write currently over the write channel. for when
298369c6923SCy Schubert 	 * tcp_write_and_read is enabled.  When tcp_write_and_read is enabled,
299369c6923SCy Schubert 	 * this is the buffer for the written packet, the commpoint.buffer
300369c6923SCy Schubert 	 * sldns buffer is the buffer for the received packet. */
301369c6923SCy Schubert 	uint8_t* tcp_write_pkt;
302369c6923SCy Schubert 	/** length of tcp_write_pkt in bytes */
303369c6923SCy Schubert 	size_t tcp_write_pkt_len;
304369c6923SCy Schubert 
305369c6923SCy Schubert 	/** if set try to read another packet again (over connection with
306369c6923SCy Schubert 	 * multiple packets), once set, tries once, then zero again,
307369c6923SCy Schubert 	 * so set it in the packet complete section.
308369c6923SCy Schubert 	 * The pointer itself has to be set before the callback is invoked,
309369c6923SCy Schubert 	 * when you set things up, and continue to exist also after the
310369c6923SCy Schubert 	 * commpoint is closed and deleted in your callback.  So that after
311369c6923SCy Schubert 	 * the callback cleans up netevent can see what it has to do.
312369c6923SCy Schubert 	 * Or leave NULL if it is not used at all. */
313369c6923SCy Schubert 	int* tcp_more_read_again;
314369c6923SCy Schubert 
315369c6923SCy Schubert 	/** if set try to write another packet (over connection with
316369c6923SCy Schubert 	 * multiple packets), once set, tries once, then zero again,
317369c6923SCy Schubert 	 * so set it in the packet complete section.
318369c6923SCy Schubert 	 * The pointer itself has to be set before the callback is invoked,
319369c6923SCy Schubert 	 * when you set things up, and continue to exist also after the
320369c6923SCy Schubert 	 * commpoint is closed and deleted in your callback.  So that after
321369c6923SCy Schubert 	 * the callback cleans up netevent can see what it has to do.
322369c6923SCy Schubert 	 * Or leave NULL if it is not used at all. */
323369c6923SCy Schubert 	int* tcp_more_write_again;
324369c6923SCy Schubert 
325b7579f77SDag-Erling Smørgrav 	/** if set, read/write completes:
326b7579f77SDag-Erling Smørgrav 		read/write state of tcp is toggled.
327b7579f77SDag-Erling Smørgrav 		buffer reset/bytecount reset.
328b7579f77SDag-Erling Smørgrav 		this flag cleared.
329b7579f77SDag-Erling Smørgrav 	    So that when that is done the callback is called. */
330b7579f77SDag-Erling Smørgrav 	int tcp_do_toggle_rw;
331b7579f77SDag-Erling Smørgrav 
332b5663de9SDag-Erling Smørgrav 	/** timeout in msec for TCP wait times for this connection */
333b5663de9SDag-Erling Smørgrav 	int tcp_timeout_msec;
334b5663de9SDag-Erling Smørgrav 
3354c75e3aaSDag-Erling Smørgrav 	/** if set, tcp keepalive is enabled on this connection */
3364c75e3aaSDag-Erling Smørgrav 	int tcp_keepalive;
3374c75e3aaSDag-Erling Smørgrav 
338b7579f77SDag-Erling Smørgrav 	/** if set, checks for pending error from nonblocking connect() call.*/
339b7579f77SDag-Erling Smørgrav 	int tcp_check_nb_connect;
340b7579f77SDag-Erling Smørgrav 
3414c75e3aaSDag-Erling Smørgrav 	/** if set, check for connection limit on tcp accept. */
3424c75e3aaSDag-Erling Smørgrav 	struct tcl_list* tcp_conn_limit;
3434c75e3aaSDag-Erling Smørgrav 	/** the entry for the connection. */
3444c75e3aaSDag-Erling Smørgrav 	struct tcl_addr* tcl_addr;
3454c75e3aaSDag-Erling Smørgrav 
346e86b9096SDag-Erling Smørgrav 	/** the structure to keep track of open requests on this channel */
347e86b9096SDag-Erling Smørgrav 	struct tcp_req_info* tcp_req_info;
348e86b9096SDag-Erling Smørgrav 
349b5663de9SDag-Erling Smørgrav #ifdef USE_MSG_FASTOPEN
350b5663de9SDag-Erling Smørgrav 	/** used to track if the sendto() call should be done when using TFO. */
351b5663de9SDag-Erling Smørgrav 	int tcp_do_fastopen;
352b5663de9SDag-Erling Smørgrav #endif
353b5663de9SDag-Erling Smørgrav 
35465b390aaSDag-Erling Smørgrav #ifdef USE_DNSCRYPT
35565b390aaSDag-Erling Smørgrav 	/** Is this a dnscrypt channel */
35665b390aaSDag-Erling Smørgrav 	int dnscrypt;
35765b390aaSDag-Erling Smørgrav 	/** encrypted buffer pointer. Either to perthread, or own buffer or NULL */
35865b390aaSDag-Erling Smørgrav 	struct sldns_buffer* dnscrypt_buffer;
35965b390aaSDag-Erling Smørgrav #endif
360b7579f77SDag-Erling Smørgrav 	/** number of queries outstanding on this socket, used by
361b7579f77SDag-Erling Smørgrav 	 * outside network for udp ports */
362b7579f77SDag-Erling Smørgrav 	int inuse;
363b7579f77SDag-Erling Smørgrav 
364b7579f77SDag-Erling Smørgrav 	/** callback when done.
365b7579f77SDag-Erling Smørgrav 	    tcp_accept does not get called back, is NULL then.
366b7579f77SDag-Erling Smørgrav 	    If a timeout happens, callback with timeout=1 is called.
367b7579f77SDag-Erling Smørgrav 	    If an error happens, callback is called with error set
368b7579f77SDag-Erling Smørgrav 	    nonzero. If not NETEVENT_NOERROR, it is an errno value.
369b7579f77SDag-Erling Smørgrav 	    If the connection is closed (by remote end) then the
370b7579f77SDag-Erling Smørgrav 	    callback is called with error set to NETEVENT_CLOSED=-1.
371b7579f77SDag-Erling Smørgrav 	    If a timeout happens on the connection, the error is set to
372b7579f77SDag-Erling Smørgrav 	    NETEVENT_TIMEOUT=-2.
373b7579f77SDag-Erling Smørgrav 	    The reply_info can be copied if the reply needs to happen at a
374b7579f77SDag-Erling Smørgrav 	    later time. It consists of a struct with commpoint and address.
375b7579f77SDag-Erling Smørgrav 	    It can be passed to a msg send routine some time later.
376b7579f77SDag-Erling Smørgrav 	    Note the reply information is temporary and must be copied.
377b7579f77SDag-Erling Smørgrav 	    NULL is passed for_reply info, in cases where error happened.
378b7579f77SDag-Erling Smørgrav 
379b7579f77SDag-Erling Smørgrav 	    declare as:
380b7579f77SDag-Erling Smørgrav 	    int my_callback(struct comm_point* c, void* my_arg, int error,
381b7579f77SDag-Erling Smørgrav 		struct comm_reply *reply_info);
382b7579f77SDag-Erling Smørgrav 
383b7579f77SDag-Erling Smørgrav 	    if the routine returns 0, nothing is done.
384b7579f77SDag-Erling Smørgrav 	    Notzero, the buffer will be sent back to client.
385b7579f77SDag-Erling Smørgrav 	    		For UDP this is done without changing the commpoint.
386b7579f77SDag-Erling Smørgrav 			In TCP it sets write state.
387b7579f77SDag-Erling Smørgrav 	*/
3883005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback;
389b7579f77SDag-Erling Smørgrav 	/** argument to pass to callback. */
390b7579f77SDag-Erling Smørgrav 	void *cb_arg;
391b7579f77SDag-Erling Smørgrav };
392b7579f77SDag-Erling Smørgrav 
393b7579f77SDag-Erling Smørgrav /**
394b7579f77SDag-Erling Smørgrav  * Structure only for making timeout events.
395b7579f77SDag-Erling Smørgrav  */
396b7579f77SDag-Erling Smørgrav struct comm_timer {
397e2d15004SDag-Erling Smørgrav 	/** the internal event stuff (derived) */
398b7579f77SDag-Erling Smørgrav 	struct internal_timer* ev_timer;
399b7579f77SDag-Erling Smørgrav 
400b7579f77SDag-Erling Smørgrav 	/** callback function, takes user arg only */
401b7579f77SDag-Erling Smørgrav 	void (*callback)(void*);
402b7579f77SDag-Erling Smørgrav 
403b7579f77SDag-Erling Smørgrav 	/** callback user argument */
404b7579f77SDag-Erling Smørgrav 	void* cb_arg;
405b7579f77SDag-Erling Smørgrav };
406b7579f77SDag-Erling Smørgrav 
407b7579f77SDag-Erling Smørgrav /**
408b7579f77SDag-Erling Smørgrav  * Structure only for signal events.
409b7579f77SDag-Erling Smørgrav  */
410b7579f77SDag-Erling Smørgrav struct comm_signal {
411b7579f77SDag-Erling Smørgrav 	/** the communication base */
412b7579f77SDag-Erling Smørgrav 	struct comm_base* base;
413b7579f77SDag-Erling Smørgrav 
414b7579f77SDag-Erling Smørgrav 	/** the internal event stuff */
415b7579f77SDag-Erling Smørgrav 	struct internal_signal* ev_signal;
416b7579f77SDag-Erling Smørgrav 
417b7579f77SDag-Erling Smørgrav 	/** callback function, takes signal number and user arg */
418b7579f77SDag-Erling Smørgrav 	void (*callback)(int, void*);
419b7579f77SDag-Erling Smørgrav 
420b7579f77SDag-Erling Smørgrav 	/** callback user argument */
421b7579f77SDag-Erling Smørgrav 	void* cb_arg;
422b7579f77SDag-Erling Smørgrav };
423b7579f77SDag-Erling Smørgrav 
424b7579f77SDag-Erling Smørgrav /**
425b7579f77SDag-Erling Smørgrav  * Create a new comm base.
426b7579f77SDag-Erling Smørgrav  * @param sigs: if true it attempts to create a default loop for
427b7579f77SDag-Erling Smørgrav  *   signal handling.
428b7579f77SDag-Erling Smørgrav  * @return: the new comm base. NULL on error.
429b7579f77SDag-Erling Smørgrav  */
430b7579f77SDag-Erling Smørgrav struct comm_base* comm_base_create(int sigs);
431b7579f77SDag-Erling Smørgrav 
432b7579f77SDag-Erling Smørgrav /**
433e2d15004SDag-Erling Smørgrav  * Create comm base that uses the given ub_event_base (underlying pluggable
434e2d15004SDag-Erling Smørgrav  * event mechanism pointer).
435e2d15004SDag-Erling Smørgrav  * @param base: underlying pluggable event base.
43617d15b25SDag-Erling Smørgrav  * @return: the new comm base. NULL on error.
43717d15b25SDag-Erling Smørgrav  */
438e2d15004SDag-Erling Smørgrav struct comm_base* comm_base_create_event(struct ub_event_base* base);
43917d15b25SDag-Erling Smørgrav 
44017d15b25SDag-Erling Smørgrav /**
44117d15b25SDag-Erling Smørgrav  * Delete comm base structure but not the underlying lib event base.
44217d15b25SDag-Erling Smørgrav  * All comm points must have been deleted.
44317d15b25SDag-Erling Smørgrav  * @param b: the base to delete.
44417d15b25SDag-Erling Smørgrav  */
44517d15b25SDag-Erling Smørgrav void comm_base_delete_no_base(struct comm_base* b);
44617d15b25SDag-Erling Smørgrav 
44717d15b25SDag-Erling Smørgrav /**
448b7579f77SDag-Erling Smørgrav  * Destroy a comm base.
449b7579f77SDag-Erling Smørgrav  * All comm points must have been deleted.
450b7579f77SDag-Erling Smørgrav  * @param b: the base to delete.
451b7579f77SDag-Erling Smørgrav  */
452b7579f77SDag-Erling Smørgrav void comm_base_delete(struct comm_base* b);
453b7579f77SDag-Erling Smørgrav 
454b7579f77SDag-Erling Smørgrav /**
455b7579f77SDag-Erling Smørgrav  * Obtain two pointers. The pointers never change (until base_delete()).
456b7579f77SDag-Erling Smørgrav  * The pointers point to time values that are updated regularly.
457b7579f77SDag-Erling Smørgrav  * @param b: the communication base that will update the time values.
458b7579f77SDag-Erling Smørgrav  * @param tt: pointer to time in seconds is returned.
459b7579f77SDag-Erling Smørgrav  * @param tv: pointer to time in microseconds is returned.
460b7579f77SDag-Erling Smørgrav  */
46117d15b25SDag-Erling Smørgrav void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
462b7579f77SDag-Erling Smørgrav 
463b7579f77SDag-Erling Smørgrav /**
464b7579f77SDag-Erling Smørgrav  * Dispatch the comm base events.
465b7579f77SDag-Erling Smørgrav  * @param b: the communication to perform.
466b7579f77SDag-Erling Smørgrav  */
467b7579f77SDag-Erling Smørgrav void comm_base_dispatch(struct comm_base* b);
468b7579f77SDag-Erling Smørgrav 
469b7579f77SDag-Erling Smørgrav /**
470b7579f77SDag-Erling Smørgrav  * Exit from dispatch loop.
471b7579f77SDag-Erling Smørgrav  * @param b: the communication base that is in dispatch().
472b7579f77SDag-Erling Smørgrav  */
473b7579f77SDag-Erling Smørgrav void comm_base_exit(struct comm_base* b);
474b7579f77SDag-Erling Smørgrav 
475b7579f77SDag-Erling Smørgrav /**
476b7579f77SDag-Erling Smørgrav  * Set the slow_accept mode handlers.  You can not provide these if you do
477b7579f77SDag-Erling Smørgrav  * not perform accept() calls.
478b7579f77SDag-Erling Smørgrav  * @param b: comm base
479b7579f77SDag-Erling Smørgrav  * @param stop_accept: function that stops listening to accept fds.
480b7579f77SDag-Erling Smørgrav  * @param start_accept: function that resumes listening to accept fds.
481b7579f77SDag-Erling Smørgrav  * @param arg: callback arg to pass to the functions.
482b7579f77SDag-Erling Smørgrav  */
483b7579f77SDag-Erling Smørgrav void comm_base_set_slow_accept_handlers(struct comm_base* b,
484b7579f77SDag-Erling Smørgrav 	void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
485b7579f77SDag-Erling Smørgrav 
486b7579f77SDag-Erling Smørgrav /**
487b7579f77SDag-Erling Smørgrav  * Access internal data structure (for util/tube.c on windows)
488b7579f77SDag-Erling Smørgrav  * @param b: comm base
489e2d15004SDag-Erling Smørgrav  * @return ub_event_base.
490b7579f77SDag-Erling Smørgrav  */
491e2d15004SDag-Erling Smørgrav struct ub_event_base* comm_base_internal(struct comm_base* b);
492b7579f77SDag-Erling Smørgrav 
493b7579f77SDag-Erling Smørgrav /**
494b7579f77SDag-Erling Smørgrav  * Create an UDP comm point. Calls malloc.
495b7579f77SDag-Erling Smørgrav  * setups the structure with the parameters you provide.
496b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
497b7579f77SDag-Erling Smørgrav  * @param fd : file descriptor of open UDP socket.
498b7579f77SDag-Erling Smørgrav  * @param buffer: shared buffer by UDP sockets from this thread.
499b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer.
500b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
501*5469a995SCy Schubert  * @param socket: and opened socket properties will be passed to your callback function.
502b7579f77SDag-Erling Smørgrav  * @return: returns the allocated communication point. NULL on error.
503b7579f77SDag-Erling Smørgrav  * Sets timeout to NULL. Turns off TCP options.
504b7579f77SDag-Erling Smørgrav  */
505b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_udp(struct comm_base* base,
50617d15b25SDag-Erling Smørgrav 	int fd, struct sldns_buffer* buffer,
507*5469a995SCy Schubert 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
508b7579f77SDag-Erling Smørgrav 
509b7579f77SDag-Erling Smørgrav /**
510b7579f77SDag-Erling Smørgrav  * Create an UDP with ancillary data comm point. Calls malloc.
511b7579f77SDag-Erling Smørgrav  * Uses recvmsg instead of recv to get udp message.
512b7579f77SDag-Erling Smørgrav  * setups the structure with the parameters you provide.
513b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
514b7579f77SDag-Erling Smørgrav  * @param fd : file descriptor of open UDP socket.
515b7579f77SDag-Erling Smørgrav  * @param buffer: shared buffer by UDP sockets from this thread.
516b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer.
517b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
518*5469a995SCy Schubert  * @param socket: and opened socket properties will be passed to your callback function.
519b7579f77SDag-Erling Smørgrav  * @return: returns the allocated communication point. NULL on error.
520b7579f77SDag-Erling Smørgrav  * Sets timeout to NULL. Turns off TCP options.
521b7579f77SDag-Erling Smørgrav  */
522b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
52317d15b25SDag-Erling Smørgrav 	int fd, struct sldns_buffer* buffer,
524*5469a995SCy Schubert 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
525b7579f77SDag-Erling Smørgrav 
526b7579f77SDag-Erling Smørgrav /**
527b7579f77SDag-Erling Smørgrav  * Create a TCP listener comm point. Calls malloc.
528b7579f77SDag-Erling Smørgrav  * Setups the structure with the parameters you provide.
529b7579f77SDag-Erling Smørgrav  * Also Creates TCP Handlers, pre allocated for you.
530b7579f77SDag-Erling Smørgrav  * Uses the parameters you provide.
531b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
532b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor of open TCP socket set to listen nonblocking.
533b7579f77SDag-Erling Smørgrav  * @param num: becomes max_tcp_count, the routine allocates that
534b7579f77SDag-Erling Smørgrav  *	many tcp handler commpoints.
5354c75e3aaSDag-Erling Smørgrav  * @param idle_timeout: TCP idle timeout in ms.
536c0caa2e2SCy Schubert  * @param harden_large_queries: whether query size should be limited.
537c0caa2e2SCy Schubert  * @param http_max_streams: maximum number of HTTP/2 streams per connection.
538c0caa2e2SCy Schubert  * @param http_endpoint: HTTP endpoint to service queries on
5394c75e3aaSDag-Erling Smørgrav  * @param tcp_conn_limit: TCP connection limit info.
540b7579f77SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
541e86b9096SDag-Erling Smørgrav  * @param spoolbuf: shared spool buffer for tcp_req_info structures.
542e86b9096SDag-Erling Smørgrav  * 	or NULL to not create those structures in the tcp handlers.
543c0caa2e2SCy Schubert  * @param port_type: the type of port we are creating a TCP listener for. Used
544c0caa2e2SCy Schubert  * 	to select handler type to use.
545b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for TCP handlers.
546b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
547*5469a995SCy Schubert  * @param socket: and opened socket properties will be passed to your callback function.
548b7579f77SDag-Erling Smørgrav  * @return: returns the TCP listener commpoint. You can find the
549b7579f77SDag-Erling Smørgrav  *  	TCP handlers in the array inside the listener commpoint.
550b7579f77SDag-Erling Smørgrav  *	returns NULL on error.
551b7579f77SDag-Erling Smørgrav  * Inits timeout to NULL. All handlers are on the free list.
552b7579f77SDag-Erling Smørgrav  */
553b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_tcp(struct comm_base* base,
554c0caa2e2SCy Schubert 	int fd, int num, int idle_timeout, int harden_large_queries,
555c0caa2e2SCy Schubert 	uint32_t http_max_streams, char* http_endpoint,
556c0caa2e2SCy Schubert 	struct tcl_list* tcp_conn_limit,
557e86b9096SDag-Erling Smørgrav 	size_t bufsize, struct sldns_buffer* spoolbuf,
558c0caa2e2SCy Schubert 	enum listen_type port_type,
559*5469a995SCy Schubert 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
560b7579f77SDag-Erling Smørgrav 
561b7579f77SDag-Erling Smørgrav /**
562b7579f77SDag-Erling Smørgrav  * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
563b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
564b7579f77SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
565b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
566b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
567b7579f77SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
568b7579f77SDag-Erling Smørgrav  */
569b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
5703005e0a3SDag-Erling Smørgrav 	size_t bufsize, comm_point_callback_type* callback, void* callback_arg);
571b7579f77SDag-Erling Smørgrav 
572b7579f77SDag-Erling Smørgrav /**
57357bddd21SDag-Erling Smørgrav  * Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1.
57457bddd21SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
57557bddd21SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
57657bddd21SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
57757bddd21SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
57857bddd21SDag-Erling Smørgrav  * @param temp: sldns buffer, shared between other http_out commpoints, for
57957bddd21SDag-Erling Smørgrav  * 	temporary data when performing callbacks.
58057bddd21SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
58157bddd21SDag-Erling Smørgrav  */
58257bddd21SDag-Erling Smørgrav struct comm_point* comm_point_create_http_out(struct comm_base* base,
58357bddd21SDag-Erling Smørgrav 	size_t bufsize, comm_point_callback_type* callback,
58457bddd21SDag-Erling Smørgrav 	void* callback_arg, struct sldns_buffer* temp);
58557bddd21SDag-Erling Smørgrav 
58657bddd21SDag-Erling Smørgrav /**
587b7579f77SDag-Erling Smørgrav  * Create commpoint to listen to a local domain file descriptor.
588b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
589b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
590b7579f77SDag-Erling Smørgrav  * @param bufsize: size of buffer to create for handlers.
591b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
592b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
593b7579f77SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
594b7579f77SDag-Erling Smørgrav  */
595b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_local(struct comm_base* base,
596b7579f77SDag-Erling Smørgrav 	int fd, size_t bufsize,
5973005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback, void* callback_arg);
598b7579f77SDag-Erling Smørgrav 
599b7579f77SDag-Erling Smørgrav /**
600b7579f77SDag-Erling Smørgrav  * Create commpoint to listen to a local domain pipe descriptor.
601b7579f77SDag-Erling Smørgrav  * @param base: in which base to alloc the commpoint.
602b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
603b7579f77SDag-Erling Smørgrav  * @param writing: true if you want to listen to writes, false for reads.
604b7579f77SDag-Erling Smørgrav  * @param callback: callback function pointer for the handler.
605b7579f77SDag-Erling Smørgrav  * @param callback_arg: will be passed to your callback function.
606b7579f77SDag-Erling Smørgrav  * @return: the commpoint or NULL on error.
607b7579f77SDag-Erling Smørgrav  */
608b7579f77SDag-Erling Smørgrav struct comm_point* comm_point_create_raw(struct comm_base* base,
609b7579f77SDag-Erling Smørgrav 	int fd, int writing,
6103005e0a3SDag-Erling Smørgrav 	comm_point_callback_type* callback, void* callback_arg);
611b7579f77SDag-Erling Smørgrav 
612b7579f77SDag-Erling Smørgrav /**
613b7579f77SDag-Erling Smørgrav  * Close a comm point fd.
614b7579f77SDag-Erling Smørgrav  * @param c: comm point to close.
615b7579f77SDag-Erling Smørgrav  */
616b7579f77SDag-Erling Smørgrav void comm_point_close(struct comm_point* c);
617b7579f77SDag-Erling Smørgrav 
618b7579f77SDag-Erling Smørgrav /**
619b7579f77SDag-Erling Smørgrav  * Close and deallocate (free) the comm point. If the comm point is
620b7579f77SDag-Erling Smørgrav  * a tcp-accept point, also its tcp-handler points are deleted.
621b7579f77SDag-Erling Smørgrav  * @param c: comm point to delete.
622b7579f77SDag-Erling Smørgrav  */
623b7579f77SDag-Erling Smørgrav void comm_point_delete(struct comm_point* c);
624b7579f77SDag-Erling Smørgrav 
625b7579f77SDag-Erling Smørgrav /**
626b7579f77SDag-Erling Smørgrav  * Send reply. Put message into commpoint buffer.
627b7579f77SDag-Erling Smørgrav  * @param repinfo: The reply info copied from a commpoint callback call.
628b7579f77SDag-Erling Smørgrav  */
629b7579f77SDag-Erling Smørgrav void comm_point_send_reply(struct comm_reply* repinfo);
630b7579f77SDag-Erling Smørgrav 
631b7579f77SDag-Erling Smørgrav /**
632b7579f77SDag-Erling Smørgrav  * Drop reply. Cleans up.
633b7579f77SDag-Erling Smørgrav  * @param repinfo: The reply info copied from a commpoint callback call.
634b7579f77SDag-Erling Smørgrav  */
635b7579f77SDag-Erling Smørgrav void comm_point_drop_reply(struct comm_reply* repinfo);
636b7579f77SDag-Erling Smørgrav 
637b7579f77SDag-Erling Smørgrav /**
638b7579f77SDag-Erling Smørgrav  * Send an udp message over a commpoint.
639b7579f77SDag-Erling Smørgrav  * @param c: commpoint to send it from.
640b7579f77SDag-Erling Smørgrav  * @param packet: what to send.
641369c6923SCy Schubert  * @param addr: where to send it to.   If NULL, send is performed,
642369c6923SCy Schubert  * 	for connected sockets, to the connected address.
643b7579f77SDag-Erling Smørgrav  * @param addrlen: length of addr.
6447341cb0cSXin LI  * @param is_connected: if the UDP socket is connect()ed.
645b7579f77SDag-Erling Smørgrav  * @return: false on a failure.
646b7579f77SDag-Erling Smørgrav  */
64717d15b25SDag-Erling Smørgrav int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
6487341cb0cSXin LI 	struct sockaddr* addr, socklen_t addrlen,int is_connected);
649b7579f77SDag-Erling Smørgrav 
650b7579f77SDag-Erling Smørgrav /**
651b7579f77SDag-Erling Smørgrav  * Stop listening for input on the commpoint. No callbacks will happen.
652b7579f77SDag-Erling Smørgrav  * @param c: commpoint to disable. The fd is not closed.
653b7579f77SDag-Erling Smørgrav  */
654b7579f77SDag-Erling Smørgrav void comm_point_stop_listening(struct comm_point* c);
655b7579f77SDag-Erling Smørgrav 
656b7579f77SDag-Erling Smørgrav /**
657b7579f77SDag-Erling Smørgrav  * Start listening again for input on the comm point.
658b7579f77SDag-Erling Smørgrav  * @param c: commpoint to enable again.
659b7579f77SDag-Erling Smørgrav  * @param newfd: new fd, or -1 to leave fd be.
660b5663de9SDag-Erling Smørgrav  * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout.
661b5663de9SDag-Erling Smørgrav  *	So seconds*1000.
662b7579f77SDag-Erling Smørgrav  */
663b5663de9SDag-Erling Smørgrav void comm_point_start_listening(struct comm_point* c, int newfd, int msec);
664b7579f77SDag-Erling Smørgrav 
665b7579f77SDag-Erling Smørgrav /**
666b7579f77SDag-Erling Smørgrav  * Stop listening and start listening again for reading or writing.
667b7579f77SDag-Erling Smørgrav  * @param c: commpoint
668b7579f77SDag-Erling Smørgrav  * @param rd: if true, listens for reading.
669b7579f77SDag-Erling Smørgrav  * @param wr: if true, listens for writing.
670b7579f77SDag-Erling Smørgrav  */
671b7579f77SDag-Erling Smørgrav void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
672b7579f77SDag-Erling Smørgrav 
673b7579f77SDag-Erling Smørgrav /**
674f44e67d1SCy Schubert  * For TCP handlers that use c->tcp_timeout_msec, this routine adjusts
675f44e67d1SCy Schubert  * it with the minimum.  Otherwise, a 0 value advertised without the
676f44e67d1SCy Schubert  * minimum applied moves to a 0 in comm_point_start_listening and that
677f44e67d1SCy Schubert  * routine treats it as no timeout, listen forever, which is not wanted.
678f44e67d1SCy Schubert  * @param c: comm point to use the tcp_timeout_msec of.
679f44e67d1SCy Schubert  * @return adjusted tcp_timeout_msec value with the minimum if smaller.
680f44e67d1SCy Schubert  */
681f44e67d1SCy Schubert int adjusted_tcp_timeout(struct comm_point* c);
682f44e67d1SCy Schubert 
683f44e67d1SCy Schubert /**
684b7579f77SDag-Erling Smørgrav  * Get size of memory used by comm point.
685b7579f77SDag-Erling Smørgrav  * For TCP handlers this includes subhandlers.
686b7579f77SDag-Erling Smørgrav  * For UDP handlers, this does not include the (shared) UDP buffer.
687b7579f77SDag-Erling Smørgrav  * @param c: commpoint.
688b7579f77SDag-Erling Smørgrav  * @return size in bytes.
689b7579f77SDag-Erling Smørgrav  */
690b7579f77SDag-Erling Smørgrav size_t comm_point_get_mem(struct comm_point* c);
691b7579f77SDag-Erling Smørgrav 
692b7579f77SDag-Erling Smørgrav /**
693b7579f77SDag-Erling Smørgrav  * create timer. Not active upon creation.
694b7579f77SDag-Erling Smørgrav  * @param base: event handling base.
695b7579f77SDag-Erling Smørgrav  * @param cb: callback function: void myfunc(void* myarg);
696b7579f77SDag-Erling Smørgrav  * @param cb_arg: user callback argument.
697b7579f77SDag-Erling Smørgrav  * @return: the new timer or NULL on error.
698b7579f77SDag-Erling Smørgrav  */
699b7579f77SDag-Erling Smørgrav struct comm_timer* comm_timer_create(struct comm_base* base,
700b7579f77SDag-Erling Smørgrav 	void (*cb)(void*), void* cb_arg);
701b7579f77SDag-Erling Smørgrav 
702b7579f77SDag-Erling Smørgrav /**
703b7579f77SDag-Erling Smørgrav  * disable timer. Stops callbacks from happening.
704b7579f77SDag-Erling Smørgrav  * @param timer: to disable.
705b7579f77SDag-Erling Smørgrav  */
706b7579f77SDag-Erling Smørgrav void comm_timer_disable(struct comm_timer* timer);
707b7579f77SDag-Erling Smørgrav 
708b7579f77SDag-Erling Smørgrav /**
709b7579f77SDag-Erling Smørgrav  * reset timevalue for timer.
710b7579f77SDag-Erling Smørgrav  * @param timer: timer to (re)set.
711b7579f77SDag-Erling Smørgrav  * @param tv: when the timer should activate. if NULL timer is disabled.
712b7579f77SDag-Erling Smørgrav  */
713b7579f77SDag-Erling Smørgrav void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
714b7579f77SDag-Erling Smørgrav 
715b7579f77SDag-Erling Smørgrav /**
716b7579f77SDag-Erling Smørgrav  * delete timer.
717b7579f77SDag-Erling Smørgrav  * @param timer: to delete.
718b7579f77SDag-Erling Smørgrav  */
719b7579f77SDag-Erling Smørgrav void comm_timer_delete(struct comm_timer* timer);
720b7579f77SDag-Erling Smørgrav 
721b7579f77SDag-Erling Smørgrav /**
722b7579f77SDag-Erling Smørgrav  * see if timeout has been set to a value.
723b7579f77SDag-Erling Smørgrav  * @param timer: the timer to examine.
724b7579f77SDag-Erling Smørgrav  * @return: false if disabled or not set.
725b7579f77SDag-Erling Smørgrav  */
726b7579f77SDag-Erling Smørgrav int comm_timer_is_set(struct comm_timer* timer);
727b7579f77SDag-Erling Smørgrav 
728b7579f77SDag-Erling Smørgrav /**
729b7579f77SDag-Erling Smørgrav  * Get size of memory used by comm timer.
730b7579f77SDag-Erling Smørgrav  * @param timer: the timer to examine.
731b7579f77SDag-Erling Smørgrav  * @return size in bytes.
732b7579f77SDag-Erling Smørgrav  */
733b7579f77SDag-Erling Smørgrav size_t comm_timer_get_mem(struct comm_timer* timer);
734b7579f77SDag-Erling Smørgrav 
735b7579f77SDag-Erling Smørgrav /**
736b7579f77SDag-Erling Smørgrav  * Create a signal handler. Call signal_bind() later to bind to a signal.
737b7579f77SDag-Erling Smørgrav  * @param base: communication base to use.
738b7579f77SDag-Erling Smørgrav  * @param callback: called when signal is caught.
739b7579f77SDag-Erling Smørgrav  * @param cb_arg: user argument to callback
740b7579f77SDag-Erling Smørgrav  * @return: the signal struct or NULL on error.
741b7579f77SDag-Erling Smørgrav  */
742b7579f77SDag-Erling Smørgrav struct comm_signal* comm_signal_create(struct comm_base* base,
743b7579f77SDag-Erling Smørgrav 	void (*callback)(int, void*), void* cb_arg);
744b7579f77SDag-Erling Smørgrav 
745b7579f77SDag-Erling Smørgrav /**
746b7579f77SDag-Erling Smørgrav  * Bind signal struct to catch a signal. A signle comm_signal can be bound
747b7579f77SDag-Erling Smørgrav  * to multiple signals, calling comm_signal_bind multiple times.
748b7579f77SDag-Erling Smørgrav  * @param comsig: the communication point, with callback information.
749b7579f77SDag-Erling Smørgrav  * @param sig: signal number.
750b7579f77SDag-Erling Smørgrav  * @return: true on success. false on error.
751b7579f77SDag-Erling Smørgrav  */
752b7579f77SDag-Erling Smørgrav int comm_signal_bind(struct comm_signal* comsig, int sig);
753b7579f77SDag-Erling Smørgrav 
754b7579f77SDag-Erling Smørgrav /**
755b7579f77SDag-Erling Smørgrav  * Delete the signal communication point.
756b7579f77SDag-Erling Smørgrav  * @param comsig: to delete.
757b7579f77SDag-Erling Smørgrav  */
758b7579f77SDag-Erling Smørgrav void comm_signal_delete(struct comm_signal* comsig);
759b7579f77SDag-Erling Smørgrav 
760b7579f77SDag-Erling Smørgrav /**
761b7579f77SDag-Erling Smørgrav  * perform accept(2) with error checking.
762b7579f77SDag-Erling Smørgrav  * @param c: commpoint with accept fd.
763b7579f77SDag-Erling Smørgrav  * @param addr: remote end returned here.
764b7579f77SDag-Erling Smørgrav  * @param addrlen: length of remote end returned here.
765b7579f77SDag-Erling Smørgrav  * @return new fd, or -1 on error.
766b7579f77SDag-Erling Smørgrav  *	if -1, error message has been printed if necessary, simply drop
767b7579f77SDag-Erling Smørgrav  *	out of the reading handler.
768b7579f77SDag-Erling Smørgrav  */
769b7579f77SDag-Erling Smørgrav int comm_point_perform_accept(struct comm_point* c,
770b7579f77SDag-Erling Smørgrav 	struct sockaddr_storage* addr, socklen_t* addrlen);
771b7579f77SDag-Erling Smørgrav 
772b7579f77SDag-Erling Smørgrav /**** internal routines ****/
773b7579f77SDag-Erling Smørgrav 
774b7579f77SDag-Erling Smørgrav /**
775b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
776b7579f77SDag-Erling Smørgrav  * handle libevent callback for udp comm point.
777b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
778b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
779b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
780b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
781b7579f77SDag-Erling Smørgrav  */
782b7579f77SDag-Erling Smørgrav void comm_point_udp_callback(int fd, short event, void* arg);
783b7579f77SDag-Erling Smørgrav 
784b7579f77SDag-Erling Smørgrav /**
785b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
786b7579f77SDag-Erling Smørgrav  * handle libevent callback for udp ancillary data comm point.
787b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
788b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
789b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
790b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
791b7579f77SDag-Erling Smørgrav  */
792b7579f77SDag-Erling Smørgrav void comm_point_udp_ancil_callback(int fd, short event, void* arg);
793b7579f77SDag-Erling Smørgrav 
794b7579f77SDag-Erling Smørgrav /**
795b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
796b7579f77SDag-Erling Smørgrav  * handle libevent callback for tcp accept comm point
797b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
798b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
799b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
800b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
801b7579f77SDag-Erling Smørgrav  */
802b7579f77SDag-Erling Smørgrav void comm_point_tcp_accept_callback(int fd, short event, void* arg);
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 tcp data 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_tcp_handle_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.
81657bddd21SDag-Erling Smørgrav  * handle libevent callback for tcp data comm point
81757bddd21SDag-Erling Smørgrav  * @param fd: file descriptor.
81857bddd21SDag-Erling Smørgrav  * @param event: event bits from libevent:
81957bddd21SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
82057bddd21SDag-Erling Smørgrav  * @param arg: the comm_point structure.
82157bddd21SDag-Erling Smørgrav  */
82257bddd21SDag-Erling Smørgrav void comm_point_http_handle_callback(int fd, short event, void* arg);
82357bddd21SDag-Erling Smørgrav 
82457bddd21SDag-Erling Smørgrav /**
825c0caa2e2SCy Schubert  * HTTP2 session.  HTTP2 related info per comm point.
826c0caa2e2SCy Schubert  */
827c0caa2e2SCy Schubert struct http2_session {
828c0caa2e2SCy Schubert 	/** first item in list of streams */
829c0caa2e2SCy Schubert 	struct http2_stream* first_stream;
830c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2
831c0caa2e2SCy Schubert 	/** nghttp2 session */
832c0caa2e2SCy Schubert 	nghttp2_session *session;
833c0caa2e2SCy Schubert 	/** store nghttp2 callbacks for easy reuse */
834c0caa2e2SCy Schubert 	nghttp2_session_callbacks* callbacks;
835c0caa2e2SCy Schubert #endif
836c0caa2e2SCy Schubert 	/** comm point containing buffer used to build answer in worker or
837c0caa2e2SCy Schubert 	 * module */
838c0caa2e2SCy Schubert 	struct comm_point* c;
839c0caa2e2SCy Schubert 	/** session is instructed to get dropped (comm port will be closed) */
840c0caa2e2SCy Schubert 	int is_drop;
841c0caa2e2SCy Schubert 	/** postpone dropping the session, can be used to prevent dropping
842c0caa2e2SCy Schubert 	 * while being in a callback */
843c0caa2e2SCy Schubert 	int postpone_drop;
844c0caa2e2SCy Schubert };
845c0caa2e2SCy Schubert 
846c0caa2e2SCy Schubert /** enum of HTTP status */
847c0caa2e2SCy Schubert enum http_status {
848c0caa2e2SCy Schubert 	HTTP_STATUS_OK = 200,
849c0caa2e2SCy Schubert 	HTTP_STATUS_BAD_REQUEST = 400,
850c0caa2e2SCy Schubert 	HTTP_STATUS_NOT_FOUND = 404,
851c0caa2e2SCy Schubert 	HTTP_STATUS_PAYLOAD_TOO_LARGE = 413,
852c0caa2e2SCy Schubert 	HTTP_STATUS_URI_TOO_LONG = 414,
853c0caa2e2SCy Schubert 	HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
854c0caa2e2SCy Schubert 	HTTP_STATUS_NOT_IMPLEMENTED = 501
855c0caa2e2SCy Schubert };
856c0caa2e2SCy Schubert 
857c0caa2e2SCy Schubert /**
858c0caa2e2SCy Schubert  * HTTP stream. Part of list of HTTP2 streams per session.
859c0caa2e2SCy Schubert  */
860c0caa2e2SCy Schubert struct http2_stream {
861c0caa2e2SCy Schubert 	/** next stream in list per session */
862c0caa2e2SCy Schubert 	struct http2_stream* next;
863c0caa2e2SCy Schubert 	/** previous stream in list per session */
864c0caa2e2SCy Schubert 	struct http2_stream* prev;
865c0caa2e2SCy Schubert 	/** HTTP2 stream ID is an unsigned 31-bit integer */
866c0caa2e2SCy Schubert 	int32_t stream_id;
867c0caa2e2SCy Schubert 	/** HTTP method used for this stream */
868c0caa2e2SCy Schubert 	enum {
869c0caa2e2SCy Schubert 		HTTP_METHOD_POST = 1,
870c0caa2e2SCy Schubert 		HTTP_METHOD_GET,
871c0caa2e2SCy Schubert 		HTTP_METHOD_UNSUPPORTED
872c0caa2e2SCy Schubert 	} http_method;
873c0caa2e2SCy Schubert 	/** message contains invalid content type */
874c0caa2e2SCy Schubert 	int invalid_content_type;
875c0caa2e2SCy Schubert 	/** message body content type */
876c0caa2e2SCy Schubert 	size_t content_length;
877c0caa2e2SCy Schubert 	/** HTTP response status */
878c0caa2e2SCy Schubert 	enum http_status status;
879c0caa2e2SCy Schubert 	/** request for non existing endpoint */
880c0caa2e2SCy Schubert 	int invalid_endpoint;
881c0caa2e2SCy Schubert 	/** query in request is too large */
882c0caa2e2SCy Schubert 	int query_too_large;
883c0caa2e2SCy Schubert 	/** buffer to store query into. Can't use session shared buffer as query
884c0caa2e2SCy Schubert 	 * can arrive in parts, intertwined with frames for other queries. */
885c0caa2e2SCy Schubert 	struct sldns_buffer* qbuffer;
886c0caa2e2SCy Schubert 	/** buffer to store response into. Can't use shared buffer as a next
887c0caa2e2SCy Schubert 	 * query read callback can overwrite it before it is send out. */
888c0caa2e2SCy Schubert 	struct sldns_buffer* rbuffer;
889c0caa2e2SCy Schubert 	/** mesh area containing mesh state */
890c0caa2e2SCy Schubert 	struct mesh_area* mesh;
891c0caa2e2SCy Schubert 	/** mesh state for query. Used to remove mesh reply before closing
892c0caa2e2SCy Schubert 	 * stream. */
893c0caa2e2SCy Schubert 	struct mesh_state* mesh_state;
894c0caa2e2SCy Schubert };
895c0caa2e2SCy Schubert 
896c0caa2e2SCy Schubert #ifdef HAVE_NGHTTP2
897c0caa2e2SCy Schubert /** nghttp2 receive cb. Read from SSL connection into nghttp2 buffer */
898c0caa2e2SCy Schubert ssize_t http2_recv_cb(nghttp2_session* session, uint8_t* buf,
899c0caa2e2SCy Schubert 	size_t len, int flags, void* cb_arg);
900c0caa2e2SCy Schubert /** nghttp2 send callback. Send from nghttp2 buffer to ssl socket */
901c0caa2e2SCy Schubert ssize_t http2_send_cb(nghttp2_session* session, const uint8_t* buf,
902c0caa2e2SCy Schubert 	size_t len, int flags, void* cb_arg);
903c0caa2e2SCy Schubert /** nghttp2 callback on closing stream */
904c0caa2e2SCy Schubert int http2_stream_close_cb(nghttp2_session* session, int32_t stream_id,
905c0caa2e2SCy Schubert 	uint32_t error_code, void* cb_arg);
906c0caa2e2SCy Schubert #endif
907c0caa2e2SCy Schubert 
908c0caa2e2SCy Schubert /**
909c0caa2e2SCy Schubert  * Create new http2 stream
910c0caa2e2SCy Schubert  * @param stream_id: ID for stream to create.
911c0caa2e2SCy Schubert  * @return malloc'ed stream, NULL on error
912c0caa2e2SCy Schubert  */
913c0caa2e2SCy Schubert struct http2_stream* http2_stream_create(int32_t stream_id);
914c0caa2e2SCy Schubert 
915c0caa2e2SCy Schubert /**
916c0caa2e2SCy Schubert  * Add new stream to session linked list
917c0caa2e2SCy Schubert  * @param h2_session: http2 session to add stream to
918c0caa2e2SCy Schubert  * @param h2_stream: stream to add to session list
919c0caa2e2SCy Schubert  */
920c0caa2e2SCy Schubert void http2_session_add_stream(struct http2_session* h2_session,
921c0caa2e2SCy Schubert 	struct http2_stream* h2_stream);
922c0caa2e2SCy Schubert 
923c0caa2e2SCy Schubert /** Add mesh state to stream. To be able to remove mesh reply on stream closure
924c0caa2e2SCy Schubert  */
925c0caa2e2SCy Schubert void http2_stream_add_meshstate(struct http2_stream* h2_stream,
926c0caa2e2SCy Schubert 	struct mesh_area* mesh, struct mesh_state* m);
927c0caa2e2SCy Schubert 
928c0caa2e2SCy Schubert /**
92957bddd21SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
930b7579f77SDag-Erling Smørgrav  * handle libevent callback for timer comm.
931b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor (always -1).
932b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
933b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
934b7579f77SDag-Erling Smørgrav  * @param arg: the comm_timer structure.
935b7579f77SDag-Erling Smørgrav  */
936b7579f77SDag-Erling Smørgrav void comm_timer_callback(int fd, short event, void* arg);
937b7579f77SDag-Erling Smørgrav 
938b7579f77SDag-Erling Smørgrav /**
939b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
940b7579f77SDag-Erling Smørgrav  * handle libevent callback for signal comm.
941b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor (used for the signal number).
942b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
943b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
944b7579f77SDag-Erling Smørgrav  * @param arg: the internal commsignal structure.
945b7579f77SDag-Erling Smørgrav  */
946b7579f77SDag-Erling Smørgrav void comm_signal_callback(int fd, short event, void* arg);
947b7579f77SDag-Erling Smørgrav 
948b7579f77SDag-Erling Smørgrav /**
949b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
950b7579f77SDag-Erling Smørgrav  * libevent callback for AF_UNIX fds
951b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
952b7579f77SDag-Erling Smørgrav  * @param event: event bits from libevent:
953b7579f77SDag-Erling Smørgrav  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
954b7579f77SDag-Erling Smørgrav  * @param arg: the comm_point structure.
955b7579f77SDag-Erling Smørgrav  */
956b7579f77SDag-Erling Smørgrav void comm_point_local_handle_callback(int fd, short event, void* arg);
957b7579f77SDag-Erling Smørgrav 
958b7579f77SDag-Erling Smørgrav /**
959b7579f77SDag-Erling Smørgrav  * This routine is published for checks and tests, and is only used internally.
960b7579f77SDag-Erling Smørgrav  * libevent callback for raw fd access.
961b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
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_point structure.
965b7579f77SDag-Erling Smørgrav  */
966b7579f77SDag-Erling Smørgrav void comm_point_raw_handle_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  * libevent callback for timeout on slow accept.
971b7579f77SDag-Erling Smørgrav  * @param fd: file descriptor.
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 comm_point structure.
975b7579f77SDag-Erling Smørgrav  */
976b7579f77SDag-Erling Smørgrav void comm_base_handle_slow_accept(int fd, short event, void* arg);
977b7579f77SDag-Erling Smørgrav 
978b7579f77SDag-Erling Smørgrav #ifdef USE_WINSOCK
979b7579f77SDag-Erling Smørgrav /**
980b7579f77SDag-Erling Smørgrav  * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
981b7579f77SDag-Erling Smørgrav  * the winsock_event of this for proper TCP nonblocking implementation.
982b7579f77SDag-Erling Smørgrav  * @param c: comm_point, fd must be set its struct event is registered.
983b7579f77SDag-Erling Smørgrav  * @param ssl: openssl SSL, fd must be set so it has a bio.
984b7579f77SDag-Erling Smørgrav  */
985b7579f77SDag-Erling Smørgrav void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
986b7579f77SDag-Erling Smørgrav #endif
987b7579f77SDag-Erling Smørgrav 
98825039b37SCy Schubert /**
98925039b37SCy Schubert  * See if errno for tcp connect has to be logged or not. This uses errno
99025039b37SCy Schubert  * @param addr: apart from checking errno, the addr is checked for ip4mapped
99125039b37SCy Schubert  * 	and broadcast type, hence passed.
99225039b37SCy Schubert  * @param addrlen: length of the addr parameter.
99325039b37SCy Schubert  * @return true if it needs to be logged.
99425039b37SCy Schubert  */
995b7579f77SDag-Erling Smørgrav int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
996b7579f77SDag-Erling Smørgrav 
99725039b37SCy Schubert #ifdef HAVE_SSL
99825039b37SCy Schubert /**
99925039b37SCy Schubert  * True if the ssl handshake error has to be squelched from the logs
100025039b37SCy Schubert  * @param err: the error returned by the openssl routine, ERR_get_error.
100125039b37SCy Schubert  * 	This is a packed structure with elements that are examined.
100225039b37SCy Schubert  * @return true if the error is squelched (not logged).
100325039b37SCy Schubert  */
100425039b37SCy Schubert int squelch_err_ssl_handshake(unsigned long err);
100525039b37SCy Schubert #endif
100625039b37SCy Schubert 
1007b7579f77SDag-Erling Smørgrav #endif /* NET_EVENT_H */
1008