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