xref: /freebsd/contrib/unbound/util/netevent.h (revision 38a52bd3)
1 /*
2  * util/netevent.h - event notification
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains event notification functions.
40  *
41  * There are three types of communication points
42  *    o UDP socket - perthread buffer.
43  *    o TCP-accept socket - array of TCP-sockets, socketcount.
44  *    o TCP socket - own buffer, parent-TCPaccept, read/write state,
45  *                   number of bytes read/written, timeout.
46  *
47  * There are sockets aimed towards our clients and towards the internet.
48  *    o frontside - aimed towards our clients, queries come in, answers back.
49  *    o behind - aimed towards internet, to the authoritative DNS servers.
50  *
51  * Several event types are available:
52  *    o comm_base - for thread safety of the comm points, one per thread.
53  *    o comm_point - udp and tcp networking, with callbacks.
54  *    o comm_timer - a timeout with callback.
55  *    o comm_signal - callbacks when signal is caught.
56  *    o comm_reply - holds reply info during networking callback.
57  *
58  */
59 
60 #ifndef NET_EVENT_H
61 #define NET_EVENT_H
62 
63 #include "dnscrypt/dnscrypt.h"
64 #ifdef HAVE_NGHTTP2_NGHTTP2_H
65 #include <nghttp2/nghttp2.h>
66 #endif
67 
68 struct sldns_buffer;
69 struct comm_point;
70 struct comm_reply;
71 struct tcl_list;
72 struct ub_event_base;
73 struct unbound_socket;
74 
75 struct mesh_state;
76 struct mesh_area;
77 
78 /* internal event notification data storage structure. */
79 struct internal_event;
80 struct internal_base;
81 struct internal_timer; /* A sub struct of the comm_timer super struct */
82 
83 enum listen_type;
84 
85 /** callback from communication point function type */
86 typedef int comm_point_callback_type(struct comm_point*, void*, int,
87 	struct comm_reply*);
88 
89 /** to pass no_error to callback function */
90 #define NETEVENT_NOERROR 0
91 /** to pass closed connection to callback function */
92 #define NETEVENT_CLOSED -1
93 /** to pass timeout happened to callback function */
94 #define NETEVENT_TIMEOUT -2
95 /** to pass fallback from capsforID to callback function; 0x20 failed */
96 #define NETEVENT_CAPSFAIL -3
97 /** to pass done transfer to callback function; http file is complete */
98 #define NETEVENT_DONE -4
99 /** to pass write of the write packet is done to callback function
100  * used when tcp_write_and_read is enabled */
101 #define NETEVENT_PKT_WRITTEN -5
102 
103 /** timeout to slow accept calls when not possible, in msec. */
104 #define NETEVENT_SLOW_ACCEPT_TIME 2000
105 /** timeout to slow down log print, so it does not spam the logs, in sec */
106 #define SLOW_LOG_TIME 10
107 
108 /**
109  * A communication point dispatcher. Thread specific.
110  */
111 struct comm_base {
112 	/** behind the scenes structure. with say libevent info. alloced */
113 	struct internal_base* eb;
114 	/** callback to stop listening on accept sockets,
115 	 * performed when accept() will not function properly */
116 	void (*stop_accept)(void*);
117 	/** callback to start listening on accept sockets, performed
118 	 * after stop_accept() then a timeout has passed. */
119 	void (*start_accept)(void*);
120 	/** user argument for stop_accept and start_accept functions */
121 	void* cb_arg;
122 };
123 
124 /**
125  * Reply information for a communication point.
126  */
127 struct comm_reply {
128 	/** the comm_point with fd to send reply on to. */
129 	struct comm_point* c;
130 	/** the address (for UDP based communication) */
131 	struct sockaddr_storage remote_addr;
132 	/** length of address */
133 	socklen_t remote_addrlen;
134 	/** return type 0 (none), 4(IP4), 6(IP6)
135 	 *  used only with listen_type_udp_ancil* */
136 	int srctype;
137 	/* DnsCrypt context */
138 #ifdef USE_DNSCRYPT
139 	uint8_t client_nonce[crypto_box_HALF_NONCEBYTES];
140 	uint8_t nmkey[crypto_box_BEFORENMBYTES];
141 	const dnsccert *dnsc_cert;
142 	int is_dnscrypted;
143 #endif
144 	/** the return source interface data */
145 	union {
146 #ifdef IPV6_PKTINFO
147 		struct in6_pktinfo v6info;
148 #endif
149 #ifdef IP_PKTINFO
150 		struct in_pktinfo v4info;
151 #elif defined(IP_RECVDSTADDR)
152 		struct in_addr v4addr;
153 #endif
154 	}
155 		/** variable with return source data */
156 		pktinfo;
157 	/** max udp size for udp packets */
158 	size_t max_udp_size;
159 	/* if set, the request came through a proxy */
160 	int is_proxied;
161 	/** the client address
162 	 *  the same as remote_addr if not proxied */
163 	struct sockaddr_storage client_addr;
164 	/** the original address length */
165 	socklen_t client_addrlen;
166 };
167 
168 /**
169  * Communication point to the network
170  * These behaviours can be accomplished by setting the flags
171  * and passing return values from the callback.
172  *    udp frontside: called after readdone. sendafter.
173  *    tcp frontside: called readdone, sendafter. close.
174  *    udp behind: called after readdone. No send after.
175  *    tcp behind: write done, read done, then called. No send after.
176  */
177 struct comm_point {
178 	/** behind the scenes structure, with say libevent info. alloced. */
179 	struct internal_event* ev;
180 	/** if the event is added or not */
181 	int event_added;
182 
183 	struct unbound_socket* socket;
184 
185 	/** file descriptor for communication point */
186 	int fd;
187 
188 	/** timeout (NULL if it does not). Malloced. */
189 	struct timeval* timeout;
190 
191 	/** buffer pointer. Either to perthread, or own buffer or NULL */
192 	struct sldns_buffer* buffer;
193 
194 	/* -------- TCP Handler -------- */
195 	/** Read/Write state for TCP */
196 	int tcp_is_reading;
197 	/** The current read/write count for TCP */
198 	size_t tcp_byte_count;
199 	/** parent communication point (for TCP sockets) */
200 	struct comm_point* tcp_parent;
201 	/** sockaddr from peer, for TCP handlers */
202 	struct comm_reply repinfo;
203 
204 	/* -------- TCP Accept -------- */
205 	/** the number of TCP handlers for this tcp-accept socket */
206 	int max_tcp_count;
207 	/** current number of tcp handler in-use for this accept socket */
208 	int cur_tcp_count;
209 	/** malloced array of tcp handlers for a tcp-accept,
210 	    of size max_tcp_count. */
211 	struct comm_point** tcp_handlers;
212 	/** linked list of free tcp_handlers to use for new queries.
213 	    For tcp_accept the first entry, for tcp_handlers the next one. */
214 	struct comm_point* tcp_free;
215 
216 	/* -------- SSL TCP DNS ------- */
217 	/** the SSL object with rw bio (owned) or for commaccept ctx ref */
218 	void* ssl;
219 	/** handshake state for init and renegotiate */
220 	enum {
221 		/** no handshake, it has been done */
222 		comm_ssl_shake_none = 0,
223 		/** ssl initial handshake wants to read */
224 		comm_ssl_shake_read,
225 		/** ssl initial handshake wants to write */
226 		comm_ssl_shake_write,
227 		/** ssl_write wants to read */
228 		comm_ssl_shake_hs_read,
229 		/** ssl_read wants to write */
230 		comm_ssl_shake_hs_write
231 	} ssl_shake_state;
232 
233 	/* -------- HTTP ------- */
234 	/** Do not allow connection to use HTTP version lower than this. 0=no
235 	 * minimum. */
236 	enum {
237 		http_version_none = 0,
238 		http_version_2 = 2
239 	} http_min_version;
240 	/** http endpoint */
241 	char* http_endpoint;
242 	/* -------- HTTP/1.1 ------- */
243 	/** Currently reading in http headers */
244 	int http_in_headers;
245 	/** Currently reading in chunk headers, 0=not, 1=firstline, 2=unused
246 	 * (more lines), 3=trailer headers after chunk */
247 	int http_in_chunk_headers;
248 	/** chunked transfer */
249 	int http_is_chunked;
250 	/** http temp buffer (shared buffer for temporary work) */
251 	struct sldns_buffer* http_temp;
252 	/** http stored content in buffer */
253 	size_t http_stored;
254 	/* -------- HTTP/2 ------- */
255 	/** http2 session */
256 	struct http2_session* h2_session;
257 	/** set to 1 if h2 is negotiated to be used (using alpn) */
258 	int use_h2;
259 	/** stream currently being handled */
260 	struct http2_stream* h2_stream;
261 	/** maximum allowed query buffer size, per stream */
262 	size_t http2_stream_max_qbuffer_size;
263 	/** maximum number of HTTP/2 streams per connection. Send in HTTP/2
264 	 * SETTINGS frame. */
265 	uint32_t http2_max_streams;
266 
267 	/* -------- dnstap ------- */
268 	/** the dnstap environment */
269 	struct dt_env* dtenv;
270 
271 	/** is this a UDP, TCP-accept or TCP socket. */
272 	enum comm_point_type {
273 		/** UDP socket - handle datagrams. */
274 		comm_udp,
275 		/** TCP accept socket - only creates handlers if readable. */
276 		comm_tcp_accept,
277 		/** TCP handler socket - handle byteperbyte readwrite. */
278 		comm_tcp,
279 		/** HTTP handler socket */
280 		comm_http,
281 		/** AF_UNIX socket - for internal commands. */
282 		comm_local,
283 		/** raw - not DNS format - for pipe readers and writers */
284 		comm_raw
285 	}
286 		/** variable with type of socket, UDP,TCP-accept,TCP,pipe */
287 		type;
288 
289 	/* -------- PROXYv2 ------- */
290 	/** if set, PROXYv2 is expected on this connection */
291 	int pp2_enabled;
292 	/** header state for the PROXYv2 header (for TCP) */
293 	enum {
294 		/** no header encounter yet */
295 		pp2_header_none = 0,
296 		/** read the static part of the header */
297 		pp2_header_init,
298 		/** read the full header */
299 		pp2_header_done
300 	} pp2_header_state;
301 
302 	/* ---------- Behaviour ----------- */
303 	/** if set the connection is NOT closed on delete. */
304 	int do_not_close;
305 
306 	/** if set, the connection is closed on error, on timeout,
307 	    and after read/write completes. No callback is done. */
308 	int tcp_do_close;
309 
310 	/** flag that indicates the stream is both written and read from. */
311 	int tcp_write_and_read;
312 
313 	/** byte count for written length over write channel, for when
314 	 * tcp_write_and_read is enabled.  When tcp_write_and_read is enabled,
315 	 * this is the counter for writing, the one for reading is in the
316 	 * commpoint.buffer sldns buffer.  The counter counts from 0 to
317 	 * 2+tcp_write_pkt_len, and includes the tcp length bytes. */
318 	size_t tcp_write_byte_count;
319 
320 	/** packet to write currently over the write channel. for when
321 	 * tcp_write_and_read is enabled.  When tcp_write_and_read is enabled,
322 	 * this is the buffer for the written packet, the commpoint.buffer
323 	 * sldns buffer is the buffer for the received packet. */
324 	uint8_t* tcp_write_pkt;
325 	/** length of tcp_write_pkt in bytes */
326 	size_t tcp_write_pkt_len;
327 
328 	/** if set try to read another packet again (over connection with
329 	 * multiple packets), once set, tries once, then zero again,
330 	 * so set it in the packet complete section.
331 	 * The pointer itself has to be set before the callback is invoked,
332 	 * when you set things up, and continue to exist also after the
333 	 * commpoint is closed and deleted in your callback.  So that after
334 	 * the callback cleans up netevent can see what it has to do.
335 	 * Or leave NULL if it is not used at all. */
336 	int* tcp_more_read_again;
337 
338 	/** if set try to write another packet (over connection with
339 	 * multiple packets), once set, tries once, then zero again,
340 	 * so set it in the packet complete section.
341 	 * The pointer itself has to be set before the callback is invoked,
342 	 * when you set things up, and continue to exist also after the
343 	 * commpoint is closed and deleted in your callback.  So that after
344 	 * the callback cleans up netevent can see what it has to do.
345 	 * Or leave NULL if it is not used at all. */
346 	int* tcp_more_write_again;
347 
348 	/** if set, read/write completes:
349 		read/write state of tcp is toggled.
350 		buffer reset/bytecount reset.
351 		this flag cleared.
352 	    So that when that is done the callback is called. */
353 	int tcp_do_toggle_rw;
354 
355 	/** timeout in msec for TCP wait times for this connection */
356 	int tcp_timeout_msec;
357 
358 	/** if set, tcp keepalive is enabled on this connection */
359 	int tcp_keepalive;
360 
361 	/** if set, checks for pending error from nonblocking connect() call.*/
362 	int tcp_check_nb_connect;
363 
364 	/** if set, check for connection limit on tcp accept. */
365 	struct tcl_list* tcp_conn_limit;
366 	/** the entry for the connection. */
367 	struct tcl_addr* tcl_addr;
368 
369 	/** the structure to keep track of open requests on this channel */
370 	struct tcp_req_info* tcp_req_info;
371 
372 #ifdef USE_MSG_FASTOPEN
373 	/** used to track if the sendto() call should be done when using TFO. */
374 	int tcp_do_fastopen;
375 #endif
376 
377 #ifdef USE_DNSCRYPT
378 	/** Is this a dnscrypt channel */
379 	int dnscrypt;
380 	/** encrypted buffer pointer. Either to perthread, or own buffer or NULL */
381 	struct sldns_buffer* dnscrypt_buffer;
382 #endif
383 	/** number of queries outstanding on this socket, used by
384 	 * outside network for udp ports */
385 	int inuse;
386 
387 	/** callback when done.
388 	    tcp_accept does not get called back, is NULL then.
389 	    If a timeout happens, callback with timeout=1 is called.
390 	    If an error happens, callback is called with error set
391 	    nonzero. If not NETEVENT_NOERROR, it is an errno value.
392 	    If the connection is closed (by remote end) then the
393 	    callback is called with error set to NETEVENT_CLOSED=-1.
394 	    If a timeout happens on the connection, the error is set to
395 	    NETEVENT_TIMEOUT=-2.
396 	    The reply_info can be copied if the reply needs to happen at a
397 	    later time. It consists of a struct with commpoint and address.
398 	    It can be passed to a msg send routine some time later.
399 	    Note the reply information is temporary and must be copied.
400 	    NULL is passed for_reply info, in cases where error happened.
401 
402 	    declare as:
403 	    int my_callback(struct comm_point* c, void* my_arg, int error,
404 		struct comm_reply *reply_info);
405 
406 	    if the routine returns 0, nothing is done.
407 	    Notzero, the buffer will be sent back to client.
408 	    		For UDP this is done without changing the commpoint.
409 			In TCP it sets write state.
410 	*/
411 	comm_point_callback_type* callback;
412 	/** argument to pass to callback. */
413 	void *cb_arg;
414 };
415 
416 /**
417  * Structure only for making timeout events.
418  */
419 struct comm_timer {
420 	/** the internal event stuff (derived) */
421 	struct internal_timer* ev_timer;
422 
423 	/** callback function, takes user arg only */
424 	void (*callback)(void*);
425 
426 	/** callback user argument */
427 	void* cb_arg;
428 };
429 
430 /**
431  * Structure only for signal events.
432  */
433 struct comm_signal {
434 	/** the communication base */
435 	struct comm_base* base;
436 
437 	/** the internal event stuff */
438 	struct internal_signal* ev_signal;
439 
440 	/** callback function, takes signal number and user arg */
441 	void (*callback)(int, void*);
442 
443 	/** callback user argument */
444 	void* cb_arg;
445 };
446 
447 /**
448  * Create a new comm base.
449  * @param sigs: if true it attempts to create a default loop for
450  *   signal handling.
451  * @return: the new comm base. NULL on error.
452  */
453 struct comm_base* comm_base_create(int sigs);
454 
455 /**
456  * Create comm base that uses the given ub_event_base (underlying pluggable
457  * event mechanism pointer).
458  * @param base: underlying pluggable event base.
459  * @return: the new comm base. NULL on error.
460  */
461 struct comm_base* comm_base_create_event(struct ub_event_base* base);
462 
463 /**
464  * Delete comm base structure but not the underlying lib event base.
465  * All comm points must have been deleted.
466  * @param b: the base to delete.
467  */
468 void comm_base_delete_no_base(struct comm_base* b);
469 
470 /**
471  * Destroy a comm base.
472  * All comm points must have been deleted.
473  * @param b: the base to delete.
474  */
475 void comm_base_delete(struct comm_base* b);
476 
477 /**
478  * Obtain two pointers. The pointers never change (until base_delete()).
479  * The pointers point to time values that are updated regularly.
480  * @param b: the communication base that will update the time values.
481  * @param tt: pointer to time in seconds is returned.
482  * @param tv: pointer to time in microseconds is returned.
483  */
484 void comm_base_timept(struct comm_base* b, time_t** tt, struct timeval** tv);
485 
486 /**
487  * Dispatch the comm base events.
488  * @param b: the communication to perform.
489  */
490 void comm_base_dispatch(struct comm_base* b);
491 
492 /**
493  * Exit from dispatch loop.
494  * @param b: the communication base that is in dispatch().
495  */
496 void comm_base_exit(struct comm_base* b);
497 
498 /**
499  * Set the slow_accept mode handlers.  You can not provide these if you do
500  * not perform accept() calls.
501  * @param b: comm base
502  * @param stop_accept: function that stops listening to accept fds.
503  * @param start_accept: function that resumes listening to accept fds.
504  * @param arg: callback arg to pass to the functions.
505  */
506 void comm_base_set_slow_accept_handlers(struct comm_base* b,
507 	void (*stop_accept)(void*), void (*start_accept)(void*), void* arg);
508 
509 /**
510  * Access internal data structure (for util/tube.c on windows)
511  * @param b: comm base
512  * @return ub_event_base.
513  */
514 struct ub_event_base* comm_base_internal(struct comm_base* b);
515 
516 /**
517  * Create an UDP comm point. Calls malloc.
518  * setups the structure with the parameters you provide.
519  * @param base: in which base to alloc the commpoint.
520  * @param fd: file descriptor of open UDP socket.
521  * @param buffer: shared buffer by UDP sockets from this thread.
522  * @param pp2_enabled: if the comm point will support PROXYv2.
523  * @param callback: callback function pointer.
524  * @param callback_arg: will be passed to your callback function.
525  * @param socket: and opened socket properties will be passed to your callback function.
526  * @return: returns the allocated communication point. NULL on error.
527  * Sets timeout to NULL. Turns off TCP options.
528  */
529 struct comm_point* comm_point_create_udp(struct comm_base* base,
530 	int fd, struct sldns_buffer* buffer, int pp2_enabled,
531 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
532 
533 /**
534  * Create an UDP with ancillary data comm point. Calls malloc.
535  * Uses recvmsg instead of recv to get udp message.
536  * setups the structure with the parameters you provide.
537  * @param base: in which base to alloc the commpoint.
538  * @param fd: file descriptor of open UDP socket.
539  * @param buffer: shared buffer by UDP sockets from this thread.
540  * @param pp2_enabled: if the comm point will support PROXYv2.
541  * @param callback: callback function pointer.
542  * @param callback_arg: will be passed to your callback function.
543  * @param socket: and opened socket properties will be passed to your callback function.
544  * @return: returns the allocated communication point. NULL on error.
545  * Sets timeout to NULL. Turns off TCP options.
546  */
547 struct comm_point* comm_point_create_udp_ancil(struct comm_base* base,
548 	int fd, struct sldns_buffer* buffer, int pp2_enabled,
549 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
550 
551 /**
552  * Create a TCP listener comm point. Calls malloc.
553  * Setups the structure with the parameters you provide.
554  * Also Creates TCP Handlers, pre allocated for you.
555  * Uses the parameters you provide.
556  * @param base: in which base to alloc the commpoint.
557  * @param fd: file descriptor of open TCP socket set to listen nonblocking.
558  * @param num: becomes max_tcp_count, the routine allocates that
559  *	many tcp handler commpoints.
560  * @param idle_timeout: TCP idle timeout in ms.
561  * @param harden_large_queries: whether query size should be limited.
562  * @param http_max_streams: maximum number of HTTP/2 streams per connection.
563  * @param http_endpoint: HTTP endpoint to service queries on
564  * @param tcp_conn_limit: TCP connection limit info.
565  * @param bufsize: size of buffer to create for handlers.
566  * @param spoolbuf: shared spool buffer for tcp_req_info structures.
567  * 	or NULL to not create those structures in the tcp handlers.
568  * @param port_type: the type of port we are creating a TCP listener for. Used
569  * 	to select handler type to use.
570  * @param pp2_enabled: if the comm point will support PROXYv2.
571  * @param callback: callback function pointer for TCP handlers.
572  * @param callback_arg: will be passed to your callback function.
573  * @param socket: and opened socket properties will be passed to your callback function.
574  * @return: returns the TCP listener commpoint. You can find the
575  *  	TCP handlers in the array inside the listener commpoint.
576  *	returns NULL on error.
577  * Inits timeout to NULL. All handlers are on the free list.
578  */
579 struct comm_point* comm_point_create_tcp(struct comm_base* base,
580 	int fd, int num, int idle_timeout, int harden_large_queries,
581 	uint32_t http_max_streams, char* http_endpoint,
582 	struct tcl_list* tcp_conn_limit,
583 	size_t bufsize, struct sldns_buffer* spoolbuf,
584 	enum listen_type port_type, int pp2_enabled,
585 	comm_point_callback_type* callback, void* callback_arg, struct unbound_socket* socket);
586 
587 /**
588  * Create an outgoing TCP commpoint. No file descriptor is opened, left at -1.
589  * @param base: in which base to alloc the commpoint.
590  * @param bufsize: size of buffer to create for handlers.
591  * @param callback: callback function pointer for the handler.
592  * @param callback_arg: will be passed to your callback function.
593  * @return: the commpoint or NULL on error.
594  */
595 struct comm_point* comm_point_create_tcp_out(struct comm_base* base,
596 	size_t bufsize, comm_point_callback_type* callback, void* callback_arg);
597 
598 /**
599  * Create an outgoing HTTP commpoint. No file descriptor is opened, left at -1.
600  * @param base: in which base to alloc the commpoint.
601  * @param bufsize: size of buffer to create for handlers.
602  * @param callback: callback function pointer for the handler.
603  * @param callback_arg: will be passed to your callback function.
604  * @param temp: sldns buffer, shared between other http_out commpoints, for
605  * 	temporary data when performing callbacks.
606  * @return: the commpoint or NULL on error.
607  */
608 struct comm_point* comm_point_create_http_out(struct comm_base* base,
609 	size_t bufsize, comm_point_callback_type* callback,
610 	void* callback_arg, struct sldns_buffer* temp);
611 
612 /**
613  * Create commpoint to listen to a local domain file descriptor.
614  * @param base: in which base to alloc the commpoint.
615  * @param fd: file descriptor of open AF_UNIX socket set to listen nonblocking.
616  * @param bufsize: size of buffer to create for handlers.
617  * @param callback: callback function pointer for the handler.
618  * @param callback_arg: will be passed to your callback function.
619  * @return: the commpoint or NULL on error.
620  */
621 struct comm_point* comm_point_create_local(struct comm_base* base,
622 	int fd, size_t bufsize,
623 	comm_point_callback_type* callback, void* callback_arg);
624 
625 /**
626  * Create commpoint to listen to a local domain pipe descriptor.
627  * @param base: in which base to alloc the commpoint.
628  * @param fd: file descriptor.
629  * @param writing: true if you want to listen to writes, false for reads.
630  * @param callback: callback function pointer for the handler.
631  * @param callback_arg: will be passed to your callback function.
632  * @return: the commpoint or NULL on error.
633  */
634 struct comm_point* comm_point_create_raw(struct comm_base* base,
635 	int fd, int writing,
636 	comm_point_callback_type* callback, void* callback_arg);
637 
638 /**
639  * Close a comm point fd.
640  * @param c: comm point to close.
641  */
642 void comm_point_close(struct comm_point* c);
643 
644 /**
645  * Close and deallocate (free) the comm point. If the comm point is
646  * a tcp-accept point, also its tcp-handler points are deleted.
647  * @param c: comm point to delete.
648  */
649 void comm_point_delete(struct comm_point* c);
650 
651 /**
652  * Send reply. Put message into commpoint buffer.
653  * @param repinfo: The reply info copied from a commpoint callback call.
654  */
655 void comm_point_send_reply(struct comm_reply* repinfo);
656 
657 /**
658  * Drop reply. Cleans up.
659  * @param repinfo: The reply info copied from a commpoint callback call.
660  */
661 void comm_point_drop_reply(struct comm_reply* repinfo);
662 
663 /**
664  * Send an udp message over a commpoint.
665  * @param c: commpoint to send it from.
666  * @param packet: what to send.
667  * @param addr: where to send it to.   If NULL, send is performed,
668  * 	for connected sockets, to the connected address.
669  * @param addrlen: length of addr.
670  * @param is_connected: if the UDP socket is connect()ed.
671  * @return: false on a failure.
672  */
673 int comm_point_send_udp_msg(struct comm_point* c, struct sldns_buffer* packet,
674 	struct sockaddr* addr, socklen_t addrlen,int is_connected);
675 
676 /**
677  * Stop listening for input on the commpoint. No callbacks will happen.
678  * @param c: commpoint to disable. The fd is not closed.
679  */
680 void comm_point_stop_listening(struct comm_point* c);
681 
682 /**
683  * Start listening again for input on the comm point.
684  * @param c: commpoint to enable again.
685  * @param newfd: new fd, or -1 to leave fd be.
686  * @param msec: timeout in milliseconds, or -1 for no (change to the) timeout.
687  *	So seconds*1000.
688  */
689 void comm_point_start_listening(struct comm_point* c, int newfd, int msec);
690 
691 /**
692  * Stop listening and start listening again for reading or writing.
693  * @param c: commpoint
694  * @param rd: if true, listens for reading.
695  * @param wr: if true, listens for writing.
696  */
697 void comm_point_listen_for_rw(struct comm_point* c, int rd, int wr);
698 
699 /**
700  * For TCP handlers that use c->tcp_timeout_msec, this routine adjusts
701  * it with the minimum.  Otherwise, a 0 value advertised without the
702  * minimum applied moves to a 0 in comm_point_start_listening and that
703  * routine treats it as no timeout, listen forever, which is not wanted.
704  * @param c: comm point to use the tcp_timeout_msec of.
705  * @return adjusted tcp_timeout_msec value with the minimum if smaller.
706  */
707 int adjusted_tcp_timeout(struct comm_point* c);
708 
709 /**
710  * Get size of memory used by comm point.
711  * For TCP handlers this includes subhandlers.
712  * For UDP handlers, this does not include the (shared) UDP buffer.
713  * @param c: commpoint.
714  * @return size in bytes.
715  */
716 size_t comm_point_get_mem(struct comm_point* c);
717 
718 /**
719  * create timer. Not active upon creation.
720  * @param base: event handling base.
721  * @param cb: callback function: void myfunc(void* myarg);
722  * @param cb_arg: user callback argument.
723  * @return: the new timer or NULL on error.
724  */
725 struct comm_timer* comm_timer_create(struct comm_base* base,
726 	void (*cb)(void*), void* cb_arg);
727 
728 /**
729  * disable timer. Stops callbacks from happening.
730  * @param timer: to disable.
731  */
732 void comm_timer_disable(struct comm_timer* timer);
733 
734 /**
735  * reset timevalue for timer.
736  * @param timer: timer to (re)set.
737  * @param tv: when the timer should activate. if NULL timer is disabled.
738  */
739 void comm_timer_set(struct comm_timer* timer, struct timeval* tv);
740 
741 /**
742  * delete timer.
743  * @param timer: to delete.
744  */
745 void comm_timer_delete(struct comm_timer* timer);
746 
747 /**
748  * see if timeout has been set to a value.
749  * @param timer: the timer to examine.
750  * @return: false if disabled or not set.
751  */
752 int comm_timer_is_set(struct comm_timer* timer);
753 
754 /**
755  * Get size of memory used by comm timer.
756  * @param timer: the timer to examine.
757  * @return size in bytes.
758  */
759 size_t comm_timer_get_mem(struct comm_timer* timer);
760 
761 /**
762  * Create a signal handler. Call signal_bind() later to bind to a signal.
763  * @param base: communication base to use.
764  * @param callback: called when signal is caught.
765  * @param cb_arg: user argument to callback
766  * @return: the signal struct or NULL on error.
767  */
768 struct comm_signal* comm_signal_create(struct comm_base* base,
769 	void (*callback)(int, void*), void* cb_arg);
770 
771 /**
772  * Bind signal struct to catch a signal. A single comm_signal can be bound
773  * to multiple signals, calling comm_signal_bind multiple times.
774  * @param comsig: the communication point, with callback information.
775  * @param sig: signal number.
776  * @return: true on success. false on error.
777  */
778 int comm_signal_bind(struct comm_signal* comsig, int sig);
779 
780 /**
781  * Delete the signal communication point.
782  * @param comsig: to delete.
783  */
784 void comm_signal_delete(struct comm_signal* comsig);
785 
786 /**
787  * perform accept(2) with error checking.
788  * @param c: commpoint with accept fd.
789  * @param addr: remote end returned here.
790  * @param addrlen: length of remote end returned here.
791  * @return new fd, or -1 on error.
792  *	if -1, error message has been printed if necessary, simply drop
793  *	out of the reading handler.
794  */
795 int comm_point_perform_accept(struct comm_point* c,
796 	struct sockaddr_storage* addr, socklen_t* addrlen);
797 
798 /**** internal routines ****/
799 
800 /**
801  * This routine is published for checks and tests, and is only used internally.
802  * handle libevent callback for udp comm point.
803  * @param fd: file descriptor.
804  * @param event: event bits from libevent:
805  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
806  * @param arg: the comm_point structure.
807  */
808 void comm_point_udp_callback(int fd, short event, void* arg);
809 
810 /**
811  * This routine is published for checks and tests, and is only used internally.
812  * handle libevent callback for udp ancillary data comm point.
813  * @param fd: file descriptor.
814  * @param event: event bits from libevent:
815  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
816  * @param arg: the comm_point structure.
817  */
818 void comm_point_udp_ancil_callback(int fd, short event, void* arg);
819 
820 /**
821  * This routine is published for checks and tests, and is only used internally.
822  * handle libevent callback for tcp accept comm point
823  * @param fd: file descriptor.
824  * @param event: event bits from libevent:
825  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
826  * @param arg: the comm_point structure.
827  */
828 void comm_point_tcp_accept_callback(int fd, short event, void* arg);
829 
830 /**
831  * This routine is published for checks and tests, and is only used internally.
832  * handle libevent callback for tcp data comm point
833  * @param fd: file descriptor.
834  * @param event: event bits from libevent:
835  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
836  * @param arg: the comm_point structure.
837  */
838 void comm_point_tcp_handle_callback(int fd, short event, void* arg);
839 
840 /**
841  * This routine is published for checks and tests, and is only used internally.
842  * handle libevent callback for tcp data comm point
843  * @param fd: file descriptor.
844  * @param event: event bits from libevent:
845  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
846  * @param arg: the comm_point structure.
847  */
848 void comm_point_http_handle_callback(int fd, short event, void* arg);
849 
850 /**
851  * HTTP2 session.  HTTP2 related info per comm point.
852  */
853 struct http2_session {
854 	/** first item in list of streams */
855 	struct http2_stream* first_stream;
856 #ifdef HAVE_NGHTTP2
857 	/** nghttp2 session */
858 	nghttp2_session *session;
859 	/** store nghttp2 callbacks for easy reuse */
860 	nghttp2_session_callbacks* callbacks;
861 #endif
862 	/** comm point containing buffer used to build answer in worker or
863 	 * module */
864 	struct comm_point* c;
865 	/** session is instructed to get dropped (comm port will be closed) */
866 	int is_drop;
867 	/** postpone dropping the session, can be used to prevent dropping
868 	 * while being in a callback */
869 	int postpone_drop;
870 };
871 
872 /** enum of HTTP status */
873 enum http_status {
874 	HTTP_STATUS_OK = 200,
875 	HTTP_STATUS_BAD_REQUEST = 400,
876 	HTTP_STATUS_NOT_FOUND = 404,
877 	HTTP_STATUS_PAYLOAD_TOO_LARGE = 413,
878 	HTTP_STATUS_URI_TOO_LONG = 414,
879 	HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE = 415,
880 	HTTP_STATUS_NOT_IMPLEMENTED = 501
881 };
882 
883 /**
884  * HTTP stream. Part of list of HTTP2 streams per session.
885  */
886 struct http2_stream {
887 	/** next stream in list per session */
888 	struct http2_stream* next;
889 	/** previous stream in list per session */
890 	struct http2_stream* prev;
891 	/** HTTP2 stream ID is an unsigned 31-bit integer */
892 	int32_t stream_id;
893 	/** HTTP method used for this stream */
894 	enum {
895 		HTTP_METHOD_POST = 1,
896 		HTTP_METHOD_GET,
897 		HTTP_METHOD_UNSUPPORTED
898 	} http_method;
899 	/** message contains invalid content type */
900 	int invalid_content_type;
901 	/** message body content type */
902 	size_t content_length;
903 	/** HTTP response status */
904 	enum http_status status;
905 	/** request for non existing endpoint */
906 	int invalid_endpoint;
907 	/** query in request is too large */
908 	int query_too_large;
909 	/** buffer to store query into. Can't use session shared buffer as query
910 	 * can arrive in parts, intertwined with frames for other queries. */
911 	struct sldns_buffer* qbuffer;
912 	/** buffer to store response into. Can't use shared buffer as a next
913 	 * query read callback can overwrite it before it is send out. */
914 	struct sldns_buffer* rbuffer;
915 	/** mesh area containing mesh state */
916 	struct mesh_area* mesh;
917 	/** mesh state for query. Used to remove mesh reply before closing
918 	 * stream. */
919 	struct mesh_state* mesh_state;
920 };
921 
922 #ifdef HAVE_NGHTTP2
923 /** nghttp2 receive cb. Read from SSL connection into nghttp2 buffer */
924 ssize_t http2_recv_cb(nghttp2_session* session, uint8_t* buf,
925 	size_t len, int flags, void* cb_arg);
926 /** nghttp2 send callback. Send from nghttp2 buffer to ssl socket */
927 ssize_t http2_send_cb(nghttp2_session* session, const uint8_t* buf,
928 	size_t len, int flags, void* cb_arg);
929 /** nghttp2 callback on closing stream */
930 int http2_stream_close_cb(nghttp2_session* session, int32_t stream_id,
931 	uint32_t error_code, void* cb_arg);
932 #endif
933 
934 /**
935  * Create new http2 stream
936  * @param stream_id: ID for stream to create.
937  * @return malloc'ed stream, NULL on error
938  */
939 struct http2_stream* http2_stream_create(int32_t stream_id);
940 
941 /**
942  * Add new stream to session linked list
943  * @param h2_session: http2 session to add stream to
944  * @param h2_stream: stream to add to session list
945  */
946 void http2_session_add_stream(struct http2_session* h2_session,
947 	struct http2_stream* h2_stream);
948 
949 /** Add mesh state to stream. To be able to remove mesh reply on stream closure
950  */
951 void http2_stream_add_meshstate(struct http2_stream* h2_stream,
952 	struct mesh_area* mesh, struct mesh_state* m);
953 
954 /**
955  * This routine is published for checks and tests, and is only used internally.
956  * handle libevent callback for timer comm.
957  * @param fd: file descriptor (always -1).
958  * @param event: event bits from libevent:
959  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
960  * @param arg: the comm_timer structure.
961  */
962 void comm_timer_callback(int fd, short event, void* arg);
963 
964 /**
965  * This routine is published for checks and tests, and is only used internally.
966  * handle libevent callback for signal comm.
967  * @param fd: file descriptor (used for the signal number).
968  * @param event: event bits from libevent:
969  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
970  * @param arg: the internal commsignal structure.
971  */
972 void comm_signal_callback(int fd, short event, void* arg);
973 
974 /**
975  * This routine is published for checks and tests, and is only used internally.
976  * libevent callback for AF_UNIX fds
977  * @param fd: file descriptor.
978  * @param event: event bits from libevent:
979  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
980  * @param arg: the comm_point structure.
981  */
982 void comm_point_local_handle_callback(int fd, short event, void* arg);
983 
984 /**
985  * This routine is published for checks and tests, and is only used internally.
986  * libevent callback for raw fd access.
987  * @param fd: file descriptor.
988  * @param event: event bits from libevent:
989  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
990  * @param arg: the comm_point structure.
991  */
992 void comm_point_raw_handle_callback(int fd, short event, void* arg);
993 
994 /**
995  * This routine is published for checks and tests, and is only used internally.
996  * libevent callback for timeout on slow accept.
997  * @param fd: file descriptor.
998  * @param event: event bits from libevent:
999  *	EV_READ, EV_WRITE, EV_SIGNAL, EV_TIMEOUT.
1000  * @param arg: the comm_point structure.
1001  */
1002 void comm_base_handle_slow_accept(int fd, short event, void* arg);
1003 
1004 #ifdef USE_WINSOCK
1005 /**
1006  * Callback for openssl BIO to on windows detect WSAEWOULDBLOCK and notify
1007  * the winsock_event of this for proper TCP nonblocking implementation.
1008  * @param c: comm_point, fd must be set its struct event is registered.
1009  * @param ssl: openssl SSL, fd must be set so it has a bio.
1010  */
1011 void comm_point_tcp_win_bio_cb(struct comm_point* c, void* ssl);
1012 #endif
1013 
1014 /**
1015  * See if errno for tcp connect has to be logged or not. This uses errno
1016  * @param addr: apart from checking errno, the addr is checked for ip4mapped
1017  * 	and broadcast type, hence passed.
1018  * @param addrlen: length of the addr parameter.
1019  * @return true if it needs to be logged.
1020  */
1021 int tcp_connect_errno_needs_log(struct sockaddr* addr, socklen_t addrlen);
1022 
1023 #ifdef HAVE_SSL
1024 /**
1025  * True if the ssl handshake error has to be squelched from the logs
1026  * @param err: the error returned by the openssl routine, ERR_get_error.
1027  * 	This is a packed structure with elements that are examined.
1028  * @return true if the error is squelched (not logged).
1029  */
1030 int squelch_err_ssl_handshake(unsigned long err);
1031 #endif
1032 
1033 #endif /* NET_EVENT_H */
1034