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